alpaqa 0.0.1
Nonconvex constrained optimization
alpaqa/problem.hpp
Go to the documentation of this file.
1#pragma once
2
4
5inline auto prob_getter_f() {
6 return [](const alpaqa::Problem &p) -> std::function<alpaqa::real_t(alpaqa::crvec)> {
7 return [n{p.n}, f{p.f}](alpaqa::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}
15inline auto prob_setter_f() {
16 return [](alpaqa::Problem &p,
17 std::function<alpaqa::real_t(alpaqa::crvec)> fun) -> void { p.f = fun; };
18}
19inline auto prob_getter_grad_f() {
20 return [](const alpaqa::Problem &p) -> std::function<alpaqa::vec(alpaqa::crvec)> {
21 return [n{p.n}, grad_f{p.grad_f}](alpaqa::crvec x) {
22 if (x.size() != n)
23 throw std::out_of_range("Dimension of x not consistent "
24 "with problem dimension n");
25 alpaqa::vec gr(n);
26 grad_f(x, gr);
27 return gr;
28 };
29 };
30}
31inline auto prob_setter_grad_f() {
32 return [](alpaqa::Problem &p, std::function<alpaqa::vec(alpaqa::crvec)> fun) -> void {
33 p.grad_f = [n{p.n}, fun{std::move(fun)}](alpaqa::crvec x, alpaqa::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}
43inline auto prob_getter_g() {
44 return [](const alpaqa::Problem &p) -> std::function<alpaqa::vec(alpaqa::crvec)> {
45 return [n{p.n}, m{p.m}, g{p.g}](alpaqa::crvec x) {
46 if (x.size() != n)
47 throw std::out_of_range("Dimension of x not consistent "
48 "with problem dimension n");
49 alpaqa::vec gg(m);
50 g(x, gg);
51 return gg;
52 };
53 };
54}
55inline auto prob_setter_g() {
56 return [](alpaqa::Problem &p, std::function<alpaqa::vec(alpaqa::crvec)> fun) -> void {
57 p.g = [m{p.m}, fun{std::move(fun)}](alpaqa::crvec x, alpaqa::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}
68 return [](const alpaqa::Problem &p)
69 -> std::function<alpaqa::vec(alpaqa::crvec, alpaqa::crvec)> {
70 return [n{p.n}, m{p.m}, grad_g_prod{p.grad_g_prod}](alpaqa::crvec x,
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 alpaqa::vec gy(n);
79 grad_g_prod(x, y, gy);
80 return gy;
81 };
82 };
83}
85 return [](alpaqa::Problem &p,
86 std::function<alpaqa::vec(alpaqa::crvec, alpaqa::crvec)> fun) -> void {
87 p.grad_g_prod = [n{p.n}, fun{std::move(fun)}](alpaqa::crvec x, alpaqa::crvec y,
88 alpaqa::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}
98inline auto prob_getter_grad_gi() {
99 return [](const alpaqa::Problem &p)
100 -> std::function<alpaqa::vec(alpaqa::crvec, unsigned)> {
101 return [n{p.n}, m{p.m}, grad_gi{p.grad_gi}](alpaqa::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 alpaqa::vec gg(n);
109 grad_gi(x, i, gg);
110 return gg;
111 };
112 };
113}
114inline auto prob_setter_grad_gi() {
115 return [](alpaqa::Problem &p,
116 std::function<alpaqa::vec(alpaqa::crvec, unsigned)> fun) -> void {
117 p.grad_gi = [n{p.n}, fun{std::move(fun)}](alpaqa::crvec x, unsigned i,
118 alpaqa::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}
128inline auto prob_getter_hess_L() {
129 return [](const alpaqa::Problem &p)
130 -> std::function<alpaqa::mat(alpaqa::crvec, alpaqa::crvec)> {
131 return [n{p.n}, m{p.m}, hess_L{p.hess_L}](alpaqa::crvec x, alpaqa::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 alpaqa::mat H(n, n);
139 hess_L(x, y, H);
140 return H;
141 };
142 };
143}
144inline auto prob_setter_hess_L() {
145 return [](alpaqa::Problem &p,
146 std::function<alpaqa::mat(alpaqa::crvec, alpaqa::crvec)> fun) -> void {
147 p.hess_L = [n{p.n}, fun{std::move(fun)}](alpaqa::crvec x, alpaqa::crvec y,
148 alpaqa::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}
163 return [](const alpaqa::Problem &p)
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 alpaqa::vec Hv(n);
177 hess_L_prod(x, y, v, Hv);
178 return Hv;
179 };
180 };
181}
183 return [](alpaqa::Problem &p,
185 -> void {
186 p.hess_L_prod = [n{p.n}, fun{std::move(fun)}](alpaqa::crvec x, alpaqa::crvec y,
188 alpaqa::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}
auto prob_getter_f()
auto prob_setter_grad_f()
auto prob_getter_grad_gi()
auto prob_setter_grad_gi()
auto prob_setter_g()
auto prob_getter_hess_L_prod()
auto prob_getter_g()
auto prob_setter_hess_L_prod()
auto prob_setter_grad_g_prod()
auto prob_getter_hess_L()
auto prob_setter_hess_L()
auto prob_getter_grad_g_prod()
auto prob_getter_grad_f()
auto prob_setter_f()
hess_L_prod
Definition: test.py:66
grad_g_prod
Definition: test.py:54
int m
Definition: test.py:41
int n
Definition: test.py:40
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
realmat mat
Default type for matrices.
Definition: vec.hpp:20
realvec vec
Default type for vectors.
Definition: vec.hpp:14
Eigen::Ref< mat > rmat
Default type for mutable references to matrices.
Definition: vec.hpp:22
double real_t
Default floating point type.
Definition: vec.hpp:8
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
H
Definition: main.py:8
Problem description for minimization problems.