alpaqa cmake-targets
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>
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 lbfgs_failures = 0;
70 unsigned lbfgs_rejected = 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>
96
97/// Unconstrained solver with Wolfe line search.
98/// @ingroup grp_InnerSolvers
99template <class DirectionT>
101 public:
102 USING_ALPAQA_CONFIG_TEMPLATE(DirectionT::config_t);
103
110
112 requires std::default_initializable<Direction>
113 : params(params) {}
118
119 Stats operator()(const Problem &problem, // in
120 const SolveOptions &opts, // in
121 rvec x, // inout
122 rvec y, // inout
123 crvec Σ, // in
124 rvec err_z); // out
125
126 template <class P>
127 Stats operator()(const P &problem, const SolveOptions &opts, rvec x, rvec y,
128 crvec Σ, rvec e) {
129 return operator()(Problem{&problem}, opts, x, y, Σ, e);
130 }
131
132 template <class P>
133 Stats operator()(const P &problem, const SolveOptions &opts, rvec x) {
134 if (problem.get_m() != 0)
135 throw std::invalid_argument("Missing arguments y, Σ, e");
136 mvec y{nullptr, 0}, Σ{nullptr, 0}, e{nullptr, 0};
137 return operator()(problem, opts, x, y, Σ, e);
138 }
139
140 /// Specify a callable that is invoked with some intermediate results on
141 /// each iteration of the algorithm.
142 /// @see @ref ProgressInfo
144 set_progress_callback(std::function<void(const ProgressInfo &)> cb) {
145 this->progress_cb = cb;
146 return *this;
147 }
148
149 [[nodiscard]] std::string get_name() const;
150
151 void stop() { stop_signal.stop(); }
152
153 const Params &get_params() const { return params; }
154
155 private:
158 std::function<void(const ProgressInfo &)> progress_cb;
160
161 public:
163 std::ostream *os = &std::cout;
164};
165
166template <class InnerSolverStats>
168
169template <Config Conf>
172
173 /// Total elapsed time in the inner solver.
174 std::chrono::nanoseconds elapsed_time{};
175 /// Total time spent in the user-provided progress callback.
176 std::chrono::nanoseconds time_progress_callback{};
177 /// Total number of inner Wolfe iterations.
178 unsigned iterations = 0;
179 /// Total number of Wolfe line search failures.
180 unsigned linesearch_failures = 0;
181 /// Total number of Wolfe line search backtracking steps.
182 unsigned linesearch_backtracks = 0;
183 /// Total number of times that the L-BFGS direction was not finite.
184 unsigned lbfgs_failures = 0;
185 /// Total number of times that the L-BFGS update was rejected (i.e. it
186 /// could have resulted in a non-positive definite Hessian estimate).
187 unsigned lbfgs_rejected = 0;
188 /// Total number of times that a line search parameter of @f$ \tau = 1 @f$
189 /// was accepted (i.e. no backtracking necessary).
190 unsigned τ_1_accepted = 0;
191 /// The total number of line searches performed (used for computing the
192 /// average value of @f$ \tau @f$).
193 unsigned count_τ = 0;
194 /// The sum of the line search parameter @f$ \tau @f$ in all iterations
195 /// (used for computing the average value of @f$ \tau @f$).
196 real_t sum_τ = 0;
197 /// Final value of the smooth cost @f$ \psi(\hat x) @f$.
198 real_t final_ψ = 0;
199};
200
201template <Config Conf>
204 const WolfeStats<Conf> &s) {
206 acc.elapsed_time += s.elapsed_time;
207 acc.time_progress_callback += s.time_progress_callback;
208 acc.linesearch_failures += s.linesearch_failures;
209 acc.linesearch_backtracks += s.linesearch_backtracks;
210 acc.lbfgs_failures += s.lbfgs_failures;
211 acc.lbfgs_rejected += s.lbfgs_rejected;
212 acc.τ_1_accepted += s.τ_1_accepted;
213 acc.count_τ += s.count_τ;
214 acc.sum_τ += s.sum_τ;
215 acc.final_ψ = s.final_ψ;
216 return acc;
217}
218
219// clang-format off
220ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeParams, EigenConfigd);
221ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeParams, EigenConfigf);)
222ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeParams, EigenConfigl);)
224
225ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeStats, EigenConfigd);
226ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeStats, EigenConfigf);)
227ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeStats, EigenConfigl);)
229
230ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeProgressInfo, EigenConfigd);
231ALPAQA_IF_FLOAT(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeProgressInfo, EigenConfigf);)
232ALPAQA_IF_LONGD(ALPAQA_EXPORT_EXTERN_TEMPLATE(struct, WolfeProgressInfo, EigenConfigl);)
234// clang-format on
235
236} // namespace alpaqa
The main polymorphic minimization problem interface.
Unconstrained solver with Wolfe line search.
Definition wolfe.hpp:100
std::string get_name() const
Stats operator()(const P &problem, const SolveOptions &opts, rvec x, rvec y, crvec Σ, rvec e)
Definition wolfe.hpp:127
std::function< void(const ProgressInfo &)> progress_cb
Definition wolfe.hpp:158
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:144
WolfeSolver(const Params &params, const Direction &direction)
Definition wolfe.hpp:116
Direction direction
Definition wolfe.hpp:162
WolfeSolver(const Params &params, Direction &&direction)
Definition wolfe.hpp:114
WolfeSolver(const Params &params)
Definition wolfe.hpp:111
AtomicStopSignal stop_signal
Definition wolfe.hpp:157
DirectionT Direction
Definition wolfe.hpp:106
Stats operator()(const Problem &problem, const SolveOptions &opts, rvec x, rvec y, crvec Σ, rvec err_z)
const Params & get_params() const
Definition wolfe.hpp:153
Stats operator()(const P &problem, const SolveOptions &opts, rvec x)
Definition wolfe.hpp:133
std::ostream * os
Definition wolfe.hpp:163
#define USING_ALPAQA_CONFIG(Conf)
Definition config.hpp:56
#define ALPAQA_IF_QUADF(...)
Definition config.hpp:182
#define ALPAQA_IF_LONGD(...)
Definition config.hpp:194
#define ALPAQA_IF_FLOAT(...)
Definition config.hpp:188
#define USING_ALPAQA_CONFIG_TEMPLATE(Conf)
Definition config.hpp:60
#define ALPAQA_EXPORT_EXTERN_TEMPLATE(...)
Definition export.hpp:21
real_t linesearch_tolerance_factor
Tolerance factor used in the line search.
Definition wolfe.hpp:53
real_t curvature_factor
Definition wolfe.hpp:37
unsigned max_no_progress
Maximum number of iterations without any progress before giving up.
Definition wolfe.hpp:41
real_t min_linesearch_coefficient
Minimum weight factor between Newton step and projected gradient step.
Definition wolfe.hpp:32
std::chrono::nanoseconds max_time
Maximum duration.
Definition wolfe.hpp:30
bool force_linesearch
Ignore the line search condition and always accept the accelerated step.
Definition wolfe.hpp:35
unsigned print_interval
When to print progress.
Definition wolfe.hpp:45
int print_precision
The precision of the floating point values printed by the solver.
Definition wolfe.hpp:47
unsigned max_iter
Maximum number of inner iterations.
Definition wolfe.hpp:28
PANOCStopCrit stop_crit
What stopping criterion to use.
Definition wolfe.hpp:39
real_t decrease_factor
Definition wolfe.hpp:36
Tuning parameters for the unconstrained solver with Wolfe line search.
Definition wolfe.hpp:24
unsigned lbfgs_rejected
Definition wolfe.hpp:70
typename Conf::mvec mvec
Definition config.hpp:67
@ ApproxKKT
Find an ε-approximate KKT point in the ∞-norm:
unsigned τ_1_accepted
Definition wolfe.hpp:71
unsigned lbfgs_failures
Definition wolfe.hpp:69
const TypeErasedProblem< config_t > * problem
Definition wolfe.hpp:93
SolverStatus
Exit status of a numerical solver such as ALM or PANOC.
@ Busy
In progress.
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:65
unsigned linesearch_backtracks
Definition wolfe.hpp:68
InnerStatsAccumulator< FISTAStats< Conf > > & operator+=(InnerStatsAccumulator< FISTAStats< Conf > > &acc, const FISTAStats< Conf > &s)
Definition fista.hpp:189
constexpr const auto inf
Definition config.hpp:85
typename Conf::rvec rvec
Definition config.hpp:69
typename Conf::crvec crvec
Definition config.hpp:70
unsigned linesearch_failures
Definition wolfe.hpp:67
unsigned iterations
Definition wolfe.hpp:66
SolverStatus status
Definition wolfe.hpp:62
unsigned count_τ
Definition wolfe.hpp:72
const WolfeParams< config_t > * params
Definition wolfe.hpp:94
Iterate information for the unconstrained solver with Wolfe line search.
Definition wolfe.hpp:79
Statistics for the unconstrained solver with Wolfe line search.
Definition wolfe.hpp:59