25 #ifndef CASADI_OPTISTACK_HPP
26 #define CASADI_OPTISTACK_HPP
28 #include "function.hpp"
29 #include "callback.hpp"
235 :
public SWIG_IF_ELSE(PrintableCommon, Printable<Opti>),
237 friend class InternalOptiCallback;
308 Opti(
const std::string& problem_type=
"nlp");
389 MX variable(casadi_int n=1, casadi_int m=1,
const std::string& attribute=
"full");
458 MX parameter(casadi_int n=1, casadi_int m=1,
const std::string& attribute=
"full");
2304 const std::vector<MX>& args,
const std::vector<MX>& res,
2308 const std::vector<MX>& args,
const std::vector<MX>& res,
2309 const std::vector<std::string>& name_in,
2310 const std::vector<std::string>& name_out,
2315 const std::map<std::string, MX>& dict,
2316 const std::vector<std::string>& name_in,
2317 const std::vector<std::string>& name_out,
2656 void disp(std::ostream& stream,
bool more=
false)
const;
2691 const OptiNode* operator->()
const;
2769 casadi_error(
"Callback objects cannot be copied");
2773 uout() <<
"This is a simple callback at iteration" << i << std::endl;
2787 friend class InternalOptiCallback;
3563 class CASADI_EXPORT
OptiSol :
public SWIG_IF_ELSE(PrintableCommon, Printable<OptiAdvanced>) {
3567 void disp(std::ostream& stream,
bool more=
false)
const;
Opti with debugging and low-level access.
casadi_index g_index_unreduce_g(casadi_index i) const
Row of g for row i of the reduced g.
void mark_problem_dirty(bool flag=true)
Flag that the problem structure changed since the last bake.
MX objective() const
Objective expression; same as f()
void mark_solver_dirty(bool flag=true)
Flag that the solver must be reconstructed (solver() was called)
MX x_lookup(casadi_index i) const
Get the variable that owns entry i of the flattened x vector.
bool problem_dirty() const
True when the problem must be baked again before solving.
std::string describe(const MX &x, casadi_index indent=0, const Dict &opts=Dict()) const
Describe an expression in terms of this instance.
std::vector< MX > symvar(const MX &expr) const
Get the symbols of this instance.
std::vector< MX > constraints() const
Get the constraint expressions as passed to subject_to.
DMDict solve_actual(const DMDict &args)
Run the solver on an argument dict (see solve_prepare)
void bake()
Fix the structure of the optimization problem.
std::vector< MX > active_symvar(VariableType type) const
Get the symbols of a given type that appear in the problem.
MX g_lookup(casadi_index i) const
Get the constraint expression that owns row i of g.
OptiAdvanced(const Opti &x)
bool solver_dirty() const
True when the solver must be reconstructed.
void set_meta_con(const MX &m, const MetaCon &meta)
Set meta-data of an expression.
casadi_index g_index_reduce_x(casadi_index i) const
Index into x when row i of g was reduced to a simple bound (-1 otherwise)
MetaCon canon_expr(const MX &expr) const
Classify a constraint expression (for internal use only)
std::string x_describe(casadi_index i, const Dict &opts=Dict()) const
Describe entry i of the flattened x vector.
std::vector< MX > symvar() const
Get the symbols of this instance.
Function casadi_solver() const
Get the underlying nlpsol/qpsol Function.
void res(const DMDict &res)
Store a solver result dict as the latest values (marks the instance solved)
std::vector< MX > symvar(const MX &expr, VariableType type) const
Get the symbols of this instance.
void assert_baked() const
Raise an error when the problem is dirty.
std::string g_describe(casadi_index i, const Dict &opts=Dict()) const
Describe row i of the constraint vector g.
void assert_empty() const
Raise an error if an objective or constraints are present.
casadi_index g_index_reduce_g(casadi_index i) const
Row of the reduced g for row i of g when "detect_simple_bounds" is active (-1 if moved)
void mark_solved(bool flag=true)
Set or clear the solved state.
OptiAdvanced baked_copy() const
Copy of the instance with the problem baked (see bake)
bool is_parametric(const MX &expr) const
Check whether an expression depends on parameters only.
DMDict res() const
Result dict of the latest solve (x, f, g, lam_x, lam_g, lam_p)
void show_infeasibilities(double tol=0, const Dict &opts=Dict()) const
Print the constraints violated at the latest iterate.
std::vector< DM > active_values(VariableType type) const
Initial guesses (or parameter values) of the symbols returned by active_symvar.
void assert_active_symbol(const MX &m) const
Raise an error unless m is a symbol of this instance that appears in the problem.
void assert_solved() const
Raise an error unless solved()
DMDict arg() const
Solver arguments prepared by solve_prepare (x0, p, lam_g0, lbg, ubg)
void solve_prepare()
Prepare the solver arguments (first half of solve)
bool solved() const
True when a solver result is available for value()
~OptiAdvanced()
Destructor.
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_int instance_number() const
Sequence number of this Opti instance in the process; prefixes the symbol names.
Base class of iteration callbacks for Opti.
virtual void call(casadi_int i)
Called after every solver iteration with the iteration number.
OptiCallback(const OptiCallback &obj)
A simplified interface for NLP modeling/solving.
Snapshot of an Opti problem and its solution.
std::string get_str(bool more=false) const
std::vector< MX > value_parameters() const
void disp(std::ostream &stream, bool more=false) const
native_DM value(const DM &x, const std::vector< MX > &values=std::vector< MX >()) const
Evaluate an expression at the solution.
std::string type_name() const
Dict stats() const
Get the solver statistics of this solve.
Opti opti() const
Get the Opti copy this solution belongs to.
native_DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
Evaluate an expression at the solution.
native_DM value(const SX &x, const std::vector< MX > &values=std::vector< MX >()) const
Evaluate an expression at the solution.
std::vector< MX > value_variables() const
Get assignment expressions for the optimal solution.
OptiSol(const Opti &opti)
A simplified interface for NLP modeling/solving.
native_DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
Evaluate an expression at the current values.
native_DM value(const SX &x, const std::vector< MX > &values=std::vector< MX >()) const
Evaluate an expression at the current values.
std::vector< MX > value_variables() const
Get assignment expressions for the latest values.
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 the domain of a decision variable.
void subject_to(const MX &g, const DM &linear_scale, const Dict &options=Dict())
Add constraints.
DM x_linear_scale() const
Per-entry scale of x (see set_linear_scale); a Python property.
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())
Same, with inputs and outputs picked by name from a dict of expressions.
native_DM value(const DM &x, const std::vector< MX > &values=std::vector< MX >()) const
Evaluate an expression at the current values.
void subject_to()
Clear all constraints.
MX parameter(const Sparsity &sp, const std::string &attribute="full")
Create a parameter with a given sparsity pattern.
static MX bounded(const MX &lb, const MX &expr, const MX &ub)
Construct a double inequality.
casadi_int ng() const
Number of (scalarised) constraints.
void minimize(const MX &f, double linear_scale=1)
Set the objective.
void callback_class(OptiCallback *callback)
Helper methods for callback()
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.
casadi_int np() const
Number of (scalarised) parameters.
void set_initial(const std::vector< MX > &assignments)
Set the initial guess of decision variables or dual variables.
Dict stats() const
Get the statistics of the last solve.
void subject_to(const std::vector< MX > &g, const Dict &options=Dict())
Add constraints.
MX variable(const MX &symbol, const std::string &attribute="full")
Register an existing MX symbol as a decision variable of this instance.
MX parameter(const MX &symbol, const std::string &attribute="full")
Register an existing MX symbol as a parameter of this instance.
void subject_to(const MX &g, const Dict &options=Dict())
Add constraints.
void disp(std::ostream &stream, bool more=false) const
Print a summary of the instance.
MX lbg() const
Get all (scalarised) bounds on constraints as a column vector.
std::string return_status() const
Get the return status of the last solve.
casadi_int nx() const
Number of (scalarised) decision variables.
Opti(const std::string &problem_type="nlp")
Create an empty Opti context.
std::string get_str(bool more=false) const
Get string representation.
OptiAdvanced debug() const
Get a copy with advanced (debugging) 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 the objective expression.
OptiAdvanced advanced() const
Get a copy with advanced functionality.
Opti copy() const
Get a deep copy of the Opti instance.
Dict user_dict(const MX &m) const
Get the user data of a symbol or constraint.
void subject_to(const std::vector< MX > &g, const DM &linear_scale, const Dict &options=Dict())
Add constraints.
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 the linear scaling of a decision variable.
std::vector< MX > initial() const
Get assignment expressions for the initial guesses.
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 the value of a parameter.
DM x_linear_scale_offset() const
Per-entry offset of x (see set_linear_scale); a Python property.
void callback_class()
Helper methods for callback()
MX p() const
Get all (scalarised) parameters as a symbolic column vector.
MX variable(const Sparsity &sp, const std::string &attribute="full")
Create a decision variable with a given sparsity pattern; only "full" is supported.
void solver(const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
Select the numerical solver and its options.
std::vector< MX > value_parameters() const
MX dual(const MX &m) const
Get the dual variable (Lagrange multiplier) of a constraint.
void set_initial(const MX &x, const DM &v)
Set the initial guess of decision variables or dual variables.
void update_user_dict(const MX &m, const Dict &meta)
Add user data to a symbol or constraint.
OptiSol solve()
Solve the problem.
void set_value(const std::vector< MX > &assignments)
Set the value of a parameter.
DM g_linear_scale() const
Per-row scale of g (see subject_to); a Python property.
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.
void update_user_dict(const std::vector< MX > &m, const Dict &meta)
std::string type_name() const
Readable name of the class.
double f_linear_scale() const
Scale of the objective (see minimize); a Python property.
OptiSol solve_limited()
Solve the problem, accepting a limit as outcome.
MX g() const
Get all (scalarised) constraint expressions as a column vector.
GenericShared implements a reference counting framework similar for efficient and.
CASADI_EXPORT std::ostream & uout()
VariableType
Kind of an Opti symbol (MetaVar::type)
DomainType
Domain of a decision variable (see Opti::set_domain)
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
ConstraintType
How subject_to() classified a constraint (MetaCon::type)
@ OPTI_GENERIC_INEQUALITY
std::map< std::string, DM > DMDict
Range [start, stop) of a symbol or constraint in the flattened x or g vector.