proxqp_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_PROXQP_INTERFACE_HPP
27 #define CASADI_PROXQP_INTERFACE_HPP
28 
29 #include "casadi/core/conic_impl.hpp"
30 #include <casadi/interfaces/proxqp/casadi_conic_proxqp_export.h>
31 
32 // Proxqp header
33 #include <proxsuite/proxqp/dense/dense.hpp> // NOLINT(build/include)
34 #include <proxsuite/proxqp/sparse/sparse.hpp> // NOLINT(build/include)
35 #include <proxsuite/proxqp/settings.hpp> // NOLINT(build/include)
36 
37 #include <Eigen/Dense>
38 #include <Eigen/Sparse>
39 
40 
50 namespace casadi {
51 
52  struct ProxqpMemory : public ConicMemory {
53 
54  typedef Eigen::Triplet<double> T;
55  // Solvers
56  proxsuite::proxqp::sparse::QP<double, long long> sparse_solver;
57  proxsuite::proxqp::dense::QP<double> dense_solver;
58 
59  // Working structures:
60  Eigen::VectorXd g_vector; // linear term
61  Eigen::VectorXd b_vector; // equality constraints
62 
63  Eigen::VectorXd uba_vector; // upper bound inequality constraints for Ax
64  Eigen::VectorXd lba_vector; // lower bound inequality constraints for Ax
65  Eigen::VectorXd ubx_vector; // upper bound inequality constraints on variable x
66  Eigen::VectorXd lbx_vector; // upper bound inequality constraints on variable x
67  Eigen::VectorXd ub_vector; // upper bound stacked (Ax, x) input for proxqp
68  Eigen::VectorXd lb_vector; // lower bound stacked (Ax, x) input for proxqp
69 
70  // For conversion from casadi::Sparsity to Eigen::SparseMatrix
71  std::vector<T> tripletList; // Used for H and C
72  std::vector<T> tripletListEq; // Used for A
73  std::vector<casadi_int> row, col;
74 
75  // Results
76  std::unique_ptr<Eigen::VectorXd> results_x;
77  std::unique_ptr<Eigen::VectorXd> results_y;
78  std::unique_ptr<Eigen::VectorXd> results_z;
79  double objValue;
80  proxsuite::proxqp::QPSolverOutput status;
81 
83  ProxqpMemory();
84 
86  ~ProxqpMemory();
87  };
88 
95  class ProxqpInterface : public Conic {
96  public:
98  explicit ProxqpInterface(const std::string& name,
99  const std::map<std::string, Sparsity>& st);
100 
102  static Conic* creator(const std::string& name,
103  const std::map<std::string, Sparsity>& st) {
104  return new ProxqpInterface(name, st);
105  }
106 
108  ~ProxqpInterface() override;
109 
110  // Get name of the plugin
111  const char* plugin_name() const override { return "proxqp";}
112 
113  // Get name of the class
114  std::string class_name() const override { return "ProxqpInterface";}
115 
117 
118  static const Options options_;
119  const Options& get_options() const override { return options_;}
121 
123  void init(const Dict& opts) override;
124 
126  void* alloc_mem() const override { return new ProxqpMemory();}
127 
129  int init_mem(void* mem) const override;
130 
132  void free_mem(void *mem) const override { delete static_cast<ProxqpMemory*>(mem);}
133 
135  int solve(const double** arg, double** res,
136  casadi_int* iw, double* w, void* mem) const override;
137 
139  bool integer_support() const override { return false;}
140 
142  bool psd_support() const override { return false;}
143 
145  static const std::string meta_doc;
146 
148  Dict get_stats(void* mem) const override;
149 
150  proxsuite::proxqp::Settings<double> settings_;
151 
152  bool warm_start_primal_, warm_start_dual_;
153  bool sparse_backend;
154  double max_iter;
155 
156  // Number of nonzeros in Hessian
157  casadi_int nH_;
158 
159  // Number of nonzeros in constraint matrix
160  casadi_int nA_;
161 
162  void serialize_body(SerializingStream &s) const override;
163 
165  static ProtoFunction* deserialize(DeserializingStream& s) { return new ProxqpInterface(s); }
166 
167  protected:
169  explicit ProxqpInterface(DeserializingStream& e);
170  };
171 
172 } // namespace casadi
173 
175 #endif // CASADI_PROXQP_INTERFACE_HPP
The casadi namespace.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.