27 #include "casadi/core/nlpsol.hpp"
32 int CASADI_CONIC_QRQP_EXPORT
35 plugin->name =
"qrqp";
37 plugin->version = CASADI_VERSION;
48 Qrqp::Qrqp(
const std::string& name,
const std::map<std::string, Sparsity> &st)
60 "Maximum number of iterations [1000]."}},
63 "Constraint violation tolerance [1e-8]."}},
66 "Dual feasibility violation tolerance [1e-8]"}},
69 "Print header [true]."}},
72 "Print iterations [true]."}},
75 "Print info [true]."}},
78 "Print dependant linear combinations of constraints [false]. "
79 "Printed numbers are 0-based indices into the vector of [simple bounds;linear bounds]"}},
82 "Smallest multiplier treated as inactive for the initial active set [0]."}}
109 for (
auto&& op : opts) {
110 if (op.first==
"max_iter") {
112 }
else if (op.first==
"constr_viol_tol") {
114 }
else if (op.first==
"dual_inf_tol") {
116 }
else if (op.first==
"min_lam") {
118 }
else if (op.first==
"print_iter") {
120 }
else if (op.first==
"print_header") {
122 }
else if (op.first==
"print_info") {
124 }
else if (op.first==
"print_lincomb") {
139 print(
"-------------------------------------------\n");
140 print(
"This is casadi::QRQP\n");
141 print(
"Number of variables: %9d\n",
nx_);
142 print(
"Number of constraints: %9d\n",
na_);
143 print(
"Number of nonzeros in H: %9d\n",
H_.
nnz());
144 print(
"Number of nonzeros in A: %9d\n",
A_.
nnz());
152 casadi_int*& iw,
double*& w)
const {
162 casadi_qrqp_init(&m->d, &iw, &w);
165 void Qrqp::set_qrqp_prob() {
173 casadi_qrqp_setup(&
p_);
184 solve(
const double** arg,
double** res, casadi_int* iw,
double* w,
void* mem)
const {
204 if (casadi_qrqp_reset(&d))
return 1;
207 int flag = casadi_qrqp_prepare(&d);
210 if (d.
iter % 10 == 0) {
212 if (casadi_qrqp_print_header(&d, buf,
sizeof(buf)))
break;
213 uout() << buf <<
"\n";
216 if (casadi_qrqp_print_iteration(&d, buf,
sizeof(buf)))
break;
217 uout() << buf <<
"\n";
220 flag = flag || casadi_qrqp_iterate(&d);
223 for (casadi_int k=0;k<d.
sing;++k) {
224 uout() <<
"lincomb: ";
225 casadi_qrqp_print_colcomb(&d, buf,
sizeof(buf), k);
226 uout() << buf <<
"\n";
237 m->return_status =
"success";
240 m->return_status =
"Maximum number of iterations reached";
243 case QP_NO_SEARCH_DIR:
244 m->return_status =
"Failed to calculate search direction";
247 case QP_PRINTING_ERROR:
248 m->return_status =
"Printing error";
257 if (
verbose_) casadi_warning(m->return_status);
258 m->d_qp.success = d.
status == QP_SUCCESS;
266 g.
local(
"d",
"struct casadi_qrqp_data");
267 g.
local(
"p",
"struct casadi_qrqp_prob");
268 g.
local(
"flag",
"int");
273 g <<
"p.qp = &p_qp;\n";
280 g <<
"casadi_qrqp_setup(&p);\n";
284 g <<
"p.min_lam = " <<
p_.
min_lam <<
";\n";
289 g <<
"d.prob = &p;\n";
290 g <<
"d.qp = &d_qp;\n";
291 g <<
"casadi_qrqp_init(&d, &iw, &w);\n";
299 g.
comment(
"Pass initial guess");
306 g <<
"if (casadi_qrqp_reset(&d)) return 1;\n";
307 g <<
"while (1) {\n";
308 g <<
"flag = casadi_qrqp_prepare(&d);\n";
311 g <<
"if (d.iter % 10 == 0) {\n";
312 g <<
"if (casadi_qrqp_print_header(&d, buf, sizeof(buf))) break;\n";
313 g << g.
printf(
"%s\\n",
"buf") <<
"\n";
316 g <<
"if (casadi_qrqp_print_iteration(&d, buf, sizeof(buf))) break;\n";
317 g << g.
printf(
"%s\\n",
"buf") <<
"\n";
319 g <<
"if (flag || casadi_qrqp_iterate(&d)) break;\n";
321 g <<
"for (k=0;k<d.sing;++k) {\n";
322 g <<
"casadi_qrqp_print_colcomb(&d, buf, sizeof(buf), k);\n";
323 g << g.
printf(
"lincomb: %s\\n",
"buf") <<
"\n";
334 g <<
"if (d.status == QP_SUCCESS) {;\n";
336 g <<
"} else if (d.status == QP_NO_SEARCH_DIR) {\n";
340 g <<
"return -1000;\n";
Helper class for C code generation.
std::string fill(const std::string &res, std::size_t n, const std::string &v)
Create a fill operation.
std::string arg(casadi_int i) const
Refer to argument.
void comment(const std::string &s)
Write a comment line (ignored if not verbose)
std::string constant(const std::vector< casadi_int > &v)
Represent an array constant; adding it when new.
std::string printf(const std::string &str, const std::vector< std::string > &arg=std::vector< std::string >())
Printf.
void local(const std::string &name, const std::string &type, const std::string &ref="")
Declare a local variable.
std::string res(casadi_int i) const
Refer to resuly.
void copy_check(const std::string &arg, std::size_t n, const std::string &res, bool check_lhs=true, bool check_rhs=true)
std::string sparsity(const Sparsity &sp, bool canonical=true)
void copy_default(const std::string &arg, std::size_t n, const std::string &res, const std::string &def, bool check_rhs=true)
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.
casadi_int na_
The number of constraints (counting both equality and inequality) == A.size1()
Sparsity H_
Problem structure.
void init(const Dict &opts) override
Initialize.
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.
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.
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 check()
Raises an error if an interrupt was captured.
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 print(const char *fmt,...) const
C-style formatted printing during evaluation.
bool verbose_
Verbose printout.
void clear_mem()
Clear all memory (called from destructor)
static const std::string meta_doc
A documentation string.
Qrqp(const std::string &name, const std::map< std::string, Sparsity > &st)
Create a new Solver.
std::vector< casadi_int > pc_
static const Options options_
Options.
~Qrqp() override
Destructor.
void init(const Dict &opts) override
Initialize.
std::vector< casadi_int > prinv_
int init_mem(void *mem) const override
Initalize memory block.
static ProtoFunction * deserialize(DeserializingStream &s)
Deserialize with type disambiguation.
void codegen_body(CodeGenerator &g) const override
Generate code for the function body.
int solve(const double **arg, double **res, casadi_int *iw, double *w, void *mem) const override
Solve the QP.
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.
casadi_qrqp_prob< double > p_
Dict get_stats(void *mem) const override
Get all statistics.
static Conic * creator(const std::string &name, const std::map< std::string, Sparsity > &st)
Create a new QP Solver.
Helper class for Serialization.
void version(const std::string &name, int v)
void pack(const Sparsity &e)
Serializes an object to the output stream.
Sparsity T() const
Transpose the matrix.
casadi_int nnz() const
Get the number of (structural) non-zeros.
static Sparsity kkt(const Sparsity &H, const Sparsity &J, bool with_x_diag=true, bool with_lam_g_diag=true)
Get KKT system sparsity.
void qr_sparse(Sparsity &V, Sparsity &R, std::vector< casadi_int > &prinv, std::vector< casadi_int > &pc, bool amd=true) const
Symbolic QR factorization.
@ CONIC_UBA
dense, (nc x 1)
@ CONIC_LBA
dense, (nc x 1)
@ CONIC_UBX
dense, (n x 1)
@ CONIC_LBX
dense, (n x 1)
int CASADI_CONIC_QRQP_EXPORT casadi_register_conic_qrqp(Conic::Plugin *plugin)
void casadi_copy(const T1 *x, casadi_int n, T1 *y)
COPY: y <-x.
void casadi_fill(T1 *x, casadi_int n, T1 alpha)
FILL: x <- alpha.
std::string str(const T &v)
String representation, any type.
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
const double nan
Not a number.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
void CASADI_CONIC_QRQP_EXPORT casadi_load_conic_qrqp()
@ CONIC_X
The primal solution.
@ CONIC_LAM_A
The dual solution corresponding to linear bounds.
@ CONIC_COST
The optimal cost.
@ CONIC_LAM_X
The dual solution corresponding to simple bounds.
Options metadata for a class.
const char * return_status
casadi_qrqp_flag_t status
const casadi_qp_prob< T1 > * qp
const casadi_int * sp_kkt