- 3.0.2 optimal control module.
HPIPMInterface.hpp
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include "LQOCSolver.hpp"
9 
10 #ifdef HPIPM
11 
12 #include <blasfeo_target.h>
13 #include <blasfeo_common.h>
14 #include <blasfeo_d_aux.h>
15 #include <blasfeo_d_aux_ext_dep.h>
16 
17 extern "C" {
18 #include <hpipm_d_ocp_qp.h>
19 #include <hpipm_d_ocp_qp_sol.h>
20 #include <hpipm_d_ocp_qp_ipm.h>
21 #include <hpipm_d_ocp_qp_dim.h>
22 #include <hpipm_timing.h>
23 }
24 
25 #include <unsupported/Eigen/MatrixFunctions>
26 
27 
28 namespace ct {
29 namespace optcon {
30 
40 template <int STATE_DIM, int CONTROL_DIM>
41 class HPIPMInterface : public LQOCSolver<STATE_DIM, CONTROL_DIM>
42 {
43 public:
44  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
45 
46  static const int state_dim = STATE_DIM;
47  static const int control_dim = CONTROL_DIM;
48  static const int max_box_constr_dim = STATE_DIM + CONTROL_DIM;
49 
50  using StateMatrix = ct::core::StateMatrix<STATE_DIM>;
52  using ControlMatrix = ct::core::ControlMatrix<CONTROL_DIM>;
56 
59 
60  // definitions for variable-size constraints
61  using constr_vec_t = Eigen::Matrix<double, -1, 1>;
62  using constr_vec_array_t = ct::core::DiscreteArray<constr_vec_t>;
63 
65  using box_constr_sparsity_t = Eigen::Matrix<int, max_box_constr_dim, 1>;
66 
68  HPIPMInterface();
69 
71  virtual ~HPIPMInterface();
72 
73  virtual void configure(const NLOptConSettings& settings) override;
74 
75  void solve() override;
76 
77  virtual void computeStatesAndControls() override;
78  virtual void computeFeedbackMatrices() override;
79  virtual void compute_lv() override;
80 
81  void printSolution();
82 
84  virtual bool configureInputBoxConstraints(
85  std::shared_ptr<LQOCProblem<STATE_DIM, CONTROL_DIM>> lqocProblem) override;
86  virtual bool configureStateBoxConstraints(
87  std::shared_ptr<LQOCProblem<STATE_DIM, CONTROL_DIM>> lqocProblem) override;
88 
90  virtual bool configureGeneralConstraints(std::shared_ptr<LQOCProblem<STATE_DIM, CONTROL_DIM>> lqocProblem) override;
91 
104  virtual void initializeAndAllocate() override;
105 
107  virtual const ct::core::ControlVectorArray<CONTROL_DIM>& get_lv() override;
108 
109 private:
110  void setSolverDimensions(const int N, const int nbu = 0, const int nbx = 0, const int ng = 0);
111 
119  void setProblemImpl(std::shared_ptr<LQOCProblem<STATE_DIM, CONTROL_DIM>> lqocProblem) override;
120 
140  void setupCostAndDynamics(StateMatrixArray& A,
142  StateVectorArray& b,
143  FeedbackArray& P,
144  StateVectorArray& qv,
145  StateMatrixArray& Q,
146  ControlVectorArray& rv,
147  ControlMatrixArray& R);
148 
153  bool changeProblemSize(std::shared_ptr<LQOCProblem<STATE_DIM, CONTROL_DIM>> lqocProblem);
154 
159  void computeLrInvArray();
160  bool isLrInvComputed_;
161 
163  void d_print_mat(int m, int n, double* A, int lda);
164 
166  void d_print_e_tran_mat(int row, int col, double* A, int lda);
167 
169  int N_;
170 
171  std::vector<int> nx_;
172  std::vector<int> nu_;
173 
174  std::vector<int> nbu_;
175  std::vector<int> nbx_;
176 
177  std::vector<int> ng_;
178 
179  std::vector<int> nsbx_; // number of softed constraints on state box constraints
180  std::vector<int> nsbu_; // number of softed constraints on input box constraints
181  std::vector<int> nsg_; // number of softed constraints on general constraints
182 
183  std::vector<double*> hA_;
184  std::vector<double*> hB_;
185  std::vector<double*> hb_;
186  Eigen::Matrix<double, state_dim, 1> hb0_;
187 
188  std::vector<double*> hQ_;
189  std::vector<double*> hS_;
190  std::vector<double*> hR_;
191  std::vector<double*> hq_;
192  std::vector<double*> hr_;
193  Eigen::Matrix<double, control_dim, 1> hr0_;
194 
195  std::vector<double*> hlbx_;
196  std::vector<double*> hubx_;
197  std::vector<double*> hlbu_;
198  std::vector<double*> hubu_;
199 
200  std::vector<int*> hidxbx_;
201  std::vector<int*> hidxbu_;
202 
203  std::vector<double*> hlg_;
204  std::vector<double*> hug_;
205  std::vector<double*> hC_;
206  std::vector<double*> hD_;
207 
208  // local vars for constraint bounds for statge k=0, which need to be different by HPIPM convention
209  Eigen::VectorXd hd_lg_0_Eigen_;
210  Eigen::VectorXd hd_ug_0_Eigen_;
211 
212  std::vector<double*> hZl_; // todo what are those quantities? (related to soft constraints?)
213  std::vector<double*> hZu_;
214  std::vector<double*> hzl_;
215  std::vector<double*> hzu_;
216  std::vector<int*> hidxs_;
217  std::vector<double*> hlls_;
218  std::vector<double*> hlus_;
219 
220  // cached data for efficiency
221  ct::core::DiscreteArray<ct::core::ControlMatrix<control_dim>> Lr_inv_; // inv of cholesky matrix of hessian H
222 
223 
225  NLOptConSettings settings_;
226 
228  int dim_size_;
229  void* dim_mem_;
230  struct d_ocp_qp_dim dim_;
231 
232  void* qp_mem_;
233  struct d_ocp_qp qp_;
234 
235  void* qp_sol_mem_;
236  struct d_ocp_qp_sol qp_sol_;
237 
238  void* ipm_arg_mem_;
239  struct d_ocp_qp_ipm_arg arg_;
240 
241  // workspace
242  void* ipm_mem_;
243  struct d_ocp_qp_ipm_ws workspace_;
244  int hpipm_status_; // status code after solving
245 
246  // todo make this a setting
247  ::hpipm_mode mode_ = ::hpipm_mode::SPEED; // ROBUST/BALANCED; see also hpipm_common.h
248 };
249 
250 } // namespace optcon
251 } // namespace ct
252 
253 #endif // HPIPM
void printSolution(const ct::core::StateVectorArray< state_dim > &x, const ct::core::ControlVectorArray< control_dim > &u, const ct::core::FeedbackArray< state_dim, control_dim > &K)
Definition: ConstrainedLQOCSolverTest.h:16
DiscreteArray< StateMatrix< STATE_DIM, SCALAR > > StateMatrixArray
const size_t state_dim
Definition: ConstraintExampleOutput.cpp:17
DiscreteArray< ControlMatrix< CONTROL_DIM, SCALAR > > ControlMatrixArray
DiscreteArray< StateControlMatrix< STATE_DIM, CONTROL_DIM, SCALAR > > StateControlMatrixArray
const size_t control_dim
Definition: ConstraintExampleOutput.cpp:18