alpaqa 1.0.0a17
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_ŷ += y.cwiseQuotient(Σ);
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 (vtable.m == 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 if (vtable.m == 0 && vtable.get_hess_L_sparsity != default_get_hess_L_sparsity)
100 return vtable.get_hess_L_sparsity(self, vtable);
101 return sparsity::Dense<config_t>{vtable.n, vtable.n, sparsity::Symmetry::Upper};
102}
103
104/** @implementation{ProblemVTable<Conf>::default_eval_f_grad_f} */
105template <Config Conf>
106/* [ProblemVTable<Conf>::default_eval_f_grad_f] */
108 const ProblemVTable &vtable) -> real_t {
109 vtable.eval_grad_f(self, x, grad_fx);
110 return vtable.eval_f(self, x);
112/* [ProblemVTable<Conf>::default_eval_f_grad_f] */
114/** @implementation{ProblemVTable<Conf>::default_eval_f_g} */
115template <Config Conf>
116/* [ProblemVTable<Conf>::default_eval_f_g] */
118 const ProblemVTable &vtable) -> real_t {
119 vtable.eval_g(self, x, g);
120 return vtable.eval_f(self, x);
122/* [ProblemVTable<Conf>::default_eval_f_g] */
123
124/** @implementation{ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod} */
125template <Config Conf>
126/* [ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod] */
128 rvec grad_f, rvec grad_gxy,
129 const ProblemVTable &vtable) {
130 vtable.eval_grad_f(self, x, grad_f);
131 vtable.eval_grad_g_prod(self, x, y, grad_gxy);
133/* [ProblemVTable<Conf>::default_eval_grad_f_grad_g_prod] */
135/** @implementation{ProblemVTable<Conf>::default_eval_grad_L} */
136template <Config Conf>
137/* [ProblemVTable<Conf>::default_eval_grad_L] */
138void ProblemVTable<Conf>::default_eval_grad_L(const void *self, crvec x, crvec y, rvec grad_L,
139 rvec work_n, const ProblemVTable &vtable) {
140 if (y.size() == 0) /* [[unlikely]] */
141 return vtable.eval_grad_f(self, x, grad_L);
142 vtable.eval_grad_f_grad_g_prod(self, x, y, grad_L, work_n, vtable);
143 grad_L += work_n;
145/* [ProblemVTable<Conf>::default_eval_grad_L] */
146
147/** @implementation{ProblemVTable<Conf>::default_eval_ψ} */
148template <Config Conf>
149/* [ProblemVTable<Conf>::default_eval_ψ] */
150auto ProblemVTable<Conf>::default_eval_ψ(const void *self, crvec x, crvec y, crvec Σ, rvec ŷ,
151 const ProblemVTable &vtable) -> real_t {
152 if (y.size() == 0) /* [[unlikely]] */
153 return vtable.eval_f(self, x);
154
155 auto f = vtable.eval_f_g(self, x, ŷ, vtable);
156 auto dᵀŷ = calc_ŷ_dᵀŷ(self, ŷ, y, Σ, vtable);
157 // ψ(x) = f(x) + ½ dᵀŷ
158 auto ψ = f + real_t(0.5) * dᵀŷ;
159 return ψ;
160}
161/* [ProblemVTable<Conf>::default_eval_ψ] */
162
163/** @implementation{ProblemVTable<Conf>::default_eval_grad_ψ} */
164template <Config Conf>
165/* [ProblemVTable<Conf>::default_eval_grad_ψ] */
167 rvec grad_ψ, rvec work_n, rvec work_m,
168 const ProblemVTable &vtable) {
169 if (y.size() == 0) /* [[unlikely]] */ {
170 vtable.eval_grad_f(self, x, grad_ψ);
171 } else {
172 vtable.eval_g(self, x, work_m);
173 (void)calc_ŷ_dᵀŷ(self, work_m, y, Σ, vtable);
174 vtable.eval_grad_L(self, x, work_m, grad_ψ, work_n, vtable);
175 }
176}
177/* [ProblemVTable<Conf>::default_eval_grad_ψ] */
178
179/** @implementation{ProblemVTable<Conf>::default_eval_ψ_grad_ψ} */
180template <Config Conf>
181/* [ProblemVTable<Conf>::default_eval_ψ_grad_ψ] */
183 rvec grad_ψ, rvec work_n, rvec work_m,
184 const ProblemVTable &vtable) -> real_t {
185 if (y.size() == 0) /* [[unlikely]] */
186 return vtable.eval_f_grad_f(self, x, grad_ψ, vtable);
187
188 auto &ŷ = work_m;
189 // ψ(x) = f(x) + ½ dᵀŷ
190 auto f = vtable.eval_f_g(self, x, ŷ, vtable);
191 auto dᵀŷ = calc_ŷ_dᵀŷ(self, ŷ, y, Σ, vtable);
192 auto ψ = f + real_t(0.5) * dᵀŷ;
193 // ∇ψ(x) = ∇f(x) + ∇g(x) ŷ
194 vtable.eval_grad_L(self, x, ŷ, grad_ψ, work_n, vtable);
195 return ψ;
196}
197/* [ProblemVTable<Conf>::default_eval_ψ_grad_ψ] */
198
199template <Config Conf>
200auto ProblemVTable<Conf>::default_get_box_C(const void *, const ProblemVTable &) -> const Box & {
201 throw not_implemented_error("get_box_C");
202}
203
204template <Config Conf>
205auto ProblemVTable<Conf>::default_get_box_D(const void *, const ProblemVTable &) -> const Box & {
206 throw not_implemented_error("get_box_D");
207}
208
209template <Config Conf>
211
212template <Config Conf>
213std::string ProblemVTable<Conf>::default_get_name(const void *, const ProblemVTable &) {
214 return "unknown problem";
215}
216
217} // namespace alpaqa
@ Upper
Symmetric, upper-triangular part is stored.
Dense matrix structure.
Definition sparsity.hpp:21
typename Conf::real_t real_t
Definition config.hpp:86
typename Conf::rindexvec rindexvec
Definition config.hpp:106
typename Conf::index_t index_t
Definition config.hpp:104
constexpr const auto inf
Definition config.hpp:112
typename Conf::rvec rvec
Definition config.hpp:91
typename Conf::crvec crvec
Definition config.hpp:92
Struct containing function pointers to all problem functions (like the objective and constraint funct...
static std::string default_get_name(const void *, const ProblemVTable &)
static real_t default_eval_ψ(const void *self, crvec x, crvec y, crvec Σ, rvec ŷ, const ProblemVTable &vtable)
required_function_t< void(crvec x, rvec grad_fx) const > eval_grad_f
static void default_eval_hess_L_prod(const void *, crvec, crvec, real_t, crvec, rvec, const ProblemVTable &)
required_function_t< real_t(crvec x) const > eval_f
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_function_t< real_t(crvec x, rvec g) const > eval_f_g
static void default_eval_grad_gi(const void *, crvec, index_t, rvec, const ProblemVTable &)
required_function_t< void(crvec x, rvec gx) const > eval_g
static void default_eval_grad_ψ(const void *self, crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m, const ProblemVTable &vtable)
optional_function_t< void(crvec x, crvec y, rvec grad_L, rvec work_n) const > eval_grad_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_function_t< void(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const > eval_hess_L_prod
static Sparsity default_get_jac_g_sparsity(const void *, const ProblemVTable &)
optional_function_t< void(crvec x, crvec y, real_t scale, rvec H_values) const > eval_hess_L
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)
required_function_t< void(crvec x, crvec y, rvec grad_gxy) const > eval_grad_g_prod
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 &)
optional_function_t< real_t(crvec x, rvec grad_fx) const > eval_f_grad_f
static void default_eval_hess_L(const void *, crvec, crvec, real_t, rvec, const ProblemVTable &)
optional_function_t< void(crvec x, crvec y, rvec grad_f, rvec grad_gxy) const > eval_grad_f_grad_g_prod
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)
static real_t default_eval_f_grad_f(const void *self, crvec x, rvec grad_fx, const ProblemVTable &vtable)
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:106