PANOC-ALM  quadratic-penalty
Nonconvex constrained optimization
CasADiLoader.cpp
Go to the documentation of this file.
1 #include <casadi/core/external.hpp>
4 
5 namespace pa {
6 
7 std::function<pa::Problem::f_sig>
8 load_CasADi_objective(const std::string &so_name, const std::string &fun_name) {
9  return CasADiFun_1Vi1So(casadi::external(fun_name, so_name));
10 }
11 std::function<pa::Problem::grad_f_sig>
12 load_CasADi_gradient_objective(const std::string &so_name,
13  const std::string &fun_name) {
14  return CasADiFun_1Vi1Vo(casadi::external(fun_name, so_name));
15 }
16 std::function<pa::Problem::g_sig>
17 load_CasADi_constraints(const std::string &so_name,
18  const std::string &fun_name) {
19  return CasADiFun_1Vi1Vo(casadi::external(fun_name, so_name));
20 }
21 std::function<pa::Problem::grad_g_prod_sig>
22 load_CasADi_gradient_constraints_prod(const std::string &so_name,
23  const std::string &fun_name) {
24  return [csf{CasADiFun_2Vi1Vo(casadi::external(fun_name, so_name))}] //
25  (pa::crvec x, pa::crvec y, pa::rvec gradprod) { //
26  if (y.size() == 0)
27  gradprod.setZero();
28  else
29  csf(x, y, gradprod);
30  };
31 }
32 std::function<pa::Problem::hess_L_sig>
33 load_CasADi_hessian_lagrangian(const std::string &so_name,
34  const std::string &fun_name) {
35  return [csf{CasADiFun_2Vi1Mo(casadi::external(fun_name, so_name))}] //
36  (pa::crvec x, pa::crvec y, pa::rmat H) { //
37  // Fix the stride if the matrix is larger than n
38  if (x.rows() != H.rows()) { // TODO: this is probably unnecessary
39  for (auto c = x.rows(); c-- > 1;)
40  for (auto r = x.rows(); r-- > 0;)
41  std::swap(H(r, c), H.data()[r + x.rows() * c]);
42  }
43  csf(x, y, H);
44  // Fix the stride if the matrix is larger than n
45  if (x.rows() != H.rows()) {
46  for (auto c = x.rows(); c-- > 1;)
47  for (auto r = x.rows(); r-- > 0;)
48  std::swap(H(r, c), H.data()[r + x.rows() * c]);
49  }
50  };
51 }
52 std::function<pa::Problem::hess_L_prod_sig>
53 load_CasADi_hessian_lagrangian_prod(const std::string &so_name,
54  const std::string &fun_name) {
55  return CasADiFun_3Vi1Vo(casadi::external(fun_name, so_name));
56 }
57 
58 pa::Problem load_CasADi_problem(const std::string &so_name, unsigned n,
59  unsigned m, bool second_order) {
60  auto prob = pa::Problem(n, m);
61  pa::vec w = pa::vec::Zero(m);
62  auto load = [&](const std::string &name) {
63  return casadi::external(name, so_name);
64  };
65  prob.f = [csf{CasADiFun_1Vi1So(load("f"))}] //
66  (pa::crvec x) { //
67  return csf(x);
68  };
69  prob.grad_f = [csf{CasADiFun_1Vi1Vo(load("grad_f"))}] //
70  (pa::crvec x, pa::rvec gr) { //
71  csf(x, gr);
72  };
73  prob.g = [csf{CasADiFun_1Vi1Vo(load("g"))}] //
74  (pa::crvec x, pa::rvec g) { //
75  csf(x, g);
76  };
77  prob.grad_g_prod = [csf{CasADiFun_2Vi1Vo(load("grad_g"))}] //
78  (pa::crvec x, pa::crvec y, pa::rvec g) { //
79  if (y.size() == 0)
80  g.setZero();
81  else
82  csf(x, y, g);
83  };
84  if (second_order) {
85  prob.grad_gi = [csf{CasADiFun_2Vi1Vo(load("grad_g"))}, w] //
86  (pa::crvec x, unsigned i, pa::rvec g) mutable { //
87  if (w.size() == 0) {
88  g.setZero();
89  } else {
90  w(i) = 1;
91  csf(x, w, g);
92  w(i) = 0;
93  }
94  };
95  prob.hess_L = [csf{CasADiFun_2Vi1Mo(load("hess_L"))}] //
96  (pa::crvec x, pa::crvec y, pa::rvec g) { //
97  csf(x, y, g);
98  };
99  prob.hess_L_prod = [csf{CasADiFun_3Vi1Vo(load("hess_L_prod"))}] //
101  csf(x, y, v, g);
102  };
103  }
104  return prob;
105 }
106 
108  unsigned n, unsigned m,
109  bool second_order) {
110  auto prob = ProblemWithParam(n, m);
111  pa::vec w = pa::vec::Zero(m);
112  const auto param = prob.get_param_ptr();
113  auto load = [&](const std::string &name) {
114  return casadi::external(name, so_name);
115  };
116  prob.f = [csf{CasADiFun_2Vi1So(load("f"))}, p{param}] //
117  (pa::crvec x) { //
118  return csf(x, *p);
119  };
120  prob.grad_f = [csf{CasADiFun_2Vi1Vo(load("grad_f"))}, p{param}] //
121  (pa::crvec x, pa::rvec gr) { //
122  csf(x, *p, gr);
123  };
124  prob.g = [csf{CasADiFun_2Vi1Vo(load("g"))}, p{param}] //
125  (pa::crvec x, pa::rvec g) { //
126  csf(x, *p, g);
127  };
128  prob.grad_g_prod = [csf{CasADiFun_3Vi1Vo(load("grad_g"))}, p{param}] //
129  (pa::crvec x, pa::crvec y, pa::rvec g) { //
130  if (y.size() == 0)
131  g.setZero();
132  else
133  csf(x, *p, y, g);
134  };
135  if (second_order) {
136  prob.grad_gi = [csf{CasADiFun_3Vi1Vo(load("grad_g"))}, p{param}, w] //
137  (pa::crvec x, unsigned i, pa::rvec g) mutable { //
138  if (w.size() == 0) {
139  g.setZero();
140  } else {
141  w(i) = 1;
142  csf(x, *p, w, g);
143  w(i) = 0;
144  }
145  };
146  prob.hess_L = [csf{CasADiFun_3Vi1Mo(load("hess_L"))}, p{param}] //
147  (pa::crvec x, pa::crvec y, pa::rvec g) { //
148  csf(x, *p, y, g);
149  };
150  prob.hess_L_prod =
151  [csf{CasADiFun_4Vi1Vo(load("hess_L_prod"))}, p{param}] //
153  csf(x, *p, y, v, g);
154  };
155  }
156  return prob;
157 }
158 
159 pa::ProblemFull load_CasADi_problem_full(const char *so_name, unsigned n, unsigned m1,
160  unsigned m2, bool second_order) {
161  auto prob = pa::ProblemFull(n, m1, m2);
162  pa::vec w = pa::vec::Zero(m1);
163  auto load = [&](const char *name) {
164  return casadi::external(name, so_name);
165  };
166  prob.f = [csf{CasADiFun_1Vi1So(load("f"))}] //
167  (pa::crvec x) { //
168  return csf(x);
169  };
170  prob.grad_f = [csf{CasADiFun_1Vi1Vo(load("grad_f"))}] //
171  (pa::crvec x, pa::rvec gr) { //
172  csf(x, gr);
173  };
174  prob.g1 = [csf{CasADiFun_1Vi1Vo(load("g1"))}] //
175  (pa::crvec x, pa::rvec g1) { //
176  csf(x, g1);
177  };
178  prob.grad_g1_prod = [csf{CasADiFun_2Vi1Vo(load("grad_g1"))}] //
179  (pa::crvec x, pa::crvec y1, pa::rvec g1) { //
180  if (y1.size() == 0)
181  g1.setZero();
182  else
183  csf(x, y1, g1);
184  };
185  prob.g2 = [csf{CasADiFun_1Vi1Vo(load("g2"))}] //
186  (pa::crvec x, pa::rvec g2) { //
187  csf(x, g2);
188  };
189  prob.grad_g2_prod = [csf{CasADiFun_2Vi1Vo(load("grad_g2"))}] //
190  (pa::crvec x, pa::crvec y2, pa::rvec g2) { //
191  if (y2.size() == 0)
192  g2.setZero();
193  else
194  csf(x, y2, g2);
195  };
196  if (second_order) {
197  prob.grad_g1i = [csf{CasADiFun_2Vi1Vo(load("grad_g1"))}, w] //
198  (pa::crvec x, unsigned i, pa::rvec g1) mutable { //
199  if (w.size() == 0) {
200  g1.setZero();
201  } else {
202  w(i) = 1;
203  csf(x, w, g1);
204  w(i) = 0;
205  }
206  };
207  //todo: check L
208  prob.hess_L = [csf{CasADiFun_2Vi1Mo(load("hess_L"))}] //
209  (pa::crvec x, pa::crvec y, pa::rvec g) { //
210  csf(x, y, g);
211  };
212  prob.hess_L_prod = [csf{CasADiFun_3Vi1Vo(load("hess_L_prod"))}] //
214  csf(x, y, v, g);
215  };
216  }
217  return prob;
218 }
219 
221  unsigned m1, unsigned m2, bool second_order) {
222  auto prob = ProblemFullWithParam(n, m1, m2);
223  pa::vec w = pa::vec::Zero(m1);
224  const auto param = prob.get_param_ptr();
225  auto load = [&](const char *name) {
226  return casadi::external(name, so_name);
227  };
228  prob.f = [csf{CasADiFun_2Vi1So(load("f"))}, p{param}] //
229  (pa::crvec x) { //
230  return csf(x, *p);
231  };
232  prob.grad_f = [csf{CasADiFun_2Vi1Vo(load("grad_f"))}, p{param}] //
233  (pa::crvec x, pa::rvec gr) { //
234  csf(x, *p, gr);
235  };
236  prob.g1 = [csf{CasADiFun_2Vi1Vo(load("g1"))}, p{param}] //
237  (pa::crvec x, pa::rvec g1) { //
238  csf(x, *p, g1);
239  };
240  prob.grad_g1_prod = [csf{CasADiFun_3Vi1Vo(load("grad_g1"))}, p{param}] //
241  (pa::crvec x, pa::crvec y1, pa::rvec g1) { //
242  if (y1.size() == 0)
243  g1.setZero();
244  else
245  csf(x, *p, y1, g1);
246  };
247  prob.g2 = [csf{CasADiFun_2Vi1Vo(load("g2"))}, p{param}] //
248  (pa::crvec x, pa::rvec g2) { //
249  csf(x, *p, g2);
250  };
251  prob.grad_g2_prod = [csf{CasADiFun_3Vi1Vo(load("grad_g2"))}, p{param}] //
252  (pa::crvec x, pa::crvec y2, pa::rvec g2) { //
253  if (y2.size() == 0)
254  g2.setZero();
255  else
256  csf(x, *p, y2, g2);
257  };
258  if (second_order) {
259  prob.grad_g1i = [csf{CasADiFun_3Vi1Vo(load("grad_g1"))}, p{param}, w] //
260  (pa::crvec x, unsigned i, pa::rvec g1) mutable { //
261  if (w.size() == 0) {
262  g1.setZero();
263  } else {
264  w(i) = 1;
265  csf(x, *p, w, g1);
266  w(i) = 0;
267  }
268  };
269  //todo: check L
270  prob.hess_L = [csf{CasADiFun_3Vi1Mo(load("hess_L"))}, p{param}] //
271  (pa::crvec x, pa::crvec y, pa::rvec g) { //
272  csf(x, *p, y, g);
273  };
274  prob.hess_L_prod =
275  [csf{CasADiFun_4Vi1Vo(load("hess_L_prod"))}, p{param}] //
277  csf(x, *p, y, v, g);
278  };
279  }
280  return prob;
281 }
282 
283 } // namespace pa
pa::load_CasADi_objective
std::function< pa::Problem::f_sig > load_CasADi_objective(const std::string &so_name, const std::string &fun_name="f")
Load an objective function generated by CasADi.
Definition: CasADiLoader.cpp:8
codegen-rosenbrock.w
w
Definition: codegen-rosenbrock.py:13
pa::rvec
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
pa::load_CasADi_constraints
std::function< pa::Problem::g_sig > load_CasADi_constraints(const std::string &so_name, const std::string &fun_name="g")
Load a constraint function generated by CasADi.
Definition: CasADiLoader.cpp:17
panocpy.test.y
y
Definition: test.py:76
panocpy.test.name
string name
Definition: test.py:169
CasADiFun_3Vi1Vo
Wrapper for CasADiFunctionEvaluator with 3 vector inputs, 1 vector output.
Definition: CasADiFunctionWrapper.hpp:126
pa::ProblemWithParam
Definition: include/panoc-alm/util/problem.hpp:124
pa::vec
realvec vec
Default type for vectors.
Definition: vec.hpp:14
pa::rmat
Eigen::Ref< mat > rmat
Default type for mutable references to matrices.
Definition: vec.hpp:22
CasADiFun_1Vi1So
Wrapper for CasADiFunctionEvaluator with 1 vector input, scalar output.
Definition: CasADiFunctionWrapper.hpp:43
CasADiLoader.hpp
bicycle-obstacle-avoidance-mpc.prob
prob
Definition: bicycle-obstacle-avoidance-mpc.py:44
pa::load_CasADi_problem_with_param
pa::ProblemWithParam load_CasADi_problem_with_param(const std::string &filename, unsigned n, unsigned m, bool second_order=false)
Load a problem generated by CasADi (with parameters).
Definition: CasADiLoader.cpp:107
CasADiFun_2Vi1Vo
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, 1 vector output.
Definition: CasADiFunctionWrapper.hpp:86
panocpy.test.v
v
Definition: test.py:42
pa
Definition: alm.hpp:10
rosenbrock.param
param
Definition: rosenbrock.py:53
panocpy.test.x
x
Definition: test.py:40
bicycle-obstacle-avoidance-mpc.c
c
Definition: bicycle-obstacle-avoidance-mpc.py:124
pa::load_CasADi_hessian_lagrangian_prod
std::function< pa::Problem::hess_L_prod_sig > load_CasADi_hessian_lagrangian_prod(const std::string &so_name, const std::string &fun_name="hess_L_prod")
Load the Hessian-vector product of a Lagrangian function generated by CasADi.
Definition: CasADiLoader.cpp:53
CasADiFunctionWrapper.hpp
pa::load_CasADi_hessian_lagrangian
std::function< pa::Problem::hess_L_sig > load_CasADi_hessian_lagrangian(const std::string &so_name, const std::string &fun_name="hess_L")
Load the Hessian of a Lagrangian function generated by CasADi.
Definition: CasADiLoader.cpp:33
pa::crvec
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
pa::load_CasADi_gradient_constraints_prod
std::function< pa::Problem::grad_g_prod_sig > load_CasADi_gradient_constraints_prod(const std::string &so_name, const std::string &fun_name="grad_g")
Load the gradient-vector product of a constraint function generated by CasADi.
Definition: CasADiLoader.cpp:22
CasADiFun_4Vi1Vo
Wrapper for CasADiFunctionEvaluator with 4 vector inputs, 1 vector output.
Definition: CasADiFunctionWrapper.hpp:140
pa::load_CasADi_problem_full_with_param
pa::ProblemFullWithParam load_CasADi_problem_full_with_param(const char *filename, unsigned n, unsigned m1, unsigned m2, bool second_order=false)
Load a problem generated by CasADi (with parameters).
Definition: CasADiLoader.cpp:220
panocpy.test.m
int m
Definition: test.py:39
CasADiFun_2Vi1So
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, scalar output.
Definition: CasADiFunctionWrapper.hpp:58
pa::ProblemFullWithParam
Definition: include/panoc-alm/util/problem.hpp:358
panocpy.test.g
g
Definition: test.py:51
pa::load_CasADi_problem_full
pa::ProblemFull load_CasADi_problem_full(const char *filename, unsigned n, unsigned m1, unsigned m2, bool second_order=false)
Load a problem generated by CasADi (without parameters).
Definition: CasADiLoader.cpp:159
panocpy.test.p
p
Definition: test.py:57
pa::ProblemFull
Problem description for minimization problems.
Definition: include/panoc-alm/util/problem.hpp:213
CasADiFun_3Vi1Mo
Wrapper for CasADiFunctionEvaluator with 3 vector inputs, 1 matrix output.
Definition: CasADiFunctionWrapper.hpp:112
panocpy.test.n
int n
Definition: test.py:38
pa::load_CasADi_gradient_objective
std::function< pa::Problem::grad_f_sig > load_CasADi_gradient_objective(const std::string &so_name, const std::string &fun_name="grad_f")
Load the gradient of an objective function generated by CasADi.
Definition: CasADiLoader.cpp:12
CasADiFun_1Vi1Vo
Wrapper for CasADiFunctionEvaluator with 1 vector input, 1 vector output.
Definition: CasADiFunctionWrapper.hpp:73
pa::Problem
Problem description for minimization problems.
Definition: include/panoc-alm/util/problem.hpp:24
main.H
H
Definition: main.py:8
pa::load_CasADi_problem
pa::Problem load_CasADi_problem(const std::string &filename, unsigned n, unsigned m, bool second_order=false)
Load a problem generated by CasADi (without parameters).
Definition: CasADiLoader.cpp:58
CasADiFun_2Vi1Mo
Wrapper for CasADiFunctionEvaluator with 2 vector inputs, 1 matrix output.
Definition: CasADiFunctionWrapper.hpp:99