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

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

#include <optistack.hpp>

Detailed Description

This class offers a view with model description facilities The API is guaranteed to be stable.

Example NLP:

  opti = casadi.Opti();

  x = opti.variable();
  y = opti.variable();

  opti.minimize(  (y-x^2)^2   );
  opti.subject_to( x^2+y^2==1 );
  opti.subject_to(     x+y>=1 );

  opti.solver('ipopt');
  sol = opti.solve();

  sol.value(x)
  sol.value(y)

Example parametric NLP:

  opti = casadi.Opti();

  x = opti.variable(2,1);
  p = opti.parameter();

  opti.minimize(  (p*x(2)-x(1)^2)^2   );
  opti.subject_to( 1<=sum(x)<=2 );

  opti.solver('ipopt');

  opti.set_value(p, 3);
  sol = opti.solve();
  sol.value(x)

  opti.set_value(p, 5);
  sol = opti.solve();
  sol.value(x)
Date
2017
Author
Joris Gillis, Erik Lambrechts, Joel Andersson

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

Definition at line 88 of file optistack.hpp.

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

Public Types

using internal_base_type = SharedObjectInternal
 
using base_type = SharedObject
 

Public Member Functions

 Opti (const std::string &problem_type="nlp")
 Create Opti Context. More...
 
MX variable (casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
 Create a decision variable (symbol) More...
 
MX variable (const Sparsity &sp, const std::string &attribute="full")
 
MX variable (const MX &symbol, const std::string &attribute="full")
 
MX parameter (casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
 Create a parameter (symbol); fixed during optimization. More...
 
MX parameter (const Sparsity &sp, const std::string &attribute="full")
 
MX parameter (const MX &symbol, const std::string &attribute="full")
 
void minimize (const MX &f, double linear_scale=1)
 Set objective. More...
 
void subject_to ()
 Clear constraints. More...
 
void solver (const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
 Set a solver. More...
 
Dict stats () const
 Get statistics. More...
 
std::string return_status () const
 Get return status of solver. More...
 
std::vector< 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
 
Function scale_helper (const Function &h) const
 Scale a helper function constructed via opti.x, opti.g, ... More...
 
MX dual (const MX &m) const
 get the dual variable More...
 
casadi_int nx () const
 Number of (scalarised) decision variables. More...
 
casadi_int np () const
 Number of (scalarised) parameters. More...
 
casadi_int ng () const
 Number of (scalarised) constraints. More...
 
MX x () const
 Get all (scalarised) decision variables as a symbolic column vector. More...
 
MX p () const
 Get all (scalarised) parameters as a symbolic column vector. More...
 
MX g () const
 Get all (scalarised) constraint expressions as a column vector. More...
 
MX f () const
 Get objective expression. More...
 
MX lbg () const
 Get all (scalarised) bounds on constraints as a column vector. More...
 
MX ubg () const
 
DM x_linear_scale () const
 
DM x_linear_scale_offset () const
 
DM g_linear_scale () const
 
double f_linear_scale () const
 
MX lam_g () const
 Get all (scalarised) dual variables as a symbolic column vector. More...
 
OptiAdvanced debug () const
 Get a copy with advanced functionality. More...
 
OptiAdvanced advanced () const
 Get a copy with advanced functionality. More...
 
Opti copy () const
 Get a copy of the. More...
 
void update_user_dict (const MX &m, const Dict &meta)
 add user data More...
 
void update_user_dict (const std::vector< MX > &m, const Dict &meta)
 
Dict user_dict (const MX &m) const
 Get user data. More...
 
std::string type_name () const
 Readable name of the class. More...
 
void disp (std::ostream &stream, bool more=false) const
 Print representation. More...
 
std::string get_str (bool more=false) const
 Get string representation. More...
 
 Opti (const Opti &x)
 
 ~Opti ()
 Destructor. More...
 
 Opti (OptiNode *node)
 
std::string class_name () const
 Get class name. More...
 
void print_ptr (std::ostream &stream=casadi::uout()) const
 
void own (SharedObjectInternal *node)
 
void assign (SharedObjectInternal *node)
 Assign the node to a node class pointer without reference counting. More...
 
SharedObjectInternalget () const
 Get a const pointer to the node. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
void swap (GenericShared &other)
 Swap content with another instance. More...
 
std::string debug_repr () const
 
bool is_null () const
 Is a null pointer? More...
 
casadi_int __hash__ () const
 Returns a number that is unique for a given Node. More...
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 
void subject_to (const MX &g, const Dict &options=Dict())
 Add constraints. More...
 
void subject_to (const std::vector< MX > &g, const Dict &options=Dict())
 Add constraints. More...
 
void subject_to (const MX &g, const DM &linear_scale, const Dict &options=Dict())
 Add constraints. More...
 
void subject_to (const std::vector< MX > &g, const DM &linear_scale, const Dict &options=Dict())
 Add constraints. More...
 
void set_initial (const MX &x, const DM &v)
 
void set_initial (const std::vector< MX > &assignments)
 
void set_value (const MX &x, const DM &v)
 Set value of parameter. More...
 
void set_value (const std::vector< MX > &assignments)
 Set value of parameter. More...
 
void set_domain (const MX &x, const std::string &domain)
 Set domain of a decision variable. More...
 
void set_linear_scale (const MX &x, const DM &scale, const DM &offset=0)
 Set scale of a decision variable. More...
 
OptiSol solve ()
 Crunch the numbers; solve the problem. More...
 
OptiSol solve_limited ()
 Crunch the numbers; solve the problem. More...
 
native_DM value (const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
 
native_DM value (const DM &x, const std::vector< MX > &values=std::vector< MX >()) const
 Set domain of a decision variable. More...
 
native_DM value (const SX &x, const std::vector< MX > &values=std::vector< MX >()) const
 Set domain of a decision variable. More...
 
Function to_function (const std::string &name, const std::vector< MX > &args, const std::vector< MX > &res, const Dict &opts=Dict())
 Create a CasADi Function from the Opti solver. More...
 
Function to_function (const std::string &name, const std::vector< MX > &args, const std::vector< MX > &res, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict())
 Create a CasADi Function from the Opti solver. More...
 
Function to_function (const std::string &name, const std::map< std::string, MX > &dict, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts=Dict())
 Create a CasADi Function from the Opti solver. More...
 
void callback_class (OptiCallback *callback)
 Helper methods for callback() More...
 
void callback_class ()
 Helper methods for callback() More...
 
OptiNodeoperator-> ()
 Access a member of the node. More...
 
const OptiNodeoperator-> () const
 Const access a member of the node. More...
 

Static Public Member Functions

static MX bounded (const MX &lb, const MX &expr, const MX &ub)
 Construct a double inequality. More...
 
static Opti create (OptiNode *node)
 

Protected Member Functions

void count_up ()
 
void count_down ()
 

Friends

class InternalOptiCallback
 

Member Typedef Documentation

◆ base_type

Definition at line 103 of file shared_object.hpp.

◆ internal_base_type

Definition at line 102 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ Opti() [1/3]

casadi::Opti::Opti ( const std::string &  problem_type = "nlp")
Parameters
[in]problem_typeof optimization 'nlp' or 'conic' (default nlp)

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

Definition at line 47 of file optistack.cpp.

47  {
48  own(OptiNode::create(problem_type));
49 }
static OptiNode * create(const std::string &problem_type)

References casadi::OptiNode::create(), and casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

Referenced by create().

◆ Opti() [2/3]

casadi::Opti::Opti ( const Opti x)

Definition at line 79 of file optistack.cpp.

79  : SharedObject(rhs) {
81 }
void callback_class()
Helper methods for callback()
Definition: optistack.cpp:471

References callback_class().

◆ ~Opti()

casadi::Opti::~Opti ( )
inline

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

Definition at line 464 of file optistack.hpp.

464 {}

◆ Opti() [3/3]

casadi::Opti::Opti ( OptiNode node)

Definition at line 75 of file optistack.cpp.

75  {
76  own(node);
77 }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::own().

Member Function Documentation

◆ __hash__()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::__hash__
inherited

If the Object does not point to any node, "0" is returned.

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

Definition at line 124 of file generic_shared_impl.hpp.

137  {
138  return reinterpret_cast<casadi_int>(get());
139  }

◆ advanced()

OptiAdvanced casadi::Opti::advanced ( ) const

You get access to more methods, but you have no guarantees about API stability

The copy is effectively a deep copy: Updating the state of the copy does not update the original.

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

Definition at line 850 of file optistack.cpp.

850  {
851  return copy();
852 }
Opti copy() const
Get a copy of the.
Definition: optistack.cpp:853

References copy().

◆ assign()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::assign ( Internal *  node)
inherited

improper use will cause memory leaks!

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

Definition at line 97 of file generic_shared_impl.hpp.

75  {
76  node = node_;
77  }

◆ bounded()

static MX casadi::Opti::bounded ( const MX lb,
const MX expr,
const MX ub 
)
inlinestatic

Constructs: lb(p) <= g(x,p) <= ub(p)

Python prohibits such syntax directly

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

Definition at line 397 of file optistack.hpp.

397 { return (lb<=expr)<= ub; }

◆ callback_class() [1/2]

void casadi::Opti::callback_class ( )

Do not use directly.

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

Definition at line 471 of file optistack.cpp.

471  {
472  try {
473  (*this)->callback_class();
474  } catch(std::exception& e) {
475  THROW_ERROR("callback_class", e.what());
476  }
477 }

Referenced by Opti().

◆ callback_class() [2/2]

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

Do not use directly.

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

Definition at line 463 of file optistack.cpp.

463  {
464  try {
465  (*this)->callback_class(callback);
466  } catch(std::exception& e) {
467  THROW_ERROR("callback_class", e.what());
468  }
469 }

◆ class_name()

std::string casadi::SharedObject::class_name ( ) const
inherited

◆ copy()

Opti casadi::Opti::copy ( ) const

The copy is effectively a deep copy: Updating the state of the copy does not update the original.

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

Definition at line 853 of file optistack.cpp.

853  {
854  return (*this)->copy();
855 }

References casadi::OptiNode::copy().

Referenced by advanced(), and debug().

◆ count_down()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_down
protectedinherited

Definition at line 134 of file generic_shared_impl.hpp.

42  {
43 #ifdef WITH_EXTRA_CHECKS
44  casadi_assert_dev(Function::call_depth_==0);
45 #endif // WITH_EXTRA_CHECKS
46  if (!node) return;
47  if (node->weak_ref_) {
48 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
49  auto mutex = node->weak_ref_->get_mutex();
50  // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
51  std::lock_guard<std::mutex> lock(*mutex);
52  // Could it be that this mutex is destroyed when the lock goes out of scope?
53 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
54 
55  if (--static_cast<Internal*>(node)->count == 0) {
56  delete node;
57  node = nullptr;
58  }
59  } else {
60  if (--static_cast<Internal*>(node)->count == 0) {
61  delete node;
62  node = nullptr;
63  }
64  }
65  }

◆ count_up()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_up
protectedinherited

Definition at line 133 of file generic_shared_impl.hpp.

32  {
33 #ifdef WITH_EXTRA_CHECKS
34  casadi_assert_dev(Function::call_depth_==0);
35 #endif // WITH_EXTRA_CHECKS
36 
37  if (node) static_cast<Internal*>(node)->count++;
38 
39  }

◆ create()

Opti casadi::Opti::create ( OptiNode node)
static

Definition at line 86 of file optistack.cpp.

86  {
87  return Opti(node);
88 }
Opti(const std::string &problem_type="nlp")
Create Opti Context.
Definition: optistack.cpp:47

References Opti().

Referenced by casadi::OptiNode::copy().

◆ debug()

OptiAdvanced casadi::Opti::debug ( ) const

You get access to more methods, but you have no guarantees about API stability

The copy is effectively a deep copy: Updating the state of the copy does not update the original.

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

Definition at line 847 of file optistack.cpp.

847  {
848  return copy();
849 }

References copy().

Referenced by disp().

◆ debug_repr()

std::string casadi::GenericShared< SharedObject , SharedObjectInternal >::debug_repr
inherited

Definition at line 113 of file generic_shared_impl.hpp.

80  {
81  if (node) {
82  return node->debug_repr(node);
83  } else {
84  return "NULL";
85  }
86  }
std::string debug_repr(const Internal *) const

◆ disp()

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

Definition at line 750 of file optistack.cpp.

750  {
751  stream << "Opti {" << std::endl;
752  OptiAdvanced mycopy = debug();
753  stream << " instance #" << mycopy.instance_number() << std::endl;
754  if (mycopy.problem_dirty()) mycopy.bake();
755  stream << " #variables: " << mycopy.active_symvar(OPTI_VAR).size()
756  << " (nx = " << mycopy.nx() << ")" << std::endl;
757  stream << " #parameters: " << mycopy.active_symvar(OPTI_PAR).size()
758  << " (np = " << mycopy.np() << ")" << std::endl;
759  stream << " #constraints: " << mycopy.active_symvar(OPTI_DUAL_G).size()
760  << " (ng = " << mycopy.ng() << ")" << std::endl;
761  if (mycopy.solver_dirty()) {
762  stream << " CasADi solver needs updating." << std::endl;
763  } else {
764  stream << " CasADi solver allocated." << std::endl;
765  }
766  if (mycopy.solved()) {
767  stream << " CasADi solver was called: " << mycopy.return_status() << std::endl;
768  }
769  stream << "}";
770 }
OptiAdvanced debug() const
Get a copy with advanced functionality.
Definition: optistack.cpp:847
@ OPTI_VAR
Definition: optistack.hpp:496
@ OPTI_DUAL_G
Definition: optistack.hpp:498
@ OPTI_PAR
Definition: optistack.hpp:497

References casadi::OptiAdvanced::active_symvar(), casadi::OptiAdvanced::bake(), debug(), casadi::OptiAdvanced::instance_number(), ng(), np(), nx(), casadi::OPTI_DUAL_G, casadi::OPTI_PAR, casadi::OPTI_VAR, casadi::OptiAdvanced::problem_dirty(), return_status(), casadi::OptiAdvanced::solved(), and casadi::OptiAdvanced::solver_dirty().

Referenced by casadi::OptiSol::disp(), and get_str().

◆ dual()

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

m must be a constraint expression. The returned value is still a symbolic expression. Use value on it to obtain the numerical value.

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

Definition at line 302 of file optistack.cpp.

302  {
303  try {
304  return (*this)->dual(m);
305  } catch(std::exception& e) {
306  THROW_ERROR("dual", e.what());
307  }
308 }

◆ f()

MX casadi::Opti::f ( ) const

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

Definition at line 358 of file optistack.cpp.

358  {
359  try {
360  return (*this)->f();
361  } catch(std::exception& e) {
362  THROW_ERROR("f", e.what());
363  }
364 }

Referenced by minimize(), and casadi::OptiAdvanced::objective().

◆ f_linear_scale()

double casadi::Opti::f_linear_scale ( ) const

Definition at line 415 of file optistack.cpp.

415  {
416  try {
417  return (*this)->f_linear_scale();
418  } catch(std::exception& e) {
419  THROW_ERROR("f_linear_scale", e.what());
420  }
421 }

◆ g()

MX casadi::Opti::g ( ) const

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

Definition at line 350 of file optistack.cpp.

350  {
351  try {
352  return (*this)->g();
353  } catch(std::exception& e) {
354  THROW_ERROR("g", e.what());
355  }
356 }

Referenced by subject_to().

◆ g_linear_scale()

DM casadi::Opti::g_linear_scale ( ) const

Definition at line 407 of file optistack.cpp.

407  {
408  try {
409  return (*this)->g_linear_scale();
410  } catch(std::exception& e) {
411  THROW_ERROR("g_linear_scale", e.what());
412  }
413 }

◆ get()

Definition at line 100 of file generic_shared_impl.hpp.

104  {
105  return node;
106  }

◆ get_str()

std::string casadi::Opti::get_str ( bool  more = false) const

Definition at line 772 of file optistack.cpp.

772  {
773  std::stringstream ss;
774  disp(ss, more);
775  return ss.str();
776 }
void disp(std::ostream &stream, bool more=false) const
Print representation.
Definition: optistack.cpp:750

References disp().

Referenced by casadi::OptiSol::get_str().

◆ getCount()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::getCount
inherited

Definition at line 103 of file generic_shared_impl.hpp.

127  {
128  return (*this)->getCount();
129  }

◆ initial()

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

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

Definition at line 278 of file optistack.cpp.

278  {
279  try {
280  return (*this)->initial();
281  } catch(std::exception& e) {
282  THROW_ERROR("initial", e.what());
283  }
284 }

◆ is_null()

Definition at line 117 of file generic_shared_impl.hpp.

109  {
110  return node==nullptr;
111  }

◆ lam_g()

MX casadi::Opti::lam_g ( ) const

Useful for obtaining the Lagrange Hessian:

* sol.value(hessian(opti.f+opti.lam_g'*opti.g,opti.x)) % MATLAB
* sol.value(hessian(opti.f+dot(opti.lam_g,opti.g),opti.x)[0]) # Python
* 

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

Definition at line 383 of file optistack.cpp.

383  {
384  try {
385  return (*this)->lam_g();
386  } catch(std::exception& e) {
387  THROW_ERROR("lam_g", e.what());
388  }
389 }

◆ lbg()

MX casadi::Opti::lbg ( ) const

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

Definition at line 366 of file optistack.cpp.

366  {
367  try {
368  return (*this)->lbg();
369  } catch(std::exception& e) {
370  THROW_ERROR("lbg", e.what());
371  }
372 }

◆ minimize()

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

Objective must be a scalar. Default objective: 0 When method is called multiple times, the last call takes effect

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

Definition at line 114 of file optistack.cpp.

114  {
115  try {
116  (*this)->minimize(f, linear_scale);
117  } catch(std::exception& e) {
118  THROW_ERROR("minimize", e.what());
119  }
120 }
MX f() const
Get objective expression.
Definition: optistack.cpp:358

References f().

◆ ng()

casadi_int casadi::Opti::ng ( ) const

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

Definition at line 326 of file optistack.cpp.

326  {
327  try {
328  return (*this)->ng();
329  } catch(std::exception& e) {
330  THROW_ERROR("ng", e.what());
331  }
332 }

Referenced by disp().

◆ np()

casadi_int casadi::Opti::np ( ) const

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

Definition at line 318 of file optistack.cpp.

318  {
319  try {
320  return (*this)->np();
321  } catch(std::exception& e) {
322  THROW_ERROR("nx", e.what());
323  }
324 }

Referenced by disp().

◆ nx()

casadi_int casadi::Opti::nx ( ) const

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

Definition at line 310 of file optistack.cpp.

310  {
311  try {
312  return (*this)->nx();
313  } catch(std::exception& e) {
314  THROW_ERROR("nx", e.what());
315  }
316 }

Referenced by disp().

◆ operator->() [1/2]

OptiNode * casadi::Opti::operator-> ( )

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

Definition at line 38 of file optistack.cpp.

38  {
39  return static_cast<OptiNode*>(SharedObject::operator->());
40 }
SharedObjectInternal * operator->() const
Access a member function or object.

References casadi::GenericShared< SharedObject, SharedObjectInternal >::operator->().

◆ operator->() [2/2]

const OptiNode * casadi::Opti::operator-> ( ) const

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

Definition at line 42 of file optistack.cpp.

42  {
43  return static_cast<const OptiNode*>(SharedObject::operator->());
44 }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::operator->().

◆ own()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::own ( Internal *  node)
inherited

Assign the node to a node class pointer (or null)

Definition at line 90 of file generic_shared_impl.hpp.

◆ p()

MX casadi::Opti::p ( ) const

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

Definition at line 342 of file optistack.cpp.

342  {
343  try {
344  return (*this)->p();
345  } catch(std::exception& e) {
346  THROW_ERROR("p", e.what());
347  }
348 }

◆ parameter() [1/3]

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

The order of creation does not matter. It is not required for parameter to actualy appear in the optimization problem. Parameters that do appear, must be given a value before the problem can be solved.

Parameters
[in]nnumber of rows (default 1)
[in]mnumber of columnss (default 1)
[in]attribute'full' (default) or 'symmetric'

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

Definition at line 90 of file optistack.cpp.

90  {
91  try {
92  return (*this)->parameter(n, m, attribute);
93  } catch(std::exception& e) {
94  THROW_ERROR("parameter", e.what());
95  }
96 }

◆ parameter() [2/3]

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

Definition at line 106 of file optistack.cpp.

106  {
107  try {
108  return (*this)->parameter(symbol, attribute);
109  } catch(std::exception& e) {
110  THROW_ERROR("parameter", e.what());
111  }
112 }

◆ parameter() [3/3]

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

Definition at line 98 of file optistack.cpp.

98  {
99  try {
100  return (*this)->parameter(sp, attribute);
101  } catch(std::exception& e) {
102  THROW_ERROR("parameter", e.what());
103  }
104 }

◆ print_ptr()

void casadi::SharedObject::print_ptr ( std::ostream &  stream = casadi::uout()) const
inherited

Print the pointer to the internal class

Definition at line 43 of file shared_object.cpp.

43  {
44  stream << get();
45  }
SharedObjectInternal * get() const
Get a const pointer to the node.

References casadi::GenericShared< SharedObject, SharedObjectInternal >::get().

◆ return_status()

std::string casadi::Opti::return_status ( ) const
   passed as-is from nlpsol

No stability can be guaranteed about this part of the API

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

Definition at line 270 of file optistack.cpp.

270  {
271  try {
272  return (*this)->return_status();
273  } catch(std::exception& e) {
274  THROW_ERROR("return_status", e.what());
275  }
276 }

Referenced by disp().

◆ scale_helper()

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

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

Definition at line 254 of file optistack.cpp.

254  {
255  try {
256  return (*this)->scale_helper(h);
257  } catch(std::exception& e) {
258  THROW_ERROR("scale_helper", e.what());
259  }
260 }

Referenced by casadi::OptiNode::scale_helper().

◆ set_domain()

void casadi::Opti::set_domain ( const MX x,
const std::string &  domain 
)
Parameters
[in]xdecision variable
[in]type'real', 'integer' (default: real)
* opti.set_domain(x, "real")
* opti.set_domain(x, "integer")
* 

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

Definition at line 189 of file optistack.cpp.

189  {
190  try {
191  (*this)->set_domain(x, domain);
192  } catch(std::exception& e) {
193  THROW_ERROR("set_domain", e.what());
194  }
195 }
MX x() const
Get all (scalarised) decision variables as a symbolic column vector.
Definition: optistack.cpp:334

References x().

◆ set_initial() [1/2]

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

Set initial guess for decision variables

* opti.set_initial(x, 2)
* opti.set_initial(10*x(1), 2)
* 

Definition at line 165 of file optistack.cpp.

165  {
166  try {
167  (*this)->set_initial(x, v);
168  } catch(std::exception& e) {
169  THROW_ERROR("set_initial", e.what());
170  }
171 }

References x().

◆ set_initial() [2/2]

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

Set initial guess for decision variables

* opti.set_initial(x, 2)
* opti.set_initial(10*x(1), 2)
* 

Definition at line 172 of file optistack.cpp.

172  {
173  try {
174  (*this)->set_initial(assignments);
175  } catch(std::exception& e) {
176  THROW_ERROR("set_initial", e.what());
177  }
178 }

◆ set_linear_scale()

void casadi::Opti::set_linear_scale ( const MX x,
const DM scale,
const DM offset = 0 
)

(x-offset)/scale will be used in the optimization problem

Parameters
[in]xdecision variable
[in]scalescaling value (default: 1)
[in]offsetscaling value (default: 0)
* opti.set_linear_scale(x, 20)
* opti.set_linear_scale(x, 20, 273.15)
* 

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

Definition at line 197 of file optistack.cpp.

197  {
198  try {
199  (*this)->set_linear_scale(x, scale, offset);
200  } catch(std::exception& e) {
201  THROW_ERROR("set_linear_scale", e.what());
202  }
203 }

References x().

◆ set_value() [1/2]

void casadi::Opti::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_1d

Definition at line 181 of file optistack.cpp.

181  {
182  try {
183  (*this)->set_value(x, v);
184  } catch(std::exception& e) {
185  THROW_ERROR("set_value", e.what());
186  }
187 }

References x().

◆ set_value() [2/2]

void casadi::Opti::set_value ( const std::vector< MX > &  assignments)

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

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

Definition at line 205 of file optistack.cpp.

205  {
206  try {
207  (*this)->set_value(assignments);
208  } catch(std::exception& e) {
209  THROW_ERROR("set_value", e.what());
210  }
211 }

◆ solve()

OptiSol casadi::Opti::solve ( )

Definition at line 213 of file optistack.cpp.

213  {
214  try {
215  return (*this)->solve(false);
216  } catch(std::exception& e) {
217  THROW_ERROR("solve", e.what());
218  }
219 }

◆ solve_limited()

OptiSol casadi::Opti::solve_limited ( )

Allows the solver to return without error when an iteration or time limit is reached

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

Definition at line 221 of file optistack.cpp.

221  {
222  try {
223  return (*this)->solve(true);
224  } catch(std::exception& e) {
225  THROW_ERROR("solve", e.what());
226  }
227 }

◆ solver()

void casadi::Opti::solver ( const std::string &  solver,
const Dict plugin_options = Dict(),
const Dict solver_options = Dict() 
)
Parameters
[in]solverany of the nlpsol plugins can be used here In practice, not all nlpsol plugins may be supported yet
[in]optionspassed on to nlpsol plugin No stability can be guaranteed about this part of the API
[in]optionsto be passed to nlpsol solver No stability can be guaranteed about this part of the API

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

Definition at line 155 of file optistack.cpp.

157  {
158  try {
159  (*this)->solver(solver, plugin_options, solver_options);
160  } catch(std::exception& e) {
161  THROW_ERROR("solver", e.what());
162  }
163 }
void solver(const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
Set a solver.
Definition: optistack.cpp:155

◆ stats()

Dict casadi::Opti::stats ( ) const

nlpsol stats are passed as-is. No stability can be guaranteed about this part of the API

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

Definition at line 262 of file optistack.cpp.

262  {
263  try {
264  return (*this)->stats();
265  } catch(std::exception& e) {
266  THROW_ERROR("stats", e.what());
267  }
268 }

Referenced by casadi::OptiSol::stats().

◆ subject_to() [1/5]

void casadi::Opti::subject_to ( )

Definition at line 146 of file optistack.cpp.

146  {
147  try {
148  (*this)->subject_to();
149  } catch(std::exception& e) {
150  THROW_ERROR("subject_to", e.what());
151  }
152 }

Referenced by subject_to().

◆ subject_to() [2/5]

void casadi::Opti::subject_to ( const MX g,
const Dict options = Dict() 
)

Examples:

* \begin{itemize}
* opti.subject_to( sqrt(x+y) >= 1);
* opti.subject_to( sqrt(x+y) > 1)}: same as above
* opti.subject_to( 1<= sqrt(x+y) )}: same as above
* opti.subject_to( 5*x+y==1 )}: equality
*
* Python
* opti.subject_to([x*y>=1,x==3])
* opti.subject_to(opti.bounded(0,x,1))
*
* MATLAB
* opti.subject_to({x*y>=1,x==3})
* opti.subject_to( 0<=x<=1 )
* 

Related functionalities:

  • opti.lbg,opti.g,opti.ubg represent the vector of flattened constraints
  • opti.debug.show_infeasibilities() may be used to inspect which constraints are violated

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

Definition at line 122 of file optistack.cpp.

122  {
123  try {
124  (*this)->subject_to(g, 1, options);
125  } catch(std::exception& e) {
126  THROW_ERROR("subject_to", e.what());
127  }
128 }
MX g() const
Get all (scalarised) constraint expressions as a column vector.
Definition: optistack.cpp:350

References g().

◆ subject_to() [3/5]

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

Examples:

* \begin{itemize}
* opti.subject_to( sqrt(x+y) >= 1);
* opti.subject_to( sqrt(x+y) > 1)}: same as above
* opti.subject_to( 1<= sqrt(x+y) )}: same as above
* opti.subject_to( 5*x+y==1 )}: equality
*
* Python
* opti.subject_to([x*y>=1,x==3])
* opti.subject_to(opti.bounded(0,x,1))
*
* MATLAB
* opti.subject_to({x*y>=1,x==3})
* opti.subject_to( 0<=x<=1 )
* 

Related functionalities:

  • opti.lbg,opti.g,opti.ubg represent the vector of flattened constraints
  • opti.debug.show_infeasibilities() may be used to inspect which constraints are violated

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

Definition at line 134 of file optistack.cpp.

134  {
135  try {
136  (*this)->subject_to(g, linear_scale, options);
137  } catch(std::exception& e) {
138  THROW_ERROR("subject_to", e.what());
139  }
140 }

References g().

◆ subject_to() [4/5]

void casadi::Opti::subject_to ( const std::vector< MX > &  g,
const Dict options = Dict() 
)

Examples:

* \begin{itemize}
* opti.subject_to( sqrt(x+y) >= 1);
* opti.subject_to( sqrt(x+y) > 1)}: same as above
* opti.subject_to( 1<= sqrt(x+y) )}: same as above
* opti.subject_to( 5*x+y==1 )}: equality
*
* Python
* opti.subject_to([x*y>=1,x==3])
* opti.subject_to(opti.bounded(0,x,1))
*
* MATLAB
* opti.subject_to({x*y>=1,x==3})
* opti.subject_to( 0<=x<=1 )
* 

Related functionalities:

  • opti.lbg,opti.g,opti.ubg represent the vector of flattened constraints
  • opti.debug.show_infeasibilities() may be used to inspect which constraints are violated

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

Definition at line 130 of file optistack.cpp.

130  {
131  for (const auto& gs : g) subject_to(gs, 1, options);
132 }
void subject_to()
Clear constraints.
Definition: optistack.cpp:146

References g(), and subject_to().

◆ subject_to() [5/5]

void casadi::Opti::subject_to ( const std::vector< MX > &  g,
const DM linear_scale,
const Dict options = Dict() 
)

Examples:

* \begin{itemize}
* opti.subject_to( sqrt(x+y) >= 1);
* opti.subject_to( sqrt(x+y) > 1)}: same as above
* opti.subject_to( 1<= sqrt(x+y) )}: same as above
* opti.subject_to( 5*x+y==1 )}: equality
*
* Python
* opti.subject_to([x*y>=1,x==3])
* opti.subject_to(opti.bounded(0,x,1))
*
* MATLAB
* opti.subject_to({x*y>=1,x==3})
* opti.subject_to( 0<=x<=1 )
* 

Related functionalities:

  • opti.lbg,opti.g,opti.ubg represent the vector of flattened constraints
  • opti.debug.show_infeasibilities() may be used to inspect which constraints are violated

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

Definition at line 142 of file optistack.cpp.

142  {
143  for (const auto& gs : g) subject_to(gs, linear_scale, options);
144 }

References g(), and subject_to().

◆ swap()

Definition at line 106 of file generic_shared_impl.hpp.

120  {
121  GenericShared<Shared, Internal> temp = *this;
122  *this = other;
123  other = temp;
124  }

◆ to_function() [1/3]

Function casadi::Opti::to_function ( const std::string &  name,
const std::map< std::string, MX > &  dict,
const std::vector< std::string > &  name_in,
const std::vector< std::string > &  name_out,
const Dict opts = Dict() 
)
Parameters
[in]nameName of the resulting CasADi Function
[in]argsList of parameters and decision/dual variables (which can be given an initial guess) with the resulting Function
[in]resList of expressions that will get evaluated at the optimal solution
[in]optsStandard CasADi Funcion options

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

Definition at line 441 of file optistack.cpp.

445  {
446  std::vector<MX> ex_in(name_in.size()), ex_out(name_out.size());
447  for (auto&& i : dict) {
448  std::vector<std::string>::const_iterator it;
449  if ((it=find(name_in.begin(), name_in.end(), i.first))!=name_in.end()) {
450  // Input expression
451  ex_in[it-name_in.begin()] = i.second;
452  } else if ((it=find(name_out.begin(), name_out.end(), i.first))!=name_out.end()) {
453  // Output expression
454  ex_out[it-name_out.begin()] = i.second;
455  } else {
456  // Neither
457  casadi_error("Unknown dictionary entry: '" + i.first + "'");
458  }
459  }
460  return to_function(name, ex_in, ex_out, name_in, name_out, opts);
461 }
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
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros

References casadi::find(), and to_function().

◆ to_function() [2/3]

Function casadi::Opti::to_function ( const std::string &  name,
const std::vector< MX > &  args,
const std::vector< MX > &  res,
const Dict opts = Dict() 
)
Parameters
[in]nameName of the resulting CasADi Function
[in]argsList of parameters and decision/dual variables (which can be given an initial guess) with the resulting Function
[in]resList of expressions that will get evaluated at the optimal solution
[in]optsStandard CasADi Funcion options

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

Definition at line 435 of file optistack.cpp.

437  {
438  return to_function(name, args, res, {}, {}, opts);
439 }

Referenced by to_function(), and casadi::OptiNode::to_function().

◆ to_function() [3/3]

Function casadi::Opti::to_function ( const std::string &  name,
const std::vector< MX > &  args,
const std::vector< MX > &  res,
const std::vector< std::string > &  name_in,
const std::vector< std::string > &  name_out,
const Dict opts = Dict() 
)
Parameters
[in]nameName of the resulting CasADi Function
[in]argsList of parameters and decision/dual variables (which can be given an initial guess) with the resulting Function
[in]resList of expressions that will get evaluated at the optimal solution
[in]optsStandard CasADi Funcion options

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

Definition at line 423 of file optistack.cpp.

427  {
428  try {
429  return (*this)->to_function(name, args, res, name_in, name_out, opts);
430  } catch(std::exception& e) {
431  THROW_ERROR("to_function", e.what());
432  }
433 }

◆ type_name()

std::string casadi::Opti::type_name ( ) const
inline

Definition at line 440 of file optistack.hpp.

440 { return "Opti"; }

◆ ubg()

MX casadi::Opti::ubg ( ) const

Definition at line 374 of file optistack.cpp.

374  {
375  try {
376  return (*this)->ubg();
377  } catch(std::exception& e) {
378  THROW_ERROR("ubg", e.what());
379  }
380 }

◆ update_user_dict() [1/2]

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

Add arbitrary data in the form of a dictionary to symbols or constraints

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

Definition at line 479 of file optistack.cpp.

479  {
480  try {
481  (*this)->update_user_dict(m, meta);
482  } catch(std::exception& e) {
483  THROW_ERROR("update_user_dict", e.what());
484  }
485 }

Referenced by update_user_dict().

◆ update_user_dict() [2/2]

void casadi::Opti::update_user_dict ( const std::vector< MX > &  m,
const Dict meta 
)

Definition at line 487 of file optistack.cpp.

487  {
488  for (const auto& m : ms)
489  update_user_dict(m, meta);
490 }
void update_user_dict(const MX &m, const Dict &meta)
add user data
Definition: optistack.cpp:479

References update_user_dict().

◆ user_dict()

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

Definition at line 492 of file optistack.cpp.

492  {
493  try {
494  return (*this)->user_dict(m);
495  } catch(std::exception& e) {
496  THROW_ERROR("user_dict", e.what());
497  }
498 }

◆ value() [1/3]

DM casadi::Opti::value ( const DM x,
const std::vector< MX > &  values = std::vector<MX>() 
) const
Parameters
[in]xdecision variable
[in]type'real', 'integer' (default: real)
* opti.set_domain(x, "real")
* opti.set_domain(x, "integer")
* 

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

Definition at line 238 of file optistack.cpp.

238  {
239  try {
240  return (*this)->value(x, values);
241  } catch(std::exception& e) {
242  THROW_ERROR("value", e.what());
243  }
244 }

References x().

◆ value() [2/3]

DM casadi::Opti::value ( const MX x,
const std::vector< MX > &  values = std::vector<MX>() 
) const

Obtain value of expression at the current value

In regular mode, teh current value is the converged solution In debug mode, the value can be non-converged

Parameters
[in]valuesOptional assignment expressions (e.g. x==3) to overrule the current value

Definition at line 229 of file optistack.cpp.

229  {
230  try {
231  return (*this)->value(x, values);
232  } catch(std::exception& e) {
233  THROW_ERROR("value", e.what());
234  }
235 }

References x().

Referenced by casadi::OptiSol::value().

◆ value() [3/3]

DM casadi::Opti::value ( const SX x,
const std::vector< MX > &  values = std::vector<MX>() 
) const
Parameters
[in]xdecision variable
[in]type'real', 'integer' (default: real)
* opti.set_domain(x, "real")
* opti.set_domain(x, "integer")
* 

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

Definition at line 246 of file optistack.cpp.

246  {
247  try {
248  return (*this)->value(x, values);
249  } catch(std::exception& e) {
250  THROW_ERROR("value", e.what());
251  }
252 }

References x().

◆ value_parameters()

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

Definition at line 294 of file optistack.cpp.

294  {
295  try {
296  return (*this)->value_parameters();
297  } catch(std::exception& e) {
298  THROW_ERROR("value_parameters", e.what());
299  }
300 }

Referenced by casadi::OptiSol::value_parameters().

◆ value_variables()

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

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

Definition at line 286 of file optistack.cpp.

286  {
287  try {
288  return (*this)->value_variables();
289  } catch(std::exception& e) {
290  THROW_ERROR("value_variables", e.what());
291  }
292 }

Referenced by casadi::OptiSol::value_variables().

◆ variable() [1/3]

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

The order of creation matters. The order will be reflected in the optimization problem. It is not required for decision variables to actualy appear in the optimization problem.

Parameters
[in]nnumber of rows (default 1)
[in]mnumber of columnss (default 1)
[in]attribute'full' (default) or 'symmetric'

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

Definition at line 51 of file optistack.cpp.

51  {
52  try {
53  return (*this)->variable(n, m, attribute);
54  } catch(std::exception& e) {
55  THROW_ERROR("variable", e.what());
56  }
57 }

◆ variable() [2/3]

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

Definition at line 67 of file optistack.cpp.

67  {
68  try {
69  return (*this)->variable(symbol, attribute);
70  } catch(std::exception& e) {
71  THROW_ERROR("variable", e.what());
72  }
73 }

◆ variable() [3/3]

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

Definition at line 59 of file optistack.cpp.

59  {
60  try {
61  return (*this)->variable(sp, attribute);
62  } catch(std::exception& e) {
63  THROW_ERROR("variable", e.what());
64  }
65 }

◆ weak()

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

Definition at line 131 of file generic_shared_impl.hpp.

132  {
133  return (*this)->weak();
134  }

◆ x()

MX casadi::Opti::x ( ) const

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

Definition at line 334 of file optistack.cpp.

334  {
335  try {
336  return (*this)->x();
337  } catch(std::exception& e) {
338  THROW_ERROR("x", e.what());
339  }
340 }

Referenced by casadi::OptiAdvanced::describe(), set_domain(), set_initial(), set_linear_scale(), set_value(), and value().

◆ x_linear_scale()

DM casadi::Opti::x_linear_scale ( ) const

Definition at line 391 of file optistack.cpp.

391  {
392  try {
393  return (*this)->x_linear_scale();
394  } catch(std::exception& e) {
395  THROW_ERROR("x_linear_scale", e.what());
396  }
397 }

◆ x_linear_scale_offset()

DM casadi::Opti::x_linear_scale_offset ( ) const

Definition at line 399 of file optistack.cpp.

399  {
400  try {
401  return (*this)->x_linear_scale_offset();
402  } catch(std::exception& e) {
403  THROW_ERROR("x_linear_scale_offset", e.what());
404  }
405 }

Friends And Related Function Documentation

◆ InternalOptiCallback

friend class InternalOptiCallback
friend

Definition at line 91 of file optistack.hpp.


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