Simbody  3.6
OptimizerRep.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATH_OPTIMIZER_REP_H_
2 #define SimTK_SIMMATH_OPTIMIZER_REP_H_
3 
4 /* -------------------------------------------------------------------------- *
5  * Simbody(tm): SimTKmath *
6  * -------------------------------------------------------------------------- *
7  * This is part of the SimTK biosimulation toolkit originating from *
8  * Simbios, the NIH National Center for Physics-Based Simulation of *
9  * Biological Structures at Stanford, funded under the NIH Roadmap for *
10  * Medical Research, grant U54 GM072970. See https://simtk.org/home/simbody. *
11  * *
12  * Portions copyright (c) 2006-13 Stanford University and the Authors. *
13  * Authors: Jack Middleton *
14  * Contributors: Michael Sherman *
15  * *
16  * Licensed under the Apache License, Version 2.0 (the "License"); you may *
17  * not use this file except in compliance with the License. You may obtain a *
18  * copy of the License at http://www.apache.org/licenses/LICENSE-2.0. *
19  * *
20  * Unless required by applicable law or agreed to in writing, software *
21  * distributed under the License is distributed on an "AS IS" BASIS, *
22  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
23  * See the License for the specific language governing permissions and *
24  * limitations under the License. *
25  * -------------------------------------------------------------------------- */
26 
27 #include "SimTKcommon.h"
28 #include "simmath/Optimizer.h"
29 #include "simmath/Differentiator.h"
30 #include <map>
31 
32 namespace SimTK {
33 
34 
35 /* class for Diff jacobian */
37 public:
38  SysObjectiveFunc(int ny, const OptimizerSystem* sysPtr )
39  : Differentiator::GradientFunction(ny) { sysp = sysPtr; }
40 
41  // Must provide this pure virtual function.
42  int f(const Vector& y, Real& fy) const override {
43  return(sysp->objectiveFunc(y, true, fy)); // class user's objectiveFunc
44  }
46 };
47 
48 
49 /* class for Diff gradient */
51  public:
52  SysConstraintFunc(int nf, int ny, const OptimizerSystem* sysPtr)
53  : Differentiator::JacobianFunction(nf,ny) { sysp = sysPtr; }
54 
55  // Must provide this pure virtual function.
56  int f(const Vector& y, Vector& fy) const override {
57  return(sysp->constraintFunc(y, true, fy)); // calls user's contraintFunc
58  }
60 };
61 
62 
64 public:
65  virtual ~OptimizerRep();
67  : sysp(&sys),
68  myHandle(0),
69  cf(0),
70  of(0),
71  jacDiff(0),
72  gradDiff(0),
73  convergenceTolerance(Real(1e-3)),
74  constraintTolerance(Real(1e-4)),
75  maxIterations(1000),
76  limitedMemoryHistory(50),
77  diagnosticsLevel(0),
78  diffMethod(Differentiator::CentralDifference),
79  objectiveEstimatedAccuracy(SignificantReal),
80  constraintsEstimatedAccuracy(SignificantReal),
81  numericalGradient(false),
82  numericalJacobian(false)
83 
84  {
85  }
87  : sysp(0),
88  myHandle(0),
89  cf(0),
90  of(0),
91  jacDiff(0),
92  gradDiff(0),
93  convergenceTolerance(Real(1e-3)),
94  constraintTolerance(Real(1e-4)),
95  maxIterations(1000),
96  limitedMemoryHistory(50),
97  diagnosticsLevel(0),
98  diffMethod(Differentiator::CentralDifference),
99  objectiveEstimatedAccuracy(SignificantReal),
100  constraintsEstimatedAccuracy(SignificantReal),
101  numericalGradient(false),
102  numericalJacobian(false)
103  {
104  }
105 
106  virtual OptimizerRep* clone() const { return 0; };
107  static bool isAvailable() { return true; }
108 
109  virtual Real optimize( Vector &results ) = 0;
110 
111  const OptimizerSystem& getOptimizerSystem() const {return *sysp;}
112 
113 
114  void setDiagnosticsLevel( const int level );
115  void setConvergenceTolerance( Real accuracy );
116  void setConstraintTolerance( Real tolerance );
117  void setMaxIterations( const int iter );
118  void setLimitedMemoryHistory( const int history );
119 
120  bool setAdvancedStrOption( const std::string &option, const std::string &value );
121  bool setAdvancedRealOption( const std::string &option, const Real value );
122  bool setAdvancedIntOption( const std::string &option, const int value );
123  bool setAdvancedBoolOption( const std::string &option, const bool value );
124  bool setAdvancedVectorOption( const std::string &option, const Vector value );
125 
126  bool getAdvancedStrOption( const std::string &option, std::string &value ) const;
127  bool getAdvancedRealOption( const std::string &option, Real &value ) const;
128  bool getAdvancedIntOption( const std::string &option, int &value ) const;
129  bool getAdvancedBoolOption( const std::string &option, bool &value ) const;
130  bool getAdvancedVectorOption( const std::string &option, Vector &value ) const;
131 
132  void setMyHandle(Optimizer& cp) {myHandle = &cp;}
133  const Optimizer& getMyHandle() const {assert(myHandle); return *myHandle;}
134  void clearMyHandle() {myHandle=0;}
135 
136  void useNumericalGradient(bool flag, Real objEstAccuracy);
137  void useNumericalJacobian(bool flag, Real consEstAccuracy);
138  void setDifferentiatorMethod( Differentiator::Method method);
139 
140  bool isUsingNumericalGradient() const { return numericalGradient; }
141  bool isUsingNumericalJacobian() const { return numericalJacobian; }
142  Differentiator::Method getDifferentiatorMethod() const {return diffMethod;}
144  { return objectiveEstimatedAccuracy; }
146  { return constraintsEstimatedAccuracy; }
147 
149  assert(gradDiff);
150  return *gradDiff;
151  }
152 
154  assert(jacDiff);
155  return *jacDiff;
156  }
157 
160  }
161 
162  static int numericalGradient_static( const OptimizerSystem&, const Vector & parameters, const bool new_parameters, Vector &gradient );
163  static int numericalJacobian_static(const OptimizerSystem&,
164  const Vector& parameters, const bool new_parameters, Matrix& jacobian );
165 
166 protected:
167  // These methods are to be called by derived classes as an interface
168  // to the OptimizerSystem virtuals. The signature must match that required by
169  // IpOpt's matching callbacks. We're using the "user data" argument to pass in
170  // the current OptimizerRep, making these behave like non-static members.
171 
172  static int objectiveFuncWrapper ( int n, const Real* x, int new_x, Real* f, void* rep);
173  static int gradientFuncWrapper ( int n, const Real* x, int new_x, Real* gradient, void* rep);
174  static int constraintFuncWrapper( int n, const Real* x, int new_x, int m, Real* g, void* rep);
175  static int constraintJacobianWrapper( int n, const Real* x, int new_x,int m, int nele_jac,
176  int* iRow, int* jCol, Real* values, void* rep);
177  static int hessianWrapper( int n, const Real* x, int new_x, Real obj_factor,
178  int m, Real* lambda, int new_lambda,
179  int nele_hess, int* iRow, int* jCol,
180  Real* values, void* rep);
181 
190 
191 private:
192  const OptimizerSystem* sysp;
193  bool numericalGradient; // true if optimizer will compute an numerical gradient
194  bool numericalJacobian; // true if optimizer will compute an numerical Jacobian
195  Differentiator *gradDiff;
196  Differentiator *jacDiff;
197 
198  SysObjectiveFunc *of;
199  SysConstraintFunc *cf;
200 
201  std::map<std::string, std::string> advancedStrOptions;
202  std::map<std::string, Real> advancedRealOptions;
203  std::map<std::string, int> advancedIntOptions;
204  std::map<std::string, bool> advancedBoolOptions;
205  std::map<std::string, Vector> advancedVectorOptions;
206 
207  friend class Optimizer;
208  Optimizer* myHandle; // The owner handle of this Rep.
209 
210 }; // end class OptimizerRep
211 
213  Real optimize( Vector &results ) override;
214  OptimizerRep* clone() const override;
215  OptimizerAlgorithm getAlgorithm() const override;
216 };
217 
218 } // namespace SimTK
219 
220 
221 #endif // SimTK_SIMMATH_OPTIMIZER_REP_H_
FunctionRep * rep
Definition: Differentiator.h:175
Given a function f(y), where f, y or both can be vectors, calculate the derivative (gradient...
Definition: Differentiator.h:77
virtual OptimizerAlgorithm getAlgorithm() const
Definition: OptimizerRep.h:158
const Differentiator & getGradientDifferentiator() const
Definition: OptimizerRep.h:148
Definition: OptimizerRep.h:36
int f(const Vector &y, Real &fy) const override
Definition: OptimizerRep.h:42
void clearMyHandle()
Definition: OptimizerRep.h:134
int f(const Vector &y, Vector &fy) const override
Definition: OptimizerRep.h:56
GradientFunction(int ny=-1, Real acc=-1)
Real constraintsEstimatedAccuracy
Definition: OptimizerRep.h:189
Real constraintTolerance
Definition: OptimizerRep.h:184
API for SimTK Simmath&#39;s optimizers.
Definition: Optimizer.h:421
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
const OptimizerSystem * sysp
Definition: OptimizerRep.h:45
Definition: OptimizerRep.h:63
bool isUsingNumericalGradient() const
Definition: OptimizerRep.h:140
SimTK_Real Real
This is the default compiled-in floating point type for SimTK, either float or double.
Definition: SimTKcommon/include/SimTKcommon/internal/common.h:606
virtual OptimizerRep * clone() const
Definition: OptimizerRep.h:106
Real convergenceTolerance
Definition: OptimizerRep.h:183
void setMyHandle(Optimizer &cp)
Definition: OptimizerRep.h:132
SysObjectiveFunc(int ny, const OptimizerSystem *sysPtr)
Definition: OptimizerRep.h:38
static bool isAvailable()
Definition: OptimizerRep.h:107
int maxIterations
Definition: OptimizerRep.h:185
Includes internal headers providing declarations for the basic SimTK Core classes, including Simmatrix.
int diagnosticsLevel
Definition: OptimizerRep.h:182
Real objectiveEstimatedAccuracy
Definition: OptimizerRep.h:188
OptimizerRep(const OptimizerSystem &sys)
Definition: OptimizerRep.h:66
const Optimizer & getMyHandle() const
Definition: OptimizerRep.h:133
Method
Definition: Differentiator.h:92
Definition: OptimizerRep.h:50
Real getEstimatedAccuracyOfObjective() const
Definition: OptimizerRep.h:143
const OptimizerSystem * sysp
Definition: OptimizerRep.h:59
SysConstraintFunc(int nf, int ny, const OptimizerSystem *sysPtr)
Definition: OptimizerRep.h:52
int limitedMemoryHistory
Definition: OptimizerRep.h:186
OptimizerRep()
Definition: OptimizerRep.h:86
virtual int objectiveFunc(const Vector &parameters, bool new_parameters, Real &f) const
Objective/cost function which is to be optimized; return 0 when successful.
Definition: Optimizer.h:98
This is the header file that user code should include to pick up the SimTK Simmath numerical differen...
const Real SignificantReal
SignificantReal is the smallest value that we consider to be clearly distinct from roundoff error whe...
Abstract class which defines an objective/cost function which is optimized by and Optimizer object...
Definition: Optimizer.h:71
const OptimizerSystem & getOptimizerSystem() const
Definition: OptimizerRep.h:111
Real getEstimatedAccuracyOfConstraints() const
Definition: OptimizerRep.h:145
virtual int constraintFunc(const Vector &parameters, bool new_parameters, Vector &constraints) const
Computes the value of the constraints; return 0 when successful.
Definition: Optimizer.h:111
Derive a concrete class from this one if you have a scalar function of multiple variables that you wa...
Definition: Differentiator.h:208
const Differentiator & getJacobianDifferentiator() const
Definition: OptimizerRep.h:153
Definition: Optimizer.h:61
OptimizerAlgorithm
The available Optimizer algorithms.
Definition: Optimizer.h:40
#define SimTK_SIMMATH_EXPORT
Definition: SimTKmath/include/simmath/internal/common.h:64
Derive a concrete class from this one if you have a set of functions (i.e., a vector-valued function)...
Definition: Differentiator.h:227
Definition: OptimizerRep.h:212
Differentiator::Method getDifferentiatorMethod() const
Definition: OptimizerRep.h:142
bool isUsingNumericalJacobian() const
Definition: OptimizerRep.h:141
Differentiator::Method diffMethod
Definition: OptimizerRep.h:187