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