| [0b990d] | 1 | //
 | 
|---|
 | 2 | // opt.h
 | 
|---|
 | 3 | //
 | 
|---|
 | 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
 | 
|---|
 | 5 | //
 | 
|---|
 | 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
 | 
|---|
 | 7 | // Maintainer: LPS
 | 
|---|
 | 8 | //
 | 
|---|
 | 9 | // This file is part of the SC Toolkit.
 | 
|---|
 | 10 | //
 | 
|---|
 | 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
 | 
|---|
 | 12 | // it under the terms of the GNU Library General Public License as published by
 | 
|---|
 | 13 | // the Free Software Foundation; either version 2, or (at your option)
 | 
|---|
 | 14 | // any later version.
 | 
|---|
 | 15 | //
 | 
|---|
 | 16 | // The SC Toolkit is distributed in the hope that it will be useful,
 | 
|---|
 | 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 19 | // GNU Library General Public License for more details.
 | 
|---|
 | 20 | //
 | 
|---|
 | 21 | // You should have received a copy of the GNU Library General Public License
 | 
|---|
 | 22 | // along with the SC Toolkit; see the file COPYING.LIB.  If not, write to
 | 
|---|
 | 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 | 
|---|
 | 24 | //
 | 
|---|
 | 25 | // The U.S. Government is granted a limited license as per AL 91-7.
 | 
|---|
 | 26 | //
 | 
|---|
 | 27 | 
 | 
|---|
 | 28 | #ifndef _math_optimize_opt_h
 | 
|---|
 | 29 | #define _math_optimize_opt_h
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | #ifdef __GNUC__
 | 
|---|
 | 32 | #pragma interface
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #include <util/state/state.h>
 | 
|---|
 | 36 | #include <util/class/class.h>
 | 
|---|
 | 37 | #include <math/scmat/matrix.h>
 | 
|---|
 | 38 | #include <math/optimize/function.h>
 | 
|---|
 | 39 | #include <math/optimize/conv.h>
 | 
|---|
 | 40 | 
 | 
