alpaqa 1.0.0a12
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>
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 -> Sparsity {
51 return sparsity::Dense<config_t>{vtable.m, vtable.n};
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 const ProblemVTable &) {
69 throw not_implemented_error("eval_hess_L");
70}
71
72template <Config Conf>
74 -> Sparsity {
75 return sparsity::Dense<config_t>{vtable.n, vtable.n, sparsity::Symmetry::Upper};
76}
77
78template <Config Conf>
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>
90 const ProblemVTable &vtable) {
91 if (y.size() == 0 && vtable.eval_hess_L != default_eval_hess_L)
92 return vtable.eval_hess_L(self, x, y, scale, H_values, vtable);
93 throw not_implemented_error("eval_hess_ψ");
94}
95
96template <Config Conf>
98 -> Sparsity {
99 return sparsity::Dense<config_t>{vtable.n, vtable.n, sparsity::Symmetry::Upper};
100}
101
102/** @implementation{ProblemVTable<Conf>::default_eval_f_grad_f} */
103template <Config Conf>
104/* [ProblemVTable<Conf>::default_eval_f_grad_f] */
106 const ProblemVTable &vtable) -> real_t {
107 vtable.eval_grad_f(self, x, grad_fx);
108 return vtable.eval_f(self, x);
109}
110/* [ProblemVTable<Conf>::default_eval_f_grad_f] */
112/** @implementation{ProblemVTable<Conf>::default_eval_f_g} */
113template <Config Conf>
114/* [ProblemVTable<Conf>::default_eval_f_g] */
116 const ProblemVTable &vtable) -> real_t {
117 vtable.eval_g(self, x, g);
118 return vtable.eval_f(self, x);
120/* [ProblemVTable<Conf>::default_eval_f_g] */
121
122/** @implementation{ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod} */
123template <Config Conf>
124/* [ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod] */
127 const ProblemVTable &vtable) {
128 vtable.eval_grad_f(self, x, grad_f);
129 vtable.eval_grad_g_prod(self, x, y, grad_gxy);
131/* [ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod] */
132
133/** @implementation{ProblemVTable<Conf>::default_eval_grad_L} */
134template <Config Conf>
135/* [ProblemVTable<Conf>::default_eval_grad_L] */
136void ProblemVTable<Conf>::default_eval_grad_L(const void *self, crvec x, crvec y, rvec grad_L,
137 rvec work_n, const ProblemVTable &vtable) {
138 if (y.size() == 0) /* [[unlikely]] */
139 return vtable.eval_grad_f(self, x, grad_L);
140 vtable.eval_grad_f_grad_g_prod(self, x, y, grad_L, work_n, vtable);
141 grad_L += work_n;
142}
143/* [ProblemVTable<Conf>::default_eval_grad_L] */
145/** @implementation{ProblemVTable<Conf>::default_eval_ψ} */
146template <Config Conf>
147/* [ProblemVTable<Conf>::default_eval_ψ] */
149 const ProblemVTable &vtable) -> real_t {
150 if (y.size() == 0) /* [[unlikely]] */
151 return vtable.eval_f(self, x);
152
153 auto f = vtable.eval_f_g(self, x, ŷ, vtable);
154 auto dᵀŷ = calc_ŷ_dᵀŷ(self, ŷ, y, Σ, vtable);
155 // ψ(x) = f(x) + ½ dᵀŷ
156 auto ψ = f + real_t(0.5) * dᵀŷ;
157 return ψ;
158}
159/* [ProblemVTable<Conf>::default_eval_ψ] */
160
161/** @implementation{ProblemVTable<Conf>::default_eval_grad_ψ} */
162template <Config Conf>
163/* [ProblemVTable<Conf>::default_eval_grad_ψ] */
165 rvec grad_ψ, rvec work_n, rvec work_m,
166 const ProblemVTable &vtable) {
167 if (y.size() == 0) /* [[unlikely]] */ {
168 vtable.eval_grad_f(self, x, grad_ψ);
169 } else {
170 vtable.eval_g(self, x, work_m);
171 (void)calc_ŷ_dᵀŷ(self, work_m, y, Σ, vtable);
172 vtable.eval_grad_L(self, x, work_m, grad_ψ, work_n, vtable);
173 }
174}
175/* [ProblemVTable<Conf>::default_eval_grad_ψ] */
176
177/** @implementation{ProblemVTable<Conf>::default_eval_ψ_grad_ψ} */
178template <Config Conf>
179/* [ProblemVTable<Conf>::default_eval_ψ_grad_ψ] */
181 rvec grad_ψ, rvec work_n, rvec work_m,
182 const ProblemVTable &vtable) -> real_t {
183 if (y.size() == 0) /* [[unlikely]] */
184 return vtable.eval_f_grad_f(self, x, grad_ψ, vtable);
185
186 auto &ŷ = work_m;
187 // ψ(x) = f(x) + ½ dᵀŷ
188 auto f = vtable.eval_f_g(self, x, ŷ, vtable);
189 auto dᵀŷ = calc_ŷ_dᵀŷ(self, ŷ, y, Σ, vtable);
190 auto ψ = f + real_t(0.5) * dᵀŷ;
191 // ∇ψ(x) = ∇f(x) + ∇g(x) ŷ
192 vtable.eval_grad_L(self, x, ŷ, grad_ψ, work_n, vtable);
193 return ψ;
194}
195/* [ProblemVTable<Conf>::default_eval_ψ_grad_ψ] */
196
197template <Config Conf>
198auto ProblemVTable<Conf>::default_get_box_C(const void *, const ProblemVTable &) -> const Box & {
199 throw not_implemented_error("get_box_C");
200}
201
202template <Config Conf>
203auto ProblemVTable<Conf>::default_get_box_D(const void *, const ProblemVTable &) -> const Box & {
204 throw not_implemented_error("get_box_D");
205}
206
207template <Config Conf>
209
210} // namespace alpaqa
@ Upper
Symmetric, upper-triangular part is stored.
Dense matrix structure.
Definition sparsity.hpp:20
typename Conf::real_t real_t
Definition config.hpp:65
typename Conf::rindexvec rindexvec
Definition config.hpp:79
typename Conf::index_t index_t
Definition config.hpp:77
constexpr const auto inf
Definition config.hpp:85
typename Conf::rvec rvec
Definition config.hpp:69
typename Conf::crvec crvec
Definition config.hpp:70
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 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, 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 &)
optional_const_function_t< void(crvec x, crvec y, real_t scale, rvec H_values)> eval_hess_L
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
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 Sparsity default_get_jac_g_sparsity(const void *, const ProblemVTable &)
static void default_eval_hess_ψ(const void *self, crvec x, crvec y, crvec, real_t scale, rvec H_values, const ProblemVTable &vtable)
static real_t default_eval_f_g(const void *self, crvec x, rvec g, 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 void default_eval_hess_L(const void *, crvec, crvec, real_t, rvec, 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 Sparsity default_get_hess_L_sparsity(const void *, const ProblemVTable &)
static Sparsity default_get_hess_ψ_sparsity(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)
Stores any of the supported sparsity patterns.
Definition sparsity.hpp:105