alpaqa 1.0.0a18
Nonconvex constrained optimization
Loading...
Searching...
No Matches
prox.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace alpaqa {
7
8/// Proximal mapping customization point.
9/// @see https://wg21.link/P1895R0
10struct prox_fn {
11 template <class T>
12 requires requires {
13 // The proximable function type T should define a valid
14 // configuration typedef.
15 typename T::config_t;
17 // The proximable function type T should opt in to the prox_fn
18 // tag to provide a custom implementation for the proximal operator.
19 requires alpaqa::tag_invocable<
20 prox_fn, T &, typename T::config_t::crmat,
21 typename T::config_t::rmat, typename T::config_t::real_t>;
22 // The return type of that proximal operator should be real_t.
23 requires std::is_same_v<
24 tag_invoke_result_t<prox_fn, T &, typename T::config_t::crmat,
25 typename T::config_t::rmat,
26 typename T::config_t::real_t>,
27 typename T::config_t::real_t>;
28 }
29 auto operator()(T &func, typename T::config_t::crmat in,
30 typename T::config_t::rmat out,
31 typename T::config_t::real_t γ = 1) const
33 prox_fn, T &, typename T::config_t::crmat,
34 typename T::config_t::rmat, typename T::config_t::real_t>) ->
35 typename T::config_t::real_t {
36 return alpaqa::alpaqa_tag_invoke(*this, func, std::move(in),
37 std::move(out), γ);
38 }
39}
40/**
41 * Compute the proximal mapping.
42 * @f[ \begin{aligned}
43 * \mathrm{out} &\leftarrow \prox_{\gamma\, \mathrm{func}}
44 * \left( \mathrm{in} \right).
45 * \end{aligned}
46 * @f]
47 * @param func
48 * The proximable function @f$ h : \Rn \to \Rn @f$ to apply the
49 * proximal mapping of.
50 * @param[in] in
51 * Input vector or matrix @f$ x @f$, e.g. current iterate.
52 * @param[out] out
53 * Proximal mapping of @f$ (\gamma\, h) @f$ at @f$ x @f$.
54 * @f$ \hat x \leftarrow \prox_{\gamma\, h}\left( x \right) @f$
55 * @param[in] γ
56 * Proximal step size @f$ \gamma @f$.
57 * @return The value of the function evaluated in the output,
58 * @f$ h(\hat x) @f$.
59 * @ingroup grp_Functions
60 */
61inline constexpr prox;
62
63/// Proximal mapping customization point for forward-backward steps.
64/// @see https://wg21.link/P1895R0
66 template <class T>
67 requires requires {
68 // The proximable function type T should define a valid
69 // configuration typedef.
70 typename T::config_t;
72 // The proximable function type T should opt in to the prox_step_fn
73 // tag to provide a custom implementation for the proximal operator.
74 requires alpaqa::tag_invocable<
75 prox_step_fn, T &, typename T::config_t::crmat,
76 typename T::config_t::crmat, typename T::config_t::rmat,
77 typename T::config_t::rmat, typename T::config_t::real_t,
78 typename T::config_t::real_t>;
79 // The return type of that proximal operator should be real_t.
80 requires std::is_same_v<
82 prox_step_fn, T &, typename T::config_t::crmat,
83 typename T::config_t::crmat, typename T::config_t::rmat,
84 typename T::config_t::rmat, typename T::config_t::real_t,
85 typename T::config_t::real_t>,
86 typename T::config_t::real_t>;
87 }
88 auto operator()(T &func, typename T::config_t::crmat in,
89 typename T::config_t::crmat fwd_step,
90 typename T::config_t::rmat out,
91 typename T::config_t::rmat fb_step,
92 typename T::config_t::real_t γ = 1,
93 typename T::config_t::real_t γ_fwd = -1) const
95 prox_step_fn, T &, typename T::config_t::crmat,
96 typename T::config_t::crmat, typename T::config_t::rmat,
97 typename T::config_t::rmat, typename T::config_t::real_t,
98 typename T::config_t::real_t>) ->
99 typename T::config_t::real_t {
100 return alpaqa::alpaqa_tag_invoke(*this, func, std::move(in),
101 std::move(fwd_step), std::move(out),
102 std::move(fb_step), γ, γ_fwd);
103 }
104
105 /// Default implementation for prox_step if only prox is provided.
106 template <class T>
107 requires requires {
108 typename T::config_t;
110 // Only enable if no implementation exists,
111 requires !alpaqa::tag_invocable<
112 prox_step_fn, T &, typename T::config_t::crmat,
113 typename T::config_t::crmat, typename T::config_t::rmat,
114 typename T::config_t::rmat, typename T::config_t::real_t,
115 typename T::config_t::real_t>;
116 // and only enable if prox is provided.
117 requires std::invocable<prox_fn, T &, typename T::config_t::crmat,
118 typename T::config_t::rmat,
119 typename T::config_t::real_t>;
120 }
121 auto operator()(T &func, typename T::config_t::crmat in,
122 typename T::config_t::crmat fwd_step,
123 typename T::config_t::rmat out,
124 typename T::config_t::rmat fb_step,
125 typename T::config_t::real_t γ = 1,
126 typename T::config_t::real_t γ_fwd = -1) const
128 prox_fn, T &, typename T::config_t::crmat,
129 typename T::config_t::rmat, typename T::config_t::real_t>) ->
130 typename T::config_t::real_t {
132 auto &&h_out = prox(func, fb_step, out, γ);
133 fb_step = out - in;
134 return h_out;
135 }
136}
137/**
138 * Compute a generalized forward-backward step
139 * @f[ \begin{aligned}
140 * \mathrm{out} &\leftarrow \prox_{\gamma\, \mathrm{func}}
141 * \left( \mathrm{in} + \gamma_\mathrm{fwd}\, \mathrm{fwd\_step} \right) \\
142 * \mathrm{fb\_step} &\leftarrow \mathrm{out} - \mathrm{in}.
143 * \end{aligned}
144 * @f]
145 * @param func
146 * The proximable function @f$ h : \Rn \to \Rn @f$ to apply the
147 * proximal mapping of.
148 * @param[in] in
149 * Input vector or matrix @f$ x @f$, e.g. current iterate.
150 * @param[in] fwd_step
151 * Step @f$ d @f$ to add to @f$ x @f$ before computing the
152 * proximal mapping. Scaled by @f$ \gamma_\text{fwd} @f$.
153 * @param[out] out
154 * Proximal mapping of @f$ (\gamma\, h) @f$ at
155 * @f$ x + \gamma_\text{fwd}\, d @f$.
156 * @f$ \hat x \leftarrow \prox_{\gamma\, h}\left(
157 * x + \gamma_\text{fwd}\, d \right) @f$
158 * @param[out] fb_step
159 * Forward-backward step @f$ p @f$.
160 * @f$ p = \hat x - \hat x @f$
161 * @param[in] γ
162 * Proximal step size @f$ \gamma @f$.
163 * @param[in] γ_fwd
164 * Forward step size @f$ \gamma_\mathrm{fwd} @f$.
165 * @return The value of the function evaluated in the output,
166 * @f$ h(\hat x) @f$.
167 * @ingroup grp_Functions
168 *
169 * This function can be used to implement the @ref TypeErasedProblem::eval_prox_grad_step function:
170 *
171 * ```cpp
172 * struct Problem {
173 * alpaqa::functions::NuclearNorm<config_t> h{λ, rows, cols};
174 * real_t eval_prox_grad_step(real_t γ, crvec x, crvec grad_ψ, rvec x̂, rvec p) const {
175 * return alpaqa::prox_step(h, x, grad_ψ, x̂, p, γ, -γ);
176 * }
177 * };
178 * ```
179 * Note the negative sign for the forward step size.
180 */
181inline constexpr prox_step;
182
183} // namespace alpaqa
struct alpaqa::prox_fn prox
Compute the proximal mapping.
struct alpaqa::prox_step_fn prox_step
Compute a generalized forward-backward step.
constexpr tag_invoke_fn_ns::tag_invoke_fn alpaqa_tag_invoke
typename Conf::crmat crmat
Definition config.hpp:97
typename Conf::rmat rmat
Definition config.hpp:96
std::invoke_result_t< decltype(::alpaqa::alpaqa_tag_invoke), Tag, Args... > tag_invoke_result_t
typename Conf::real_t real_t
Definition config.hpp:86
constexpr const auto inf
Definition config.hpp:112
constexpr bool is_nothrow_tag_invocable_v
Proximal mapping customization point.
Definition prox.hpp:10
auto operator()(T &func, typename T::config_t::crmat in, typename T::config_t::rmat out, typename T::config_t::real_t γ=1) const noexcept(alpaqa::is_nothrow_tag_invocable_v< prox_fn, T &, typename T::config_t::crmat, typename T::config_t::rmat, typename T::config_t::real_t >) -> typename T::config_t::real_t
Definition prox.hpp:29
Proximal mapping customization point for forward-backward steps.
Definition prox.hpp:65
auto operator()(T &func, typename T::config_t::crmat in, typename T::config_t::crmat fwd_step, typename T::config_t::rmat out, typename T::config_t::rmat fb_step, typename T::config_t::real_t γ=1, typename T::config_t::real_t γ_fwd=-1) const noexcept(std::is_nothrow_invocable_v< prox_fn, T &, typename T::config_t::crmat, typename T::config_t::rmat, typename T::config_t::real_t >) -> typename T::config_t::real_t
Default implementation for prox_step if only prox is provided.
Definition prox.hpp:121
auto operator()(T &func, typename T::config_t::crmat in, typename T::config_t::crmat fwd_step, typename T::config_t::rmat out, typename T::config_t::rmat fb_step, typename T::config_t::real_t γ=1, typename T::config_t::real_t γ_fwd=-1) const noexcept(alpaqa::is_nothrow_tag_invocable_v< prox_step_fn, T &, typename T::config_t::crmat, typename T::config_t::crmat, typename T::config_t::rmat, typename T::config_t::rmat, typename T::config_t::real_t, typename T::config_t::real_t >) -> typename T::config_t::real_t
Definition prox.hpp:88