ocra-recipes
Doxygen documentation for the ocra-recipes repository
Solver.cpp
Go to the documentation of this file.
1 #include "ocra/optim/Solver.h"
3 #include <boost/foreach.hpp>
4 #include <sstream>
5 #include <stdexcept>
6 #include <fstream>
7 
8 namespace ocra
9 {
10  // --- Restricted access to auto_map ------------------------------
11 
12  const std::vector<int>& Solver::findMapping(Variable& var)
13  {
14  VariableMapping* result = _problemVariable.find(&var);
16  result &&
17  "Attempt to find a variable, means you have a pointer on it, which means it has been added through a function, which means it should have been registered..."
18  );
19  return result->getMapping();
20  }
21 
22 
23  // --- Solver -----------------------------------------------------
24 
26  :NamedInstance("solver")
27  ,_result()
28  ,_problemVariable("SolverVariable")
29  ,_objectives()
30  ,_constraints()
31  ,_memory()
32  ,_autodumpFile()
33  ,_autodump(false)
34  {
35  }
36 
38  {
39  _result.solution.resize(_problemVariable.getVariable().getSize());
40 
41  doPrepare();
42 
43  if (_memory.capacity() > 0)
44  _memory.push_back(toString());
45 
46  doSolve();
47 
48  if (_autodump && _result.info!=RETURN_SUCCESS)
49  dump(_autodumpFile);
50 
51  doConclude();
52 
53  // TODO: We get a seg fault here when a task is deleted after it has been removed.
54  _problemVariable.setValue(_result.solution);
55 
56  return _result;
57  }
58 
60  {
61  return _result;
62  }
63 
64  void Solver::printStatus(std::ostream& os)
65  {
66  //TODO
67  os << "Solver status:" << std::endl;
68  os << "\tSize: " << n() << std::endl;
69  }
70 
71  const std::string& Solver::getMoreInfo() const
72  {
73  static const std::string info = "Abstract solver";
74  return info;
75  }
76 
78  {
79  if (std::find(_objectives.begin(), _objectives.end(), &objective) != _objectives.end())
80  {
81  std::cout << "[ocra::Solver::internalAddObjective] Objective was already added in the solver, so it cannot be added again\n" << "\tObjective is named: " << objective.getName() << std::endl;
82  }
83  else
84  {
86  _objectives.push_back(&objective);
87  _problemVariable.insert(&const_cast<GenericObjective*>(&objective)->getVariable());
88  }
89  }
90 
92  {
93  if (std::find(_constraints.begin(), _constraints.end(), &constraint) != _constraints.end())
94  {
95  std::cout << "[ocra::Solver::internalAddConstraint] Constraint was already added in the solver, so it cannot be added again\n" << "\tConstraint is named: " << constraint.getName() << std::endl;
96  }
97  else
98  {
100  constraint.connect(*this, &Solver::onConstraintResize);
101  _constraints.push_back(&constraint);
102  _problemVariable.insert(&const_cast<GenericConstraint*>(&constraint)->getVariable());
103  }
104  }
105 
107  {
108  std::vector<const GenericObjective*>::iterator it = std::find(_objectives.begin(), _objectives.end(), &objective);
109  if(it == _objectives.end())
110  {
111  std::cout << "[ocra::Solver::internalRemoveObjective] Objective was not added in the solver, so it cannot be removed\n" << "\tObjective is named: " << objective.getName() << std::endl;
112  }
113  else
114  {
115  _objectives.erase(it);
117  _problemVariable.remove(&const_cast<GenericObjective*>(&objective)->getVariable());
118  }
119  }
120 
122  {
123  std::vector<const GenericConstraint*>::iterator it = std::find(_constraints.begin(), _constraints.end(), &constraint);
124  if(it == _constraints.end())
125  {
126  std::cout << "[ocra::Solver::internalRemoveConstraint] Constraint was not added in the solver, or has already been removed, so it cannot be removed\n" << "\tConstraint is named: " << constraint.getName() << std::endl;
127  }
128  else
129  {
130  _constraints.erase(it);
131  constraint.disconnect(*this, &Solver::onConstraintResize);
133  _problemVariable.remove(&const_cast<GenericConstraint*>(&constraint)->getVariable());
134  }
135  }
136 
137  void Solver::onConstraintResize(int timestamp)
138  {
139  //do nothing
140  }
141 
142  void Solver::onObjectiveResize(int timestamp)
143  {
144  //do nothing
145  }
146 
147  void Solver::setMemoryLevel(int level)
148  {
149  _memory.set_capacity(level);
150  }
151 
152  void Solver::setAutoDumpFile(const std::string& file)
153  {
154  if(file.empty())
155  throw std::runtime_error("[Solver::setAutoDumpFile] Invalid (empty) filename!");
156 
157  _autodumpFile = file;
158  }
159 
161  {
162  _autodump = true;
163  }
164 
166  {
167  _autodump = false;
168  }
169 
170  void Solver::dump(const std::string& file) const
171  {
172  if(file.empty())
173  std::copy(_memory.begin(), _memory.end(), std::ostream_iterator<std::string>(std::cout, "\r\n\r\n----------------\r\n"));
174  else
175  {
176  std::ofstream os(file.c_str());
177  std::copy(_memory.begin(), _memory.end(), std::ostream_iterator<std::string>(os, "\r\n\r\n----------------\r\n"));
178  }
179  }
180 }
181 
182 // cmake:sourcegroup=Solvers
void internalAddConstraint(const GenericConstraint &constraint)
Definition: Solver.cpp:91
virtual Function & getFunction()
Definition: Constraint.h:231
void printStatus(std::ostream &os)
Definition: Solver.cpp:64
void internalRemoveConstraint(const GenericConstraint &constraint)
Definition: Solver.cpp:121
void insert(Variable *var)
void internalAddObjective(const GenericObjective &objective)
Definition: Solver.cpp:77
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
void connect(T &object, typename SubjectBaseTraits< EVT, T >::callback_type newCallback) const
Call this method to register a non-static method as a callback.
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
int getSize() const
Definition: Variable.cpp:81
virtual void doConclude()=0
void setAutoDumpFile(const std::string &file)
Definition: Solver.cpp:152
void remove(Variable *var)
void disconnect(T &object, typename SubjectBaseTraits< EVT, T >::callback_type callback) const
Disconnect non-static method.
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
Declaration file of the Solver class.
virtual const std::string & getMoreInfo() const
Definition: Solver.cpp:71
const std::vector< int > & getMapping() const
void setMemoryLevel(int level)
Definition: Solver.cpp:147
const OptimizationResult & getLastResult() const
Definition: Solver.cpp:59
A class to manage the relative mapping of a variable with respect to another one. ...
void activateAutoDump()
Definition: Solver.cpp:160
virtual Function & getFunction()
Definition: Objective.h:97
VariableMapping * find(Variable *var) const
int n()
Definition: Solver.h:146
CompositeVariable & getVariable()
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
virtual std::string toString()=0
void disconnect(Derived &object, void(Base::*callbackToErase)(int)) const
Disconnect non-static method.
void connect(Derived &object, void(Base::*newCallback)(int)) const
Call this method to register a non-static method as a callback.
virtual void doSolve()=0
Declaration file of the VariableMapping class.