Simbody  3.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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 
48  LBFGS = 2,
51  LBFGSB = 3,
56  CFSQP = 4,
60  CMAES = 5,
61  UnknownOptimizerAlgorithm = 6, // the default impl. of getAlgorithm.
64 };
65 
72 public:
73  OptimizerSystem() : numParameters(0),
74  numEqualityConstraints(0),
75  numInequalityConstraints(0),
76  numLinearEqualityConstraints(0),
77  numLinearInequalityConstraints(0),
78  useLimits( false ),
79  lowerLimits(0),
80  upperLimits(0) {
81  }
82 
83  explicit OptimizerSystem(int nParameters ) {
84  new (this) OptimizerSystem(); // call the above constructor
85  setNumParameters(nParameters);
86  }
87 
88  virtual ~OptimizerSystem() {
89  if( useLimits ) {
90  delete lowerLimits;
91  delete upperLimits;
92  }
93  }
94 
98  virtual int objectiveFunc ( const Vector& parameters,
99  bool new_parameters, Real& f ) const {
100  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "objectiveFunc" );
101  return -1; }
102 
105  virtual int gradientFunc ( const Vector &parameters,
106  bool new_parameters, Vector &gradient ) const {
107  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "gradientFunc" );
108  return -1; }
111  virtual int constraintFunc ( const Vector & parameters,
112  bool new_parameters, Vector & constraints ) const {
113  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "constraintFunc" );
114  return -1; }
117  virtual int constraintJacobian ( const Vector& parameters,
118  bool new_parameters, Matrix& jac ) const {
119  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "constraintJacobian" );
120  return -1; }
123  virtual int hessian ( const Vector &parameters,
124  bool new_parameters, Vector &gradient) const {
125  SimTK_THROW2(SimTK::Exception::UnimplementedVirtualMethod , "OptimizerSystem", "hessian" );
126  return -1; }
127 
129  void setNumParameters( const int nParameters ) {
130  if( nParameters < 1 ) {
131  const char* where = " OptimizerSystem Constructor";
132  const char* szName = "number of parameters";
133  SimTK_THROW5(SimTK::Exception::ValueOutOfRange, szName, 1, nParameters, INT_MAX, where);
134  } else {
135  numParameters = nParameters;
136  }
137  }
139  void setNumEqualityConstraints( const int n ) {
140  if( n < 0 ) {
141  const char* where = " OptimizerSystem setNumEqualityConstraints";
142  const char* szName = "number of equality constraints";
144  } else {
145  numEqualityConstraints = n;
146  }
147  }
149  void setNumInequalityConstraints( const int n ) {
150  if( n < 0 ) {
151  const char* where = " OptimizerSystem setNumInequalityConstraints";
152  const char* szName = "number of inequality constraints";
154  } else {
155  numInequalityConstraints = n;
156  }
157  }
159  void setNumLinearEqualityConstraints( const int n ) {
160  if( n < 0 || n > numEqualityConstraints ) {
161  const char* where = " OptimizerSystem setNumLinearEqualityConstraints";
162  const char* szName = "number of linear equality constraints";
163  SimTK_THROW4(SimTK::Exception::SizeOutOfRange, szName, n, numEqualityConstraints, where);
164  } else {
165  numLinearEqualityConstraints = n;
166  }
167  }
169  void setNumLinearInequalityConstraints( const int n ) {
170  if( n < 0 || n > numInequalityConstraints ) {
171  const char* where = " OptimizerSystem setNumLinearInequalityConstraints";
172  const char* szName = "number of linear inequality constraints";
173  SimTK_THROW4(SimTK::Exception::SizeOutOfRange, szName, n, numInequalityConstraints, where);
174  } else {
175  numLinearInequalityConstraints = n;
176  }
177  }
179  void setParameterLimits( const Vector& lower, const Vector& upper ) {
180  if( upper.size() != numParameters && upper.size() != 0) {
181  const char* where = " OptimizerSystem setParamtersLimits";
182  const char* szName = "upper limits length";
183  SimTK_THROW5(Exception::IncorrectArrayLength, szName, upper.size(), "numParameters", numParameters, where);
184  }
185  if( lower.size() != numParameters && lower.size() != 0 ) {
186  const char* where = " OptimizerSystem setParamtersLimits";
187  const char* szName = "lower limits length";
188  SimTK_THROW5(Exception::IncorrectArrayLength, szName, lower.size(), "numParameters", numParameters, where);
189  }
190 
191  // set the upper and lower limits
192  if( useLimits ) {
193  delete lowerLimits;
194  delete upperLimits;
195  }
196 
197  if( upper.size() == 0 ) {
198  useLimits = false;
199  } else {
200  lowerLimits = new Vector( lower );
201  upperLimits = new Vector( upper );
202  useLimits = true;
203  }
204  }
205 
208  int getNumParameters() const {return numParameters;}
210  int getNumConstraints() const {return numEqualityConstraints+numInequalityConstraints;}
212  int getNumEqualityConstraints() const {return numEqualityConstraints;}
214  int getNumInequalityConstraints() const {return numInequalityConstraints;}
216  int getNumLinearEqualityConstraints() const {return numLinearEqualityConstraints;}
218  int getNumNonlinearEqualityConstraints() const {return numEqualityConstraints-numLinearEqualityConstraints;}
220  int getNumLinearInequalityConstraints() const {return numLinearInequalityConstraints;}
222  int getNumNonlinearInequalityConstraints() const {return numInequalityConstraints-numLinearInequalityConstraints;}
223 
225  bool getHasLimits() const { return useLimits; }
229  void getParameterLimits( Real **lower, Real **upper ) const {
230  *lower = &(*lowerLimits)[0];
231  *upper = &(*upperLimits)[0];
232  }
233 
234 private:
235  int numParameters;
236  int numEqualityConstraints;
237  int numInequalityConstraints;
238  int numLinearEqualityConstraints;
239  int numLinearInequalityConstraints;
240  bool useLimits;
241  Vector* lowerLimits;
242  Vector* upperLimits;
243 
244 }; // class OptimizerSystem
245 
356 public:
357  Optimizer();
358  Optimizer( const OptimizerSystem& sys);
359  Optimizer( const OptimizerSystem& sys, OptimizerAlgorithm algorithm);
360  ~Optimizer();
361 
364  static bool isAlgorithmAvailable(OptimizerAlgorithm algorithm);
365 
367  void setConvergenceTolerance(Real accuracy );
370  void setConstraintTolerance(Real tolerance);
371 
372 
378  void setMaxIterations( int iter );
381  void setLimitedMemoryHistory( int history );
383  void setDiagnosticsLevel( int level );
384 
385  void setOptimizerSystem( const OptimizerSystem& sys );
386  void setOptimizerSystem( const OptimizerSystem& sys, OptimizerAlgorithm algorithm );
387 
389  bool setAdvancedStrOption( const char *option, const char *value );
391  bool setAdvancedRealOption( const char *option, const Real value );
393  bool setAdvancedIntOption( const char *option, const int value );
395  bool setAdvancedBoolOption( const char *option, const bool value );
396 
397 
407  void setDifferentiatorMethod(Differentiator::Method method);
412  Differentiator::Method getDifferentiatorMethod() const;
413 
418  OptimizerAlgorithm getAlgorithm() const;
419 
431  void useNumericalGradient(bool flag,
432  Real estimatedAccuracyOfObjective = SignificantReal);
445  void useNumericalJacobian(bool flag,
446  Real estimatedAccuracyOfConstraints = SignificantReal);
447 
449  Real optimize(Vector&);
450 
452  const OptimizerSystem& getOptimizerSystem() const;
453 
455  bool isUsingNumericalGradient() const;
457  bool isUsingNumericalJacobian() const;
459  Real getEstimatedAccuracyOfObjective() const;
461  Real getEstimatedAccuracyOfConstraints() const;
462 
463  // This is a local class.
464  class OptimizerRep;
465 private:
466  Optimizer( const Optimizer& c );
467  Optimizer& operator=(const Optimizer& rhs);
468 
469  OptimizerRep* constructOptimizerRep(const OptimizerSystem&, OptimizerAlgorithm);
470  const OptimizerRep& getRep() const {assert(rep); return *rep;}
471  OptimizerRep& updRep() {assert(rep); return *rep;}
472 
473  // Hidden implementation to preserve binary compatibility.
474  OptimizerRep* rep;
475 
476 friend class OptimizerRep;
477 }; // class Optimizer
478 
479 } // namespace SimTK
480 
481 #endif //SimTK_SIMMATH_OPTIMIZER_H_
482 
LBFGS with simple bound constraints; gradient descent.
Definition: Optimizer.h:51
Definition: Exception.h:204
void setNumParameters(const int nParameters)
Sets the number of parameters in the objective function.
Definition: Optimizer.h:129
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
Limited-memory Broyden-Fletcher-Goldfarb-Shanno algorithm; gradient descent.
Definition: Optimizer.h:48
int getNumNonlinearInequalityConstraints() const
Returns the number of linear inequality constraints.
Definition: Optimizer.h:222
Simmath will select best Optimizer based on problem type.
Definition: Optimizer.h:42
void setParameterLimits(const Vector &lower, const Vector &upper)
Set the upper and lower bounds on the paramters.
Definition: Optimizer.h:179
void setNumEqualityConstraints(const int n)
Sets the number of equality constraints.
Definition: Optimizer.h:139
API for SimTK Simmath's optimizers.
Definition: Optimizer.h:355
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
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:123
int size() const
Definition: VectorBase.h:396
OptimizerSystem()
Definition: Optimizer.h:73
int getNumInequalityConstraints() const
Returns the number of inequality constraints.
Definition: Optimizer.h:214
OptimizerSystem(int nParameters)
Definition: Optimizer.h:83
Definition: SimTKmath/include/simmath/internal/common.h:118
virtual int constraintJacobian(const Vector &parameters, bool new_parameters, Matrix &jac) const
Computes Jacobian of the constraints; return 0 when successful.
Definition: Optimizer.h:117
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:105
Definition: OptimizerRep.h:63
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:577
int getNumLinearInequalityConstraints() const
Returns the number of linear inequality constraints.
Definition: Optimizer.h:220
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:229
Definition: Exception.h:189
void setNumInequalityConstraints(const int n)
Sets the number of inequality constraints.
Definition: Optimizer.h:149
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:208
#define SimTK_THROW2(exc, a1, a2)
Definition: Exception.h:313
An algorithm that is implemented outside of Simmath.
Definition: Optimizer.h:63
#define SimTK_THROW5(exc, a1, a2, a3, a4, a5)
Definition: Exception.h:319
C implementation of sequential quadratic programming (requires external library: ftp://frcatel.fri.uniza.sk/pub/soft/math/matprog/doc/fsqp.html); gradient descent.
Definition: Optimizer.h:56
Method
Definition: Differentiator.h:92
int getNumLinearEqualityConstraints() const
Returns the number of linear equality constraints.
Definition: Optimizer.h:216
#define SimTK_THROW3(exc, a1, a2, a3)
Definition: Exception.h:315
int getNumNonlinearEqualityConstraints() const
Returns the number of nonlinear equality constraints.
Definition: Optimizer.h:218
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
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:210
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...
Vector_< Real > Vector
Variable-size column vector of Real elements; abbreviation for Vector_.
Definition: BigMatrix.h:1473
Abstract class which defines an objective/cost function which is optimized by and Optimizer object...
Definition: Optimizer.h:71
IpOpt algorithm (https://projects.coin-or.org/ipopt); gradient descent.
Definition: Optimizer.h:45
void setNumLinearEqualityConstraints(const int n)
Sets the number of lineaer equality constraints.
Definition: Optimizer.h:159
bool getHasLimits() const
Returns true if there are limits on the parameters.
Definition: Optimizer.h:225
virtual ~OptimizerSystem()
Definition: Optimizer.h:88
Definition: Optimizer.h:61
OptimizerAlgorithm
The available Optimizer algorithms.
Definition: Optimizer.h:40
Covariance matrix adaptation, evolution strategy (https://github.com/cma-es/c-cmaes); this is a rando...
Definition: Optimizer.h:60
#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:169
int getNumEqualityConstraints() const
Returns the number of equality constraints.
Definition: Optimizer.h:212