hpipm_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_HPIPM_INTERFACE_HPP
27 #define CASADI_HPIPM_INTERFACE_HPP
28 
29 #include "casadi/core/conic_impl.hpp"
30 #include "casadi/core/linsol.hpp"
31 #include <casadi/interfaces/hpipm/casadi_conic_hpipm_export.h>
32 
59 #include <blasfeo_d_aux_ext_dep.h>
60 
61 #include <hpipm_d_ocp_qp_ipm.h>
62 #include <hpipm_d_ocp_qp_dim.h>
63 #include <hpipm_d_ocp_qp.h>
64 #include <hpipm_d_ocp_qp_sol.h>
65 #include <hpipm_d_ocp_qp_utils.h>
66 
67 
68 namespace casadi {
69  #include "hpipm_runtime.hpp"
70 }
74 namespace casadi {
75 
76  // Forward declaration
77  class HpipmInterface;
78  struct HpipmMemory : public ConicMemory {
79  // Problem data structure
81 
82  // unused: lamg, xs, us, workspace, stats
83 
85  HpipmMemory();
86 
88  ~HpipmMemory();
89  };
90 
100  class HpipmInterface : public Conic {
101  public:
103  explicit HpipmInterface();
104 
106  static Conic* creator(const std::string& name,
107  const std::map<std::string, Sparsity>& st) {
108  return new HpipmInterface(name, st);
109  }
110 
112  explicit HpipmInterface(const std::string& name,
113  const std::map<std::string, Sparsity>& st);
114 
116  Dict get_stats(void* mem) const override;
117 
119  ~HpipmInterface() override;
120 
121  // Get name of the plugin
122  const char* plugin_name() const override { return "hpipm";}
123 
124  // Get name of the class
125  std::string class_name() const override { return "HpipmInterface";}
126 
128 
129  static const Options options_;
130  const Options& get_options() const override { return options_;}
132 
133  void set_hpipm_prob();
134  void set_hpipm_prob(CodeGenerator& g) const;
135 
137  void codegen_body(CodeGenerator& g) const override;
138 
140  void init(const Dict& opts) override;
141 
143  void* alloc_mem() const override { return new HpipmMemory();}
144 
146  int init_mem(void* mem) const override;
147 
149  void free_mem(void *mem) const override { delete static_cast<HpipmMemory*>(mem);}
150 
152  void set_work(void* mem, const double**& arg, double**& res,
153  casadi_int*& iw, double*& w) const override;
154 
156  int solve(const double** arg, double** res,
157  casadi_int* iw, double* w, void* mem) const override;
158 
160  static void mproject(double factor, const double* x, const casadi_int* sp_x,
161  double* y, const casadi_int* sp_y, double* w);
162 
165  static void dense_transfer(double factor, const double* x, const casadi_int* sp_x, double* y,
166  const casadi_int* sp_y, double* w);
167 
169  static const std::string meta_doc;
170 
171  void serialize_body(SerializingStream &s) const override;
172 
174  static ProtoFunction* deserialize(DeserializingStream& s) { return new HpipmInterface(s); }
175 
176  protected:
177  explicit HpipmInterface(DeserializingStream& s);
178 
179  // Memory structure
181 
182  static Sparsity blocksparsity(casadi_int rows, casadi_int cols,
183  const std::vector<casadi_hpipm_block>& blocks, bool eye=false);
184  static void blockptr(std::vector<double *>& vs, std::vector<double>& v,
185  const std::vector<casadi_hpipm_block>& blocks, bool eye=false);
186  Sparsity Asp_, Bsp_, Csp_, Dsp_, Isp_, Rsp_, Ssp_, Qsp_, bsp_, lugsp_, usp_, xsp_;
187  Sparsity theirs_xsp_, theirs_usp_, theirs_Xsp_, theirs_Usp_;
188  Sparsity lamg_gapsp_;
189  Sparsity lam_cusp_, pisp_;
190 
191  std::vector< casadi_hpipm_block > R_blocks, S_blocks, Q_blocks;
192  std::vector< casadi_hpipm_block > b_blocks, lug_blocks;
193  std::vector< casadi_hpipm_block > u_blocks, x_blocks;
194  std::vector< casadi_hpipm_block > lam_ul_blocks, lam_xl_blocks,
195  lam_uu_blocks, lam_xu_blocks, lam_cl_blocks;
196  std::vector< casadi_hpipm_block > lam_cu_blocks, A_blocks, B_blocks,
197  C_blocks, D_blocks, I_blocks;
198 
199  std::vector<int> nxs_;
200  std::vector<int> nus_;
201  std::vector<int> ngs_;
202  std::vector<int> zeros_;
203  casadi_int N_;
204  casadi_int print_level_;
205 
206  bool warm_start_;
207  double inf_;
208 
209  d_ocp_qp_ipm_arg hpipm_options_;
210 
211  };
212 
213 
214 
215 } // namespace casadi
216 
218 #endif // CASADI_HPIPM_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.