ocra-recipes
Doxygen documentation for the ocra-recipes repository
|
Solver class. More...
#include <Solver.h>
Public Member Functions | |
const std::vector< int > & | findMapping (Variable &var) |
virtual | ~Solver () |
const OptimizationResult & | solve () |
const OptimizationResult & | getLastResult () const |
void | printStatus (std::ostream &os) |
virtual const std::string & | getMoreInfo () const |
virtual void | printValuesAtSolution ()=0 |
virtual std::string | toString ()=0 |
void | setMemoryLevel (int level) |
void | setAutoDumpFile (const std::string &file) |
void | activateAutoDump () |
void | deactivateAutoDump () |
void | dump (const std::string &file="") const |
int | n () |
Variable & | getProblemVariable () |
void | setVariableValue (const VectorXd &value) |
Public Member Functions inherited from ocra::ObserverSubject | |
ObserverSubject () | |
virtual | ~ObserverSubject () |
Public Member Functions inherited from ocra::Observer | |
virtual | ~Observer () |
template<int EVT> | |
void | bind (SubjectBase< EVT > &subject) |
Call this method to automatically propagate observed events to observers connected to the subject given in argument. More... | |
template<int EVT> | |
void | stopPropagation () |
Public Member Functions inherited from ocra::ObserverBase< EVT_RESIZE > | |
void | bind (subject_type &subject) |
Call this method to automatically propagate observed events to observers connected to the subject given in argument. More... | |
Public Member Functions inherited from ocra::ObserverBase< EVT_CHANGE_DEPENDENCIES > | |
void | bind (subject_type &subject) |
Call this method to automatically propagate observed events to observers connected to the subject given in argument. More... | |
Public Member Functions inherited from ocra::ObserverBase< EVT_CHANGE_VALUE > | |
void | bind (subject_type &subject) |
Call this method to automatically propagate observed events to observers connected to the subject given in argument. More... | |
Public Member Functions inherited from ocra::Subject | |
virtual | ~Subject () |
template<int EVT, class Derived , class Base > | |
void | connect (Derived &object, void(Base::*newCallback)(int)) const |
Call this method to register a non-static method as a callback. More... | |
template<int EVT> | |
void | connect (void(*newCallback)(int)) const |
Call this method to register a free function as a callback. More... | |
template<int EVT, class Derived , class Base > | |
void | disconnect (Derived &object, void(Base::*callbackToErase)(int)) const |
Disconnect non-static method. More... | |
template<int EVT> | |
void | disconnect (void(*callbackToErase)(int)) const |
Disconnect free function. More... | |
template<int EVT> | |
void | propagate () const |
template<int EVT> | |
void | propagate (int timestamp) const |
Public Member Functions inherited from ocra::SubjectBase< EVT_RESIZE > | |
void | connect (T &object, typename SubjectBaseTraits< EVT, T >::callback_type newCallback) const |
Call this method to register a non-static method as a callback. More... | |
void | connect (typename SubjectBaseTraits< EVT, void >::callback_type newCallback) const |
Call this method to register a free function as a callback. More... | |
void | disconnect (T &object, typename SubjectBaseTraits< EVT, T >::callback_type callback) const |
Disconnect non-static method. More... | |
void | disconnect (typename SubjectBaseTraits< EVT, void >::callback_type callbackToErase) const |
Disconnect free function. More... | |
void | propagate (int timestamp) const |
void | propagate () const |
Public Member Functions inherited from ocra::SubjectBase< EVT_CHANGE_DEPENDENCIES > | |
void | connect (T &object, typename SubjectBaseTraits< EVT, T >::callback_type newCallback) const |
Call this method to register a non-static method as a callback. More... | |
void | connect (typename SubjectBaseTraits< EVT, void >::callback_type newCallback) const |
Call this method to register a free function as a callback. More... | |
void | disconnect (T &object, typename SubjectBaseTraits< EVT, T >::callback_type callback) const |
Disconnect non-static method. More... | |
void | disconnect (typename SubjectBaseTraits< EVT, void >::callback_type callbackToErase) const |
Disconnect free function. More... | |
void | propagate (int timestamp) const |
void | propagate () const |
Public Member Functions inherited from ocra::SubjectBase< EVT_CHANGE_VALUE > | |
void | connect (T &object, typename SubjectBaseTraits< EVT, T >::callback_type newCallback) const |
Call this method to register a non-static method as a callback. More... | |
void | connect (typename SubjectBaseTraits< EVT, void >::callback_type newCallback) const |
Call this method to register a free function as a callback. More... | |
void | disconnect (T &object, typename SubjectBaseTraits< EVT, T >::callback_type callback) const |
Disconnect non-static method. More... | |
void | disconnect (typename SubjectBaseTraits< EVT, void >::callback_type callbackToErase) const |
Disconnect free function. More... | |
void | propagate (int timestamp) const |
void | propagate () const |
Public Member Functions inherited from ocra::NamedInstance | |
NamedInstance (const std::string &name) | |
const std::string & | getName () const |
virtual | ~NamedInstance () |
Protected Member Functions | |
Solver () | |
void | internalAddObjective (const GenericObjective &objective) |
void | internalAddConstraint (const GenericConstraint &constraint) |
void | internalRemoveObjective (const GenericObjective &objective) |
void | internalRemoveConstraint (const GenericConstraint &constraint) |
virtual void | onConstraintResize (int timestamp) |
virtual void | onObjectiveResize (int timestamp) |
virtual void | doPrepare ()=0 |
virtual void | doSolve ()=0 |
virtual void | doConclude ()=0 |
Protected Member Functions inherited from ocra::ObserverBase< EVT_RESIZE > | |
void | stopPropagation () |
Call this method from your callbacks to avoid propagation to the bound subject (if any). More... | |
ObserverBase () | |
~ObserverBase () | |
Protected Member Functions inherited from ocra::ObserverBase< EVT_CHANGE_DEPENDENCIES > | |
void | stopPropagation () |
Call this method from your callbacks to avoid propagation to the bound subject (if any). More... | |
ObserverBase () | |
~ObserverBase () | |
Protected Member Functions inherited from ocra::ObserverBase< EVT_CHANGE_VALUE > | |
void | stopPropagation () |
Call this method from your callbacks to avoid propagation to the bound subject (if any). More... | |
ObserverBase () | |
~ObserverBase () | |
Protected Member Functions inherited from ocra::SubjectBase< EVT_RESIZE > | |
SubjectBase () | |
~SubjectBase () | |
Protected Member Functions inherited from ocra::SubjectBase< EVT_CHANGE_DEPENDENCIES > | |
SubjectBase () | |
~SubjectBase () | |
Protected Member Functions inherited from ocra::SubjectBase< EVT_CHANGE_VALUE > | |
SubjectBase () | |
~SubjectBase () | |
Protected Attributes | |
OptimizationResult | _result |
Additional Inherited Members | |
Protected Types inherited from ocra::ObserverBase< EVT_RESIZE > | |
typedef SubjectBase< EVT > | subject_type |
typedef InvokerBase< EVT > | invoker_type |
Protected Types inherited from ocra::ObserverBase< EVT_CHANGE_DEPENDENCIES > | |
typedef SubjectBase< EVT > | subject_type |
typedef InvokerBase< EVT > | invoker_type |
Protected Types inherited from ocra::ObserverBase< EVT_CHANGE_VALUE > | |
typedef SubjectBase< EVT > | subject_type |
typedef InvokerBase< EVT > | invoker_type |
Solver class.
This is a base class for every optimization solver in ocra. It provides an interface for derived solvers and some core methods. It has two responsibilities of its own:
(i) can be explain by an example: if we want to optimize the sum of two functions f(x,y) and g(y,z), then we need to solve a problem over V=(x,y,z). V is said to be the problem variable. If at one point g becomes a function of y only, then we have a new problem variable V'=(x,y). The solver tracks correctly such changes to keep the problem variable up-to-date. This implies to observe the EVT_CHANGE_DEPENDENCIES events coming from the variables of all constraints and objectives. This observation is configured in the internalAdd/RemoveObjective/Constraint methods which the user must call when adding/removing an objective/constraint in a derived solver. When this event is triggered, a flag is simply change, the actual recomputation of the problem variable will only take automatically place, if needed, when the solve method is called.
(ii) the variable of any constraint or objective in an optimization problem is a subset of the problem variable. Many solvers need to know how a specific variable is mapped with respect to the problem variable. The Solver class provides facilities to keep the mapping of each constraint/objective's variable wrt the problem variable up-to-date. It is done in a transparent way for the user, who just need to use the findMapping() method to retrieve the mapping of a variable.
Some general rules which must be followed by the derived solver:
|
protected |
The class is meant to be derived
Definition at line 25 of file Solver.cpp.
void ocra::Solver::activateAutoDump | ( | ) |
Activates auto dumping when solver fails.
Definition at line 160 of file Solver.cpp.
void ocra::Solver::deactivateAutoDump | ( | ) |
Deactivates auto dumping when solver fails.
Definition at line 165 of file Solver.cpp.
|
protectedpure virtual |
Implemented in ocra::FSQPSolver, ocra::NewtonSolver, ocra::OneLevelSolver, ocra::CascadeQPSolver, and ocra::QLDSolver.
|
protectedpure virtual |
Implemented in ocra::FSQPSolver, ocra::NewtonSolver, ocra::CmlQuadraticSolver, ocra::OneLevelSolver, ocra::CascadeQPSolver, and ocra::QLDSolver.
|
protectedpure virtual |
void ocra::Solver::dump | ( | const std::string & | file = "" | ) | const |
Dumps the memory.
Empty string parameter (default): dumps on standard output. Else, dumps in specified file, from older to newer state.
Definition at line 170 of file Solver.cpp.
const std::vector< int > & ocra::Solver::findMapping | ( | Variable & | var | ) |
Definition at line 12 of file Solver.cpp.
const OptimizationResult & ocra::Solver::getLastResult | ( | ) | const |
Get the last result computed with solve()
Definition at line 59 of file Solver.cpp.
|
virtual |
Reimplemented in ocra::CmlQuadraticSolver, and ocra::QLDSolver.
Definition at line 71 of file Solver.cpp.
|
protected |
This method MUST be called whenever constraint is added to the optimization problem. Failing to do that will induce an erroneous management of the problem variable and the mappings.
constraint | Constraint. |
This | method will throw a runtime_error if one attempts to add an already added constraint that does not exist in the problem. |
Definition at line 91 of file Solver.cpp.
|
protected |
This method MUST be called whenever objective is added to the optimization problem. Failing to do that will induce an erroneous management of the problem variable and the mappings.
objective | Objective. |
This | method will throw a runtime_error if one attempts to add an already added objective that does not exist in the problem. |
Definition at line 77 of file Solver.cpp.
|
protected |
This method MUST be called whenever a constraint is removed from the optimization problem. Failing to do that will induce an erroneous management of the problem variable and the mappings.
constraint | Constraint to be removed. |
This | method will throw a runtime_error if one attempts to remove an already removed constraint that does not exist in the problem. |
Definition at line 121 of file Solver.cpp.
|
protected |
This method MUST be called whenever an objective is removed from the optimization problem. Failing to do that will induce an erroneous management of the problem variable and the mappings.
objective | Objective to be removed. |
This | method will throw a runtime_error if one attempts to remove an already removed objective that does not exist in the problem. |
Definition at line 106 of file Solver.cpp.
|
protectedvirtual |
Callback method to be invoked when the function of a constraint indicates an EVT_RESIZE, or the constraint itself triggers an EVT_CHANGE_BOUNDS_NUMBER event.
timestamp | Time stamp. |
Reimplemented in ocra::FSQPSolver, and ocra::QuadraticSolver.
Definition at line 137 of file Solver.cpp.
|
protectedvirtual |
Callback method to be invoked when the function of an objective triggers a EVT_RESIZE event
timestamp | Time stamp. |
Reimplemented in ocra::FSQPSolver.
Definition at line 142 of file Solver.cpp.
void ocra::Solver::printStatus | ( | std::ostream & | os | ) |
Definition at line 64 of file Solver.cpp.
|
pure virtual |
Implemented in ocra::FSQPSolver, ocra::QuadraticSolver, ocra::NewtonSolver, ocra::CascadeQPSolver, ocra::CmlQuadraticSolver, and ocra::OneLevelSolver.
void ocra::Solver::setAutoDumpFile | ( | const std::string & | file | ) |
Sets the file where memory will be dumped after solve() errors.
Activates auto dumping automatically. If an empty filename is given, throws runtime_error. Else, each time solve() fails, the memorized states (see setMemoryLevel) will be dumped into the specified file from older to newer state.
Definition at line 152 of file Solver.cpp.
void ocra::Solver::setMemoryLevel | ( | int | level | ) |
Sets the number of solver states that will be kept in memory.
If the level is set to 0, the state will never be saved. If it is set to 1, the current state (ie, the state before solving the problem) will be stored when calling solve(). If set to 2, the state before the last two solve() will be stored and so on.
Definition at line 147 of file Solver.cpp.
|
inline |
const OptimizationResult & ocra::Solver::solve | ( | ) |
Translate the actual optimization problem for a specific solver and call its optimization routine.
Definition at line 37 of file Solver.cpp.
|
pure virtual |
Returns the state of the solver (e.g. matrices) as a string.
Implemented in ocra::FSQPSolver, ocra::QuadraticSolver, ocra::NewtonSolver, ocra::OneLevelSolver, and ocra::CascadeQPSolver.
|
protected |