daqp_interface.cpp
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 #include "daqp_interface.hpp"
26 #include "casadi/core/nlp_tools.hpp"
27 
28 #include <daqp_runtime_str.h>
29 namespace casadi {
30 
31  extern "C"
32  int CASADI_CONIC_DAQP_EXPORT
33  casadi_register_conic_daqp(Conic::Plugin* plugin) {
34  plugin->creator = DaqpInterface::creator;
35  plugin->name = "daqp";
36  plugin->doc = DaqpInterface::meta_doc.c_str();
37  plugin->version = CASADI_VERSION;
38  plugin->options = &DaqpInterface::options_;
39  plugin->deserialize = &DaqpInterface::deserialize;
40  return 0;
41  }
42 
43  extern "C"
44  void CASADI_CONIC_DAQP_EXPORT casadi_load_conic_daqp() {
46  }
47 
48 
49  DaqpInterface::DaqpInterface(const std::string& name,
50  const std::map<std::string, Sparsity>& st)
51  : Conic(name, st) {
52  }
53 
55  = {{&Conic::options_},
56  {{"daqp",
57  {OT_DICT,
58  "Options to be passed to Daqp."
59  }},
60  }
61  };
62 
63  void DaqpInterface::init(const Dict& opts) {
64  // Call the init method of the base class
65  Conic::init(opts);
66 
67  // Read user options
68  for (auto&& op : opts) {
69  if (op.first=="daqp") {
70  opts_ = op.second;
71  }
72  }
73 
74  // Initialize read-only members of class that don't require saving
75  // since they can be derived from other read-only members
76  set_daqp_prob();
77 
78  // Allocate memory
79  casadi_int sz_arg, sz_res, sz_w, sz_iw;
80  casadi_daqp_work(&p_, &sz_arg, &sz_res, &sz_iw, &sz_w);
81 
82  alloc_arg(sz_arg, true);
83  alloc_res(sz_res, true);
84  alloc_iw(sz_iw, true);
85  alloc_w(sz_w, true);
86  }
87 
89  g << "p.qp = &p_qp;\n";
90  g << "daqp_default_settings(&p.settings);\n";
91  for (auto&& op : opts_) {
92  if (op.first=="primal_tol") {
93  g << "p.settings.primal_tol = " << op.second.to_double() << ";\n";
94  } else if (op.first=="dual_tol") {
95  g << "p.settings.dual_tol = " << op.second.to_double() << ";\n";
96  } else if (op.first=="zero_tol") {
97  g << "p.settings.zero_tol = " << op.second.to_double() << ";\n";
98  } else if (op.first=="pivot_tol") {
99  g << "p.settings.pivot_tol = " << op.second.to_double() << ";\n";
100  } else if (op.first=="progress_tol") {
101  g << "p.settings.progress_tol = " << op.second.to_double() << ";\n";
102  } else if (op.first=="cycle_tol") {
103  g << "p.settings.cycle_tol = " << op.second.to_int() << ";\n";
104  } else if (op.first=="iter_limit") {
105  g << "p.settings.iter_limit = " << op.second.to_int() << ";\n";
106  } else if (op.first=="fval_bound") {
107  g << "p.settings.fval_bound = " << op.second.to_double() << ";\n";
108  } else if (op.first=="eps_prox") {
109  g << "p.settings.eps_prox = " << op.second.to_double() << ";\n";
110  } else if (op.first=="eta_prox") {
111  g << "p.settings.eta_prox = " << op.second.to_double() << ";\n";
112  } else if (op.first=="rho_soft") {
113  g << "p.settings.rho_soft = " << op.second.to_double() << ";\n";
114  } else if (op.first=="rel_subopt") {
115  g << "p.settings.rel_subopt = " << op.second.to_double() << ";\n";
116  } else if (op.first=="abs_subopt") {
117  g << "p.settings.abs_subopt = " << op.second.to_double() << ";\n";
118  } else {
119  casadi_error("Unknown option '" + op.first + "'.");
120  }
121  }
122 
123  g << "casadi_daqp_setup(&p);\n";
124  }
125 
127  g << "daqp_init_mem(&" + codegen_mem(g) + ");\n";
128  g << "return 0;\n";
129  }
130 
132  g << "daqp_free_mem(&" + codegen_mem(g) + ");\n";
133  }
134 
136  p_.qp = &p_qp_;
137 
138  DAQPSettings* settings = &p_.settings;
139 
140  daqp_default_settings(settings);
141 
142  for (auto&& op : opts_) {
143  if (op.first=="primal_tol") {
144  settings->primal_tol = op.second.to_double();
145  } else if (op.first=="dual_tol") {
146  settings->dual_tol = op.second.to_double();
147  } else if (op.first=="zero_tol") {
148  settings->zero_tol = op.second.to_double();
149  } else if (op.first=="pivot_tol") {
150  settings->pivot_tol = op.second.to_double();
151  } else if (op.first=="progress_tol") {
152  settings->progress_tol = op.second.to_double();
153  } else if (op.first=="cycle_tol") {
154  settings->cycle_tol = op.second.to_int();
155  } else if (op.first=="iter_limit") {
156  settings->iter_limit = op.second.to_int();
157  } else if (op.first=="fval_bound") {
158  settings->fval_bound = op.second.to_double();
159  } else if (op.first=="eps_prox") {
160  settings->eps_prox = op.second.to_double();
161  } else if (op.first=="eta_prox") {
162  settings->eta_prox = op.second.to_double();
163  } else if (op.first=="rho_soft") {
164  settings->rho_soft = op.second.to_double();
165  } else if (op.first=="rel_subopt") {
166  settings->rel_subopt = op.second.to_double();
167  } else if (op.first=="abs_subopt") {
168  settings->abs_subopt = op.second.to_double();
169  } else {
170  casadi_error("Unknown option '" + op.first + "'.");
171  }
172  }
173 
174  casadi_daqp_setup(&p_);
175  }
176 
177  int DaqpInterface::init_mem(void* mem) const {
178  if (Conic::init_mem(mem)) return 1;
179  if (!mem) return 1;
180  auto m = static_cast<DaqpMemory*>(mem);
181  daqp_init_mem(&m->d);
182 
183  m->add_stat("preprocessing");
184  m->add_stat("solver");
185  m->add_stat("postprocessing");
186 
187  return 0;
188  }
189 
190  void DaqpInterface::free_mem(void* mem) const {
191  auto m = static_cast<DaqpMemory*>(mem);
192  daqp_free_mem(&m->d);
193  delete static_cast<DaqpMemory*>(mem);
194  }
195 
197  void DaqpInterface::set_work(void* mem, const double**& arg, double**& res,
198  casadi_int*& iw, double*& w) const {
199 
200  auto m = static_cast<DaqpMemory*>(mem);
201 
202  Conic::set_work(mem, arg, res, iw, w);
203 
204  m->d.prob = &p_;
205  m->d.qp = &m->d_qp;
206 
207  casadi_daqp_init(&m->d, &arg, &res, &iw, &w);
208 
209  }
210 
212  solve(const double** arg, double** res, casadi_int* iw, double* w, void* mem) const {
213  auto m = static_cast<DaqpMemory*>(mem);
214 
215  // Statistics
216  m->fstats.at("solver").tic();
217 
218  casadi_daqp_solve(&m->d, arg, res, iw, w);
219  m->fstats.at("solver").toc();
220 
221  return 0;
222  }
223 
225  clear_mem();
226  }
227 
229  qp_codegen_body(g);
232  g.add_include("daqp/api.h");
233  g.add_include("stdio.h");
234 
235  g.auxiliaries << g.sanitize_source(daqp_runtime_str, {"casadi_real"});
236 
237  g.local("d", "struct casadi_daqp_data*");
238  g.init_local("d", "&" + codegen_mem(g));
239  g.local("p", "struct casadi_daqp_prob");
240  set_daqp_prob(g);
241 
242  // Setup data structure (corresponds to set_work)
243  g << "d->prob = &p;\n";
244  g << "d->qp = &d_qp;\n";
245  g << "casadi_daqp_init(d, &arg, &res, &iw, &w);\n";
246 
247  g << "casadi_daqp_solve(d, arg, res, iw, w);\n";
248 
249  g << "if (!d_qp.success) {\n";
250  if (error_on_fail_) {
251  g << "return -1000;\n";
252  } else {
253  g << "return -1;\n";
254  }
255  g << "}\n";
256  g << "return 0;\n";
257  }
258 
259  Dict DaqpInterface::get_stats(void* mem) const {
260  Dict stats = Conic::get_stats(mem);
261  auto m = static_cast<DaqpMemory*>(mem);
262  stats["return_status"] = m->d.return_status;
263  return stats;
264  }
265 
267  s.version("DaqpInterface", 1);
268  s.unpack("DaqpInterface::opts", opts_);
269  set_daqp_prob();
270  }
271 
274 
275  s.version("DaqpInterface", 1);
276  s.pack("DaqpInterface::opts", opts_);
277  }
278 
279 } // end namespace casadi
Helper class for C code generation.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
void init_local(const std::string &name, const std::string &def)
Specify the default value for a local variable.
std::string sanitize_source(const std::string &src, const std::vector< std::string > &inst, bool add_shorthand=true)
Sanitize source files for codegen.
void add_include(const std::string &new_include, bool relative_path=false, const std::string &use_ifdef=std::string())
Add an include file optionally using a relative path "..." instead of an absolute path <....
std::stringstream auxiliaries
void add_auxiliary(Auxiliary f, const std::vector< std::string > &inst={"casadi_real"})
Add a built-in auxiliary function.
Internal class.
Definition: conic_impl.hpp:44
static const Options options_
Options.
Definition: conic_impl.hpp:83
int init_mem(void *mem) const override
Initalize memory block.
Definition: conic.cpp:451
void init(const Dict &opts) override
Initialize.
Definition: conic.cpp:412
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
Definition: conic.cpp:738
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
Definition: conic.cpp:458
Dict get_stats(void *mem) const override
Get all statistics.
Definition: conic.cpp:711
casadi_qp_prob< double > p_qp_
Definition: conic_impl.hpp:47
void qp_codegen_body(CodeGenerator &g) const
Generate code for the function body.
Definition: conic.cpp:789
static Conic * creator(const std::string &name, const std::map< std::string, Sparsity > &st)
Create a new QP Solver.
void codegen_body(CodeGenerator &g) const override
Generate code for the function body.
void init(const Dict &opts) override
Initialize.
Dict get_stats(void *mem) const override
Get all statistics.
void codegen_init_mem(CodeGenerator &g) const override
Codegen decref for init_mem.
Dict opts_
All Daqp options.
void serialize_body(SerializingStream &s) const override
Serialize an object without type information.
void set_work(void *mem, const double **&arg, double **&res, casadi_int *&iw, double *&w) const override
Set the (persistent) work vectors.
static const Options options_
Options.
~DaqpInterface() override
Destructor.
static const std::string meta_doc
A documentation string.
void codegen_free_mem(CodeGenerator &g) const override
Codegen for free_mem.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
void free_mem(void *mem) const override
Free memory block.
int solve(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Solve the QP.
DaqpInterface(const std::string &name, const std::map< std::string, Sparsity > &st)
Constructor using sparsity patterns.
int init_mem(void *mem) const override
Initalize memory block.
Helper class for Serialization.
void unpack(Sparsity &e)
Reconstruct an object from the input stream.
void version(const std::string &name, int v)
void alloc_iw(size_t sz_iw, bool persistent=false)
Ensure required length of iw field.
void alloc_res(size_t sz_res, bool persistent=false)
Ensure required length of res field.
void alloc_arg(size_t sz_arg, bool persistent=false)
Ensure required length of arg field.
std::string codegen_mem(CodeGenerator &g, const std::string &index="mem") const
Get thread-local memory object.
size_t sz_res() const
Get required length of res field.
size_t sz_w() const
Get required length of w field.
void alloc_w(size_t sz_w, bool persistent=false)
Ensure required length of w field.
size_t sz_arg() const
Get required length of arg field.
size_t sz_iw() const
Get required length of iw field.
static void registerPlugin(const Plugin &plugin, bool needs_lock=true)
Register an integrator in the factory.
bool error_on_fail_
Throw an exception on failure?
void clear_mem()
Clear all memory (called from destructor)
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
The casadi namespace.
Definition: archiver.cpp:28
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void CASADI_CONIC_DAQP_EXPORT casadi_load_conic_daqp()
int CASADI_CONIC_DAQP_EXPORT casadi_register_conic_daqp(Conic::Plugin *plugin)
casadi_daqp_data< double > d
Options metadata for a class.
Definition: options.hpp:40
std::map< std::string, FStats > fstats
DAQPSettings settings
const casadi_qp_prob< T1 > * qp