alpaqa pantr
Nonconvex constrained optimization
Loading...
Searching...
No Matches
accelerators/anderson.hpp
Go to the documentation of this file.
1#pragma once
2
5#include <alpaqa/export.hpp>
6
7#include <limits>
8#include <stdexcept>
9
10namespace alpaqa {
11
12/// Parameters for the @ref AndersonAccel class.
13template <Config Conf = DefaultConfig>
16 /// Length of the history to keep (the number of columns in the QR
17 /// factorization).
18 /// If this number is greater than the problem dimension, the memory is set
19 /// to the problem dimension (otherwise the system is underdetermined).
21 /// Minimum divisor when solving close to singular systems,
22 /// scaled by the maximum eigenvalue of R.
23 real_t min_div_fac = real_t(1e2) * std::numeric_limits<real_t>::epsilon();
24};
25
26/**
27 * Anderson Acceleration.
28 *
29 * Algorithm for accelerating fixed-point iterations for finding fixed points
30 * of a function @f$ g @f$, i.e. @f$ g(x^\star) = x^\star @f$, or equivalently,
31 * roots of the residual @f$ r(x) \triangleq g(x) - x @f$.
32 *
33 * @todo Condition estimation of the QR factorization.
34 *
35 * @ingroup grp_Accelerators
36 */
37template <Config Conf = DefaultConfig>
39 public:
42
43 AndersonAccel() = default;
44 /// @param params
45 /// Parameters.
47 /// @param params
48 /// Parameters.
49 /// @param n
50 /// Problem dimension (size of the vectors).
52
53 /// Change the problem dimension. Flushes the history.
54 /// @param n
55 /// Problem dimension (size of the vectors).
57 length_t m_AA = std::min(n, params.memory); // TODO: support m > n?
58 qr.resize(n, m_AA);
59 G.resize(n, m_AA);
60 rₗₐₛₜ.resize(n);
61 γ_LS.resize(m_AA);
62 initialized = false;
63 }
64
65 /// Call this function on the first iteration to initialize the accelerator.
66 void initialize(crvec g_0, crvec r_0) {
67 assert(g_0.size() == n());
68 assert(r_0.size() == n());
69 G.col(0) = g_0;
70 rₗₐₛₜ = r_0;
71 qr.reset();
72 initialized = true;
73 }
74
75 /// Compute the accelerated iterate @f$ x^k_\text{AA} @f$, given the
76 /// function value at the current iterate @f$ g^k = g(x^k) @f$ and the
77 /// corresponding residual @f$ r^k = g^k - x^k @f$.
78 void compute(crvec gₖ, crvec rₖ, rvec xₖ_aa) {
79 if (!initialized)
80 throw std::logic_error("AndersonAccel::compute() called before "
81 "AndersonAccel::initialize()");
83 rₖ, rₗₐₛₜ, gₖ, params.min_div_fac, // in
84 γ_LS, xₖ_aa); // out
85 rₗₐₛₜ = rₖ;
86 }
87 /// @copydoc compute(crvec, crvec, rvec)
88 void compute(crvec gₖ, vec &&rₖ, rvec xₖ_aa) {
89 if (!initialized)
90 throw std::logic_error("AndersonAccel::compute() called before "
91 "AndersonAccel::initialize()");
93 rₖ, rₗₐₛₜ, gₖ, params.min_div_fac, // in
94 γ_LS, xₖ_aa); // out
95 rₗₐₛₜ = std::move(rₖ);
96 }
97
98 /// Reset the accelerator (but keep the last function value and residual, so
99 /// calling @ref initialize is not necessary).
100 void reset() {
101 index_t newest_g_idx = qr.ring_tail();
102 if (newest_g_idx != 0)
103 G.col(0) = G.col(newest_g_idx);
104 qr.reset();
105 }
106
107 /// Get the problem dimension.
108 length_t n() const { return qr.n(); }
109 /// Get the maximum number of stored columns.
110 length_t history() const { return qr.m(); }
111 /// Get the number of columns currently stored in the buffer.
113
114 /// Get the parameters.
115 const Params &get_params() const { return params; }
116
117 std::string get_name() const {
118 return "AndersonAccel<" + std::string(config_t::get_name()) + '>';
119 }
120
121 /// Scale the factorization
122 void scale_R(real_t scal) { qr.scale_R(scal); }
123
124 private:
130 bool initialized = false;
131};
132
133} // namespace alpaqa
Anderson Acceleration.
std::string get_name() const
AndersonAccel(Params params, length_t n)
length_t current_history() const
Get the number of columns currently stored in the buffer.
LimitedMemoryQR< config_t > qr
length_t n() const
Get the problem dimension.
void compute(crvec gₖ, crvec rₖ, rvec xₖ_aa)
Compute the accelerated iterate , given the function value at the current iterate and the correspond...
const Params & get_params() const
Get the parameters.
length_t history() const
Get the maximum number of stored columns.
void initialize(crvec g_0, crvec r_0)
Call this function on the first iteration to initialize the accelerator.
void resize(length_t n)
Change the problem dimension.
void reset()
Reset the accelerator (but keep the last function value and residual, so calling initialize is not ne...
void scale_R(real_t scal)
Scale the factorization.
void compute(crvec gₖ, vec &&rₖ, rvec xₖ_aa)
Compute the accelerated iterate , given the function value at the current iterate and the correspond...
Incremental QR factorization using modified Gram-Schmidt with reorthogonalization.
length_t current_history() const
Get the number of columns currently stored in the buffer.
index_t ring_tail() const
Get the tail index of the circular buffer (points to one past the most recent element).
void resize(length_t n, length_t m)
Re-allocate storage for a problem with a different size.
void reset()
Reset all indices, clearing the Q and R matrices.
void scale_R(real_t scal)
Multiply the matrix R by a scalar.
#define USING_ALPAQA_CONFIG(Conf)
Definition: config.hpp:42
typename Conf::mat mat
Definition: config.hpp:57
length_t memory
Length of the history to keep (the number of columns in the QR factorization).
typename Conf::real_t real_t
Definition: config.hpp:51
typename Conf::index_t index_t
Definition: config.hpp:63
typename Conf::length_t length_t
Definition: config.hpp:62
typename Conf::rvec rvec
Definition: config.hpp:55
typename Conf::crvec crvec
Definition: config.hpp:56
typename Conf::vec vec
Definition: config.hpp:52
real_t min_div_fac
Minimum divisor when solving close to singular systems, scaled by the maximum eigenvalue of R.
void minimize_update_anderson(LimitedMemoryQR< Conf > &qr, rmat< Conf > G̃, crvec< Conf > rₖ, crvec< Conf > rₗₐₛₜ, crvec< Conf > gₖ, real_t< Conf > min_div_fac, rvec< Conf > γ_LS, rvec< Conf > xₖ_aa)
Solve one step of Anderson acceleration to find a fixed point of a function g(x):
Parameters for the AndersonAccel class.