ocra-recipes
Doxygen documentation for the ocra-recipes repository
OneLevelSolver.h
Go to the documentation of this file.
1 
10 #ifndef __ONE_LEVEL_SOLVER_H__
11 #define __ONE_LEVEL_SOLVER_H__
12 
13 #include "ocra/optim/Solver.h"
14 #include "ocra/optim/Objective.h"
18 #include "ocra/optim/ObjQLD.h"
20 #include "ocra/control/Model.h"
21 #include <ocra/util/ErrorsHelper.h>
22 
23 #include "ocra/optim/QuadProg++.h"
24 
25 #include <string>
26 #include <Eigen/SVD>
27 
28 //#ifdef USE_QPOASES
29 
30 #include <qpOASES.hpp>
31 #include <mutex>
32 //#endif
33 
34 namespace ocra
35 {
36 
41 //typedef ocra::Objective<ocra::SquaredLinearFunction> SquaredLinearObjective;
42 //typedef ocra::Objective<ocra::QuadraticFunction> SquaredLinearObjective;
43 
44 typedef Eigen::Map<Eigen::MatrixXd> MatrixMap;
45 typedef Eigen::Map<Eigen::VectorXd> VectorMap;
46 
47 typedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic,Eigen::RowMajor> MatrixXdRm;
48 
59 {
62 public:
64  virtual ~OneLevelSolver();
65 
66  virtual void printValuesAtSolution();
67 
68 // virtual void addObjective(SquaredLinearObjective& obj);
69 // virtual void removeObjective(SquaredLinearObjective& obj);
70 // virtual void setObjectiveLevel(SquaredLinearObjective& obj, int level) = 0;
71 
73  {
75  _objectives.push_back(&obj);
76  }
78  {
80  _objectives.erase(std::find(_objectives.begin(), _objectives.end(), &obj));
81  }
82 
83  void addConstraint(ocra::LinearConstraint& constraint);
84  void removeConstraint(ocra::LinearConstraint& constraint);
85 
86  void writePerformanceInStream(std::ostream& myOstream, bool addCommaAtEnd);
87  virtual void setObjectiveLevel(ocra::GenericObjective& obj, int level){};
88 
89  virtual std::string toString();
91  virtual OneLevelSolver::Ptr clone() const = 0;
92  const std::vector<ObjectiveType*>& getObjectives(){return _objectives;}
93  const Eigen::MatrixXd& getQuadraticMatrix(){return _C;}
94  const Eigen::VectorXd& getQuadraticVector(){return _d;}
95 
96 protected:
97 
98  virtual void doPrepare();
99  virtual void doConclude();
100  virtual void doSolve() = 0;
101 
102  // function in doPrepare
103  virtual void prepareMatrices();
104  virtual void updateObjectiveEquations() = 0;
105  virtual void updateConstraintEquations() = 0;
106 
107  //function in doSolve
108  void reduceConstraints(const Eigen::MatrixXd& A, const Eigen::VectorXd& b, Eigen::MatrixXd& Ar, Eigen::VectorXd& br, double tolerance=1e-6);
109 
110 
111  std::vector<ObjectiveType*> _objectives;
112 // std::vector<ocra::QuadraticObjective*> _objectives;
113 
114  std::vector<ocra::LinearConstraint*> _equalityConstraints; //< set of constraints
115  Eigen::MatrixXd _A;
116  Eigen::VectorXd _b;
117  // for equality constraint, which will grow over levels, or when they are reduced
118  Eigen::MatrixXd _Atotal;
119  Eigen::VectorXd _btotal;
120  int ne;
121 
122  std::vector<ocra::LinearConstraint*> _inequalityConstraints; //< set of constraints
123  Eigen::MatrixXd _G;
124  Eigen::VectorXd _h;
125  int ni;
126 
127  Eigen::VectorXd Xsolution;
128 
129 
130 
131  Eigen::MatrixXd _C; // Quadratic Matrix of the task (n,n)
132  Eigen::VectorXd _d; // Quadratic vector of the task (n)
133 
134 };
135 
136 
137 
139 
153 {
155 public:
156  virtual OneLevelSolver::Ptr clone() const{ return std::make_shared<OneLevelSolverWithQuadProg>();}
158  virtual ~OneLevelSolverWithQuadProg();
159 
160 protected:
161 
162  // function in doPrepare
163  virtual void doSolve();
164  virtual void updateObjectiveEquations();
165  virtual void updateConstraintEquations();
166  std::mutex mutex;
167 };
168 
169 //#ifdef USE_QPOASES
171 
185 {
187 public:
188  virtual OneLevelSolver::Ptr clone() const{ return std::make_shared<OneLevelSolverWithQPOASES>();}
189 public:
192 
193 protected:
194  std::unique_ptr<qpOASES::SQProblem> sqp_prob;
195  qpOASES::Options sqp_options;
196  //struct qp{
197  std::vector<qpOASES::real_t> H;
198  qpOASES::real_t* g;
199  qpOASES::real_t* lb;
200  qpOASES::real_t* ub;
201  qpOASES::real_t* A;
202  qpOASES::real_t* lbA;
203  qpOASES::real_t* ubA;
204  //};
205 
206  Eigen::VectorXd _xl;
207  Eigen::VectorXd _xu;
208 
210  Eigen::MatrixXd _RegTerm;
211  Eigen::VectorXd _lbAandG,_ubAandG;
212  Eigen::VectorXd _lbA,_lbG,_ubA,_ubG;
213 
214  int _nWSR_every_run,nWSR;
215  // function in doPrepare
216  virtual void doSolve();
217  virtual void updateObjectiveEquations();
218  virtual void updateConstraintEquations();
219  static ocra::eReturnInfo toOcraRetValue(const qpOASES::returnValue& ret);
220 
221 };
222 //#endif
223 
225 
246 {
248 public:
249  virtual OneLevelSolver::Ptr clone() const{ return std::make_shared<OneLevelSolverWithQLD>();}
250 public:
252  virtual ~OneLevelSolverWithQLD();
253 
254 
255 protected:
256 
257  // function in doPrepare
258  virtual void doSolve();
259  virtual void updateObjectiveEquations();
260  virtual void updateConstraintEquations();
261 
262 
263 protected:
264  Eigen::VectorXd _xl;
265  Eigen::VectorXd _xu;
266 
267  Eigen::MatrixXd AandG;
268  Eigen::VectorXd bandh;
269 
271 
272 
275 
278 
282 
283 };
284 
285  // end group solver
287 
288 }// namespace ocra
289 
290 #endif
void removeObjective(ObjectiveType &obj)
Eigen::MatrixXd _A
Eigen::MatrixXd _C
virtual void doPrepare()
Objective class.
Definition: Objective.h:44
void addObjective(ObjectiveType &obj)
Eigen::Map< Eigen::VectorXd > VectorMap
Eigen::VectorXd _h
Eigen::VectorXd Xsolution
void internalAddObjective(const GenericObjective &objective)
Definition: Solver.cpp:77
const Eigen::VectorXd & getQuadraticVector()
Declaration file of the SquaredLinearFunction class.
ObjQLD class.
Definition: ObjQLD.h:52
Declaration file of the Model class.
virtual void prepareMatrices()
virtual OneLevelSolver::Ptr clone() const =0
virtual OneLevelSolver::Ptr clone() const
const Eigen::MatrixXd & getQuadraticMatrix()
virtual void doSolve()=0
Solver class that only consider one level of importance for all tasks using Quadprog++.
std::vector< ocra::LinearConstraint * > _inequalityConstraints
Eigen::Map< Eigen::MatrixXd > MatrixMap
#define DEFINE_CLASS_POINTER_TYPEDEFS(Class)
Definition: Macros.h:8
void internalRemoveObjective(const GenericObjective &objective)
Definition: Solver.cpp:106
void writePerformanceInStream(std::ostream &myOstream, bool addCommaAtEnd)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
virtual OneLevelSolver::Ptr clone() const
virtual void updateObjectiveEquations()=0
Constraint class.
Definition: Constraint.h:100
virtual std::string toString()
virtual void doConclude()
void reduceConstraints(const Eigen::MatrixXd &A, const Eigen::VectorXd &b, Eigen::MatrixXd &Ar, Eigen::VectorXd &br, double tolerance=1e-6)
void removeConstraint(ocra::LinearConstraint &constraint)
void addConstraint(ocra::LinearConstraint &constraint)
const std::vector< ObjectiveType * > & getObjectives()
Declaration file of the Solver class.
virtual void updateConstraintEquations()=0
Declaration file of the ObjQLD class. This class should be merged with ocra::ObjQLD. It has been created to have a cml-free solver.
A generic abstract class the solvers that can be used in the wOcra Controller.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor > MatrixXdRm
virtual void printValuesAtSolution()
Eigen::VectorXd _btotal
Eigen::MatrixXd _G
Solver class that only consider one level of importance for all tasks using QLD.
std::vector< qpOASES::real_t > H
Eigen::VectorXd _b
Eigen::VectorXd _d
virtual void setObjectiveLevel(ocra::GenericObjective &obj, int level)
Eigen::MatrixXd _Atotal
Declaration file of the Objective class.
std::vector< ObjectiveType * > _objectives
Solver class.
Definition: Solver.h:70
Solver class that only consider one level of importance for all tasks using QPOASES.
virtual OneLevelSolver::Ptr clone() const
Declaration file of the QuadraticFunction class.
Some enumerations for the control.
std::vector< ocra::LinearConstraint * > _equalityConstraints