ocra-recipes
Doxygen documentation for the ocra-recipes repository
FSQPSolver.h
Go to the documentation of this file.
1 
11 #ifndef _OCRABASE_FSQP_SOLVER_H_
12 #define _OCRABASE_FSQP_SOLVER_H_
13 
14 // includes
15 #include "Solver.h"
16 #include "OFSQP.h"
17 #include "FSQPConstraintManager.h"
18 #include "ocra/optim/Buffer.h"
19 
20 
21 namespace ocra
22 {
45  class FSQPSolver : public Solver, public OFSQPProblem
46  {
47  // ------------------------ structures --------------------------------------
48  public:
49  typedef Map<VectorXd> VectorMap;
50  typedef Map<MatrixXd> MatrixMap;
51 
52 
54  {
55  NORMAL_PB=0, //A=0: min sum f_i
56  INFINITE_PB //A=1: min |sum f_i|
57  };
58 
59  enum eFsqpAlgo
60  {
61  AL=0, //B=0: algorithm FSQP-AL, decrease of the (modified) objectif function at each step
62  NL //B=1: algorithm FSQP-NL, decrease of the (modified) objectif function after at most 4 steps
63  };
64 
66  {
67  LOOSE=1, //C=1: objectives and constraints can be evaluated outside the feasible space
68  STRICT //C=2: objectives and constraints can not be evaluated outside the feasible space
69  };
70 
72  {
77  };
78 
79  protected:
80  private:
81 
82  // ------------------------ public static members ---------------------------
83  public:
84 
85  // ------------------------ constructors ------------------------------------
86  private:
87  FSQPSolver(const FSQPSolver&);
88  FSQPSolver& operator=(const FSQPSolver&);
89  protected:
90  public:
91  FSQPSolver();
92 
93  // ------------------------ public interface --------------------------------
94  public:
104 
109  void addConstraint(LinearConstraint& constraint);
110  void addConstraint(GenericConstraint& constraint);
111  void removeConstraint(LinearConstraint& constraint);
112  void removeConstraint(GenericConstraint& constraint);
114 
119  void addBounds(BoundConstraint& constraint);
120  void addBounds(IdentityConstraint& constraint);
121  void removeBounds(BoundConstraint& constraint);
122  void removeBounds(IdentityConstraint& constraint);
124 
127  void clearObjectives();
128  void clearConstraints();
135  void clearBounds();
137 
139  void set_x0(const VectorXd& x0, const Variable& ref);
140 
143  void setA(eFsqpProblemType type);
144  void setB(eFsqpAlgo type);
146  void setMode(int m);
148  void setPrintStep(int N);
149  void setPrintOption(int option);
150  void setMaxIter(int n);
151  void setInfinity(double infinity);
152  void setEps(double eps);
153  void setEqnViol(double eps);
154  void setUDelta(double udelta);
156 
157  eFsqpProblemType getA() const;
158  eFsqpAlgo getB() const;
160  int getMode() const;
162  int getPrintStep() const;
163  int getPrintOption() const;
164  int getMaxIter() const;
165  double getInfinity() const;
166  double setEps() const;
167  double setEqnViol() const;
168  double setUDelta() const;
169 
170 
171  void printValuesAtSolution();
172 
174  std::string toString();
175 
176  // ------------------------ public methods ----------------------------------
177  public:
178  void obj(int nparam, int j, double* x, double* fj, void* cd);
179  void constr(int nparam, int j, double* x, double* gj, void* cd);
180  void gradob(int nparam, int j, double* x, double* gradfj, void* cd);
181  void gradcn(int nparam, int j, double* x, double* gradgj, void* cd);
182 
183  // ------------------------ public static methods ---------------------------
184  public:
185 
186  // ------------------------ protected methods -------------------------------
187  protected:
188  void doPrepare();
189  void doSolve();
190  void doConclude();
191 
195  virtual void onConstraintResize(int timestamp);
196 
198  virtual void onObjectiveResize(int timestamp);
199 
200  // ------------------------ protected static methods ------------------------
201  protected:
202 
203  // ------------------------ private methods ---------------------------------
204  private:
205  void resize();
206  void updateBounds();
207  eReturnInfo translateReturnInfo() const;
208 
209  void addBounds_(DiagonalLinearConstraint& constraint);
210  void removeBounds_(DiagonalLinearConstraint& constraint);
211  void checkNewX(int nparam, double* x);
212 
213  // ------------------------ private static methods --------------------------
214  private:
215 
216  // ------------------------ protected members -------------------------------
217  protected:
218 
219  // ------------------------ protected static members ------------------------
220  protected:
221 
222  // ------------------------ private members ---------------------------------
223  private:
224  OFSQP solver;
225 
226  int nparam;
227  int nf;
228  int nfsr;
229  int nineqn;
230  int nineq;
231  int neqn;
232  int neq;
233  int ncsrl;
234  int ncsrn;
235  int mode;
236  int iprint;
237  int miter;
238  int inform;
239  double bigbnd;
240  double eps;
241  double epseqn;
242  double udelta;
243 
244  VectorXd x0;
245  VectorMap bl;
246  VectorMap bu;
247  VectorMap f;
248  VectorMap g;
249  VectorMap lambda;
250 
251  Map<VectorXi> mesh_pts;
252 
253  Buffer<double> _buffer;
254 
255  bool _allObjectivesProvideAGradient;
256  std::vector<GenericObjective*> _objectives;
257  std::vector<DiagonalLinearConstraint*> _bounds;
258  FSQPConstraintManager _constraints;
259 
260  // ------------------------ private static members --------------------------
261  private:
262 
263  // ------------------------ friendship declarations -------------------------
264  };
265 
266  void testFSQPSolver01();
267  void testFSQPSolver02();
268 }
269 
270 #endif //_OCRABASE_FSQP_SOLVER_H_
void clearConstraints()
Definition: FSQPSolver.cpp:125
void removeBounds(BoundConstraint &constraint)
Definition: FSQPSolver.cpp:103
eFsqpProblemType getA() const
Definition: FSQPSolver.cpp:261
void addBounds(BoundConstraint &constraint)
Definition: FSQPSolver.cpp:86
void setInfinity(double infinity)
Definition: FSQPSolver.cpp:237
void constr(int nparam, int j, double *x, double *gj, void *cd)
Definition: FSQPSolver.cpp:341
void clearLinearInequalityConstraints()
Definition: FSQPSolver.cpp:158
double setEqnViol() const
Definition: FSQPSolver.cpp:311
double setEps() const
Definition: FSQPSolver.cpp:306
void setC(eFsqpEvaluationDomainPolicy type)
Definition: FSQPSolver.cpp:197
FSQPSolver class.
Definition: FSQPSolver.h:45
void setMode(int m)
Definition: FSQPSolver.cpp:203
eFsqpAlgo getB() const
Definition: FSQPSolver.cpp:266
int getMaxIter() const
Definition: FSQPSolver.cpp:296
FSQPConstraintManager class.
void clearInequalityConstraints()
Definition: FSQPSolver.cpp:147
void setA(eFsqpProblemType type)
Definition: FSQPSolver.cpp:186
void addConstraint(LinearConstraint &constraint)
Definition: FSQPSolver.cpp:61
void setPrintOption(int option)
Definition: FSQPSolver.cpp:224
void gradob(int nparam, int j, double *x, double *gradfj, void *cd)
Definition: FSQPSolver.cpp:347
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
Function class.
Definition: Function.h:77
int getMode() const
Definition: FSQPSolver.cpp:276
Map< VectorXd > VectorMap
Definition: FSQPSolver.h:49
void clearLinearEqualityConstraints()
Definition: FSQPSolver.cpp:142
Constraint class.
Definition: Constraint.h:100
void removeConstraint(LinearConstraint &constraint)
Definition: FSQPSolver.cpp:73
double getInfinity() const
Definition: FSQPSolver.cpp:301
void printValuesAtSolution()
Definition: FSQPSolver.cpp:321
void clearObjectives()
Definition: FSQPSolver.cpp:120
void obj(int nparam, int j, double *x, double *fj, void *cd)
Definition: FSQPSolver.cpp:332
eFsqpEvaluationDomainPolicy getC() const
Definition: FSQPSolver.cpp:271
void addObjective(GenericObjective &obj)
Definition: FSQPSolver.cpp:29
void testFSQPSolver01()
Definition: FSQPSolver.cpp:493
int getPrintOption() const
Definition: FSQPSolver.cpp:291
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
Declaration file of the Solver class.
void gradcn(int nparam, int j, double *x, double *gradgj, void *cd)
Definition: FSQPSolver.cpp:362
int getPrintStep() const
Definition: FSQPSolver.cpp:286
void setMaxIter(int n)
Definition: FSQPSolver.cpp:231
void setPrintStep(int N)
Definition: FSQPSolver.cpp:217
Map< MatrixXd > MatrixMap
Definition: FSQPSolver.h:50
void clearNonLinearInequalityConstraints()
Definition: FSQPSolver.cpp:153
void clearNonLinearEqualityConstraints()
Definition: FSQPSolver.cpp:137
Declaration file of the FSQPConstraintManager class.
void setPrintMode(eFsqpPrintOption m)
Definition: FSQPSolver.cpp:209
void removeObjective(Function &obj)
Definition: FSQPSolver.cpp:38
double setUDelta() const
Definition: FSQPSolver.cpp:316
eFsqpPrintOption getPrintMode() const
Definition: FSQPSolver.cpp:281
void set_x0(const VectorXd &x0, const Variable &ref)
Definition: FSQPSolver.cpp:169
int n()
Definition: Solver.h:146
virtual void onObjectiveResize(int timestamp)
Definition: FSQPSolver.cpp:427
void setB(eFsqpAlgo type)
Definition: FSQPSolver.cpp:191
void testFSQPSolver02()
Definition: FSQPSolver.cpp:652
std::string toString()
Definition: FSQPSolver.cpp:326
Solver class.
Definition: Solver.h:70
virtual void onConstraintResize(int timestamp)
Definition: FSQPSolver.cpp:422
void clearEqualityConstraints()
Definition: FSQPSolver.cpp:131