ocra-recipes
Doxygen documentation for the ocra-recipes repository
QuadraticSolver.cpp
Go to the documentation of this file.
2 
3 namespace
4 {
5  template<class T, class Y>
6  void eraseFunctionFromSolver
7  (std::vector<T*>& v, void (ocra::Solver::*internalRemove)(const Y&), ocra::Solver* solver)
8  {
9  while(v.size())
10  {
11  (solver->*internalRemove)(*v.back());
12  v.pop_back();
13  }
14  }
15 }
16 
17 namespace ocra
18 {
20  {
22  _objectives.push_back(&obj);
23  }
24 
26  {
27  for(size_t i = 0; i < _objectives.size(); ++i)
28  {
29  if(&_objectives[i]->getFunction() == &obj)
30  {
32  break;
33  }
34  }
35  }
36 
38  {
40  _objectives.erase(std::find(_objectives.begin(), _objectives.end(), &obj));
41  }
42 
44  {
45  internalAddConstraint(constraint);
46  if(constraint.isEquality())
47  _equalityConstraints.push_back(&constraint);
48  else
49  _inequalityConstraints.push_back(&constraint);
50  _invalidatedMP = true;
51  }
52 
54  {
55  internalRemoveConstraint(constraint);
56  if(constraint.isEquality())
57  _equalityConstraints.erase(std::find(_equalityConstraints.begin(), _equalityConstraints.end(), &constraint));
58  else
59  _inequalityConstraints.erase(std::find(_inequalityConstraints.begin(), _inequalityConstraints.end(), &constraint));
60  _invalidatedMP = true;
61  }
62 
63 
65  {
66  addBounds_(constraint);
67  }
68 
70  {
71  addBounds_(constraint);
72  }
73 
74  void QuadraticSolver::addBounds_(DiagonalLinearConstraint& constraint)
75  {
76  ocra_assert(constraint.isInequality() && "How queer! A bound constraint that is no inequality...");
78  addConstraint(constraint);
79  else
80  {
81  internalAddConstraint(constraint);
82  _bounds.push_back(&constraint);
83  }
84  }
85 
86 
88  {
89  removeBounds_(constraint);
90  }
91 
93  {
94  removeBounds_(constraint);
95  }
96 
97  void QuadraticSolver::removeBounds_(DiagonalLinearConstraint& constraint)
98  {
99  ocra_assert(constraint.isInequality() && "How queer! A bound constraint that is no inequality...");
101  removeConstraint(constraint);
102  else
103  {
104  internalRemoveConstraint(constraint);
105  _bounds.erase(std::find(_bounds.begin(), _bounds.end(), &constraint));
106  }
107  }
108 
109 
111  {
112  eraseFunctionFromSolver(_objectives, &QuadraticSolver::internalRemoveObjective, this);
113  }
114 
116  {
119  }
120 
122  {
123  eraseFunctionFromSolver(_equalityConstraints, &QuadraticSolver::internalRemoveConstraint, this);
124  }
125 
127  {
128  eraseFunctionFromSolver(_inequalityConstraints, &QuadraticSolver::internalRemoveConstraint, this);
129  }
130 
132  {
133  eraseFunctionFromSolver(_bounds, &QuadraticSolver::internalRemoveConstraint, this);
134  }
135 
136 
138  {
139  std::cout << "objective(s):" <<std::endl;
140  for (unsigned int i=0; i<_objectives.size(); ++i)
141  std::cout << _objectives[i]->getValue() << std::endl;
142  std::cout << "equalities:" <<std::endl;
143  for (unsigned int i=0; i<_equalityConstraints.size(); ++i)
144  std::cout << _equalityConstraints[i]->getValue() << std::endl;
145  std::cout << "inequalities:" <<std::endl;
146  for (unsigned int i=0; i<_inequalityConstraints.size(); ++i)
147  std::cout << _inequalityConstraints[i]->getValue() << std::endl;
148  }
149 
151  {
152  _invalidatedMP = true;
153  }
154 
156  {
157  _m = 0;
158  _p = 0;
159  _ps = 0;
160  for (size_t i=0; i<_equalityConstraints.size(); ++i)
161  _m += _equalityConstraints[i]->getDimension();
162 
163  for (size_t i=0; i<_inequalityConstraints.size(); ++i)
164  {
165  _p += _inequalityConstraints[i]->getDimension();
166  if (_inequalityConstraints[i]->getType() == CSTR_LOWER_AND_GREATER)
167  _ps += 2*_inequalityConstraints[i]->getDimension();
168  else
169  _ps += _inequalityConstraints[i]->getDimension();
170  }
171 
172  _invalidatedMP = false;
173  }
174 
176  {
177  std::stringstream ss;
178 
179  ss << "P = " << std::endl << getP() << std::endl << std::endl;
180  ss << "q = " << std::endl << getq() << std::endl << std::endl;
181  ss << "A = " << std::endl << getA() << std::endl << std::endl;
182  ss << "b = " << std::endl << getb() << std::endl << std::endl;
183  ss << "b' = " << std::endl << getbp() << std::endl << std::endl;
184  ss << "C = " << std::endl << getC() << std::endl << std::endl;
185  ss << "d = " << std::endl << getd() << std::endl << std::endl;
186  ss << "l = " << std::endl << getl() << std::endl << std::endl;
187  ss << "u = " << std::endl << getu() << std::endl << std::endl;
188  ss << "xl = " << std::endl << getxl() << std::endl << std::endl;
189  ss << "xu = " << std::endl << getxu() << std::endl << std::endl;
190 
191  return ss.str();
192  }
193 }
194 
195 // cmake:sourcegroup=Solvers
void internalAddConstraint(const GenericConstraint &constraint)
Definition: Solver.cpp:91
Objective class.
Definition: Objective.h:44
virtual VectorXd getxl() const =0
void internalRemoveConstraint(const GenericConstraint &constraint)
Definition: Solver.cpp:121
Declaration file of the QuadraticSolver class.
virtual MatrixXd getC() const =0
void internalAddObjective(const GenericObjective &objective)
Definition: Solver.cpp:77
virtual VectorXd getb() const =0
std::vector< LinearConstraint * > _equalityConstraints
void removeConstraint(LinearConstraint &constraint)
virtual VectorXd getu() const =0
void addBounds(BoundConstraint &constraint)
void internalRemoveObjective(const GenericObjective &objective)
Definition: Solver.cpp:106
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual VectorXd getd() const =0
void removeBounds(BoundConstraint &constraint)
Constraint class.
Definition: Constraint.h:100
void addConstraint(LinearConstraint &constraint)
virtual VectorXd getq() const =0
virtual VectorXd getxu() const =0
std::vector< LinearConstraint * > _inequalityConstraints
virtual VectorXd getbp() const =0
void addObjective(QuadraticObjective &obj)
void removeObjective(QuadraticFunction &obj)
virtual MatrixXd getP() const =0
std::vector< DiagonalLinearConstraint * > _bounds
std::vector< QuadraticObjective * > _objectives
virtual MatrixXd getA() const =0
virtual VectorXd getl() const =0
const bool _boundsAsConstraints
void onConstraintResize(int timestamp)
#define ocra_assert(ocra_expression)
Definition: ocra_assert.h:45
QuadraticFunction class.
Solver class.
Definition: Solver.h:70