A simplified interface for NLP modeling/solving. More...
#include <optistack.hpp>
This class offers a view with model description facilities The API is guaranteed to be stable.
Example NLP:
opti = casadi.Opti();
x = opti.variable();
y = opti.variable();
opti.minimize( (y-x^2)^2 );
opti.subject_to( x^2+y^2==1 );
opti.subject_to( x+y>=1 );
opti.solver('ipopt');
sol = opti.solve();
sol.value(x)
sol.value(y)Example parametric NLP:
opti = casadi.Opti();
x = opti.variable(2,1);
p = opti.parameter();
opti.minimize( (p*x(2)-x(1)^2)^2 );
opti.subject_to( 1<=sum(x)<=2 );
opti.solver('ipopt');
opti.set_value(p, 3);
sol = opti.solve();
sol.value(x)
opti.set_value(p, 5);
sol = opti.solve();
sol.value(x)Extra doc: https://github.com/casadi/casadi/wiki/L_16
Definition at line 88 of file optistack.hpp.

Public Types | |
| using | internal_base_type = SharedObjectInternal |
| using | base_type = SharedObject |
Public Member Functions | |
| Opti (const std::string &problem_type="nlp") | |
| Create Opti Context. More... | |
| MX | variable (casadi_int n=1, casadi_int m=1, const std::string &attribute="full") |
| Create a decision variable (symbol) More... | |
| MX | variable (const Sparsity &sp, const std::string &attribute="full") |
| MX | variable (const MX &symbol, const std::string &attribute="full") |
| MX | parameter (casadi_int n=1, casadi_int m=1, const std::string &attribute="full") |
| Create a parameter (symbol); fixed during optimization. More... | |
| MX | parameter (const Sparsity &sp, const std::string &attribute="full") |
| MX | parameter (const MX &symbol, const std::string &attribute="full") |
| void | minimize (const MX &f, double linear_scale=1) |
| Set objective. More... | |
| void | subject_to () |
| Clear constraints. More... | |
| void | solver (const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict()) |
| Set a solver. More... | |
| Dict | stats () const |
| Get statistics. More... | |
| std::string | return_status () const |
| Get return status of solver. More... | |
| std::vector< MX > | initial () const |
| get assignment expressions for initial values More... | |
| std::vector< MX > | value_variables () const |
| get assignment expressions for latest values More... | |
| std::vector< MX > | value_parameters () const |
| Function | scale_helper (const Function &h) const |
| Scale a helper function constructed via opti.x, opti.g, ... More... | |
| MX | dual (const MX &m) const |
| get the dual variable More... | |
| casadi_int | nx () const |
| Number of (scalarised) decision variables. More... | |
| casadi_int | np () const |
| Number of (scalarised) parameters. More... | |
| casadi_int | ng () const |
| Number of (scalarised) constraints. More... | |
| MX | x () const |
| Get all (scalarised) decision variables as a symbolic column vector. More... | |
| MX | p () const |
| Get all (scalarised) parameters as a symbolic column vector. More... | |
| MX | g () const |
| Get all (scalarised) constraint expressions as a column vector. More... | |
| MX | f () const |
| Get objective expression. More... | |
| MX | lbg () const |
| Get all (scalarised) bounds on constraints as a column vector. More... | |
| MX | ubg () const |
| DM | x_linear_scale () const |
| DM | x_linear_scale_offset () const |
| DM | g_linear_scale () const |
| double | f_linear_scale () const |
| MX | lam_g () const |
| Get all (scalarised) dual variables as a symbolic column vector. More... | |
| OptiAdvanced | debug () const |
| Get a copy with advanced functionality. More... | |
| OptiAdvanced | advanced () const |
| Get a copy with advanced functionality. More... | |
| Opti | copy () const |
| Get a copy of the. More... | |
| void | update_user_dict (const MX &m, const Dict &meta) |
| add user data More... | |
| void | update_user_dict (const std::vector< MX > &m, const Dict &meta) |
| Dict | user_dict (const MX &m) const |
| Get user data. More... | |
| std::string | type_name () const |
| Readable name of the class. More... | |
| void | disp (std::ostream &stream, bool more=false) const |
| Print representation. More... | |
| std::string | get_str (bool more=false) const |
| Get string representation. More... | |
| std::string | class_name () const |
| Get class name. More... | |
| std::string | debug_repr () const |
| bool | is_null () const |
| Is a null pointer? More... | |
| casadi_int | __hash__ () const |
| Returns a number that is unique for a given Node. More... | |
| void | subject_to (const MX &g, const Dict &options=Dict()) |
| Add constraints. More... | |
| void | subject_to (const std::vector< MX > &g, const Dict &options=Dict()) |
| Add constraints. More... | |
| void | subject_to (const MX &g, const DM &linear_scale, const Dict &options=Dict()) |
| Add constraints. More... | |
| void | subject_to (const std::vector< MX > &g, const DM &linear_scale, const Dict &options=Dict()) |
| Add constraints. More... | |
| void | set_initial (const MX &x, const DM &v) |
| void | set_initial (const std::vector< MX > &assignments) |
| void | set_value (const MX &x, const DM &v) |
| Set value of parameter. More... | |
| void | set_value (const std::vector< MX > &assignments) |
| Set value of parameter. More... | |
| void | set_domain (const MX &x, const std::string &domain) |
| Set domain of a decision variable. More... | |
| void | set_linear_scale (const MX &x, const DM &scale, const DM &offset=0) |
| Set scale of a decision variable. More... | |
| OptiSol | solve () |
| Crunch the numbers; solve the problem. More... | |
| OptiSol | solve_limited () |
| Crunch the numbers; solve the problem. More... | |
| native_DM | value (const MX &x, const std::vector< MX > &values=std::vector< MX >()) const |
| native_DM | value (const DM &x, const std::vector< MX > &values=std::vector< MX >()) const |
| Set domain of a decision variable. More... | |
| native_DM | value (const SX &x, const std::vector< MX > &values=std::vector< MX >()) const |
| Set domain of a decision variable. More... | |
| 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. More... | |
| Function | to_function (const std::string &name, const std::vector< MX > &args, const std::vector< MX > &res, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict()) |
| Create a CasADi Function from the Opti solver. More... | |
| Function | to_function (const std::string &name, const std::map< std::string, MX > &dict, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict()) |
| Create a CasADi Function from the Opti solver. More... | |
| void | callback_class (OptiCallback *callback) |
| Helper methods for callback() More... | |
| void | callback_class () |
| Helper methods for callback() More... | |
Static Public Member Functions | |
| static MX | bounded (const MX &lb, const MX &expr, const MX &ub) |
| Construct a double inequality. More... | |
Friends | |
| class | InternalOptiCallback |
|
inherited |
Definition at line 103 of file shared_object.hpp.
|
inherited |
Definition at line 102 of file shared_object.hpp.
| casadi::Opti::Opti | ( | const std::string & | problem_type = "nlp" | ) |
| [in] | problem_type | of optimization 'nlp' or 'conic' (default nlp) |
Extra doc: https://github.com/casadi/casadi/wiki/L_17
|
inherited |
If the Object does not point to any node, "0" is returned.
Extra doc: https://github.com/casadi/casadi/wiki/L_av
Definition at line 123 of file generic_shared_impl.hpp.
| OptiAdvanced casadi::Opti::advanced | ( | ) | const |
You get access to more methods, but you have no guarantees about API stability
The copy is effectively a deep copy: Updating the state of the copy does not update the original.
Extra doc: https://github.com/casadi/casadi/wiki/L_1m
Constructs: lb(p) <= g(x,p) <= ub(p)
Python prohibits such syntax directly
Extra doc: https://github.com/casadi/casadi/wiki/L_1k
Definition at line 397 of file optistack.hpp.
| void casadi::Opti::callback_class | ( | ) |
Do not use directly.
Extra doc: https://github.com/casadi/casadi/wiki/L_1p
| void casadi::Opti::callback_class | ( | OptiCallback * | callback | ) |
Do not use directly.
Extra doc: https://github.com/casadi/casadi/wiki/L_1p
|
inherited |
Extra doc: https://github.com/casadi/casadi/wiki/L_au
| Opti casadi::Opti::copy | ( | ) | const |
The copy is effectively a deep copy: Updating the state of the copy does not update the original.
Extra doc: https://github.com/casadi/casadi/wiki/L_1n
| OptiAdvanced casadi::Opti::debug | ( | ) | const |
You get access to more methods, but you have no guarantees about API stability
The copy is effectively a deep copy: Updating the state of the copy does not update the original.
Extra doc: https://github.com/casadi/casadi/wiki/L_1l
|
inherited |
Definition at line 112 of file generic_shared_impl.hpp.
| void casadi::Opti::disp | ( | std::ostream & | stream, |
| bool | more = false |
||
| ) | const |
m must be a constraint expression. The returned value is still a symbolic expression. Use value on it to obtain the numerical value.
Extra doc: https://github.com/casadi/casadi/wiki/L_1h
| MX casadi::Opti::f | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_26e
| double casadi::Opti::f_linear_scale | ( | ) | const |
| MX casadi::Opti::g | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_26d
| DM casadi::Opti::g_linear_scale | ( | ) | const |
| std::string casadi::Opti::get_str | ( | bool | more = false | ) | const |
| std::vector<MX> casadi::Opti::initial | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_266
|
inherited |
Definition at line 116 of file generic_shared_impl.hpp.
| MX casadi::Opti::lam_g | ( | ) | const |
Useful for obtaining the Lagrange Hessian:
* sol.value(hessian(opti.f+opti.lam_g'*opti.g,opti.x)) % MATLAB * sol.value(hessian(opti.f+dot(opti.lam_g,opti.g),opti.x)[0]) # Python *
Extra doc: https://github.com/casadi/casadi/wiki/L_1i
| MX casadi::Opti::lbg | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_26f
| void casadi::Opti::minimize | ( | const MX & | f, |
| double | linear_scale = 1 |
||
| ) |
Objective must be a scalar. Default objective: 0 When method is called multiple times, the last call takes effect
Extra doc: https://github.com/casadi/casadi/wiki/L_1a
| casadi_int casadi::Opti::ng | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_26a
| casadi_int casadi::Opti::np | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_269
| casadi_int casadi::Opti::nx | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_268
| MX casadi::Opti::p | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_26c
| MX casadi::Opti::parameter | ( | casadi_int | n = 1, |
| casadi_int | m = 1, |
||
| const std::string & | attribute = "full" |
||
| ) |
The order of creation does not matter. It is not required for parameter to actualy appear in the optimization problem. Parameters that do appear, must be given a value before the problem can be solved.
| [in] | n | number of rows (default 1) |
| [in] | m | number of columnss (default 1) |
| [in] | attribute | 'full' (default) or 'symmetric' |
Extra doc: https://github.com/casadi/casadi/wiki/L_19
| std::string casadi::Opti::return_status | ( | ) | const |
passed as-is from nlpsol
No stability can be guaranteed about this part of the API
Extra doc: https://github.com/casadi/casadi/wiki/L_1g
Extra doc: https://github.com/casadi/casadi/wiki/L_2ci
| void casadi::Opti::set_domain | ( | const MX & | x, |
| const std::string & | domain | ||
| ) |
| [in] | x | decision variable |
| [in] | type | 'real', 'integer' (default: real) |
* opti.set_domain(x, "real") * opti.set_domain(x, "integer") *
Extra doc: https://github.com/casadi/casadi/wiki/L_27t
Set initial guess for decision variables
* opti.set_initial(x, 2) * opti.set_initial(10*x(1), 2) *
| void casadi::Opti::set_initial | ( | const std::vector< MX > & | assignments | ) |
Set initial guess for decision variables
* opti.set_initial(x, 2) * opti.set_initial(10*x(1), 2) *
(x-offset)/scale will be used in the optimization problem
| [in] | x | decision variable |
| [in] | scale | scaling value (default: 1) |
| [in] | offset | scaling value (default: 0) |
* opti.set_linear_scale(x, 20) * opti.set_linear_scale(x, 20, 273.15) *
Extra doc: https://github.com/casadi/casadi/wiki/L_2bs
Each parameter must be given a value before 'solve' can be called
Extra doc: https://github.com/casadi/casadi/wiki/L_1d
| void casadi::Opti::set_value | ( | const std::vector< MX > & | assignments | ) |
Each parameter must be given a value before 'solve' can be called
Extra doc: https://github.com/casadi/casadi/wiki/L_1d
| OptiSol casadi::Opti::solve | ( | ) |
| OptiSol casadi::Opti::solve_limited | ( | ) |
Allows the solver to return without error when an iteration or time limit is reached
Extra doc: https://github.com/casadi/casadi/wiki/L_1e
| void casadi::Opti::solver | ( | const std::string & | solver, |
| const Dict & | plugin_options = Dict(), |
||
| const Dict & | solver_options = Dict() |
||
| ) |
| [in] | solver | any of the nlpsol plugins can be used here In practice, not all nlpsol plugins may be supported yet |
| [in] | options | passed on to nlpsol plugin No stability can be guaranteed about this part of the API |
| [in] | options | to be passed to nlpsol solver No stability can be guaranteed about this part of the API |
Extra doc: https://github.com/casadi/casadi/wiki/L_1c
| Dict casadi::Opti::stats | ( | ) | const |
nlpsol stats are passed as-is. No stability can be guaranteed about this part of the API
Extra doc: https://github.com/casadi/casadi/wiki/L_1f
| void casadi::Opti::subject_to | ( | ) |
Examples:
* \begin{itemize}
* opti.subject_to( sqrt(x+y) >= 1);
* opti.subject_to( sqrt(x+y) > 1)}: same as above
* opti.subject_to( 1<= sqrt(x+y) )}: same as above
* opti.subject_to( 5*x+y==1 )}: equality
*
* Python
* opti.subject_to([x*y>=1,x==3])
* opti.subject_to(opti.bounded(0,x,1))
*
* MATLAB
* opti.subject_to({x*y>=1,x==3})
* opti.subject_to( 0<=x<=1 )
* Related functionalities:
opti.debug.show_infeasibilities() may be used to inspect which constraints are violated
Extra doc: https://github.com/casadi/casadi/wiki/L_1b
| void casadi::Opti::subject_to | ( | const MX & | g, |
| const DM & | linear_scale, | ||
| const Dict & | options = Dict() |
||
| ) |
Examples:
* \begin{itemize}
* opti.subject_to( sqrt(x+y) >= 1);
* opti.subject_to( sqrt(x+y) > 1)}: same as above
* opti.subject_to( 1<= sqrt(x+y) )}: same as above
* opti.subject_to( 5*x+y==1 )}: equality
*
* Python
* opti.subject_to([x*y>=1,x==3])
* opti.subject_to(opti.bounded(0,x,1))
*
* MATLAB
* opti.subject_to({x*y>=1,x==3})
* opti.subject_to( 0<=x<=1 )
* Related functionalities:
opti.debug.show_infeasibilities() may be used to inspect which constraints are violated
Extra doc: https://github.com/casadi/casadi/wiki/L_1b
Examples:
* \begin{itemize}
* opti.subject_to( sqrt(x+y) >= 1);
* opti.subject_to( sqrt(x+y) > 1)}: same as above
* opti.subject_to( 1<= sqrt(x+y) )}: same as above
* opti.subject_to( 5*x+y==1 )}: equality
*
* Python
* opti.subject_to([x*y>=1,x==3])
* opti.subject_to(opti.bounded(0,x,1))
*
* MATLAB
* opti.subject_to({x*y>=1,x==3})
* opti.subject_to( 0<=x<=1 )
* Related functionalities:
opti.debug.show_infeasibilities() may be used to inspect which constraints are violated
Extra doc: https://github.com/casadi/casadi/wiki/L_1b
| void casadi::Opti::subject_to | ( | const std::vector< MX > & | g, |
| const DM & | linear_scale, | ||
| const Dict & | options = Dict() |
||
| ) |
Examples:
* \begin{itemize}
* opti.subject_to( sqrt(x+y) >= 1);
* opti.subject_to( sqrt(x+y) > 1)}: same as above
* opti.subject_to( 1<= sqrt(x+y) )}: same as above
* opti.subject_to( 5*x+y==1 )}: equality
*
* Python
* opti.subject_to([x*y>=1,x==3])
* opti.subject_to(opti.bounded(0,x,1))
*
* MATLAB
* opti.subject_to({x*y>=1,x==3})
* opti.subject_to( 0<=x<=1 )
* Related functionalities:
opti.debug.show_infeasibilities() may be used to inspect which constraints are violated
Extra doc: https://github.com/casadi/casadi/wiki/L_1b
| Function casadi::Opti::to_function | ( | const std::string & | name, |
| const std::map< std::string, MX > & | dict, | ||
| const std::vector< std::string > & | name_in, | ||
| const std::vector< std::string > & | name_out, | ||
| const Dict & | opts = Dict() |
||
| ) |
| [in] | name | Name of the resulting CasADi Function |
| [in] | args | List of parameters and decision/dual variables (which can be given an initial guess) with the resulting Function |
| [in] | res | List of expressions that will get evaluated at the optimal solution |
| [in] | opts | Standard CasADi Funcion options |
Extra doc: https://github.com/casadi/casadi/wiki/L_1j
| Function casadi::Opti::to_function | ( | const std::string & | name, |
| const std::vector< MX > & | args, | ||
| const std::vector< MX > & | res, | ||
| const Dict & | opts = Dict() |
||
| ) |
| [in] | name | Name of the resulting CasADi Function |
| [in] | args | List of parameters and decision/dual variables (which can be given an initial guess) with the resulting Function |
| [in] | res | List of expressions that will get evaluated at the optimal solution |
| [in] | opts | Standard CasADi Funcion options |
Extra doc: https://github.com/casadi/casadi/wiki/L_1j
| Function casadi::Opti::to_function | ( | const std::string & | name, |
| const std::vector< MX > & | args, | ||
| const std::vector< MX > & | res, | ||
| const std::vector< std::string > & | name_in, | ||
| const std::vector< std::string > & | name_out, | ||
| const Dict & | opts = Dict() |
||
| ) |
| [in] | name | Name of the resulting CasADi Function |
| [in] | args | List of parameters and decision/dual variables (which can be given an initial guess) with the resulting Function |
| [in] | res | List of expressions that will get evaluated at the optimal solution |
| [in] | opts | Standard CasADi Funcion options |
Extra doc: https://github.com/casadi/casadi/wiki/L_1j
|
inline |
Definition at line 440 of file optistack.hpp.
| MX casadi::Opti::ubg | ( | ) | const |
Add arbitrary data in the form of a dictionary to symbols or constraints
Extra doc: https://github.com/casadi/casadi/wiki/L_1o
| native_DM casadi::Opti::value | ( | const DM & | x, |
| const std::vector< MX > & | values = std::vector< MX >() |
||
| ) | const |
| [in] | x | decision variable |
| [in] | type | 'real', 'integer' (default: real) |
* opti.set_domain(x, "real") * opti.set_domain(x, "integer") *
Extra doc: https://github.com/casadi/casadi/wiki/L_27t
| native_DM casadi::Opti::value | ( | const MX & | x, |
| const std::vector< MX > & | values = std::vector< MX >() |
||
| ) | const |
Obtain value of expression at the current value
In regular mode, teh current value is the converged solution In debug mode, the value can be non-converged
| [in] | values | Optional assignment expressions (e.g. x==3) to overrule the current value |
| native_DM casadi::Opti::value | ( | const SX & | x, |
| const std::vector< MX > & | values = std::vector< MX >() |
||
| ) | const |
| [in] | x | decision variable |
| [in] | type | 'real', 'integer' (default: real) |
* opti.set_domain(x, "real") * opti.set_domain(x, "integer") *
Extra doc: https://github.com/casadi/casadi/wiki/L_27t
| std::vector<MX> casadi::Opti::value_parameters | ( | ) | const |
| std::vector<MX> casadi::Opti::value_variables | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_267
| MX casadi::Opti::variable | ( | casadi_int | n = 1, |
| casadi_int | m = 1, |
||
| const std::string & | attribute = "full" |
||
| ) |
The order of creation matters. The order will be reflected in the optimization problem. It is not required for decision variables to actualy appear in the optimization problem.
| [in] | n | number of rows (default 1) |
| [in] | m | number of columnss (default 1) |
| [in] | attribute | 'full' (default) or 'symmetric' |
Extra doc: https://github.com/casadi/casadi/wiki/L_18
| MX casadi::Opti::x | ( | ) | const |
Extra doc: https://github.com/casadi/casadi/wiki/L_26b
| DM casadi::Opti::x_linear_scale | ( | ) | const |
| DM casadi::Opti::x_linear_scale_offset | ( | ) | const |
|
friend |
Definition at line 91 of file optistack.hpp.