25 #include "daqp_interface.hpp"
26 #include "casadi/core/nlp_tools.hpp"
28 #include <daqp_runtime_str.h>
32 int CASADI_CONIC_DAQP_EXPORT
35 plugin->name =
"daqp";
37 plugin->version = CASADI_VERSION;
50 const std::map<std::string, Sparsity>& st)
60 "Options to be passed to Daqp."
70 for (
auto&& op : opts) {
71 if (op.first==
"daqp") {
79 integrality_.resize(
nx_);
98 std::string n = name +
"[]";
99 g.
local(n,
"static const int");
100 std::stringstream init;
102 for (casadi_int i=0;i<v.size();++i) {
104 if (i<v.size()-1) init <<
", ";
107 if (v.empty()) init <<
"0";
114 g <<
"p.qp = &p_qp;\n";
115 g <<
"daqp_default_settings(&p.settings);\n";
120 g <<
"p.integrality = 0;\n";
122 g <<
"p.integrality = integrality;\n";
125 for (
auto&& op :
opts_) {
126 if (op.first==
"primal_tol") {
127 g <<
"p.settings.primal_tol = " << g.
constant(op.second.to_double()) <<
";\n";
128 }
else if (op.first==
"dual_tol") {
129 g <<
"p.settings.dual_tol = " << g.
constant(op.second.to_double()) <<
";\n";
130 }
else if (op.first==
"zero_tol") {
131 g <<
"p.settings.zero_tol = " << g.
constant(op.second.to_double()) <<
";\n";
132 }
else if (op.first==
"pivot_tol") {
133 g <<
"p.settings.pivot_tol = " << g.
constant(op.second.to_double()) <<
";\n";
134 }
else if (op.first==
"progress_tol") {
135 g <<
"p.settings.progress_tol = " << g.
constant(op.second.to_double()) <<
";\n";
136 }
else if (op.first==
"cycle_tol") {
137 g <<
"p.settings.cycle_tol = " << op.second.to_int() <<
";\n";
138 }
else if (op.first==
"iter_limit") {
139 g <<
"p.settings.iter_limit = " << op.second.to_int() <<
";\n";
140 }
else if (op.first==
"fval_bound") {
141 g <<
"p.settings.fval_bound = " << g.
constant(op.second.to_double()) <<
";\n";
142 }
else if (op.first==
"eps_prox") {
143 g <<
"p.settings.eps_prox = " << g.
constant(op.second.to_double()) <<
";\n";
144 }
else if (op.first==
"eta_prox") {
145 g <<
"p.settings.eta_prox = " << g.
constant(op.second.to_double()) <<
";\n";
146 }
else if (op.first==
"rho_soft") {
147 g <<
"p.settings.rho_soft = " << g.
constant(op.second.to_double()) <<
";\n";
148 }
else if (op.first==
"rel_subopt") {
149 g <<
"p.settings.rel_subopt = " << g.
constant(op.second.to_double()) <<
";\n";
150 }
else if (op.first==
"abs_subopt") {
151 g <<
"p.settings.abs_subopt = " << g.
constant(op.second.to_double()) <<
";\n";
153 casadi_error(
"Unknown option '" + op.first +
"'.");
157 g <<
"casadi_daqp_setup(&p);\n";
161 g <<
"casadi_daqp_init_mem(&" +
codegen_mem(g) +
");\n";
166 g <<
"casadi_daqp_free_mem(&" +
codegen_mem(g) +
");\n";
172 DAQPSettings* settings = &p_.
settings;
174 daqp_default_settings(settings);
176 for (
auto&& op :
opts_) {
177 if (op.first==
"primal_tol") {
178 settings->primal_tol = op.second.to_double();
179 }
else if (op.first==
"dual_tol") {
180 settings->dual_tol = op.second.to_double();
181 }
else if (op.first==
"zero_tol") {
182 settings->zero_tol = op.second.to_double();
183 }
else if (op.first==
"pivot_tol") {
184 settings->pivot_tol = op.second.to_double();
185 }
else if (op.first==
"progress_tol") {
186 settings->progress_tol = op.second.to_double();
187 }
else if (op.first==
"cycle_tol") {
188 settings->cycle_tol = op.second.to_int();
189 }
else if (op.first==
"iter_limit") {
190 settings->iter_limit = op.second.to_int();
191 }
else if (op.first==
"fval_bound") {
192 settings->fval_bound = op.second.to_double();
193 }
else if (op.first==
"eps_prox") {
194 settings->eps_prox = op.second.to_double();
195 }
else if (op.first==
"eta_prox") {
196 settings->eta_prox = op.second.to_double();
197 }
else if (op.first==
"rho_soft") {
198 settings->rho_soft = op.second.to_double();
199 }
else if (op.first==
"rel_subopt") {
200 settings->rel_subopt = op.second.to_double();
201 }
else if (op.first==
"abs_subopt") {
202 settings->abs_subopt = op.second.to_double();
204 casadi_error(
"Unknown option '" + op.first +
"'.");
209 casadi_daqp_setup(&p_);
216 casadi_daqp_init_mem(&m->d);
218 m->add_stat(
"preprocessing");
219 m->add_stat(
"solver");
220 m->add_stat(
"postprocessing");
227 casadi_daqp_free_mem(&m->d);
233 casadi_int*& iw,
double*& w)
const {
242 casadi_daqp_set_work(&m->d, &arg, &res, &iw, &w);
247 solve(
const double** arg,
double** res, casadi_int* iw,
double* w,
void* mem)
const {
251 m->
fstats.at(
"solver").tic();
253 casadi_daqp_solve(&m->d, arg, res, iw, w);
254 m->fstats.at(
"solver").toc();
273 g.
local(
"d",
"struct casadi_daqp_data*");
275 g.
local(
"p",
"struct casadi_daqp_prob");
279 g <<
"d->prob = &p;\n";
280 g <<
"d->qp = &d_qp;\n";
281 g <<
"casadi_daqp_set_work(d, &arg, &res, &iw, &w);\n";
283 g <<
"casadi_daqp_solve(d, arg, res, iw, w);\n";
285 g <<
"if (!d_qp.success) {\n";
287 g <<
"return -1000;\n";
299 stats[
"bnb_nodecount"] = m->d.nodecount;
300 stats[
"bnb_itercount"] = m->d.bnb_itercount;
Helper class for C code generation.
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
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.
static const Options options_
Options.
casadi_int nx_
Number of decision variables.
int init_mem(void *mem) const override
Initalize memory block.
void init(const Dict &opts) override
Initialize.
std::vector< bool > discrete_
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.
Dict get_stats(void *mem) const override
Get all statistics.
casadi_qp_prob< double > p_qp_
void qp_codegen_body(CodeGenerator &g) const
Generate code for the function body.
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)
bool has_refcount_
Reference counting in codegen?
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.
void copy_vector(const std::vector< S > &s, std::vector< D > &d)
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
void CASADI_CONIC_DAQP_EXPORT casadi_load_conic_daqp()
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
void codegen_local(CodeGenerator &g, const std::string &name, const std::vector< int > &v)
int CASADI_CONIC_DAQP_EXPORT casadi_register_conic_daqp(Conic::Plugin *plugin)
casadi_daqp_data< double > d
Options metadata for a class.
std::map< std::string, FStats > fstats
const casadi_qp_prob< T1 > * qp