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 
34 namespace qpOASES = casadi_qpOASES;
35 
46 namespace casadi {
47 
48  // Forward declaration
49  class QpoasesInterface;
50 
51  struct QpoasesMemory : public ConicMemory {
52  // Linear solver
53  Linsol linsol;
54 
55  // Linear solver plugin class
56  std::string linsol_plugin;
57 
59  union {
60  qpOASES::SQProblem *sqp;
61  qpOASES::QProblemB *qp;
62  };
63 
64  // Sparse QP matrices
65  qpOASES::SymSparseMat *h;
66  qpOASES::SparseMatrix *a;
67 
69  bool called_once;
70 
71  // Map linear system nonzeros
72  std::vector<casadi_int> lin_map;
73 
74  // Sparsity pattern as sparse triplet
75  std::vector<casadi_int> row, col, nz_map;
76 
77  std::vector<int> h_row, h_colind, a_row, a_colind;
78 
79  // Nonzero entries
80  std::vector<double> nz;
81 
82  int return_status;
83 
85  QpoasesMemory();
86 
88  ~QpoasesMemory();
89  };
90 
100  class QpoasesInterface : public Conic {
101  public:
103  explicit QpoasesInterface();
104 
106  static Conic* creator(const std::string& name,
107  const std::map<std::string, Sparsity>& st) {
108  return new QpoasesInterface(name, st);
109  }
110 
112  explicit QpoasesInterface(const std::string& name,
113  const std::map<std::string, Sparsity>& st);
114 
116  ~QpoasesInterface() override;
117 
118  // Get name of the plugin
119  const char* plugin_name() const override { return "qpoases";}
120 
121  // Get name of the class
122  std::string class_name() const override { return "QpoasesInterface";}
123 
125 
126  static const Options options_;
127  const Options& get_options() const override { return options_;}
129 
131  void init(const Dict& opts) override;
132 
134  void* alloc_mem() const override { return new QpoasesMemory();}
135 
137  int init_mem(void* mem) const override;
138 
140  void free_mem(void *mem) const override { delete static_cast<QpoasesMemory*>(mem);}
141 
143  int solve(const double** arg, double** res,
144  casadi_int* iw, double* w, void* mem) const override;
145 
147  static const std::string meta_doc;
148 
150  static int qpoases_init(void* mem, int dim, int nnz, const int* row, const int* col);
151 
153  static int qpoases_sfact(void* mem, const double* vals);
154 
156  static int qpoases_nfact(void* mem, const double* vals, int* neig, int* rank);
157 
159  static int qpoases_solve(void* mem, int nrhs, double* rhs);
160 
162  static void qpoases_printf(const char* s);
163 
165  Dict get_stats(void* mem) const override;
166 
167 
169  void serialize_body(SerializingStream &s) const override;
170 
172  static ProtoFunction* deserialize(DeserializingStream& s) { return new QpoasesInterface(s); }
173 
174  protected:
176  explicit QpoasesInterface(DeserializingStream& s);
177 
180  static bool from_BooleanType(qpOASES::BooleanType b);
181  static qpOASES::BooleanType to_BooleanType(bool b);
182  static std::string from_SubjectToStatus(qpOASES::SubjectToStatus b);
183  static qpOASES::SubjectToStatus to_SubjectToStatus(std::string b);
184  static std::string from_PrintLevel(qpOASES::PrintLevel b);
185  static qpOASES::PrintLevel to_PrintLevel(std::string b);
187 
190  casadi_int max_nWSR_;
191  double max_cputime_;
192  qpOASES::Options ops_;
193  qpOASES::HessianType hess_;
194  bool sparse_;
195  bool schur_;
196  casadi_int max_schur_;
197  std::string linsol_plugin_;
199 
201  static void qpoases_error(const std::string& module, casadi_int flag);
202 
204  static std::string getErrorMessage(casadi_int flag);
205  };
206 
207 } // namespace casadi
208 
210 #endif // CASADI_QPOASES_INTERFACE_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.