alpaqa 1.0.0a8
Nonconvex constrained optimization
Loading...
Searching...
No Matches
type-erased-problem.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <alpaqa/export.hpp>
11#include <chrono>
12#include <stdexcept>
13#include <type_traits>
14#include <utility>
15
16namespace alpaqa {
17
18/// Struct containing function pointers to all problem functions (like the
19/// objective and constraint functions, with their derivatives, and more).
20/// Some default implementations are available.
21/// Internal struct, it is used by @ref TypeErasedProblem.
22template <Config Conf>
26
27 template <class F>
29 template <class F>
32
33 // clang-format off
34
35 // Required
44 required_const_function_t<void(crvec x, rvec grad_fx)>
48 required_const_function_t<void(crvec x, crvec y, rvec grad_gxy)>
52
53 // Second order
54 optional_const_function_t<void(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values)>
58 optional_const_function_t<void(crvec x, index_t i, rvec grad_gi)>
60 optional_const_function_t<void(crvec x, crvec y, real_t scale, crvec v, rvec Hv)>
62 optional_const_function_t<void(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values)>
66 optional_const_function_t<void(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv)>
68 optional_const_function_t<void(crvec x, crvec y, crvec Σ, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values)>
72
73 // Combined evaluations
78 optional_const_function_t<void(crvec x, crvec y, rvec grad_f, rvec grad_gxy)>
80
81 // Lagrangian and augmented lagrangian evaluations
82 optional_const_function_t<void(crvec x, crvec y, rvec grad_L, rvec work_n)>
86 optional_const_function_t<void(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m)>
88 optional_const_function_t<real_t(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m)>
90
91 // Constraint sets
96
97 // Check
100
101 // clang-format on
102
103 ALPAQA_EXPORT static real_t calc_ŷ_dᵀŷ(const void *self, rvec g_ŷ, crvec y, crvec Σ,
104 const ProblemVTable &vtable);
105 ALPAQA_EXPORT static index_t default_eval_inactive_indices_res_lna(const void *, real_t, crvec,
107 const ProblemVTable &);
108 ALPAQA_EXPORT static void default_eval_jac_g(const void *, crvec, rindexvec, rindexvec, rvec,
109 const ProblemVTable &);
110 ALPAQA_EXPORT static length_t default_get_jac_g_num_nonzeros(const void *,
111 const ProblemVTable &);
112 ALPAQA_EXPORT static void default_eval_grad_gi(const void *, crvec, index_t, rvec,
113 const ProblemVTable &);
114 ALPAQA_EXPORT static void default_eval_hess_L_prod(const void *, crvec, crvec, real_t, crvec,
115 rvec, const ProblemVTable &);
116 ALPAQA_EXPORT static void default_eval_hess_L(const void *, crvec, crvec, real_t, rindexvec,
117 rindexvec, rvec, const ProblemVTable &);
118 ALPAQA_EXPORT static length_t default_get_hess_L_num_nonzeros(const void *,
119 const ProblemVTable &);
120 ALPAQA_EXPORT static void default_eval_hess_ψ_prod(const void *self, crvec x, crvec y, crvec,
121 real_t scale, crvec v, rvec Hv,
122 const ProblemVTable &vtable);
123 ALPAQA_EXPORT static void default_eval_hess_ψ(const void *self, crvec x, crvec y, crvec,
124 real_t scale, rindexvec inner_idx,
125 rindexvec outer_ptr, rvec H_values,
126 const ProblemVTable &vtable);
127 ALPAQA_EXPORT static length_t default_get_hess_ψ_num_nonzeros(const void *,
128 const ProblemVTable &);
129 ALPAQA_EXPORT static real_t default_eval_f_grad_f(const void *self, crvec x, rvec grad_fx,
130 const ProblemVTable &vtable);
131 ALPAQA_EXPORT static real_t default_eval_f_g(const void *self, crvec x, rvec g,
132 const ProblemVTable &vtable);
133 ALPAQA_EXPORT static void default_eval_grad_f_grad_g_prod(const void *self, crvec x, crvec y,
134 rvec grad_f, rvec grad_gxy,
135 const ProblemVTable &vtable);
136 ALPAQA_EXPORT static void default_eval_grad_L(const void *self, crvec x, crvec y, rvec grad_L,
137 rvec work_n, const ProblemVTable &vtable);
138 ALPAQA_EXPORT static real_t default_eval_ψ(const void *self, crvec x, crvec y, crvec Σ, rvec ŷ,
139 const ProblemVTable &vtable);
140 ALPAQA_EXPORT static void default_eval_grad_ψ(const void *self, crvec x, crvec y, crvec Σ,
141 rvec grad_ψ, rvec work_n, rvec work_m,
142 const ProblemVTable &vtable);
143 ALPAQA_EXPORT static real_t default_eval_ψ_grad_ψ(const void *self, crvec x, crvec y, crvec Σ,
144 rvec grad_ψ, rvec work_n, rvec work_m,
145 const ProblemVTable &vtable);
146 ALPAQA_EXPORT static const Box &default_get_box_C(const void *, const ProblemVTable &);
147 ALPAQA_EXPORT static const Box &default_get_box_D(const void *, const ProblemVTable &);
148 ALPAQA_EXPORT static void default_check(const void *, const ProblemVTable &);
149
151
152 template <class P>
153 ProblemVTable(std::in_place_t, P &p) : util::BasicVTable{std::in_place, p} {
154 auto &vtable = *this;
155
156 // Initialize all methods
157
158 // Required
167 // Second order
177 // Combined evaluations
179 ALPAQA_TE_OPTIONAL_METHOD(vtable, P, eval_f_g, p);
181 // Lagrangian and augmented lagrangian evaluations
183 ALPAQA_TE_OPTIONAL_METHOD(vtable, P, eval_ψ, p);
186 // Constraint set
189 // Check
190 ALPAQA_TE_OPTIONAL_METHOD(vtable, P, check, p);
191
192 // Dimensions
193 vtable.n = p.get_n();
194 vtable.m = p.get_m();
195 }
196 ProblemVTable() = default;
197};
198
203#ifdef ALPAQA_WITH_QUAD_PRECISION
204ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, ProblemVTable, EigenConfigq);
205#endif
206
207/// @addtogroup grp_Problems
208/// @{
209
210/// The main polymorphic minimization problem interface.
211///
212/// This class wraps the actual problem implementation class, filling in the
213/// missing member functions with sensible defaults, and providing a uniform
214/// interface that is used by the solvers.
215///
216/// The problem implementations do not inherit from an abstract base class.
217/// Instead, [structural typing](https://en.wikipedia.org/wiki/Structural_type_system)
218/// is used. The @ref ProblemVTable constructor uses reflection to discover
219/// which member functions are provided by the problem implementation. See
220/// @ref page_problem_formulations for more information, and
221/// @ref C++/CustomCppProblem/main.cpp for an example.
222template <Config Conf = DefaultConfig, class Allocator = std::allocator<std::byte>>
223class TypeErasedProblem : public util::TypeErased<ProblemVTable<Conf>, Allocator> {
224 public:
228 using allocator_type = Allocator;
231
232 protected:
233 using TypeErased::call;
234 using TypeErased::self;
235 using TypeErased::vtable;
236
237 public:
238 template <class T, class... Args>
239 static TypeErasedProblem make(Args &&...args) {
240 return TypeErased::template make<TypeErasedProblem, T>(std::forward<Args>(args)...);
241 }
242
243 /// @name Problem dimensions
244 /// @{
245
246 /// **[Required]**
247 /// Number of decision variables.
249 /// **[Required]**
250 /// Number of constraints.
252
253 /// @}
254
255 /// @name Required cost and constraint functions
256 /// @{
257
258 /// **[Required]**
259 /// Function that evaluates the cost, @f$ f(x) @f$
260 /// @param [in] x
261 /// Decision variable @f$ x \in \R^n @f$
263 /// **[Required]**
264 /// Function that evaluates the gradient of the cost, @f$ \nabla f(x) @f$
265 /// @param [in] x
266 /// Decision variable @f$ x \in \R^n @f$
267 /// @param [out] grad_fx
268 /// Gradient of cost function @f$ \nabla f(x) \in \R^n @f$
269 void eval_grad_f(crvec x, rvec grad_fx) const;
270 /// **[Required]**
271 /// Function that evaluates the constraints, @f$ g(x) @f$
272 /// @param [in] x
273 /// Decision variable @f$ x \in \R^n @f$
274 /// @param [out] gx
275 /// Value of the constraints @f$ g(x) \in \R^m @f$
276 void eval_g(crvec x, rvec gx) const;
277 /// **[Required]**
278 /// Function that evaluates the gradient of the constraints times a vector,
279 /// @f$ \nabla g(x)\,y = \tp{\jac_g(x)}y @f$
280 /// @param [in] x
281 /// Decision variable @f$ x \in \R^n @f$
282 /// @param [in] y
283 /// Vector @f$ y \in \R^m @f$ to multiply the gradient by
284 /// @param [out] grad_gxy
285 /// Gradient of the constraints
286 /// @f$ \nabla g(x)\,y \in \R^n @f$
287 void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const;
288
289 /// @}
290
291 /// @name Projections onto constraint sets and proximal mappings
292 /// @{
293
294 /// **[Required]**
295 /// Function that evaluates the difference between the given point @f$ z @f$
296 /// and its projection onto the constraint set @f$ D @f$.
297 /// @param [in] z
298 /// Slack variable, @f$ z \in \R^m @f$
299 /// @param [out] e
300 /// The difference relative to its projection,
301 /// @f$ e = z - \Pi_D(z) \in \R^m @f$
302 /// @note @p z and @p e can refer to the same vector.
303 void eval_proj_diff_g(crvec z, rvec e) const;
304 /// **[Required]**
305 /// Function that projects the Lagrange multipliers for ALM.
306 /// @param [inout] y
307 /// Multipliers, @f$ y \leftarrow \Pi_Y(y) \in \R^m @f$
308 /// @param [in] M
309 /// The radius/size of the set @f$ Y @f$.
310 /// See @ref ALMParams::max_multiplier.
312 /// **[Required]**
313 /// Function that computes a proximal gradient step.
314 /// @param [in] γ
315 /// Step size, @f$ \gamma \in \R_{>0} @f$
316 /// @param [in] x
317 /// Decision variable @f$ x \in \R^n @f$
318 /// @param [in] grad_ψ
319 /// Gradient of the subproblem cost, @f$ \nabla\psi(x) \in \R^n @f$
320 /// @param [out] x̂
321 /// Next proximal gradient iterate, @f$ \hat x = T_\gamma(x) =
322 /// \prox_{\gamma h}(x - \gamma\nabla\psi(x)) \in \R^n @f$
323 /// @param [out] p
324 /// The proximal gradient step,
325 /// @f$ p = \hat x - x \in \R^n @f$
326 /// @return The nonsmooth function evaluated at x̂,
327 /// @f$ h(\hat x) @f$.
328 /// @note The vector @f$ p @f$ is often used in stopping criteria, so its
329 /// numerical accuracy is more important than that of @f$ \hat x @f$.
330 real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const;
331 /// **[Optional]**
332 /// Function that computes the inactive indices @f$ \mathcal J(x) @f$ for
333 /// the evaluation of the linear Newton approximation of the residual, as in
334 /// @cite pas2022alpaqa.
335 /// @param [in] γ
336 /// Step size, @f$ \gamma \in \R_{>0} @f$
337 /// @param [in] x
338 /// Decision variable @f$ x \in \R^n @f$
339 /// @param [in] grad_ψ
340 /// Gradient of the subproblem cost, @f$ \nabla\psi(x) \in \R^n @f$
341 /// @param [out] J
342 /// The indices of the components of @f$ x @f$ that are in the
343 /// index set @f$ \mathcal J(x) @f$. In ascending order, at most n.
344 /// @return The number of inactive constraints, @f$ \# \mathcal J(x) @f$.
345 ///
346 /// For example, in the case of box constraints, we have
347 /// @f[ \mathcal J(x) \defeq \defset{i \in \N_{[0, n-1]}}{\underline x_i
348 /// \lt x_i - \gamma\nabla_{\!x_i}\psi(x) \lt \overline x_i}. @f]
350
351 /// @}
352
353 /// @name Constraint sets
354 /// @{
355
356 /// **[Optional]**
357 /// Get the rectangular constraint set of the decision variables,
358 /// @f$ x \in C @f$.
359 const Box &get_box_C() const;
360 /// **[Optional]**
361 /// Get the rectangular constraint set of the general constraint function,
362 /// @f$ g(x) \in D @f$.
363 const Box &get_box_D() const;
364
365 /// @}
366
367 /// @name Functions for second-order solvers
368 /// @{
369
370 /// **[Optional]**
371 /// Function that evaluates the Jacobian of the constraints as a sparse
372 /// matrix, @f$ \jac_g(x) @f$
373 /// @param [in] x
374 /// Decision variable @f$ x \in \R^n @f$
375 /// @param [inout] inner_idx
376 /// Inner indices (row indices of nonzeros).
377 /// @param [inout] outer_ptr
378 /// Outer pointers (points to the first nonzero in each column).
379 /// @param [out] J_values
380 /// Nonzero values of the Jacobian
381 /// @f$ \jac_g(x) \in \R^{m\times n} @f$
382 /// If @p J_values has size zero, this function should initialize
383 /// @p inner_idx and @p outer_ptr. If @p J_values is nonempty, @p inner_idx
384 /// and @p outer_ptr can be assumed to be initialized, and this function
385 /// should evaluate @p J_values.
386 ///
387 /// Required for second-order solvers only.
388 void eval_jac_g(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values) const;
389 /// **[Optional]**
390 /// Function that gets the number of nonzeros of the sparse Jacobian of the
391 /// constraints. Should return -1 for a dense Jacobian.
392 ///
393 /// Required for second-order solvers only.
395 /// **[Optional]**
396 /// Function that evaluates the gradient of one specific constraint,
397 /// @f$ \nabla g_i(x) @f$
398 /// @param [in] x
399 /// Decision variable @f$ x \in \R^n @f$
400 /// @param [in] i
401 /// Which constraint @f$ 0 \le i \lt m @f$
402 /// @param [out] grad_gi
403 /// Gradient of the constraint
404 /// @f$ \nabla g_i(x) \in \R^n @f$
405 ///
406 /// Required for second-order solvers only.
407 void eval_grad_gi(crvec x, index_t i, rvec grad_gi) const;
408 /// **[Optional]**
409 /// Function that evaluates the Hessian of the Lagrangian multiplied by a
410 /// vector,
411 /// @f$ \nabla_{xx}^2L(x, y)\,v @f$
412 /// @param [in] x
413 /// Decision variable @f$ x \in \R^n @f$
414 /// @param [in] y
415 /// Lagrange multipliers @f$ y \in \R^m @f$
416 /// @param [in] scale
417 /// Scale factor for the cost function.
418 /// @param [in] v
419 /// Vector to multiply by @f$ v \in \R^n @f$
420 /// @param [out] Hv
421 /// Hessian-vector product
422 /// @f$ \nabla_{xx}^2 L(x, y)\,v \in \R^{n} @f$
423 ///
424 /// Required for second-order solvers only.
425 void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const;
426 /// **[Optional]**
427 /// Function that evaluates the Hessian of the Lagrangian as a sparse matrix,
428 /// @f$ \nabla_{xx}^2L(x, y) @f$
429 /// @param [in] x
430 /// Decision variable @f$ x \in \R^n @f$
431 /// @param [in] y
432 /// Lagrange multipliers @f$ y \in \R^m @f$
433 /// @param [in] scale
434 /// Scale factor for the cost function.
435 /// @param [inout] inner_idx
436 /// Inner indices (row indices of nonzeros).
437 /// @param [inout] outer_ptr
438 /// Outer pointers (points to the first nonzero in each column).
439 /// @param [out] H_values
440 /// Nonzero values of the Hessian
441 /// @f$ \nabla_{xx}^2 L(x, y) \in \R^{n\times n} @f$.
442 /// If @p H_values has size zero, this function should initialize
443 /// @p inner_idx and @p outer_ptr. If @p H_values is nonempty, @p inner_idx
444 /// and @p outer_ptr can be assumed to be initialized, and this function
445 /// should evaluate @p H_values.
446 ///
447 /// Required for second-order solvers only.
448 void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr,
449 rvec H_values) const;
450 /// **[Optional]**
451 /// Function that gets the number of nonzeros of the sparse Hessian of the
452 /// Lagrangian. Should return -1 for a dense Hessian.
453 ///
454 /// Required for second-order solvers only.
456 /// **[Optional]**
457 /// Function that evaluates the Hessian of the augmented Lagrangian
458 /// multiplied by a vector,
459 /// @f$ \nabla_{xx}^2L_\Sigma(x, y)\,v @f$
460 /// @param [in] x
461 /// Decision variable @f$ x \in \R^n @f$
462 /// @param [in] y
463 /// Lagrange multipliers @f$ y \in \R^m @f$
464 /// @param [in] Σ
465 /// Penalty weights @f$ \Sigma @f$
466 /// @param [in] scale
467 /// Scale factor for the cost function.
468 /// @param [in] v
469 /// Vector to multiply by @f$ v \in \R^n @f$
470 /// @param [out] Hv
471 /// Hessian-vector product
472 /// @f$ \nabla_{xx}^2 L_\Sigma(x, y)\,v \in \R^{n} @f$
473 ///
474 /// Required for second-order solvers only.
475 void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const;
476 /// **[Optional]**
477 /// Function that evaluates the Hessian of the augmented Lagrangian,
478 /// @f$ \nabla_{xx}^2L_\Sigma(x, y) @f$
479 /// @param [in] x
480 /// Decision variable @f$ x \in \R^n @f$
481 /// @param [in] y
482 /// Lagrange multipliers @f$ y \in \R^m @f$
483 /// @param [in] Σ
484 /// Penalty weights @f$ \Sigma @f$
485 /// @param [in] scale
486 /// Scale factor for the cost function.
487 /// @param [inout] inner_idx
488 /// Inner indices (row indices of nonzeros).
489 /// @param [inout] outer_ptr
490 /// Outer pointers (points to the first nonzero in each column).
491 /// @param [out] H_values
492 /// Nonzero values of the Hessian
493 /// @f$ \nabla_{xx}^2 L_\Sigma(x, y) \in \R^{n\times n} @f$
494 /// If @p H_values has size zero, this function should initialize
495 /// @p inner_idx and @p outer_ptr. If @p H_values is nonempty, @p inner_idx
496 /// and @p outer_ptr can be assumed to be initialized, and this function
497 /// should evaluate @p H_values.
498 ///
499 /// Required for second-order solvers only.
500 void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rindexvec inner_idx,
501 rindexvec outer_ptr, rvec H_values) const;
502 /// **[Optional]**
503 /// Function that gets the number of nonzeros of the Hessian of the
504 /// augmented Lagrangian.
505 ///
506 /// Required for second-order solvers only.
508
509 /// @}
510
511 /// @name Combined evaluations
512 /// @{
513
514 /// **[Optional]**
515 /// Evaluate both @f$ f(x) @f$ and its gradient, @f$ \nabla f(x) @f$.
516 /// @default_impl ProblemVTable::default_eval_f_grad_f
517 real_t eval_f_grad_f(crvec x, rvec grad_fx) const;
518 /// **[Optional]**
519 /// Evaluate both @f$ f(x) @f$ and @f$ g(x) @f$.
520 /// @default_impl ProblemVTable::default_eval_f_g
522 /// **[Optional]**
523 /// Evaluate both @f$ \nabla f(x) @f$ and @f$ \nabla g(x)\,y @f$.
524 /// @default_impl ProblemVTable::default_eval_grad_f_grad_g_prod
525 void eval_grad_f_grad_g_prod(crvec x, crvec y, rvec grad_f, rvec grad_gxy) const;
526 /// **[Optional]**
527 /// Evaluate the gradient of the Lagrangian
528 /// @f$ \nabla_x L(x, y) = \nabla f(x) + \nabla g(x)\,y @f$
529 /// @default_impl ProblemVTable::default_eval_grad_L
530 void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const;
531
532 /// @}
533
534 /// @name Augmented Lagrangian
535 /// @{
536
537 /// **[Optional]**
538 /// Calculate both ψ(x) and the vector ŷ that can later be used to compute
539 /// ∇ψ.
540 /// @f[ \psi(x) = f(x) + \tfrac{1}{2}
541 /// \text{dist}_\Sigma^2\left(g(x) + \Sigma^{-1}y,\;D\right) @f]
542 /// @f[ \hat y = \Sigma\, \left(g(x) + \Sigma^{-1}y - \Pi_D\left(g(x)
543 /// + \Sigma^{-1}y\right)\right) @f]
544 /// @default_impl ProblemVTable::default_eval_ψ
545 real_t eval_ψ(crvec x, ///< [in] Decision variable @f$ x @f$
546 crvec y, ///< [in] Lagrange multipliers @f$ y @f$
547 crvec Σ, ///< [in] Penalty weights @f$ \Sigma @f$
548 rvec ŷ ///< [out] @f$ \hat y @f$
549 ) const;
550 /// **[Optional]**
551 /// Calculate the gradient ∇ψ(x).
552 /// @f[ \nabla \psi(x) = \nabla f(x) + \nabla g(x)\,\hat y(x) @f]
553 /// @default_impl ProblemVTable::default_eval_grad_ψ
554 void eval_grad_ψ(crvec x, ///< [in] Decision variable @f$ x @f$
555 crvec y, ///< [in] Lagrange multipliers @f$ y @f$
556 crvec Σ, ///< [in] Penalty weights @f$ \Sigma @f$
557 rvec grad_ψ, ///< [out] @f$ \nabla \psi(x) @f$
558 rvec work_n, ///< Dimension @f$ n @f$
559 rvec work_m ///< Dimension @f$ m @f$
560 ) const;
561 /// **[Optional]**
562 /// Calculate both ψ(x) and its gradient ∇ψ(x).
563 /// @f[ \psi(x) = f(x) + \tfrac{1}{2}
564 /// \text{dist}_\Sigma^2\left(g(x) + \Sigma^{-1}y,\;D\right) @f]
565 /// @f[ \nabla \psi(x) = \nabla f(x) + \nabla g(x)\,\hat y(x) @f]
566 /// @default_impl ProblemVTable::default_eval_ψ_grad_ψ
567 real_t eval_ψ_grad_ψ(crvec x, ///< [in] Decision variable @f$ x @f$
568 crvec y, ///< [in] Lagrange multipliers @f$ y @f$
569 crvec Σ, ///< [in] Penalty weights @f$ \Sigma @f$
570 rvec grad_ψ, ///< [out] @f$ \nabla \psi(x) @f$
571 rvec work_n, ///< Dimension @f$ n @f$
572 rvec work_m ///< Dimension @f$ m @f$
573 ) const;
574
575 /// @}
576
577 /// @name Checks
578 /// @{
579
580 /// **[Optional]**
581 /// Check that the problem formulation is well-defined, the dimensions match,
582 /// etc. Throws an exception if this is not the case.
583 void check() const;
584
585 /// @}
586
587 /// @name Querying specialized implementations
588 /// @{
589
590 /// Returns true if the problem provides an implementation of
591 /// @ref eval_inactive_indices_res_lna.
594 }
595 /// Returns true if the problem provides an implementation of
596 /// @ref eval_jac_g.
598 /// Returns true if the problem provides an implementation of
599 /// @ref get_jac_g_num_nonzeros.
602 }
603 /// Returns true if the problem provides an implementation of
604 /// @ref eval_grad_gi.
607 }
608 /// Returns true if the problem provides an implementation of
609 /// @ref eval_hess_L_prod.
612 }
613 /// Returns true if the problem provides an implementation of
614 /// @ref eval_hess_L.
616 /// Returns true if the problem provides an implementation of
617 /// @ref get_hess_L_num_nonzeros.
620 }
621 /// Returns true if the problem provides an implementation of
622 /// @ref eval_hess_ψ_prod.
625 }
626 /// Returns true if the problem provides an implementation of
627 /// @ref eval_hess_ψ.
629 /// Returns true if the problem provides an implementation of
630 /// @ref get_hess_ψ_num_nonzeros.
633 }
634 /// Returns true if the problem provides a specialized implementation of
635 /// @ref eval_f_grad_f, false if it uses the default implementation.
638 }
639 /// Returns true if the problem provides a specialized implementation of
640 /// @ref eval_f_g, false if it uses the default implementation.
642 /// Returns true if the problem provides a specialized implementation of
643 /// @ref eval_grad_f_grad_g_prod, false if it uses the default implementation.
646 }
647 /// Returns true if the problem provides a specialized implementation of
648 /// @ref eval_grad_L, false if it uses the default implementation.
650 /// Returns true if the problem provides a specialized implementation of
651 /// @ref eval_ψ, false if it uses the default implementation.
653 /// Returns true if the problem provides a specialized implementation of
654 /// @ref eval_grad_ψ, false if it uses the default implementation.
656 /// Returns true if the problem provides a specialized implementation of
657 /// @ref eval_ψ_grad_ψ, false if it uses the default implementation.
660 }
661 /// Returns true if the problem provides an implementation of
662 /// @ref get_box_C.
664 /// Returns true if the problem provides an implementation of
665 /// @ref get_box_D.
667 /// Returns true if the problem provides an implementation of @ref check.
668 bool provides_check() const { return vtable.check != vtable.default_check; }
669
670 /// @}
671
672 /// @name Helpers
673 /// @{
674
675 /// Given g(x), compute the intermediate results ŷ and dᵀŷ that can later be
676 /// used to compute ψ(x) and ∇ψ(x).
677 ///
678 /// Computes the result using the following algorithm:
679 /// @f[ \begin{aligned}
680 /// \zeta &= g(x) + \Sigma^{-1} y \\[]
681 /// d &= \zeta - \Pi_D(\zeta)
682 /// = \operatorname{eval\_proj\_diff\_g}(\zeta, \zeta) \\[]
683 /// \hat y &= \Sigma d \\[]
684 /// \end{aligned} @f]
685 /// @see @ref page_math
686 ///
687 /// @param[inout] g_ŷ
688 /// Input @f$ g(x) @f$, outputs @f$ \hat y @f$
689 /// @param[in] y
690 /// Lagrange multipliers @f$ y @f$
691 /// @param[in] Σ
692 /// Penalty weights @f$ \Sigma @f$
693 /// @return The inner product @f$ d^\top \hat y @f$
695
696 /// @}
697};
698
699/// @}
700
701#ifndef DOXYGEN
702template <class Tref>
703explicit TypeErasedProblem(Tref &&d)
705
706template <class Tref, class Allocator>
707explicit TypeErasedProblem(Tref &&d, Allocator alloc)
709#endif
710
711template <Config Conf, class Allocator>
713 return vtable.n;
714}
715template <Config Conf, class Allocator>
717 return vtable.m;
718}
719
720template <Config Conf, class Allocator>
722 return call(vtable.eval_proj_diff_g, z, e);
723}
724template <Config Conf, class Allocator>
726 return call(vtable.eval_proj_multipliers, y, M);
727}
728template <Config Conf, class Allocator>
730 rvec x̂, rvec p) const -> real_t {
731 return call(vtable.eval_prox_grad_step, γ, x, grad_ψ, x̂, p);
732}
733template <Config Conf, class Allocator>
735 crvec grad_ψ,
736 rindexvec J) const
737 -> index_t {
738 return call(vtable.eval_inactive_indices_res_lna, γ, x, grad_ψ, J);
739}
740template <Config Conf, class Allocator>
742 return call(vtable.eval_f, x);
743}
744template <Config Conf, class Allocator>
746 return call(vtable.eval_grad_f, x, grad_fx);
747}
748template <Config Conf, class Allocator>
750 return call(vtable.eval_g, x, gx);
751}
752template <Config Conf, class Allocator>
754 return call(vtable.eval_grad_g_prod, x, y, grad_gxy);
755}
756template <Config Conf, class Allocator>
758 return call(vtable.eval_grad_gi, x, i, grad_gi);
759}
760template <Config Conf, class Allocator>
762 rindexvec outer_ptr, rvec J_values) const {
763 return call(vtable.eval_jac_g, x, inner_idx, outer_ptr, J_values);
764}
765template <Config Conf, class Allocator>
767 return call(vtable.get_jac_g_num_nonzeros);
768}
769template <Config Conf, class Allocator>
771 rvec Hv) const {
772 return call(vtable.eval_hess_L_prod, x, y, scale, v, Hv);
773}
774template <Config Conf, class Allocator>
776 rindexvec inner_idx, rindexvec outer_ptr,
777 rvec H_values) const {
778 return call(vtable.eval_hess_L, x, y, scale, inner_idx, outer_ptr, H_values);
779}
780template <Config Conf, class Allocator>
782 return call(vtable.get_hess_L_num_nonzeros);
783}
784template <Config Conf, class Allocator>
786 crvec v, rvec Hv) const {
787 return call(vtable.eval_hess_ψ_prod, x, y, Σ, scale, v, Hv);
788}
789template <Config Conf, class Allocator>
791 rindexvec inner_idx, rindexvec outer_ptr,
792 rvec H_values) const {
793 return call(vtable.eval_hess_ψ, x, y, Σ, scale, inner_idx, outer_ptr, H_values);
794}
795template <Config Conf, class Allocator>
797 return call(vtable.get_hess_ψ_num_nonzeros);
798}
799template <Config Conf, class Allocator>
801 return call(vtable.eval_f_grad_f, x, grad_fx);
802}
803template <Config Conf, class Allocator>
805 return call(vtable.eval_f_g, x, g);
806}
807template <Config Conf, class Allocator>
809 rvec grad_gxy) const {
810 return call(vtable.eval_grad_f_grad_g_prod, x, y, grad_f, grad_gxy);
811}
812template <Config Conf, class Allocator>
814 rvec work_n) const {
815 return call(vtable.eval_grad_L, x, y, grad_L, work_n);
816}
817template <Config Conf, class Allocator>
819 return call(vtable.eval_ψ, x, y, Σ, ŷ);
820}
821template <Config Conf, class Allocator>
823 rvec work_n, rvec work_m) const {
824 return call(vtable.eval_grad_ψ, x, y, Σ, grad_ψ, work_n, work_m);
825}
826template <Config Conf, class Allocator>
828 rvec work_n, rvec work_m) const -> real_t {
829 return call(vtable.eval_ψ_grad_ψ, x, y, Σ, grad_ψ, work_n, work_m);
830}
831template <Config Conf, class Allocator>
833 return call(vtable.calc_ŷ_dᵀŷ, g_ŷ, y, Σ);
834}
835template <Config Conf, class Allocator>
837 return call(vtable.get_box_C);
838}
839template <Config Conf, class Allocator>
841 return call(vtable.get_box_D);
842}
843template <Config Conf, class Allocator>
845 return call(vtable.check);
846}
847
848/// @addtogroup grp_Problems
849/// @{
850
851template <Config Conf>
852void print_provided_functions(std::ostream &os, const TypeErasedProblem<Conf> &problem) {
853 os << "inactive_indices_res_lna: " << problem.provides_eval_inactive_indices_res_lna() << '\n'
854 << " grad_gi: " << problem.provides_eval_grad_gi() << '\n'
855 << " jac_g: " << problem.provides_eval_jac_g() << '\n'
856 << " hess_L_prod: " << problem.provides_eval_hess_L_prod() << '\n'
857 << " hess_L: " << problem.provides_eval_hess_L() << '\n'
858 << " hess_ψ_prod: " << problem.provides_eval_hess_ψ_prod() << '\n'
859 << " hess_ψ: " << problem.provides_eval_hess_ψ() << '\n'
860 << " f_grad_f: " << problem.provides_eval_f_grad_f() << '\n'
861 << " f_g: " << problem.provides_eval_f_g() << '\n'
862 << " grad_f_grad_g_prod: " << problem.provides_eval_grad_f_grad_g_prod() << '\n'
863 << " grad_L: " << problem.provides_eval_grad_L() << '\n'
864 << " ψ: " << problem.provides_eval_ψ() << '\n'
865 << " grad_ψ: " << problem.provides_eval_grad_ψ() << '\n'
866 << " ψ_grad_ψ: " << problem.provides_eval_ψ_grad_ψ() << '\n'
867 << " get_box_C: " << problem.provides_get_box_C() << '\n'
868 << " get_box_D: " << problem.provides_get_box_D() << '\n'
869 << " check: " << problem.provides_check() << '\n';
870}
871
872/// @}
873
874} // namespace alpaqa
The main polymorphic minimization problem interface.
bool provides_eval_hess_L() const
Returns true if the problem provides an implementation of eval_hess_L.
real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const
[Required] Function that computes a proximal gradient step.
real_t eval_ψ_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
[Optional] Calculate both ψ(x) and its gradient ∇ψ(x).
const Box & get_box_D() const
[Optional] Get the rectangular constraint set of the general constraint function, .
void eval_grad_gi(crvec x, index_t i, rvec grad_gi) const
[Optional] Function that evaluates the gradient of one specific constraint,
void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
[Optional] Function that evaluates the Hessian of the augmented Lagrangian,
bool provides_eval_hess_ψ_prod() const
Returns true if the problem provides an implementation of eval_hess_ψ_prod.
bool provides_eval_ψ_grad_ψ() const
Returns true if the problem provides a specialized implementation of eval_ψ_grad_ψ,...
bool provides_get_box_C() const
Returns true if the problem provides an implementation of get_box_C.
real_t eval_f_g(crvec x, rvec g) const
[Optional] Evaluate both and .
bool provides_get_hess_L_num_nonzeros() const
Returns true if the problem provides an implementation of get_hess_L_num_nonzeros.
bool provides_get_jac_g_num_nonzeros() const
Returns true if the problem provides an implementation of get_jac_g_num_nonzeros.
bool provides_eval_jac_g() const
Returns true if the problem provides an implementation of eval_jac_g.
bool provides_check() const
Returns true if the problem provides an implementation of check.
length_t get_n() const
[Required] Number of decision variables.
void check() const
[Optional] Check that the problem formulation is well-defined, the dimensions match,...
length_t get_m() const
[Required] Number of constraints.
real_t eval_ψ(crvec x, crvec y, crvec Σ, rvec ŷ) const
[Optional] Calculate both ψ(x) and the vector ŷ that can later be used to compute ∇ψ.
bool provides_eval_inactive_indices_res_lna() const
Returns true if the problem provides an implementation of eval_inactive_indices_res_lna.
void eval_grad_L(crvec x, crvec y, rvec grad_L, rvec work_n) const
[Optional] Evaluate the gradient of the Lagrangian
void eval_grad_f_grad_g_prod(crvec x, crvec y, rvec grad_f, rvec grad_gxy) const
[Optional] Evaluate both and .
bool provides_eval_grad_f_grad_g_prod() const
Returns true if the problem provides a specialized implementation of eval_grad_f_grad_g_prod,...
void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values) const
[Optional] Function that evaluates the Hessian of the Lagrangian as a sparse matrix,
static TypeErasedProblem make(Args &&...args)
index_t eval_inactive_indices_res_lna(real_t γ, crvec x, crvec grad_ψ, rindexvec J) const
[Optional] Function that computes the inactive indices for the evaluation of the linear Newton appro...
bool provides_eval_hess_L_prod() const
Returns true if the problem provides an implementation of eval_hess_L_prod.
bool provides_get_hess_ψ_num_nonzeros() const
Returns true if the problem provides an implementation of get_hess_ψ_num_nonzeros.
real_t eval_f_grad_f(crvec x, rvec grad_fx) const
[Optional] Evaluate both and its gradient, .
bool provides_eval_f_grad_f() const
Returns true if the problem provides a specialized implementation of eval_f_grad_f,...
void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const
[Required] Function that evaluates the gradient of the constraints times a vector,
void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const
[Optional] Function that evaluates the Hessian of the Lagrangian multiplied by a vector,
bool provides_eval_grad_gi() const
Returns true if the problem provides an implementation of eval_grad_gi.
void eval_proj_multipliers(rvec y, real_t M) const
[Required] Function that projects the Lagrange multipliers for ALM.
bool provides_eval_f_g() const
Returns true if the problem provides a specialized implementation of eval_f_g, false if it uses the d...
void eval_grad_f(crvec x, rvec grad_fx) const
[Required] Function that evaluates the gradient of the cost,
real_t eval_f(crvec x) const
[Required] Function that evaluates the cost,
bool provides_eval_grad_L() const
Returns true if the problem provides a specialized implementation of eval_grad_L, false if it uses th...
bool provides_eval_grad_ψ() const
Returns true if the problem provides a specialized implementation of eval_grad_ψ, false if it uses th...
void eval_g(crvec x, rvec gx) const
[Required] Function that evaluates the constraints,
length_t get_jac_g_num_nonzeros() const
[Optional] Function that gets the number of nonzeros of the sparse Jacobian of the constraints.
bool provides_eval_hess_ψ() const
Returns true if the problem provides an implementation of eval_hess_ψ.
real_t calc_ŷ_dᵀŷ(rvec g_ŷ, crvec y, crvec Σ) const
Given g(x), compute the intermediate results ŷ and dᵀŷ that can later be used to compute ψ(x) and ∇ψ(...
bool provides_get_box_D() const
Returns true if the problem provides an implementation of get_box_D.
const Box & get_box_C() const
[Optional] Get the rectangular constraint set of the decision variables, .
void eval_proj_diff_g(crvec z, rvec e) const
[Required] Function that evaluates the difference between the given point and its projection onto th...
void eval_jac_g(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values) const
[Optional] Function that evaluates the Jacobian of the constraints as a sparse matrix,
void eval_grad_ψ(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m) const
[Optional] Calculate the gradient ∇ψ(x).
bool provides_eval_ψ() const
Returns true if the problem provides a specialized implementation of eval_ψ, false if it uses the def...
void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const
[Optional] Function that evaluates the Hessian of the augmented Lagrangian multiplied by a vector,
length_t get_hess_ψ_num_nonzeros() const
[Optional] Function that gets the number of nonzeros of the Hessian of the augmented Lagrangian.
length_t get_hess_L_num_nonzeros() const
[Optional] Function that gets the number of nonzeros of the sparse Hessian of the Lagrangian.
Class for polymorphism through type erasure.
decltype(auto) call(Ret(*f)(const void *, FArgs...), Args &&...args) const
Call the vtable function f with the given arguments args, implicitly passing the self pointer and vta...
TypeErased() noexcept(noexcept(allocator_type()))=default
Default constructor.
void * self
Pointer to the stored object.
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:42
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
Definition: export.hpp:21
void print_provided_functions(std::ostream &os, const TypeErasedProblem< Conf > &problem)
typename Conf::real_t real_t
Definition: config.hpp:51
typename Conf::rindexvec rindexvec
Definition: config.hpp:65
typename Conf::index_t index_t
Definition: config.hpp:63
typename Conf::length_t length_t
Definition: config.hpp:62
typename Conf::rvec rvec
Definition: config.hpp:55
typename Conf::crvec crvec
Definition: config.hpp:56
#define ALPAQA_TE_OPTIONAL_METHOD(vtable, type, member, instance)
#define ALPAQA_TE_REQUIRED_METHOD(vtable, type, member)
Double-precision double configuration.
Definition: config.hpp:115
Single-precision float configuration.
Definition: config.hpp:111
long double configuration.
Definition: config.hpp:120
Struct containing function pointers to all problem functions (like the objective and constraint funct...
required_const_function_t< real_t(crvec x)> eval_f
util::BasicVTable::optional_const_function_t< F, ProblemVTable > optional_const_function_t
optional_const_function_t< void(crvec x, crvec y, real_t scale, crvec v, rvec Hv)> eval_hess_L_prod
required_const_function_t< real_t(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p)> eval_prox_grad_step
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 &)
optional_const_function_t< void(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m)> eval_grad_ψ
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)
optional_const_function_t< real_t(crvec x, crvec y, crvec Σ, rvec ŷ)> eval_ψ
optional_const_function_t< index_t(real_t γ, crvec x, crvec grad_ψ, rindexvec J)> eval_inactive_indices_res_lna
ProblemVTable(std::in_place_t, P &p)
optional_const_function_t< void(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv)> eval_hess_ψ_prod
static void default_eval_jac_g(const void *, crvec, rindexvec, rindexvec, rvec, const ProblemVTable &)
optional_const_function_t< length_t()> get_hess_ψ_num_nonzeros
optional_const_function_t< void()> check
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< real_t(crvec x, crvec y, crvec Σ, rvec grad_ψ, rvec work_n, rvec work_m)> eval_ψ_grad_ψ
util::BasicVTable::optional_function_t< F, ProblemVTable > optional_function_t
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< length_t()> get_jac_g_num_nonzeros
optional_const_function_t< void(crvec x, crvec y, crvec Σ, real_t scale, rindexvec inner_idx, rindexvec outer_ptr, rvec H_values)> eval_hess_ψ
optional_const_function_t< const Box &()> get_box_D
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
required_const_function_t< void(crvec z, rvec e)> eval_proj_diff_g
static real_t default_eval_f_g(const void *self, crvec x, rvec g, const ProblemVTable &vtable)
optional_const_function_t< void(crvec x, index_t i, rvec grad_gi)> eval_grad_gi
optional_const_function_t< length_t()> get_hess_L_num_nonzeros
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(rvec y, real_t M)> eval_proj_multipliers
required_const_function_t< void(crvec x, rvec grad_fx)> eval_grad_f
optional_const_function_t< void(crvec x, rindexvec inner_idx, rindexvec outer_ptr, rvec J_values)> eval_jac_g
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)
optional_const_function_t< const Box &()> get_box_C
Struct that stores the size of a polymorphic object, as well as pointers to functions to copy,...
typename optional_function< F, VTable >::type optional_function_t
An optional function includes a void pointer to self, the arguments of F, and an additional reference...
typename required_const_function< F >::type required_const_function_t
A required function includes a void pointer to self, in addition to the arguments of F.
typename optional_const_function< F, VTable >::type optional_const_function_t
An optional function includes a void pointer to self, the arguments of F, and an additional reference...