Crocoddyl
box-qp.hpp
1 // BSD 3-Clause License
3 //
4 // Copyright (C) 2019-2022, 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 <Eigen/Cholesky>
13 #include <Eigen/Dense>
14 #include <vector>
15 
16 #include "crocoddyl/core/utils/exception.hpp"
17 
18 namespace crocoddyl {
19 
29 struct BoxQPSolution {
30  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
31 
36 
45  BoxQPSolution(const Eigen::MatrixXd& Hff_inv, const Eigen::VectorXd& x,
46  const std::vector<size_t>& free_idx,
47  const std::vector<size_t>& clamped_idx)
49 
50  Eigen::MatrixXd Hff_inv;
51  Eigen::VectorXd x;
52  std::vector<size_t> free_idx;
53  std::vector<size_t> clamped_idx;
54 };
55 
82 class BoxQP {
83  public:
84  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
85 
96  BoxQP(const std::size_t nx, const std::size_t maxiter = 100,
97  const double th_acceptstep = 0.1, const double th_grad = 1e-9,
98  const double reg = 1e-9);
102  ~BoxQP();
103 
115  const BoxQPSolution& solve(const Eigen::MatrixXd& H, const Eigen::VectorXd& q,
116  const Eigen::VectorXd& lb,
117  const Eigen::VectorXd& ub,
118  const Eigen::VectorXd& xinit);
119 
123  const BoxQPSolution& get_solution() const;
124 
128  std::size_t get_nx() const;
129 
133  std::size_t get_maxiter() const;
134 
138  double get_th_acceptstep() const;
139 
143  double get_th_grad() const;
144 
148  double get_reg() const;
149 
153  const std::vector<double>& get_alphas() const;
154 
158  void set_nx(const std::size_t nx);
159 
163  void set_maxiter(const std::size_t maxiter);
164 
168  void set_th_acceptstep(const double th_acceptstep);
169 
173  void set_th_grad(const double th_grad);
174 
178  void set_reg(const double reg);
179 
183  void set_alphas(const std::vector<double>& alphas);
184 
185  private:
186  std::size_t nx_;
187  BoxQPSolution solution_;
188  std::size_t maxiter_;
189  double th_acceptstep_;
190  double
191  th_grad_;
192  double reg_;
193 
194  double fold_;
195  double fnew_;
196  std::size_t nf_;
197  std::size_t nc_;
198  std::vector<double>
199  alphas_;
200  Eigen::VectorXd x_;
201  Eigen::VectorXd xnew_;
202  Eigen::VectorXd g_;
203  Eigen::VectorXd dx_;
204 
205  Eigen::VectorXd xo_;
206  Eigen::VectorXd
207  dxo_;
208  Eigen::VectorXd
209  qo_;
210  Eigen::MatrixXd Ho_;
211 
212  Eigen::LLT<Eigen::MatrixXd> Hff_inv_llt_;
213 };
214 
215 } // namespace crocoddyl
216 
217 #endif // CROCODDYL_CORE_SOLVERS_BOX_QP_HPP_
This class implements a Box QP solver based on a Projected Newton method.
Definition: box-qp.hpp:82
void set_reg(const double reg)
Modify the regularization value.
Definition: box-qp.cpp:239
void set_th_grad(const double th_grad)
Modify the gradient tolerance threshold.
Definition: box-qp.cpp:231
const BoxQPSolution & get_solution() const
Return the stored solution.
Definition: box-qp.cpp:195
double get_th_grad() const
Return the gradient tolerance threshold.
Definition: box-qp.cpp:203
const BoxQPSolution & solve(const Eigen::MatrixXd &H, const Eigen::VectorXd &q, const Eigen::VectorXd &lb, const Eigen::VectorXd &ub, const Eigen::VectorXd &xinit)
Compute the solution of bound-constrained QP based on Newton projection.
Definition: box-qp.cpp:69
std::size_t get_maxiter() const
Return the maximum allowed number of iterations.
Definition: box-qp.cpp:199
void set_maxiter(const std::size_t maxiter)
Modify the maximum allowed number of iterations.
Definition: box-qp.cpp:221
~BoxQP()
Destroy the Projected-Newton QP solver.
Definition: box-qp.cpp:67
void set_nx(const std::size_t nx)
Modify the decision vector dimension.
Definition: box-qp.cpp:209
EIGEN_MAKE_ALIGNED_OPERATOR_NEW BoxQP(const std::size_t nx, const std::size_t maxiter=100, const double th_acceptstep=0.1, const double th_grad=1e-9, const double reg=1e-9)
Initialize the Projected-Newton QP for bound constraints.
Definition: box-qp.cpp:17
const std::vector< double > & get_alphas() const
Return the stack of step lengths using by the line-search procedure.
Definition: box-qp.cpp:207
void set_alphas(const std::vector< double > &alphas)
Modify the stack of step lengths using by the line-search procedure.
Definition: box-qp.cpp:247
void set_th_acceptstep(const double th_acceptstep)
Modify the acceptance step threshold.
Definition: box-qp.cpp:223
double get_th_acceptstep() const
Return the acceptance step threshold.
Definition: box-qp.cpp:201
double get_reg() const
Return the regularization value.
Definition: box-qp.cpp:205
std::size_t get_nx() const
Return the decision vector dimension.
Definition: box-qp.cpp:197
Box QP solution.
Definition: box-qp.hpp:29
std::vector< size_t > free_idx
Free space indexes.
Definition: box-qp.hpp:52
EIGEN_MAKE_ALIGNED_OPERATOR_NEW BoxQPSolution()
Initialize the QP solution structure.
Definition: box-qp.hpp:35
Eigen::MatrixXd Hff_inv
Inverse of the free space Hessian.
Definition: box-qp.hpp:50
Eigen::VectorXd x
Decision vector.
Definition: box-qp.hpp:51
BoxQPSolution(const Eigen::MatrixXd &Hff_inv, const Eigen::VectorXd &x, const std::vector< size_t > &free_idx, const std::vector< size_t > &clamped_idx)
Initialize the QP solution structure.
Definition: box-qp.hpp:45
std::vector< size_t > clamped_idx
Clamped space indexes.
Definition: box-qp.hpp:53