qpoases_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_QPOASES_INTERFACE_HPP
27 #define CASADI_QPOASES_INTERFACE_HPP
28 
29 #include "casadi/core/conic_impl.hpp"
30 #include "casadi/core/linsol.hpp"
31 #include <casadi/interfaces/qpoases/casadi_conic_qpoases_export.h>
32 #include <qpOASES.hpp>
33 
44 namespace casadi {
45 
46  // Forward declaration
47  class QpoasesInterface;
48 
49  struct QpoasesMemory : public ConicMemory {
50  // Linear solver
51  Linsol linsol;
52 
53  // Linear solver plugin class
54  std::string linsol_plugin;
55 
57  union {
58  qpOASES::SQProblem *sqp;
59  qpOASES::QProblemB *qp;
60  };
61 
62  // Sparse QP matrices
63  qpOASES::SymSparseMat *h;
64  qpOASES::SparseMatrix *a;
65 
67  bool called_once;
68 
69  // Map linear system nonzeros
70  std::vector<casadi_int> lin_map;
71 
72  // Sparsity pattern as sparse triplet
73  std::vector<casadi_int> row, col, nz_map;
74 
75  std::vector<int> h_row, h_colind, a_row, a_colind;
76 
77  // Nonzero entries
78  std::vector<double> nz;
79 
80  int return_status;
81 
83  QpoasesMemory();
84 
86  ~QpoasesMemory();
87  };
88 
98  class QpoasesInterface : public Conic {
99  public:
101  explicit QpoasesInterface();
102 
104  static Conic* creator(const std::string& name,
105  const std::map<std::string, Sparsity>& st) {
106  return new QpoasesInterface(name, st);
107  }
108 
110  explicit QpoasesInterface(const std::string& name,
111  const std::map<std::string, Sparsity>& st);
112 
114  ~QpoasesInterface() override;
115 
116  // Get name of the plugin
117  const char* plugin_name() const override { return "qpoases";}
118 
119  // Get name of the class
120  std::string class_name() const override { return "QpoasesInterface";}
121 
123 
124  static const Options options_;
125  const Options& get_options() const override { return options_;}
127 
129  void init(const Dict& opts) override;
130 
132  void* alloc_mem() const override { return new QpoasesMemory();}
133 
135  int init_mem(void* mem) const override;
136 
138  void free_mem(void *mem) const override { delete static_cast<QpoasesMemory*>(mem);}
139 
141  int solve(const double** arg, double** res,
142  casadi_int* iw, double* w, void* mem) const override;
143 
145  static const std::string meta_doc;
146 
148  static int qpoases_init(void* mem, int dim, int nnz, const int* row, const int* col);
149 
151  static int qpoases_sfact(void* mem, const double* vals);
152 
154  static int qpoases_nfact(void* mem, const double* vals, int* neig, int* rank);
155 
157  static int qpoases_solve(void* mem, int nrhs, double* rhs);
158 
160  static void qpoases_printf(const char* s);
161 
163  Dict get_stats(void* mem) const override;
164 
165 
167  void serialize_body(SerializingStream &s) const override;
168 
170  static ProtoFunction* deserialize(DeserializingStream& s) { return new QpoasesInterface(s); }
171 
172  protected:
174  explicit QpoasesInterface(DeserializingStream& s);
175 
178  static bool from_BooleanType(qpOASES::BooleanType b);
179  static qpOASES::BooleanType to_BooleanType(bool b);
180  static std::string from_SubjectToStatus(qpOASES::SubjectToStatus b);
181  static qpOASES::SubjectToStatus to_SubjectToStatus(std::string b);
182  static std::string from_PrintLevel(qpOASES::PrintLevel b);
183  static qpOASES::PrintLevel to_PrintLevel(std::string b);
185 
188  casadi_int max_nWSR_;
189  double max_cputime_;
190  qpOASES::Options ops_;
191  qpOASES::HessianType hess_;
192  bool sparse_;
193  bool schur_;
194  casadi_int max_schur_;
195  std::string linsol_plugin_;
197 
199  static void qpoases_error(const std::string& module, casadi_int flag);
200 
202  static std::string getErrorMessage(casadi_int flag);
203  };
204 
205 } // namespace casadi
206 
208 #endif // CASADI_QPOASES_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.