Simbody  3.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Optimizer.h
Go to the documentation of this file.
1 #ifndef SimTK_SIMMATH_OPTIMIZER_H_
2 #define SimTK_SIMMATH_OPTIMIZER_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 
28 #include "SimTKcommon.h"
30 #include "simmath/Differentiator.h"
31 
32 namespace SimTK {
33 
35  BestAvailable = 0, // Simmath will select best Optimizer based on problem type
36  InteriorPoint = 1, // IPOPT interior point optimizer
37  LBFGS = 2, // LBFGS optimizer
38  LBFGSB = 3, // LBFGS optimizer with simple bounds
39  CFSQP = 4 // CFSQP sequential quadratic programming optimizer (requires external library)
40 };
41 
48 public:
49  OptimizerSystem() : numParameters(0),
50  numEqualityConstraints(0),
51  numInequalityConstraints(0),
52  numLinearEqualityConstraints(0),
53  numLinearInequalityConstraints(0),
54  useLimits( false ),
55  lowerLimits(0),
56  upperLimits(0) {
57  }
58 
59  explicit OptimizerSystem(int nParameters ) {
60  new (this) OptimizerSystem(); // call the above constructor
61  setNumParameters(nParameters);
62  }
63 
64  virtual ~OptimizerSystem() {
65  if( useLimits ) {
66  delete lowerLimits;
67  delete upperLimits;
68  }
69  }
70 
73  virtual int objectiveFunc ( const Vector& parameters,
74  bool new_parameters, Real& f ) const {
75  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "objectiveFunc" );
76  return -1; }
77 
80  virtual int gradientFunc ( const Vector &parameters,
81  bool new_parameters, Vector &gradient ) const {
82  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "gradientFunc" );
83  return -1; }
86  virtual int constraintFunc ( const Vector & parameters,
87  bool new_parameters, Vector & constraints ) const {
88  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "constraintFunc" );
89  return -1; }
92  virtual int constraintJacobian ( const Vector& parameters,
93  bool new_parameters, Matrix& jac ) const {
94  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "constraintJacobian" );
95  return -1; }
98  virtual int hessian ( const Vector &parameters,
99  bool new_parameters, Vector &gradient) const {
100  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "hessian" );
101  return -1; }
102 
104  void setNumParameters( const int nParameters ) {
105  if( nParameters < 1 ) {
106  const char* where = " OptimizerSystem Constructor";
107  const char* szName = "number of parameters";
108  SimTK_THROW5(SimTK::Exception::ValueOutOfRange, szName, 1, nParameters, INT_MAX, where);
109  } else {
110  numParameters = nParameters;
111  }
112  }
114  void setNumEqualityConstraints( const int n ) {
115  if( n < 0 ) {
116  const char* where = " OptimizerSystem setNumEqualityConstraints";
117  const char* szName = "number of equality constraints";
119  } else {
120  numEqualityConstraints = n;
121  }
122  }
124  void setNumInequalityConstraints( const int n ) {
125  if( n < 0 ) {
126  const char* where = " OptimizerSystem setNumInequalityConstraints";
127  const char* szName = "number of inequality constraints";
129  } else {
130  numInequalityConstraints = n;
131  }
132  }
134  void setNumLinearEqualityConstraints( const int n ) {
135  if( n < 0 || n > numEqualityConstraints ) {
136  const char* where = " OptimizerSystem setNumLinearEqualityConstraints";
137  const char* szName = "number of linear equality constraints";
138  SimTK_THROW4(SimTK::Exception::SizeOutOfRange, szName, n, numEqualityConstraints, where);
139  } else {
140  numLinearEqualityConstraints = n;
141  }
142  }
144  void setNumLinearInequalityConstraints( const int n ) {
145  if( n < 0 || n > numInequalityConstraints ) {
146  const char* where = " OptimizerSystem setNumLinearInequalityConstraints";
147  const char* szName = "number of linear inequality constraints";
148  SimTK_THROW4(SimTK::Exception::SizeOutOfRange, szName, n, numInequalityConstraints, where);
149  } else {
150  numLinearInequalityConstraints = n;
151  }
152  }
154  void setParameterLimits( const Vector& lower, const Vector& upper ) {
155  if( upper.size() != numParameters && upper.size() != 0) {
156  const char* where = " OptimizerSystem setParamtersLimits";
157  const char* szName = "upper limits length";
158  SimTK_THROW5(Exception::IncorrectArrayLength, szName, upper.size(), "numParameters", numParameters, where);
159  }
160  if( lower.size() != numParameters && lower.size() != 0 ) {
161  const char* where = " OptimizerSystem setParamtersLimits";
162  const char* szName = "lower limits length";
163  SimTK_THROW5(Exception::IncorrectArrayLength, szName, lower.size(), "numParameters", numParameters, where);
164  }
165 
166  // set the upper and lower limits
167  if( useLimits ) {
168  delete lowerLimits;
169  delete upperLimits;
170  }
171 
172  if( upper.size() == 0 ) {
173  useLimits = false;
174  } else {
175  lowerLimits = new Vector( lower );
176  upperLimits = new Vector( upper );
177  useLimits = true;
178  }
179  }
180 
183  int getNumParameters() const {return numParameters;}
185  int getNumConstraints() const {return numEqualityConstraints+numInequalityConstraints;}
187  int getNumEqualityConstraints() const {return numEqualityConstraints;}
189  int getNumInequalityConstraints() const {return numInequalityConstraints;}
191  int getNumLinearEqualityConstraints() const {return numLinearEqualityConstraints;}
193  int getNumNonlinearEqualityConstraints() const {return numEqualityConstraints-numLinearEqualityConstraints;}
195  int getNumLinearInequalityConstraints() const {return numLinearInequalityConstraints;}
197  int getNumNonlinearInequalityConstraints() const {return numInequalityConstraints-numLinearInequalityConstraints;}
198 
200  bool getHasLimits() const { return useLimits; }
204  void getParameterLimits( Real **lower, Real **upper ) const {
205  *lower = &(*lowerLimits)[0];
206  *upper = &(*upperLimits)[0];
207  }
208 
209 private:
210  int numParameters;
211  int numEqualityConstraints;
212  int numInequalityConstraints;
213  int numLinearEqualityConstraints;
214  int numLinearInequalityConstraints;
215  bool useLimits;
216  Vector* lowerLimits;
217  Vector* upperLimits;
218 
219 }; // class OptimizerSystem
220 
242 public:
243  Optimizer();
244  Optimizer( const OptimizerSystem& sys);
245  Optimizer( const OptimizerSystem& sys, OptimizerAlgorithm algorithm);
246  ~Optimizer();
247 
248  static bool isAlgorithmAvailable(OptimizerAlgorithm algorithm);
249 
251  void setConvergenceTolerance(Real accuracy );
254  void setConstraintTolerance(Real tolerance);
255 
256 
262  void setMaxIterations( int iter );
264  void setLimitedMemoryHistory( int history );
266  void setDiagnosticsLevel( int level );
267 
268  void setOptimizerSystem( const OptimizerSystem& sys );
269  void setOptimizerSystem( const OptimizerSystem& sys, OptimizerAlgorithm algorithm );
270 
272  bool setAdvancedStrOption( const char *option, const char *value );
274  bool setAdvancedRealOption( const char *option, const Real value );
276  bool setAdvancedIntOption( const char *option, const int value );
278  bool setAdvancedBoolOption( const char *option, const bool value );
279 
280 
290  void setDifferentiatorMethod(Differentiator::Method method);
295  Differentiator::Method getDifferentiatorMethod() const;
296 
308  void useNumericalGradient(bool flag,
309  Real estimatedAccuracyOfObjective = SignificantReal);
322  void useNumericalJacobian(bool flag,
323  Real estimatedAccuracyOfConstraints = SignificantReal);
324 
326  Real optimize(Vector&);
327 
329  const OptimizerSystem& getOptimizerSystem() const;
330 
332  bool isUsingNumericalGradient() const;
334  bool isUsingNumericalJacobian() const;
336  Real getEstimatedAccuracyOfObjective() const;
338  Real getEstimatedAccuracyOfConstraints() const;
339 
340  // This is a local class.
341  class OptimizerRep;
342 private:
343  Optimizer( const Optimizer& c );
344  Optimizer& operator=(const Optimizer& rhs);
345 
346  OptimizerRep* constructOptimizerRep(const OptimizerSystem&, OptimizerAlgorithm);
347  const OptimizerRep& getRep() const {assert(rep); return *rep;}
348  OptimizerRep& updRep() {assert(rep); return *rep;}
349 
350  // Hidden implementation to preserve binary compatibility.
351  OptimizerRep* rep;
352 
353 friend class OptimizerRep;
354 }; // class Optimizer
355 
356 } // namespace SimTK
357 
358 #endif //SimTK_SIMMATH_OPTIMIZER_H_
359 
Definition: Optimizer.h:38
Definition: Exception.h:204
void setNumParameters(const int nParameters)
Sets the number of parameters in the objective function.
Definition: Optimizer.h:104
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:73
Definition: Optimizer.h:37
int getNumNonlinearInequalityConstraints() const
Returns the number of linear inequality constraints.
Definition: Optimizer.h:197
Definition: Optimizer.h:35
void setParameterLimits(const Vector &lower, const Vector &upper)
Set the upper and lower bounds on the paramters.
Definition: Optimizer.h:154
void setNumEqualityConstraints(const int n)
Sets the number of equality constraints.
Definition: Optimizer.h:114
API for SimTK Simmath's optimizers.
Definition: Optimizer.h:241
virtual int hessian(const Vector &parameters, bool new_parameters, Vector &gradient) const
Computes Hessian of the objective function; return 0 when successful.
Definition: Optimizer.h:98
OptimizerSystem()
Definition: Optimizer.h:49
int getNumInequalityConstraints() const
Returns the number of inequality constraints.
Definition: Optimizer.h:189
OptimizerSystem(int nParameters)
Definition: Optimizer.h:59
Definition: SimTKmath/include/simmath/internal/common.h:118
int size() const
Definition: BigMatrix.h:1411
virtual int constraintJacobian(const Vector &parameters, bool new_parameters, Matrix &jac) const
Computes Jacobian of the constraints; return 0 when successful.
Definition: Optimizer.h:92
virtual int gradientFunc(const Vector &parameters, bool new_parameters, Vector &gradient) const
Computes the gradient of the objective function; return 0 when successful.
Definition: Optimizer.h:80
Definition: OptimizerRep.h:63
int getNumLinearInequalityConstraints() const
Returns the number of linear inequality constraints.
Definition: Optimizer.h:195
Definition: Exception.h:174
void getParameterLimits(Real **lower, Real **upper) const
Returns the limits on the allowed values of each parameter, as an array of lower bounds and an array ...
Definition: Optimizer.h:204
Definition: Exception.h:189
void setNumInequalityConstraints(const int n)
Sets the number of inequality constraints.
Definition: Optimizer.h:124
Includes internal headers providing declarations for the basic SimTK Core classes, including Simmatrix.
int getNumParameters() const
Returns the number of parameters, that is, the number of variables that the Optimizer may adjust whil...
Definition: Optimizer.h:183
Vector_< Real > Vector
Read fixed-size VectorView from input stream.
Definition: BigMatrix.h:3541
#define SimTK_THROW2(exc, a1, a2)
Definition: Exception.h:313
#define SimTK_THROW5(exc, a1, a2, a3, a4, a5)
Definition: Exception.h:319
Definition: Optimizer.h:39
Method
Definition: Differentiator.h:92
int getNumLinearEqualityConstraints() const
Returns the number of linear equality constraints.
Definition: Optimizer.h:191
This is the Matrix class intended to appear in user code.
Definition: BigMatrix.h:181
#define SimTK_THROW3(exc, a1, a2, a3)
Definition: Exception.h:315
int getNumNonlinearEqualityConstraints() const
Returns the number of nonlinear equality constraints.
Definition: Optimizer.h:193
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:86
This is the header file that every Simmath compilation unit should include first. ...
int getNumConstraints() const
Returns the total number of constraints.
Definition: Optimizer.h:185
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:47
Definition: Optimizer.h:36
void setNumLinearEqualityConstraints(const int n)
Sets the number of lineaer equality constraints.
Definition: Optimizer.h:134
bool getHasLimits() const
Returns true if there are limits on the parameters.
Definition: Optimizer.h:200
virtual ~OptimizerSystem()
Definition: Optimizer.h:64
OptimizerAlgorithm
Definition: Optimizer.h:34
#define SimTK_THROW4(exc, a1, a2, a3, a4)
Definition: Exception.h:317
#define SimTK_SIMMATH_EXPORT
Definition: SimTKmath/include/simmath/internal/common.h:64
void setNumLinearInequalityConstraints(const int n)
Sets the number of lineaer inequality constraints.
Definition: Optimizer.h:144
int getNumEqualityConstraints() const
Returns the number of equality constraints.
Definition: Optimizer.h:187