Crocoddyl
Loading...
Searching...
No Matches
box-qp.hpp
1
2// BSD 3-Clause License
3//
4// Copyright (C) 2019-2025, University of Edinburgh, Heriot-Watt University
5// Copyright note valid unless otherwise stated in individual files.
6// All rights reserved.
8
9#ifndef CROCODDYL_CORE_SOLVERS_BOX_QP_HPP_
10#define CROCODDYL_CORE_SOLVERS_BOX_QP_HPP_
11
12#include "crocoddyl/core/fwd.hpp"
13#include "crocoddyl/core/utils/exception.hpp"
14
15namespace crocoddyl {
16
26template <typename _Scalar>
28 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
29
30 typedef _Scalar Scalar;
31 typedef MathBaseTpl<Scalar> MathBase;
32 typedef typename MathBase::VectorXs VectorXs;
33 typedef typename MathBase::MatrixXs MatrixXs;
34
39
48 BoxQPSolutionTpl(const MatrixXs& Hff_inv, const VectorXs& x,
49 const std::vector<size_t>& free_idx,
50 const std::vector<size_t>& clamped_idx)
52
62 template <typename NewScalar>
64 typedef BoxQPSolutionTpl<NewScalar> ReturnType;
65 ReturnType ret(Hff_inv.template cast<NewScalar>(),
67 return ret;
68 }
69
70 MatrixXs Hff_inv;
71 VectorXs x;
72 std::vector<size_t> free_idx;
73 std::vector<size_t> clamped_idx;
74};
75
102template <typename _Scalar>
103class BoxQPTpl {
104 public:
105 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
106
107 typedef _Scalar Scalar;
108 typedef BoxQPSolutionTpl<Scalar> BoxQPSolution;
109 typedef MathBaseTpl<Scalar> MathBase;
110 typedef typename MathBase::VectorXs VectorXs;
111 typedef typename MathBase::MatrixXs MatrixXs;
112
123 BoxQPTpl(const std::size_t nx, const std::size_t maxiter = 100,
124 const Scalar th_acceptstep = Scalar(0.1),
125 const Scalar th_grad = Scalar(1e-9),
126 const Scalar reg = Scalar(1e-9));
130 ~BoxQPTpl() = default;
131
143 const BoxQPSolution& solve(const MatrixXs& H, const VectorXs& q,
144 const VectorXs& lb, const VectorXs& ub,
145 const VectorXs& xinit);
146
155 template <typename NewScalar>
157
161 const BoxQPSolution& get_solution() const;
162
166 std::size_t get_nx() const;
167
171 std::size_t get_maxiter() const;
172
176 Scalar get_th_acceptstep() const;
177
181 Scalar get_th_grad() const;
182
186 Scalar get_reg() const;
187
191 const std::vector<Scalar>& get_alphas() const;
192
196 void set_nx(const std::size_t nx);
197
201 void set_maxiter(const std::size_t maxiter);
202
206 void set_th_acceptstep(const Scalar th_acceptstep);
207
211 void set_th_grad(const Scalar th_grad);
212
216 void set_reg(const Scalar reg);
217
221 void set_alphas(const std::vector<Scalar>& alphas);
222
223 private:
224 std::size_t nx_;
225 BoxQPSolution solution_;
226 std::size_t maxiter_;
227 Scalar th_acceptstep_;
228 Scalar
229 th_grad_;
230 Scalar reg_;
231
232 Scalar fold_;
233 Scalar fnew_;
234 std::size_t nf_;
235 std::size_t nc_;
236 std::vector<Scalar>
237 alphas_;
238 VectorXs x_;
239 VectorXs xnew_;
240 VectorXs g_;
241 VectorXs dx_;
242
243 VectorXs xo_;
244 VectorXs
245 dxo_;
246 VectorXs qo_;
247 MatrixXs Ho_;
248
249 Eigen::LLT<MatrixXs> Hff_inv_llt_;
250};
251
252} // namespace crocoddyl
253
254/* --- Details -------------------------------------------------------------- */
255/* --- Details -------------------------------------------------------------- */
256/* --- Details -------------------------------------------------------------- */
257#include "crocoddyl/core/solvers/box-qp.hxx"
258
259CROCODDYL_DECLARE_EXTERN_TEMPLATE_CLASS(crocoddyl::BoxQPTpl)
260
261#endif // CROCODDYL_CORE_SOLVERS_BOX_QP_HPP_
This class implements a Box QP solver based on a Projected Newton method.
Definition box-qp.hpp:103
void set_th_acceptstep(const Scalar th_acceptstep)
Modify the acceptance step threshold.
const BoxQPSolution & get_solution() const
Return the stored solution.
void set_reg(const Scalar reg)
Modify the regularization value.
const std::vector< Scalar > & get_alphas() const
Return the stack of step lengths using by the line-search procedure.
std::size_t get_maxiter() const
Return the maximum allowed number of iterations.
Scalar get_th_acceptstep() const
Return the acceptance step threshold.
void set_maxiter(const std::size_t maxiter)
Modify the maximum allowed number of iterations.
BoxQPTpl(const std::size_t nx, const std::size_t maxiter=100, const Scalar th_acceptstep=Scalar(0.1), const Scalar th_grad=Scalar(1e-9), const Scalar reg=Scalar(1e-9))
Initialize the Projected-Newton QP for bound constraints.
~BoxQPTpl()=default
Destroy the Projected-Newton QP solver.
Scalar get_reg() const
Return the regularization value.
void set_nx(const std::size_t nx)
Modify the decision vector dimension.
const BoxQPSolution & solve(const MatrixXs &H, const VectorXs &q, const VectorXs &lb, const VectorXs &ub, const VectorXs &xinit)
Compute the solution of bound-constrained QP based on Newton projection.
Scalar get_th_grad() const
Return the gradient tolerance threshold.
void set_th_grad(const Scalar th_grad)
Modify the gradient tolerance threshold.
void set_alphas(const std::vector< Scalar > &alphas)
Modify the stack of step lengths using by the line-search procedure.
std::size_t get_nx() const
Return the decision vector dimension.
BoxQPTpl< NewScalar > cast() const
Cast the BoxQP solver to a different scalar type.
Box QP solution.
Definition box-qp.hpp:27
BoxQPSolutionTpl()
Initialize the QP solution structure.
Definition box-qp.hpp:38
std::vector< size_t > free_idx
Definition box-qp.hpp:72
BoxQPSolutionTpl< NewScalar > cast() const
Cast the BoxQP solution to a different scalar type.
Definition box-qp.hpp:63
BoxQPSolutionTpl(const MatrixXs &Hff_inv, const VectorXs &x, const std::vector< size_t > &free_idx, const std::vector< size_t > &clamped_idx)
Initialize the QP solution structure.
Definition box-qp.hpp:48
std::vector< size_t > clamped_idx
Definition box-qp.hpp:73