Simbody  3.6
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 setParametersLimits";
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 setParametersLimits";
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 
422 public:
423  Optimizer();
424  Optimizer( const OptimizerSystem& sys);
425  Optimizer( const OptimizerSystem& sys, OptimizerAlgorithm algorithm);
426  ~Optimizer();
427 
430  static bool isAlgorithmAvailable(OptimizerAlgorithm algorithm);
431 
433  void setConvergenceTolerance(Real accuracy );
436  void setConstraintTolerance(Real tolerance);
437 
438 
444  void setMaxIterations( int iter );
447  void setLimitedMemoryHistory( int history );
449  void setDiagnosticsLevel( int level );
450 
451  void setOptimizerSystem( const OptimizerSystem& sys );
452  void setOptimizerSystem( const OptimizerSystem& sys, OptimizerAlgorithm algorithm );
453 
455  bool setAdvancedStrOption( const char *option, const char *value );
457  bool setAdvancedRealOption( const char *option, const Real value );
459  bool setAdvancedIntOption( const char *option, const int value );
461  bool setAdvancedBoolOption( const char *option, const bool value );
463  bool setAdvancedVectorOption( const char *option, const Vector value );
464 
465 
475  void setDifferentiatorMethod(Differentiator::Method method);
480  Differentiator::Method getDifferentiatorMethod() const;
481 
486  OptimizerAlgorithm getAlgorithm() const;
487 
499  void useNumericalGradient(bool flag,
500  Real estimatedAccuracyOfObjective = SignificantReal);
513  void useNumericalJacobian(bool flag,
514  Real estimatedAccuracyOfConstraints = SignificantReal);
515 
517  Real optimize(Vector&);
518 
520  const OptimizerSystem& getOptimizerSystem() const;
521 
523  bool isUsingNumericalGradient() const;
525  bool isUsingNumericalJacobian() const;
527  Real getEstimatedAccuracyOfObjective() const;
529  Real getEstimatedAccuracyOfConstraints() const;
530 
531  // This is a local class.
532  class OptimizerRep;
533 private:
534  Optimizer( const Optimizer& c );
535  Optimizer& operator=(const Optimizer& rhs);
536 
537  OptimizerRep* constructOptimizerRep(const OptimizerSystem&, OptimizerAlgorithm);
538  const OptimizerRep& getRep() const {assert(rep); return *rep;}
539  OptimizerRep& updRep() {assert(rep); return *rep;}
540 
541  // Hidden implementation to preserve binary compatibility.
542  OptimizerRep* rep;
543 
544 friend class OptimizerRep;
545 }; // class Optimizer
546 
547 } // namespace SimTK
548 
549 #endif //SimTK_SIMMATH_OPTIMIZER_H_
550 
LBFGS with simple bound constraints; gradient descent.
Definition: Optimizer.h:51
Definition: Exception.h:205
void setNumParameters(const int nParameters)
Sets the number of parameters in the objective function.
Definition: Optimizer.h:129
Limited-memory Broyden-Fletcher-Goldfarb-Shanno algorithm; gradient descent.
Definition: Optimizer.h:48
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 parameters.
Definition: Optimizer.h:179
bool getHasLimits() const
Returns true if there are limits on the parameters.
Definition: Optimizer.h:225
void setNumEqualityConstraints(const int n)
Sets the number of equality constraints.
Definition: Optimizer.h:139
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
int getNumLinearInequalityConstraints() const
Returns the number of linear inequality constraints.
Definition: Optimizer.h:220
OptimizerSystem()
Definition: Optimizer.h:73
int getNumEqualityConstraints() const
Returns the number of equality constraints.
Definition: Optimizer.h:212
OptimizerSystem(int nParameters)
Definition: Optimizer.h:83
Definition: SimTKmath/include/simmath/internal/common.h:122
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:606
Definition: Exception.h:175
Definition: Exception.h:190
int getNumConstraints() const
Returns the total number of constraints.
Definition: Optimizer.h:210
void setNumInequalityConstraints(const int n)
Sets the number of inequality constraints.
Definition: Optimizer.h:149
int getNumNonlinearEqualityConstraints() const
Returns the number of nonlinear equality constraints.
Definition: Optimizer.h:218
Includes internal headers providing declarations for the basic SimTK Core classes, including Simmatrix.
#define SimTK_THROW2(exc, a1, a2)
Definition: Exception.h:318
An algorithm that is implemented outside of Simmath.
Definition: Optimizer.h:63
#define SimTK_THROW5(exc, a1, a2, a3, a4, a5)
Definition: Exception.h:324
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
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
#define SimTK_THROW3(exc, a1, a2, a3)
Definition: Exception.h:320
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 every Simmath compilation unit should include first. ...
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
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...
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
Vector_< Real > Vector
Variable-size column vector of Real elements; abbreviation for Vector_<Real>.
Definition: BigMatrix.h:1473
Abstract class which defines an objective/cost function which is optimized by and Optimizer object...
Definition: Optimizer.h:71
int size() const
Definition: VectorBase.h:396
int getNumParameters() const
Returns the number of parameters, that is, the number of variables that the Optimizer may adjust whil...
Definition: Optimizer.h:208
IpOpt algorithm (https://projects.coin-or.org/ipopt); gradient descent.
Definition: Optimizer.h:45
int getNumLinearEqualityConstraints() const
Returns the number of linear equality constraints.
Definition: Optimizer.h:216
int getNumNonlinearInequalityConstraints() const
Returns the number of linear inequality constraints.
Definition: Optimizer.h:222
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
void setNumLinearEqualityConstraints(const int n)
Sets the number of lineaer equality constraints.
Definition: Optimizer.h:159
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:322
#define SimTK_SIMMATH_EXPORT
Definition: SimTKmath/include/simmath/internal/common.h:64
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
int getNumInequalityConstraints() const
Returns the number of inequality constraints.
Definition: Optimizer.h:214
void setNumLinearInequalityConstraints(const int n)
Sets the number of lineaer inequality constraints.
Definition: Optimizer.h:169