Simbody
3.7
|
API for SimTK Simmath's optimizers. More...
Classes | |
class | OptimizerRep |
Public Member Functions | |
Optimizer () | |
Optimizer (const OptimizerSystem &sys) | |
Optimizer (const OptimizerSystem &sys, OptimizerAlgorithm algorithm) | |
~Optimizer () | |
void | setConvergenceTolerance (Real accuracy) |
Sets the relative accuracy used determine if the problem has converged. More... | |
void | setConstraintTolerance (Real tolerance) |
Sets the absolute tolerance used to determine whether constraint violation is acceptable. More... | |
void | setMaxIterations (int iter) |
Set the maximum number of iterations allowed of the optimization method's outer stepping loop. More... | |
void | setLimitedMemoryHistory (int history) |
Set the maximum number of previous hessians used in a limited memory hessian approximation. More... | |
void | setDiagnosticsLevel (int level) |
Set the level of debugging info displayed. More... | |
void | setOptimizerSystem (const OptimizerSystem &sys) |
void | setOptimizerSystem (const OptimizerSystem &sys, OptimizerAlgorithm algorithm) |
bool | setAdvancedStrOption (const char *option, const char *value) |
Set the value of an advanced option specified by a string. More... | |
bool | setAdvancedRealOption (const char *option, const Real value) |
Set the value of an advanced option specified by a real value. More... | |
bool | setAdvancedIntOption (const char *option, const int value) |
Set the value of an advanced option specified by an integer value. More... | |
bool | setAdvancedBoolOption (const char *option, const bool value) |
Set the value of an advanced option specified by an boolean value. More... | |
bool | setAdvancedVectorOption (const char *option, const Vector value) |
Set the value of an advanced option specified by an Vector value. More... | |
void | setDifferentiatorMethod (Differentiator::Method method) |
Set which numerical differentiation algorithm is to be used for the next useNumericalGradient() or useNumericalJacobian() call. More... | |
Differentiator::Method | getDifferentiatorMethod () const |
Return the differentiation method last supplied in a call to setDifferentiatorMethod(), not necessarily the method currently in use. More... | |
OptimizerAlgorithm | getAlgorithm () const |
Return the algorithm used for the optimization. More... | |
void | useNumericalGradient (bool flag, Real estimatedAccuracyOfObjective=SignificantReal) |
Enable numerical calculation of gradient, with optional estimation of the accuracy to which the objective function is calculated. More... | |
void | useNumericalJacobian (bool flag, Real estimatedAccuracyOfConstraints=SignificantReal) |
Enable numerical calculation of the constraint Jacobian, with optional estimation of the accuracy to which the constraint functions are calculated. More... | |
Real | optimize (Vector &) |
Compute optimization. More... | |
const OptimizerSystem & | getOptimizerSystem () const |
Return a reference to the OptimizerSystem currently associated with this Optimizer. More... | |
bool | isUsingNumericalGradient () const |
Indicate whether the Optimizer is currently set to use a numerical gradient. More... | |
bool | isUsingNumericalJacobian () const |
Indicate whether the Optimizer is currently set to use a numerical Jacobian. More... | |
Real | getEstimatedAccuracyOfObjective () const |
Return the estimated accuracy last specified in useNumericalGradient(). More... | |
Real | getEstimatedAccuracyOfConstraints () const |
Return the estimated accuracy last specified in useNumericalJacobian(). More... | |
Static Public Member Functions | |
static bool | isAlgorithmAvailable (OptimizerAlgorithm algorithm) |
BestAvailable, UnknownAlgorithm, and UserSuppliedAlgorithm are treated as never available. More... | |
Friends | |
class | OptimizerRep |
API for SimTK Simmath's optimizers.
An optimizer finds a minimum to an objective function. Usually, this minimum is a local minimum. Some algorithms, like CMAES, are designed to find the global minumum. The optimizer can be constrained to search for a minimum within a feasible region. The feasible region is defined in two ways: via limits on the parameters of the objective function; and, for algorithms other than CMAES, by supplying constraint functions that must be satisfied. The optimizer starts searching for a minimum beginning at a user supplied initial value for the set of parameters.
The objective function and constraints are specified by supplying the Optimizer with a concrete implemenation of an OptimizerSystem class. The OptimizerSystem can be passed to the Optimizer either through the Optimizer constructor or by calling the Optimizer::setOptimizerSystem method. The Optimizer class will select the best optimization algorithm to solve the problem based on the constraints supplied by the OptimizerSystem. A user can also override the optimization algorithm selected by the Optimizer by specifying the optimization algorithm.
See OptimizerAlgorithm for a brief description of the available algorithms. Most of these algorithms have options that are specific to the algorithm. These options are set via methods like Optimizer::setAdvancedStrOption. If you want to get going quickly, you can just use the default values of these options and ignore this section. As an example, an int option popsize would be set via:
For now, we only have detailed documentation for the CMAES algorithm.
This is the c-cmaes algorithm written by Niko Hansen (https://github.com/cma-es/c-cmaes).
Some notes:
Encoding of Variables
Inappropriate initialization of the algorithm may lead to resampling of the parameter distribution. Since some parameters may be defined in a bounded region [a, b]. The CMA algorithm involves sampling a random distribution for the variables (parameters) in the optimization problem. If the sampled values do not lie within the variables' bounds, CMA must resample the distribution until the variables lie within the bounds. This resampling is undesirable, and may prevent the algorithm from functioning properly. There are two ways to avoid excessive resampling:To overcome this the problem the user can formulate the problem as follows:
1) Either define the bounds of the parameter and choose the appropriate stddev and initial value for each parameter (see init_stepsize) 2) Or to reformulate the problem, by rescaling the parameter space so that each parameter map in a region between e.g. [0, 1] and each parameter has an initial value of 0.5 and stddev of 0.2 (see comments below).
The specific formulation of a (real) optimization problem has a tremendous impact on the optimization performance. In particular, a reasonable parameter encoding is essential. All parameters should be rescaled such that they have presumably similar sensitivity (this makes the identity as initial covariance matrix the right choice). Usually, the best approach is to write a wrapper around the objective function that transforms the parameters before the actual function call. The wrapper scales, for example, in each parameter/coordinate the value [0; 10] into the typical actual domain of the parameter/coordinate.
The natural encoding of (some of) the parameters can also be "logarithmic". That is, for a parameter that must always be positive, with a ratio between typical upper and lower value being larger than 100, we might use 10x instead of x to call the objective function. More specifically, to achieve the parameter range [10^–4,10^–1], we use 10^–4×10^3x/10 with x in [0; 10]. Again, the idea is to have similar sensitivity: this makes sense if we expect the change from 10^–4 to 10^–3 to have an impact similar to the change from 10^–2 to 10^–1. In order to avoid the problem that changes of very small values have too less an impact, an alternative is to choose 10^–1 × (x/10)2 >= 0. In the case where only a lower bound at zero is necessary, a simple and natural transformation is x2 × default_x, such that x=1 represents the default (or initial) value and x remains unbounded during optimization.
In summary, to map the values [0;10] into [a;b] we have the alternative transformations a + (b-a) × x/10 or a + (b-a) × (x/10)2 >= a or a × (b/a)x/10 >= 0.
For more details see: https://www.lri.fr/~hansen/cmaes_inmatlab.html
Advanced options:
The default values for options whose name begins with "stop" are specified at https://github.com/CMA-ES/c-cmaes/blob/master/cmaes_initials.par
init_stepsize (Vector/Real; default: 0.3) Initial stddev; After the encoding of variables, the initial solution point x0 and the initial standard deviation (step_size) sigma0 must be chosen. In a practical application, one often wants to start by trying to improve a given solution locally. In this case we choose a rather small sigma0 (say in [0.001, 0.1], given the x-values "live" in [0,10]). Thereby we can also check whether the initial solution is possibly a local optimum. When a global optimum is sought-after on rugged or multimodal landscapes, sigma0 should be chosen such that the final desirable location (or at least some of its domain of attraction) is not far outside of x0 ± 2sigma0 in each coordinate. (Remark that in Rn, if each boundary domain is in distance sigma, then the boundary corner is sigma*sqrt(n) away, which poses a slight dilemma for larger n.)
A warning is emitted if this is not set and default value is used for each variable.
Example setting the init_stepsize:
Vector initStepSize(N, 0.3); opt.setAdvancedVectorOption("init_stepsize", initStepSize); "or" opt.setAdvancedRealOption("init_stepsize", 0.3);
If you want to generate identical results with repeated optimizations, you can set the seed option. In addition, you must set the maxTimeFractionForEigendecomposition option to be greater than or equal to 1.0.
SimTK::Optimizer::Optimizer | ( | ) |
SimTK::Optimizer::Optimizer | ( | const OptimizerSystem & | sys | ) |
SimTK::Optimizer::Optimizer | ( | const OptimizerSystem & | sys, |
OptimizerAlgorithm | algorithm | ||
) |
SimTK::Optimizer::~Optimizer | ( | ) |
|
static |
BestAvailable, UnknownAlgorithm, and UserSuppliedAlgorithm are treated as never available.
void SimTK::Optimizer::setConvergenceTolerance | ( | Real | accuracy | ) |
Sets the relative accuracy used determine if the problem has converged.
void SimTK::Optimizer::setConstraintTolerance | ( | Real | tolerance | ) |
Sets the absolute tolerance used to determine whether constraint violation is acceptable.
void SimTK::Optimizer::setMaxIterations | ( | int | iter | ) |
Set the maximum number of iterations allowed of the optimization method's outer stepping loop.
Most optimizers also have an inner loop ("line search") which is also iterative but is not affected by this setting. Inner loop convergence is typically prescribed by theory, and failure there is often an indication of an ill-formed problem.
void SimTK::Optimizer::setLimitedMemoryHistory | ( | int | history | ) |
Set the maximum number of previous hessians used in a limited memory hessian approximation.
void SimTK::Optimizer::setDiagnosticsLevel | ( | int | level | ) |
Set the level of debugging info displayed.
void SimTK::Optimizer::setOptimizerSystem | ( | const OptimizerSystem & | sys | ) |
void SimTK::Optimizer::setOptimizerSystem | ( | const OptimizerSystem & | sys, |
OptimizerAlgorithm | algorithm | ||
) |
bool SimTK::Optimizer::setAdvancedStrOption | ( | const char * | option, |
const char * | value | ||
) |
Set the value of an advanced option specified by a string.
bool SimTK::Optimizer::setAdvancedRealOption | ( | const char * | option, |
const Real | value | ||
) |
Set the value of an advanced option specified by a real value.
bool SimTK::Optimizer::setAdvancedIntOption | ( | const char * | option, |
const int | value | ||
) |
Set the value of an advanced option specified by an integer value.
bool SimTK::Optimizer::setAdvancedBoolOption | ( | const char * | option, |
const bool | value | ||
) |
Set the value of an advanced option specified by an boolean value.
bool SimTK::Optimizer::setAdvancedVectorOption | ( | const char * | option, |
const Vector | value | ||
) |
Set the value of an advanced option specified by an Vector value.
void SimTK::Optimizer::setDifferentiatorMethod | ( | Differentiator::Method | method | ) |
Set which numerical differentiation algorithm is to be used for the next useNumericalGradient() or useNumericalJacobian() call.
Choices are Differentiator::ForwardDifference (first order) or Differentiator::CentralDifference (second order) with central the default.
Differentiator::Method SimTK::Optimizer::getDifferentiatorMethod | ( | ) | const |
Return the differentiation method last supplied in a call to setDifferentiatorMethod(), not necessarily the method currently in use.
See setDifferentiatorMethod() for more information.
OptimizerAlgorithm SimTK::Optimizer::getAlgorithm | ( | ) | const |
Return the algorithm used for the optimization.
You may be interested in this value if you didn't specify an algorithm, or specified for Simbody to choose the BestAvailable algorithm. This method won't return BestAvailable, even if it's the 'algorithm' that you chose.
void SimTK::Optimizer::useNumericalGradient | ( | bool | flag, |
Real | estimatedAccuracyOfObjective = SignificantReal |
||
) |
Enable numerical calculation of gradient, with optional estimation of the accuracy to which the objective function is calculated.
For example, if you are calculate about 6 significant digits, supply the estimated accuracy as 1e-6. Providing the estimated accuracy improves the quality of the calculated derivative. If no accuracy is provided we'll assume the objective is calculated to near machine precision. The method used for calculating the derivative will be whatever was previously supplied in a call to setDifferentiatorMethod(), or the default which is to use central differencing (two function evaluations per gradient entry). See SimTK::Differentiator for more information.
void SimTK::Optimizer::useNumericalJacobian | ( | bool | flag, |
Real | estimatedAccuracyOfConstraints = SignificantReal |
||
) |
Enable numerical calculation of the constraint Jacobian, with optional estimation of the accuracy to which the constraint functions are calculated.
For example, if you are calculating about 6 significant digits, supply the estimated accuracy as 1e-6. Providing the estimated accuracy improves the quality of the calculated derivative. If no accuracy is provided we'll assume the constraints are calculated to near machine precision. The method used for calculating the derivative will be whatever was previously supplied in a call to setDifferentiatorMethod(), or the default which is to use central differencing (two function evaluations per Jacobian column. See SimTK::Differentiator for more information.
const OptimizerSystem& SimTK::Optimizer::getOptimizerSystem | ( | ) | const |
Return a reference to the OptimizerSystem currently associated with this Optimizer.
bool SimTK::Optimizer::isUsingNumericalGradient | ( | ) | const |
Indicate whether the Optimizer is currently set to use a numerical gradient.
bool SimTK::Optimizer::isUsingNumericalJacobian | ( | ) | const |
Indicate whether the Optimizer is currently set to use a numerical Jacobian.
Real SimTK::Optimizer::getEstimatedAccuracyOfObjective | ( | ) | const |
Return the estimated accuracy last specified in useNumericalGradient().
Real SimTK::Optimizer::getEstimatedAccuracyOfConstraints | ( | ) | const |
Return the estimated accuracy last specified in useNumericalJacobian().
|
friend |