PANOC-ALM  quadratic-penalty
Nonconvex constrained optimization
panocpy/problem.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 
5 inline auto prob_getter_f() {
6  return [](const pa::Problem &p) -> std::function<pa::real_t(pa::crvec)> {
7  return [n{p.n}, f{p.f}](pa::crvec x) {
8  if (x.size() != n)
9  throw std::out_of_range("Dimension of x not consistent "
10  "with problem dimension n");
11  return f(x);
12  };
13  };
14 }
15 inline auto prob_setter_f() {
16  return [](pa::Problem &p,
17  std::function<pa::real_t(pa::crvec)> fun) -> void { p.f = fun; };
18 }
19 inline auto prob_getter_grad_f() {
20  return [](const pa::Problem &p) -> std::function<pa::vec(pa::crvec)> {
21  return [n{p.n}, grad_f{p.grad_f}](pa::crvec x) {
22  if (x.size() != n)
23  throw std::out_of_range("Dimension of x not consistent "
24  "with problem dimension n");
25  pa::vec gr(n);
26  grad_f(x, gr);
27  return gr;
28  };
29  };
30 }
31 inline auto prob_setter_grad_f() {
32  return [](pa::Problem &p, std::function<pa::vec(pa::crvec)> fun) -> void {
33  p.grad_f = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::rvec gr) {
34  auto &&res = fun(x);
35  if (res.size() != n)
36  throw std::out_of_range(
37  "Dimension of result of grad_f not consistent "
38  "with problem dimension n");
39  gr = std::move(res);
40  };
41  };
42 }
43 inline auto prob_getter_g() {
44  return [](const pa::Problem &p) -> std::function<pa::vec(pa::crvec)> {
45  return [n{p.n}, m{p.m}, g{p.g}](pa::crvec x) {
46  if (x.size() != n)
47  throw std::out_of_range("Dimension of x not consistent "
48  "with problem dimension n");
49  pa::vec gg(m);
50  g(x, gg);
51  return gg;
52  };
53  };
54 }
55 inline auto prob_setter_g() {
56  return [](pa::Problem &p, std::function<pa::vec(pa::crvec)> fun) -> void {
57  p.g = [m{p.m}, fun{std::move(fun)}](pa::crvec x, pa::rvec gg) {
58  auto &&res = fun(x);
59  if (res.size() != m)
60  throw std::out_of_range(
61  "Dimension of result of g not consistent "
62  "with problem dimension m");
63  gg = std::move(res);
64  };
65  };
66 }
67 inline auto prob_getter_grad_g_prod() {
68  return [](const pa::Problem &p)
69  -> std::function<pa::vec(pa::crvec, pa::crvec)> {
70  return [n{p.n}, m{p.m}, grad_g_prod{p.grad_g_prod}](pa::crvec x,
71  pa::crvec y) {
72  if (x.size() != n)
73  throw std::out_of_range("Dimension of x not consistent "
74  "with problem dimension n");
75  if (y.size() != m)
76  throw std::out_of_range("Dimension of y not consistent "
77  "with problem dimension m");
78  pa::vec gy(n);
79  grad_g_prod(x, y, gy);
80  return gy;
81  };
82  };
83 }
84 inline auto prob_setter_grad_g_prod() {
85  return [](pa::Problem &p,
86  std::function<pa::vec(pa::crvec, pa::crvec)> fun) -> void {
87  p.grad_g_prod = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::crvec y,
88  pa::rvec gy) {
89  auto &&res = fun(x, y);
90  if (res.size() != n)
91  throw std::out_of_range(
92  "Dimension of result of grad_g_prod not consistent "
93  "with problem dimension n");
94  gy = std::move(res);
95  };
96  };
97 }
98 inline auto prob_getter_grad_gi() {
99  return [](const pa::Problem &p)
100  -> std::function<pa::vec(pa::crvec, unsigned)> {
101  return [n{p.n}, m{p.m}, grad_gi{p.grad_gi}](pa::crvec x, unsigned i) {
102  if (x.size() != n)
103  throw std::out_of_range("Dimension of x not consistent "
104  "with problem dimension n");
105  if (i < m)
106  throw std::out_of_range("Constraint index greater or "
107  "equal to problem dimension m");
108  pa::vec gg(n);
109  grad_gi(x, i, gg);
110  return gg;
111  };
112  };
113 }
114 inline auto prob_setter_grad_gi() {
115  return [](pa::Problem &p,
116  std::function<pa::vec(pa::crvec, unsigned)> fun) -> void {
117  p.grad_gi = [n{p.n}, fun{std::move(fun)}](pa::crvec x, unsigned i,
118  pa::rvec gg) {
119  auto &&res = fun(x, i);
120  if (res.size() != n)
121  throw std::out_of_range(
122  "Dimension of result of grad_gi not consistent "
123  "with problem dimension n");
124  gg = std::move(res);
125  };
126  };
127 }
128 inline auto prob_getter_hess_L() {
129  return [](const pa::Problem &p)
130  -> std::function<pa::mat(pa::crvec, pa::crvec)> {
131  return [n{p.n}, m{p.m}, hess_L{p.hess_L}](pa::crvec x, pa::crvec y) {
132  if (x.size() != n)
133  throw std::out_of_range("Dimension of x not consistent "
134  "with problem dimension n");
135  if (y.size() != m)
136  throw std::out_of_range("Dimension of y not consistent "
137  "with problem dimension m");
138  pa::mat H(n, n);
139  hess_L(x, y, H);
140  return H;
141  };
142  };
143 }
144 inline auto prob_setter_hess_L() {
145  return [](pa::Problem &p,
146  std::function<pa::mat(pa::crvec, pa::crvec)> fun) -> void {
147  p.hess_L = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::crvec y,
148  pa::rmat H) {
149  auto &&res = fun(x, y);
150  if (res.rows() != n)
151  throw std::out_of_range(
152  "Number of rows of result of hess_L not consistent "
153  "with problem dimension n");
154  if (res.cols() != n)
155  throw std::out_of_range("Number of columns of result "
156  "of hess_L not consistent "
157  "with problem dimension n");
158  H = std::move(res);
159  };
160  };
161 }
162 inline auto prob_getter_hess_L_prod() {
163  return [](const pa::Problem &p)
164  -> std::function<pa::vec(pa::crvec, pa::crvec, pa::crvec)> {
165  return [n{p.n}, m{p.m}, hess_L_prod{p.hess_L_prod}](
167  if (x.size() != n)
168  throw std::out_of_range("Dimension of x not consistent "
169  "with problem dimension n");
170  if (y.size() != m)
171  throw std::out_of_range("Dimension of y not consistent "
172  "with problem dimension m");
173  if (v.size() != n)
174  throw std::out_of_range("Dimension of v not consistent "
175  "with problem dimension n");
176  pa::vec Hv(n);
177  hess_L_prod(x, y, v, Hv);
178  return Hv;
179  };
180  };
181 }
182 inline auto prob_setter_hess_L_prod() {
183  return [](pa::Problem &p,
184  std::function<pa::vec(pa::crvec, pa::crvec, pa::crvec)> fun)
185  -> void {
186  p.hess_L_prod = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::crvec y,
187  pa::crvec v,
188  pa::rvec Hv) {
189  auto &&res = fun(x, y, v);
190  if (res.rows() != n)
191  throw std::out_of_range(
192  "Dimension of result of hess_L_prod not consistent "
193  "with problem dimension n");
194  Hv = std::move(res);
195  };
196  };
197 }
198 
199 inline auto prob_full_getter_f() {
200  return [](const pa::ProblemFull &p) -> std::function<pa::real_t(pa::crvec)> {
201  return [n{p.n}, f{p.f}](pa::crvec x) {
202  if (x.size() != n)
203  throw std::out_of_range("Dimension of x not consistent "
204  "with problem dimension n");
205  return f(x);
206  };
207  };
208 }
209 inline auto prob_full_setter_f() {
210  return [](pa::ProblemFull &p,
211  std::function<pa::real_t(pa::crvec)> fun) -> void { p.f = fun; };
212 }
213 inline auto prob_full_getter_grad_f() {
214  return [](const pa::ProblemFull &p) -> std::function<pa::vec(pa::crvec)> {
215  return [n{p.n}, grad_f{p.grad_f}](pa::crvec x) {
216  if (x.size() != n)
217  throw std::out_of_range("Dimension of x not consistent "
218  "with problem dimension n");
219  pa::vec gr(n);
220  grad_f(x, gr);
221  return gr;
222  };
223  };
224 }
225 inline auto prob_full_setter_grad_f() {
226  return [](pa::ProblemFull &p, std::function<pa::vec(pa::crvec)> fun) -> void {
227  p.grad_f = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::rvec gr) {
228  auto &&res = fun(x);
229  if (res.size() != n)
230  throw std::out_of_range(
231  "Dimension of result of grad_f not consistent "
232  "with problem dimension n");
233  gr = std::move(res);
234  };
235  };
236 }
237 inline auto prob_full_getter_g1() {
238  return [](const pa::ProblemFull &p) -> std::function<pa::vec(pa::crvec)> {
239  return [n{p.n}, m1{p.m1}, g1{p.g1}](pa::crvec x) {
240  if (x.size() != n)
241  throw std::out_of_range("Dimension of x not consistent "
242  "with problem dimension n");
243  pa::vec gg(m1);
244  g1(x, gg);
245  return gg;
246  };
247  };
248 }
249 inline auto prob_full_setter_g1() {
250  return [](pa::ProblemFull &p, std::function<pa::vec(pa::crvec)> fun) -> void {
251  p.g1 = [m1{p.m1}, fun{std::move(fun)}](pa::crvec x, pa::rvec gg) {
252  auto &&res = fun(x);
253  if (res.size() != m1)
254  throw std::out_of_range(
255  "Dimension of result of g1 not consistent "
256  "with problem dimension m1");
257  gg = std::move(res);
258  };
259  };
260 }
262  return [](const pa::ProblemFull &p)
263  -> std::function<pa::vec(pa::crvec, pa::crvec)> {
264  return [n{p.n}, m1{p.m1}, grad_g1_prod{p.grad_g1_prod}](pa::crvec x,
265  pa::crvec y) {
266  if (x.size() != n)
267  throw std::out_of_range("Dimension of x not consistent "
268  "with problem dimension n");
269  if (y.size() != m1)
270  throw std::out_of_range("Dimension of y not consistent "
271  "with problem dimension m");
272  pa::vec g1y(n);
273  grad_g1_prod(x, y, g1y);
274  return g1y;
275  };
276  };
277 }
279  return [](pa::ProblemFull &p,
280  std::function<pa::vec(pa::crvec, pa::crvec)> fun) -> void {
281  p.grad_g1_prod = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::crvec y,
282  pa::rvec g1y) {
283  auto &&res = fun(x, y);
284  if (res.size() != n)
285  throw std::out_of_range(
286  "Dimension of result of grad_g1_prod not consistent "
287  "with problem dimension n");
288  g1y = std::move(res);
289  };
290  };
291 }
293  return [](const pa::ProblemFull &p)
294  -> std::function<pa::vec(pa::crvec, unsigned)> {
295  return [n{p.n}, m1{p.m1}, grad_g1i{p.grad_g1i}](pa::crvec x, unsigned i) {
296  if (x.size() != n)
297  throw std::out_of_range("Dimension of x not consistent "
298  "with problem dimension n");
299  if (i < m1)
300  throw std::out_of_range("Constraint index greater or "
301  "equal to problem dimension m1");
302  pa::vec gg(n);
303  grad_g1i(x, i, gg);
304  return gg;
305  };
306  };
307 }
309  return [](pa::ProblemFull &p,
310  std::function<pa::vec(pa::crvec, unsigned)> fun) -> void {
311  p.grad_g1i = [n{p.n}, fun{std::move(fun)}](pa::crvec x, unsigned i,
312  pa::rvec gg) {
313  auto &&res = fun(x, i);
314  if (res.size() != n)
315  throw std::out_of_range(
316  "Dimension of result of grad_g1i not consistent "
317  "with problem dimension n");
318  gg = std::move(res);
319  };
320  };
321 }
322 inline auto prob_full_getter_g2() {
323  return [](const pa::ProblemFull &p) -> std::function<pa::vec(pa::crvec)> {
324  return [n{p.n}, m2{p.m2}, g2{p.g2}](pa::crvec x) {
325  if (x.size() != n)
326  throw std::out_of_range("Dimension of x not consistent "
327  "with problem dimension n");
328  pa::vec gg(m2);
329  g2(x, gg);
330  return gg;
331  };
332  };
333 }
334 inline auto prob_full_setter_g2() {
335  return [](pa::ProblemFull &p, std::function<pa::vec(pa::crvec)> fun) -> void {
336  p.g2 = [m2{p.m2}, fun{std::move(fun)}](pa::crvec x, pa::rvec gg) {
337  auto &&res = fun(x);
338  if (res.size() != m2)
339  throw std::out_of_range(
340  "Dimension of result of g2 not consistent "
341  "with problem dimension m2");
342  gg = std::move(res);
343  };
344  };
345 }
347  return [](const pa::ProblemFull &p)
348  -> std::function<pa::vec(pa::crvec, pa::crvec)> {
349  return [n{p.n}, m2{p.m2}, grad_g2_prod{p.grad_g2_prod}](pa::crvec x,
350  pa::crvec y) {
351  if (x.size() != n)
352  throw std::out_of_range("Dimension of x not consistent "
353  "with problem dimension n");
354  if (y.size() != m2)
355  throw std::out_of_range("Dimension of y not consistent "
356  "with problem dimension m");
357  pa::vec g2y(n);
358  grad_g2_prod(x, y, g2y);
359  return g2y;
360  };
361  };
362 }
364  return [](pa::ProblemFull &p,
365  std::function<pa::vec(pa::crvec, pa::crvec)> fun) -> void {
366  p.grad_g2_prod = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::crvec y,
367  pa::rvec g2y) {
368  auto &&res = fun(x, y);
369  if (res.size() != n)
370  throw std::out_of_range(
371  "Dimension of result of grad_g2_prod not consistent "
372  "with problem dimension n");
373  g2y = std::move(res);
374  };
375  };
376 }
378  return [](const pa::ProblemFull &p)
379  -> std::function<pa::vec(pa::crvec, unsigned)> {
380  return [n{p.n}, m2{p.m2}, grad_g2i{p.grad_g2i}](pa::crvec x, unsigned i) {
381  if (x.size() != n)
382  throw std::out_of_range("Dimension of x not consistent "
383  "with problem dimension n");
384  if (i < m2)
385  throw std::out_of_range("Constraint index greater or "
386  "equal to problem dimension m2");
387  pa::vec gg(n);
388  grad_g2i(x, i, gg);
389  return gg;
390  };
391  };
392 }
394  return [](pa::ProblemFull &p,
395  std::function<pa::vec(pa::crvec, unsigned)> fun) -> void {
396  p.grad_g2i = [n{p.n}, fun{std::move(fun)}](pa::crvec x, unsigned i,
397  pa::rvec gg) {
398  auto &&res = fun(x, i);
399  if (res.size() != n)
400  throw std::out_of_range(
401  "Dimension of result of grad_g2i not consistent "
402  "with problem dimension n");
403  gg = std::move(res);
404  };
405  };
406 }
407 inline auto prob_full_getter_hess_L() {
408  return [](const pa::ProblemFull &p)
409  -> std::function<pa::mat(pa::crvec, pa::crvec)> {
410  return [n{p.n}, m1{p.m1}, hess_L{p.hess_L}](pa::crvec x, pa::crvec y) {
411  if (x.size() != n)
412  throw std::out_of_range("Dimension of x not consistent "
413  "with problem dimension n");
414  if (y.size() != m1)
415  throw std::out_of_range("Dimension of y not consistent "
416  "with problem dimension m");
417  pa::mat H(n, n);
418  hess_L(x, y, H);
419  return H;
420  };
421  };
422 }
423 inline auto prob_full_setter_hess_L() {
424  return [](pa::ProblemFull &p,
425  std::function<pa::mat(pa::crvec, pa::crvec)> fun) -> void {
426  p.hess_L = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::crvec y,
427  pa::rmat H) {
428  auto &&res = fun(x, y);
429  if (res.rows() != n)
430  throw std::out_of_range(
431  "Number of rows of result of hess_L not consistent "
432  "with problem dimension n");
433  if (res.cols() != n)
434  throw std::out_of_range("Number of columns of result "
435  "of hess_L not consistent "
436  "with problem dimension n");
437  H = std::move(res);
438  };
439  };
440 }
442  return [](const pa::ProblemFull &p)
443  -> std::function<pa::vec(pa::crvec, pa::crvec, pa::crvec)> {
444  return [n{p.n}, m1{p.m1}, hess_L_prod{p.hess_L_prod}](
446  if (x.size() != n)
447  throw std::out_of_range("Dimension of x not consistent "
448  "with problem dimension n");
449  if (y.size() != m1)
450  throw std::out_of_range("Dimension of y not consistent "
451  "with problem dimension m1");
452  if (v.size() != n)
453  throw std::out_of_range("Dimension of v not consistent "
454  "with problem dimension n");
455  pa::vec Hv(n);
456  hess_L_prod(x, y, v, Hv);
457  return Hv;
458  };
459  };
460 }
462  return [](pa::ProblemFull &p,
463  std::function<pa::vec(pa::crvec, pa::crvec, pa::crvec)> fun)
464  -> void {
465  p.hess_L_prod = [n{p.n}, fun{std::move(fun)}](pa::crvec x, pa::crvec y,
466  pa::crvec v,
467  pa::rvec Hv) {
468  auto &&res = fun(x, y, v);
469  if (res.rows() != n)
470  throw std::out_of_range(
471  "Dimension of result of hess_L_prod not consistent "
472  "with problem dimension n");
473  Hv = std::move(res);
474  };
475  };
476 }
prob_setter_grad_gi
auto prob_setter_grad_gi()
Definition: panocpy/problem.hpp:114
prob_getter_g
auto prob_getter_g()
Definition: panocpy/problem.hpp:43
pa::rvec
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
prob_full_setter_hess_L_prod
auto prob_full_setter_hess_L_prod()
Definition: panocpy/problem.hpp:461
panocpy.test.y
y
Definition: test.py:76
prob_setter_g
auto prob_setter_g()
Definition: panocpy/problem.hpp:55
prob_full_setter_f
auto prob_full_setter_f()
Definition: panocpy/problem.hpp:209
pa::vec
realvec vec
Default type for vectors.
Definition: vec.hpp:14
prob_full_getter_hess_L_prod
auto prob_full_getter_hess_L_prod()
Definition: panocpy/problem.hpp:441
pa::rmat
Eigen::Ref< mat > rmat
Default type for mutable references to matrices.
Definition: vec.hpp:22
panocpy.test.f
f
Definition: test.py:49
prob_full_setter_g2
auto prob_full_setter_g2()
Definition: panocpy/problem.hpp:334
panocpy.test.v
v
Definition: test.py:42
prob_full_getter_grad_g2i
auto prob_full_getter_grad_g2i()
Definition: panocpy/problem.hpp:377
panocpy.test.x
x
Definition: test.py:40
prob_full_setter_g1
auto prob_full_setter_g1()
Definition: panocpy/problem.hpp:249
prob_setter_f
auto prob_setter_f()
Definition: panocpy/problem.hpp:15
prob_setter_hess_L
auto prob_setter_hess_L()
Definition: panocpy/problem.hpp:144
prob_full_getter_grad_g1i
auto prob_full_getter_grad_g1i()
Definition: panocpy/problem.hpp:292
panocpy.test.grad_f
grad_f
Definition: test.py:50
panocpy.test.hess_L_prod
hess_L_prod
Definition: test.py:64
prob_full_getter_grad_g1_prod
auto prob_full_getter_grad_g1_prod()
Definition: panocpy/problem.hpp:261
prob_full_getter_g2
auto prob_full_getter_g2()
Definition: panocpy/problem.hpp:322
panocpy.test.grad_g_prod
grad_g_prod
Definition: test.py:52
prob_full_getter_grad_f
auto prob_full_getter_grad_f()
Definition: panocpy/problem.hpp:213
prob_full_setter_grad_g1i
auto prob_full_setter_grad_g1i()
Definition: panocpy/problem.hpp:308
prob_full_getter_grad_g2_prod
auto prob_full_getter_grad_g2_prod()
Definition: panocpy/problem.hpp:346
prob_getter_grad_g_prod
auto prob_getter_grad_g_prod()
Definition: panocpy/problem.hpp:67
panocpy.test.grad_gi
grad_gi
Definition: test.py:53
prob_getter_grad_f
auto prob_getter_grad_f()
Definition: panocpy/problem.hpp:19
pa::crvec
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
prob_full_getter_f
auto prob_full_getter_f()
Definition: panocpy/problem.hpp:199
prob_full_setter_grad_g2_prod
auto prob_full_setter_grad_g2_prod()
Definition: panocpy/problem.hpp:363
prob_full_setter_hess_L
auto prob_full_setter_hess_L()
Definition: panocpy/problem.hpp:423
prob_getter_hess_L_prod
auto prob_getter_hess_L_prod()
Definition: panocpy/problem.hpp:162
panocpy.test.hess_L
hess_L
Definition: test.py:63
prob_full_getter_g1
auto prob_full_getter_g1()
Definition: panocpy/problem.hpp:237
prob_full_getter_hess_L
auto prob_full_getter_hess_L()
Definition: panocpy/problem.hpp:407
panocpy.test.m
int m
Definition: test.py:39
prob_full_setter_grad_g2i
auto prob_full_setter_grad_g2i()
Definition: panocpy/problem.hpp:393
pa::mat
realmat mat
Default type for matrices.
Definition: vec.hpp:20
prob_getter_f
auto prob_getter_f()
Definition: panocpy/problem.hpp:5
prob_setter_grad_g_prod
auto prob_setter_grad_g_prod()
Definition: panocpy/problem.hpp:84
panocpy.test.g
g
Definition: test.py:51
problem.hpp
panocpy.test.p
p
Definition: test.py:57
prob_setter_grad_f
auto prob_setter_grad_f()
Definition: panocpy/problem.hpp:31
pa::ProblemFull
Problem description for minimization problems.
Definition: include/panoc-alm/util/problem.hpp:213
prob_getter_grad_gi
auto prob_getter_grad_gi()
Definition: panocpy/problem.hpp:98
panocpy.test.n
int n
Definition: test.py:38
prob_getter_hess_L
auto prob_getter_hess_L()
Definition: panocpy/problem.hpp:128
pa::real_t
double real_t
Default floating point type.
Definition: vec.hpp:8
prob_setter_hess_L_prod
auto prob_setter_hess_L_prod()
Definition: panocpy/problem.hpp:182
pa::Problem
Problem description for minimization problems.
Definition: include/panoc-alm/util/problem.hpp:24
main.H
H
Definition: main.py:8
prob_full_setter_grad_g1_prod
auto prob_full_setter_grad_g1_prod()
Definition: panocpy/problem.hpp:278
prob_full_setter_grad_f
auto prob_full_setter_grad_f()
Definition: panocpy/problem.hpp:225