ocra-recipes
Doxygen documentation for the ocra-recipes repository
Solver.h
Go to the documentation of this file.
1 
10 #ifndef _OCRABASE_SOLVER_H_
11 #define _OCRABASE_SOLVER_H_
12 
13 //ocra includes
14 #include <ocra/util/MathTypes.h>
15 #include "ocra/optim/Variable.h"
18 #include "ocra/optim/Objective.h"
19 #include "ocra/optim/Constraint.h"
21 #include "ocra/util/ErrorsHelper.h"
22 
23 //boost
24 #include <boost/circular_buffer.hpp>
25 
26 //std includes
27 #include <string>
28 #include <map>
29 #include <iosfwd>
30 
36 namespace ocra
37 {
70  class Solver
71  : public ObserverSubject
72  , virtual public NamedInstance
73  {
75  public: // restricted access to automap
76  const std::vector<int>& findMapping(Variable& var);
77 
78  // ------------------------ constructors ------------------------------------
79  private:
82  Solver(const Solver&);
83  Solver& operator=(const Solver&);
85  protected:
87  Solver();
88  public:
89  virtual ~Solver() {}
90 
91  // ------------------------ public interface --------------------------------
92  public:
100  const OptimizationResult& solve();
101 
103  const OptimizationResult& getLastResult() const;
104 
105  void printStatus(std::ostream& os);
106 
107  virtual const std::string& getMoreInfo() const;
108 
109  virtual void printValuesAtSolution() = 0;
111  virtual std::string toString() = 0;
112 
120  void setMemoryLevel(int level);
121 
129  void setAutoDumpFile(const std::string& file);
130 
132  void activateAutoDump();
133 
135  void deactivateAutoDump();
136 
142  void dump(const std::string& file = "") const;
143 
144  // ------------------------ protected methods -------------------------------
145 
146  int n(){return _problemVariable.getVariable().getSize();}
147 
148  Variable& getProblemVariable(){return _problemVariable.getVariable();}
149 
150  void setVariableValue(const VectorXd& value) {_problemVariable.setValue(value);}
151 protected:
164  virtual void doPrepare() = 0;
165  virtual void doSolve() = 0;
166  virtual void doConclude() = 0;
168 
175  void internalAddObjective(const GenericObjective& objective);
182  void internalAddConstraint(const GenericConstraint& constraint);
189  void internalRemoveObjective(const GenericObjective& objective);
196  void internalRemoveConstraint(const GenericConstraint& constraint);
197 
204  virtual void onConstraintResize(int timestamp);
205 
211  virtual void onObjectiveResize(int timestamp);
212 
213  // ------------------------ protected members ---------------------------------
214  protected:
215  OptimizationResult _result; //< structure to store the result of an optimization problem.
216 
217  // ------------------------ private members ---------------------------------
218  private:
222  bool _isVariableUpToDate;
226  MergedVariable _problemVariable;
230  std::vector<const GenericObjective*> _objectives;
234  std::vector<const GenericConstraint*> _constraints;
238  boost::circular_buffer<std::string> _memory;
242  std::string _autodumpFile;
243  bool _autodump;
244  };
245 }
246 
247 #endif //_OCRABASE_SOLVER_H_
248 
249 // cmake:sourcegroup=Solvers
void internalAddConstraint(const GenericConstraint &constraint)
Definition: Solver.cpp:91
void setVariableValue(const VectorXd &value)
Definition: Solver.h:150
void printStatus(std::ostream &os)
Definition: Solver.cpp:64
void internalRemoveConstraint(const GenericConstraint &constraint)
Definition: Solver.cpp:121
void internalAddObjective(const GenericObjective &objective)
Definition: Solver.cpp:77
virtual ~Solver()
Definition: Solver.h:89
virtual void doPrepare()=0
OptimizationResult _result
Definition: Solver.h:215
void setValue(const VectorXd &val) const
const OptimizationResult & solve()
Definition: Solver.cpp:37
const std::vector< int > & findMapping(Variable &var)
Definition: Solver.cpp:12
#define DEFINE_CLASS_POINTER_TYPEDEFS(Class)
Definition: Macros.h:8
void internalRemoveObjective(const GenericObjective &objective)
Definition: Solver.cpp:106
void dump(const std::string &file="") const
Definition: Solver.cpp:170
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual void onObjectiveResize(int timestamp)
Definition: Solver.cpp:142
void deactivateAutoDump()
Definition: Solver.cpp:165
virtual void onConstraintResize(int timestamp)
Definition: Solver.cpp:137
Variable & getProblemVariable()
Definition: Solver.h:148
int getSize() const
Definition: Variable.cpp:81
virtual void doConclude()=0
void setAutoDumpFile(const std::string &file)
Definition: Solver.cpp:152
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
virtual const std::string & getMoreInfo() const
Definition: Solver.cpp:71
Declaration file of the Constraint class.
void setMemoryLevel(int level)
Definition: Solver.cpp:147
const OptimizationResult & getLastResult() const
Definition: Solver.cpp:59
void activateAutoDump()
Definition: Solver.cpp:160
int n()
Definition: Solver.h:146
CompositeVariable & getVariable()
virtual std::string toString()=0
Declaration file of the Objective class.
Declaration file of the Variable class.
virtual void printValuesAtSolution()=0
Declaration file of the OptimizationVariable class.
Solver class.
Definition: Solver.h:70
virtual void doSolve()=0