optistack.cpp
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 #include "optistack_internal.hpp"
26 #include "exception.hpp"
27 #include "global_options.hpp"
28 
29 namespace casadi {
30 
31 // Throw informative error message
32 #define THROW_ERROR(FNAME, WHAT) \
33 throw CasadiException("Error in Opti::" FNAME " "\
34  "[" + this->class_name() + "] at " + CASADI_WHERE + ":\n"\
35  + std::string(WHAT));
36 
37 
39  return static_cast<OptiNode*>(SharedObject::operator->());
40 }
41 
42 const OptiNode* Opti::operator->() const {
43  return static_cast<const OptiNode*>(SharedObject::operator->());
44 }
45 
46 
47 Opti::Opti(const std::string& problem_type) {
48  own(OptiNode::create(problem_type));
49 }
50 
51 MX Opti::variable(casadi_int n, casadi_int m, const std::string& attribute) {
52  try {
53  return (*this)->variable(n, m, attribute);
54  } catch(std::exception& e) {
55  THROW_ERROR("variable", e.what());
56  }
57 }
58 
59 MX Opti::variable(const Sparsity& sp, const std::string& attribute) {
60  try {
61  return (*this)->variable(sp, attribute);
62  } catch(std::exception& e) {
63  THROW_ERROR("variable", e.what());
64  }
65 }
66 
67 MX Opti::variable(const MX& symbol, const std::string& attribute) {
68  try {
69  return (*this)->variable(symbol, attribute);
70  } catch(std::exception& e) {
71  THROW_ERROR("variable", e.what());
72  }
73 }
74 
76  own(node);
77 }
78 
79 Opti::Opti(const Opti& rhs) : SharedObject(rhs) {
81 }
82 
83 OptiAdvanced::OptiAdvanced(const Opti& rhs) : Opti(rhs) {
84 }
85 
87  return Opti(node);
88 }
89 
90 MX Opti::parameter(casadi_int n, casadi_int m, const std::string& attribute) {
91  try {
92  return (*this)->parameter(n, m, attribute);
93  } catch(std::exception& e) {
94  THROW_ERROR("parameter", e.what());
95  }
96 }
97 
98 MX Opti::parameter(const Sparsity& sp, const std::string& attribute) {
99  try {
100  return (*this)->parameter(sp, attribute);
101  } catch(std::exception& e) {
102  THROW_ERROR("parameter", e.what());
103  }
104 }
105 
106 MX Opti::parameter(const MX& symbol, const std::string& attribute) {
107  try {
108  return (*this)->parameter(symbol, attribute);
109  } catch(std::exception& e) {
110  THROW_ERROR("parameter", e.what());
111  }
112 }
113 
114 void Opti::minimize(const MX& f, double linear_scale) {
115  try {
116  (*this)->minimize(f, linear_scale);
117  } catch(std::exception& e) {
118  THROW_ERROR("minimize", e.what());
119  }
120 }
121 
122 void Opti::subject_to(const MX& g, const Dict& options) {
123  try {
124  (*this)->subject_to(g, 1, options);
125  } catch(std::exception& e) {
126  THROW_ERROR("subject_to", e.what());
127  }
128 }
129 
130 void Opti::subject_to(const std::vector<MX>& g, const Dict& options) {
131  for (const auto& gs : g) subject_to(gs, 1, options);
132 }
133 
134 void Opti::subject_to(const MX& g, const DM& linear_scale, const Dict& options) {
135  try {
136  (*this)->subject_to(g, linear_scale, options);
137  } catch(std::exception& e) {
138  THROW_ERROR("subject_to", e.what());
139  }
140 }
141 
142 void Opti::subject_to(const std::vector<MX>& g, const DM& linear_scale, const Dict& options) {
143  for (const auto& gs : g) subject_to(gs, linear_scale, options);
144 }
145 
147  try {
148  (*this)->subject_to();
149  } catch(std::exception& e) {
150  THROW_ERROR("subject_to", e.what());
151  }
152 }
153 
154 
155 void Opti::solver(const std::string& solver,
156  const Dict& plugin_options,
157  const Dict& solver_options) {
158  try {
159  (*this)->solver(solver, plugin_options, solver_options);
160  } catch(std::exception& e) {
161  THROW_ERROR("solver", e.what());
162  }
163 }
164 
165 void Opti::set_initial(const MX& x, const DM& v) {
166  try {
167  (*this)->set_initial(x, v);
168  } catch(std::exception& e) {
169  THROW_ERROR("set_initial", e.what());
170  }
171 }
172 void Opti::set_initial(const std::vector<MX>& assignments) {
173  try {
174  (*this)->set_initial(assignments);
175  } catch(std::exception& e) {
176  THROW_ERROR("set_initial", e.what());
177  }
178 }
179 
180 
181 void Opti::set_value(const MX& x, const DM& v) {
182  try {
183  (*this)->set_value(x, v);
184  } catch(std::exception& e) {
185  THROW_ERROR("set_value", e.what());
186  }
187 }
188 
189 void Opti::set_domain(const MX& x, const std::string& domain) {
190  try {
191  (*this)->set_domain(x, domain);
192  } catch(std::exception& e) {
193  THROW_ERROR("set_domain", e.what());
194  }
195 }
196 
197 void Opti::set_linear_scale(const MX& x, const DM& scale, const DM& offset) {
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 }
204 
205 void Opti::set_value(const std::vector<MX>& assignments) {
206  try {
207  (*this)->set_value(assignments);
208  } catch(std::exception& e) {
209  THROW_ERROR("set_value", e.what());
210  }
211 }
212 
214  try {
215  return (*this)->solve(false);
216  } catch(std::exception& e) {
217  THROW_ERROR("solve", e.what());
218  }
219 }
220 
222  try {
223  return (*this)->solve(true);
224  } catch(std::exception& e) {
225  THROW_ERROR("solve", e.what());
226  }
227 }
228 
229 DM Opti::value(const MX& x, const std::vector<MX>& values) const {
230  try {
231  return (*this)->value(x, values);
232  } catch(std::exception& e) {
233  THROW_ERROR("value", e.what());
234  }
235 }
236 
237 
238 DM Opti::value(const DM& x, const std::vector<MX>& values) const {
239  try {
240  return (*this)->value(x, values);
241  } catch(std::exception& e) {
242  THROW_ERROR("value", e.what());
243  }
244 }
245 
246 DM Opti::value(const SX& x, const std::vector<MX>& values) const {
247  try {
248  return (*this)->value(x, values);
249  } catch(std::exception& e) {
250  THROW_ERROR("value", e.what());
251  }
252 }
253 
255  try {
256  return (*this)->scale_helper(h);
257  } catch(std::exception& e) {
258  THROW_ERROR("scale_helper", e.what());
259  }
260 }
261 
262 Dict Opti::stats() const {
263  try {
264  return (*this)->stats();
265  } catch(std::exception& e) {
266  THROW_ERROR("stats", e.what());
267  }
268 }
269 
270 std::string Opti::return_status() const {
271  try {
272  return (*this)->return_status();
273  } catch(std::exception& e) {
274  THROW_ERROR("return_status", e.what());
275  }
276 }
277 
278 std::vector<MX> Opti::initial() const {
279  try {
280  return (*this)->initial();
281  } catch(std::exception& e) {
282  THROW_ERROR("initial", e.what());
283  }
284 }
285 
286 std::vector<MX> Opti::value_variables() const {
287  try {
288  return (*this)->value_variables();
289  } catch(std::exception& e) {
290  THROW_ERROR("value_variables", e.what());
291  }
292 }
293 
294 std::vector<MX> Opti::value_parameters() const {
295  try {
296  return (*this)->value_parameters();
297  } catch(std::exception& e) {
298  THROW_ERROR("value_parameters", e.what());
299  }
300 }
301 
302 MX Opti::dual(const MX& m) const {
303  try {
304  return (*this)->dual(m);
305  } catch(std::exception& e) {
306  THROW_ERROR("dual", e.what());
307  }
308 }
309 
310 casadi_int Opti::nx() const {
311  try {
312  return (*this)->nx();
313  } catch(std::exception& e) {
314  THROW_ERROR("nx", e.what());
315  }
316 }
317 
318 casadi_int Opti::np() const {
319  try {
320  return (*this)->np();
321  } catch(std::exception& e) {
322  THROW_ERROR("nx", e.what());
323  }
324 }
325 
326 casadi_int Opti::ng() const {
327  try {
328  return (*this)->ng();
329  } catch(std::exception& e) {
330  THROW_ERROR("ng", e.what());
331  }
332 }
333 
334 MX Opti::x() const {
335  try {
336  return (*this)->x();
337  } catch(std::exception& e) {
338  THROW_ERROR("x", e.what());
339  }
340 }
341 
342 MX Opti::p() const {
343  try {
344  return (*this)->p();
345  } catch(std::exception& e) {
346  THROW_ERROR("p", e.what());
347  }
348 }
349 
350 MX Opti::g() const {
351  try {
352  return (*this)->g();
353  } catch(std::exception& e) {
354  THROW_ERROR("g", e.what());
355  }
356 }
357 
358 MX Opti::f() const {
359  try {
360  return (*this)->f();
361  } catch(std::exception& e) {
362  THROW_ERROR("f", e.what());
363  }
364 }
365 
366 MX Opti::lbg() const {
367  try {
368  return (*this)->lbg();
369  } catch(std::exception& e) {
370  THROW_ERROR("lbg", e.what());
371  }
372 }
373 
374 MX Opti::ubg() const {
375  try {
376  return (*this)->ubg();
377  } catch(std::exception& e) {
378  THROW_ERROR("ubg", e.what());
379  }
380 }
381 
382 
383 MX Opti::lam_g() const {
384  try {
385  return (*this)->lam_g();
386  } catch(std::exception& e) {
387  THROW_ERROR("lam_g", e.what());
388  }
389 }
390 
392  try {
393  return (*this)->x_linear_scale();
394  } catch(std::exception& e) {
395  THROW_ERROR("x_linear_scale", e.what());
396  }
397 }
398 
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 }
406 
408  try {
409  return (*this)->g_linear_scale();
410  } catch(std::exception& e) {
411  THROW_ERROR("g_linear_scale", e.what());
412  }
413 }
414 
415 double Opti::f_linear_scale() const {
416  try {
417  return (*this)->f_linear_scale();
418  } catch(std::exception& e) {
419  THROW_ERROR("f_linear_scale", e.what());
420  }
421 }
422 
423 Function Opti::to_function(const std::string& name,
424  const std::vector<MX>& args, const std::vector<MX>& res,
425  const std::vector<std::string>& name_in,
426  const std::vector<std::string>& name_out,
427  const Dict& opts) {
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 }
434 
435 Function Opti::to_function(const std::string& name,
436  const std::vector<MX>& args, const std::vector<MX>& res,
437  const Dict& opts) {
438  return to_function(name, args, res, {}, {}, opts);
439 }
440 
441 Function Opti::to_function(const std::string& name,
442  const std::map<std::string, MX>& dict,
443  const std::vector<std::string>& name_in,
444  const std::vector<std::string>& name_out,
445  const Dict& opts) {
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  it=find(name_in.begin(), name_in.end(), i.first);
450  if (it!=name_in.end()) {
451  // Input expression
452  ex_in[it-name_in.begin()] = i.second;
453  } else {
454  it=find(name_out.begin(), name_out.end(), i.first);
455  if (it!=name_out.end()) {
456  // Output expression
457  ex_out[it-name_out.begin()] = i.second;
458  } else {
459  // Neither
460  casadi_error("Unknown dictionary entry: '" + i.first + "'");
461  }
462  }
463  }
464  return to_function(name, ex_in, ex_out, name_in, name_out, opts);
465 }
466 
468  try {
469  (*this)->callback_class(callback);
470  } catch(std::exception& e) {
471  THROW_ERROR("callback_class", e.what());
472  }
473 }
474 
476  try {
477  (*this)->callback_class();
478  } catch(std::exception& e) {
479  THROW_ERROR("callback_class", e.what());
480  }
481 }
482 
483 void Opti::update_user_dict(const MX& m, const Dict& meta) {
484  try {
485  (*this)->update_user_dict(m, meta);
486  } catch(std::exception& e) {
487  THROW_ERROR("update_user_dict", e.what());
488  }
489 }
490 
491 void Opti::update_user_dict(const std::vector<MX>& ms, const Dict& meta) {
492  for (const auto& m : ms)
493  update_user_dict(m, meta);
494 }
495 
496 Dict Opti::user_dict(const MX& m) const {
497  try {
498  return (*this)->user_dict(m);
499  } catch(std::exception& e) {
500  THROW_ERROR("user_dict", e.what());
501  }
502 }
503 
505  try {
506  return (*this)->casadi_solver();
507  } catch(std::exception& e) {
508  THROW_ERROR("casadi_solver", e.what());
509  }
510 }
511 
512 bool OptiAdvanced::is_parametric(const MX& expr) const {
513  try {
514  return (*this)->is_parametric(expr);
515  } catch(std::exception& e) {
516  THROW_ERROR("is_parametric", e.what());
517  }
518 }
519 
520 std::vector<MX> OptiAdvanced::symvar() const {
521  try {
522  return (*this)->symvar();
523  } catch(std::exception& e) {
524  THROW_ERROR("symvar", e.what());
525  }
526 }
527 
528 std::vector<MX> OptiAdvanced::symvar(const MX& expr) const {
529  try {
530  return (*this)->symvar(expr);
531  } catch(std::exception& e) {
532  THROW_ERROR("symvar", e.what());
533  }
534 }
535 
536 std::vector<MX> OptiAdvanced::symvar(const MX& expr, VariableType type) const {
537  try {
538  return (*this)->symvar(expr, type);
539  } catch(std::exception& e) {
540  THROW_ERROR("symvar", e.what());
541  }
542 }
543 
544 MetaCon OptiAdvanced::canon_expr(const MX& expr) const {
545  try {
546  return (*this)->canon_expr(expr);
547  } catch(std::exception& e) {
548  THROW_ERROR("canon_expr", e.what());
549  }
550 }
551 
553  try {
554  return (*this)->get_meta(m);
555  } catch(std::exception& e) {
556  THROW_ERROR("get_meta", e.what());
557  }
558 }
559 
561  try {
562  return (*this)->get_meta_con(m);
563  } catch(std::exception& e) {
564  THROW_ERROR("get_meta_con", e.what());
565  }
566 }
567 
568 void OptiAdvanced::set_meta(const MX& m, const MetaVar& meta) {
569  try {
570  (*this)->set_meta(m, meta);
571  } catch(std::exception& e) {
572  THROW_ERROR("set_meta", e.what());
573  }
574 }
575 
576 void OptiAdvanced::set_meta_con(const MX& m, const MetaCon& meta) {
577  try {
578  (*this)->set_meta_con(m, meta);
579  } catch(std::exception& e) {
580  THROW_ERROR("set_meta_con", e.what());
581  }
582 }
583 
584 
586  try {
587  (*this)->assert_active_symbol(m);
588  } catch(std::exception& e) {
589  THROW_ERROR("assert_active_symbol", e.what());
590  }
591 }
592 
593 std::vector<MX> OptiAdvanced::active_symvar(VariableType type) const {
594  try {
595  return (*this)->active_symvar(type);
596  } catch(std::exception& e) {
597  THROW_ERROR("active_symvar", e.what());
598  }
599 }
600 
601 std::vector<DM> OptiAdvanced::active_values(VariableType type) const {
602  try {
603  return (*this)->active_values(type);
604  } catch(std::exception& e) {
605  THROW_ERROR("active_values", e.what());
606  }
607 }
608 
609 MX OptiAdvanced::x_lookup(casadi_int i) const {
610  try {
611  return (*this)->x_lookup(i);
612  } catch(std::exception& e) {
613  THROW_ERROR("x_lookup", e.what());
614  }
615 }
616 
617 casadi_int OptiAdvanced::g_index_reduce_g(casadi_int i) const {
618  try {
619  return (*this)->g_index_reduce_g(i);
620  } catch(std::exception& e) {
621  THROW_ERROR("g_index_reduce_g", e.what());
622  }
623 }
624 
625 casadi_int OptiAdvanced::g_index_reduce_x(casadi_int i) const {
626  try {
627  return (*this)->g_index_reduce_x(i);
628  } catch(std::exception& e) {
629  THROW_ERROR("g_index_reduce_x", e.what());
630  }
631 }
632 
633 casadi_int OptiAdvanced::g_index_unreduce_g(casadi_int i) const {
634  try {
635  return (*this)->g_index_unreduce_g(i);
636  } catch(std::exception& e) {
637  THROW_ERROR("g_index_unreduce_g", e.what());
638  }
639 }
640 
641 MX OptiAdvanced::g_lookup(casadi_int i) const {
642  try {
643  return (*this)->g_lookup(i);
644  } catch(std::exception& e) {
645  THROW_ERROR("g_lookup", e.what());
646  }
647 }
648 
649 std::string OptiAdvanced::x_describe(casadi_int i, const Dict& opts) const {
650  try {
651  return (*this)->x_describe(i, opts);
652  } catch(std::exception& e) {
653  THROW_ERROR("x_describe", e.what());
654  }
655 }
656 std::string OptiAdvanced::g_describe(casadi_int i, const Dict& opts) const {
657  try {
658  return (*this)->g_describe(i, opts);
659  } catch(std::exception& e) {
660  THROW_ERROR("g_describe", e.what());
661  }
662 }
663 std::string OptiAdvanced::describe(const MX& x, casadi_int indent, const Dict& opts) const {
664  try {
665  return (*this)->describe(x, indent, opts);
666  } catch(std::exception& e) {
667  THROW_ERROR("describe", e.what());
668  }
669 }
670 
671 void OptiAdvanced::show_infeasibilities(double tol, const Dict& opts) const {
672  try {
673  (*this)->show_infeasibilities(tol, opts);
674  } catch(std::exception& e) {
675  THROW_ERROR("show_infeasibilities", e.what());
676  }
677 }
678 
680  try {
681  (*this)->solve_prepare();
682  } catch(std::exception& e) {
683  THROW_ERROR("solve_prepare", e.what());
684  }
685 }
687  try {
688  return (*this)->solve_actual(args);
689  } catch(std::exception& e) {
690  THROW_ERROR("solve_actual", e.what());
691  }
692 }
693 
695  try {
696  return (*this)->arg();
697  } catch(std::exception& e) {
698  THROW_ERROR("arg", e.what());
699  }
700 }
701 
702 
703 void OptiAdvanced::res(const DMDict& res) {
704  try {
705  (*this)->res(res);
706  } catch(std::exception& e) {
707  THROW_ERROR("res", e.what());
708  }
709 }
710 
712  try {
713  return (*this)->res();
714  } catch(std::exception& e) {
715  THROW_ERROR("res", e.what());
716  }
717 }
718 
719 std::vector<MX> OptiAdvanced::constraints() const {
720  try {
721  return (*this)->constraints();
722  } catch(std::exception& e) {
723  THROW_ERROR("constraints", e.what());
724  }
725 }
727  return f();
728 }
729 
731  try {
732  return (*this)->baked_copy();
733  } catch(std::exception& e) {
734  THROW_ERROR("baked_copy", e.what());
735  }
736 }
737 
739  try {
740  (*this)->assert_empty();
741  } catch(std::exception& e) {
742  THROW_ERROR("assert_empty", e.what());
743  }
744 }
745 
746 casadi_int OptiAdvanced::instance_number() const {
747  try {
748  return (*this)->instance_number();
749  } catch(std::exception& e) {
750  THROW_ERROR("instance_number", e.what());
751  }
752 }
753 
754 void Opti::disp(std::ostream& stream, bool more) const {
755  stream << "Opti {" << std::endl;
756  OptiAdvanced mycopy = debug();
757  stream << " instance #" << mycopy.instance_number() << std::endl;
758  if (mycopy.problem_dirty()) mycopy.bake();
759  stream << " #variables: " << mycopy.active_symvar(OPTI_VAR).size()
760  << " (nx = " << mycopy.nx() << ")" << std::endl;
761  stream << " #parameters: " << mycopy.active_symvar(OPTI_PAR).size()
762  << " (np = " << mycopy.np() << ")" << std::endl;
763  stream << " #constraints: " << mycopy.active_symvar(OPTI_DUAL_G).size()
764  << " (ng = " << mycopy.ng() << ")" << std::endl;
765  if (mycopy.solver_dirty()) {
766  stream << " CasADi solver needs updating." << std::endl;
767  } else {
768  stream << " CasADi solver allocated." << std::endl;
769  }
770  if (mycopy.solved()) {
771  stream << " CasADi solver was called: " << mycopy.return_status() << std::endl;
772  }
773  stream << "}";
774 }
775 
776 std::string Opti::get_str(bool more) const {
777  std::stringstream ss;
778  disp(ss, more);
779  return ss.str();
780 }
781 
783  try {
784  (*this)->bake();
785  } catch(std::exception& e) {
786  THROW_ERROR("bake", e.what());
787  }
788 }
789 
791  try {
792  (*this)->mark_problem_dirty(flag);
793  } catch(std::exception& e) {
794  THROW_ERROR("mark_problem_dirty", e.what());
795  }
796 }
798  try {
799  return (*this)->problem_dirty();
800  } catch(std::exception& e) {
801  THROW_ERROR("problem_dirty", e.what());
802  }
803 }
804 
806  try {
807  (*this)->mark_solver_dirty(flag);
808  } catch(std::exception& e) {
809  THROW_ERROR("mark_solver_dirty", e.what());
810  }
811 }
813  try {
814  return (*this)->solver_dirty();
815  } catch(std::exception& e) {
816  THROW_ERROR("solver_dirty", e.what());
817  }
818 }
819 
820 void OptiAdvanced::mark_solved(bool flag) {
821  try {
822  (*this)->mark_solved(flag);
823  } catch(std::exception& e) {
824  THROW_ERROR("mark_solved", e.what());
825  }
826 }
827 
828 bool OptiAdvanced::solved() const {
829  try {
830  return (*this)->solved();
831  } catch(std::exception& e) {
832  THROW_ERROR("solved", e.what());
833  }
834 }
835 
837  try {
838  (*this)->assert_solved();
839  } catch(std::exception& e) {
840  THROW_ERROR("assert_solved", e.what());
841  }
842 }
844  try {
845  (*this)->assert_baked();
846  } catch(std::exception& e) {
847  THROW_ERROR("assert_baked", e.what());
848  }
849 }
850 
852  return copy();
853 }
855  return copy();
856 }
857 Opti Opti::copy() const {
858  return (*this)->copy();
859 }
860 
861 OptiSol::OptiSol(const Opti& opti) : optistack_(opti) {
862 }
863 
864 void OptiSol::disp(std::ostream& stream, bool more) const {
865  optistack_.disp(stream, more);
866 }
867 
868 std::string OptiSol::get_str(bool more) const {
869  return optistack_.get_str(more);
870 }
871 
872 DM OptiSol::value(const MX& x, const std::vector<MX>& values) const {
873  return optistack_.value(x, values);
874 }
875 DM OptiSol::value(const DM& x, const std::vector<MX>& values) const {
876  return optistack_.value(x, values);
877 }
878 DM OptiSol::value(const SX& x, const std::vector<MX>& values) const {
879  return optistack_.value(x, values);
880 }
881 
882 std::vector<MX> OptiSol::value_variables() const {
883  return optistack_.value_variables();
884 }
885 
886 std::vector<MX> OptiSol::value_parameters() const {
887  return optistack_.value_parameters();
888 }
889 
891  return optistack_.stats();
892 }
893 
894 
895 
896 
897 } // namespace casadi
Function object.
Definition: function.hpp:60
SharedObjectInternal * operator->() const
Access a member function or object.
MX - Matrix expression.
Definition: mx.hpp:92
casadi_index g_index_unreduce_g(casadi_index i) const
Definition: optistack.cpp:633
void mark_problem_dirty(bool flag=true)
Definition: optistack.cpp:790
std::vector< MX > active_symvar(VariableType type) const
Definition: optistack.cpp:593
MX objective() const
Definition: optistack.cpp:726
void mark_solver_dirty(bool flag=true)
Definition: optistack.cpp:805
MX x_lookup(casadi_index i) const
Definition: optistack.cpp:609
bool problem_dirty() const
Definition: optistack.cpp:797
std::vector< MX > symvar() const
Get symbols present in expression.
Definition: optistack.cpp:520
std::string describe(const MX &x, casadi_index indent=0, const Dict &opts=Dict()) const
Definition: optistack.cpp:663
casadi_index g_index_reduce_x(casadi_index i) const
Definition: optistack.cpp:625
DMDict solve_actual(const DMDict &args)
Definition: optistack.cpp:686
void bake()
Fix the structure of the optimization problem.
Definition: optistack.cpp:782
MX g_lookup(casadi_index i) const
Definition: optistack.cpp:641
std::vector< MX > constraints() const
Definition: optistack.cpp:719
bool solver_dirty() const
Definition: optistack.cpp:812
void set_meta_con(const MX &m, const MetaCon &meta)
Set meta-data of an expression.
Definition: optistack.cpp:576
MetaCon canon_expr(const MX &expr) const
Interpret an expression (for internal use only)
Definition: optistack.cpp:544
std::string x_describe(casadi_index i, const Dict &opts=Dict()) const
Definition: optistack.cpp:649
Function casadi_solver() const
Get the underlying CasADi solver of the Opti stack.
Definition: optistack.cpp:504
void assert_baked() const
Definition: optistack.cpp:843
std::string g_describe(casadi_index i, const Dict &opts=Dict()) const
Definition: optistack.cpp:656
void assert_empty() const
Definition: optistack.cpp:738
void mark_solved(bool flag=true)
Definition: optistack.cpp:820
OptiAdvanced baked_copy() const
Definition: optistack.cpp:730
bool is_parametric(const MX &expr) const
return true if expression is only dependant on Opti parameters, not variables
Definition: optistack.cpp:512
DMDict res() const
Definition: optistack.cpp:711
void show_infeasibilities(double tol=0, const Dict &opts=Dict()) const
Definition: optistack.cpp:671
void assert_active_symbol(const MX &m) const
Definition: optistack.cpp:585
void assert_solved() const
Definition: optistack.cpp:836
DMDict arg() const
Definition: optistack.cpp:694
std::vector< DM > active_values(VariableType type) const
Definition: optistack.cpp:601
bool solved() const
Definition: optistack.cpp:828
void set_meta(const MX &m, const MetaVar &meta)
Set meta-data of an expression.
Definition: optistack.cpp:568
MetaVar get_meta(const MX &m) const
Get meta-data of symbol (for internal use only)
Definition: optistack.cpp:552
MetaCon get_meta_con(const MX &m) const
Get meta-data of symbol (for internal use only)
Definition: optistack.cpp:560
casadi_index g_index_reduce_g(casadi_index i) const
Definition: optistack.cpp:617
casadi_int instance_number() const
Definition: optistack.cpp:746
A simplified interface for NLP modeling/solving.
OptiAdvanced baked_copy() const
Opti copy() const
Copy.
static OptiNode * create(const std::string &problem_type)
A simplified interface for NLP modeling/solving.
Definition: optistack.hpp:662
std::string get_str(bool more=false) const
Definition: optistack.cpp:868
void disp(std::ostream &stream, bool more=false) const
Definition: optistack.cpp:864
native_DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
Definition: optistack.cpp:872
Dict stats() const
Get statistics.
Definition: optistack.cpp:890
std::vector< MX > value_parameters() const
Definition: optistack.cpp:886
std::vector< MX > value_variables() const
get assignment expressions for the optimal solution
Definition: optistack.cpp:882
OptiAdvanced optistack_
Definition: optistack.hpp:698
OptiSol(const Opti &opti)
Definition: optistack.cpp:861
A simplified interface for NLP modeling/solving.
Definition: optistack.hpp:90
Function scale_helper(const Function &h) const
Scale a helper function constructed via opti.x, opti.g, ...
Definition: optistack.cpp:254
void set_domain(const MX &x, const std::string &domain)
Set domain of a decision variable.
Definition: optistack.cpp:189
DM x_linear_scale() const
Definition: optistack.cpp:391
void subject_to()
Clear constraints.
Definition: optistack.cpp:146
casadi_int ng() const
Number of (scalarised) constraints.
Definition: optistack.cpp:326
void minimize(const MX &f, double linear_scale=1)
Set objective.
Definition: optistack.cpp:114
casadi_int np() const
Number of (scalarised) parameters.
Definition: optistack.cpp:318
Dict stats() const
Get statistics.
Definition: optistack.cpp:262
native_DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
Definition: optistack.cpp:229
void disp(std::ostream &stream, bool more=false) const
Print representation.
Definition: optistack.cpp:754
MX lbg() const
Get all (scalarised) bounds on constraints as a column vector.
Definition: optistack.cpp:366
std::string return_status() const
Get return status of solver.
Definition: optistack.cpp:270
casadi_int nx() const
Number of (scalarised) decision variables.
Definition: optistack.cpp:310
Opti(const std::string &problem_type="nlp")
Create Opti Context.
Definition: optistack.cpp:47
std::string get_str(bool more=false) const
Get string representation.
Definition: optistack.cpp:776
OptiAdvanced debug() const
Get a copy with advanced functionality.
Definition: optistack.cpp:851
MX variable(casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
Create a decision variable (symbol)
Definition: optistack.cpp:51
MX f() const
Get objective expression.
Definition: optistack.cpp:358
std::vector< MX > initial() const
get assignment expressions for initial values
Definition: optistack.cpp:278
OptiAdvanced advanced() const
Get a copy with advanced functionality.
Definition: optistack.cpp:854
Opti copy() const
Get a copy of the.
Definition: optistack.cpp:857
Dict user_dict(const MX &m) const
Get user data.
Definition: optistack.cpp:496
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 set_linear_scale(const MX &x, const DM &scale, const DM &offset=0)
Set scale of a decision variable.
Definition: optistack.cpp:197
OptiNode * operator->()
Access a member of the node.
Definition: optistack.cpp:38
MX parameter(casadi_int n=1, casadi_int m=1, const std::string &attribute="full")
Create a parameter (symbol); fixed during optimization.
Definition: optistack.cpp:90
void set_value(const MX &x, const DM &v)
Set value of parameter.
Definition: optistack.cpp:181
DM x_linear_scale_offset() const
Definition: optistack.cpp:399
void callback_class()
Helper methods for callback()
Definition: optistack.cpp:475
MX p() const
Get all (scalarised) parameters as a symbolic column vector.
Definition: optistack.cpp:342
std::vector< MX > value_variables() const
get assignment expressions for latest values
Definition: optistack.cpp:286
MX ubg() const
Definition: optistack.cpp:374
void solver(const std::string &solver, const Dict &plugin_options=Dict(), const Dict &solver_options=Dict())
Set a solver.
Definition: optistack.cpp:155
MX dual(const MX &m) const
get the dual variable
Definition: optistack.cpp:302
void set_initial(const MX &x, const DM &v)
Definition: optistack.cpp:165
static Opti create(OptiNode *node)
Definition: optistack.cpp:86
void update_user_dict(const MX &m, const Dict &meta)
add user data
Definition: optistack.cpp:483
OptiSol solve()
Crunch the numbers; solve the problem.
Definition: optistack.cpp:213
std::vector< MX > value_parameters() const
Definition: optistack.cpp:294
DM g_linear_scale() const
Definition: optistack.cpp:407
MX lam_g() const
Get all (scalarised) dual variables as a symbolic column vector.
Definition: optistack.cpp:383
MX x() const
Get all (scalarised) decision variables as a symbolic column vector.
Definition: optistack.cpp:334
double f_linear_scale() const
Definition: optistack.cpp:415
OptiSol solve_limited()
Crunch the numbers; solve the problem.
Definition: optistack.cpp:221
MX g() const
Get all (scalarised) constraint expressions as a column vector.
Definition: optistack.cpp:350
GenericShared implements a reference counting framework similar for efficient and.
General sparsity class.
Definition: sparsity.hpp:106
The casadi namespace.
Definition: archiver.cpp:28
@ OPTI_VAR
Definition: optistack.hpp:496
@ OPTI_DUAL_G
Definition: optistack.hpp:498
@ OPTI_PAR
Definition: optistack.hpp:497
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.
std::map< std::string, DM > DMDict
Definition: dm_fwd.hpp:36