ocra-recipes
Doxygen documentation for the ocra-recipes repository
SolverUtilities.h
Go to the documentation of this file.
1 #ifndef _OCRA_SOLVER_UTILITIES_H_
2 #define _OCRA_SOLVER_UTILITIES_H_
3 
4 #ifdef WIN32
5 # pragma once
6 #endif
7 
8 #include <ocra/util/MathTypes.h>
9 #include "ocra/optim/Variable.h"
10 #include "ocra/optim/Constraint.h"
11 #include "ocra/optim/uncompress.h"
12 
13 #include <Eigen/Eigen>
14 namespace ocra{using namespace Eigen;}
15 
16 #include <iostream>
17 #include <iomanip>
18 
19 namespace ocra
20 {
22  {
23  RETURN_SUCCESS = 0, //< lucky you
24  RETURN_INCONSISTENT_PROBLEM, //< the problem does not meet the solver requirements
25  RETURN_INFEASIBLE_PROBLEM, //< the problem was decided to be infeasible by the solver
26  RETURN_MAX_ITER_REACHED, //< the maximum number of iterations was reached before a solution was found
27  RETURN_MAX_TIME_REACHED, //< the maximum time allowed was reached before a solution was found
28  RETURN_MEMORY_ERROR, //< the solver was not able to allocate all the memory needed, or the provided memory was not enough
29  RETURN_NUMERICAL_ERROR, //< some numerical errors occured which prevented the solution to be found
30  RETURN_ERROR, //< other/undetermined
31  };
32 
33 
35  {
36  eReturnInfo info; //< error code
37  VectorXd solution; //< the solution if inform is 0. If not, it depends on the solver
38  };
39 
40 
43  {
44  CSTR_PLUS_EQUAL=0, // Cx+d=0
45  CSTR_MINUS_EQUAL, // Cx-d=0
46  CSTR_PLUS_LOWER, // Cx+d<0
47  CSTR_MINUS_LOWER, // Cx-d<0
48  CSTR_PLUS_GREATER, // Cx+d>0
49  CSTR_MINUS_GREATER, // Cx-d>0
50  CSTR_DOUBLE_BOUNDS // d1<Cx<d2
51  };
52 
53 
54  namespace utils
55  {
56  namespace details
57  {
62  {
63  PLUS_A_PLUS_B=0, //C=A, d=b
64  PLUS_A_MINUS_B, //C=A, d=-b
65  PLUS_A_PLUS_BV, //C=A, d=b-v
66  PLUS_A_MINUS_BV, //C=A, d=v-b
67  MINUS_A_PLUS_B, //C=-A, d=b
68  MINUS_A_MINUS_B, //C=-A, d=-b
69  MINUS_A_PLUS_BV, //C=-A, d=b-v
70  MINUS_A_MINUS_BV, //C=-A, d=v-b
73  };
74  }
75 
80  extern const details::eConvertCase conversion_cases[7][7];
81 
83  template<class Derived, class VectorBase>
84  inline void printLinearEquation(const MatrixBase<Derived>& A, const VectorBase& b, int space = 9, int precision=3);
85 
87  void printSolution(const VectorXd& result, const Variable& var, int space = 9, int precision=3);
88 
136 
137  // const_cast Eigen trick : http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html#TopicPlainFunctionsFailing
138 #define DECLARE_UNCOMPRESSED_FUNCTION(functionName) \
139  template<class Derived1, class Derived2> \
140  void functionName(const MatrixBase<Derived1>& in, MatrixBase<Derived2> const& _out, \
141  const std::vector<int>& mapping, double scale=1., bool reverseMapping=false); \
142  template<class Derived1, class Derived2> \
143  void functionName(const Variable& base, const Variable& rel, \
144  const MatrixBase<Derived1>& in, MatrixBase<Derived2> const& _out, \
145  std::vector<int>& mapping, double scale=1.); \
146  template<typename Scalar, class Derived1, class Derived2> \
147  /* specialization for when in is of the form a*M */ \
148  inline void functionName(const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, Derived1>& in, MatrixBase<Derived2> const& _out, \
149  const std::vector<int>& mapping, double scale=1., bool reverseMapping=false) \
150  { functionName(in.nestedExpression(), _out, mapping, scale*in.functor().m_other, reverseMapping); } \
151  template<typename Scalar, class Derived1, class Derived2> \
152  void functionName(const Variable& base, const Variable& rel, \
153  const CwiseUnaryOp<internal::scalar_multiple_op<Scalar>, Derived1>& in, MatrixBase<Derived2> const& _out, \
154  std::vector<int>& mapping, double scale=1.) \
155  { \
156  MatrixBase<Derived2>& out = const_cast<MatrixBase<Derived2> & >(_out);\
157  functionName(base, rel, in.nestedExpression(), out, mapping, scale*in.functor().m_other); \
158  }
159 
173 
174 
175  /*Uncompress and convert a linear constraint*/
176  template<class Derived, class VectorBase1, class VectorBase2>
177  void convert(const LinearConstraint& cstr, const std::vector<int>& mapping, eConstraintOutput type,
178  MatrixBase<Derived>& A, VectorBase1& b, VectorBase2& l, double infinity = 0.);
179 
184  template<class VectorBase1, class VectorBase2>
185  void intersectBounds(const DiagonalLinearConstraint& bounds, const std::vector<int>& mapping, VectorBase1& bl, VectorBase2& bu);
186  }
187 
188  void testUtilities();
189 }
190 
191 
192 #include "SolverUtilities.hxx"
193 
194 #endif // _OCRA_SOLVER_UTILITIES_H_
195 
196 // cmake:sourcegroup=Solvers
void minCompressedByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
void addCompressedByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
const details::eConvertCase conversion_cases[7][7]
void addCompressed2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
void minCompressedByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
Constraint< LinearFunction > LinearConstraint
Definition: Constraint.h:673
void uncompressByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
Optimization-based Robot Controller namespace. a library of classes to write and solve optimization p...
void uncompressByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
eConstraintOutput
void convert(const LinearConstraint &cstr, const std::vector< int > &mapping, eConstraintOutput type, MatrixBase< Derived > &A, VectorBase1 &b, VectorBase2 &l, double infinity=0.)
void uncompress2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
This class represents a variable in a mathematical sense.
Definition: Variable.h:105
void addCompressedByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
#define DECLARE_UNCOMPRESSED_FUNCTION(functionName)
void maxCompressed2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
void testUtilities()
Declaration file of the Constraint class.
void intersectBounds(const DiagonalLinearConstraint &bounds, const std::vector< int > &mapping, VectorBase1 &bl, VectorBase2 &bu)
void maxCompressedByRow(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
Constraint< DiagonalLinearFunction > DiagonalLinearConstraint
Definition: Constraint.h:676
void maxCompressedByCol(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)
Declaration file of the Variable class.
Declaration file of the OptimizationVariable class.
void printLinearEquation(const MatrixBase< Derived > &A, const VectorBase &b, int space=9, int precision=3)
void printSolution(const VectorXd &result, const Variable &var, int space=9, int precision=3)
void minCompressed2d(const MatrixBase< Derived1 > &in, MatrixBase< Derived2 > const &_out, const std::vector< int > &mapping, double scale, bool reverseMapping)