|---|
 | 41 | namespace sc {
 | 
|---|
 | 42 | 
 | 
|---|
 | 43 | // //////////////////////////////////////////////////////////////////////
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | /** The Optimize class is an abstract base class for classes
 | 
|---|
 | 46 |     that find the extreme points of Function's. */
 | 
|---|
 | 47 | class Optimize: virtual public SavableState {
 | 
|---|
 | 48 |   protected:
 | 
|---|
 | 49 |     int max_iterations_;
 | 
|---|
 | 50 |     int n_iterations_;
 | 
|---|
 | 51 |     int ckpt_;
 | 
|---|
 | 52 |     int print_timings_;
 | 
|---|
 | 53 |     double max_stepsize_;
 | 
|---|
 | 54 |     char *ckpt_file;
 | 
|---|
 | 55 |     Ref<Function> function_;
 | 
|---|
 | 56 |     Ref<Convergence> conv_;
 | 
|---|
 | 57 |   public:
 | 
|---|
 | 58 |     Optimize();
 | 
|---|
 | 59 |     /// Restore the state of a Function object.
 | 
|---|
 | 60 |     Optimize(StateIn&);
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |     /** The KeyVal constructor reads the following information:
 | 
|---|
 | 63 | 
 | 
|---|
 | 64 |         <dl>
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |         <dt><tt>checkpoint</tt><dd> If true, the optimization will be
 | 
|---|
 | 67 |         checkpointed.  The default is false.
 | 
|---|
 | 68 | 
 | 
|---|
 | 69 |         <dt><tt>checkpoint_file</tt><dd> The name of the checkpoint file.
 | 
|---|
 | 70 |         The name defaults to opt_ckpt.dat.
 | 
|---|
 | 71 | 
 | 
|---|
 | 72 |         <dt><tt>max_iterations</tt><dd> The maximum number of interations.
 | 
|---|
 | 73 |         The default is 10.
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 |         <dt><tt>max_stepsize</tt><dd> The maximum stepsize.  The default is
 | 
|---|
 | 76 |         0.6.
 | 
|---|
 | 77 | 
 | 
|---|
 | 78 |         <dt><tt>function</tt><dd> A Function object.  There is no default.
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 |         <dt><tt>convergence</tt><dd> This can be either a floating point
 | 
|---|
 | 81 |         number or a Convergence object.  If it is a floating point number
 | 
|---|
 | 82 |         then it is the convergence criterion.  See the description
 | 
|---|
 | 83 |         Convergence class for the default.
 | 
|---|
 | 84 | 
 | 
|---|
 | 85 |         </dl> */
 | 
|---|
 | 86 |     Optimize(const Ref<KeyVal>&);
 | 
|---|
 | 87 |     virtual ~Optimize();
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |     void save_data_state(StateOut&);
 | 
|---|
 | 90 | 
 | 
|---|
 | 91 |     /** Do the optimization.  Returns nonzero if the optimization
 | 
|---|
 | 92 |         is complete. */
 | 
|---|
 | 93 |     virtual int optimize();
 | 
|---|
 | 94 | 
 | 
|---|
 | 95 |     /// Set up for checkpointing.
 | 
|---|
 | 96 |     void set_checkpoint();
 | 
|---|
 | 97 |     void set_checkpoint_file(const char*);
 | 
|---|
 | 98 | 
 | 
|---|
 | 99 |     /// Set the function to be optimized
 | 
|---|
 | 100 |     void set_function(const Ref<Function>&);
 | 
|---|
 | 101 |     
 | 
|---|
 | 102 |     /// Set the iteration limit.
 | 
|---|
 | 103 |     void set_max_iterations(int);
 | 
|---|
 | 104 |   
 | 
|---|
 | 105 |     /// Initialize the optimizer.
 | 
|---|
 | 106 |     virtual void init();
 | 
|---|
 | 107 |     /** Take a step.  Returns 1 if the optimization has converged,
 | 
|---|
 | 108 |         otherwise 0. */
 | 
|---|
 | 109 |     virtual int update() = 0;
 | 
|---|
 | 110 | 
 | 
|---|
 | 111 |     virtual void apply_transform(const Ref<NonlinearTransform>&);
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |     /// Returns information about the Function being optimized.
 | 
|---|
 | 114 |     Ref<Function> function() const { return function_; }
 | 
|---|
 | 115 |     Ref<SCMatrixKit> matrixkit() const { return function_->matrixkit(); }
 | 
|---|
 | 116 |     RefSCDimension dimension() const { return function_->dimension(); }
 | 
|---|
 | 117 | 
 | 
|---|
 | 118 | };
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | /** The LineOpt abstract class is used to perform one dimensional
 | 
|---|
 | 122 | optimizations.*/
 | 
|---|
 | 123 | class LineOpt: public Optimize {
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |   protected:
 | 
|---|
 | 126 | 
 | 
|---|
 | 127 |     double decrease_factor_;
 | 
|---|
 | 128 |     RefSCVector initial_x_;
 | 
|---|
 | 129 |     double initial_value_;
 | 
|---|
 | 130 |     RefSCVector initial_grad_;
 | 
|---|
 | 131 |     RefSCVector search_direction_;
 | 
|---|
 | 132 |     Ref<Function> function_;
 | 
|---|
 | 133 |     
 | 
|---|
 | 134 |     int sufficient_decrease(RefSCVector& step);
 | 
|---|
 | 135 | 
 | 
|---|
 | 136 |   public:
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 |     LineOpt();
 | 
|---|
 | 139 |     LineOpt(StateIn&);
 | 
|---|
 | 140 |     LineOpt(const Ref<KeyVal>&);
 | 
|---|
 | 141 |     ~LineOpt();
 | 
|---|
 | 142 |     void save_data_state(StateOut&);
 | 
|---|
 | 143 | 
 | 
|---|
 | 144 |     /** Initializes the line search object. Argument is a search direction.
 | 
|---|
 | 145 |       * Use of this method assumes the Optimize base class already has a 
 | 
|---|
 | 146 |       * function object (got it from a keyval or elsewhere). */
 | 
|---|
 | 147 |     virtual void init(RefSCVector& direction);
 | 
|---|
 | 148 |     /** Initializes the line search object. First argument is a search 
 | 
|---|
 | 149 |       * direction, second argument is a function object to optimize.
 | 
|---|
 | 150 |       * Use this method when a function must be passed to the Optimize 
 | 
|---|
 | 151 |       * base class. */
 | 
|---|
 | 152 |     virtual void init(RefSCVector& direction, Ref<Function> function);
 | 
|---|
 | 153 |     /// Applies a nonlinear transform.
 | 
|---|
 | 154 |     void apply_transform(const Ref<NonlinearTransform>&);
 | 
|---|
 | 155 |   
 | 
|---|
 | 156 |     /// Returns factor for sufficient decrease test
 | 
|---|
 | 157 |     double decrease_factor() { return decrease_factor_; }
 | 
|---|
 | 158 |     /// Sets factor for sufficient decrease test
 | 
|---|
 | 159 |     double set_decrease_factor( double factor ) 
 | 
|---|
 | 160 |     { double temp = decrease_factor_; decrease_factor_ = factor; return temp; }
 | 
|---|
 | 161 | };
 | 
|---|
 | 162 | 
 | 
|---|
 | 163 | class Backtrack: public LineOpt {
 | 
|---|
 | 164 | 
 | 
|---|
 | 165 |  protected:
 | 
|---|
 | 166 |    double backtrack_factor_;
 | 
|---|
 | 167 | 
 | 
|---|
 | 168 |  public:
 | 
|---|
 | 169 |    Backtrack(const Ref<KeyVal>&);
 | 
|---|
 | 170 |    ~Backtrack(){}
 | 
|---|
 | 171 |    int update();
 | 
|---|
 | 172 | 
 | 
|---|
 | 173 | };
 | 
|---|
 | 174 | 
 | 
|---|
 | 175 | }
 | 
|---|
 | 176 | 
 | 
|---|
 | 177 | #endif
 | 
|---|
 | 178 | 
 | 
|---|
 | 179 | // Local Variables:
 | 
|---|
 | 180 | // mode: c++
 | 
|---|
 | 181 | // c-file-style: "CLJ"
 | 
|---|
 | 182 | // End:
 | 
|---|