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 if ((it=
find(name_in.begin(), name_in.end(), i.first))!=name_in.end()) {
451 ex_in[it-name_in.begin()] = i.second;
452 }
else if ((it=
find(name_out.begin(), name_out.end(), i.first))!=name_out.end()) {
454 ex_out[it-name_out.begin()] = i.second;
457 casadi_error(
"Unknown dictionary entry: '" + i.first +
"'");
460 return to_function(name, ex_in, ex_out, name_in, name_out, opts);
465 (*this)->callback_class(callback);
466 }
catch(std::exception& e) {
467 THROW_ERROR(
"callback_class", e.what());
473 (*this)->callback_class();
474 }
catch(std::exception& e) {
475 THROW_ERROR(
"callback_class", e.what());
481 (*this)->update_user_dict(m, meta);
482 }
catch(std::exception& e) {
483 THROW_ERROR(
"update_user_dict", e.what());
488 for (
const auto& m : ms)
494 return (*this)->user_dict(m);
495 }
catch(std::exception& e) {
496 THROW_ERROR(
"user_dict", e.what());
502 return (*this)->casadi_solver();
503 }
catch(std::exception& e) {
504 THROW_ERROR(
"casadi_solver", e.what());
510 return (*this)->is_parametric(expr);
511 }
catch(std::exception& e) {
512 THROW_ERROR(
"is_parametric", e.what());
518 return (*this)->symvar();
519 }
catch(std::exception& e) {
520 THROW_ERROR(
"symvar", e.what());
526 return (*this)->symvar(expr);
527 }
catch(std::exception& e) {
528 THROW_ERROR(
"symvar", e.what());
534 return (*this)->symvar(expr, type);
535 }
catch(std::exception& e) {
536 THROW_ERROR(
"symvar", e.what());
542 return (*this)->canon_expr(expr);
543 }
catch(std::exception& e) {
544 THROW_ERROR(
"canon_expr", e.what());
550 return (*this)->get_meta(m);
551 }
catch(std::exception& e) {
552 THROW_ERROR(
"get_meta", e.what());
558 return (*this)->get_meta_con(m);
559 }
catch(std::exception& e) {
560 THROW_ERROR(
"get_meta_con", e.what());
566 (*this)->set_meta(m, meta);
567 }
catch(std::exception& e) {
568 THROW_ERROR(
"set_meta", e.what());
574 return (*this)->set_meta_con(m, meta);
575 }
catch(std::exception& e) {
576 THROW_ERROR(
"set_meta_con", e.what());
583 (*this)->assert_active_symbol(m);
584 }
catch(std::exception& e) {
585 THROW_ERROR(
"assert_active_symbol", e.what());
591 return (*this)->active_symvar(type);
592 }
catch(std::exception& e) {
593 THROW_ERROR(
"active_symvar", e.what());
599 return (*this)->active_values(type);
600 }
catch(std::exception& e) {
601 THROW_ERROR(
"active_values", e.what());
607 return (*this)->x_lookup(i);
608 }
catch(std::exception& e) {
609 THROW_ERROR(
"x_lookup", e.what());
615 return (*this)->g_index_reduce_g(i);
616 }
catch(std::exception& e) {
617 THROW_ERROR(
"g_index_reduce_g", e.what());
623 return (*this)->g_index_reduce_x(i);
624 }
catch(std::exception& e) {
625 THROW_ERROR(
"g_index_reduce_x", e.what());
631 return (*this)->g_index_unreduce_g(i);
632 }
catch(std::exception& e) {
633 THROW_ERROR(
"g_index_unreduce_g", e.what());
639 return (*this)->g_lookup(i);
640 }
catch(std::exception& e) {
641 THROW_ERROR(
"g_lookup", e.what());
647 return (*this)->x_describe(i, opts);
648 }
catch(std::exception& e) {
649 THROW_ERROR(
"x_describe", e.what());
654 return (*this)->g_describe(i, opts);
655 }
catch(std::exception& e) {
656 THROW_ERROR(
"g_describe", e.what());
661 return (*this)->describe(
x, indent, opts);
662 }
catch(std::exception& e) {
663 THROW_ERROR(
"describe", e.what());
669 (*this)->show_infeasibilities(tol, opts);
670 }
catch(std::exception& e) {
671 THROW_ERROR(
"show_infeasibilities", e.what());
677 (*this)->solve_prepare();
678 }
catch(std::exception& e) {
679 THROW_ERROR(
"solve_prepare", e.what());
684 return (*this)->solve_actual(args);
685 }
catch(std::exception& e) {
686 THROW_ERROR(
"solve_actual", e.what());
692 return (*this)->arg();
693 }
catch(std::exception& e) {
694 THROW_ERROR(
"arg", e.what());
701 return (*this)->res(
res);
702 }
catch(std::exception& e) {
703 THROW_ERROR(
"res", e.what());
709 return (*this)->res();
710 }
catch(std::exception& e) {
711 THROW_ERROR(
"res", e.what());
717 return (*this)->constraints();
718 }
catch(std::exception& e) {
719 THROW_ERROR(
"constraints", e.what());
729 }
catch(std::exception& e) {
730 THROW_ERROR(
"baked_copy", e.what());
736 return (*this)->assert_empty();
737 }
catch(std::exception& e) {
738 THROW_ERROR(
"assert_empty", e.what());
744 return (*this)->instance_number();
745 }
catch(std::exception& e) {
746 THROW_ERROR(
"instance_number", e.what());
751 stream <<
"Opti {" << std::endl;
756 <<
" (nx = " << mycopy.
nx() <<
")" << std::endl;
758 <<
" (np = " << mycopy.
np() <<
")" << std::endl;
760 <<
" (ng = " << mycopy.
ng() <<
")" << std::endl;
762 stream <<
" CasADi solver needs updating." << std::endl;
764 stream <<
" CasADi solver allocated." << std::endl;
767 stream <<
" CasADi solver was called: " << mycopy.
return_status() << std::endl;
773 std::stringstream ss;
781 }
catch(std::exception& e) {
782 THROW_ERROR(
"bake", e.what());
788 (*this)->mark_problem_dirty(flag);
789 }
catch(std::exception& e) {
790 THROW_ERROR(
"mark_problem_dirty", e.what());
795 return (*this)->problem_dirty();
796 }
catch(std::exception& e) {
797 THROW_ERROR(
"problem_dirty", e.what());
803 (*this)->mark_solver_dirty(flag);
804 }
catch(std::exception& e) {
805 THROW_ERROR(
"mark_solver_dirty", e.what());
810 return (*this)->solver_dirty();
811 }
catch(std::exception& e) {
812 THROW_ERROR(
"solver_dirty", e.what());
818 (*this)->mark_solved(flag);
819 }
catch(std::exception& e) {
820 THROW_ERROR(
"mark_solved", e.what());
826 return (*this)->solved();
827 }
catch(std::exception& e) {
828 THROW_ERROR(
"solved", e.what());
834 (*this)->assert_solved();
835 }
catch(std::exception& e) {
836 THROW_ERROR(
"assert_solved", e.what());
841 (*this)->assert_baked();
842 }
catch(std::exception& e) {
843 THROW_ERROR(
"assert_baked", e.what());
854 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