25 #include "optistack_internal.hpp"
26 #include "exception.hpp"
27 #include "global_options.hpp"
32 #define THROW_ERROR(FNAME, WHAT) \
33 throw CasadiException("Error in Opti::" FNAME " "\
34 "[" + this->class_name() + "] at " + CASADI_WHERE + ":\n"\
53 return (*this)->variable(n, m, attribute);
54 }
catch(std::exception& e) {
55 THROW_ERROR(
"variable", e.what());
61 return (*this)->variable(sp, attribute);
62 }
catch(std::exception& e) {
63 THROW_ERROR(
"variable", e.what());
69 return (*this)->variable(symbol, attribute);
70 }
catch(std::exception& e) {
71 THROW_ERROR(
"variable", e.what());
92 return (*this)->parameter(n, m, attribute);
93 }
catch(std::exception& e) {
94 THROW_ERROR(
"parameter", e.what());
100 return (*this)->parameter(sp, attribute);
101 }
catch(std::exception& e) {
102 THROW_ERROR(
"parameter", e.what());
108 return (*this)->parameter(symbol, attribute);
109 }
catch(std::exception& e) {
110 THROW_ERROR(
"parameter", e.what());
116 (*this)->minimize(
f, linear_scale);
117 }
catch(std::exception& e) {
118 THROW_ERROR(
"minimize", e.what());
124 (*this)->subject_to(
g, 1, options);
125 }
catch(std::exception& e) {
126 THROW_ERROR(
"subject_to", e.what());
131 for (
const auto& gs :
g)
subject_to(gs, 1, options);
136 (*this)->subject_to(
g, linear_scale, options);
137 }
catch(std::exception& e) {
138 THROW_ERROR(
"subject_to", e.what());
143 for (
const auto& gs :
g)
subject_to(gs, linear_scale, options);
148 (*this)->subject_to();
149 }
catch(std::exception& e) {
150 THROW_ERROR(
"subject_to", e.what());
156 const Dict& plugin_options,
157 const Dict& solver_options) {
159 (*this)->solver(
solver, plugin_options, solver_options);
160 }
catch(std::exception& e) {
161 THROW_ERROR(
"solver", e.what());
167 (*this)->set_initial(
x, v);
168 }
catch(std::exception& e) {
169 THROW_ERROR(
"set_initial", e.what());
174 (*this)->set_initial(assignments);
175 }
catch(std::exception& e) {
176 THROW_ERROR(
"set_initial", e.what());
183 (*this)->set_value(
x, v);
184 }
catch(std::exception& e) {
185 THROW_ERROR(
"set_value", e.what());
191 (*this)->set_domain(
x, domain);
192 }
catch(std::exception& e) {
193 THROW_ERROR(
"set_domain", e.what());
199 (*this)->set_linear_scale(
x, scale, offset);
200 }
catch(std::exception& e) {
201 THROW_ERROR(
"set_linear_scale", e.what());
207 (*this)->set_value(assignments);
208 }
catch(std::exception& e) {
209 THROW_ERROR(
"set_value", e.what());
215 return (*this)->solve(
false);
216 }
catch(std::exception& e) {
217 THROW_ERROR(
"solve", e.what());
223 return (*this)->solve(
true);
224 }
catch(std::exception& e) {
225 THROW_ERROR(
"solve", e.what());
231 return (*this)->value(
x, values);
232 }
catch(std::exception& e) {
233 THROW_ERROR(
"value", e.what());
240 return (*this)->value(
x, values);
241 }
catch(std::exception& e) {
242 THROW_ERROR(
"value", e.what());
248 return (*this)->value(
x, values);
249 }
catch(std::exception& e) {
250 THROW_ERROR(
"value", e.what());
256 return (*this)->scale_helper(h);
257 }
catch(std::exception& e) {
258 THROW_ERROR(
"scale_helper", e.what());
264 return (*this)->stats();
265 }
catch(std::exception& e) {
266 THROW_ERROR(
"stats", e.what());
272 return (*this)->return_status();
273 }
catch(std::exception& e) {
274 THROW_ERROR(
"return_status", e.what());
280 return (*this)->initial();
281 }
catch(std::exception& e) {
282 THROW_ERROR(
"initial", e.what());
288 return (*this)->value_variables();
289 }
catch(std::exception& e) {
290 THROW_ERROR(
"value_variables", e.what());
296 return (*this)->value_parameters();
297 }
catch(std::exception& e) {
298 THROW_ERROR(
"value_parameters", e.what());
304 return (*this)->dual(m);
305 }
catch(std::exception& e) {
306 THROW_ERROR(
"dual", e.what());
312 return (*this)->nx();
313 }
catch(std::exception& e) {
314 THROW_ERROR(
"nx", e.what());
320 return (*this)->np();
321 }
catch(std::exception& e) {
322 THROW_ERROR(
"nx", e.what());
328 return (*this)->ng();
329 }
catch(std::exception& e) {
330 THROW_ERROR(
"ng", e.what());
337 }
catch(std::exception& e) {
338 THROW_ERROR(
"x", e.what());
345 }
catch(std::exception& e) {
346 THROW_ERROR(
"p", e.what());
353 }
catch(std::exception& e) {
354 THROW_ERROR(
"g", e.what());
361 }
catch(std::exception& e) {
362 THROW_ERROR(
"f", e.what());
368 return (*this)->lbg();
369 }
catch(std::exception& e) {
370 THROW_ERROR(
"lbg", e.what());
376 return (*this)->ubg();
377 }
catch(std::exception& e) {
378 THROW_ERROR(
"ubg", e.what());
385 return (*this)->lam_g();
386 }
catch(std::exception& e) {
387 THROW_ERROR(
"lam_g", e.what());
393 return (*this)->x_linear_scale();
394 }
catch(std::exception& e) {
395 THROW_ERROR(
"x_linear_scale", e.what());
401 return (*this)->x_linear_scale_offset();
402 }
catch(std::exception& e) {
403 THROW_ERROR(
"x_linear_scale_offset", e.what());
409 return (*this)->g_linear_scale();
410 }
catch(std::exception& e) {
411 THROW_ERROR(
"g_linear_scale", e.what());
417 return (*this)->f_linear_scale();
418 }
catch(std::exception& e) {
419 THROW_ERROR(
"f_linear_scale", e.what());
424 const std::vector<MX>& args,
const std::vector<MX>& res,
425 const std::vector<std::string>& name_in,
426 const std::vector<std::string>& name_out,
429 return (*this)->to_function(name, args, res, name_in, name_out, opts);
430 }
catch(std::exception& e) {
431 THROW_ERROR(
"to_function", e.what());
436 const std::vector<MX>& args,
const std::vector<MX>& res,
442 const std::map<std::string, MX>& dict,
443 const std::vector<std::string>& name_in,
444 const std::vector<std::string>& name_out,
446 std::vector<MX> ex_in(name_in.size()), ex_out(name_out.size());
447 for (
auto&& i : dict) {
448 std::vector<std::string>::const_iterator it;
449 it=
find(name_in.begin(), name_in.end(), i.first);
450 if (it!=name_in.end()) {
452 ex_in[it-name_in.begin()] = i.second;
454 it=
find(name_out.begin(), name_out.end(), i.first);
455 if (it!=name_out.end()) {
457 ex_out[it-name_out.begin()] = i.second;
460 casadi_error(
"Unknown dictionary entry: '" + i.first +
"'");
464 return to_function(name, ex_in, ex_out, name_in, name_out, opts);
469 (*this)->callback_class(callback);
470 }
catch(std::exception& e) {
471 THROW_ERROR(
"callback_class", e.what());
477 (*this)->callback_class();
478 }
catch(std::exception& e) {
479 THROW_ERROR(
"callback_class", e.what());
485 (*this)->update_user_dict(m, meta);
486 }
catch(std::exception& e) {
487 THROW_ERROR(
"update_user_dict", e.what());
492 for (
const auto& m : ms)
498 return (*this)->user_dict(m);
499 }
catch(std::exception& e) {
500 THROW_ERROR(
"user_dict", e.what());
506 return (*this)->casadi_solver();
507 }
catch(std::exception& e) {
508 THROW_ERROR(
"casadi_solver", e.what());
514 return (*this)->is_parametric(expr);
515 }
catch(std::exception& e) {
516 THROW_ERROR(
"is_parametric", e.what());
522 return (*this)->symvar();
523 }
catch(std::exception& e) {
524 THROW_ERROR(
"symvar", e.what());
530 return (*this)->symvar(expr);
531 }
catch(std::exception& e) {
532 THROW_ERROR(
"symvar", e.what());
538 return (*this)->symvar(expr, type);
539 }
catch(std::exception& e) {
540 THROW_ERROR(
"symvar", e.what());
546 return (*this)->canon_expr(expr);
547 }
catch(std::exception& e) {
548 THROW_ERROR(
"canon_expr", e.what());
554 return (*this)->get_meta(m);
555 }
catch(std::exception& e) {
556 THROW_ERROR(
"get_meta", e.what());
562 return (*this)->get_meta_con(m);
563 }
catch(std::exception& e) {
564 THROW_ERROR(
"get_meta_con", e.what());
570 (*this)->set_meta(m, meta);
571 }
catch(std::exception& e) {
572 THROW_ERROR(
"set_meta", e.what());
578 (*this)->set_meta_con(m, meta);
579 }
catch(std::exception& e) {
580 THROW_ERROR(
"set_meta_con", e.what());
587 (*this)->assert_active_symbol(m);
588 }
catch(std::exception& e) {
589 THROW_ERROR(
"assert_active_symbol", e.what());
595 return (*this)->active_symvar(type);
596 }
catch(std::exception& e) {
597 THROW_ERROR(
"active_symvar", e.what());
603 return (*this)->active_values(type);
604 }
catch(std::exception& e) {
605 THROW_ERROR(
"active_values", e.what());
611 return (*this)->x_lookup(i);
612 }
catch(std::exception& e) {
613 THROW_ERROR(
"x_lookup", e.what());
619 return (*this)->g_index_reduce_g(i);
620 }
catch(std::exception& e) {
621 THROW_ERROR(
"g_index_reduce_g", e.what());
627 return (*this)->g_index_reduce_x(i);
628 }
catch(std::exception& e) {
629 THROW_ERROR(
"g_index_reduce_x", e.what());
635 return (*this)->g_index_unreduce_g(i);
636 }
catch(std::exception& e) {
637 THROW_ERROR(
"g_index_unreduce_g", e.what());
643 return (*this)->g_lookup(i);
644 }
catch(std::exception& e) {
645 THROW_ERROR(
"g_lookup", e.what());
651 return (*this)->x_describe(i, opts);
652 }
catch(std::exception& e) {
653 THROW_ERROR(
"x_describe", e.what());
658 return (*this)->g_describe(i, opts);
659 }
catch(std::exception& e) {
660 THROW_ERROR(
"g_describe", e.what());
665 return (*this)->describe(
x, indent, opts);
666 }
catch(std::exception& e) {
667 THROW_ERROR(
"describe", e.what());
673 (*this)->show_infeasibilities(tol, opts);
674 }
catch(std::exception& e) {
675 THROW_ERROR(
"show_infeasibilities", e.what());
681 (*this)->solve_prepare();
682 }
catch(std::exception& e) {
683 THROW_ERROR(
"solve_prepare", e.what());
688 return (*this)->solve_actual(args);
689 }
catch(std::exception& e) {
690 THROW_ERROR(
"solve_actual", e.what());
696 return (*this)->arg();
697 }
catch(std::exception& e) {
698 THROW_ERROR(
"arg", e.what());
706 }
catch(std::exception& e) {
707 THROW_ERROR(
"res", e.what());
713 return (*this)->res();
714 }
catch(std::exception& e) {
715 THROW_ERROR(
"res", e.what());
721 return (*this)->constraints();
722 }
catch(std::exception& e) {
723 THROW_ERROR(
"constraints", e.what());
733 }
catch(std::exception& e) {
734 THROW_ERROR(
"baked_copy", e.what());
740 (*this)->assert_empty();
741 }
catch(std::exception& e) {
742 THROW_ERROR(
"assert_empty", e.what());
748 return (*this)->instance_number();
749 }
catch(std::exception& e) {
750 THROW_ERROR(
"instance_number", e.what());
755 stream <<
"Opti {" << std::endl;
760 <<
" (nx = " << mycopy.
nx() <<
")" << std::endl;
762 <<
" (np = " << mycopy.
np() <<
")" << std::endl;
764 <<
" (ng = " << mycopy.
ng() <<
")" << std::endl;
766 stream <<
" CasADi solver needs updating." << std::endl;
768 stream <<
" CasADi solver allocated." << std::endl;
771 stream <<
" CasADi solver was called: " << mycopy.
return_status() << std::endl;
777 std::stringstream ss;
785 }
catch(std::exception& e) {
786 THROW_ERROR(
"bake", e.what());
792 (*this)->mark_problem_dirty(flag);
793 }
catch(std::exception& e) {
794 THROW_ERROR(
"mark_problem_dirty", e.what());
799 return (*this)->problem_dirty();
800 }
catch(std::exception& e) {
801 THROW_ERROR(
"problem_dirty", e.what());
807 (*this)->mark_solver_dirty(flag);
808 }
catch(std::exception& e) {
809 THROW_ERROR(
"mark_solver_dirty", e.what());
814 return (*this)->solver_dirty();
815 }
catch(std::exception& e) {
816 THROW_ERROR(
"solver_dirty", e.what());
822 (*this)->mark_solved(flag);
823 }
catch(std::exception& e) {
824 THROW_ERROR(
"mark_solved", e.what());
830 return (*this)->solved();
831 }
catch(std::exception& e) {
832 THROW_ERROR(
"solved", e.what());
838 (*this)->assert_solved();
839 }
catch(std::exception& e) {
840 THROW_ERROR(
"assert_solved", e.what());
845 (*this)->assert_baked();
846 }
catch(std::exception& e) {
847 THROW_ERROR(
"assert_baked", e.what());
858 return (*this)->
copy();
void own(SharedObjectInternal *node)
SharedObjectInternal * operator->() const
Access a member function or object.
casadi_index g_index_unreduce_g(casadi_index i) const
void mark_problem_dirty(bool flag=true)
std::vector< MX > active_symvar(VariableType type) const
void mark_solver_dirty(bool flag=true)
MX x_lookup(casadi_index i) const
bool problem_dirty() const
std::vector< MX > symvar() const
Get symbols present in expression.
std::string describe(const MX &x, casadi_index indent=0, const Dict &opts=Dict()) const
casadi_index g_index_reduce_x(casadi_index i) const
DMDict solve_actual(const DMDict &args)
void bake()
Fix the structure of the optimization problem.
MX g_lookup(casadi_index i) const
std::vector< MX > constraints() const
bool solver_dirty() const
void set_meta_con(const MX &m, const MetaCon &meta)
Set meta-data of an expression.
MetaCon canon_expr(const MX &expr) const
Interpret an expression (for internal use only)
std::string x_describe(casadi_index i, const Dict &opts=Dict()) const
Function casadi_solver() const
Get the underlying CasADi solver of the Opti stack.
void assert_baked() const
std::string g_describe(casadi_index i, const Dict &opts=Dict()) const
void assert_empty() const
void mark_solved(bool flag=true)
OptiAdvanced baked_copy() const
bool is_parametric(const MX &expr) const
return true if expression is only dependant on Opti parameters, not variables
void show_infeasibilities(double tol=0, const Dict &opts=Dict()) const
void assert_active_symbol(const MX &m) const
void assert_solved() const
std::vector< DM > active_values(VariableType type) const
void set_meta(const MX &m, const MetaVar &meta)
Set meta-data of an expression.
MetaVar get_meta(const MX &m) const
Get meta-data of symbol (for internal use only)
MetaCon get_meta_con(const MX &m) const
Get meta-data of symbol (for internal use only)
casadi_index g_index_reduce_g(casadi_index i) const
casadi_int instance_number() const
A simplified interface for NLP modeling/solving.
OptiAdvanced baked_copy() const
static OptiNode * create(const std::string &problem_type)
A simplified interface for NLP modeling/solving.
std::string get_str(bool more=false) const
void disp(std::ostream &stream, bool more=false) const
native_DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
Dict stats() const
Get statistics.
std::vector< MX > value_parameters() const
std::vector< MX > value_variables() const
get assignment expressions for the optimal solution
OptiSol(const Opti &opti)
A simplified interface for NLP modeling/solving.
Function scale_helper(const Function &h) const
Scale a helper function constructed via opti.x, opti.g, ...
void set_domain(const MX &x, const std::string &domain)
Set domain of a decision variable.
DM x_linear_scale() const
void subject_to()
Clear constraints.
casadi_int ng() const
Number of (scalarised) constraints.
void minimize(const MX &f, double linear_scale=1)
Set objective.
casadi_int np() const
Number of (scalarised) parameters.
Dict stats() const
Get statistics.
native_DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
void disp(std::ostream &stream, bool more=false) const
Print representation.
MX lbg() const
Get all (scalarised) bounds on constraints as a column vector.
std::string return_status() const
Get return status of solver.
casadi_int nx() const
Number of (scalarised) decision variables.
Opti(const std::string &problem_type="nlp")
Create Opti Context.
std::string get_str(bool more=false) const
Get string representation.
OptiAdvanced debug() const
Get a copy with advanced functionality.
MX variable(casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
Create a decision variable (symbol)
MX f() const
Get objective expression.
std::vector< MX > initial() const
get assignment expressions for initial values
OptiAdvanced advanced() const
Get a copy with advanced functionality.
Opti copy() const
Get a copy of the.
Dict user_dict(const MX &m) const
Get user data.
Function to_function(const std::string &name, const std::vector< MX > &args, const std::vector< MX > &res, const Dict &opts=Dict())
Create a CasADi Function from the Opti solver.
void set_linear_scale(const MX &x, const DM &scale, const DM &offset=0)
Set scale of a decision variable.
OptiNode * operator->()
Access a member of the node.
MX parameter(casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
Create a parameter (symbol); fixed during optimization.
void set_value(const MX &x, const DM &v)
Set value of parameter.
DM x_linear_scale_offset() const
void callback_class()
Helper methods for callback()
MX p() const
Get all (scalarised) parameters as a symbolic column vector.
std::vector< MX > value_variables() const
get assignment expressions for latest values
void solver(const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
Set a solver.
MX dual(const MX &m) const
get the dual variable
void set_initial(const MX &x, const DM &v)
static Opti create(OptiNode *node)
void update_user_dict(const MX &m, const Dict &meta)
add user data
OptiSol solve()
Crunch the numbers; solve the problem.
std::vector< MX > value_parameters() const
DM g_linear_scale() const
MX lam_g() const
Get all (scalarised) dual variables as a symbolic column vector.
MX x() const
Get all (scalarised) decision variables as a symbolic column vector.
double f_linear_scale() const
OptiSol solve_limited()
Crunch the numbers; solve the problem.
MX g() const
Get all (scalarised) constraint expressions as a column vector.
GenericShared implements a reference counting framework similar for efficient and.
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::map< std::string, DM > DMDict