conic_impl.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_CONIC_IMPL_HPP
27 #define CASADI_CONIC_IMPL_HPP
28 
29 #include "conic.hpp"
30 #include "function_internal.hpp"
31 #include "plugin_interface.hpp"
32 #include "im.hpp"
33 
35 namespace casadi {
36 
37  struct CASADI_EXPORT ConicMemory : public FunctionMemory {
38  // Problem data structure
40 
41  };
42 
44  class CASADI_EXPORT Conic : public FunctionInternal, public PluginInterface<Conic> {
45  public:
46  // Memory structure
48 
49  // Constructor
50  Conic(const std::string& name, const std::map<std::string, Sparsity> &st);
51 
52  // Destructor
53  ~Conic() override = 0;
54 
56 
59  size_t get_n_in() override { return CONIC_NUM_IN;}
60  size_t get_n_out() override { return CONIC_NUM_OUT;}
62 
64 
67  Sparsity get_sparsity_in(casadi_int i) override;
68  Sparsity get_sparsity_out(casadi_int i) override;
70 
72 
75  std::string get_name_in(casadi_int i) override { return conic_in(i);}
76  std::string get_name_out(casadi_int i) override { return conic_out(i);}
78 
80 
83  static const Options options_;
84  const Options& get_options() const override { return options_;}
86 
88  int eval(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const final;
89 
91  virtual int solve(const double** arg, double** res,
92  casadi_int* iw, double* w, void* mem) const = 0;
93 
94  // Initialize
95  void init(const Dict& opts) override;
96 
97  // Finalize the object creation
98  void finalize() override;
99 
103  int init_mem(void* mem) const override;
104 
108  void set_work(void* mem, const double**& arg, double**& res,
109  casadi_int*& iw, double*& w) const override;
110 
112  virtual void check_inputs(const double* lbx, const double* ubx,
113  const double* lba, const double* uba) const;
114 
116  virtual void generateNativeCode(std::ostream& file) const;
117 
118  // Creator function for internal class
119  typedef Conic* (*Creator)(const std::string& name,
120  const std::map<std::string, Sparsity>& st);
121 
122  // No static functions exposed
123  struct Exposed{ };
124 
126  static std::map<std::string, Plugin> solvers_;
127 
128 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
129  static std::mutex mutex_solvers_;
130 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
131 
133  static const std::string infix_;
134 
136  static std::string shortname() { return "conic";}
137 
141  bool is_a(const std::string& type, bool recursive) const override;
142 
146  double get_default_in(casadi_int ind) const override;
147 
149  virtual bool integer_support() const { return false;}
150 
152  virtual bool psd_support() const { return false;}
153 
155  Dict get_stats(void* mem) const override;
156 
157  // KKT matrix for parametric sensitivities, active set given by the multiplier signs
158  static MX sens_kkt(const MX& H, const MX& A, const MX& lam_x, const MX& lam_a,
159  const MX& lbx, const MX& ubx, const MX& lba, const MX& uba,
160  double min_lam, bool force_equality,
161  MX& lbIx, MX& ubIx, MX& lbIa, MX& ubIa);
162 
166  bool uses_output() const override {return true;}
167 
169 
172  bool has_forward(casadi_int nfwd) const override { return true;}
173  Function get_forward(casadi_int nfwd, const std::string& name,
174  const std::vector<std::string>& inames,
175  const std::vector<std::string>& onames,
176  const Dict& opts) const override;
178 
180 
183  bool has_reverse(casadi_int nadj) const override { return true;}
184  Function get_reverse(casadi_int nadj, const std::string& name,
185  const std::vector<std::string>& inames,
186  const std::vector<std::string>& onames,
187  const Dict& opts) const override;
189 
193  void qp_codegen_body(CodeGenerator& g) const;
194 
195  protected:
197  std::vector<bool> discrete_;
198  std::vector<bool> equality_;
199  bool print_problem_;
200  bool solver_version_check_;
201  double sens_min_lam_;
202  bool sens_force_equality_;
203  std::string sens_linsol_;
204  Dict sens_linsol_options_;
205 
207  Sparsity H_, A_, Q_, P_;
208 
210  casadi_int nx_;
211 
213  casadi_int na_;
214 
216  casadi_int np_;
217 
219  struct SDPToSOCPMem {
220  // Block partition vector for SOCP (block i runs from r[i] to r[i+1])
221  std::vector<casadi_int> r;
222 
223  // Tranpose of A, and corresponding mapping
224  Sparsity AT;
225  std::vector<casadi_int> A_mapping;
226 
227  // Aggregate SOCP helper constraints (lhs)
228  IM map_Q;
229 
230  // Aggregate SOCP helper constraints (rhs)
231  std::vector<casadi_int> map_P;
232 
233  // Maximum size of ind/val vectors
234  casadi_int indval_size;
235  };
236 
238  void sdp_to_socp_init(SDPToSOCPMem& mem) const;
239 
240  void serialize(SerializingStream &s, const SDPToSOCPMem& m) const;
241  void deserialize(DeserializingStream &s, SDPToSOCPMem& m);
242 
243  public:
247  void serialize_body(SerializingStream &s) const override;
251  void serialize_type(SerializingStream &s) const override;
252 
256  std::string serialize_base_function() const override { return "Conic"; }
260  static ProtoFunction* deserialize(DeserializingStream& s);
261 
262  protected:
263 
267  explicit Conic(DeserializingStream& s);
268  private:
269  void set_qp_prob();
270  };
271 
272 
273 } // namespace casadi
275 #endif // CASADI_CONIC_IMPL_HPP
CASADI_EXPORT std::vector< std::string > conic_out()
Get the output scheme of QP solvers.
CASADI_EXPORT std::vector< std::string > conic_in()
Get the input scheme of QP solvers.
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
Matrix< casadi_int > IM
Definition: im_fwd.hpp:31