| 1 | // | 
|---|
| 2 | // function.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 | #ifdef __GNUC__ | 
|---|
| 29 | #pragma interface | 
|---|
| 30 | #endif | 
|---|
| 31 |  | 
|---|
| 32 | #ifndef _math_optimize_function_h | 
|---|
| 33 | #define _math_optimize_function_h | 
|---|
| 34 |  | 
|---|
| 35 | #include <math.h> | 
|---|
| 36 | #include <float.h> | 
|---|
| 37 |  | 
|---|
| 38 | #include <util/state/state.h> | 
|---|
| 39 | #include <math/optimize/transform.h> | 
|---|
| 40 | #include <math/scmat/matrix.h> | 
|---|
| 41 | #include <math/scmat/result.h> | 
|---|
| 42 |  | 
|---|
| 43 | namespace sc { | 
|---|
| 44 |  | 
|---|
| 45 | /** The Function class is an abstract base class that, | 
|---|
| 46 | given a set of coordinates, will compute a value and possibly | 
|---|
| 47 | a gradient and hessian at that point. */ | 
|---|
| 48 | class Function: virtual public SavableState, public Compute { | 
|---|
| 49 | protected: | 
|---|
| 50 | Ref<SCMatrixKit> matrixkit_;          ///< Used to construct new matrices. | 
|---|
| 51 |  | 
|---|
| 52 | RefSCVector x_;                     ///< The variables. | 
|---|
| 53 | RefSCDimension dim_;                ///< The dimension of x_. | 
|---|
| 54 | AccResultdouble value_;             ///< The value of the function at x_. | 
|---|
| 55 | AccResultRefSCVector gradient_;     ///< The gradient at x_ | 
|---|
| 56 | AccResultRefSymmSCMatrix hessian_;  ///< The hessian at x_. | 
|---|
| 57 |  | 
|---|
| 58 | /** @name Update Members | 
|---|
| 59 | Update the various computable results. | 
|---|
| 60 | */ | 
|---|
| 61 | //@{ | 
|---|
| 62 | virtual void set_value(double); | 
|---|
| 63 | virtual void set_gradient(RefSCVector&); | 
|---|
| 64 | virtual void set_hessian(RefSymmSCMatrix&); | 
|---|
| 65 | //@} | 
|---|
| 66 |  | 
|---|
| 67 | /** Set the SCMatrixKit that should be used to | 
|---|
| 68 | construct the requisite vectors and matrices. */ | 
|---|
| 69 | virtual void set_matrixkit(const Ref<SCMatrixKit>&); | 
|---|
| 70 | virtual void set_dimension(const RefSCDimension&); | 
|---|
| 71 |  | 
|---|
| 72 | /** @name Accuracy Setting Members | 
|---|
| 73 | Set the accuracies with which the various computables | 
|---|
| 74 | have been computed. */ | 
|---|
| 75 | //@{ | 
|---|
| 76 | virtual void set_actual_value_accuracy(double); | 
|---|
| 77 | virtual void set_actual_gradient_accuracy(double); | 
|---|
| 78 | virtual void set_actual_hessian_accuracy(double); | 
|---|
| 79 | //@} | 
|---|
| 80 |  | 
|---|
| 81 | /// Get read/write access to the coordinates for modification. | 
|---|
| 82 | RefSCVector& get_x_reference() { obsolete(); return x_; } | 
|---|
| 83 |  | 
|---|
| 84 | /** Change the coordinate system and apply the given transform to | 
|---|
| 85 | intermediates matrices and vectors. */ | 
|---|
| 86 | void do_change_coordinates(const Ref<NonlinearTransform>&); | 
|---|
| 87 | public: | 
|---|
| 88 | Function(); | 
|---|
| 89 | Function(StateIn&); | 
|---|
| 90 | Function(const Function&); | 
|---|
| 91 |  | 
|---|
| 92 | /** The keyval constructor reads the following keywords: | 
|---|
| 93 | <dl> | 
|---|
| 94 |  | 
|---|
| 95 | <dt><tt>matrixkit</tt><dd> Gives a SCMatrixKit | 
|---|
| 96 | object.  If it is not specified, a default SCMatrixKit is selected. | 
|---|
| 97 |  | 
|---|
| 98 | <dt><tt>value_accuracy</tt><dd> Sets the accuracy to which values are | 
|---|
| 99 | computed.  The default is the machine accuracy. | 
|---|
| 100 |  | 
|---|
| 101 | <dt><tt>gradient_accuracy</tt><dd> Sets the accuracy to which | 
|---|
| 102 | gradients are computed.  The default is the machine accuracy. | 
|---|
| 103 |  | 
|---|
| 104 | <dt><tt>hessian_accuracy</tt><dd> Sets the accuracy to which | 
|---|
| 105 | hessians are computed.  The default is the machine accuracy. | 
|---|
| 106 |  | 
|---|
| 107 | </dl> */ | 
|---|
| 108 | Function(const Ref<KeyVal>&, double funcacc = DBL_EPSILON, | 
|---|
| 109 | double gradacc = DBL_EPSILON, double hessacc = DBL_EPSILON); | 
|---|
| 110 | virtual ~Function(); | 
|---|
| 111 |  | 
|---|
| 112 | Function & operator=(const Function&); | 
|---|
| 113 |  | 
|---|
| 114 | /** Return the SCMatrixKit used to construct | 
|---|
| 115 | vectors and matrices. */ | 
|---|
| 116 | Ref<SCMatrixKit> matrixkit() const; | 
|---|
| 117 | /// Return the SCDimension of the problem. | 
|---|
| 118 | RefSCDimension dimension() const; | 
|---|
| 119 |  | 
|---|
| 120 | virtual void save_data_state(StateOut&); | 
|---|
| 121 |  | 
|---|
| 122 | /// Return the value of the function. | 
|---|
| 123 | virtual double value(); | 
|---|
| 124 | /// Returns nonzero if the current value is not up-to-date. | 
|---|
| 125 | int value_needed() const; | 
|---|
| 126 | /** If passed a nonzero number, compute the value the next | 
|---|
| 127 | time compute() is called.  Return a nonzero number | 
|---|
| 128 | if the value was previously to be computed. */ | 
|---|
| 129 | int do_value(int); | 
|---|
| 130 | AccResultdouble& value_result() { return value_; } | 
|---|
| 131 |  | 
|---|
| 132 | /// Set the accuracy to which the value is to be computed. | 
|---|
| 133 | virtual void set_desired_value_accuracy(double); | 
|---|
| 134 | /// Return the accuracy with which the value has been computed. | 
|---|
| 135 | virtual double actual_value_accuracy() const; | 
|---|
| 136 | /// Return the accuracy with which the value is to be computed. | 
|---|
| 137 | virtual double desired_value_accuracy() const; | 
|---|
| 138 |  | 
|---|
| 139 | /** @name Gradient Members | 
|---|
| 140 | These are analogous to the routines that deal with values, | 
|---|
| 141 | but work with gradients instead. */ | 
|---|
| 142 | //@{ | 
|---|
| 143 | virtual RefSCVector gradient(); | 
|---|
| 144 | int gradient_needed() const; | 
|---|
| 145 | int do_gradient(int); | 
|---|
| 146 | virtual void set_desired_gradient_accuracy(double); | 
|---|
| 147 | virtual double actual_gradient_accuracy() const; | 
|---|
| 148 | virtual double desired_gradient_accuracy() const; | 
|---|
| 149 | AccResultRefSCVector& gradient_result() { return gradient_; } | 
|---|
| 150 | //@} | 
|---|
| 151 |  | 
|---|
| 152 | /** @name Hessian Members | 
|---|
| 153 | These are analogous to the routines that deal with values, | 
|---|
| 154 | but work with the hessian instead. */ | 
|---|
| 155 | //@{ | 
|---|
| 156 | virtual RefSymmSCMatrix hessian(); | 
|---|
| 157 | int hessian_needed() const; | 
|---|
| 158 | int do_hessian(int); | 
|---|
| 159 | virtual void set_desired_hessian_accuracy(double); | 
|---|
| 160 | virtual double actual_hessian_accuracy() const; | 
|---|
| 161 | virtual double desired_hessian_accuracy() const; | 
|---|
| 162 | AccResultRefSymmSCMatrix& hessian_result() { return hessian_; } | 
|---|
| 163 | //@} | 
|---|
| 164 |  | 
|---|
| 165 | // hessian by gradients at finite displacements | 
|---|
| 166 | // virtual RefSCMatrix fd1_hessian(); | 
|---|
| 167 |  | 
|---|
| 168 | /// Compute a quick, approximate hessian. | 
|---|
| 169 | virtual void guess_hessian(RefSymmSCMatrix&); | 
|---|
| 170 | virtual RefSymmSCMatrix inverse_hessian(RefSymmSCMatrix&); | 
|---|
| 171 |  | 
|---|
| 172 | /** Information about the availability of values, gradients, | 
|---|
| 173 | and hessians. */ | 
|---|
| 174 | virtual int value_implemented() const; | 
|---|
| 175 | virtual int gradient_implemented() const; | 
|---|
| 176 | virtual int hessian_implemented() const; | 
|---|
| 177 |  | 
|---|
| 178 | /// Set and retrieve the coordinate values. | 
|---|
| 179 | virtual void set_x(const RefSCVector&); | 
|---|
| 180 | RefSCVector get_x() const { return x_.copy(); } | 
|---|
| 181 | const RefSCVector& get_x_no_copy() const { return x_; } | 
|---|
| 182 |  | 
|---|
| 183 | /** An optimizer can call change coordinates periodically to give the | 
|---|
| 184 | function an opportunity to change its coordinate system.  A return | 
|---|
| 185 | value of 0 means the coordinates were not changed.  Otherwise, a | 
|---|
| 186 | transform object to the new coordinate system is return.  The | 
|---|
| 187 | function object applies the transform to any objects it contains. | 
|---|
| 188 | This will obsolete the function data. */ | 
|---|
| 189 | virtual Ref<NonlinearTransform> change_coordinates(); | 
|---|
| 190 |  | 
|---|
| 191 | /// Print information about the object. | 
|---|
| 192 | virtual void print(std::ostream& = ExEnv::out0()) const; | 
|---|
| 193 | }; | 
|---|
| 194 |  | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | #endif | 
|---|
| 198 |  | 
|---|
| 199 | // Local Variables: | 
|---|
| 200 | // mode: c++ | 
|---|
| 201 | // c-file-style: "CLJ" | 
|---|
| 202 | // End: | 
|---|