osqp_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_OSQP_INTERFACE_HPP
27 #define CASADI_OSQP_INTERFACE_HPP
28 
29 #include "casadi/core/conic_impl.hpp"
30 #include <casadi/interfaces/osqp/casadi_conic_osqp_export.h>
31 
32 // OSQP header
33 extern "C" {
34 #include "osqp.h" // NOLINT(build/include)
35 }
36 
47 namespace casadi {
48 
49  struct OsqpMemory : public ConicMemory {
50  // Structures
51 #ifdef WITH_OSQP_V1
52  OSQPSolver* work;
53 #else
54  OSQPWorkspace* work;
55 #endif
56 
58  OsqpMemory();
59 
61  ~OsqpMemory();
62  };
63 
70  class OsqpInterface : public Conic {
71  public:
73  explicit OsqpInterface(const std::string& name,
74  const std::map<std::string, Sparsity>& st);
75 
77  static Conic* creator(const std::string& name,
78  const std::map<std::string, Sparsity>& st) {
79  return new OsqpInterface(name, st);
80  }
81 
83  ~OsqpInterface() override;
84 
85  // Get name of the plugin
86  const char* plugin_name() const override { return "osqp";}
87 
88  // Check if dependencies of the plugin have the correct version
89  void deps_version_check(const std::string& stage) const override;
90 
91  // Get name of the class
92  std::string class_name() const override { return "OsqpInterface";}
93 
95 
96  static const Options options_;
97  const Options& get_options() const override { return options_;}
99 
101  void init(const Dict& opts) override;
102 
104  void* alloc_mem() const override { return new OsqpMemory();}
105 
107  int init_mem(void* mem) const override;
108 
110  void free_mem(void *mem) const override { delete static_cast<OsqpMemory*>(mem);}
111 
113  int solve(const double** arg, double** res,
114  casadi_int* iw, double* w, void* mem) const override;
115 
117  bool integer_support() const override { return true;}
118 
120  bool psd_support() const override { return true;}
121 
123  static const std::string meta_doc;
124 
126  Dict get_stats(void* mem) const override;
127 
128  // Number of nonzeros in upper part of Hessian
129  casadi_int nnzHupp_, nnzA_;
130 
131  OSQPSettings settings_;
132 
133  // OSQP override settings.rho
134  double rho_initial_;
135 
136  // If there is a version mismatch in osqp, default settings will spill over
137  // into this variable. We use this to detect such a situation.
138  double overrun_check_[10000];
139 
140  bool warm_start_primal_, warm_start_dual_;
141 
143  void codegen_body(CodeGenerator& g) const override;
144 
146  void codegen_init_mem(CodeGenerator& g) const override;
147 
149  void codegen_free_mem(CodeGenerator& g) const override;
150 
152 #ifdef WITH_OSQP_V1
153  std::string codegen_mem_type() const override { return "OSQPSolver*"; }
154 #else
155  std::string codegen_mem_type() const override { return "OSQPWorkspace*"; }
156 #endif
158  bool codegen_needs_mem() const override { return true; }
159 
160  void serialize_body(SerializingStream &s) const override;
161 
163  static ProtoFunction* deserialize(DeserializingStream& s) { return new OsqpInterface(s); }
164 
165  protected:
167  explicit OsqpInterface(DeserializingStream& e);
168  };
169 
170 } // namespace casadi
171 
173 #endif // CASADI_OSQP_INTERFACE_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.