Simbody 3.7
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"
31
32namespace SimTK {
33
48 LBFGS = 2,
51 LBFGSB = 3,
56 CFSQP = 4,
60 CMAES = 5,
61 UnknownOptimizerAlgorithm = 6, // the default impl. of getAlgorithm.
64};
65
72public:
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 }
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 }
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
234private:
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
422public:
427
431
437
438
444 void setMaxIterations( int iter );
447 void setLimitedMemoryHistory( int history );
449 void setDiagnosticsLevel( int level );
450
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
481
487
499 void useNumericalGradient(bool flag,
500 Real estimatedAccuracyOfObjective = SignificantReal);
513 void useNumericalJacobian(bool flag,
514 Real estimatedAccuracyOfConstraints = SignificantReal);
515
518
521
530
531 // This is a local class.
532 class OptimizerRep;
533private:
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
544friend class OptimizerRep;
545}; // class Optimizer
546
547} // namespace SimTK
548
549#endif //SimTK_SIMMATH_OPTIMIZER_H_
550
This is the header file that user code should include to pick up the SimTK Simmath numerical differen...
#define SimTK_THROW2(exc, a1, a2)
Definition: Exception.h:318
#define SimTK_THROW5(exc, a1, a2, a3, a4, a5)
Definition: Exception.h:324
#define SimTK_THROW3(exc, a1, a2, a3)
Definition: Exception.h:320
#define SimTK_THROW4(exc, a1, a2, a3, a4)
Definition: Exception.h:322
Includes internal headers providing declarations for the basic SimTK Core classes,...
This is the header file that every Simmath compilation unit should include first.
#define SimTK_SIMMATH_EXPORT
Definition: SimTKmath/include/simmath/internal/common.h:64
Method
Definition: Differentiator.h:92
Definition: SimTKmath/include/simmath/internal/common.h:122
Definition: Exception.h:175
Definition: Exception.h:190
Definition: Exception.h:205
Abstract class which defines an objective/cost function which is optimized by and Optimizer object.
Definition: Optimizer.h:71
void setNumEqualityConstraints(const int n)
Sets the number of equality constraints.
Definition: Optimizer.h:139
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
int getNumConstraints() const
Returns the total number of constraints.
Definition: Optimizer.h:210
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
void setNumParameters(const int nParameters)
Sets the number of parameters in the objective function.
Definition: Optimizer.h:129
int getNumLinearEqualityConstraints() const
Returns the number of linear equality constraints.
Definition: Optimizer.h:216
int getNumLinearInequalityConstraints() const
Returns the number of linear inequality constraints.
Definition: Optimizer.h:220
int getNumInequalityConstraints() const
Returns the number of inequality constraints.
Definition: Optimizer.h:214
void setNumInequalityConstraints(const int n)
Sets the number of inequality constraints.
Definition: Optimizer.h:149
void setNumLinearEqualityConstraints(const int n)
Sets the number of lineaer equality constraints.
Definition: Optimizer.h:159
void setNumLinearInequalityConstraints(const int n)
Sets the number of lineaer inequality constraints.
Definition: Optimizer.h:169
OptimizerSystem()
Definition: Optimizer.h:73
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
void setParameterLimits(const Vector &lower, const Vector &upper)
Set the upper and lower bounds on the parameters.
Definition: Optimizer.h:179
virtual ~OptimizerSystem()
Definition: Optimizer.h:88
bool getHasLimits() const
Returns true if there are limits on the parameters.
Definition: Optimizer.h:225
int getNumEqualityConstraints() const
Returns the number of equality constraints.
Definition: Optimizer.h:212
OptimizerSystem(int nParameters)
Definition: Optimizer.h:83
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
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
int getNumNonlinearInequalityConstraints() const
Returns the number of linear inequality constraints.
Definition: Optimizer.h:222
int getNumParameters() const
Returns the number of parameters, that is, the number of variables that the Optimizer may adjust whil...
Definition: Optimizer.h:208
int getNumNonlinearEqualityConstraints() const
Returns the number of nonlinear equality constraints.
Definition: Optimizer.h:218
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
Definition: OptimizerRep.h:63
API for SimTK Simmath's optimizers.
Definition: Optimizer.h:421
void setOptimizerSystem(const OptimizerSystem &sys)
bool setAdvancedVectorOption(const char *option, const Vector value)
Set the value of an advanced option specified by an Vector value.
Real getEstimatedAccuracyOfObjective() const
Return the estimated accuracy last specified in useNumericalGradient().
Real getEstimatedAccuracyOfConstraints() const
Return the estimated accuracy last specified in useNumericalJacobian().
static bool isAlgorithmAvailable(OptimizerAlgorithm algorithm)
BestAvailable, UnknownAlgorithm, and UserSuppliedAlgorithm are treated as never available.
void setConstraintTolerance(Real tolerance)
Sets the absolute tolerance used to determine whether constraint violation is acceptable.
bool setAdvancedBoolOption(const char *option, const bool value)
Set the value of an advanced option specified by an boolean value.
Real optimize(Vector &)
Compute optimization.
Optimizer(const OptimizerSystem &sys)
void setConvergenceTolerance(Real accuracy)
Sets the relative accuracy used determine if the problem has converged.
void setOptimizerSystem(const OptimizerSystem &sys, OptimizerAlgorithm algorithm)
Differentiator::Method getDifferentiatorMethod() const
Return the differentiation method last supplied in a call to setDifferentiatorMethod(),...
bool isUsingNumericalGradient() const
Indicate whether the Optimizer is currently set to use a numerical gradient.
const OptimizerSystem & getOptimizerSystem() const
Return a reference to the OptimizerSystem currently associated with this Optimizer.
void useNumericalGradient(bool flag, Real estimatedAccuracyOfObjective=SignificantReal)
Enable numerical calculation of gradient, with optional estimation of the accuracy to which the objec...
bool setAdvancedIntOption(const char *option, const int value)
Set the value of an advanced option specified by an integer value.
void setMaxIterations(int iter)
Set the maximum number of iterations allowed of the optimization method's outer stepping loop.
bool setAdvancedRealOption(const char *option, const Real value)
Set the value of an advanced option specified by a real value.
bool setAdvancedStrOption(const char *option, const char *value)
Set the value of an advanced option specified by a string.
OptimizerAlgorithm getAlgorithm() const
Return the algorithm used for the optimization.
void setLimitedMemoryHistory(int history)
Set the maximum number of previous hessians used in a limited memory hessian approximation.
Optimizer(const OptimizerSystem &sys, OptimizerAlgorithm algorithm)
void setDiagnosticsLevel(int level)
Set the level of debugging info displayed.
bool isUsingNumericalJacobian() const
Indicate whether the Optimizer is currently set to use a numerical Jacobian.
void setDifferentiatorMethod(Differentiator::Method method)
Set which numerical differentiation algorithm is to be used for the next useNumericalGradient() or us...
void useNumericalJacobian(bool flag, Real estimatedAccuracyOfConstraints=SignificantReal)
Enable numerical calculation of the constraint Jacobian, with optional estimation of the accuracy to ...
int size() const
Definition: VectorBase.h:396
Vector_< Real > Vector
Variable-size column vector of Real elements; abbreviation for Vector_<Real>.
Definition: BigMatrix.h:1473
const Real SignificantReal
SignificantReal is the smallest value that we consider to be clearly distinct from roundoff error whe...
This is the top-level SimTK namespace into which all SimTK names are placed to avoid collision with o...
Definition: Assembler.h:37
OptimizerAlgorithm
The available Optimizer algorithms.
Definition: Optimizer.h:40
@ InteriorPoint
IpOpt algorithm (https://projects.coin-or.org/ipopt); gradient descent.
Definition: Optimizer.h:45
@ CMAES
Covariance matrix adaptation, evolution strategy (https://github.com/cma-es/c-cmaes); this is a rando...
Definition: Optimizer.h:60
@ UserSuppliedOptimizerAlgorithm
An algorithm that is implemented outside of Simmath.
Definition: Optimizer.h:63
@ BestAvailable
Simmath will select best Optimizer based on problem type.
Definition: Optimizer.h:42
@ LBFGS
Limited-memory Broyden-Fletcher-Goldfarb-Shanno algorithm; gradient descent.
Definition: Optimizer.h:48
@ LBFGSB
LBFGS with simple bound constraints; gradient descent.
Definition: Optimizer.h:51
@ UnknownOptimizerAlgorithm
Definition: Optimizer.h:61
@ CFSQP
C implementation of sequential quadratic programming (requires external library: ftp://frcatel....
Definition: Optimizer.h:56
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