43 m_hessian_regularization(other.m_hessian_regularization) {
57 std::cout <<
"[SolverOSQP." <<
m_name <<
"] " <<
s << std::endl;
61 const bool resizeVar =
n !=
m_n;
62 const bool resizeEq = (resizeVar ||
neq !=
m_neq);
63 const bool resizeIn = (resizeVar ||
nin !=
m_nin);
65 if (resizeIn || resizeEq) {
83 if (resizeVar || resizeEq || resizeIn) {
86 m_solver.data()->clearHessianMatrix();
87 m_solver.data()->clearLinearConstraintsMatrix();
108 const bool hessianRegularization) {
109 if (problemData.size() > 2) {
111 false,
"Solver not implemented for more than 2 hierarchical levels.");
115 unsigned int neq = 0,
nin = 0;
118 if (cl0.size() > 0) {
119 const unsigned int n = cl0[0].second->cols();
120 for (ConstraintLevel::const_iterator
it = cl0.begin();
it != cl0.end();
122 auto constr =
it->second;
123 assert(
n == constr->cols());
124 if (constr->isEquality())
125 neq += constr->rows();
127 nin += constr->rows();
132 unsigned int i_in = 0;
133 for (ConstraintLevel::const_iterator
it = cl0.begin();
it != cl0.end();
135 auto constr =
it->second;
136 if (constr->isEquality()) {
137 m_qpData.
CI.middleRows(i_in, constr->rows()) = constr->matrix();
138 m_qpData.
ci_lb.segment(i_in, constr->rows()) = constr->vector();
139 m_qpData.
ci_ub.segment(i_in, constr->rows()) = constr->vector();
140 i_in += constr->rows();
141 }
else if (constr->isInequality()) {
142 m_qpData.
CI.middleRows(i_in, constr->rows()) = constr->matrix();
143 m_qpData.
ci_lb.segment(i_in, constr->rows()) = constr->lowerBound();
144 m_qpData.
ci_ub.segment(i_in, constr->rows()) = constr->upperBound();
145 i_in += constr->rows();
146 }
else if (constr->isBound()) {
147 m_qpData.
CI.middleRows(i_in, constr->rows()).setIdentity();
148 m_qpData.
ci_lb.segment(i_in, constr->rows()) = constr->lowerBound();
149 m_qpData.
ci_ub.segment(i_in, constr->rows()) = constr->upperBound();
150 i_in += constr->rows();
160 if (problemData.size() > 1) {
165 for (ConstraintLevel::const_iterator
it = cl1.begin();
it != cl1.end();
167 const double&
w =
it->first;
168 auto constr =
it->second;
169 if (!constr->isEquality())
171 false,
"Inequalities in the cost function are not implemented yet");
175 w * constr->matrix().transpose() * constr->matrix();
179 w * constr->matrix().transpose() * constr->vector();
182 if (hessianRegularization) {
190 typedef Eigen::SparseMatrix<double> SpMat;
203 static_cast<SpMat
>(
m_qpData.
H.sparseView()));
205 m_solver.data()->setLinearConstraintsMatrix(
206 static_cast<SpMat
>(
m_qpData.
CI.sparseView()));
212 m_solver.updateLinearConstraintsMatrix(
213 static_cast<SpMat
>(
m_qpData.
CI.sparseView()));
223 OsqpEigen::Status status =
m_solver.getStatus();
225 if (status == OsqpEigen::Status::Solved) {
235 if (cl0.size() > 0) {
236 for (ConstraintLevel::const_iterator
it = cl0.begin();
it != cl0.end();
238 auto constr =
it->second;
239 if (constr->checkConstraint(
x) ==
false) {
241 if (constr->isEquality()) {
242 sendMsg(
"Equality " + constr->name() +
" violated: " +
243 toString((constr->matrix() *
x - constr->vector()).norm()));
244 }
else if (constr->isInequality()) {
246 "Inequality " + constr->name() +
" violated: " +
248 (constr->matrix() *
x - constr->lowerBound()).minCoeff()) +
251 (constr->upperBound() - constr->matrix() *
x).minCoeff()));
252 }
else if (constr->isBound()) {
253 sendMsg(
"Bound " + constr->name() +
" violated: " +
254 toString((
x - constr->lowerBound()).minCoeff()) +
"\n" +
255 toString((constr->upperBound() -
x).minCoeff()));
261 }
else if (status == OsqpEigen::Status::PrimalInfeasible)
263 else if (status == OsqpEigen::Status::MaxIterReached)
265 else if (status == OsqpEigen::Status::DualInfeasible)
267 else if (status == OsqpEigen::Status::SolvedInaccurate)
279 m_solver.settings()->setMaxIteration(
int(maxIter));