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  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 }
462 
464  try {
465  (*this)->callback_class(callback);
466  } catch(std::exception& e) {
467  THROW_ERROR("callback_class", e.what());
468  }
469 }
470 
472  try {
473  (*this)->callback_class();
474  } catch(std::exception& e) {
475  THROW_ERROR("callback_class", e.what());
476  }
477 }
478 
479 void Opti::update_user_dict(const MX& m, const Dict& meta) {
480  try {
481  (*this)->update_user_dict(m, meta);
482  } catch(std::exception& e) {
483  THROW_ERROR("update_user_dict", e.what());
484  }
485 }
486 
487 void Opti::update_user_dict(const std::vector<MX>& ms, const Dict& meta) {
488  for (const auto& m : ms)
489  update_user_dict(m, meta);
490 }
491 
492 Dict Opti::user_dict(const MX& m) const {
493  try {
494  return (*this)->user_dict(m);
495  } catch(std::exception& e) {
496  THROW_ERROR("user_dict", e.what());
497  }
498 }
499 
501  try {
502  return (*this)->casadi_solver();
503  } catch(std::exception& e) {
504  THROW_ERROR("casadi_solver", e.what());
505  }
506 }
507 
508 bool OptiAdvanced::is_parametric(const MX& expr) const {
509  try {
510  return (*this)->is_parametric(expr);
511  } catch(std::exception& e) {
512  THROW_ERROR("is_parametric", e.what());
513  }
514 }
515 
516 std::vector<MX> OptiAdvanced::symvar() const {
517  try {
518  return (*this)->symvar();
519  } catch(std::exception& e) {
520  THROW_ERROR("symvar", e.what());
521  }
522 }
523 
524 std::vector<MX> OptiAdvanced::symvar(const MX& expr) const {
525  try {
526  return (*this)->symvar(expr);
527  } catch(std::exception& e) {
528  THROW_ERROR("symvar", e.what());
529  }
530 }
531 
532 std::vector<MX> OptiAdvanced::symvar(const MX& expr, VariableType type) const {
533  try {
534  return (*this)->symvar(expr, type);
535  } catch(std::exception& e) {
536  THROW_ERROR("symvar", e.what());
537  }
538 }
539 
540 MetaCon OptiAdvanced::canon_expr(const MX& expr) const {
541  try {
542  return (*this)->canon_expr(expr);
543  } catch(std::exception& e) {
544  THROW_ERROR("canon_expr", e.what());
545  }
546 }
547 
549  try {
550  return (*this)->get_meta(m);
551  } catch(std::exception& e) {
552  THROW_ERROR("get_meta", e.what());
553  }
554 }
555 
557  try {
558  return (*this)->get_meta_con(m);
559  } catch(std::exception& e) {
560  THROW_ERROR("get_meta_con", e.what());
561  }
562 }
563 
564 void OptiAdvanced::set_meta(const MX& m, const MetaVar& meta) {
565  try {
566  (*this)->set_meta(m, meta);
567  } catch(std::exception& e) {
568  THROW_ERROR("set_meta", e.what());
569  }
570 }
571 
572 void OptiAdvanced::set_meta_con(const MX& m, const MetaCon& meta) {
573  try {
574  return (*this)->set_meta_con(m, meta);
575  } catch(std::exception& e) {
576  THROW_ERROR("set_meta_con", e.what());
577  }
578 }
579 
580 
582  try {
583  (*this)->assert_active_symbol(m);
584  } catch(std::exception& e) {
585  THROW_ERROR("assert_active_symbol", e.what());
586  }
587 }
588 
589 std::vector<MX> OptiAdvanced::active_symvar(VariableType type) const {
590  try {
591  return (*this)->active_symvar(type);
592  } catch(std::exception& e) {
593  THROW_ERROR("active_symvar", e.what());
594  }
595 }
596 
597 std::vector<DM> OptiAdvanced::active_values(VariableType type) const {
598  try {
599  return (*this)->active_values(type);
600  } catch(std::exception& e) {
601  THROW_ERROR("active_values", e.what());
602  }
603 }
604 
605 MX OptiAdvanced::x_lookup(casadi_int i) const {
606  try {
607  return (*this)->x_lookup(i);
608  } catch(std::exception& e) {
609  THROW_ERROR("x_lookup", e.what());
610  }
611 }
612 
613 casadi_int OptiAdvanced::g_index_reduce_g(casadi_int i) const {
614  try {
615  return (*this)->g_index_reduce_g(i);
616  } catch(std::exception& e) {
617  THROW_ERROR("g_index_reduce_g", e.what());
618  }
619 }
620 
621 casadi_int OptiAdvanced::g_index_reduce_x(casadi_int i) const {
622  try {
623  return (*this)->g_index_reduce_x(i);
624  } catch(std::exception& e) {
625  THROW_ERROR("g_index_reduce_x", e.what());
626  }
627 }
628 
629 casadi_int OptiAdvanced::g_index_unreduce_g(casadi_int i) const {
630  try {
631  return (*this)->g_index_unreduce_g(i);
632  } catch(std::exception& e) {
633  THROW_ERROR("g_index_unreduce_g", e.what());
634  }
635 }
636 
637 MX OptiAdvanced::g_lookup(casadi_int i) const {
638  try {
639  return (*this)->g_lookup(i);
640  } catch(std::exception& e) {
641  THROW_ERROR("g_lookup", e.what());
642  }
643 }
644 
645 std::string OptiAdvanced::x_describe(casadi_int i, const Dict& opts) const {
646  try {
647  return (*this)->x_describe(i, opts);
648  } catch(std::exception& e) {
649  THROW_ERROR("x_describe", e.what());
650  }
651 }
652 std::string OptiAdvanced::g_describe(casadi_int i, const Dict& opts) const {
653  try {
654  return (*this)->g_describe(i, opts);
655  } catch(std::exception& e) {
656  THROW_ERROR("g_describe", e.what());
657  }
658 }
659 std::string OptiAdvanced::describe(const MX& x, casadi_int indent, const Dict& opts) const {
660  try {
661  return (*this)->describe(x, indent, opts);
662  } catch(std::exception& e) {
663  THROW_ERROR("describe", e.what());
664  }
665 }
666 
667 void OptiAdvanced::show_infeasibilities(double tol, const Dict& opts) const {
668  try {
669  (*this)->show_infeasibilities(tol, opts);
670  } catch(std::exception& e) {
671  THROW_ERROR("show_infeasibilities", e.what());
672  }
673 }
674 
676  try {
677  (*this)->solve_prepare();
678  } catch(std::exception& e) {
679  THROW_ERROR("solve_prepare", e.what());
680  }
681 }
683  try {
684  return (*this)->solve_actual(args);
685  } catch(std::exception& e) {
686  THROW_ERROR("solve_actual", e.what());
687  }
688 }
689 
691  try {
692  return (*this)->arg();
693  } catch(std::exception& e) {
694  THROW_ERROR("arg", e.what());
695  }
696 }
697 
698 
699 void OptiAdvanced::res(const DMDict& res) {
700  try {
701  return (*this)->res(res);
702  } catch(std::exception& e) {
703  THROW_ERROR("res", e.what());
704  }
705 }
706 
708  try {
709  return (*this)->res();
710  } catch(std::exception& e) {
711  THROW_ERROR("res", e.what());
712  }
713 }
714 
715 std::vector<MX> OptiAdvanced::constraints() const {
716  try {
717  return (*this)->constraints();
718  } catch(std::exception& e) {
719  THROW_ERROR("constraints", e.what());
720  }
721 }
723  return f();
724 }
725 
727  try {
728  return (*this)->baked_copy();
729  } catch(std::exception& e) {
730  THROW_ERROR("baked_copy", e.what());
731  }
732 }
733 
735  try {
736  return (*this)->assert_empty();
737  } catch(std::exception& e) {
738  THROW_ERROR("assert_empty", e.what());
739  }
740 }
741 
742 casadi_int OptiAdvanced::instance_number() const {
743  try {
744  return (*this)->instance_number();
745  } catch(std::exception& e) {
746  THROW_ERROR("instance_number", e.what());
747  }
748 }
749 
750 void Opti::disp(std::ostream& stream, bool more) const {
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 }
771 
772 std::string Opti::get_str(bool more) const {
773  std::stringstream ss;
774  disp(ss, more);
775  return ss.str();
776 }
777 
779  try {
780  (*this)->bake();
781  } catch(std::exception& e) {
782  THROW_ERROR("bake", e.what());
783  }
784 }
785 
787  try {
788  (*this)->mark_problem_dirty(flag);
789  } catch(std::exception& e) {
790  THROW_ERROR("mark_problem_dirty", e.what());
791  }
792 }
794  try {
795  return (*this)->problem_dirty();
796  } catch(std::exception& e) {
797  THROW_ERROR("problem_dirty", e.what());
798  }
799 }
800 
802  try {
803  (*this)->mark_solver_dirty(flag);
804  } catch(std::exception& e) {
805  THROW_ERROR("mark_solver_dirty", e.what());
806  }
807 }
809  try {
810  return (*this)->solver_dirty();
811  } catch(std::exception& e) {
812  THROW_ERROR("solver_dirty", e.what());
813  }
814 }
815 
816 void OptiAdvanced::mark_solved(bool flag) {
817  try {
818  (*this)->mark_solved(flag);
819  } catch(std::exception& e) {
820  THROW_ERROR("mark_solved", e.what());
821  }
822 }
823 
824 bool OptiAdvanced::solved() const {
825  try {
826  return (*this)->solved();
827  } catch(std::exception& e) {
828  THROW_ERROR("solved", e.what());
829  }
830 }
831 
833  try {
834  (*this)->assert_solved();
835  } catch(std::exception& e) {
836  THROW_ERROR("assert_solved", e.what());
837  }
838 }
840  try {
841  (*this)->assert_baked();
842  } catch(std::exception& e) {
843  THROW_ERROR("assert_baked", e.what());
844  }
845 }
846 
848  return copy();
849 }
851  return copy();
852 }
853 Opti Opti::copy() const {
854  return (*this)->copy();
855 }
856 
857 OptiSol::OptiSol(const Opti& opti) : optistack_(opti) {
858 }
859 
860 void OptiSol::disp(std::ostream& stream, bool more) const {
861  optistack_.disp(stream, more);
862 }
863 
864 std::string OptiSol::get_str(bool more) const {
865  return optistack_.get_str(more);
866 }
867 
868 DM OptiSol::value(const MX& x, const std::vector<MX>& values) const {
869  return optistack_.value(x, values);
870 }
871 DM OptiSol::value(const DM& x, const std::vector<MX>& values) const {
872  return optistack_.value(x, values);
873 }
874 DM OptiSol::value(const SX& x, const std::vector<MX>& values) const {
875  return optistack_.value(x, values);
876 }
877 
878 std::vector<MX> OptiSol::value_variables() const {
879  return optistack_.value_variables();
880 }
881 
882 std::vector<MX> OptiSol::value_parameters() const {
883  return optistack_.value_parameters();
884 }
885 
887  return optistack_.stats();
888 }
889 
890 
891 
892 
893 } // 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:629
void mark_problem_dirty(bool flag=true)
Definition: optistack.cpp:786
std::vector< MX > active_symvar(VariableType type) const
Definition: optistack.cpp:589
MX objective() const
Definition: optistack.cpp:722
void mark_solver_dirty(bool flag=true)
Definition: optistack.cpp:801
MX x_lookup(casadi_index i) const
Definition: optistack.cpp:605
bool problem_dirty() const
Definition: optistack.cpp:793
std::vector< MX > symvar() const
Get symbols present in expression.
Definition: optistack.cpp:516
std::string describe(const MX &x, casadi_index indent=0, const Dict &opts=Dict()) const
Definition: optistack.cpp:659
casadi_index g_index_reduce_x(casadi_index i) const
Definition: optistack.cpp:621
DMDict solve_actual(const DMDict &args)
Definition: optistack.cpp:682
void bake()
Fix the structure of the optimization problem.
Definition: optistack.cpp:778
MX g_lookup(casadi_index i) const
Definition: optistack.cpp:637
std::vector< MX > constraints() const
Definition: optistack.cpp:715
bool solver_dirty() const
Definition: optistack.cpp:808
void set_meta_con(const MX &m, const MetaCon &meta)
Set meta-data of an expression.
Definition: optistack.cpp:572
MetaCon canon_expr(const MX &expr) const
Interpret an expression (for internal use only)
Definition: optistack.cpp:540
std::string x_describe(casadi_index i, const Dict &opts=Dict()) const
Definition: optistack.cpp:645
Function casadi_solver() const
Get the underlying CasADi solver of the Opti stack.
Definition: optistack.cpp:500
void assert_baked() const
Definition: optistack.cpp:839
std::string g_describe(casadi_index i, const Dict &opts=Dict()) const
Definition: optistack.cpp:652
void assert_empty() const
Definition: optistack.cpp:734
void mark_solved(bool flag=true)
Definition: optistack.cpp:816
OptiAdvanced baked_copy() const
Definition: optistack.cpp:726
bool is_parametric(const MX &expr) const
return true if expression is only dependant on Opti parameters, not variables
Definition: optistack.cpp:508
DMDict res() const
Definition: optistack.cpp:707
void show_infeasibilities(double tol=0, const Dict &opts=Dict()) const
Definition: optistack.cpp:667
void assert_active_symbol(const MX &m) const
Definition: optistack.cpp:581
void assert_solved() const
Definition: optistack.cpp:832
DMDict arg() const
Definition: optistack.cpp:690
std::vector< DM > active_values(VariableType type) const
Definition: optistack.cpp:597
bool solved() const
Definition: optistack.cpp:824
void set_meta(const MX &m, const MetaVar &meta)
Set meta-data of an expression.
Definition: optistack.cpp:564
MetaVar get_meta(const MX &m) const
Get meta-data of symbol (for internal use only)
Definition: optistack.cpp:548
MetaCon get_meta_con(const MX &m) const
Get meta-data of symbol (for internal use only)
Definition: optistack.cpp:556
casadi_index g_index_reduce_g(casadi_index i) const
Definition: optistack.cpp:613
casadi_int instance_number() const
Definition: optistack.cpp:742
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:864
void disp(std::ostream &stream, bool more=false) const
Definition: optistack.cpp:860
native_DM value(const MX &x, const std::vector< MX > &values=std::vector< MX >()) const
Definition: optistack.cpp:868
Dict stats() const
Get statistics.
Definition: optistack.cpp:886
std::vector< MX > value_parameters() const
Definition: optistack.cpp:882
std::vector< MX > value_variables() const
get assignment expressions for the optimal solution
Definition: optistack.cpp:878
OptiAdvanced optistack_
Definition: optistack.hpp:698
OptiSol(const Opti &opti)
Definition: optistack.cpp:857
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:750
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:772
OptiAdvanced debug() const
Get a copy with advanced functionality.
Definition: optistack.cpp:847
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:850
Opti copy() const
Get a copy of the.
Definition: optistack.cpp:853
Dict user_dict(const MX &m) const
Get user data.
Definition: optistack.cpp:492
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:471
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:479
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