alpaqa 1.0.0a9
Nonconvex constrained optimization
Loading...
Searching...
No Matches
type-erased-problem.tpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace alpaqa {
6
7template <Config Conf>
8auto ProblemVTable<Conf>::calc_ŷ_dᵀŷ(const void *self, rvec g_ŷ, crvec y, crvec Σ,
9 const ProblemVTable &vtable) -> real_t {
10 if (Σ.size() == 1) {
11 // ζ = g(x) + Σ⁻¹y
12 g_ŷ += (1 / Σ(0)) * y;
13 // d = ζ - Π(ζ, D)
14 vtable.eval_proj_diff_g(self, g_ŷ, g_ŷ);
15 // dᵀŷ, ŷ = Σ d
16 real_t dᵀŷ = Σ(0) * g_ŷ.dot(g_ŷ);
17 g_ŷ *= Σ(0);
18 return dᵀŷ;
19 } else {
20 // ζ = g(x) + Σ⁻¹y
21 g_ŷ += Σ.asDiagonal().inverse() * y;
22 // d = ζ - Π(ζ, D)
23 vtable.eval_proj_diff_g(self, g_ŷ, g_ŷ);
24 // dᵀŷ, ŷ = Σ d
25 real_t dᵀŷ = 0;
26 for (index_t i = 0; i < y.size(); ++i) {
27 dᵀŷ += g_ŷ(i) * Σ(i) * g_ŷ(i); // TODO: vectorize
28 g_ŷ(i) = Σ(i) * g_ŷ(i);
29 }
30 return dᵀŷ;
31 }
32}
33
34template <Config Conf>
36 rindexvec, const ProblemVTable &)
37 -> index_t {
38 throw not_implemented_error("eval_inactive_indices_res_lna");
39}
40
41template <Config Conf>
43 const ProblemVTable &vtable) {
44 if (vtable.m != 0)
45 throw not_implemented_error("eval_jac_g");
46}
47
48template <Config Conf>
50 -> length_t {
51 return -1;
52}
53
54template <Config Conf>
56 const ProblemVTable &) {
57 throw not_implemented_error("eval_grad_gi");
58}
59
60template <Config Conf>
62 const ProblemVTable &) {
63 throw not_implemented_error("eval_hess_L_prod");
64}
65
66template <Config Conf>
68 rindexvec, rvec, const ProblemVTable &) {
69 throw not_implemented_error("eval_hess_L");
70}
71
72template <Config Conf>
74 -> length_t {
75 return -1;
76}
77
78template <Config Conf>
80 real_t scale, crvec v, rvec Hv,
81 const ProblemVTable &vtable) {
83 return vtable.eval_hess_L_prod(self, x, y, scale, v, Hv, vtable);
84 throw not_implemented_error("eval_hess_ψ_prod");
85}
86
87template <Config Conf>
89 real_t scale, rindexvec inner_idx,
90 rindexvec outer_ptr, rvec H_values,
91 const ProblemVTable &vtable) {
92 if (y.size() == 0 && vtable.eval_hess_L != default_eval_hess_L)
93 return vtable.eval_hess_L(self, x, y, scale, inner_idx, outer_ptr, H_values, vtable);
94 throw not_implemented_error("eval_hess_ψ");
95}
96
97template <Config Conf>
99 -> length_t {
100 return 0;
101}
102
103/** @implementation{ProblemVTable<Conf>::default_eval_f_grad_f} */
104template <Config Conf>
105/* [ProblemVTable<Conf>::default_eval_f_grad_f] */
106auto ProblemVTable<Conf>::default_eval_f_grad_f(const void *self, crvec x, rvec grad_fx,
107 const ProblemVTable &vtable) -> real_t {
108 vtable.eval_grad_f(self, x, grad_fx);
109 return vtable.eval_f(self, x);
111/* [ProblemVTable<Conf>::default_eval_f_grad_f] */
113/** @implementation{ProblemVTable<Conf>::default_eval_f_g} */
114template <Config Conf>
115/* [ProblemVTable<Conf>::default_eval_f_g] */
117 const ProblemVTable &vtable) -> real_t {
118 vtable.eval_g(self, x, g);
119 return vtable.eval_f(self, x);
121/* [ProblemVTable<Conf>::default_eval_f_g] */
122
123/** @implementation{ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod} */
124template <Config Conf>
125/* [ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod] */
127 rvec grad_f, rvec grad_gxy,
128 const ProblemVTable &vtable) {
129 vtable.eval_grad_f(self, x, grad_f);
130 vtable.eval_grad_g_prod(self, x, y, grad_gxy);
132/* [ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod] */
134/** @implementation{ProblemVTable<Conf>::default_eval_grad_L} */
135template <Config Conf>
136/* [ProblemVTable<Conf>::default_eval_grad_L] */
137void ProblemVTable<Conf>::default_eval_grad_L(const void *self, crvec x, crvec y, rvec grad_L,
138 rvec work_n, const ProblemVTable &vtable) {
139 if (y.size() == 0) /* [[unlikely]] */
140 return vtable.eval_grad_f(self, x, grad_L);
141 vtable.eval_grad_f_grad_g_prod(self, x, y, grad_L, work_n, vtable);
142 grad_L += work_n;
144/* [ProblemVTable<Conf>::default_eval_grad_L] */
145
146/** @implementation{ProblemVTable<Conf>::default_eval_ψ} */
147template <Config Conf>
148/* [ProblemVTable<Conf>::default_eval_ψ] */
149auto ProblemVTable<Conf>::default_eval_ψ(const void *self, crvec x, crvec y, crvec Σ, rvec ŷ,
150 const ProblemVTable &vtable) -> real_t {
151 if (y.size() == 0) /* [[unlikely]] */
152 return vtable.eval_f(self, x);
153
154 auto f = vtable.eval_f_g(self, x, ŷ, vtable);
155 auto dᵀŷ = calc_ŷ_dᵀŷ(self, ŷ, y, Σ, vtable);
156 // ψ(x) = f(x) + ½ dᵀŷ
157 auto ψ = f + real_t(0.5) * dᵀŷ;
158 return ψ;
159}
160/* [ProblemVTable<Conf>::default_eval_ψ] */
161
162/** @implementation{ProblemVTable<Conf>::default_eval_grad_ψ} */
163template <Config Conf>
164/* [ProblemVTable<Conf>::default_eval_grad_ψ] */
166 rvec grad_ψ, rvec work_n, rvec work_m,
167 const ProblemVTable &vtable) {
168 if (y.size() == 0) /* [[unlikely]] */ {
169 vtable.eval_grad_f(self, x, grad_ψ);
170 } else {
171 vtable.eval_g(self, x, work_m);
172 (void)calc_ŷ_dᵀŷ(self, work_m, y, Σ, vtable);
173 vtable.eval_grad_L(self, x, work_m, grad_ψ, work_n, vtable);
174 }
175}
176/* [ProblemVTable<Conf>::default_eval_grad_ψ] */
177
178/** @implementation{ProblemVTable<Conf>::default_eval_ψ_grad_ψ} */
179template <Config Conf>
180/* [ProblemVTable<Conf>::default_eval_ψ_grad_ψ] */
182 rvec grad_ψ, rvec work_n, rvec work_m,
183 const ProblemVTable &vtable) -> real_t {
184 if (y.size() == 0) /* [[unlikely]] */
185 return vtable.eval_f_grad_f(self, x, grad_ψ, vtable);
186
187 auto &ŷ = work_m;
188 // ψ(x) = f(x) + ½ dᵀŷ
189 auto f = vtable.eval_f_g(self, x, ŷ, vtable);
190 auto dᵀŷ = calc_ŷ_dᵀŷ(self, ŷ, y, Σ, vtable);
191 auto ψ = f + real_t(0.5) * dᵀŷ;
192 // ∇ψ(x) = ∇f(x) + ∇g(x) ŷ
193 vtable.eval_grad_L(self, x, ŷ, grad_ψ, work_n, vtable);
194 return ψ;
195}
196/* [ProblemVTable<Conf>::default_eval_ψ_grad_ψ] */
197
198template <Config Conf>
199auto ProblemVTable<Conf>::default_get_box_C(const void *, const ProblemVTable &) -> const Box & {
200 throw not_implemented_error("get_box_C");
201}
202
203template <Config Conf>
204auto ProblemVTable<Conf>::default_get_box_D(const void *, const ProblemVTable &) -> const Box & {
205 throw not_implemented_error("get_box_D");
206}
207
208template <Config Conf>
210
211} // namespace alpaqa
typename Conf::real_t real_t
Definition: config.hpp:63
typename Conf::rindexvec rindexvec
Definition: config.hpp:77
typename Conf::index_t index_t
Definition: config.hpp:75
typename Conf::length_t length_t
Definition: config.hpp:74
typename Conf::rvec rvec
Definition: config.hpp:67
typename Conf::crvec crvec
Definition: config.hpp:68
Struct containing function pointers to all problem functions (like the objective and constraint funct...
required_const_function_t< real_t(crvec x)> eval_f
optional_const_function_t< void(crvec x, crvec y, real_t scale, crvec v, rvec Hv)> eval_hess_L_prod
static real_t default_eval_ψ(const void *self, crvec x, crvec y, crvec Σ, rvec ŷ, const ProblemVTable &vtable)
optional_const_function_t< real_t(crvec x, rvec grad_fx)> eval_f_grad_f
static length_t default_get_hess_L_num_nonzeros(const void *, const ProblemVTable &)
static void default_eval_hess_L(const void *, crvec, crvec, real_t, rindexvec, rindexvec, rvec, const ProblemVTable &)
static void default_eval_hess_L_prod(const void *, crvec, crvec, real_t, crvec, rvec, const ProblemVTable &)
optional_const_function_t< void(crvec x, crvec y, rvec grad_L, rvec work_n)> eval_grad_L
static void default_eval_hess_ψ_prod(const void *self, crvec x, crvec y, crvec, real_t scale, crvec v, rvec Hv, const ProblemVTable &vtable)
static void default_eval_jac_g(const void *, crvec, rindexvec, rindexvec, rvec, const ProblemVTable &)
optional_const_function_t< real_t(crvec x, rvec g)> eval_f_g
static void default_eval_grad_gi(const void *, crvec, index_t, rvec, const ProblemVTable &)
static length_t default_get_jac_g_num_nonzeros(const void *, const ProblemVTable &)
static void default_eval_grad_ψ(const void *self, crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m, const ProblemVTable &vtable)
required_const_function_t< void(crvec x, rvec gx)> eval_g
optional_const_function_t< void(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values)> eval_hess_L
static const Box & default_get_box_C(const void *, const ProblemVTable &)
static void default_eval_grad_L(const void *self, crvec x, crvec y, rvec grad_L, rvec work_n, const ProblemVTable &vtable)
static const Box & default_get_box_D(const void *, const ProblemVTable &)
optional_const_function_t< void(crvec x, crvec y, rvec grad_f, rvec grad_gxy)> eval_grad_f_grad_g_prod
static real_t default_eval_f_g(const void *self, crvec x, rvec g, const ProblemVTable &vtable)
static void default_eval_hess_ψ(const void *self, crvec x, crvec y, crvec, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values, const ProblemVTable &vtable)
static index_t default_eval_inactive_indices_res_lna(const void *, real_t, crvec, crvec, rindexvec, const ProblemVTable &)
static void default_check(const void *, const ProblemVTable &)
static real_t calc_ŷ_dᵀŷ(const void *self, rvec g_ŷ, crvec y, crvec Σ, const ProblemVTable &vtable)
static void default_eval_grad_f_grad_g_prod(const void *self, crvec x, crvec y, rvec grad_f, rvec grad_gxy, const ProblemVTable &vtable)
required_const_function_t< void(crvec x, crvec y, rvec grad_gxy)> eval_grad_g_prod
static real_t default_eval_f_grad_f(const void *self, crvec x, rvec grad_fx, const ProblemVTable &vtable)
required_const_function_t< void(crvec x, rvec grad_fx)> eval_grad_f
static length_t default_get_hess_ψ_num_nonzeros(const void *, const ProblemVTable &)
static real_t default_eval_ψ_grad_ψ(const void *self, crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m, const ProblemVTable &vtable)