18 template <
int max_dim_qp,
int max_num_eq,
int max_num_ineq>
35 ePP_MightZeroIneqs = 1 << 0
62 int numVariables()
const {
return H_.rows(); }
63 int numEqConstr()
const {
return Eq_mat_.rows(); }
64 int numIneqConstr()
const {
return Ineq_mat_.rows(); }
71 bool virtual optimize() =0;
72 bool virtual isOptimized()
const =0;
75 template <
typename Derived,
typename OtherDerived>
76 inline void appendEqualities(
const Eigen::MatrixBase<Derived>& mat,
const Eigen::MatrixBase<OtherDerived>& vec)
78 RtAffineUtils::append(Eq_mat_, eq_vec_, mat, vec);
81 template <
typename Derived>
82 inline void appendEqualities(
const Eigen::MatrixBase<Derived>& mat)
84 RtAffineUtils::append(Eq_mat_, eq_vec_, mat);
88 template <
typename Derived,
typename OtherDerived>
89 inline void appendInequalities(
const Eigen::MatrixBase<Derived>& mat,
const Eigen::MatrixBase<OtherDerived>& vec)
91 RtAffineUtils::append(Ineq_mat_, ineq_vec_, mat, vec);
94 template <
typename Derived>
95 inline void appendInequalities(
const Eigen::MatrixBase<Derived>& mat)
97 RtAffineUtils::append(Ineq_mat_, ineq_vec_, mat);
105 inline virtual void reset()
108 g_ = Eigen::Matrix<double, max_dim_qp, 1>::Zero();
109 sol_ = Eigen::Matrix<double, max_dim_qp, 1>::Zero();
110 H_ = Eigen::Matrix<double, max_dim_qp, max_dim_qp>::Zero();
112 RtVectorUtils::setZero(eq_vec_, 0);
113 RtVectorUtils::setZero(ineq_vec_, 0);
114 RtMatrixUtils::setZero(Eq_mat_, 0, max_dim_qp);
115 RtMatrixUtils::setZero(Ineq_mat_, 0, max_dim_qp);
118 inline virtual void reset(
int dim_qp,
int num_eq,
int num_ineq)
121 RtVectorUtils::setZero(g_, dim_qp);
122 RtVectorUtils::setZero(eq_vec_, 0);
123 RtVectorUtils::setZero(ineq_vec_, 0);
124 RtVectorUtils::setZero(sol_, dim_qp);
125 RtMatrixUtils::setZero(H_, dim_qp, dim_qp);
126 RtMatrixUtils::setZero(Eq_mat_, 0, dim_qp);
127 RtMatrixUtils::setZero(Ineq_mat_, 0, dim_qp);
131 double computeOptimalityCondition()
const 133 typename RtMatrix<max_dim_qp, max_num_eq+max_num_ineq>::d constr_jac_transp;
134 constr_jac_transp.resize(numVariables(), numEqConstr()+numIneqConstr());
135 constr_jac_transp.leftCols(numEqConstr()) = eqConstraintsMat().transpose();
137 double eq_vio_sqr = 0;
138 if (numEqConstr() != 0) { eq_vio_sqr = (eqConstraintsMat()*solution() + eqConstraintsVec()).squaredNorm(); }
140 int to_col=numEqConstr();
141 double ineq_vio_sqr = 0;
142 for (
int r=0; r<numIneqConstr(); r++) {
143 const double ineq_val = ineqConstraintsMat().row(r).dot(solution()) + ineqConstraintsVec()[r];
144 const double vio = std::max(0., ineq_val);
145 ineq_vio_sqr += vio*vio;
146 if (ineq_val > -1e-8) {
147 constr_jac_transp.col(to_col) = ineqConstraintsMat().row(r).transpose();
152 constr_jac_transp.conservativeResize(constr_jac_transp.rows(), to_col);
154 obj_grad.resize(numVariables());
155 obj_grad = objectiveQuadPart()*solution() + objectiveLinPart();
156 double grad_sqr_norm;
157 if (constr_jac_transp.cols() != 0) {
158 typename RtVector<max_num_eq+max_num_ineq>::d lagr;
159 lagr.resize(constr_jac_transp.cols());
160 lagr = constr_jac_transp.jacobiSvd(Eigen::ComputeThinU | Eigen::ComputeThinV).solve(obj_grad);
161 grad_sqr_norm = (constr_jac_transp*lagr-obj_grad).squaredNorm();
163 grad_sqr_norm = obj_grad.squaredNorm();
165 return std::sqrt(grad_sqr_norm + ineq_vio_sqr + eq_vio_sqr);
169 virtual bool checkSolution(
bool print =
true,
double eq_threashold = 0.001,
double ineq_threashold = 0.001)
171 const double eq_norm = (eqConstraintsMat() * solution() + eqConstraintsVec()).norm();
173 for (
int i=0; i<numIneqConstr(); ++i)
174 ineq_dist[i] *= ineq_dist[i] <= 0.0 ? 0.0 : 1.0;
175 const double ineq_norm = (ineq_dist).norm();
176 const double lagr_grad_norm = -1;
177 bool is_sol_valid = eq_norm <= eq_threashold && ineq_norm <= ineq_threashold;
179 if (print || (!is_sol_valid && isOptimized()))
181 std::cout <<
"qp check (num vars, num equalities, num inequalities, objective value): " <<
182 numVariables() <<
" " << numEqConstr() <<
" " << numIneqConstr() <<
" " <<
183 solution().transpose() * objectiveQuadPart() * solution() + 2*objectiveLinPart().transpose()*solution()<< std::endl;
184 std::cout <<
" lagrangian gradient norm: " << lagr_grad_norm << std::endl;
185 std::cout <<
" equality squared dist: " << eq_norm << std::endl;
186 std::cout <<
" inequality squared dist: " << ineq_norm << std::endl;
187 std::cout <<
" isOptimized(): " << ( isOptimized()?
"yes" :
"no" ) << std::endl;
193 void printProblemCondition(std::ostream& stream)
195 stream <<
"QP:" << std::endl;
196 stream <<
" num Variables: " << numVariables() << std::endl;
197 stream <<
" num Equalities: " << numEqConstr() << std::endl;
198 stream <<
" num Inequalities: " << numIneqConstr() << std::endl;
200 Eigen::JacobiSVD<typename RtMatrix<max_dim_qp, max_dim_qp>::d > Hess_svd;
201 if (objectiveQuadPart().rows() > 0)
203 Hess_svd.compute(objectiveQuadPart());
204 stream <<
" Hessian condition: " << Hess_svd.singularValues().maxCoeff()/
205 Hess_svd.singularValues().minCoeff() <<
" = " << Hess_svd.
206 singularValues().maxCoeff() <<
" / " << Hess_svd.singularValues().minCoeff() << std::endl;
208 Eigen::JacobiSVD<typename RtMatrix<max_num_eq, max_dim_qp>::d > EqConstr_svd;
209 if (eqConstraintsMat().rows() > 0)
211 EqConstr_svd.compute(eqConstraintsMat());
212 stream <<
" Equality Matrix condition: " << EqConstr_svd.singularValues().maxCoeff()/
213 EqConstr_svd.singularValues().minCoeff() <<
" = " << EqConstr_svd.
214 singularValues().maxCoeff() <<
" / " << EqConstr_svd.singularValues().minCoeff() << std::endl;
216 Eigen::JacobiSVD<typename RtMatrix<max_num_ineq, max_dim_qp>::d > IneqConstr_svd;
217 if (ineqConstraintsMat().rows() > 0)
219 IneqConstr_svd.compute(ineqConstraintsMat());
220 stream <<
" Inequality Matrix condition: " << IneqConstr_svd.singularValues().maxCoeff()/
221 IneqConstr_svd.singularValues().minCoeff() <<
" = " << IneqConstr_svd.
222 singularValues().maxCoeff() <<
" / " << IneqConstr_svd.singularValues().minCoeff() << std::endl;
225 if(numVariables() < 10)
227 stream <<
" Hessian: " << objectiveQuadPart() << std::endl;
228 stream <<
" linear part: " << objectiveLinPart().transpose() << std::endl;
229 stream <<
" Equality Matrix: " << eqConstraintsMat() << std::endl;
230 stream <<
" Equality Vector: " << eqConstraintsVec().transpose() << std::endl;
231 stream <<
" Inequality Matrix: " << ineqConstraintsMat() << std::endl;
232 stream <<
" Inequality Vector: " << ineqConstraintsVec().transpose() << std::endl;
Definition: RtQPSolverInterface.hpp:19
Definition: RtMatrix.hpp:18
Definition: RtMatrix.hpp:44