25 #ifndef CASADI_OPTISTACK_INTERNAL_HPP
26 #define CASADI_OPTISTACK_INTERNAL_HPP
28 #include "optistack.hpp"
29 #include "shared_object_internal.hpp"
36 class null_ptr_on_copy {
38 null_ptr_on_copy<T>() : ptr_(nullptr) {}
39 null_ptr_on_copy<T>(
const null_ptr_on_copy<T>& rhs) : ptr_(nullptr) {}
40 void operator=(T* ptr) { ptr_ = ptr; }
41 T* operator->() {
return ptr_; }
42 operator bool()
const {
return ptr_; }
56 public SharedObjectInternal {
57 friend class InternalOptiCallback;
67 MX variable(casadi_int n=1, casadi_int m=1,
const std::string& attribute=
"full");
70 MX parameter(casadi_int n=1, casadi_int m=1,
const std::string& attribute=
"full");
81 void solver(
const std::string& solver,
109 DM value(
const MX& x,
const std::vector<MX>& values=std::vector<MX>())
const;
110 DM value(
const DM& x,
const std::vector<MX>& values=std::vector<MX>())
const {
return x; }
111 DM value(
const SX& x,
const std::vector<MX>& values=std::vector<MX>())
const {
183 std::string
describe(
const MX& x, casadi_int indent=0)
const;
201 std::string
class_name()
const override {
return "OptiNode"; }
204 casadi_int
nx()
const {
205 if (problem_dirty())
return baked_copy().nx();
206 return nlp_.at(
"x").size1();
210 casadi_int
np()
const {
211 if (problem_dirty())
return baked_copy().np();
212 return nlp_.at(
"p").size1();
216 casadi_int
ng()
const {
217 if (problem_dirty())
return baked_copy().ng();
218 return nlp_.at(
"g").size1();
223 if (problem_dirty())
return baked_copy().x();
229 if (problem_dirty())
return baked_copy().p();
235 if (problem_dirty())
return baked_copy().g();
241 if (problem_dirty())
return baked_copy().f();
246 if (problem_dirty())
return baked_copy().lbg();
251 if (problem_dirty())
return baked_copy().ubg();
257 if (problem_dirty())
return baked_copy().lam_g();
267 const std::vector<MX>& args,
const std::vector<MX>& res,
268 const std::vector<std::string>& name_in,
269 const std::vector<std::string>& name_out,
273 void disp(std::ostream& stream,
bool more=
false)
const override;
299 static std::map<VariableType, std::string> VariableType2String_;
300 std::string variable_type_to_string(
VariableType vt)
const;
302 bool parse_opti_name(
const std::string& name,
VariableType& vt)
const;
303 void register_dual(
MetaCon& meta);
306 void set_value_internal(
const MX& x,
const DM& v);
316 static std::vector<MX> ineq_unchain(
const MX& a,
bool& SWIG_OUTPUT(flipped));
324 const MetaCon& meta_con(
const MX& m)
const;
329 std::vector<MX> sort(
const std::vector<MX>& v)
const;
332 void assert_has(
const MX& m)
const;
334 bool has(
const MX& m)
const;
337 void assert_has_con(
const MX& m)
const;
339 bool has_con(
const MX& m)
const;
344 std::map<MXNode*, MetaVar> meta_;
346 std::map<MXNode*, MetaCon> meta_con_;
349 std::vector<MX> symbols_;
352 std::vector<bool> discrete_;
357 casadi_int count_var_;
358 casadi_int count_par_;
359 casadi_int count_dual_;
362 std::map< VariableType, std::vector<DM> > store_initial_, store_latest_;
365 std::vector<bool> symbol_active_;
380 std::vector<bool> equality_;
389 std::string problem_type_;
391 null_ptr_on_copy<OptiCallback> user_callback_;
394 bool old_callback()
const;
396 std::string solver_name_;
397 Dict solver_options_;
399 void assert_only_opti_symbols(
const MX& e)
const;
400 void assert_only_opti_nondual(
const MX& e)
const;
403 static casadi_int instance_count_;
404 casadi_int instance_number_;
407 std::string name_prefix()
const;
409 static std::string format_stacktrace(
const Dict& stacktrace, casadi_int indent);
Sparsity sparsity() const
Get the sparsity pattern.
Sparse matrix class. SX and DM are specializations.
static Matrix< Scalar > nan(const Sparsity &sp)
create a matrix with all nan
bool problem_dirty() const
void bake()
Fix the structure of the optimization problem.
A simplified interface for NLP modeling/solving.
Function solver_construct(bool callback=true)
std::string g_describe(casadi_int i) const
Dict user_dict(const MX &m) const
std::vector< MX > constraints() const
MX g() const
Get all (scalarised) constraint expressions as a column vector.
std::string describe(const MX &x, casadi_int indent=0) const
MetaCon get_meta_con(const MX &m) const
Get meta-data of symbol (for internal use only)
OptiAdvanced baked_copy() const
MX dual(const MX &m) const
get the dual variable
MX x() const
Get all (scalarised) decision variables as a symbolic column vector.
casadi_int nx() const
Number of (scalarised) decision variables.
MX variable(casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
Create a decision variable (symbol)
void bake()
Fix the structure of the optimization problem.
casadi_int instance_number() const
DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
void disp(std::ostream &stream, bool more=false) const override
Print representation.
std::vector< MX > active_symvar(VariableType type) const
void set_domain(const MX &x, const std::string &domain)
Set domain of variable.
static OptiNode * create(const std::string &problem_type)
OptiSol solve(bool accept_limit)
Crunch the numbers; solve the problem.
void res(const DMDict &res)
bool solver_dirty() const
casadi_int np() const
Number of (scalarised) parameters.
bool has_callback_class() const
MX g_lookup(casadi_int i) const
void subject_to(const MX &g)
brief Add constraints
casadi_int ng() const
Number of (scalarised) constraints.
MetaVar get_meta(const MX &m) const
Get meta-data of symbol (for internal use only)
void subject_to()
Clear constraints.
void minimize(const MX &f)
Set objective.
void assert_baked() const
void update_user_dict(const MX &m, const Dict &meta)
add meta-data of an expression
void assert_solved() const
std::vector< MX > initial() const
get assignment expressions for initial values
void set_meta(const MX &m, const MetaVar &meta)
Set meta-data of an expression.
std::vector< DM > active_values(VariableType type) const
std::string class_name() const override
MX parameter(casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
Create a parameter (symbol); fixed during optimization.
void set_initial(const std::vector< MX > &assignments)
DM value(const DM &x, const std::vector< MX > &values=std::vector< MX >()) const
bool is_parametric(const MX &expr) const
return true if expression is only dependant on Opti parameters, not variables
void assert_active_symbol(const MX &m) const
bool return_success(bool accept_limit) const
Did the solver return successfully?
bool problem_dirty() const
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)
Create a CasADi Function from the Opti solver.
std::vector< MX > symvar() const
void callback_class(OptiCallback *callback)
std::vector< MX > value_variables() const
get assignment expressions for latest values
MX x_lookup(casadi_int i) const
std::vector< MX > value_parameters() const
void mark_problem_dirty(bool flag=true)
OptiNode(const std::string &problem_type)
Create Opti Context.
std::string return_status() const
Get return status of solver.
std::vector< MX > symvar(const MX &expr, VariableType type) const
void assert_empty() const
void set_initial(const MX &x, const DM &v)
void mark_solver_dirty(bool flag=true)
MX p() const
Get all (scalarised) parameters as a symbolic column vector.
void set_value(const MX &x, const DM &v)
Set value of parameter.
MX lam_g() const
Get dual variables as a symbolic column vector.
MetaCon canon_expr(const MX &expr) const
Interpret an expression (for internal use only)
void set_value(const std::vector< MX > &assignments)
Set value of parameter.
DM value(const SX &x, const std::vector< MX > &values=std::vector< MX >()) const
std::string x_describe(casadi_int i) const
void solver(const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
Solver.
MX f() const
Get objective expression.
void mark_solved(bool flag=true)
Function casadi_solver() const
Get the underlying CasADi solver of the Opti stack.
std::vector< MX > symvar(const MX &expr) const
DMDict solve_actual(const DMDict &args)
Dict stats() const
Get statistics.
void set_meta_con(const MX &m, const MetaCon &meta)
Set meta-data of an expression.
A simplified interface for NLP modeling/solving.
A simplified interface for NLP modeling/solving.
std::map< std::string, MX > MXDict
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::map< std::string, DM > DMDict