tsid 1.9.0
Efficient Task Space Inverse Dynamics for Multi-body Systems based on Pinocchio
Loading...
Searching...
No Matches
solver-HQP-eiquadprog-rt.hxx
Go to the documentation of this file.
1//
2// Copyright (c) 2017 CNRS
3//
4
5#ifndef __invdyn_solvers_hqp_eiquadprog_rt_hxx__
6#define __invdyn_solvers_hqp_eiquadprog_rt_hxx__
7
11#include "tsid/math/utils.hpp"
12
13namespace eiquadprog::solvers;
14
15#define PROFILE_EIQUADPROG_PREPARATION "EiquadprogRT problem preparation"
16#define PROFILE_EIQUADPROG_SOLUTION "EiquadprogRT problem solution"
17
18namespace tsid {
19namespace solvers {
20
21template <int nVars, int nEqCon, int nIneqCon>
23 const std::string& name)
26 m_n = nVars;
27 m_neq = nEqCon;
28 m_nin = nIneqCon;
29 m_output.resize(nVars, nEqCon, 2 * nIneqCon);
30}
31
32template <int nVars, int nEqCon, int nIneqCon>
34 std::cout << "[SolverHQuadProgRT." << m_name << "] " << s << std::endl;
35}
36
37template <int nVars, int nEqCon, int nIneqCon>
39 unsigned int neq,
40 unsigned int nin) {
41 assert(n == nVars);
42 assert(neq == nEqCon);
43 assert(nin == nIneqCon);
44 if ((n != nVars) || (neq != nEqCon) || (nin != nIneqCon))
45 std::cerr
46 << "[SolverHQuadProgRT] (n!=nVars) || (neq!=nEqCon) || (nin!=nIneqCon)"
47 << std::endl;
48}
49
50template <int nVars, int nEqCon, int nIneqCon>
52 const HQPData& problemData) {
53 using namespace tsid::math;
54
55 // #ifndef EIGEN_RUNTIME_NO_MALLOC
56 // Eigen::internal::set_is_malloc_allowed(false);
57 // #endif
58
60
61 if (problemData.size() > 2) {
62 PINOCCHIO_CHECK_INPUT_ARGUMENT(
63 false, "Solver not implemented for more than 2 hierarchical levels.");
64 }
65
66 // Compute the constraint matrix sizes
67 unsigned int neq = 0, nin = 0;
68 const ConstraintLevel& cl0 = problemData[0];
69 if (cl0.size() > 0) {
70 const unsigned int n = cl0[0].second->cols();
71 for (ConstraintLevel::const_iterator it = cl0.begin(); it != cl0.end();
72 it++) {
73 auto constr = it->second;
74 assert(n == constr->cols());
75 if (constr->isEquality())
76 neq += constr->rows();
77 else
78 nin += constr->rows();
79 }
80 // If necessary, resize the constraint matrices
81 resize(n, neq, nin);
82
83 int i_eq = 0, i_in = 0;
84 for (ConstraintLevel::const_iterator it = cl0.begin(); it != cl0.end();
85 it++) {
86 auto constr = it->second;
87 if (constr->isEquality()) {
88 m_CE.middleRows(i_eq, constr->rows()) = constr->matrix();
89 m_ce0.segment(i_eq, constr->rows()) = -constr->vector();
90 i_eq += constr->rows();
91 } else if (constr->isInequality()) {
92 m_CI.middleRows(i_in, constr->rows()) = constr->matrix();
93 m_ci0.segment(i_in, constr->rows()) = -constr->lowerBound();
94 i_in += constr->rows();
95 m_CI.middleRows(i_in, constr->rows()) = -constr->matrix();
96 m_ci0.segment(i_in, constr->rows()) = constr->upperBound();
97 i_in += constr->rows();
98 } else if (constr->isBound()) {
99 m_CI.middleRows(i_in, constr->rows()).setIdentity();
100 m_ci0.segment(i_in, constr->rows()) = -constr->lowerBound();
101 i_in += constr->rows();
102 m_CI.middleRows(i_in, constr->rows()) = -Matrix::Identity(m_n, m_n);
103 m_ci0.segment(i_in, constr->rows()) = constr->upperBound();
104 i_in += constr->rows();
105 }
106 }
107 } else
108 resize(m_n, neq, nin);
109
110 if (problemData.size() > 1) {
111 const ConstraintLevel& cl1 = problemData[1];
112 m_H.setZero();
113 m_g.setZero();
114 for (ConstraintLevel::const_iterator it = cl1.begin(); it != cl1.end();
115 it++) {
116 const double& w = it->first;
117 auto constr = it->second;
118 if (!constr->isEquality())
119 PINOCCHIO_CHECK_INPUT_ARGUMENT(
120 false, "Inequalities in the cost function are not implemented yet");
121
123 m_H.noalias() += w * constr->matrix().transpose() * constr->matrix();
125 m_g.noalias() -= w * (constr->matrix().transpose() * constr->vector());
126 }
127 m_H.diagonal().noalias() += m_hessian_regularization * Vector::Ones(m_n);
128 }
129
131
132 // // eliminate equality constraints
133 // if(m_neq>0)
134 // {
135 // m_CE_lu.compute(m_CE);
136 // sendMsg("The rank of CD is "+toString(m_CE_lu.rank());
137 // const MatrixXd & Z = m_CE_lu.kernel();
138
139 // }
140
142
143 // min 0.5 * x G x + g0 x
144 // s.t.
145 // CE x + ce0 = 0
146 // CI x + ci0 >= 0
147 typename RtVectorX<nVars>::d sol(m_n);
150 m_solver.solve_quadprog(m_H, m_g, m_CE, m_ce0, m_CI, m_ci0, sol);
152
153 m_output.x = sol;
154
155 // #ifndef EIGEN_RUNTIME_NO_MALLOC
156 // Eigen::internal::set_is_malloc_allowed(true);
157 // #endif
158
159 if (status == eisol::RT_EIQUADPROG_OPTIMAL) {
161 m_output.lambda = m_solver.getLagrangeMultipliers();
162 // m_output.activeSet = m_solver.getActiveSet().template tail< 2*nIneqCon
163 // >().head(m_solver.getActiveSetSize());
164 m_output.activeSet = m_solver.getActiveSet().segment(
165 m_neq, m_solver.getActiveSetSize() - m_neq);
166 m_output.iterations = m_solver.getIteratios();
167
168#ifndef NDEBUG
169 const Vector& x = m_output.x;
170
171 if (cl0.size() > 0) {
172 for (ConstraintLevel::const_iterator it = cl0.begin(); it != cl0.end();
173 it++) {
174 auto constr = it->second;
175 if (constr->checkConstraint(x) == false) {
176 if (constr->isEquality()) {
177 sendMsg("Equality " + constr->name() + " violated: " +
178 toString((constr->matrix() * x - constr->vector()).norm()));
179 } else if (constr->isInequality()) {
180 sendMsg(
181 "Inequality " + constr->name() + " violated: " +
183 (constr->matrix() * x - constr->lowerBound()).minCoeff()) +
184 "\n" +
186 (constr->upperBound() - constr->matrix() * x).minCoeff()));
187 } else if (constr->isBound()) {
188 sendMsg("Bound " + constr->name() + " violated: " +
189 toString((x - constr->lowerBound()).minCoeff()) + "\n" +
190 toString((constr->upperBound() - x).minCoeff()));
191 }
192 }
193 }
194 }
195#endif
196 } else if (status == eisol::RT_EIQUADPROG_UNBOUNDED)
201 m_output.status = HQP_STATUS_ERROR;
202
203 return m_output;
204}
205
206template <int nVars, int nEqCon, int nIneqCon>
210
211template <int nVars, int nEqCon, int nIneqCon>
213 unsigned int maxIter) {
215 return m_solver.setMaxIter(maxIter);
216}
217
218} // namespace solvers
219} // namespace tsid
220
221#endif // ifndef __invdyn_solvers_hqp_eiquadprog_rt_hxx__
Definition solver-HQP-output.hpp:29
SolverHQPBase(const std::string &name)
Definition solver-HQP-base.cpp:16
std::string m_name
Definition solver-HQP-base.hpp:80
virtual bool setMaximumIterations(unsigned int maxIter)
Definition solver-HQP-base.cpp:23
HQPOutput m_output
Definition solver-HQP-base.hpp:84
virtual const std::string & name() const
Definition solver-HQP-base.hpp:47
double getObjectiveValue() override
Definition solver-HQP-eiquadprog-rt.hxx:207
eiquadprog::solvers::RtEiquadprog< nVars, nEqCon, 2 *nIneqCon > m_solver
Definition solver-HQP-eiquadprog-rt.hpp:68
RtVectorX< 2 *nIneqCon >::d m_ci0
twice the rows because inequality constraints are bilateral
Definition solver-HQP-eiquadprog-rt.hpp:76
int m_n
number of inequality constraints
Definition solver-HQP-eiquadprog-rt.hpp:90
RtMatrixX< nVars, nVars >::d m_H
Definition solver-HQP-eiquadprog-rt.hpp:70
RtVectorX< nVars >::d m_g
Definition solver-HQP-eiquadprog-rt.hpp:71
const HQPOutput & solve(const HQPData &problemData) override
Definition solver-HQP-eiquadprog-rt.hxx:51
RtMatrixX< 2 *nIneqCon, nVars >::d m_CI
Definition solver-HQP-eiquadprog-rt.hpp:75
void sendMsg(const std::string &s)
Definition solver-HQP-eiquadprog-rt.hxx:33
int m_nin
number of equality constraints
Definition solver-HQP-eiquadprog-rt.hpp:89
RtVectorX< nEqCon >::d m_ce0
Definition solver-HQP-eiquadprog-rt.hpp:73
void resize(unsigned int n, unsigned int neq, unsigned int nin) override
Definition solver-HQP-eiquadprog-rt.hxx:38
bool setMaximumIterations(unsigned int maxIter) override
Definition solver-HQP-eiquadprog-rt.hxx:212
SolverHQuadProgRT(const std::string &name)
Definition solver-HQP-eiquadprog-rt.hxx:22
RtMatrixX< nEqCon, nVars >::d m_CE
Definition solver-HQP-eiquadprog-rt.hpp:72
int m_neq
Definition solver-HQP-eiquadprog-rt.hpp:88
double m_hessian_regularization
Definition solver-HQP-eiquadprog-rt.hpp:79
math::Vector Vector
Definition solver-HQP-eiquadprog-rt.hpp:38
#define START_PROFILER_EIQUADPROG_RT(x)
#define STOP_PROFILER_EIQUADPROG_RT(x)
#define EIGEN_MALLOC_ALLOWED
Definition fwd.hpp:14
#define EIGEN_MALLOC_NOT_ALLOWED
Definition fwd.hpp:15
Definition constraint-base.hpp:13
pinocchio::container::aligned_vector< ConstraintLevel > HQPData
Definition fwd.hpp:99
pinocchio::container::aligned_vector< aligned_pair< double, std::shared_ptr< math::ConstraintBase > > > ConstraintLevel
Definition fwd.hpp:95
Definition constraint-bound.hpp:25
std::string toString(const T &v)
Definition utils.hpp:26
#define PROFILE_EIQUADPROG_SOLUTION
Definition solver-HQP-eiquadprog-rt.hxx:16
#define PROFILE_EIQUADPROG_PREPARATION
Definition solver-HQP-eiquadprog-rt.hxx:15
#define DEFAULT_HESSIAN_REGULARIZATION
Definition fwd.hpp:28
HQP_STATUS_MAX_ITER_REACHED
Definition fwd.hpp:66
HQP_STATUS_OPTIMAL
Definition fwd.hpp:63
HQP_STATUS_INFEASIBLE
Definition fwd.hpp:64
Eigen::Matrix< double, Rows, 1 > d