Nonconvex constrained optimization
Loading...
Searching...
No Matches
wolfe.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <alpaqa/export.hpp>
10#include <guanaqo/atomic-stop-signal.hpp>
11
12#include <chrono>
13#include <iostream>
14#include <limits>
15#include <stdexcept>
16#include <string>
17#include <type_traits>
18
19namespace alpaqa {
20
21/// Tuning parameters for the unconstrained solver with Wolfe line search.
22/// @ingroup grp_Parameters
23template <Config Conf = DefaultConfig>
26
27 /// Maximum number of inner iterations.
28 unsigned max_iter = 100;
29 /// Maximum duration.
30 std::chrono::nanoseconds max_time = std::chrono::minutes(5);
31 /// Minimum weight factor between Newton step and projected gradient step.
33 /// Ignore the line search condition and always accept the accelerated step.
34 /// (For testing purposes only).
35 bool force_linesearch = false;
38 /// What stopping criterion to use.
40 /// Maximum number of iterations without any progress before giving up.
41 unsigned max_no_progress = 10;
42
43 /// When to print progress. If set to zero, nothing will be printed.
44 /// If set to N != 0, progress is printed every N iterations.
45 unsigned print_interval = 0;
46 /// The precision of the floating point values printed by the solver.
47 int print_precision = std::numeric_limits<real_t>::max_digits10 / 2;
48
49 /// Tolerance factor used in the line search. Its goal is to account for
50 /// numerical errors in the function and gradient evaluations. If you notice
51 /// that accelerated steps are rejected (τ = 0) when getting closer to the
52 /// solution, you may want to increase this factor.
54 10 * std::numeric_limits<real_t>::epsilon();
55};
56
57/// Statistics for the unconstrained solver with Wolfe line search.
58template <Config Conf = DefaultConfig>
59struct WolfeStats {
61
64 std::chrono::nanoseconds elapsed_time{};
65 std::chrono::nanoseconds time_progress_callback{};
66 unsigned iterations = 0;
67 unsigned linesearch_failures = 0;
69 unsigned direction_failures = 0;
71 unsigned τ_1_accepted = 0;
72 unsigned count_τ = 0;
75};
76
77/// Iterate information for the unconstrained solver with Wolfe line search.
78template <Config Conf = DefaultConfig>
98
99/// Unconstrained solver with Wolfe line search.
100/// @ingroup grp_InnerSolvers
101template <class DirectionT>
103 public:
104 USING_ALPAQA_CONFIG_TEMPLATE(DirectionT::config_t);
105
108 using Direction = DirectionT;
112
114 requires std::default_initializable<Direction>
115 : params(params) {}
120
121 Stats operator()(const Problem &problem, // in
122 const SolveOptions &opts, // in
123 rvec x, // inout
124 rvec y, // inout
125 crvec Σ, // in
126 rvec err_z); // out
127
128 template <class P>
129 Stats operator()(const P &problem, const SolveOptions &opts, rvec x, rvec y,
130 crvec Σ, rvec e) {
131 return operator()(Problem{&problem}, opts, x, y, Σ, e);
132 }
133
134 template <class P>
135 Stats operator()(const P &problem, const SolveOptions &opts, rvec x) {
136 if (problem.get_m() != 0)
137 throw std::invalid_argument("Missing arguments y, Σ, e");
138 mvec y{nullptr, 0}, Σ{nullptr, 0}, e{nullptr, 0};
139 return operator()(problem, opts, x, y, Σ, e);
140 }
141
142 /// Specify a callable that is invoked with some intermediate results on
143 /// each iteration of the algorithm.
144 /// @see @ref ProgressInfo
146 set_progress_callback(std::function<void(const ProgressInfo &)> cb) {
147 this->progress_cb = cb;
148 return *this;
149 }
150
151 [[nodiscard]] std::string get_name() const;
152
153 void stop() { stop_signal.stop(); }
154
155 const Params &get_params() const { return params; }
156
157 private:
159 guanaqo::AtomicStopSignal stop_signal;
160 std::function<void(const ProgressInfo &)> progress_cb;
162
163 public:
165 std::ostream *os = &std::cout;
166};
167
168template <class InnerSolverStats>
170
171template <Config Conf>
174
175 /// Total elapsed time in the inner solver.
176 std::chrono::nanoseconds elapsed_time{};
177 /// Total time spent in the user-provided progress callback.
178 std::chrono::nanoseconds time_progress_callback{};
179 /// Total number of inner Wolfe iterations.
180 unsigned iterations = 0;
181 /// Total number of Wolfe line search failures.
183 /// Total number of Wolfe line search backtracking steps.
185 /// Total number of times that the L-BFGS direction was not finite.
186 unsigned direction_failures = 0;
187 /// Total number of times that the L-BFGS update was rejected (i.e. it
188 /// could have resulted in a non-positive definite Hessian estimate).
190 /// Total number of times that a line search parameter of @f$ \tau = 1 @f$
191 /// was accepted (i.e. no backtracking necessary).
192 unsigned τ_1_accepted = 0;
193 /// The total number of line searches performed (used for computing the
194 /// average value of @f$ \tau @f$).
195 unsigned count_τ = 0;
196 /// The sum of the line search parameter @f$ \tau @f$ in all iterations
197 /// (used for computing the average value of @f$ \tau @f$).
199 /// Final value of the smooth cost @f$ \psi(\hat x) @f$.
201};
202
203template <Config Conf>
206 const WolfeStats<Conf> &s) {
207 acc.iterations += s.iterations;
208 acc.elapsed_time += s.elapsed_time;
209 acc.time_progress_callback += s.time_progress_callback;
210 acc.linesearch_failures += s.linesearch_failures;
211 acc.linesearch_backtracks += s.linesearch_backtracks;
212 acc.direction_failures += s.direction_failures;
213 acc.direction_update_rejected += s.direction_update_rejected;
214 acc.τ_1_accepted += s.τ_1_accepted;
215 acc.count_τ += s.count_τ;
216 acc.sum_τ += s.sum_τ;
217 acc.final_ψ = s.final_ψ;
218 return acc;
219}
220
221// clang-format off
222ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeParams, EigenConfigd);
223ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeParams, EigenConfigf);)
224ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeParams, EigenConfigl);)
225ALPAQA_IF_QUADF(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeParams, EigenConfigq);)
226
227ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeStats, EigenConfigd);
228ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeStats, EigenConfigf);)
229ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeStats, EigenConfigl);)
230ALPAQA_IF_QUADF(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeStats, EigenConfigq);)
231
232ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeProgressInfo, EigenConfigd);
233ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeProgressInfo, EigenConfigf);)
234ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeProgressInfo, EigenConfigl);)
235ALPAQA_IF_QUADF(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeProgressInfo, EigenConfigq);)
236// clang-format on
237
238} // namespace alpaqa
The main polymorphic minimization problem interface.
Unconstrained solver with Wolfe line search.
Definition wolfe.hpp:102
std::string get_name() const
Stats operator()(const P &problem, const SolveOptions &opts, rvec x, rvec y, crvec Σ, rvec e)
Definition wolfe.hpp:129
std::function< void(const ProgressInfo &)> progress_cb
Definition wolfe.hpp:160
WolfeProgressInfo< config_t > ProgressInfo
Definition wolfe.hpp:110
WolfeSolver & 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 wolfe.hpp:146
WolfeSolver(const Params &params, const Direction &direction)
Definition wolfe.hpp:118
Direction direction
Definition wolfe.hpp:164
InnerSolveOptions< config_t > SolveOptions
Definition wolfe.hpp:111
WolfeParams< config_t > Params
Definition wolfe.hpp:107
WolfeSolver(const Params &params, Direction &&direction)
Definition wolfe.hpp:116
WolfeSolver(const Params &params)
Definition wolfe.hpp:113
DirectionT Direction
Definition wolfe.hpp:108
Stats operator()(const Problem &problem, const SolveOptions &opts, rvec x, rvec y, crvec Σ, rvec err_z)
const Params & get_params() const
Definition wolfe.hpp:155
WolfeStats< config_t > Stats
Definition wolfe.hpp:109
guanaqo::AtomicStopSignal stop_signal
Definition wolfe.hpp:159
detail::PANOCHelpers< config_t > Helpers
Definition wolfe.hpp:161
Stats operator()(const P &problem, const SolveOptions &opts, rvec x)
Definition wolfe.hpp:135
TypeErasedProblem< config_t > Problem
Definition wolfe.hpp:106
std::ostream * os
Definition wolfe.hpp:165
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:77
#define ALPAQA_IF_QUADF(...)
Definition config.hpp:223
#define ALPAQA_IF_LONGD(...)
Definition config.hpp:235
#define ALPAQA_IF_FLOAT(...)
Definition config.hpp:229
#define USING_ALPAQA_CONFIG_TEMPLATE(Conf)
Definition config.hpp:81
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
Definition export.hpp:23
std::chrono::nanoseconds max_time
Definition wolfe.hpp:30
Tuning parameters for the unconstrained solver with Wolfe line search.
Definition wolfe.hpp:24
typename Conf::mvec mvec
Definition config.hpp:89
@ ApproxKKT
Find an ε-approximate KKT point in the ∞-norm:
SolverStatus
Exit status of a numerical solver such as ALM or PANOC.
std::chrono::nanoseconds time_progress_callback
Definition wolfe.hpp:65
std::chrono::nanoseconds elapsed_time
Definition wolfe.hpp:64
typename Conf::real_t real_t
Definition config.hpp:86
constexpr const auto NaN
Definition config.hpp:114
InnerStatsAccumulator< FISTAStats< Conf > > & operator+=(InnerStatsAccumulator< FISTAStats< Conf > > &acc, const FISTAStats< Conf > &s)
Definition fista.hpp:191
constexpr const auto inf
Definition config.hpp:112
typename Conf::rvec rvec
Definition config.hpp:91
typename Conf::crvec crvec
Definition config.hpp:92
Statistics for the unconstrained solver with Wolfe line search.
Definition wolfe.hpp:59
Iterate information for the unconstrained solver with Wolfe line search.
Definition wolfe.hpp:79
const TypeErasedProblem< config_t > * problem
Definition wolfe.hpp:93
WolfeProgressInfo & operator=(const WolfeProgressInfo &)=delete
const WolfeParams< config_t > * params
Definition wolfe.hpp:94