alpaqa 1.0.0a16
Nonconvex constrained optimization
Loading...
Searching...
No Matches
panoc.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <alpaqa/export.hpp>
12
13#include <chrono>
14#include <iostream>
15#include <limits>
16#include <stdexcept>
17#include <string>
18#include <type_traits>
19
20namespace alpaqa {
21
22/// Tuning parameters for the PANOC algorithm.
23/// @ingroup grp_Parameters
24template <Config Conf = DefaultConfig>
27
28 /// Parameters related to the Lipschitz constant estimate and step size.
30 /// Maximum number of inner PANOC iterations.
31 unsigned max_iter = 100;
32 /// Maximum duration.
33 std::chrono::nanoseconds max_time = std::chrono::minutes(5);
34 /// Minimum weight factor between Newton step and projected gradient step.
36 /// Ignore the line search condition and always accept the accelerated step.
37 /// (For testing purposes only).
38 bool force_linesearch = false;
39 /// Parameter β used in the line search (see Algorithm 2 in
40 /// @cite de_marchi_proximal_2022). @f$ 0 < \beta < 1 @f$
42 /// Minimum Lipschitz constant estimate.
44 /// Maximum Lipschitz constant estimate.
46 /// What stopping criterion to use.
48 /// Maximum number of iterations without any progress before giving up.
49 unsigned max_no_progress = 10;
50
51 /// When to print progress. If set to zero, nothing will be printed.
52 /// If set to N != 0, progress is printed every N iterations.
53 unsigned print_interval = 0;
54 /// The precision of the floating point values printed by the solver.
55 int print_precision = std::numeric_limits<real_t>::max_digits10 / 2;
56
57 /// Tolerance factor used in the quadratic upper bound condition that
58 /// determines the step size. Its goal is to account for numerical errors
59 /// in the function and gradient evaluations. If you notice that the step
60 /// size γ becomes very small, you may want to increase this factor.
62 10 * std::numeric_limits<real_t>::epsilon();
63 /// Tolerance factor used in the line search. Its goal is to account for
64 /// numerical errors in the function and gradient evaluations. If you notice
65 /// that accelerated steps are rejected (τ = 0) when getting closer to the
66 /// solution, you may want to increase this factor.
68 10 * std::numeric_limits<real_t>::epsilon();
69
70 /// Use the candidate point rather than the accepted point to update the
71 /// quasi-Newton direction.
73 /// If the step size changes, the direction buffer is flushed. The current
74 /// step will still be used to update the direction, but may still use the
75 /// old step size. If set to true, the current step will be recomputed with
76 /// the new step size as well, to match the step in the candidate iterate.
78 /// When evaluating ψ(x̂) in a candidate point, always evaluate ∇ψ(x̂) as
79 /// well. Can be beneficial if computing ∇ψ(x̂) is not much more expensive
80 /// than computing just ψ(x), and if ∇ψ(x̂) is required in the next iteration
81 /// (e.g. for the stopping criterion, or when using the NoopDirection).
82 bool eager_gradient_eval = false;
83};
84
85template <Config Conf = DefaultConfig>
86struct PANOCStats {
88
91 std::chrono::nanoseconds elapsed_time{};
92 std::chrono::nanoseconds time_progress_callback{};
93 unsigned iterations = 0;
94 unsigned linesearch_failures = 0;
96 unsigned stepsize_backtracks = 0;
97 unsigned lbfgs_failures = 0;
98 unsigned lbfgs_rejected = 0;
99 unsigned τ_1_accepted = 0;
100 unsigned count_τ = 0;
106};
107
108template <Config Conf = DefaultConfig>
135
136/// PANOC solver for ALM.
137/// @ingroup grp_InnerSolvers
138template <class DirectionT>
140 public:
141 USING_ALPAQA_CONFIG_TEMPLATE(DirectionT::config_t);
142
149
151 requires std::default_initializable<Direction>
152 : params(params) {}
157
158 Stats operator()(const Problem &problem, // in
159 const SolveOptions &opts, // in
160 rvec x, // inout
161 rvec y, // inout
162 crvec Σ, // in
163 rvec err_z); // out
164
165 template <class P>
166 Stats operator()(const P &problem, const SolveOptions &opts, rvec x, rvec y,
167 crvec Σ, rvec e) {
168 return operator()(Problem{&problem}, opts, x, y, Σ, e);
169 }
170
171 template <class P>
172 Stats operator()(const P &problem, const SolveOptions &opts, rvec x) {
173 if (problem.get_m() != 0)
174 throw std::invalid_argument("Missing arguments y, Σ, e");
175 mvec y{nullptr, 0}, Σ{nullptr, 0}, e{nullptr, 0};
176 return operator()(problem, opts, x, y, Σ, e);
177 }
178
179 /// Specify a callable that is invoked with some intermediate results on
180 /// each iteration of the algorithm.
181 /// @see @ref ProgressInfo
183 set_progress_callback(std::function<void(const ProgressInfo &)> cb) {
184 this->progress_cb = cb;
185 return *this;
186 }
187
188 std::string get_name() const;
189
190 void stop() { stop_signal.stop(); }
191
192 const Params &get_params() const { return params; }
193
194 private:
197 std::function<void(const ProgressInfo &)> progress_cb;
199
200 public:
202 std::ostream *os = &std::cout;
203};
204
205template <class InnerSolverStats>
207
208template <Config Conf>
211
212 /// Total elapsed time in the inner solver.
213 std::chrono::nanoseconds elapsed_time{};
214 /// Total time spent in the user-provided progress callback.
215 std::chrono::nanoseconds time_progress_callback{};
216 /// Total number of inner PANOC iterations.
217 unsigned iterations = 0;
218 /// Total number of PANOC line search failures.
219 unsigned linesearch_failures = 0;
220 /// Total number of PANOC line search backtracking steps.
221 unsigned linesearch_backtracks = 0;
222 /// Total number of PANOC step size reductions.
223 unsigned stepsize_backtracks = 0;
224 /// Total number of times that the L-BFGS direction was not finite.
225 unsigned lbfgs_failures = 0;
226 /// Total number of times that the L-BFGS update was rejected (i.e. it
227 /// could have resulted in a non-positive definite Hessian estimate).
228 unsigned lbfgs_rejected = 0;
229 /// Total number of times that a line search parameter of @f$ \tau = 1 @f$
230 /// was accepted (i.e. no backtracking necessary).
231 unsigned τ_1_accepted = 0;
232 /// The total number of line searches performed (used for computing the
233 /// average value of @f$ \tau @f$).
234 unsigned count_τ = 0;
235 /// The sum of the line search parameter @f$ \tau @f$ in all iterations
236 /// (used for computing the average value of @f$ \tau @f$).
237 real_t sum_τ = 0;
238 /// The final PANOC step size γ.
239 real_t final_γ = 0;
240 /// Final value of the smooth cost @f$ \psi(\hat x) @f$.
241 real_t final_ψ = 0;
242 /// Final value of the nonsmooth cost @f$ h(\hat x) @f$.
243 real_t final_h = 0;
244 /// Final value of the forward-backward envelope, @f$ \varphi_\gamma(x) @f$
245 /// (note that this is in the point @f$ x @f$, not @f$ \hat x @f$).
246 real_t final_φγ = 0;
247};
248
249template <Config Conf>
252 const PANOCStats<Conf> &s) {
254 acc.elapsed_time += s.elapsed_time;
255 acc.time_progress_callback += s.time_progress_callback;
256 acc.linesearch_failures += s.linesearch_failures;
257 acc.linesearch_backtracks += s.linesearch_backtracks;
258 acc.stepsize_backtracks += s.stepsize_backtracks;
259 acc.lbfgs_failures += s.lbfgs_failures;
260 acc.lbfgs_rejected += s.lbfgs_rejected;
261 acc.τ_1_accepted += s.τ_1_accepted;
262 acc.count_τ += s.count_τ;
263 acc.sum_τ += s.sum_τ;
264 acc.final_γ = s.final_γ;
265 acc.final_ψ = s.final_ψ;
266 acc.final_h = s.final_h;
267 acc.final_φγ = s.final_φγ;
268 return acc;
269}
270
271// clang-format off
272ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCParams, EigenConfigd);
273ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCParams, EigenConfigf);)
274ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCParams, EigenConfigl);)
276
277ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCStats, EigenConfigd);
278ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCStats, EigenConfigf);)
279ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCStats, EigenConfigl);)
281
282ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCProgressInfo, EigenConfigd);
283ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCProgressInfo, EigenConfigf);)
284ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, PANOCProgressInfo, EigenConfigl);)
286// clang-format on
287
288} // namespace alpaqa
PANOC solver for ALM.
Definition panoc.hpp:139
std::string get_name() const
Definition panoc.tpp:20
PANOCSolver(const Params &params, Direction &&direction)
Definition panoc.hpp:153
PANOCSolver(const Params &params)
Definition panoc.hpp:150
Stats operator()(const P &problem, const SolveOptions &opts, rvec x, rvec y, crvec Σ, rvec e)
Definition panoc.hpp:166
PANOCStats< config_t > Stats
Definition panoc.hpp:146
std::function< void(const ProgressInfo &)> progress_cb
Definition panoc.hpp:197
Stats operator()(const Problem &problem, const SolveOptions &opts, rvec x, rvec y, crvec Σ, rvec err_z)
Definition panoc.tpp:25
PANOCSolver & set_progress_callback(std::function< void(const ProgressInfo &)> cb)
Specify a callable that is invoked with some intermediate results on each iteration of the algorithm.
Definition panoc.hpp:183
Direction direction
Definition panoc.hpp:201
InnerSolveOptions< config_t > SolveOptions
Definition panoc.hpp:148
PANOCSolver(const Params &params, const Direction &direction)
Definition panoc.hpp:155
AtomicStopSignal stop_signal
Definition panoc.hpp:196
DirectionT Direction
Definition panoc.hpp:145
const Params & get_params() const
Definition panoc.hpp:192
Stats operator()(const P &problem, const SolveOptions &opts, rvec x)
Definition panoc.hpp:172
TypeErasedProblem< config_t > Problem
Definition panoc.hpp:143
std::ostream * os
Definition panoc.hpp:202
The main polymorphic minimization problem interface.
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:63
#define ALPAQA_IF_QUADF(...)
Definition config.hpp:207
#define ALPAQA_IF_LONGD(...)
Definition config.hpp:219
#define ALPAQA_IF_FLOAT(...)
Definition config.hpp:213
#define USING_ALPAQA_CONFIG_TEMPLATE(Conf)
Definition config.hpp:67
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
Definition export.hpp:21
real_t linesearch_tolerance_factor
Tolerance factor used in the line search.
Definition panoc.hpp:67
LipschitzEstimateParams< config_t > Lipschitz
Parameters related to the Lipschitz constant estimate and step size.
Definition panoc.hpp:29
unsigned max_no_progress
Maximum number of iterations without any progress before giving up.
Definition panoc.hpp:49
real_t L_max
Maximum Lipschitz constant estimate.
Definition panoc.hpp:45
bool recompute_last_prox_step_after_stepsize_change
If the step size changes, the direction buffer is flushed.
Definition panoc.hpp:77
real_t min_linesearch_coefficient
Minimum weight factor between Newton step and projected gradient step.
Definition panoc.hpp:35
std::chrono::nanoseconds max_time
Maximum duration.
Definition panoc.hpp:33
real_t quadratic_upperbound_tolerance_factor
Tolerance factor used in the quadratic upper bound condition that determines the step size.
Definition panoc.hpp:61
bool force_linesearch
Ignore the line search condition and always accept the accelerated step.
Definition panoc.hpp:38
real_t linesearch_strictness_factor
Parameter β used in the line search (see Algorithm 2 in ).
Definition panoc.hpp:41
unsigned print_interval
When to print progress.
Definition panoc.hpp:53
int print_precision
The precision of the floating point values printed by the solver.
Definition panoc.hpp:55
real_t L_min
Minimum Lipschitz constant estimate.
Definition panoc.hpp:43
bool update_direction_in_candidate
Use the candidate point rather than the accepted point to update the quasi-Newton direction.
Definition panoc.hpp:72
bool eager_gradient_eval
When evaluating ψ(x̂) in a candidate point, always evaluate ∇ψ(x̂) as well.
Definition panoc.hpp:82
unsigned max_iter
Maximum number of inner PANOC iterations.
Definition panoc.hpp:31
PANOCStopCrit stop_crit
What stopping criterion to use.
Definition panoc.hpp:47
Parameters for the estimation of the Lipschitz constant of the gradient of the smooth term of the cos...
Definition lipschitz.hpp:12
Tuning parameters for the PANOC algorithm.
Definition panoc.hpp:25
unsigned stepsize_backtracks
Definition panoc.hpp:96
unsigned lbfgs_rejected
Definition panoc.hpp:98
typename Conf::mvec mvec
Definition config.hpp:75
@ ApproxKKT
Find an ε-approximate KKT point in the ∞-norm:
unsigned τ_1_accepted
Definition panoc.hpp:99
unsigned lbfgs_failures
Definition panoc.hpp:97
const TypeErasedProblem< config_t > * problem
Definition panoc.hpp:132
const PANOCParams< config_t > * params
Definition panoc.hpp:133
SolverStatus
Exit status of a numerical solver such as ALM or PANOC.
@ Busy
In progress.
std::chrono::nanoseconds time_progress_callback
Definition panoc.hpp:92
std::chrono::nanoseconds elapsed_time
Definition panoc.hpp:91
typename Conf::real_t real_t
Definition config.hpp:72
unsigned linesearch_backtracks
Definition panoc.hpp:95
InnerStatsAccumulator< FISTAStats< Conf > > & operator+=(InnerStatsAccumulator< FISTAStats< Conf > > &acc, const FISTAStats< Conf > &s)
Definition fista.hpp:189
constexpr const auto inf
Definition config.hpp:98
typename Conf::rvec rvec
Definition config.hpp:77
typename Conf::crvec crvec
Definition config.hpp:78
unsigned linesearch_failures
Definition panoc.hpp:94
unsigned iterations
Definition panoc.hpp:93
SolverStatus status
Definition panoc.hpp:89
unsigned count_τ
Definition panoc.hpp:100