List of all members | Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Protected Member Functions | Friends
casadi::OptiNode Class Reference

A simplified interface for NLP modeling/solving. More...

#include <optistack_internal.hpp>

Detailed Description

Date
2017
Author
Joris Gillis, Erik Lambrechts

Extra doc: https://github.com/casadi/casadi/wiki/L_172

Definition at line 55 of file optistack_internal.hpp.

Inheritance diagram for casadi::OptiNode:
Inheritance graph
[legend]
Collaboration diagram for casadi::OptiNode:
Collaboration graph
[legend]

Public Types

using weak_ref_type = WeakRefInternal
 

Public Member Functions

 OptiNode (const std::string &problem_type)
 Create Opti Context. More...
 
 ~OptiNode ()
 Destructor. 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")
 Create a parameter (symbol); fixed during optimization. More...
 
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 (const MX &g, const DM &linear_scale=1, const Dict &options=Dict())
 brief Add constraints More...
 
void subject_to ()
 Clear constraints. More...
 
void solver (const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
 Solver. More...
 
void set_domain (const MX &x, const std::string &domain)
 Set domain of variable. More...
 
void set_linear_scale (const MX &x, const DM &scale, const DM &offset)
 Set scale of a decision variable. More...
 
OptiSol solve (bool accept_limit)
 Crunch the numbers; solve the problem. More...
 
Opti copy () const
 Copy. More...
 
Dict stats () const
 Get statistics. More...
 
std::string return_status () const
 Get return status of solver. More...
 
bool return_success (bool accept_limit) const
 Did the solver return successfully? More...
 
Function casadi_solver () const
 Get the underlying CasADi solver of the Opti stack. More...
 
Function scale_helper (const Function &h) const
 Scale a helper function constructed via opti.x, opti.g, ... More...
 
std::vector< MXinitial () const
 get assignment expressions for initial values More...
 
std::vector< MXvalue_variables () const
 get assignment expressions for latest values More...
 
std::vector< MXvalue_parameters () const
 
void callback_class (OptiCallback *callback)
 
void callback_class ()
 
bool has_callback_class () const
 
bool is_parametric (const MX &expr) const
 return true if expression is only dependant on Opti parameters, not variables More...
 
MetaCon canon_expr (const MX &expr, const DM &linear_scale=1) const
 Interpret an expression (for internal use only) More...
 
MetaVar get_meta (const MX &m) const
 Get meta-data of symbol (for internal use only) More...
 
MetaCon get_meta_con (const MX &m) const
 Get meta-data of symbol (for internal use only) More...
 
void set_meta (const MX &m, const MetaVar &meta)
 Set meta-data of an expression. More...
 
void set_meta_con (const MX &m, const MetaCon &meta)
 Set meta-data of an expression. More...
 
void update_user_dict (const MX &m, const Dict &meta)
 add meta-data of an expression More...
 
Dict user_dict (const MX &m) const
 
MX dual (const MX &m) const
 get the dual variable More...
 
void assert_active_symbol (const MX &m) const
 
std::vector< MXsymvar (VariableType type) const
 
std::vector< MXactive_symvar (VariableType type) const
 
std::vector< DMactive_values (VariableType type) const
 
std::vector< DMactive_values (VariableType type, const std::map< VariableType, std::vector< DM > > &store) const
 
MX x_lookup (casadi_int i) const
 
MX g_lookup (casadi_int i) const
 
std::string x_describe (casadi_int i, const Dict &opts=Dict()) const
 
std::string g_describe (casadi_int i, const Dict &opts=Dict()) const
 
std::string describe (const MX &x, casadi_int indent=0, const Dict &opts=Dict()) const
 
void solve_prepare ()
 
Function solver_construct (bool callback=true)
 
DMDict solve_actual (const DMDict &args)
 
DMDict arg () const
 
void res (const DMDict &res)
 
DMDict res () const
 
std::vector< MXconstraints () const
 
MX objective () const
 
OptiAdvanced baked_copy () const
 
std::string class_name () const override
 Readable name of the internal class. 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
 
MX ubg () const
 
MX lam_g () const
 Get dual variables as a symbolic column vector. More...
 
DM x_linear_scale () const
 
DM x_linear_scale_offset () const
 
DM g_linear_scale () const
 
double f_linear_scale () const
 
void assert_empty () const
 
void show_infeasibilities (double tol=0, const Dict &opts=Dict()) 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. More...
 
void disp (std::ostream &stream, bool more=false) const override
 Print representation. More...
 
void bake ()
 Fix the structure of the optimization problem. More...
 
casadi_int instance_number () const
 
void mark_problem_dirty (bool flag=true)
 
bool problem_dirty () const
 
void mark_solver_dirty (bool flag=true)
 
bool solver_dirty () const
 
void mark_solved (bool flag=true)
 
bool solved () const
 
void assert_solved () const
 
void assert_baked () const
 
casadi_int g_index_reduce_g (casadi_int i) const
 
casadi_int g_index_reduce_x (casadi_int i) const
 
casadi_int g_index_unreduce_g (casadi_int i) const
 
casadi_int getCount () const
 Get the reference count. More...
 
std::string debug_repr (const SharedObjectInternal *) const
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. 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...
 
DM value (const MX &x, const std::vector< MX > &values=std::vector< MX >(), bool scaled=false) const
 
DM value (const DM &x, const std::vector< MX > &values=std::vector< MX >(), bool scaled=false) const
 
DM value (const SX &x, const std::vector< MX > &values=std::vector< MX >(), bool scaled=false) const
 
std::vector< MXsymvar () const
 
std::vector< MXsymvar (const MX &expr) const
 
std::vector< MXsymvar (const MX &expr, VariableType type) const
 

Static Public Member Functions

static OptiNodecreate (const std::string &problem_type)
 

Public Attributes

bool problem_dirty_
 
bool solver_dirty_
 
bool solved_
 

Protected Member Functions

void initSingleton ()
 
void destroySingleton ()
 
shared_from_this ()
 Get a shared object from the current internal object. More...
 
const B shared_from_this () const
 Get a shared object from the current internal object. More...
 

Friends

class InternalOptiCallback
 

Member Typedef Documentation

◆ weak_ref_type

Definition at line 152 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ OptiNode()

casadi::OptiNode::OptiNode ( const std::string &  problem_type)

Definition at line 256 of file optistack_internal.cpp.

256  :
257  count_(0), count_var_(0), count_par_(0), count_dual_(0) {
258  f_ = 0;
259  f_linear_scale_ = 1;
260  instance_number_ = instance_count_++;
261  user_callback_ = nullptr;
262  store_initial_[OPTI_VAR] = {};
263  store_initial_[OPTI_PAR] = {};
264  store_initial_[OPTI_DUAL_G] = {};
265  store_latest_[OPTI_VAR] = {};
266  store_latest_[OPTI_DUAL_G] = {};
267  casadi_assert(problem_type=="nlp" || problem_type=="conic",
268  "Specified problem type '" + problem_type + "'unknown. "
269  "Choose 'nlp' (default) or 'conic'.");
270  problem_type_ = problem_type;
272  helpers_ = std::make_shared<ValueCache>();
273 }
void mark_problem_dirty(bool flag=true)
@ OPTI_VAR
Definition: optistack.hpp:496
@ OPTI_DUAL_G
Definition: optistack.hpp:498
@ OPTI_PAR
Definition: optistack.hpp:497

References mark_problem_dirty(), casadi::OPTI_DUAL_G, casadi::OPTI_PAR, and casadi::OPTI_VAR.

Referenced by copy(), and create().

◆ ~OptiNode()

casadi::OptiNode::~OptiNode ( )

Definition at line 275 of file optistack_internal.cpp.

275  {
276 }

Member Function Documentation

◆ active_symvar()

std::vector< MX > casadi::OptiNode::active_symvar ( VariableType  type) const

Definition at line 1643 of file optistack_internal.cpp.

1643  {
1644  if (symbol_active_.empty()) return std::vector<MX>{};
1645  std::vector<MX> ret;
1646  for (const auto& s : symbols_) {
1647  if (symbol_active_[meta(s).count] && meta(s).type==type)
1648  ret.push_back(s);
1649  }
1650  return ret;
1651 }

Referenced by bake(), res(), solve_prepare(), to_function(), and x_lookup().

◆ active_values() [1/2]

std::vector< DM > casadi::OptiNode::active_values ( VariableType  type) const

Definition at line 1663 of file optistack_internal.cpp.

1663  {
1664  return active_values(type, store_initial_);
1665 }
std::vector< DM > active_values(VariableType type) const

Referenced by bake(), solve_prepare(), and to_function().

◆ active_values() [2/2]

std::vector< DM > casadi::OptiNode::active_values ( VariableType  type,
const std::map< VariableType, std::vector< DM > > &  store 
) const

Definition at line 1667 of file optistack_internal.cpp.

1668  {
1669  if (symbol_active_.empty()) return std::vector<DM>{};
1670  std::vector<DM> ret;
1671  for (const auto& s : symbols_) {
1672  if (symbol_active_[meta(s).count] && meta(s).type==type) {
1673  ret.push_back(store.at(meta(s).type)[meta(s).i]);
1674  }
1675  }
1676  return ret;
1677 }

◆ arg()

DMDict casadi::OptiNode::arg ( ) const
inline

Definition at line 210 of file optistack_internal.hpp.

210 { return arg_; }

Referenced by scale_helper(), solve_actual(), solve_prepare(), to_function(), and value().

◆ assert_active_symbol()

void casadi::OptiNode::assert_active_symbol ( const MX m) const

Definition at line 1485 of file optistack_internal.cpp.

1485  {
1486  assert_has(m);
1487  assert_baked();
1488  casadi_assert(symbol_active_[meta(m).count], "Opti symbol is not used in Solver."
1489  " It does not make sense to assign a value to it:\n" + describe(m, 1));
1490 }
std::string describe(const MX &x, casadi_int indent=0, const Dict &opts=Dict()) const

References assert_baked(), and describe().

◆ assert_baked()

void casadi::OptiNode::assert_baked ( ) const

Definition at line 1177 of file optistack_internal.cpp.

1177  {
1178  casadi_assert(!problem_dirty(),
1179  "This action is forbidden since you have not baked the Opti stack yet "
1180  "(with calling 'solve').");
1181 }
bool problem_dirty() const

References problem_dirty().

Referenced by assert_active_symbol().

◆ assert_empty()

void casadi::OptiNode::assert_empty ( ) const

Definition at line 1183 of file optistack_internal.cpp.

1183  {
1184  casadi_assert_dev(g_.empty());
1185  casadi_assert_dev(f_.is_empty());
1186 }
bool is_empty(bool both=false) const
Check if the sparsity is empty, i.e. if one of the dimensions is zero.

References casadi::GenericMatrix< MatType >::is_empty().

◆ assert_solved()

void casadi::OptiNode::assert_solved ( ) const

Definition at line 1171 of file optistack_internal.cpp.

1171  {
1172  casadi_assert(solved(),
1173  "This action is forbidden since you have not solved the Opti stack yet "
1174  "(with calling 'solve').");
1175 }

References solved().

Referenced by stats(), and value().

◆ bake()

void casadi::OptiNode::bake ( )

Definition at line 729 of file optistack_internal.cpp.

729  {
730  casadi_assert(!f_.is_empty() || !g_.empty(),
731  "You need to specify at least an objective (y calling 'minimize'), "
732  "or a constraint (by calling 'subject_to').");
733 
734  symbol_active_.clear();
735  symbol_active_.resize(symbols_.size());
736  helpers_ = std::make_shared<ValueCache>();
737 
738  // Gather all expressions
739  MX total_expr = vertcat(f_, veccat(g_));
740 
741  // Categorize the symbols appearing in those expressions
742  for (const auto& d : symvar(total_expr))
743  symbol_active_[meta(d).count] = true;
744 
745  std::vector<MX> x = active_symvar(OPTI_VAR);
746  for (casadi_int i=0;i<x.size();++i) meta(x[i]).active_i = i;
747 
748  casadi_int offset = 0;
749  for (const auto& v : x) {
750  meta(v).start = offset;
751  offset+= v.nnz();
752  meta(v).stop = offset;
753  }
754  std::vector<MX> p = active_symvar(OPTI_PAR);
755  for (casadi_int i=0;i<p.size();++i) meta(p[i]).active_i = i;
756 
757  // Fill the nlp definition
758  nlp_["x"] = veccat(x);
759  nlp_["p"] = veccat(p);
760 
761  nlp_unscaled_["x"] = veccat(x);
762  nlp_unscaled_["p"] = veccat(p);
763 
764  discrete_.clear();
765  for (const MX& e : x) {
766  discrete_.insert(discrete_.end(), e.nnz(), meta(e).domain == OPTI_DOMAIN_INTEGER);
767  }
768 
769  nlp_["f"] = f_;
770  nlp_unscaled_["f"] = f_;
771 
772  offset = 0;
773  for (casadi_int i=0;i<g_.size();++i) {
774  MetaCon& r = meta_con(g_[i]);
775  MetaVar& r2 = meta(r.dual_canon);
776  symbol_active_[r2.count] = true;
777 
778  // Compute offsets for this constraint:
779  // location into the global constraint variable
780  r.start = offset;
781  offset+= r.canon.nnz();
782  r.stop = offset;
783 
784  r2.start = r.start;
785  r2.stop = r.stop;
786 
787  }
788  index_all_to_g_.resize(offset);
789  offset = 0;
790  casadi_int offset_g = 0;
791  for (const auto& g : g_) {
792  const MetaCon& r = meta_con(g);
793  if (r.type==OPTI_PSD) {
794  for (casadi_int i=0;i<r.stop-r.start;++i) {
795  index_all_to_g_[r.start+i] = -1;
796  }
797  } else {
798  for (casadi_int i=0;i<r.stop-r.start;++i) {
799  index_all_to_g_[r.start+i] = offset_g+i;
800  }
801  offset_g += r.canon.nnz();
802  }
803  }
804 
805  std::vector<MX> lam = active_symvar(OPTI_DUAL_G);
806  for (casadi_int i=0;i<lam.size();++i) meta(lam[i]).active_i = i;
807 
808  lam_ = veccat(lam);
809 
810  // Collect bounds and canonical form of constraints
811  std::vector<MX> g_all, g_unscaled_all;
812  std::vector<MX> h_all, h_unscaled_all;
813  std::vector<MX> lbg_all, lbg_unscaled_all;
814  std::vector<MX> ubg_all, ubg_unscaled_all;
815 
816  g_linear_scale_.clear();
817  h_linear_scale_.clear();
818  std::vector<DM> g_linear_scale, h_linear_scale;
819 
820  equality_.clear();
821  for (const auto& g : g_) {
822  if (meta_con(g).type==OPTI_PSD) {
823  h_all.push_back(meta_con(g).canon/meta_con(g).linear_scale);
824  if (meta_con(g).canon.numel()==meta_con(g).linear_scale.numel()) {
825  h_linear_scale.push_back(meta_con(g).linear_scale);
826  } else {
827  casadi_assert_dev(meta_con(g).linear_scale.numel()==1);
828  h_linear_scale.push_back(DM::ones(meta_con(g).canon.sparsity())*meta_con(g).linear_scale);
829  }
830  h_unscaled_all.push_back(meta_con(g).canon);
831  } else {
832  g_all.push_back(meta_con(g).canon/meta_con(g).linear_scale);
833  if (meta_con(g).canon.numel()==meta_con(g).linear_scale.numel()) {
834  g_linear_scale.push_back(meta_con(g).linear_scale);
835  } else {
836  casadi_assert_dev(meta_con(g).linear_scale.numel()==1);
837  g_linear_scale.push_back(DM::ones(meta_con(g).canon.sparsity())*meta_con(g).linear_scale);
838  }
839  g_unscaled_all.push_back(meta_con(g).canon);
840  lbg_all.push_back(meta_con(g).lb/meta_con(g).linear_scale);
841  lbg_unscaled_all.push_back(meta_con(g).lb);
842  ubg_all.push_back(meta_con(g).ub/meta_con(g).linear_scale);
843  ubg_unscaled_all.push_back(meta_con(g).ub);
844 
845  equality_.insert(equality_.end(),
846  meta_con(g).canon.numel(),
847  meta_con(g).type==OPTI_EQUALITY || meta_con(g).type==OPTI_GENERIC_EQUALITY);
848  }
849  }
850 
851  nlp_["g"] = veccat(g_all);
852  g_linear_scale_ = veccat(g_linear_scale).nonzeros();
853  nlp_unscaled_["g"] = veccat(g_unscaled_all);
854  if (problem_type_=="conic") {
855  nlp_["h"] = diagcat(h_all);
856  nlp_unscaled_["h"] = diagcat(h_unscaled_all);
857  h_linear_scale_ = veccat(h_linear_scale).nonzeros();
858  }
859 
860  // Get scaling data
861  std::vector<DM> linear_scale = active_values(OPTI_VAR, store_linear_scale_);
862  std::vector<DM> linear_scale_offset = active_values(OPTI_VAR, store_linear_scale_offset_);
863 
864  linear_scale_ = veccat(linear_scale).nonzeros();
865  linear_scale_offset_ = veccat(linear_scale_offset).nonzeros();
866 
867  // Unscaled version of x
868  std::vector<MX> x_unscaled(x.size());
869  for (casadi_int i=0;i<x.size();++i) {
870  x_unscaled[i] = x[i]*linear_scale[i] + linear_scale_offset[i];
871  }
872 
873  // Perform substitution
874  std::vector<MX> expr = {nlp_["f"], nlp_["g"]};
875  if (problem_type_=="conic") expr.push_back(nlp_["h"]);
876  std::vector<MX> fgh = substitute(expr, x, x_unscaled);
877  nlp_["f"] = fgh[0];
878  nlp_["g"] = fgh[1];
879  if (problem_type_=="conic") {
880  nlp_["h"] = fgh[2];
881  }
882 
883  // Scale of objective
884  nlp_["f"] = nlp_["f"]/f_linear_scale_;
885 
886  // Create bounds helper function
887  MXDict bounds;
888  bounds["p"] = nlp_["p"];
889  bounds_lbg_ = veccat(lbg_all);
890  bounds_ubg_ = veccat(ubg_all);
891  bounds_unscaled_lbg_ = veccat(lbg_unscaled_all);
892  bounds_unscaled_ubg_ = veccat(ubg_unscaled_all);
893 
894  bounds["lbg"] = bounds_lbg_;
895  bounds["ubg"] = bounds_ubg_;
896 
897  bounds_ = Function("bounds", bounds, {"p"}, {"lbg", "ubg"});
898  mark_problem_dirty(false);
899 }
std::pair< casadi_int, casadi_int > size() const
Get the shape.
static MatType ones(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries one.
std::vector< MX > active_symvar(VariableType type) const
MX g() const
Get all (scalarised) constraint expressions as a column vector.
MX x() const
Get all (scalarised) decision variables as a symbolic column vector.
std::vector< MX > symvar() const
MX p() const
Get all (scalarised) parameters as a symbolic column vector.
std::map< std::string, MX > MXDict
Definition: mx.hpp:1110
@ OPTI_DOMAIN_INTEGER
Definition: optistack.hpp:502
@ OPTI_EQUALITY
Definition: optistack.hpp:490
@ OPTI_GENERIC_EQUALITY
Definition: optistack.hpp:488
@ OPTI_PSD
Definition: optistack.hpp:493
casadi_int active_i
Definition: optistack.hpp:533
casadi_int count
Definition: optistack.hpp:531

References casadi::MetaVar::active_i, active_symvar(), active_values(), casadi::MetaCon::canon, casadi::MetaVar::count, casadi::MetaCon::dual_canon, g(), g_linear_scale(), casadi::GenericMatrix< MatType >::is_empty(), mark_problem_dirty(), casadi::GenericMatrix< MatType >::nnz(), casadi::GenericMatrix< MatType >::ones(), casadi::OPTI_DOMAIN_INTEGER, casadi::OPTI_DUAL_G, casadi::OPTI_EQUALITY, casadi::OPTI_GENERIC_EQUALITY, casadi::OPTI_PAR, casadi::OPTI_PSD, casadi::OPTI_VAR, p(), casadi::GenericMatrix< MatType >::size(), casadi::IndexAbstraction::start, casadi::IndexAbstraction::stop, symvar(), casadi::MetaCon::type, and x().

Referenced by solve(), and solver_construct().

◆ baked_copy()

OptiAdvanced casadi::OptiNode::baked_copy ( ) const
inline

Definition at line 216 of file optistack_internal.hpp.

216  {
217  OptiAdvanced s = copy();
218  if (s.problem_dirty()) s.bake();
219  return s;
220  }
Opti copy() const
Copy.

References casadi::OptiAdvanced::bake(), casadi::copy(), and casadi::OptiAdvanced::problem_dirty().

Referenced by casadi::OptiAdvanced::baked_copy(), describe(), g_describe(), g_lookup(), scale_helper(), to_function(), x_describe(), and x_lookup().

◆ callback_class() [1/2]

void casadi::OptiNode::callback_class ( )

Definition at line 104 of file optistack_internal.cpp.

104  {
105  user_callback_ = nullptr;
106 }

◆ callback_class() [2/2]

void casadi::OptiNode::callback_class ( OptiCallback callback)

Definition at line 100 of file optistack_internal.cpp.

100  {
101  user_callback_ = callback;
102 }

◆ canon_expr()

MetaCon casadi::OptiNode::canon_expr ( const MX expr,
const DM linear_scale = 1 
) const

Definition at line 1037 of file optistack_internal.cpp.

1037  {
1038  MX c = expr;
1039 
1040  MetaCon con;
1041  con.original = expr;
1042  casadi_assert(linear_scale.is_scalar() || linear_scale.size()==expr.size(),
1043  "Linear scale must have the same size as the expression. "
1044  "You got linear_scale " + con.linear_scale.dim() + " while " + expr.dim() + " is expected.");
1045  con.linear_scale = linear_scale;
1046 
1047  if (c.is_op(OP_LE) || c.is_op(OP_LT)) { // Inequalities
1048  std::vector<MX> ret;
1049  bool flipped;
1050  std::vector<MX> args = ineq_unchain(c, flipped);
1051  std::vector<bool> parametric;
1052  for (auto &a : args) parametric.push_back(is_parametric(a));
1053 
1054  if (args.size()==2 && (parametric[0] || parametric[1])) {
1055  // case: g(x,p) <= bound(p)
1056  MX e = args[0]-args[1];
1057  if (e.is_vector()) {
1058  casadi_assert(!parametric[0] || !parametric[1],
1059  "Constraint must contain decision variables.");
1060  if (problem_type_=="conic") {
1061  if (args[0].op()==OP_NORMF || args[0].op()==OP_NORM2) {
1062  args[0] = -soc(args[0].dep(), args[1]);
1063  args[1] = 0;
1064  }
1065  } else {
1066  con.type = OPTI_INEQUALITY;
1067  if (parametric[0]) {
1068  con.lb = args[0]*DM::ones(e.sparsity());
1069  con.ub = inf*DM::ones(e.sparsity());
1070  con.canon = args[1]*DM::ones(e.sparsity());
1071  } else {
1072  con.lb = -inf*DM::ones(e.sparsity());
1073  con.ub = args[1]*DM::ones(e.sparsity());
1074  con.canon = args[0]*DM::ones(e.sparsity());
1075  }
1076  return con;
1077  }
1078  }
1079  // Fall through to generic inequalities
1080  } else if (args.size()==3 && parametric[0] && parametric[2]) {
1081  // lb(p) <= g(x,p) <= ub(p)
1082  con.type = OPTI_DOUBLE_INEQUALITY;
1083  con.lb = args[0]*DM::ones(args[1].sparsity());
1084  con.ub = args[2]*DM::ones(args[1].sparsity());
1085  con.canon = args[1]*DM::ones(args[1].sparsity());
1086  con.flipped = flipped;
1087  con.n = 2;
1088  return con;
1089  }
1090 
1091  bool type_known = false;
1092  for (casadi_int j=0;j<args.size()-1;++j) {
1093  MX e = args[j]-args[j+1];
1094  if (problem_type_=="conic") {
1095  if (args[j].op()==OP_NORMF || args[j].op()==OP_NORM2) {
1096  args[j] = -soc(args[j].dep(), args[j+1]);
1097  args[j+1] = 0;
1098  e = args[j]-args[j+1];
1099  }
1100  }
1101  if (e.is_vector()) {
1102  // g1(x,p) <= g2(x,p)
1103  ret.push_back(e);
1104  casadi_assert_dev(!type_known || con.type==OPTI_GENERIC_INEQUALITY);
1105  type_known = true;
1106  con.type = OPTI_GENERIC_INEQUALITY;
1107  con.flipped = flipped;
1108  } else {
1109  // A(x,p) >= b(p)
1110  MX a = args[j+1];
1111  MX b = args[j];
1112  e = a-b;
1113 
1114  casadi_assert(e.size1()==e.size2(),
1115  "Matrix inequalities must be square. Did you mean element-wise inequality instead?");
1116  if (a.is_scalar()) a*= MX::eye(e.size1());
1117  if (b.is_scalar()) b*= MX::eye(e.size1());
1118  e = a-b;
1119 
1120  ret.push_back(e);
1121  casadi_assert_dev(!type_known || con.type==OPTI_PSD);
1122  type_known = true;
1123  con.type = OPTI_PSD;
1124  }
1125  }
1126 
1127  if (con.type==OPTI_GENERIC_INEQUALITY) {
1128  con.canon = veccat(ret);
1129  con.lb = -inf*DM::ones(con.canon.sparsity());
1130  con.ub = DM::zeros(con.canon.sparsity());
1131  con.n = ret.size();
1132  if (!con.linear_scale.is_scalar()) {
1133  con.linear_scale = repmat(con.linear_scale, ret.size(), 1);
1134  }
1135  } else {
1136  con.canon = diagcat(ret);
1137  con.n = ret.size();
1138  }
1139  return con;
1140  } else if (c.is_op(OP_EQ)) { // Inequalities
1141  casadi_assert(!is_parametric(c.dep(0)) || !is_parametric(c.dep(1)),
1142  "Constraint must contain decision variables.");
1143  MX e = c.dep(0)-c.dep(1);
1144  if (is_parametric(c.dep(0))) {
1145  con.canon = c.dep(1)*DM::ones(e.sparsity());
1146  con.lb = c.dep(0)*DM::ones(e.sparsity());
1147  con.type = OPTI_EQUALITY;
1148  casadi_assert(c.dep(0).size1()<=c.dep(1).size1() && c.dep(0).size2()<=c.dep(1).size2(),
1149  "Constraint shape mismatch.");
1150  } else if (is_parametric(c.dep(1))) {
1151  con.canon = c.dep(0)*DM::ones(e.sparsity());
1152  con.lb = c.dep(1)*DM::ones(e.sparsity());
1153  con.type = OPTI_EQUALITY;
1154  casadi_assert(c.dep(1).size1()<=c.dep(0).size1() && c.dep(1).size2()<=c.dep(0).size2(),
1155  "Constraint shape mismatch.");
1156  } else {
1157  con.lb = DM::zeros(e.sparsity());
1158  con.canon = e;
1159  con.type = OPTI_GENERIC_EQUALITY;
1160  }
1161  con.ub = con.lb;
1162  return con;
1163  } else { // Something else
1164  con.type = OPTI_UNKNOWN;
1165  con.canon = c;
1166  return con;
1167  }
1168 
1169 }
static MatType zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
static MX eye(casadi_int n)
Identity matrix.
Definition: mx.cpp:600
bool is_parametric(const MX &expr) const
return true if expression is only dependant on Opti parameters, not variables
const double inf
infinity
Definition: calculus.hpp:50
@ OPTI_DOUBLE_INEQUALITY
Definition: optistack.hpp:492
@ OPTI_INEQUALITY
Definition: optistack.hpp:491
@ OPTI_GENERIC_INEQUALITY
Definition: optistack.hpp:489
@ OPTI_UNKNOWN
Definition: optistack.hpp:494
@ OP_LT
Definition: calculus.hpp:70
@ OP_EQ
Definition: calculus.hpp:70
@ OP_NORM2
Definition: calculus.hpp:178
@ OP_LE
Definition: calculus.hpp:70
@ OP_NORMF
Definition: calculus.hpp:178

References casadi::MetaCon::canon, casadi::MX::dep(), casadi::GenericMatrix< MatType >::dim(), casadi::MX::eye(), casadi::MetaCon::flipped, casadi::inf, casadi::MX::is_op(), is_parametric(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::GenericMatrix< MatType >::is_vector(), casadi::MetaCon::lb, casadi::MetaCon::linear_scale, casadi::MetaCon::n, casadi::GenericMatrix< MatType >::ones(), casadi::OP_EQ, casadi::OP_LE, casadi::OP_LT, casadi::OP_NORM2, casadi::OP_NORMF, casadi::OPTI_DOUBLE_INEQUALITY, casadi::OPTI_EQUALITY, casadi::OPTI_GENERIC_EQUALITY, casadi::OPTI_GENERIC_INEQUALITY, casadi::OPTI_INEQUALITY, casadi::OPTI_PSD, casadi::OPTI_UNKNOWN, casadi::MetaCon::original, casadi::GenericMatrix< MatType >::size(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), casadi::MX::sparsity(), casadi::MetaCon::type, casadi::MetaCon::ub, and casadi::GenericMatrix< MatType >::zeros().

Referenced by subject_to().

◆ casadi_solver()

Function casadi::OptiNode::casadi_solver ( ) const

Definition at line 564 of file optistack_internal.cpp.

564  {
565  return solver_;
566 }

◆ class_name()

std::string casadi::OptiNode::class_name ( ) const
inlineoverridevirtual

Implements casadi::SharedObjectInternal.

Definition at line 222 of file optistack_internal.hpp.

222 { return "OptiNode"; }

◆ constraints()

std::vector<MX> casadi::OptiNode::constraints ( ) const
inline

Definition at line 213 of file optistack_internal.hpp.

213 { return g_; }

◆ copy()

Opti casadi::OptiNode::copy ( ) const

Definition at line 365 of file optistack_internal.cpp.

365  {
366  return Opti::create(new OptiNode(*this));
367 }
OptiNode(const std::string &problem_type)
Create Opti Context.
static Opti create(OptiNode *node)
Definition: optistack.cpp:86

References casadi::Opti::create(), and OptiNode().

Referenced by casadi::Opti::copy(), and solve().

◆ create()

OptiNode * casadi::OptiNode::create ( const std::string &  problem_type)
static

Definition at line 95 of file optistack_internal.cpp.

95  {
96 return new OptiNode(problem_type);
97 }

References OptiNode().

Referenced by casadi::Opti::Opti().

◆ debug_repr()

std::string casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::debug_repr ( const Internal *  i) const
inherited

Definition at line 62 of file generic_shared_internal.hpp.

175  {
176  // Note: i != this because of something something multiple inheritance
177  return str( (casadi_int)(i)) + "/" + static_cast<const Internal*>(this)->class_name();
178  }
std::string str(const T &v)
String representation, any type.

◆ describe()

std::string casadi::OptiNode::describe ( const MX x,
casadi_int  indent = 0,
const Dict opts = Dict() 
) const

Definition at line 141 of file optistack_internal.cpp.

141  {
142  if (problem_dirty()) return baked_copy().describe(expr, indent, opts);
143  casadi_int max_stacktrace_depth = 1;
144  for (const auto& op : opts) {
145  if (op.first=="max_stacktrace_depth") {
146  max_stacktrace_depth = op.second.as_int();
147  } else {
148  casadi_warning("Unknown option '" + op.first + "'");
149  }
150  }
151  std::string s_indent;
152  for (casadi_int i=0;i<indent;++i) {
153  s_indent+= " ";
154  }
155  std::string description = s_indent;
156  if (expr.is_symbolic()) {
157  if (has(expr)) {
158  description += "Opti " + variable_type_to_string(meta(expr).type) + " '" + expr.name() +
159  "' of shape " + expr.dim();
160  const Dict& extra = meta(expr).extra;
161  auto it = extra.find("stacktrace");
162  if (it!=extra.end()) {
163  for (const Dict& stacktrace : it->second.as_dict_vector()) {
164  description += ", " + format_stacktrace(stacktrace, indent+1);
165  if (--max_stacktrace_depth==0) break;
166  }
167  }
168  } else {
169  VariableType vt;
170  if (parse_opti_name(expr.name(), vt)) {
171  description += "Opti " + variable_type_to_string(vt) + " '" + expr.name() +
172  "' of shape " + expr.dim()+
173  ", belonging to a different instance of Opti.";
174  } else {
175  description += "MX symbol '" + expr.name() + "' of shape " + expr.dim();
176  description += ", declared outside of Opti.";
177  }
178  }
179  } else {
180  if (has_con(expr)) {
181  description = "Opti constraint of shape " + expr.dim();
182  const Dict& extra = meta_con(expr).extra;
183  auto it = extra.find("stacktrace");
184  if (it!=extra.end()) {
185  for (const Dict& stacktrace : it->second.as_dict_vector()) {
186  description += ", " + format_stacktrace(stacktrace, indent+1);
187  if (--max_stacktrace_depth==0) break;
188  }
189  }
190  } else {
191  std::vector<MX> s = symvar(expr);
192  if (s.empty()) {
193  description+= "Constant epxression.";
194  } else {
195  description+= "General expression, dependent on " + str(s.size()) + " symbols:";
196  for (casadi_int i=0;i<s.size();++i) {
197  description+= "\n"+describe(s[i], indent+1);
198  if (i>5) {
199  description+= "\n...";
200  break;
201  }
202  }
203  }
204  }
205  }
206 
207  return description;
208 }
std::string describe(const MX &x, casadi_index indent=0, const Dict &opts=Dict()) const
Definition: optistack.cpp:663
OptiAdvanced baked_copy() const
std::string description(Category v)
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.

References baked_copy(), casadi::OptiAdvanced::describe(), casadi::description(), casadi::GenericMatrix< MatType >::dim(), casadi::MetaCon::extra, casadi::MetaVar::extra, casadi::MX::is_symbolic(), casadi::MX::name(), problem_dirty(), casadi::str(), and symvar().

Referenced by assert_active_symbol(), g_describe(), solve_prepare(), value(), and x_describe().

◆ destroySingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::destroySingleton ( )
inlineprotectedinherited

Called in the destructor of singletons

Definition at line 77 of file generic_shared_internal.hpp.

77  {
78  static_cast<Internal*>(this)->count--;
79  }

◆ disp()

void casadi::OptiNode::disp ( std::ostream &  stream,
bool  more = false 
) const
overridevirtual

Implements casadi::SharedObjectInternal.

Definition at line 1735 of file optistack_internal.cpp.

1735  {
1736 
1737 }

◆ dual()

MX casadi::OptiNode::dual ( const MX m) const

Definition at line 606 of file optistack_internal.cpp.

606  {
607  return meta_con(m).dual;
608 }

References casadi::MetaCon::dual.

◆ f()

MX casadi::OptiNode::f ( ) const
inline

Definition at line 261 of file optistack_internal.hpp.

261  {
262  if (problem_dirty()) return baked_copy().f();
263  return nlp_unscaled_.at("f");
264  }
MX f() const
Get objective expression.
Definition: optistack.cpp:358

Referenced by minimize().

◆ f_linear_scale()

double casadi::OptiNode::f_linear_scale ( ) const
inline

Definition at line 294 of file optistack_internal.hpp.

294  {
295  if (problem_dirty()) return baked_copy().f_linear_scale();
296  return f_linear_scale_;
297  }
double f_linear_scale() const
Definition: optistack.cpp:415

Referenced by scale_helper().

◆ g()

MX casadi::OptiNode::g ( ) const
inline

Definition at line 255 of file optistack_internal.hpp.

255  {
256  if (problem_dirty()) return baked_copy().g();
257  return nlp_unscaled_.at("g");
258  }
MX g() const
Get all (scalarised) constraint expressions as a column vector.
Definition: optistack.cpp:350

Referenced by bake(), show_infeasibilities(), solve_prepare(), solver_construct(), and subject_to().

◆ g_describe()

std::string casadi::OptiNode::g_describe ( casadi_int  i,
const Dict opts = Dict() 
) const

Definition at line 210 of file optistack_internal.cpp.

210  {
211  if (problem_dirty()) return baked_copy().g_describe(i, opts);
212  MX expr = g_lookup(i);
213  casadi_int local_i = i-meta_con(expr).start + GlobalOptions::start_index;
214  std::string description = describe(expr, 0, opts);
215  if (expr.numel()>1)
216  description += "\nAt nonzero " + str(local_i % expr.numel()) +
217  ", part " + str((casadi_int) local_i / expr.numel()) + ".";
218  return description;
219 }
static casadi_int start_index
std::string g_describe(casadi_index i, const Dict &opts=Dict()) const
Definition: optistack.cpp:656
MX g_lookup(casadi_int i) const

References baked_copy(), describe(), casadi::description(), casadi::OptiAdvanced::g_describe(), g_lookup(), casadi::GenericMatrix< MatType >::numel(), problem_dirty(), casadi::IndexAbstraction::start, casadi::GlobalOptions::start_index, and casadi::str().

Referenced by show_infeasibilities().

◆ g_index_reduce_g()

casadi_int casadi::OptiNode::g_index_reduce_g ( casadi_int  i) const

Definition at line 519 of file optistack_internal.cpp.

519  {
520  stats();
521  return g_index_reduce_g_[i];
522 }
Dict stats() const
Get statistics.

References stats().

◆ g_index_reduce_x()

casadi_int casadi::OptiNode::g_index_reduce_x ( casadi_int  i) const

Definition at line 527 of file optistack_internal.cpp.

527  {
528  stats();
529  return g_index_reduce_x_[i];
530 }

References stats().

◆ g_index_unreduce_g()

casadi_int casadi::OptiNode::g_index_unreduce_g ( casadi_int  i) const

Definition at line 523 of file optistack_internal.cpp.

523  {
524  stats();
525  return g_index_unreduce_g_[i];
526 }

References stats().

◆ g_linear_scale()

DM casadi::OptiNode::g_linear_scale ( ) const
inline

Definition at line 290 of file optistack_internal.hpp.

290  {
291  if (problem_dirty()) return baked_copy().g_linear_scale();
292  return DM(g_linear_scale_);
293  }
DM g_linear_scale() const
Definition: optistack.cpp:407
Matrix< double > DM
Definition: dm_fwd.hpp:33

Referenced by bake(), and scale_helper().

◆ g_lookup()

MX casadi::OptiNode::g_lookup ( casadi_int  i) const

Definition at line 244 of file optistack_internal.cpp.

244  {
245  if (problem_dirty()) return baked_copy().g_lookup(i);
246  casadi_assert_dev(i>=0);
247  casadi_assert_dev(i<ng());
248  for (const auto& e : g_) {
249  const MetaCon& m = meta_con(e);
250  if (i>=m.start && i<m.stop) return e;
251  }
252  casadi_error("Internal error");
253  return MX();
254 }
MX g_lookup(casadi_index i) const
Definition: optistack.cpp:641
casadi_int ng() const
Number of (scalarised) constraints.

References baked_copy(), casadi::OptiAdvanced::g_lookup(), ng(), problem_dirty(), casadi::IndexAbstraction::start, and casadi::IndexAbstraction::stop.

Referenced by g_describe().

◆ get_meta()

MetaVar casadi::OptiNode::get_meta ( const MX m) const

Definition at line 634 of file optistack_internal.cpp.

634  {
635  return meta(m);
636 }

Referenced by update_user_dict(), and user_dict().

◆ get_meta_con()

MetaCon casadi::OptiNode::get_meta_con ( const MX m) const

Definition at line 638 of file optistack_internal.cpp.

638  {
639  return meta_con(m);
640 }

Referenced by update_user_dict(), and user_dict().

◆ getCount()

Definition at line 60 of file generic_shared_internal.hpp.

205  {
206  return static_cast<const Internal*>(this)->count;
207  }

◆ has_callback_class()

bool casadi::OptiNode::has_callback_class ( ) const

Definition at line 108 of file optistack_internal.cpp.

108  {
109  return user_callback_ != 0;
110 }

◆ initial()

std::vector< MX > casadi::OptiNode::initial ( ) const

Definition at line 702 of file optistack_internal.cpp.

702  {
703  std::vector<MX> ret;
704  for (const auto& e : symvar()) {
705  if (meta(e).type==OPTI_VAR || meta(e).type==OPTI_DUAL_G)
706  ret.push_back(e==store_initial_.at(meta(e).type)[meta(e).i]);
707  }
708  return ret;
709 }

References casadi::OPTI_DUAL_G, casadi::OPTI_VAR, and symvar().

◆ initSingleton()

void casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::initSingleton ( )
inlineprotectedinherited

Called in the constructor of singletons to avoid that the counter reaches zero

Definition at line 71 of file generic_shared_internal.hpp.

71  {
72  casadi_assert_dev(static_cast<Internal*>(this)->count==0);
73  static_cast<Internal*>(this)->count++;
74  }

◆ instance_number()

casadi_int casadi::OptiNode::instance_number ( ) const

Definition at line 1739 of file optistack_internal.cpp.

1739  {
1740  return instance_number_;
1741 }

◆ is_parametric()

bool casadi::OptiNode::is_parametric ( const MX expr) const

Definition at line 1033 of file optistack_internal.cpp.

1033  {
1034  return symvar(expr, OPTI_VAR).empty();
1035 }

References casadi::OPTI_VAR, and symvar().

Referenced by canon_expr().

◆ lam_g()

MX casadi::OptiNode::lam_g ( ) const
inline

Definition at line 277 of file optistack_internal.hpp.

277  {
278  if (problem_dirty()) return baked_copy().lam_g();
279  return lam_;
280  }
MX lam_g() const
Get all (scalarised) dual variables as a symbolic column vector.
Definition: optistack.cpp:383

Referenced by to_function().

◆ lbg()

MX casadi::OptiNode::lbg ( ) const
inline

Definition at line 266 of file optistack_internal.hpp.

266  {
267  if (problem_dirty()) return baked_copy().lbg();
268  return bounds_unscaled_lbg_;
269  }
MX lbg() const
Get all (scalarised) bounds on constraints as a column vector.
Definition: optistack.cpp:366

Referenced by show_infeasibilities().

◆ mark_problem_dirty()

void casadi::OptiNode::mark_problem_dirty ( bool  flag = true)
inline

Definition at line 322 of file optistack_internal.hpp.

void mark_solver_dirty(bool flag=true)

Referenced by bake(), minimize(), OptiNode(), set_domain(), and subject_to().

◆ mark_solved()

void casadi::OptiNode::mark_solved ( bool  flag = true)
inline

Definition at line 330 of file optistack_internal.hpp.

330 { solved_ = flag;}

Referenced by res(), and set_domain().

◆ mark_solver_dirty()

void casadi::OptiNode::mark_solver_dirty ( bool  flag = true)
inline

Definition at line 326 of file optistack_internal.hpp.

326 { solver_dirty_=flag; mark_solved(false); }
void mark_solved(bool flag=true)

Referenced by solve(), and solver().

◆ minimize()

void casadi::OptiNode::minimize ( const MX f,
double  linear_scale = 1 
)

Definition at line 1188 of file optistack_internal.cpp.

1188  {
1189  assert_only_opti_nondual(f);
1191  casadi_assert(f.is_scalar(), "Objective must be scalar, got " + f.dim() + ".");
1192  f_ = f;
1193  f_linear_scale_ = linear_scale;
1194 }
std::string dim(bool with_nz=false) const
Get string representation of dimensions.
bool is_scalar(bool scalar_and_dense=false) const
Check if the matrix expression is scalar.
MX f() const
Get objective expression.

References casadi::GenericMatrix< MatType >::dim(), f(), casadi::GenericMatrix< MatType >::is_scalar(), and mark_problem_dirty().

◆ ng()

casadi_int casadi::OptiNode::ng ( ) const
inline

Definition at line 237 of file optistack_internal.hpp.

237  {
238  if (problem_dirty()) return baked_copy().ng();
239  return nlp_.at("g").size1();
240  }
casadi_int ng() const
Number of (scalarised) constraints.
Definition: optistack.cpp:326

Referenced by g_lookup(), scale_helper(), and stats().

◆ np()

casadi_int casadi::OptiNode::np ( ) const
inline

Definition at line 231 of file optistack_internal.hpp.

231  {
232  if (problem_dirty()) return baked_copy().np();
233  return nlp_.at("p").size1();
234  }
casadi_int np() const
Number of (scalarised) parameters.
Definition: optistack.cpp:318

Referenced by scale_helper().

◆ nx()

casadi_int casadi::OptiNode::nx ( ) const
inline

Definition at line 225 of file optistack_internal.hpp.

225  {
226  if (problem_dirty()) return baked_copy().nx();
227  return nlp_.at("x").size1();
228  }
casadi_int nx() const
Number of (scalarised) decision variables.
Definition: optistack.cpp:310

Referenced by scale_helper(), and x_lookup().

◆ objective()

MX casadi::OptiNode::objective ( ) const
inline

Definition at line 214 of file optistack_internal.hpp.

214 { return f_; }

◆ p()

MX casadi::OptiNode::p ( ) const
inline

Definition at line 249 of file optistack_internal.hpp.

249  {
250  if (problem_dirty()) return baked_copy().p();
251  return nlp_.at("p");
252  }
MX p() const
Get all (scalarised) parameters as a symbolic column vector.
Definition: optistack.cpp:342

Referenced by bake(), to_function(), and value().

◆ parameter() [1/3]

MX casadi::OptiNode::parameter ( casadi_int  n = 1,
casadi_int  m = 1,
const std::string &  attribute = "full" 
)

Definition at line 463 of file optistack_internal.cpp.

463  {
464  casadi_assert_dev(attribute=="full");
465 
466  // Prepare metadata
467  MetaVar meta_data;
468  meta_data.attribute = attribute;
469  meta_data.n = n;
470  meta_data.m = m;
471  meta_data.type = OPTI_PAR;
472  meta_data.count = count_++;
473  meta_data.i = count_par_++;
474 
475  MX symbol = MX::sym(name_prefix() + "p_" + str(count_par_), n, m);
476  symbols_.push_back(symbol);
477  store_initial_[OPTI_PAR].push_back(DM::nan(symbol.sparsity()));
478 
479  set_meta(symbol, meta_data);
480  return symbol;
481 }
static MX sym(const std::string &name, casadi_int nrow=1, casadi_int ncol=1)
Create an nrow-by-ncol symbolic primitive.
static Matrix< double > nan(const Sparsity &sp)
create a matrix with all nan
void set_meta(const MX &m, const MetaVar &meta)
Set meta-data of an expression.

References casadi::MetaVar::attribute, casadi::MetaVar::count, casadi::MetaVar::i, casadi::MetaVar::m, casadi::MetaVar::n, casadi::Matrix< double >::nan(), casadi::OPTI_PAR, set_meta(), casadi::MX::sparsity(), casadi::str(), casadi::GenericMatrix< MX >::sym(), and casadi::MetaVar::type.

◆ parameter() [2/3]

MX casadi::OptiNode::parameter ( const MX symbol,
const std::string &  attribute = "full" 
)

Definition at line 424 of file optistack_internal.cpp.

424  {
425  casadi_assert_dev(attribute=="full");
426 
427  // Prepare metadata
428  MetaVar meta_data;
429  meta_data.attribute = attribute;
430  meta_data.n = symbol.size1();
431  meta_data.m = symbol.size2();
432  meta_data.type = OPTI_PAR;
433  meta_data.count = count_++;
434  meta_data.i = count_par_++;
435 
436  symbols_.push_back(symbol);
437  store_initial_[OPTI_PAR].push_back(DM::nan(symbol.sparsity()));
438 
439  set_meta(symbol, meta_data);
440  return symbol;
441 }

References casadi::MetaVar::attribute, casadi::MetaVar::count, casadi::MetaVar::i, casadi::MetaVar::m, casadi::MetaVar::n, casadi::Matrix< double >::nan(), casadi::OPTI_PAR, set_meta(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), casadi::MX::sparsity(), and casadi::MetaVar::type.

◆ parameter() [3/3]

MX casadi::OptiNode::parameter ( const Sparsity sp,
const std::string &  attribute = "full" 
)

Definition at line 443 of file optistack_internal.cpp.

443  {
444  casadi_assert_dev(attribute=="full");
445 
446  // Prepare metadata
447  MetaVar meta_data;
448  meta_data.attribute = attribute;
449  meta_data.n = sp.size1();
450  meta_data.m = sp.size2();
451  meta_data.type = OPTI_PAR;
452  meta_data.count = count_++;
453  meta_data.i = count_par_++;
454 
455  MX symbol = MX::sym(name_prefix() + "p_" + str(count_par_), sp);
456  symbols_.push_back(symbol);
457  store_initial_[OPTI_PAR].push_back(DM::nan(symbol.sparsity()));
458 
459  set_meta(symbol, meta_data);
460  return symbol;
461 }

References casadi::MetaVar::attribute, casadi::MetaVar::count, casadi::MetaVar::i, casadi::MetaVar::m, casadi::MetaVar::n, casadi::Matrix< double >::nan(), casadi::OPTI_PAR, set_meta(), casadi::Sparsity::size1(), casadi::Sparsity::size2(), casadi::MX::sparsity(), casadi::str(), casadi::GenericMatrix< MX >::sym(), and casadi::MetaVar::type.

◆ problem_dirty()

bool casadi::OptiNode::problem_dirty ( ) const
inline

◆ res() [1/2]

DMDict casadi::OptiNode::res ( ) const
inline

Definition at line 212 of file optistack_internal.hpp.

212 { return res_; }

Referenced by res(), scale_helper(), solve(), solve_prepare(), and to_function().

◆ res() [2/2]

void casadi::OptiNode::res ( const DMDict res)

Definition at line 1243 of file optistack_internal.cpp.

1243  {
1244  const std::vector<double> & x_v = res.at("x").nonzeros();
1245  for (const auto &v : active_symvar(OPTI_VAR)) {
1246  casadi_int i = meta(v).i;
1247  std::vector<double> & data_v = store_latest_[OPTI_VAR][i].nonzeros();
1248  for (casadi_int i=0;i<data_v.size();++i) {
1249  casadi_int j = meta(v).start+i;
1250  data_v[i] = x_v[j]*linear_scale_[j] + linear_scale_offset_[j];
1251  }
1252  }
1253  if (res.find("lam_g")!=res.end()) {
1254  const std::vector<double> & lam_v = res.at("lam_g").nonzeros();
1255  for (const auto &v : active_symvar(OPTI_DUAL_G)) {
1256  casadi_int i = meta(v).i;
1257  std::vector<double> & data_v = store_latest_[OPTI_DUAL_G][i].nonzeros();
1258  for (casadi_int i=0;i<data_v.size();++i) {
1259  casadi_int j = meta(v).start+i;
1260  j = index_all_to_g_.at(j);
1261  if (j<0) continue;
1262  data_v[i] = lam_v.at(j)/g_linear_scale_.at(j)*f_linear_scale_;
1263  }
1264  }
1265  }
1266  res_ = res;
1267  mark_solved();
1268 }
casadi_int i
Definition: optistack.hpp:532

References active_symvar(), casadi::MetaVar::i, mark_solved(), casadi::OPTI_DUAL_G, casadi::OPTI_VAR, res(), and casadi::IndexAbstraction::start.

◆ return_status()

std::string casadi::OptiNode::return_status ( ) const

Definition at line 532 of file optistack_internal.cpp.

532  {
533  Dict mystats;
534  try {
535  mystats = stats();
536  } catch (...) {
537  return "unknown";
538  }
539  if (mystats.find("return_status")!=mystats.end()) {
540  std::stringstream ss;
541  ss << mystats.at("return_status");
542  return ss.str();
543  }
544  return "unknown";
545 }

References stats().

Referenced by solve().

◆ return_success()

bool casadi::OptiNode::return_success ( bool  accept_limit) const

Definition at line 547 of file optistack_internal.cpp.

547  {
548  Dict mystats;
549  try {
550  mystats = stats();
551  } catch (...) {
552  return false;
553  }
554  bool success = false;
555  if (mystats.find("success")!=mystats.end()) success = mystats.at("success");
556  if (!accept_limit) return success;
557 
558  bool limited = false;
559  if (mystats.find("unified_return_status")!=mystats.end())
560  limited = mystats.at("unified_return_status")=="SOLVER_RET_LIMITED";
561  return success || limited;
562 }

References stats().

Referenced by solve().

◆ scale_helper()

Function casadi::OptiNode::scale_helper ( const Function h) const

Extra doc: https://github.com/casadi/casadi/wiki/L_2cd

Definition at line 901 of file optistack_internal.cpp.

901  {
902  if (problem_dirty()) return baked_copy().scale_helper(h);
903  std::string name = h.name();
904  MX g_linear_scale_mx = g_linear_scale();
905  MX g_linear_scale_inv = 1/g_linear_scale();
906  MX f_linear_scale_mx = f_linear_scale();
907  MX x_linear_scale_mx = x_linear_scale();
908  MX x_linear_scale_offset_mx = x_linear_scale_offset();
909  if (name=="nlp_jac_g") {
910  MXDict arg;
911  arg["x"] = MX::sym("x", nx());
912  arg["p"] = MX::sym("p", np());
913  MXDict args;
914  args["x"] = arg["x"]*x_linear_scale_mx+x_linear_scale_offset_mx;
915  args["p"] = arg["p"];
916  MXDict res = h(args);
917  for (const auto & it : res) {
918  if (it.first=="g") {
919  arg[it.first] = it.second*g_linear_scale_inv;
920  } else if (it.first=="jac_g_x") {
921  arg[it.first] = mtimes(
922  mtimes(diag(g_linear_scale_inv), it.second),
923  diag(x_linear_scale_mx));
924  } else {
925  casadi_error("Unknown output '" + it.first + "'. Expecting g, jac_g_x.");
926  }
927  }
928  Function ret(name, arg, h.name_in(), h.name_out());
929  return ret;
930  } else if (name=="nlp_hess_l") {
931  MXDict arg;
932  arg["x"] = MX::sym("x", nx());
933  arg["p"] = MX::sym("p", np());
934  arg["lam_f"] = MX::sym("lam_f");
935  arg["lam_g"] = MX::sym("lam_g", ng());
936  MXDict args;
937  args["x"] = arg["x"]*x_linear_scale_mx+x_linear_scale_offset_mx;
938  args["p"] = arg["p"];
939  args["lam_f"] = arg["lam_f"]/f_linear_scale_mx;
940  args["lam_g"] = arg["lam_g"]/g_linear_scale_mx;
941  MXDict res = h(args);
942  for (const auto & it : res) {
943  if (it.first=="triu_hess_gamma_x_x" ||
944  it.first=="hess_gamma_x_x" ||
945  (it.second.size1()==nx() && it.second.is_square())) {
946  MX D = diag(x_linear_scale_mx);
947  arg[it.first] = mtimes(mtimes(D, it.second), D);
948  } else {
949  casadi_error("Unknown output '" + it.first + "'. Expecting triu_hess_gamma_x_x");
950  }
951  }
952  Function ret(name, arg, h.name_in(), h.name_out());
953  return ret;
954  } else {
955  casadi_error("Unknown helper function '" + name + "'");
956  }
957 }
const std::string & name() const
Name of the function.
Definition: function.cpp:1504
casadi_int nx() const
Number of (scalarised) decision variables.
casadi_int np() const
Number of (scalarised) parameters.
DM x_linear_scale_offset() const
double f_linear_scale() const
Function scale_helper(const Function &h) const
Scale a helper function constructed via opti.x, opti.g, ...
Definition: optistack.cpp:254

References arg(), baked_copy(), casadi::D, f_linear_scale(), g_linear_scale(), casadi::Function::name(), casadi::Function::name_in(), casadi::Function::name_out(), ng(), np(), nx(), problem_dirty(), res(), casadi::Opti::scale_helper(), casadi::GenericMatrix< MX >::sym(), x_linear_scale(), and x_linear_scale_offset().

◆ set_domain()

void casadi::OptiNode::set_domain ( const MX x,
const std::string &  domain 
)

Definition at line 1501 of file optistack_internal.cpp.

1501  {
1502  mark_solved(false);
1504  casadi_assert(x.is_valid_input(), "First argument to set_domain should be a variable.");
1505  DomainType type;
1506  if (domain=="real") {
1507  type = OPTI_DOMAIN_REAL;
1508  } else if (domain=="integer") {
1509  type = OPTI_DOMAIN_INTEGER;
1510  } else {
1511  casadi_error("Unknown domain '" + domain + "'. Known values are 'real', 'integer'.");
1512  }
1513  for (const auto& prim : x.primitives()) {
1514  MetaVar& m = meta(prim);
1515  m.domain = type;
1516  }
1517 }
bool is_valid_input() const
Check if matrix can be used to define function inputs.
Definition: mx.cpp:954
std::vector< MX > primitives() const
Get primitives.
Definition: mx.cpp:962
@ OPTI_DOMAIN_REAL
Definition: optistack.hpp:501

References casadi::MetaVar::domain, casadi::MX::is_valid_input(), mark_problem_dirty(), mark_solved(), casadi::OPTI_DOMAIN_INTEGER, casadi::OPTI_DOMAIN_REAL, casadi::MX::primitives(), and x().

◆ set_initial() [1/2]

void casadi::OptiNode::set_initial ( const MX x,
const DM v 
)

Set initial value for decision variables

Definition at line 1616 of file optistack_internal.cpp.

1616  {
1617  for (const auto & s : MX::symvar(x))
1618  casadi_assert(meta(s).type!=OPTI_PAR,
1619  "You cannot set an initial value for a parameter. Did you mean 'set_value'?");
1620  set_value_internal(x, v, store_initial_);
1621 }
static std::vector< MX > symvar(const MX &x)
Definition: mx.cpp:2041

References casadi::OPTI_PAR, casadi::MX::symvar(), and x().

Referenced by set_initial().

◆ set_initial() [2/2]

void casadi::OptiNode::set_initial ( const std::vector< MX > &  assignments)

Set initial value for decision variables

Definition at line 1492 of file optistack_internal.cpp.

1492  {
1493  for (const auto& v : assignments) {
1494  casadi_assert_dev(v.is_op(OP_EQ));
1495  casadi_assert_dev(v.dep(0).is_constant());
1496  if (has(v.dep(1)))
1497  set_initial(v.dep(1), static_cast<DM>(v.dep(0)));
1498  }
1499 }
void set_initial(const MX &x, const DM &v)

References casadi::OP_EQ, and set_initial().

◆ set_linear_scale()

void casadi::OptiNode::set_linear_scale ( const MX x,
const DM scale,
const DM offset 
)

Definition at line 1630 of file optistack_internal.cpp.

1630  {
1631  for (const auto & s : MX::symvar(x))
1632  casadi_assert(meta(s).type!=OPTI_PAR,
1633  "You cannot set a scale value for a parameter.");
1634  casadi_assert(scale.is_scalar() || scale.size()==x.size(),
1635  "Dimension mismatch in linear_scale. Expected " + x.dim() + ", got " + scale.dim()+ ".");
1636  set_value_internal(x, scale, store_linear_scale_);
1637  casadi_assert(offset.is_scalar() || offset.size()==x.size(),
1638  "Dimension mismatch in linear_scale offset. Expected " + x.dim() +
1639  ", got " + scale.dim()+ ".");
1640  set_value_internal(x, offset, store_linear_scale_offset_);
1641 }

References casadi::GenericMatrix< MatType >::dim(), casadi::GenericMatrix< MatType >::is_scalar(), casadi::OPTI_PAR, casadi::GenericMatrix< MatType >::size(), casadi::MX::symvar(), and x().

◆ set_meta()

void casadi::OptiNode::set_meta ( const MX m,
const MetaVar meta 
)

Definition at line 568 of file optistack_internal.cpp.

568  {
569  meta_[m.get()] = meta;
570 }

References casadi::MX::get().

Referenced by parameter(), update_user_dict(), and variable().

◆ set_meta_con()

void casadi::OptiNode::set_meta_con ( const MX m,
const MetaCon meta 
)

Definition at line 572 of file optistack_internal.cpp.

572  {
573  meta_con_[m.get()] = meta;
574 }

References casadi::MX::get().

Referenced by subject_to(), and update_user_dict().

◆ set_value() [1/2]

void casadi::OptiNode::set_value ( const MX x,
const DM v 
)

Each parameter must be given a value before 'solve' can be called

Extra doc: https://github.com/casadi/casadi/wiki/L_173

Definition at line 1623 of file optistack_internal.cpp.

1623  {
1624  for (const auto & s : MX::symvar(x))
1625  casadi_assert(meta(s).type!=OPTI_VAR,
1626  "You cannot set a value for a variable. Did you mean 'set_initial'?");
1627  set_value_internal(x, v, store_initial_);
1628 }

References casadi::OPTI_VAR, casadi::MX::symvar(), and x().

Referenced by set_value().

◆ set_value() [2/2]

void casadi::OptiNode::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_173

Definition at line 1519 of file optistack_internal.cpp.

1519  {
1520  for (const auto& v : assignments) {
1521  casadi_assert_dev(v.is_op(OP_EQ));
1522  casadi_assert_dev(v.dep(0).is_constant());
1523  if (has(v.dep(1)))
1524  set_value(v.dep(1), static_cast<DM>(v.dep(0)));
1525  }
1526 }
void set_value(const MX &x, const DM &v)
Set value of parameter.

References casadi::OP_EQ, and set_value().

◆ shared_from_this() [1/2]

B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( )
inlineprotectedinherited

Definition at line 83 of file generic_shared_internal.hpp.

83  {
84  casadi_assert_dev(B::test_cast(static_cast<Internal*>(this)));
85  B ret;
86  ret.own(static_cast<Internal*>(this));
87  return ret;
88  }

◆ shared_from_this() [2/2]

const B casadi::GenericSharedInternal< SharedObject , SharedObjectInternal >::shared_from_this ( ) const
inlineprotectedinherited

Definition at line 92 of file generic_shared_internal.hpp.

92  {
93  casadi_assert_dev(B::test_cast(static_cast<const Internal*>(this)));
94  B ret;
95  ret.own(const_cast<Internal*>(static_cast<const Internal*>(this)));
96  return ret;
97  }

◆ show_infeasibilities()

void casadi::OptiNode::show_infeasibilities ( double  tol = 0,
const Dict opts = Dict() 
) const

Definition at line 1743 of file optistack_internal.cpp.

1743  {
1744  stats();
1745  std::vector<double> lbg_ = value(lbg()).get_elements();
1746  std::vector<double> ubg_ = value(ubg()).get_elements();
1747  std::vector<double> g_scaled_ = value(nlp_.at("g"), std::vector<MX>(), true).get_elements();
1748  std::vector<double> lbg_scaled_ = value(bounds_lbg_, std::vector<MX>(), true).get_elements();
1749  std::vector<double> ubg_scaled_ = value(bounds_ubg_, std::vector<MX>(), true).get_elements();
1750 
1751 
1752  std::vector<double> g_ = value(g()).get_elements();
1753  uout() << "Violated constraints (tol " << tol << "), in order of declaration:" << std::endl;
1754 
1755  for (casadi_int i=0;i<g_.size();++i) {
1756  double err = std::max(g_[i]-ubg_[i], lbg_[i]-g_[i]);
1757  double err_scaled = std::max(g_scaled_[i]-ubg_scaled_[i], lbg_scaled_[i]-g_scaled_[i]);
1758  if (err>=tol) {
1759  uout() << "------- i = " << i+GlobalOptions::start_index;
1760  uout() << "/" << g_.size();
1761 
1762  if (reduced_) {
1763  if (is_simple_[i]) {
1764  if (GlobalOptions::start_index==0) {
1765  uout() << " reduced to bound on x[" << g_index_reduce_x_.at(i) << "]";
1766  } else {
1767  uout() << " reduced to bound on x(" << g_index_reduce_x_.at(i)+1 << ")";
1768  }
1769  } else {
1770  uout() << " reduced to g[" << g_index_reduce_g_.at(i) << "]";
1771  }
1772  }
1773 
1774  uout() << " ------ " << std::endl;
1775  uout() << lbg_[i] << " <= " << g_[i] << " <= " << ubg_[i];
1776  uout() << " (viol " << err << ")" << std::endl;
1777  if (g_[i]!=g_scaled_[i]) {
1778  uout() << lbg_scaled_[i] << " <= " << g_scaled_[i] << " <= " << ubg_scaled_[i];
1779  uout() << " (scaled) (viol " << err_scaled << ")" << std::endl;
1780  }
1781  uout() << g_describe(i, opts) << std::endl;
1782  }
1783  }
1784 }
std::vector< Scalar > get_elements() const
Get all elements.
DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >(), bool scaled=false) const
std::string g_describe(casadi_int i, const Dict &opts=Dict()) const
std::ostream & uout()

References g(), g_describe(), casadi::Matrix< Scalar >::get_elements(), lbg(), casadi::GlobalOptions::start_index, stats(), ubg(), casadi::uout(), and value().

◆ solve()

OptiSol casadi::OptiNode::solve ( bool  accept_limit)

Definition at line 1322 of file optistack_internal.cpp.

1322  {
1323 
1324  if (problem_dirty()) {
1325  bake();
1326  }
1327 
1328  bool solver_update = solver_dirty() || old_callback() || (user_callback_ && callback_.is_null());
1329 
1330  if (solver_update) {
1331  solver_ = solver_construct(true);
1332  mark_solver_dirty(false);
1333  }
1334 
1335  solve_prepare();
1336  res(solve_actual(arg_));
1337 
1338  std::string ret = return_status();
1339 
1340  casadi_assert(return_success(accept_limit),
1341  "Solver failed. You may use opti.debug.value to investigate the latest values of variables."
1342  " return_status is '" + ret + "'");
1343 
1344  return copy();
1345 }
bool is_null() const
Is a null pointer?
Function solver_construct(bool callback=true)
void bake()
Fix the structure of the optimization problem.
bool return_success(bool accept_limit) const
Did the solver return successfully?
std::string return_status() const
Get return status of solver.
DMDict solve_actual(const DMDict &args)

References bake(), copy(), casadi::GenericShared< Shared, Internal >::is_null(), mark_solver_dirty(), problem_dirty(), res(), return_status(), return_success(), solve_actual(), solve_prepare(), solver_construct(), and solver_dirty().

◆ solve_actual()

DMDict casadi::OptiNode::solve_actual ( const DMDict args)

Definition at line 1387 of file optistack_internal.cpp.

1387  {
1388  return solver_(arg);
1389 }

References arg().

Referenced by solve().

◆ solve_prepare()

void casadi::OptiNode::solve_prepare ( )

Definition at line 1348 of file optistack_internal.cpp.

1348  {
1349 
1350 
1351  // Verify the constraint types
1352  for (const auto& g : g_) {
1353  if (meta_con(g).type==OPTI_UNKNOWN)
1354  casadi_error("Constraint type unknown. Use ==, >= or <= .");
1355  }
1356 
1357  if (user_callback_) {
1358  InternalOptiCallback* cb = static_cast<InternalOptiCallback*>(callback_.get());
1359  cb->reset();
1360  }
1361 
1362  // Get initial guess and parameter values
1363  arg_["x0"] = (veccat(active_values(OPTI_VAR))-linear_scale_offset_)/linear_scale_;
1364  arg_["p"] = veccat(active_values(OPTI_PAR));
1365  arg_["lam_g0"] = veccat(active_values(OPTI_DUAL_G));
1366  if (!arg_["p"].is_regular()) {
1367  std::vector<MX> s = active_symvar(OPTI_PAR);
1368  std::vector<DM> v = active_values(OPTI_PAR);
1369  for (casadi_int i=0;i<s.size();++i) {
1370  casadi_assert(v[i].is_regular(),
1371  "You have forgotten to assign a value to a parameter ('set_value'), "
1372  "or have set it to NaN/Inf:\n" + describe(s[i], 1));
1373  }
1374  }
1375 
1376 
1377  // Evaluate bounds for given parameter values
1378  DMDict arg;
1379  arg["p"] = arg_["p"];
1380  DMDict res = bounds_(arg);
1381  arg_["lbg"] = res["lbg"];
1382  arg_["ubg"] = res["ubg"];
1383 
1384  stats_.clear();
1385 }
FunctionInternal * get() const
Definition: function.cpp:505
friend class InternalOptiCallback
bool is_regular(const std::vector< T > &v)
Checks if array does not contain NaN or Inf.
std::map< std::string, DM > DMDict
Definition: dm_fwd.hpp:36

References active_symvar(), active_values(), arg(), describe(), g(), casadi::Function::get(), InternalOptiCallback, casadi::is_regular(), casadi::OPTI_DUAL_G, casadi::OPTI_PAR, casadi::OPTI_UNKNOWN, casadi::OPTI_VAR, and res().

Referenced by solve().

◆ solved()

bool casadi::OptiNode::solved ( ) const
inline

Definition at line 331 of file optistack_internal.hpp.

331 { return solved_; }

Referenced by assert_solved().

◆ solver()

void casadi::OptiNode::solver ( const std::string &  solver,
const Dict plugin_options = Dict(),
const Dict solver_options = Dict() 
)

Definition at line 959 of file optistack_internal.cpp.

960  {
961  solver_name_ = solver_name;
962  solver_options_ = plugin_options;
963  if (!solver_options.empty())
964  solver_options_[solver_name] = solver_options;
966 }

References mark_solver_dirty().

Referenced by to_function().

◆ solver_construct()

Function casadi::OptiNode::solver_construct ( bool  callback = true)

Definition at line 1276 of file optistack_internal.cpp.

1276  {
1277  if (problem_dirty()) {
1278  bake();
1279  }
1280 
1281  // Verify the constraint types
1282  for (const auto& g : g_) {
1283  if (problem_type_!="conic") {
1284  if (meta_con(g).type==OPTI_PSD)
1285  casadi_error("Psd constraints not implemented yet. "
1286  "Perhaps you intended an element-wise inequality? "
1287  "In that case, make sure that the matrix is flattened (e.g. mat(:)).");
1288  }
1289  }
1290 
1291  Dict solver_options_all = solver_options_;
1292 
1293  if (solver_options_all.find("equality")==solver_options_all.end()) {
1294  solver_options_all["equality"] = equality_;
1295  }
1296 
1297  if (solver_options_all.find("discrete")==solver_options_all.end()) {
1298  solver_options_all["discrete"] = discrete_;
1299  }
1300 
1301  Dict opts = solver_options_all;
1302 
1303  // Handle callbacks
1304  if (callback && user_callback_) {
1305  callback_ = Function::create(new InternalOptiCallback(*this), Dict());
1306  opts["iteration_callback"] = callback_;
1307  }
1308 
1309  casadi_assert(!solver_name_.empty(),
1310  "You must call 'solver' on the Opti stack to select a solver. "
1311  "Suggestion: opti.solver('ipopt')");
1312 
1313  if (problem_type_=="conic") {
1314  return qpsol("solver", solver_name_, nlp_, opts);
1315  } else {
1316  return nlpsol("solver", solver_name_, nlp_, opts);
1317  }
1318 
1319 }
static Function create(FunctionInternal *node)
Create from node.
Definition: function.cpp:488
Function qpsol(const std::string &name, const std::string &solver, const SXDict &qp, const Dict &opts)
Definition: conic.cpp:261
Function nlpsol(const std::string &name, const std::string &solver, const SXDict &nlp, const Dict &opts)
Definition: nlpsol.cpp:195

References bake(), casadi::Function::create(), g(), InternalOptiCallback, casadi::nlpsol(), casadi::OPTI_PSD, problem_dirty(), and casadi::qpsol().

Referenced by solve(), and to_function().

◆ solver_dirty()

bool casadi::OptiNode::solver_dirty ( ) const
inline

Definition at line 327 of file optistack_internal.hpp.

327 { return solver_dirty_; }

Referenced by solve().

◆ stats()

Dict casadi::OptiNode::stats ( ) const

Definition at line 483 of file optistack_internal.cpp.

483  {
484  assert_solved();
485  if (stats_.empty()) {
486  stats_ = solver_.stats();
487  is_simple_ = get_from_dict(stats_,
488  "detect_simple_bounds_is_simple",
489  std::vector<bool>(ng(), false));
490  target_x_ = get_from_dict(stats_,
491  "detect_simple_bounds_target_x",
492  std::vector<casadi_int>{});
493  casadi_assert_dev(is_simple_.size()==ng());
494 
495  g_index_reduce_g_.resize(ng());
496  g_index_reduce_x_.resize(ng(), -1);
497  g_index_unreduce_g_.resize(ng(), -1);
498 
499  casadi_int* target_x_ptr = target_x_.data();
500 
501  casadi_int k=0;
502  for (casadi_int i=0;i<is_simple_.size();++i) {
503  if (!is_simple_[i]) {
504  g_index_reduce_g_[i] = k;
505  g_index_unreduce_g_[k] = i;
506  k++;
507  } else {
508  g_index_reduce_g_[i] = -1;
509  g_index_reduce_x_[i] = *target_x_ptr;
510  target_x_ptr++;
511  }
512  }
513 
514  reduced_ = any(is_simple_);
515  }
516  return stats_;
517 }
Dict stats(int mem=0) const
Get all statistics obtained at the end of the last evaluate call.
Definition: function.cpp:1080
T get_from_dict(const std::map< std::string, T > &d, const std::string &key, const T &default_value)
bool any(const std::vector< bool > &v)
Check if any arguments are true.
Definition: casadi_misc.cpp:88

References casadi::any(), assert_solved(), casadi::get_from_dict(), ng(), and casadi::Function::stats().

Referenced by g_index_reduce_g(), g_index_reduce_x(), g_index_unreduce_g(), return_status(), return_success(), and show_infeasibilities().

◆ subject_to() [1/2]

void casadi::OptiNode::subject_to ( )

Definition at line 1226 of file optistack_internal.cpp.

1226  {
1228  g_.clear();
1229  store_initial_[OPTI_DUAL_G].clear();
1230  store_latest_[OPTI_DUAL_G].clear();
1231  count_dual_ = 0;
1232 }

References mark_problem_dirty(), and casadi::OPTI_DUAL_G.

◆ subject_to() [2/2]

void casadi::OptiNode::subject_to ( const MX g,
const DM linear_scale = 1,
const Dict options = Dict() 
)

Definition at line 1196 of file optistack_internal.cpp.

1196  {
1197  assert_only_opti_nondual(g);
1199  g_.push_back(g);
1200 
1201  casadi_assert(!g.is_empty(), "You passed an empty expression to `subject_to`. "
1202  "Make sure the number of rows and columns is non-zero. "
1203  "Got " + g.dim(true) + ".");
1204  casadi_assert(g.nnz()>0, "You passed a fully sparse expression to `subject_to`. "
1205  "Make sure the expression has at least one nonzero. "
1206  "Got " + g.dim(true) + ".");
1207  casadi_assert(!g.is_constant(), "You passed a constant to `subject_to`. "
1208  "You need a symbol to form a constraint.");
1209 
1210  // Store the meta-data
1211  set_meta_con(g, canon_expr(g, linear_scale));
1212  register_dual(meta_con(g));
1213 
1214  for (auto && it : options) {
1215  if (it.first=="stacktrace") {
1216  meta_con(g).extra["stacktrace"] = it.second.to_dict_vector();
1217  meta(meta_con(g).dual_canon).extra["stacktrace"] = it.second.to_dict_vector();
1218  } else if (it.first=="meta") {
1219  update_user_dict(g, it.second.to_dict());
1220  } else {
1221  casadi_error("Unknown option: " + it.first);
1222  }
1223  }
1224 }
casadi_int nnz() const
Get the number of (structural) non-zero elements.
bool is_constant() const
Check if constant.
Definition: mx.cpp:799
void update_user_dict(const MX &m, const Dict &meta)
add meta-data of an expression
MetaCon canon_expr(const MX &expr, const DM &linear_scale=1) const
Interpret an expression (for internal use only)
void set_meta_con(const MX &m, const MetaCon &meta)
Set meta-data of an expression.

References canon_expr(), casadi::GenericMatrix< MatType >::dim(), casadi::MetaCon::extra, casadi::MetaVar::extra, g(), casadi::MX::is_constant(), casadi::GenericMatrix< MatType >::is_empty(), mark_problem_dirty(), casadi::GenericMatrix< MatType >::nnz(), set_meta_con(), and update_user_dict().

◆ symvar() [1/4]

std::vector< MX > casadi::OptiNode::symvar ( ) const

Get symbols present in expression

Definition at line 983 of file optistack_internal.cpp.

983  {
984  return symbols_;
985 }

Referenced by bake(), describe(), initial(), is_parametric(), symvar(), value(), value_parameters(), and value_variables().

◆ symvar() [2/4]

std::vector< MX > casadi::OptiNode::symvar ( const MX expr) const

Get symbols present in expression

Definition at line 987 of file optistack_internal.cpp.

987  {
988  return sort(MX::symvar(expr));
989 }

References casadi::MX::symvar().

◆ symvar() [3/4]

std::vector< MX > casadi::OptiNode::symvar ( const MX expr,
VariableType  type 
) const

Get symbols present in expression

Definition at line 1234 of file optistack_internal.cpp.

1234  {
1235  std::vector<MX> ret;
1236  for (const auto& d : symvar(expr)) {
1237  if (meta(d).type==type) ret.push_back(d);
1238  }
1239 
1240  return ret;
1241 }

References symvar().

◆ symvar() [4/4]

std::vector< MX > casadi::OptiNode::symvar ( VariableType  type) const

Definition at line 1653 of file optistack_internal.cpp.

1653  {
1654  if (symbols_.empty()) return std::vector<MX>{};
1655  std::vector<MX> ret;
1656  for (const auto& s : symbols_) {
1657  if (meta(s).type==type)
1658  ret.push_back(s);
1659  }
1660  return ret;
1661 }

◆ to_function()

Function casadi::OptiNode::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 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_174

Definition at line 1679 of file optistack_internal.cpp.

1683  {
1684  if (problem_dirty()) return baked_copy().to_function(name, args, res, name_in, name_out, opts);
1685 
1686  Function solver = solver_construct(false);
1687 
1688  // Get initial guess and parameter values
1689  std::vector<MX> x0, p, lam_g;
1693 
1694  casadi_int k = 0;
1695  for (const auto& a : args) {
1696  casadi_assert(a.is_valid_input(), "Argument " + str(k) + " is not purely symbolic.");
1697  k++;
1698  for (const auto& prim : a.primitives()) {
1699  if (!symbol_active_[meta(prim).count]) continue;
1700  casadi_int i = meta(prim).active_i;
1701  if (meta(prim).type==OPTI_VAR) {
1702  x0.at(i) = prim;
1703  } else if (meta(prim).type==OPTI_PAR) {
1704  p.at(i) = prim;
1705  } else if (meta(prim).type==OPTI_DUAL_G) {
1706  lam_g.at(i) = prim;
1707  } else {
1708  casadi_error("Unknown");
1709  }
1710  }
1711  }
1712  MXDict arg;
1713  arg["p"] = veccat(p);
1714 
1715  // Evaluate bounds for given parameter values
1716  MXDict r = bounds_(arg);
1717  arg["x0"] = veccat(x0);
1718  arg["lam_g0"] = veccat(lam_g);
1719  arg["lbg"] = r["lbg"];
1720  arg["ubg"] = r["ubg"];
1721 
1722  r = solver(arg);
1723 
1724  std::vector<MX> helper_in = {veccat(active_symvar(OPTI_VAR)),
1725  veccat(active_symvar(OPTI_PAR)),
1726  veccat(active_symvar(OPTI_DUAL_G))};
1727  Function helper("helper", helper_in, {res});
1728 
1729  std::vector<MX> arg_in = helper(std::vector<MX>{r.at("x"), arg["p"], r.at("lam_g")});
1730 
1731  return Function(name, args, arg_in, name_in, name_out, opts);
1732 
1733 }
MX lam_g() const
Get dual variables as a symbolic column vector.
void solver(const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
Solver.
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.
Definition: optistack.cpp:435
void assign_vector(const std::vector< S > &s, std::vector< D > &d)

References casadi::MetaVar::active_i, active_symvar(), active_values(), arg(), casadi::assign_vector(), baked_copy(), casadi::MX::is_valid_input(), lam_g(), casadi::OPTI_DUAL_G, casadi::OPTI_PAR, casadi::OPTI_VAR, p(), casadi::MX::primitives(), problem_dirty(), res(), solver(), solver_construct(), casadi::str(), and casadi::Opti::to_function().

◆ ubg()

MX casadi::OptiNode::ubg ( ) const
inline

Definition at line 271 of file optistack_internal.hpp.

271  {
272  if (problem_dirty()) return baked_copy().ubg();
273  return bounds_unscaled_ubg_;
274  }
MX ubg() const
Definition: optistack.cpp:374

Referenced by show_infeasibilities().

◆ update_user_dict()

void casadi::OptiNode::update_user_dict ( const MX m,
const Dict meta 
)

Definition at line 576 of file optistack_internal.cpp.

576  {
577  if (has_con(m)) {
578  MetaCon m_update = get_meta_con(m);
579  MetaVar m_update2 = get_meta(m_update.dual_canon);
580  for (const auto & it : meta) {
581  m_update.extra[it.first] = it.second;
582  m_update2.extra[it.first] = it.second;
583  }
584  set_meta_con(m, m_update);
585  set_meta(m_update.dual_canon, m_update2);
586  } else {
587  for (const auto & s : MX::symvar(m)) {
588  MetaVar m_update = get_meta(s);
589  for (const auto & it : meta)
590  m_update.extra[it.first] = it.second;
591  set_meta(s, m_update);
592  }
593  }
594 }
MetaCon get_meta_con(const MX &m) const
Get meta-data of symbol (for internal use only)
MetaVar get_meta(const MX &m) const
Get meta-data of symbol (for internal use only)

References casadi::MetaCon::dual_canon, casadi::MetaCon::extra, casadi::MetaVar::extra, get_meta(), get_meta_con(), set_meta(), set_meta_con(), and casadi::MX::symvar().

Referenced by subject_to().

◆ user_dict()

Dict casadi::OptiNode::user_dict ( const MX m) const

Definition at line 596 of file optistack_internal.cpp.

596  {
597  if (has_con(m)) {
598  MetaCon meta = get_meta_con(m);
599  return meta.extra;
600  } else {
601  MetaVar meta = get_meta(m);
602  return meta.extra;
603  }
604 }

References casadi::MetaCon::extra, casadi::MetaVar::extra, get_meta(), and get_meta_con().

◆ value() [1/3]

DM casadi::OptiNode::value ( const DM x,
const std::vector< MX > &  values = std::vector<MX>(),
bool  scaled = false 
) const
inline

Obtain value of expression at the current value

Definition at line 121 of file optistack_internal.hpp.

122  { return x; }

◆ value() [2/3]

DM casadi::OptiNode::value ( const MX x,
const std::vector< MX > &  values = std::vector<MX>(),
bool  scaled = false 
) const

Obtain value of expression at the current value

Definition at line 1405 of file optistack_internal.cpp.

1405  {
1406 
1407  std::shared_ptr<ValueHelper> vh;
1408  if (!helpers_->incache(expr, vh)) {
1409  vh = std::make_shared<ValueHelper>();
1410  std::vector<MX> x = symvar(expr, OPTI_VAR);
1411  std::vector<MX> p = symvar(expr, OPTI_PAR);
1412  std::vector<MX> lam = symvar(expr, OPTI_DUAL_G);
1413  vh->x = x;
1414  vh->p = p;
1415  vh->lam = lam;
1416  vh->helper = Function("helper", std::vector<MX>{veccat(x), veccat(p), veccat(lam)}, {expr});
1417  if (vh->helper.has_free())
1418  casadi_error("This expression has symbols that are not defined "
1419  "within Opti using variable/parameter.");
1420  helpers_->tocache_if_missing(expr, vh);
1421  }
1422 
1423  const std::vector<MX>& x = vh->x;
1424  const std::vector<MX>& p = vh->p;
1425  const std::vector<MX>& lam = vh->lam;
1426  const Function& helper = vh->helper;
1427 
1428  std::map<VariableType, std::map<casadi_int, MX> > temp;
1429  temp[OPTI_DUAL_G] = std::map<casadi_int, MX>();
1430  for (const auto& v : values) {
1431  casadi_assert_dev(v.is_op(OP_EQ));
1432  casadi_int i = meta(v.dep(1)).i;
1433  casadi_assert_dev(v.dep(0).is_constant());
1434  temp[meta(v.dep(1)).type][i] = v.dep(0);
1435  }
1436 
1437  bool undecided_vars = false;
1438  std::vector<DM> x_num;
1439  for (const auto& e : x) {
1440  casadi_int i = meta(e).i;
1441  x_num.push_back(store_latest_.at(OPTI_VAR).at(i));
1442  undecided_vars |= override_num(temp[OPTI_VAR], x_num, i);
1443  if (scaled) {
1444  x_num.back() = x_num.back()/store_linear_scale_.at(OPTI_VAR)[meta(e).i] -
1445  store_linear_scale_offset_.at(OPTI_VAR)[meta(e).i];
1446  }
1447  }
1448 
1449  std::vector<DM> lam_num;
1450  for (const auto& e : lam) {
1451  casadi_int i = meta(e).i;
1452  casadi_assert(i<store_latest_.at(OPTI_DUAL_G).size(),
1453  "This expression has a dual for a constraint that is not given to Opti:\n" +
1454  describe(e, 1));
1455  lam_num.push_back(store_latest_.at(OPTI_DUAL_G).at(i));
1456  undecided_vars |= override_num(temp[OPTI_DUAL_G], lam_num, i);
1457  }
1458 
1459  std::vector<DM> p_num;
1460  for (const auto& e : p) {
1461  casadi_int i = meta(e).i;
1462  p_num.push_back(store_initial_.at(OPTI_PAR).at(i));
1463  override_num(temp[OPTI_PAR], p_num, i);
1464  casadi_assert(p_num.back().is_regular(),
1465  "This expression depends on a parameter with unset value:\n"+
1466  describe(e, 1));
1467  }
1468 
1469  if (undecided_vars) {
1470  assert_solved();
1471  for (const auto& e : x)
1472  casadi_assert(symbol_active_[meta(e).count],
1473  "This expression has symbols that do not appear in the constraints and objective:\n" +
1474  describe(e, 1));
1475  for (const auto& e : lam)
1476  casadi_assert(symbol_active_[meta(e).count],
1477  "This expression has a dual for a constraint that is not given to Opti:\n" +
1478  describe(e, 1));
1479  }
1480 
1481  std::vector<DM> arg = helper(std::vector<DM>{veccat(x_num), veccat(p_num), veccat(lam_num)});
1482  return arg[0];
1483 }
bool override_num(const std::map< casadi_int, MX > &temp, std::vector< DM > &num, casadi_int i)
VariableType type
Definition: optistack.hpp:529

References arg(), assert_solved(), describe(), casadi::MetaVar::i, casadi::OP_EQ, casadi::OPTI_DUAL_G, casadi::OPTI_PAR, casadi::OPTI_VAR, casadi::override_num(), p(), symvar(), casadi::MetaVar::type, and x().

Referenced by show_infeasibilities().

◆ value() [3/3]

DM casadi::OptiNode::value ( const SX x,
const std::vector< MX > &  values = std::vector<MX>(),
bool  scaled = false 
) const
inline

Obtain value of expression at the current value

Definition at line 123 of file optistack_internal.hpp.

124  {
125  return DM::nan(x.sparsity());
126  }
const Sparsity & sparsity() const
Get the sparsity pattern.
Definition: mx.cpp:612

References casadi::Matrix< double >::nan(), and casadi::Matrix< Scalar >::sparsity().

◆ value_parameters()

std::vector< MX > casadi::OptiNode::value_parameters ( ) const

Definition at line 720 of file optistack_internal.cpp.

720  {
721  std::vector<MX> ret;
722  for (const auto& e : symvar()) {
723  if (meta(e).type==OPTI_PAR)
724  ret.push_back(e==store_initial_.at(meta(e).type)[meta(e).i]);
725  }
726  return ret;
727 }

References casadi::OPTI_PAR, and symvar().

◆ value_variables()

std::vector< MX > casadi::OptiNode::value_variables ( ) const

Definition at line 711 of file optistack_internal.cpp.

711  {
712  std::vector<MX> ret;
713  for (const auto& e : symvar()) {
714  if (meta(e).type==OPTI_VAR)
715  ret.push_back(e==store_latest_.at(meta(e).type)[meta(e).i]);
716  }
717  return ret;
718 }

References casadi::OPTI_VAR, and symvar().

◆ variable() [1/3]

MX casadi::OptiNode::variable ( casadi_int  n = 1,
casadi_int  m = 1,
const std::string &  attribute = "full" 
)

Definition at line 278 of file optistack_internal.cpp.

278  {
279 
280  // Prepare metadata
281  MetaVar meta_data;
282  meta_data.attribute = attribute;
283  meta_data.n = n;
284  meta_data.m = m;
285  meta_data.type = OPTI_VAR;
286  meta_data.count = count_++;
287  meta_data.i = count_var_++;
288  meta_data.domain = OPTI_DOMAIN_REAL;
289 
290  MX symbol, ret;
291 
292  if (attribute=="symmetric") {
293  casadi_assert(n==m, "You specified attribute 'symmetric', "
294  "while matrix is not even square, but " + str(n) + "-by-" + str(m) + ".");
295  symbol = MX::sym(name_prefix() + "x_" + str(count_var_), n*(n+1)/2);
296  ret = tril2symm(MX(Sparsity::lower(n), symbol));
297  } else if (attribute=="full") {
298  symbol = MX::sym(name_prefix() + "x_" + str(count_var_), n, m);
299  ret = symbol;
300  } else {
301  casadi_error("Unknown attribute '" + attribute + "'. Choose from 'full' or 'symmetric'.");
302  }
303 
304  // Store the symbol; preventing it from going ut of scope
305  symbols_.push_back(symbol);
306  store_initial_[OPTI_VAR].push_back(DM::zeros(symbol.sparsity()));
307  store_latest_[OPTI_VAR].push_back(DM::nan(symbol.sparsity()));
308  store_linear_scale_[OPTI_VAR].push_back(DM::ones(symbol.sparsity()));
309  store_linear_scale_offset_[OPTI_VAR].push_back(DM::zeros(symbol.sparsity()));
310 
311  set_meta(symbol, meta_data);
312  return ret;
313 }
static Sparsity lower(casadi_int n)
Create a lower triangular square sparsity pattern *.
Definition: sparsity.cpp:1065

References casadi::MetaVar::attribute, casadi::MetaVar::count, casadi::MetaVar::domain, casadi::MetaVar::i, casadi::Sparsity::lower(), casadi::MetaVar::m, casadi::MetaVar::n, casadi::Matrix< double >::nan(), casadi::GenericMatrix< MatType >::ones(), casadi::OPTI_DOMAIN_REAL, casadi::OPTI_VAR, set_meta(), casadi::MX::sparsity(), casadi::str(), casadi::GenericMatrix< MX >::sym(), casadi::MetaVar::type, and casadi::GenericMatrix< MatType >::zeros().

◆ variable() [2/3]

MX casadi::OptiNode::variable ( const MX symbol,
const std::string &  attribute = "full" 
)

Definition at line 315 of file optistack_internal.cpp.

315  {
316 
317  // Prepare metadata
318  MetaVar meta_data;
319  meta_data.attribute = attribute;
320  meta_data.n = symbol.size1();
321  meta_data.m = symbol.size2();
322  meta_data.type = OPTI_VAR;
323  meta_data.count = count_++;
324  meta_data.i = count_var_++;
325 
326  // Store the symbol; preventing it from going ut of scope
327  symbols_.push_back(symbol);
328  store_initial_[OPTI_VAR].push_back(DM::zeros(symbol.sparsity()));
329  store_latest_[OPTI_VAR].push_back(DM::nan(symbol.sparsity()));
330  store_linear_scale_[OPTI_VAR].push_back(DM::ones(symbol.sparsity()));
331  store_linear_scale_offset_[OPTI_VAR].push_back(DM::zeros(symbol.sparsity()));
332 
333  set_meta(symbol, meta_data);
334  return symbol;
335 }

References casadi::MetaVar::attribute, casadi::MetaVar::count, casadi::MetaVar::i, casadi::MetaVar::m, casadi::MetaVar::n, casadi::Matrix< double >::nan(), casadi::GenericMatrix< MatType >::ones(), casadi::OPTI_VAR, set_meta(), casadi::GenericMatrix< MatType >::size1(), casadi::GenericMatrix< MatType >::size2(), casadi::MX::sparsity(), casadi::MetaVar::type, and casadi::GenericMatrix< MatType >::zeros().

◆ variable() [3/3]

MX casadi::OptiNode::variable ( const Sparsity sp,
const std::string &  attribute = "full" 
)

Definition at line 337 of file optistack_internal.cpp.

337  {
338 
339  // Prepare metadata
340  MetaVar meta_data;
341  meta_data.attribute = attribute;
342  meta_data.n = sp.size1();
343  meta_data.m = sp.size2();
344  meta_data.type = OPTI_VAR;
345  meta_data.count = count_++;
346  meta_data.i = count_var_++;
347 
348  MX symbol = MX::sym(name_prefix() + "x_" + str(count_var_), sp);
349 
350  // Store the symbol; preventing it from going ut of scope
351  symbols_.push_back(symbol);
352  store_initial_[OPTI_VAR].push_back(DM::zeros(symbol.sparsity()));
353  store_latest_[OPTI_VAR].push_back(DM::nan(symbol.sparsity()));
354  store_linear_scale_[OPTI_VAR].push_back(DM::ones(symbol.sparsity()));
355  store_linear_scale_offset_[OPTI_VAR].push_back(DM::zeros(symbol.sparsity()));
356 
357  set_meta(symbol, meta_data);
358  return symbol;
359 }

References casadi::MetaVar::attribute, casadi::MetaVar::count, casadi::MetaVar::i, casadi::MetaVar::m, casadi::MetaVar::n, casadi::Matrix< double >::nan(), casadi::GenericMatrix< MatType >::ones(), casadi::OPTI_VAR, set_meta(), casadi::Sparsity::size1(), casadi::Sparsity::size2(), casadi::MX::sparsity(), casadi::str(), casadi::GenericMatrix< MX >::sym(), casadi::MetaVar::type, and casadi::GenericMatrix< MatType >::zeros().

◆ weak()

Extra doc: https://github.com/casadi/casadi/wiki/L_1ai

Definition at line 67 of file generic_shared_internal.hpp.

210  {
211 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
212  auto* w = weak_ref_.load(std::memory_order_acquire);
213  if (!w) {
214  auto* nw = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
215  GenericWeakRef<Shared, Internal>* expected = nullptr;
216  if (weak_ref_.compare_exchange_strong(
217  expected, nw, std::memory_order_release, std::memory_order_acquire)) {
218  w = nw;
219  } else {
220  delete nw; // lost the race; another thread published first
221  w = expected;
222  }
223  }
224  return w;
225 #else
226  if (weak_ref_==nullptr) {
227  weak_ref_ = new GenericWeakRef<Shared, Internal>(static_cast<Internal*>(this));
228  }
229  return weak_ref_;
230 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
231  }

◆ x()

MX casadi::OptiNode::x ( ) const
inline

Definition at line 243 of file optistack_internal.hpp.

243  {
244  if (problem_dirty()) return baked_copy().x();
245  return nlp_.at("x");
246  }
MX x() const
Get all (scalarised) decision variables as a symbolic column vector.
Definition: optistack.cpp:334

Referenced by bake(), set_domain(), set_initial(), set_linear_scale(), set_value(), value(), and x_lookup().

◆ x_describe()

std::string casadi::OptiNode::x_describe ( casadi_int  i,
const Dict opts = Dict() 
) const

Definition at line 221 of file optistack_internal.cpp.

221  {
222  if (problem_dirty()) return baked_copy().x_describe(i, opts);
223  MX symbol = x_lookup(i);
224  casadi_int local_i = i-meta(symbol).start + GlobalOptions::start_index;
225  std::string description = describe(symbol, 0, opts);
226  if (symbol.numel()>1)
227  description += "\nAt nonzero " + str(local_i) + ".";
228  return description;
229 }
std::string x_describe(casadi_index i, const Dict &opts=Dict()) const
Definition: optistack.cpp:649
MX x_lookup(casadi_int i) const

References baked_copy(), describe(), casadi::description(), casadi::GenericMatrix< MatType >::numel(), problem_dirty(), casadi::IndexAbstraction::start, casadi::GlobalOptions::start_index, casadi::str(), casadi::OptiAdvanced::x_describe(), and x_lookup().

◆ x_linear_scale()

DM casadi::OptiNode::x_linear_scale ( ) const
inline

Definition at line 282 of file optistack_internal.hpp.

282  {
283  if (problem_dirty()) return baked_copy().x_linear_scale();
284  return DM(linear_scale_);
285  }
DM x_linear_scale() const
Definition: optistack.cpp:391

Referenced by scale_helper().

◆ x_linear_scale_offset()

DM casadi::OptiNode::x_linear_scale_offset ( ) const
inline

Definition at line 286 of file optistack_internal.hpp.

286  {
288  return DM(linear_scale_offset_);
289  }
DM x_linear_scale_offset() const
Definition: optistack.cpp:399

Referenced by scale_helper().

◆ x_lookup()

MX casadi::OptiNode::x_lookup ( casadi_int  i) const

Definition at line 231 of file optistack_internal.cpp.

231  {
232  if (problem_dirty()) return baked_copy().x_lookup(i);
233  casadi_assert_dev(i>=0);
234  casadi_assert_dev(i<nx());
235  std::vector<MX> x = active_symvar(OPTI_VAR);
236  for (const auto& e : x) {
237  const MetaVar& m = meta(e);
238  if (i>=m.start && i<m.stop) return e;
239  }
240  casadi_error("Internal error");
241  return MX();
242 }
MX x_lookup(casadi_index i) const
Definition: optistack.cpp:609

References active_symvar(), baked_copy(), nx(), casadi::OPTI_VAR, problem_dirty(), casadi::IndexAbstraction::start, casadi::IndexAbstraction::stop, x(), and casadi::OptiAdvanced::x_lookup().

Referenced by x_describe().

Friends And Related Function Documentation

◆ InternalOptiCallback

friend class InternalOptiCallback
friend

Definition at line 57 of file optistack_internal.hpp.

Referenced by solve_prepare(), and solver_construct().

Member Data Documentation

◆ problem_dirty_

bool casadi::OptiNode::problem_dirty_

Definition at line 321 of file optistack_internal.hpp.

◆ solved_

bool casadi::OptiNode::solved_

Definition at line 329 of file optistack_internal.hpp.

◆ solver_dirty_

bool casadi::OptiNode::solver_dirty_

Definition at line 325 of file optistack_internal.hpp.


The documentation for this class was generated from the following files: