optistack_internal.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 #ifndef CASADI_OPTISTACK_INTERNAL_HPP
26 #define CASADI_OPTISTACK_INTERNAL_HPP
27 
28 #include "optistack.hpp"
29 #include "shared_object_internal.hpp"
30 
31 namespace casadi {
32 
33 #ifndef SWIG
35 template<class T>
36 class null_ptr_on_copy {
37 public:
38  null_ptr_on_copy<T>() : ptr_(nullptr) {}
39  null_ptr_on_copy<T>(const null_ptr_on_copy<T>& rhs) : ptr_(nullptr) {}
40  void operator=(T* ptr) { ptr_ = ptr; }
41  T* operator->() { return ptr_; }
42  operator bool() const { return ptr_; }
43 private:
44  T* ptr_;
45 };
46 #endif
47 
55 class CASADI_EXPORT OptiNode :
56  public SharedObjectInternal {
57  friend class InternalOptiCallback;
58 public:
59 
61  OptiNode(const std::string& problem_type);
62 
65 
67  MX variable(casadi_int n=1, casadi_int m=1, const std::string& attribute="full");
68 
70  MX parameter(casadi_int n=1, casadi_int m=1, const std::string& attribute="full");
71 
73  void minimize(const MX& f);
74 
76  void subject_to(const MX& g);
78  void subject_to();
79 
81  void solver(const std::string& solver,
82  const Dict& plugin_options=Dict(),
83  const Dict& solver_options=Dict());
84 
87  void set_initial(const MX& x, const DM& v);
88  void set_initial(const std::vector<MX>& assignments);
90 
92 
97  void set_value(const MX& x, const DM& v);
98  void set_value(const std::vector<MX>& assignments);
100 
102  void set_domain(const MX& x, const std::string& domain);
103 
105  OptiSol solve(bool accept_limit);
106 
109  DM value(const MX& x, const std::vector<MX>& values=std::vector<MX>()) const;
110  DM value(const DM& x, const std::vector<MX>& values=std::vector<MX>()) const { return x; }
111  DM value(const SX& x, const std::vector<MX>& values=std::vector<MX>()) const {
112  return DM::nan(x.sparsity());
113  }
115 
117  Opti copy() const;
118 
120  Dict stats() const;
121 
123  std::string return_status() const;
125  bool return_success(bool accept_limit) const;
126 
129 
131  std::vector<MX> initial() const;
132 
134  std::vector<MX> value_variables() const;
135  std::vector<MX> value_parameters() const;
136 
137  void callback_class(OptiCallback* callback);
139  bool has_callback_class() const;
140 
142  bool is_parametric(const MX& expr) const;
143 
146  std::vector<MX> symvar() const;
147  std::vector<MX> symvar(const MX& expr) const;
148  std::vector<MX> symvar(const MX& expr, VariableType type) const;
150 
152  MetaCon canon_expr(const MX& expr) const;
153 
155  MetaVar get_meta(const MX& m) const;
156 
158  MetaCon get_meta_con(const MX& m) const;
159 
161  void set_meta(const MX& m, const MetaVar& meta);
162 
164  void set_meta_con(const MX& m, const MetaCon& meta);
165 
167  void update_user_dict(const MX& m, const Dict& meta);
168  Dict user_dict(const MX& m) const;
169 
171  MX dual(const MX& m) const;
172 
173  void assert_active_symbol(const MX& m) const;
174 
175  std::vector<MX> active_symvar(VariableType type) const;
176  std::vector<DM> active_values(VariableType type) const;
177 
178  MX x_lookup(casadi_int i) const;
179  MX g_lookup(casadi_int i) const;
180 
181  std::string x_describe(casadi_int i) const;
182  std::string g_describe(casadi_int i) const;
183  std::string describe(const MX& x, casadi_int indent=0) const;
184 
186  DMDict solve_actual(const DMDict& args);
187 
188  DMDict arg() const { return arg_; }
189  void res(const DMDict& res);
190  DMDict res() const { return res_; }
191  std::vector<MX> constraints() const { return g_; }
192  MX objective() const { return f_; }
193 
195  OptiAdvanced s = copy();
196  if (s.problem_dirty()) s.bake();
197  return s;
198  }
199 
200  std::string class_name() const override { return "OptiNode"; }
201 
203  casadi_int nx() const {
204  if (problem_dirty()) return baked_copy().nx();
205  return nlp_.at("x").size1();
206  }
207 
209  casadi_int np() const {
210  if (problem_dirty()) return baked_copy().np();
211  return nlp_.at("p").size1();
212  }
213 
215  casadi_int ng() const {
216  if (problem_dirty()) return baked_copy().ng();
217  return nlp_.at("g").size1();
218  }
219 
221  MX x() const {
222  if (problem_dirty()) return baked_copy().x();
223  return nlp_.at("x");
224  }
225 
227  MX p() const {
228  if (problem_dirty()) return baked_copy().p();
229  return nlp_.at("p");
230  }
231 
233  MX g() const {
234  if (problem_dirty()) return baked_copy().g();
235  return nlp_.at("g");
236  }
237 
239  MX f() const {
240  if (problem_dirty()) return baked_copy().f();
241  return nlp_.at("f");
242  }
243 
244  MX lbg() const {
245  if (problem_dirty()) return baked_copy().lbg();
246  return bounds_lbg_;
247  }
248 
249  MX ubg() const {
250  if (problem_dirty()) return baked_copy().ubg();
251  return bounds_ubg_;
252  }
253 
255  MX lam_g() const {
256  if (problem_dirty()) return baked_copy().lam_g();
257  return lam_;
258  }
259  void assert_empty() const;
260 
261 
265  Function to_function(const std::string& name,
266  const std::vector<MX>& args, const std::vector<MX>& res,
267  const std::vector<std::string>& name_in,
268  const std::vector<std::string>& name_out,
269  const Dict& opts);
270 
272  void disp(std::ostream& stream, bool more=false) const override;
273 
275  void bake();
276 
277  casadi_int instance_number() const;
278 
279  static OptiNode* create(const std::string& problem_type);
280 
282  void mark_problem_dirty(bool flag=true) { problem_dirty_=flag; mark_solver_dirty(); }
283  bool problem_dirty() const { return problem_dirty_; }
284 
286  void mark_solver_dirty(bool flag=true) { solver_dirty_=flag; mark_solved(false); }
287  bool solver_dirty() const { return solver_dirty_; }
288 
289  bool solved_;
290  void mark_solved(bool flag=true) { solved_ = flag;}
291  bool solved() const { return solved_; }
292 
293  void assert_solved() const;
294  void assert_baked() const;
295 
296 private:
297 
298  static std::map<VariableType, std::string> VariableType2String_;
299  std::string variable_type_to_string(VariableType vt) const;
300 
301  bool parse_opti_name(const std::string& name, VariableType& vt) const;
302  void register_dual(MetaCon& meta);
303 
305  void set_value_internal(const MX& x, const DM& v);
306 
315  static std::vector<MX> ineq_unchain(const MX& a, bool& SWIG_OUTPUT(flipped));
316 
318  const MetaVar& meta(const MX& m) const;
320  MetaVar& meta(const MX& m);
321 
323  const MetaCon& meta_con(const MX& m) const;
325  MetaCon& meta_con(const MX& m);
326 
328  std::vector<MX> sort(const std::vector<MX>& v) const;
329 
331  void assert_has(const MX& m) const;
332 
333  bool has(const MX& m) const;
334 
336  void assert_has_con(const MX& m) const;
337 
338  bool has_con(const MX& m) const;
339 
340  // Data members
341 
343  std::map<MXNode*, MetaVar> meta_;
345  std::map<MXNode*, MetaCon> meta_con_;
346 
348  std::vector<MX> symbols_;
349 
350  // Which x entries are discrete?
351  std::vector<bool> discrete_;
352 
354  casadi_int count_;
355 
356  casadi_int count_var_;
357  casadi_int count_par_;
358  casadi_int count_dual_;
359 
361  std::map< VariableType, std::vector<DM> > store_initial_, store_latest_;
362 
364  std::vector<bool> symbol_active_;
365 
367  Function solver_;
368 
370  DMDict res_;
371  DMDict arg_;
372  MXDict nlp_;
373  MX lam_;
374 
376  Function bounds_;
377  MX bounds_lbg_;
378  MX bounds_ubg_;
379  std::vector<bool> equality_;
380 
382  std::vector<MX> g_;
383 
385  MX f_;
386 
388  std::string problem_type_;
389 
390  null_ptr_on_copy<OptiCallback> user_callback_;
391  Function callback_;
392 
393  bool old_callback() const;
394 
395  std::string solver_name_;
396  Dict solver_options_;
397  Dict solver_options_all_;
398 
399  void assert_only_opti_symbols(const MX& e) const;
400  void assert_only_opti_nondual(const MX& e) const;
401 
402 
403  static casadi_int instance_count_;
404  casadi_int instance_number_;
405 
406 
407  std::string name_prefix() const;
408 
409  static std::string format_stacktrace(const Dict& stacktrace, casadi_int indent);
410 
411 };
412 
413 
414 } // namespace casadi
415 
416 #endif // CASADI_OPTISTACK_INTERNAL_HPP
Function object.
Definition: function.hpp:60
Sparsity sparsity() const
Get the sparsity pattern.
MX - Matrix expression.
Definition: mx.hpp:84
Sparse matrix class. SX and DM are specializations.
Definition: matrix_decl.hpp:92
static Matrix< Scalar > nan(const Sparsity &sp)
create a matrix with all nan
bool problem_dirty() const
void bake()
Fix the structure of the optimization problem.
A simplified interface for NLP modeling/solving.
std::string g_describe(casadi_int i) const
Dict user_dict(const MX &m) const
std::vector< MX > constraints() const
MX g() const
Get all (scalarised) constraint expressions as a column vector.
std::string describe(const MX &x, casadi_int indent=0) const
MetaCon get_meta_con(const MX &m) const
Get meta-data of symbol (for internal use only)
OptiAdvanced baked_copy() const
MX dual(const MX &m) const
get the dual variable
MX x() const
Get all (scalarised) decision variables as a symbolic column vector.
casadi_int nx() const
Number of (scalarised) decision variables.
MX variable(casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
Create a decision variable (symbol)
void bake()
Fix the structure of the optimization problem.
casadi_int instance_number() const
DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
void disp(std::ostream &stream, bool more=false) const override
Print representation.
std::vector< MX > active_symvar(VariableType type) const
void set_domain(const MX &x, const std::string &domain)
Set domain of variable.
static OptiNode * create(const std::string &problem_type)
OptiSol solve(bool accept_limit)
Crunch the numbers; solve the problem.
void res(const DMDict &res)
casadi_int np() const
Number of (scalarised) parameters.
bool has_callback_class() const
MX g_lookup(casadi_int i) const
void subject_to(const MX &g)
brief Add constraints
casadi_int ng() const
Number of (scalarised) constraints.
MetaVar get_meta(const MX &m) const
Get meta-data of symbol (for internal use only)
void subject_to()
Clear constraints.
void minimize(const MX &f)
Set objective.
void assert_baked() const
void update_user_dict(const MX &m, const Dict &meta)
add meta-data of an expression
void assert_solved() const
std::vector< MX > initial() const
get assignment expressions for initial values
void set_meta(const MX &m, const MetaVar &meta)
Set meta-data of an expression.
std::vector< DM > active_values(VariableType type) const
std::string class_name() const override
MX parameter(casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
Create a parameter (symbol); fixed during optimization.
void set_initial(const std::vector< MX > &assignments)
DM value(const DM &x, const std::vector< MX > &values=std::vector< MX >()) const
bool is_parametric(const MX &expr) const
return true if expression is only dependant on Opti parameters, not variables
void assert_active_symbol(const MX &m) const
bool return_success(bool accept_limit) const
Did the solver return successfully?
bool problem_dirty() const
Function to_function(const std::string &name, const std::vector< MX > &args, const std::vector< MX > &res, const std::vector< std::string > &name_in, const std::vector< std::string > &name_out, const Dict &opts)
Create a CasADi Function from the Opti solver.
std::vector< MX > symvar() const
void callback_class(OptiCallback *callback)
std::vector< MX > value_variables() const
get assignment expressions for latest values
Opti copy() const
Copy.
MX x_lookup(casadi_int i) const
std::vector< MX > value_parameters() const
void mark_problem_dirty(bool flag=true)
OptiNode(const std::string &problem_type)
Create Opti Context.
std::string return_status() const
Get return status of solver.
~OptiNode()
Destructor.
std::vector< MX > symvar(const MX &expr, VariableType type) const
void assert_empty() const
void set_initial(const MX &x, const DM &v)
void mark_solver_dirty(bool flag=true)
MX p() const
Get all (scalarised) parameters as a symbolic column vector.
void set_value(const MX &x, const DM &v)
Set value of parameter.
MX lam_g() const
Get dual variables as a symbolic column vector.
MetaCon canon_expr(const MX &expr) const
Interpret an expression (for internal use only)
void set_value(const std::vector< MX > &assignments)
Set value of parameter.
DM value(const SX &x, const std::vector< MX > &values=std::vector< MX >()) const
std::string x_describe(casadi_int i) const
void solver(const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
Solver.
MX f() const
Get objective expression.
void mark_solved(bool flag=true)
Function casadi_solver() const
Get the underlying CasADi solver of the Opti stack.
std::vector< MX > symvar(const MX &expr) const
DMDict solve_actual(const DMDict &args)
Dict stats() const
Get statistics.
void set_meta_con(const MX &m, const MetaCon &meta)
Set meta-data of an expression.
A simplified interface for NLP modeling/solving.
Definition: optistack.hpp:628
A simplified interface for NLP modeling/solving.
Definition: optistack.hpp:90
The casadi namespace.
std::map< std::string, MX > MXDict
Definition: mx.hpp:943
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::map< std::string, DM > DMDict
Definition: dm_fwd.hpp:36