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 
100  int init_mem(void* mem) const override;
101 
105  void set_work(void* mem, const double**& arg, double**& res,
106  casadi_int*& iw, double*& w) const override;
107 
109  virtual void check_inputs(const double* lbx, const double* ubx,
110  const double* lba, const double* uba) const;
111 
113  virtual void generateNativeCode(std::ostream& file) const;
114 
115  // Creator function for internal class
116  typedef Conic* (*Creator)(const std::string& name,
117  const std::map<std::string, Sparsity>& st);
118 
119  // No static functions exposed
120  struct Exposed{ };
121 
123  static std::map<std::string, Plugin> solvers_;
124 
125 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
126  static std::mutex mutex_solvers_;
127 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
128 
130  static const std::string infix_;
131 
133  static std::string shortname() { return "conic";}
134 
138  bool is_a(const std::string& type, bool recursive) const override;
139 
143  double get_default_in(casadi_int ind) const override;
144 
146  virtual bool integer_support() const { return false;}
147 
149  virtual bool psd_support() const { return false;}
150 
152  Dict get_stats(void* mem) const override;
153 
157  void qp_codegen_body(CodeGenerator& g) const;
158 
159  protected:
161  std::vector<bool> discrete_;
162  std::vector<bool> equality_;
164 
166  Sparsity H_, A_, Q_, P_;
167 
169  casadi_int nx_;
170 
172  casadi_int na_;
173 
175  casadi_int np_;
176 
178  struct SDPToSOCPMem {
179  // Block partition vector for SOCP (block i runs from r[i] to r[i+1])
180  std::vector<casadi_int> r;
181 
182  // Tranpose of A, and corresponding mapping
184  std::vector<casadi_int> A_mapping;
185 
186  // Aggregate SOCP helper constraints (lhs)
188 
189  // Aggregate SOCP helper constraints (rhs)
190  std::vector<casadi_int> map_P;
191 
192  // Maximum size of ind/val vectors
193  casadi_int indval_size;
194  };
195 
197  void sdp_to_socp_init(SDPToSOCPMem& mem) const;
198 
199  void serialize(SerializingStream &s, const SDPToSOCPMem& m) const;
200  void deserialize(DeserializingStream &s, SDPToSOCPMem& m);
201 
202  public:
206  void serialize_body(SerializingStream &s) const override;
210  void serialize_type(SerializingStream &s) const override;
211 
215  std::string serialize_base_function() const override { return "Conic"; }
219  static ProtoFunction* deserialize(DeserializingStream& s);
220 
221  protected:
222 
226  explicit Conic(DeserializingStream& s);
227  private:
228  void set_qp_prob();
229  };
230 
231 
232 } // namespace casadi
234 #endif // CASADI_CONIC_IMPL_HPP
Helper class for C code generation.
Internal class.
Definition: conic_impl.hpp:44
static const Options options_
Options.
Definition: conic_impl.hpp:83
casadi_int nx_
Number of decision variables.
Definition: conic_impl.hpp:169
virtual int solve(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const =0
Solve the QP.
casadi_int np_
The shape of psd constraint matrix.
Definition: conic_impl.hpp:175
size_t get_n_in() override
Number of function inputs and outputs.
Definition: conic_impl.hpp:59
casadi_int na_
The number of constraints (counting both equality and inequality) == A.size1()
Definition: conic_impl.hpp:172
static std::map< std::string, Plugin > solvers_
Collection of solvers.
Definition: conic_impl.hpp:123
size_t get_n_out() override
Number of function inputs and outputs.
Definition: conic_impl.hpp:60
std::string serialize_base_function() const override
String used to identify the immediate FunctionInternal subclass.
Definition: conic_impl.hpp:215
std::vector< bool > discrete_
Options.
Definition: conic_impl.hpp:161
const Options & get_options() const override
Options.
Definition: conic_impl.hpp:84
std::vector< bool > equality_
Definition: conic_impl.hpp:162
virtual bool psd_support() const
Can psd constraints be treated.
Definition: conic_impl.hpp:149
static std::string shortname()
Short name.
Definition: conic_impl.hpp:133
static const std::string infix_
Infix.
Definition: conic_impl.hpp:130
casadi_qp_prob< double > p_qp_
Definition: conic_impl.hpp:47
std::string get_name_out(casadi_int i) override
Names of function input and outputs.
Definition: conic_impl.hpp:76
std::string get_name_in(casadi_int i) override
Names of function input and outputs.
Definition: conic_impl.hpp:75
virtual bool integer_support() const
Can discrete variables be treated.
Definition: conic_impl.hpp:146
Helper class for Serialization.
Internal class for Function.
Interface for accessing input and output data structures.
Base class for FunctionInternal and LinsolInternal.
Helper class for Serialization.
General sparsity class.
Definition: sparsity.hpp:106
std::vector< std::string > conic_out()
Get QP solver output scheme of QP solvers.
Definition: conic.cpp:67
std::vector< std::string > conic_in()
Get input scheme of QP solvers.
Definition: conic.cpp:61
The casadi namespace.
Definition: archiver.cpp:28
@ CONIC_NUM_IN
Definition: conic.hpp:196
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
@ CONIC_NUM_OUT
Definition: conic.hpp:208
casadi_qp_data< double > d_qp
Definition: conic_impl.hpp:39
SDP to SOCP conversion memory.
Definition: conic_impl.hpp:178
std::vector< casadi_int > r
Definition: conic_impl.hpp:180
std::vector< casadi_int > A_mapping
Definition: conic_impl.hpp:184
std::vector< casadi_int > map_P
Definition: conic_impl.hpp:190
Function memory with temporary work vectors.
Options metadata for a class.
Definition: options.hpp:40