quala main
Quasi-Newton and other accelerators
Loading...
Searching...
No Matches
anderson-acceleration.hpp
Go to the documentation of this file.
1#pragma once
2
4
5#include <limits>
6#include <stdexcept>
7
8namespace quala {
9
10/// Parameters for the @ref AndersonAccel class.
12 /// Length of the history to keep (the number of columns in the QR
13 /// factorization).
14 /// If this number is greater than the problem dimension, the memory is set
15 /// to the problem dimension (otherwise the system is underdetermined).
17 /// Minimum divisor when solving close to singular systems,
18 /// scaled by the maximum eigenvalue of R.
19 real_t min_div = 1e2 * std::numeric_limits<real_t>::epsilon();
20};
21
22/**
23 * Anderson Acceleration.
24 *
25 * Algorithm for accelerating fixed-point iterations for finding fixed points
26 * of a function @f$ g @f$, i.e. @f$ g(x^\star) = x^\star @f$, or equivalently,
27 * roots of the residual @f$ r(x) \triangleq g(x) - x @f$.
28 *
29 * @todo Condition estimation of the QR factorization.
30 *
31 * @ingroup accelerators-grp
32 */
34 public:
36
37 /// @param params
38 /// Parameters.
40 /// @param params
41 /// Parameters.
42 /// @param n
43 /// Problem dimension (size of the vectors).
45
46 /// Change the problem dimension. Flushes the history.
47 /// @param n
48 /// Problem dimension (size of the vectors).
50 length_t m_AA = std::min(n, params.memory); // TODO: support m > n?
51 qr.resize(n, m_AA);
52 G.resize(n, m_AA);
53 rₗₐₛₜ.resize(n);
54 γ_LS.resize(m_AA);
55 initialized = false;
56 }
57
58 /// Call this function on the first iteration to initialize the accelerator.
59 void initialize(crvec g_0, vec r_0) {
60 assert(g_0.size() == vec::Index(n()));
61 assert(r_0.size() == vec::Index(n()));
62 G.col(0) = g_0;
63 rₗₐₛₜ = std::move(r_0);
64 qr.reset();
65 initialized = true;
66 }
67
68 /// Compute the accelerated iterate @f$ x^k_\text{AA} @f$, given the
69 /// function value at the current iterate @f$ g^k = g(x^k) @f$ and the
70 /// corresponding residual @f$ r^k = g^k - x^k @f$.
71 void compute(crvec gₖ, crvec rₖ, rvec xₖ_aa) {
72 if (!initialized)
73 throw std::logic_error("AndersonAccel::compute() called before "
74 "AndersonAccel::initialize()");
76 rₖ, rₗₐₛₜ, gₖ, params.min_div, // in
77 γ_LS, xₖ_aa); // out
78 rₗₐₛₜ = rₖ;
79 }
80 /// @copydoc compute(crvec, crvec, rvec)
81 void compute(crvec gₖ, vec &&rₖ, rvec xₖ_aa) {
82 if (!initialized)
83 throw std::logic_error("AndersonAccel::compute() called before "
84 "AndersonAccel::initialize()");
86 rₖ, rₗₐₛₜ, gₖ, params.min_div, // in
87 γ_LS, xₖ_aa); // out
88 rₗₐₛₜ = std::move(rₖ);
89 }
90
91 /// Reset the accelerator (but keep the last function value and residual, so
92 /// calling @ref initialize is not necessary).
93 void reset() {
94 index_t newest_g_idx = qr.ring_tail();
95 if (newest_g_idx != 0)
96 G.col(0) = G.col(newest_g_idx);
97 qr.reset();
98 }
99
100 /// Get the problem dimension.
101 length_t n() const { return qr.n(); }
102 /// Get the maximum number of stored columns.
103 length_t history() const { return qr.m(); }
104 /// Get the number of columns currently stored in the buffer.
106
107 /// Get the parameters.
108 const Params &get_params() const { return params; }
109
110 private:
116 bool initialized = false;
117};
118
119} // namespace quala
Anderson Acceleration.
AndersonAccel(Params params, length_t n)
length_t current_history() const
Get the number of columns currently stored in the buffer.
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...
void initialize(crvec g_0, vec r_0)
Call this function on the first iteration to initialize the accelerator.
const Params & get_params() const
Get the parameters.
length_t history() const
Get the maximum number of stored columns.
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 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 minimize_update_anderson(LimitedMemoryQR &qr, rmat G̃, crvec rₖ, crvec rₗₐₛₜ, crvec gₖ, real_t min_div, rvec γ_LS, rvec xₖ_aa)
Solve one step of Anderson acceleration to find a fixed point of a function g(x):
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
length_t memory
Length of the history to keep (the number of columns in the QR factorization).
realmat mat
Default type for matrices.
Definition: vec.hpp:20
index_t length_t
Default type for vector sizes.
Definition: vec.hpp:29
real_t min_div
Minimum divisor when solving close to singular systems, scaled by the maximum eigenvalue of R.
realvec vec
Default type for vectors.
Definition: vec.hpp:14
Eigen::Index index_t
Default type for vector indices.
Definition: vec.hpp:27
double real_t
Default floating point type.
Definition: vec.hpp:8
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
Parameters for the AndersonAccel class.