hpmpc_interface.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_HPMPC_INTERFACE_HPP
27 #define CASADI_HPMPC_INTERFACE_HPP
28 
29 #include "casadi/core/conic_impl.hpp"
30 #include "casadi/core/linsol.hpp"
31 #include <casadi/interfaces/hpmpc/casadi_conic_hpmpc_export.h>
32 
60 #ifndef HPMPC_DLOPEN
61 #include <target.h>
62 extern "C" {
63 #include <c_interface.h>
64 }
65 #endif
66 
70 namespace casadi {
71 
72  // Forward declaration
73  class HpmpcInterface;
74 
75  struct HpmpcMemory : public ConicMemory {
76 
77  std::vector<double> A, B, b, b2, Q, S, R, q, r, lb, ub, C, D, lg, ug;
78  std::vector<double*> As, Bs, bs, Qs, Ss, Rs, qs, rs, lbs, ubs, Cs, Ds, lgs, ugs;
79 
80  std::vector<double> I;
81  std::vector<double*> Is;
82  std::vector<double> x, u, pi, lam;
83  std::vector<double*> xs, us, pis, lams;
84 
85  std::vector<int> hidxb;
86  std::vector<int*> hidxbs;
87 
88  std::vector<int> nx;
89  std::vector<int> nu;
90  std::vector<int> ng;
91  std::vector<int> nb;
92  std::vector<double> stats;
93 
94  std::vector<char> workspace;
95 
96  std::vector<double> pv;
97 
98  int iter_count;
99  int return_status;
100  std::vector<double> res;
101 
103  HpmpcMemory();
104 
106  ~HpmpcMemory();
107  };
108 
118  class HpmpcInterface : public Conic {
119  public:
121  explicit HpmpcInterface();
122 
124  static Conic* creator(const std::string& name,
125  const std::map<std::string, Sparsity>& st) {
126  return new HpmpcInterface(name, st);
127  }
128 
130  explicit HpmpcInterface(const std::string& name,
131  const std::map<std::string, Sparsity>& st);
132 
134  Dict get_stats(void* mem) const override;
135 
137  ~HpmpcInterface() override;
138 
139  // Get name of the plugin
140  const char* plugin_name() const override { return "hpmpc";}
141 
142  // Get name of the class
143  std::string class_name() const override { return "HpmpcInterface";}
144 
146 
147  static const Options options_;
148  const Options& get_options() const override { return options_;}
150 
152  void init(const Dict& opts) override;
153 
155  void* alloc_mem() const override { return new HpmpcMemory();}
156 
158  int init_mem(void* mem) const override;
159 
161  void free_mem(void *mem) const override { delete static_cast<HpmpcMemory*>(mem);}
162 
164  int solve(const double** arg, double** res,
165  casadi_int* iw, double* w, void* mem) const override;
166 
168  static void mproject(double factor, const double* x, const casadi_int* sp_x,
169  double* y, const casadi_int* sp_y, double* w);
170 
173  static void dense_transfer(double factor, const double* x, const casadi_int* sp_x, double* y,
174  const casadi_int* sp_y, double* w);
175 
177  static const std::string meta_doc;
178 
179 #ifdef HPMPC_DLOPEN
181  typedef int (*Work_size)(int N, int *nx, int *nu, int *nb, int **hidxb, int *ng, int N2);
183  typedef int (*Ocp_solve)(int *kk, int k_max, double mu0, double mu_tol,
184  int N, int *nx, int *nu, int *nb, int **hidxb, int *ng, int N2, int warm_start,
185  double **A, double **B, double **b, double **Q, double **S, double **R, double **q,
186  double **r, double **lb, double **ub, double **C, double **D, double **lg, double **ug,
187  double **x, double **u, double **pi, double **lam, /*double **t,*/
188  double *inf_norm_res, void *work0, double *stat);
189 #endif
190 
191  protected:
192 
193  struct Block {
194  casadi_int offset_r;
195  casadi_int offset_c;
196  casadi_int rows;
197  casadi_int cols;
198  };
199 
200  static Sparsity blocksparsity(casadi_int rows, casadi_int cols,
201  const std::vector<Block>& blocks, bool eye=false);
202  static void blockptr(std::vector<double *>& vs, std::vector<double>& v,
203  const std::vector<Block>& blocks, bool eye=false);
204  Sparsity Asp_, Bsp_, Csp_, Dsp_, Isp_, Rsp_, Ssp_, Qsp_, bsp_, lugsp_, usp_, xsp_;
205  Sparsity theirs_xsp_, theirs_usp_, theirs_Xsp_, theirs_Usp_;
206  Sparsity lamg_gapsp_, lamg_csp_, lam_ulsp_, lam_uusp_, lam_xlsp_, lam_xusp_, lam_clsp_;
207  Sparsity lam_cusp_, pisp_;
208 
209  std::vector< Block > R_blocks, S_blocks, Q_blocks;
210  std::vector< Block > b_blocks, lug_blocks;
211  std::vector< Block > u_blocks, x_blocks;
212  std::vector< Block > lam_ul_blocks, lam_xl_blocks, lam_uu_blocks, lam_xu_blocks, lam_cl_blocks;
213  std::vector< Block > lam_cu_blocks, A_blocks, B_blocks, C_blocks, D_blocks, I_blocks;
214 
215  std::vector<casadi_int> nxs_;
216  std::vector<casadi_int> nus_;
217  std::vector<casadi_int> ngs_;
218  casadi_int N_;
219  casadi_int print_level_;
220 
221  bool warm_start_;
222  double inf_;
223 
224  double mu0_; // max element in cost function as estimate of max multiplier
225  casadi_int max_iter_; // maximum number of iterations
226  double tol_; // tolerance in the duality measure
227 
228  std::string blasfeo_target_;
229  std::string target_;
230 
231 #ifdef HPMPC_DLOPEN
232  Work_size hpmpc_d_ip_ocp_hard_tv_work_space_size_bytes;
233  Ocp_solve fortran_order_d_ip_ocp_hard_tv;
234 #endif
235 
236 
237  };
238 
239 } // namespace casadi
240 
242 #endif // CASADI_HPMPC_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.