piqp_interface.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2014 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * K.U. 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_PIQP_INTERFACE_HPP
27 #define CASADI_PIQP_INTERFACE_HPP
28 
29 #include "casadi/core/conic_impl.hpp"
30 #include <casadi/interfaces/piqp/casadi_conic_piqp_export.h>
31 
32 #include <Eigen/Dense>
33 #include <Eigen/Sparse>
34 
35 // PIQP header
36 #include <piqp/piqp.hpp> // NOLINT(build/include)
37 
47 namespace casadi {
48 
49  struct PiqpMemory : public ConicMemory {
50  typedef Eigen::Triplet<double> TripletT;
51 
52  // Working structures:
53  Eigen::VectorXd g_vector; // linear term
54  Eigen::VectorXd eq_b_vector; // equality constraints
55 
56  Eigen::VectorXd uba_vector; // upper bound inequality constraints for Ax
57  Eigen::VectorXd lba_vector; // lower bound inequality constraints for Ax
58  Eigen::VectorXd ubx_vector; // upper bound inequality constraints on variable x
59  Eigen::VectorXd lbx_vector; // upper bound inequality constraints on variable x
60 
61  // For conversion from casadi::Sparsity to Eigen::SparseMatrix
62  std::vector<TripletT> tripletList; // Used for H and C
63  std::vector<TripletT> tripletListEq; // Used for A
64  std::vector<casadi_int> row, col;
65 
66  // Results
67  std::unique_ptr<Eigen::VectorXd> results_x;
68  std::unique_ptr<Eigen::VectorXd> results_lam_x;
69  std::unique_ptr<Eigen::VectorXd> results_y;
70  std::unique_ptr<Eigen::VectorXd> results_z;
71  double objValue;
72  piqp::Status status;
73 
75  PiqpMemory();
76 
78  ~PiqpMemory();
79  };
80 
87  class PiqpInterface : public Conic {
88  public:
90  explicit PiqpInterface(const std::string& name,
91  const std::map<std::string, Sparsity>& st);
92 
94  static Conic* creator(const std::string& name,
95  const std::map<std::string, Sparsity>& st) {
96  return new PiqpInterface(name, st);
97  }
98 
100  ~PiqpInterface() override;
101 
102  // Get name of the plugin
103  const char* plugin_name() const override { return "piqp";}
104 
105  // Get name of the class
106  std::string class_name() const override { return "PiqpInterface";}
107 
109 
110  static const Options options_;
111  const Options& get_options() const override { return options_;}
113 
115  void init(const Dict& opts) override;
116 
118  void finalize() override;
119 
121  void* alloc_mem() const override { return new PiqpMemory();}
122 
124  int init_mem(void* mem) const override;
125 
127  void free_mem(void *mem) const override { delete static_cast<PiqpMemory*>(mem);}
128 
130  int solve(const double** arg, double** res,
131  casadi_int* iw, double* w, void* mem) const override;
132 
134  bool integer_support() const override { return false;}
135 
137  bool psd_support() const override { return false;}
138 
140  static const std::string meta_doc;
141 
143  Dict get_stats(void* mem) const override;
144 
145  // Number of nonzeros in upper part of Hessian
146  casadi_int nnzH_, nnzA_;
147 
148  piqp::Settings<double> settings_;
149 
150  // Derived in finalize() from settings_.kkt_solver: dense_cholesky -> dense
151  bool sparse_backend_;
152 
153  void serialize_body(SerializingStream &s) const override;
154 
156  static ProtoFunction* deserialize(DeserializingStream& s) { return new PiqpInterface(s); }
157 
158  protected:
160  explicit PiqpInterface(DeserializingStream& e);
161  };
162 
163 } // namespace casadi
164 
166 #endif // CASADI_PIQP_INTERFACE_HPP
The casadi namespace.
Definition: archiver.hpp:32
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.