quala main
Quasi-Newton and other accelerators
anderson-acceleration.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <stdexcept>
5
6namespace quala {
7
8/// Parameters for the @ref AndersonAccel class.
10 /// Length of the history to keep (the number of columns in the QR
11 /// factorization).
12 /// If this number is greater than the problem dimension, the memory is set
13 /// to the problem dimension (otherwise the system is underdetermined).
15};
16
17/// Anderson Acceleration.
18///
19/// @todo Condition estimation of the QR factorization.
20///
21/// @ingroup accelerators-grp
23 public:
25
26 /// @param params
27 /// Parameters.
29 /// @param params
30 /// Parameters.
31 /// @param n
32 /// Problem dimension (size of the vectors).
34
35 /// Change the problem dimension. Flushes the history.
36 /// @param n
37 /// Problem dimension (size of the vectors).
39 length_t m_AA = std::min(n, params.memory); // TODO: support m > n?
40 qr.resize(n, m_AA);
41 G.resize(n, m_AA);
42 rₖ₋₁.resize(n);
43 γ_LS.resize(m_AA);
44 initialized = false;
45 }
46
47 /// Call this function on the first iteration to initialize the accelerator.
48 void initialize(crvec g₀, vec r₀) {
49 assert(g₀.size() == vec::Index(n()));
50 assert(r₀.size() == vec::Index(n()));
51 G.col(0) = g₀;
52 rₖ₋₁ = std::move(r₀);
53 qr.reset();
54 initialized = true;
55 }
56
57 /// Compute the accelerated iterate @f$ x^k_\text{AA} @f$, given the
58 /// function value at the current iterate @f$ g^k = g(x^k) @f$ and the
59 /// corresponding residual @f$ r^k = g^k - x^k @f$.
60 void compute(crvec gₖ, crvec rₖ, rvec xₖ_aa) {
61 if (!initialized)
62 throw std::logic_error("AndersonAccel::compute() called before "
63 "AndersonAccel::initialize()");
65 rₖ, rₖ₋₁, gₖ, // in
66 γ_LS, xₖ_aa); // out
67 rₖ₋₁ = rₖ;
68 }
69 /// @copydoc compute(crvec, crvec, rvec)
70 void compute(crvec gₖ, vec &&rₖ, rvec xₖ_aa) {
71 if (!initialized)
72 throw std::logic_error("AndersonAccel::compute() called before "
73 "AndersonAccel::initialize()");
75 rₖ, rₖ₋₁, gₖ, // in
76 γ_LS, xₖ_aa); // out
77 rₖ₋₁ = std::move(rₖ);
78 }
79
80 /// Reset the accelerator (but keep the last function value and residual, so
81 /// calling @ref initialize is not necessary).
82 void reset() {
83 index_t newest_g_idx = qr.ring_tail();
84 if (newest_g_idx != 0)
85 G.col(0) = G.col(newest_g_idx);
86 qr.reset();
87 }
88
89 /// Get the problem dimension.
90 length_t n() const { return qr.n(); }
91 /// Get the maximum number of stored columns.
92 length_t history() const { return qr.m(); }
93 /// Get the number of columns currently stored in the buffer.
95
96 /// Get the parameters.
97 const Params &get_params() const { return params; }
98
99 private:
105 bool initialized = false;
106};
107
108} // 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...
const Params & get_params() const
Get the parameters.
length_t history() const
Get the maximum number of stored columns.
void initialize(crvec g₀, vec r₀)
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 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.
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
realvec vec
Default type for vectors.
Definition: vec.hpp:14
Eigen::Index index_t
Default type for vector indices.
Definition: vec.hpp:27
void minimize_update_anderson(LimitedMemoryQR &qr, rmat G, crvec rₖ, crvec rₖ₋₁, crvec gₖ, rvec γ_LS, rvec xₖ_aa)
Solve one step of Anderson acceleration to find a fixed point of a function g(x):
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
Parameters for the AndersonAccel class.