alpaqa 1.0.0a9
Nonconvex constrained optimization
Loading...
Searching...
No Matches
functional-problem.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace alpaqa {
7
8/// Problem class that allows specifying the basic functions as C++
9/// `std::function`s.
10/// @ingroup grp_Problems
11template <Config Conf = DefaultConfig>
12class FunctionalProblem : public BoxConstrProblem<Conf> {
13 public:
15 using BoxConstrProblem<Conf>::BoxConstrProblem;
16
17 std::function<real_t(crvec)> f;
18 std::function<void(crvec, rvec)> grad_f;
19 std::function<void(crvec, rvec)> g;
20 std::function<void(crvec, crvec, rvec)> grad_g_prod;
21 std::function<void(crvec, index_t, rvec)> grad_gi;
22 std::function<void(crvec, rmat)> jac_g;
23 std::function<void(crvec, crvec, real_t, crvec, rvec)> hess_L_prod;
24 std::function<void(crvec, crvec, real_t, rmat)> hess_L;
25 std::function<void(crvec, crvec, crvec, real_t, crvec, rvec)> hess_ψ_prod;
26 std::function<void(crvec, crvec, crvec, real_t, rmat)> hess_ψ;
27
28 // clang-format off
29 real_t eval_f(crvec x) const { ScopedMallocAllower ma; return f(x); }
30 void eval_grad_f(crvec x, rvec grad_fx) const { ScopedMallocAllower ma; grad_f(x, grad_fx); }
31 void eval_g(crvec x, rvec gx) const { ScopedMallocAllower ma; g(x, gx); }
32 void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const { ScopedMallocAllower ma; grad_g_prod(x, y, grad_gxy); }
33 void eval_grad_gi(crvec x, index_t i, rvec grad_gix) const { ScopedMallocAllower ma; grad_gi(x, i, grad_gix); }
34 void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const { ScopedMallocAllower ma; hess_L_prod(x, y, scale, v, Hv); }
35 void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const { ScopedMallocAllower ma; hess_ψ_prod(x, y, Σ, scale, v, Hv); }
36 // clang-format on
37 void eval_jac_g(crvec x, rindexvec, rindexvec, rvec J_values) const {
39 if (J_values.size() > 0)
40 jac_g(x, J_values.reshaped(this->m, this->n));
41 }
42 void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec, rindexvec, rvec H_values) const {
44 if (H_values.size() > 0)
45 hess_L(x, y, scale, H_values.reshaped(this->n, this->n));
46 }
48 rvec H_values) const {
50 if (H_values.size() > 0)
51 hess_ψ(x, y, Σ, scale, H_values.reshaped(this->n, this->n));
52 }
53
54 /// @see @ref TypeErasedProblem::provides_eval_grad_gi
55 [[nodiscard]] bool provides_eval_grad_gi() const { return bool{grad_gi}; }
56 /// @see @ref TypeErasedProblem::provides_eval_jac_g
57 [[nodiscard]] bool provides_eval_jac_g() const { return bool{jac_g}; }
58 /// @see @ref TypeErasedProblem::provides_eval_hess_L_prod
59 [[nodiscard]] bool provides_eval_hess_L_prod() const { return bool{hess_L_prod}; }
60 /// @see @ref TypeErasedProblem::provides_eval_hess_L
61 [[nodiscard]] bool provides_eval_hess_L() const { return bool{hess_L}; }
62 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ_prod
63 [[nodiscard]] bool provides_eval_hess_ψ_prod() const { return bool{hess_ψ_prod}; }
64 /// @see @ref TypeErasedProblem::provides_eval_hess_ψ
65 [[nodiscard]] bool provides_eval_hess_ψ() const { return bool{hess_ψ}; }
66
69 FunctionalProblem(FunctionalProblem &&) noexcept = default;
70 FunctionalProblem &operator=(FunctionalProblem &&) noexcept = default;
71};
72
73} // namespace alpaqa
Implements common problem functions for minimization problems with box constraints.
Problem class that allows specifying the basic functions as C++ std::functions.
std::function< void(crvec, crvec, real_t, rmat)> hess_L
std::function< real_t(crvec)> f
std::function< void(crvec, crvec, real_t, crvec, rvec)> hess_L_prod
std::function< void(crvec, index_t, rvec)> grad_gi
std::function< void(crvec, crvec, crvec, real_t, crvec, rvec)> hess_ψ_prod
std::function< void(crvec, crvec, crvec, real_t, rmat)> hess_ψ
std::function< void(crvec, crvec, rvec)> grad_g_prod
void eval_hess_ψ(crvec x, crvec y, crvec Σ, real_t scale, rindexvec, rindexvec, rvec H_values) const
std::function< void(crvec, rvec)> g
FunctionalProblem & operator=(const FunctionalProblem &)=default
std::function< void(crvec, rmat)> jac_g
void eval_jac_g(crvec x, rindexvec, rindexvec, rvec J_values) const
void eval_hess_L(crvec x, crvec y, real_t scale, rindexvec, rindexvec, rvec H_values) const
std::function< void(crvec, rvec)> grad_f
FunctionalProblem(const FunctionalProblem &)=default
void eval_grad_g_prod(crvec x, crvec y, rvec grad_gxy) const
void eval_hess_L_prod(crvec x, crvec y, real_t scale, crvec v, rvec Hv) const
void eval_grad_f(crvec x, rvec grad_fx) const
FunctionalProblem(FunctionalProblem &&) noexcept=default
void eval_g(crvec x, rvec gx) const
real_t eval_f(crvec x) const
void eval_grad_gi(crvec x, index_t i, rvec grad_gix) const
void eval_hess_ψ_prod(crvec x, crvec y, crvec Σ, real_t scale, crvec v, rvec Hv) const
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:54
typename Conf::rmat rmat
Definition: config.hpp:72
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::rvec rvec
Definition: config.hpp:67
typename Conf::crvec crvec
Definition: config.hpp:68