fatrop_conic_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_FATROP_CONIC_INTERFACE_HPP
27 #define CASADI_FATROP_CONIC_INTERFACE_HPP
28 
29 #include "casadi/core/conic_impl.hpp"
30 #include "casadi/core/linsol.hpp"
31 #include <casadi/interfaces/fatrop/casadi_conic_fatrop_export.h>
32 
60 namespace casadi {
61  #include "fatrop_conic_runtime.hpp"
62 }
63 
64 #include <ocp/OCPAbstract.hpp>
65 #include <ocp/StageOCPApplication.hpp>
66 #include <ocp/OCPCInterface.h>
67 
71 namespace casadi {
72 
73  // Forward declaration
74  class FatropConicInterface;
75  struct FatropConicMemory : public ConicMemory {
76  // Problem data structure
78 
79  // unused: lamg, xs, us, workspace, stats
80 
82  FatropConicMemory();
83 
85  ~FatropConicMemory();
86  };
87 
97  class FatropConicInterface : public Conic {
98  public:
100  explicit FatropConicInterface();
101 
103  static Conic* creator(const std::string& name,
104  const std::map<std::string, Sparsity>& st) {
105  return new FatropConicInterface(name, st);
106  }
107 
109  explicit FatropConicInterface(const std::string& name,
110  const std::map<std::string, Sparsity>& st);
111 
113  Dict get_stats(void* mem) const override;
114 
116  ~FatropConicInterface() override;
117 
118  // Get name of the plugin
119  const char* plugin_name() const override { return "fatrop";}
120 
121  // Get name of the class
122  std::string class_name() const override { return "FatropConicInterface";}
123 
125 
126  static const Options options_;
127  const Options& get_options() const override { return options_;}
129 
130  void set_fatrop_conic_prob();
131 
133  void init(const Dict& opts) override;
134 
136  void* alloc_mem() const override { return new FatropConicMemory();}
137 
139  int init_mem(void* mem) const override;
140 
142  void free_mem(void *mem) const override { delete static_cast<FatropConicMemory*>(mem);}
143 
145  void set_work(void* mem, const double**& arg, double**& res,
146  casadi_int*& iw, double*& w) const override;
148  void set_temp(void* mem, const double** arg, double** res,
149  casadi_int* iw, double* w) const override;
150 
152  int solve(const double** arg, double** res,
153  casadi_int* iw, double* w, void* mem) const override;
154 
156  static void mproject(double factor, const double* x, const casadi_int* sp_x,
157  double* y, const casadi_int* sp_y, double* w);
158 
161  static void dense_transfer(double factor, const double* x, const casadi_int* sp_x, double* y,
162  const casadi_int* sp_y, double* w);
163 
165  static const std::string meta_doc;
166 
167  void serialize_body(SerializingStream &s) const override;
168 
170  static ProtoFunction* deserialize(DeserializingStream& s) {
171  return new FatropConicInterface(s);
172  }
173 
174  friend class CasadiStructuredQP;
175 
176  public:
177  explicit FatropConicInterface(DeserializingStream& s);
178 
179  // Memory structure
181 
182  static Sparsity blocksparsity(casadi_int rows, casadi_int cols,
183  const std::vector<casadi_ocp_block>& blocks, bool eye=false);
184  static void blockptr(std::vector<double *>& vs, std::vector<double>& v,
185  const std::vector<casadi_ocp_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  Sparsity ABsp_, CDsp_, RSQsp_;
192 
193  std::vector< casadi_ocp_block > R_blocks, S_blocks, Q_blocks;
194  std::vector< casadi_ocp_block > b_blocks, lug_blocks;
195  std::vector< casadi_ocp_block > u_blocks, x_blocks;
196  std::vector< casadi_ocp_block > lam_ul_blocks, lam_xl_blocks,
197  lam_uu_blocks, lam_xu_blocks, lam_cl_blocks;
198  std::vector< casadi_ocp_block > lam_cu_blocks, A_blocks, B_blocks,
199  C_blocks, D_blocks, I_blocks;
200 
201 
202  std::vector< casadi_ocp_block > AB_blocks, CD_blocks, RSQ_blocks;
203 
204  std::vector<int> nxs_;
205  std::vector<int> nus_;
206  std::vector<int> ngs_;
207  std::vector<int> zeros_;
208  casadi_int N_;
209  casadi_int print_level_;
210 
211 
212  // An enum field for the structure detection
213  enum structure_detection {
214  STRUCTURE_NONE,
215  STRUCTURE_AUTO,
216  STRUCTURE_MANUAL
217  };
218  structure_detection structure_detection_;
219 
220  std::vector<casadi_int> AB_offsets_, CD_offsets_, RSQ_offsets_;
221 
222  bool warm_start_;
223  double inf_;
224 
225  };
226 
227 
228 
229 } // namespace casadi
230 
232 #endif // CASADI_FATROP_CONIC_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.