To solve an optimization problem with bound constraints. More...
Public Types | |
enum | OptMethod { GRADIENT = 0, TRUNCATED_NEWTON = 1, SIMULATED_ANNEALING = 2, NELDER_MEAD = 3, NEWTON = 4 } |
Choose optimization algorithm. More... | |
Public Member Functions | |
OptSolver () | |
Default constructor. | |
OptSolver (Vect< real_t > &x) | |
Constructor using vector of optimization variables. More... | |
OptSolver (MyOpt &opt, Vect< real_t > &x) | |
Constructor using vector of optimization variables. More... | |
~OptSolver () | |
Destructor. | |
void | set (Vect< real_t > &x) |
Set Solution vector. | |
int | getNbFctEval () const |
Return the total number of function evaluations. | |
void | setOptMethod (OptMethod m) |
Choose optimization method. More... | |
void | setBC (const Vect< real_t > &bc) |
Prescribe boundary conditions as constraints. More... | |
void | setObjective (string exp) |
Define the objective function to minimize by an algebraic expression. More... | |
void | setGradient (string exp, int i=1) |
Define a component of the gradient of the objective function to minimize by an algebraic expression. More... | |
void | setHessian (string exp, int i=1, int j=1) |
Define an entry of the Hessian matrix. More... | |
void | setIneqConstraint (string exp, real_t penal=1./OFELI_TOLERANCE) |
Impose an inequatity constraint by a penalty method. More... | |
void | setEqConstraint (string exp, real_t penal=1./OFELI_TOLERANCE) |
Impose an equatity constraint by a penalty method. More... | |
void | setObjective (function< real_t(real_t)> f) |
Define the objective function by a user defined one-variable function. More... | |
void | setObjective (function< real_t(Vect< real_t >)> f) |
Define the objective function by a user defined multi-variable function. More... | |
void | setGradient (function< real_t(real_t)> f) |
Define the derivative of the objective function by a user defined function. More... | |
void | setGradient (function< Vect< real_t >(Vect< real_t >)> f) |
Define the gradient of the objective function by a user defined function. More... | |
void | setOptClass (MyOpt &opt) |
Choose user defined optimization class. More... | |
void | setLowerBound (size_t i, real_t lb) |
Define lower bound for a particular optimization variable. More... | |
void | setUpperBound (size_t i, real_t ub) |
Define upper bound for a particular optimization variable. More... | |
void | setEqBound (size_t i, real_t b) |
Define value to impose to a particular optimization variable. More... | |
void | setUpperBound (real_t ub) |
Define upper bound for optimization variable. More... | |
void | setUpperBounds (Vect< real_t > &ub) |
Define upper bounds for optimization variables. More... | |
void | setLowerBound (real_t lb) |
Define lower bound for optimization variable. More... | |
void | setLowerBounds (Vect< real_t > &lb) |
Define lower bounds for optimization variables. More... | |
void | setSAOpt (real_t rt, int ns, int nt, int &neps, int maxevl, real_t t, Vect< real_t > &vm, Vect< real_t > &xopt, real_t &fopt) |
Set Simulated annealing options. More... | |
void | setTolerance (real_t toler) |
Set error tolerance. More... | |
void | setMaxIterations (int n) |
Set maximal number of iterations. | |
int | getNbObjEval () const |
Return number of objective function evaluations. | |
real_t | getTemperature () const |
Return the final temperature. More... | |
int | getNbAcc () const |
Return the number of accepted objective function evaluations. More... | |
int | getNbOutOfBounds () const |
Return the total number of trial function evaluations that would have been out of bounds. More... | |
real_t | getOptObj () const |
Return Optimal value of the objective. | |
int | run () |
Run the optimization algorithm. More... | |
int | run (real_t toler, int max_it) |
Run the optimization algorithm. More... | |
real_t | getSolution () const |
Return solution in the case of a one variable optimization. More... | |
void | getSolution (Vect< real_t > &x) const |
Get solution vector. More... | |
Friends | |
ostream & | operator<< (ostream &s, const OptSolver &os) |
Output class information. | |
To solve an optimization problem with bound constraints.
enum OptMethod |
Constructor using vector of optimization variables.
[in] | x | Vector having as size the number of optimization variables. It contains the initial guess for the optimization algorithm. |
Constructor using vector of optimization variables.
[in] | opt | Reference to instance of user defined optimization class. This class inherits from abstract class MyOpt. It must contain the member function double Objective(Vect<double> &x) which returns the value of the objective for a given solution vector x . The user defined class must contain, if the optimization algorithm requires it the member function Gradient(Vect<double> &x, Vect<double> &g) which stores the gradient of the objective in the vector g for a given optimization vector x . The user defined class must also contain, if the optimization algorithm requires it the member function |
[in] | x | Vector having as size the number of optimization variables. It contains the initial guess for the optimization algorithm. |
int getNbAcc | ( | ) | const |
Return the number of accepted objective function evaluations.
This function is meaningful only if the Simulated Annealing algorithm is used
int getNbOutOfBounds | ( | ) | const |
Return the total number of trial function evaluations that would have been out of bounds.
This function is meaningful only if the Simulated Annealing algorithm is used
real_t getSolution | ( | ) | const |
Return solution in the case of a one variable optimization.
In the case of a one variable problem, the solution value is returned, if the optimization procedure was successful
Get solution vector.
The vector x contains the solution of the optimization problem. Note that if the constructor using an initial vector was used, the vector will contain the solution once the member function run has beed used (If the optimization procedure was successful)
[out] | x | solution vector |
real_t getTemperature | ( | ) | const |
Return the final temperature.
This function is meaningful only if the Simulated Annealing algorithm is used
int run | ( | ) |
Run the optimization algorithm.
This function runs the optimization procedure using default values for parameters. To modify these values, user the function run with arguments
int run | ( | real_t | toler, |
int | max_it | ||
) |
Run the optimization algorithm.
[in] | toler | Tolerance value for convergence testing |
[in] | max_it | Maximal number of iterations to achieve convergence |
Prescribe boundary conditions as constraints.
This member function is useful in the case of optimization problems where the optimization variable vector is the solution of a partial differential equation. For this case, Dirichlet boundary conditions can be prescribed as constraints for the optimization problem
[in] | bc | Vector containing the values to impose on degrees of freedom. This vector must have been constructed using the Mesh instance. |
void setEqBound | ( | size_t | i, |
real_t | b | ||
) |
Define value to impose to a particular optimization variable.
Method to impose a value for a component of the optimization variable
[in] | i | Index of component to enforce (index starts from 1 ) |
[in] | b | Value to impose |
void setEqConstraint | ( | string | exp, |
real_t | penal = 1./OFELI_TOLERANCE |
||
) |
Impose an equatity constraint by a penalty method.
The constraint is of the form F(x) = 0 where F is any function of the optimization variable vector v
[in] | exp | Regular expression defining the constraint (the function F |
[in] | penal | Penalty parameter (large number) [Default: 1./DBL_EPSILON ] |
void setGradient | ( | string | exp, |
int | i = 1 |
||
) |
Define a component of the gradient of the objective function to minimize by an algebraic expression.
[in] | exp | Regular expression defining the objective function |
[in] | i | Component of gradient [Default: 1 ] |
Define the derivative of the objective function by a user defined function.
[in] | f | Function given as a function of a real variable and returning the derivative of the objective value. This function can be defined by the calling program as a C-function and then cast to an instance of class function |
Define the gradient of the objective function by a user defined function.
[in] | f | Function given as a function of a many real variables and returning the partial derivatives of the objective value. This function can be defined by the calling program as a C-function and then cast to an instance of class function |
void setHessian | ( | string | exp, |
int | i = 1 , |
||
int | j = 1 |
||
) |
Define an entry of the Hessian matrix.
[in] | exp | Regular expression defining the Hessian matrix entry |
[in] | i | i-th row of Hessian matrix [Default: 1 ] |
[in] | j | j-th column of Hessian matrix [Default: 1 ] |
void setIneqConstraint | ( | string | exp, |
real_t | penal = 1./OFELI_TOLERANCE |
||
) |
Impose an inequatity constraint by a penalty method.
The constraint is of the form F(x) <= 0 where F is any function of the optimization variable vector v
[in] | exp | Regular expression defining the constraint (the function F |
[in] | penal | Penalty parameter (large number) [Default: 1./DBL_EPSILON ] |
void setLowerBound | ( | size_t | i, |
real_t | lb | ||
) |
Define lower bound for a particular optimization variable.
Method to impose a lower bound for a component of the optimization variable
[in] | i | Index of component to bound (index starts from 1 ) |
[in] | lb | Lower bound |
void setLowerBound | ( | real_t | lb | ) |
Define lower bound for optimization variable.
Case of a one-variable problem
[in] | lb | Lower value |
Define lower bounds for optimization variables.
[in] | lb | Vector containing lower values for variables |
void setObjective | ( | string | exp | ) |
Define the objective function to minimize by an algebraic expression.
[in] | exp | Regular expression defining the objective function |
Define the objective function by a user defined one-variable function.
This function can be used in the case where a user defined function is to be given. To be used in the one-variable case.
[in] | f | Function given as a function of one real variable which is the optimization variable and returning the objective value. This function can be defined by the calling program as a C-function and then cast to an instance of class function |
Define the objective function by a user defined multi-variable function.
This function can be used in the case where a user defined function is to be given. To be used in the multivariable case.
[in] | f | Function given as a function of many real variables and returning the objective value. This function can be defined by the calling program as a C-function and then cast to an instance of class function |
void setOptClass | ( | MyOpt & | opt | ) |
Choose user defined optimization class.
[in] | opt | Reference to inherited user specified optimization class |
void setOptMethod | ( | OptMethod | m | ) |
Choose optimization method.
[in] | m | Enumerated value to choose the optimization algorithm to use. Must be chosen among the enumerated values:
|
void setSAOpt | ( | real_t | rt, |
int | ns, | ||
int | nt, | ||
int & | neps, | ||
int | maxevl, | ||
real_t | t, | ||
Vect< real_t > & | vm, | ||
Vect< real_t > & | xopt, | ||
real_t & | fopt | ||
) |
Set Simulated annealing options.
[in] | rt | The temperature reduction factor. The value suggested by Corana et al. is .85. See Goffe et al. for more advice. |
[in] | ns | Number of cycles. After ns*nb_var function evaluations, each element of vm is adjusted so that approximately half of all function evaluations are accepted. The suggested value is 20. |
[in] | nt | Number of iterations before temperature reduction. After nt*ns*n function evaluations, temperature (t) is changed by the factor rt. Value suggested by Corana et al. is max(100,5*nb_var). See Goffe et al. for further advice. |
[in] | neps | Number of final function values used to decide upon termination. See eps . Suggested value is 4 |
[in] | maxevl | The maximum number of function evaluations. If it is exceeded, the return code=1. |
[in] | t | The initial temperature. See Goffe et al. for advice. |
[in] | vm | The step length vector. On input it should encompass the region of interest given the starting value x. For point x[i], the next trial point is selected is from x[i]-vm[i] to x[i]+vm[i]. Since vm is adjusted so that about half of all points are accepted, the input value is not very important (i.e. is the value is off, OptimSA adjusts vm to the correct value). |
[out] | xopt | optimal values of optimization variables |
[out] | fopt | Optimal value of objective |
void setTolerance | ( | real_t | toler | ) |
Set error tolerance.
[in] | toler | Error tolerance for termination. If the final function values from the last neps temperatures differ from the corresponding value at the current temperature by less than eps and the final function value at the current temperature differs from the current optimal function value by less than toler, execution terminates and the value 0 is returned. |
void setUpperBound | ( | size_t | i, |
real_t | ub | ||
) |
Define upper bound for a particular optimization variable.
Method to impose an upper bound for a component of the optimization variable
[in] | i | Index of component to bound (index starts from 1 ) |
[in] | ub | Upper bound |
void setUpperBound | ( | real_t | ub | ) |
Define upper bound for optimization variable.
Case of a one-variable problem
[in] | ub | Upper bound |