quala main
Quasi-Newton and other accelerators
Loading...
Searching...
No Matches
anderson-helpers.hpp
Go to the documentation of this file.
1#pragma once
2
4
5namespace quala {
6
7/**
8 * @brief Solve one step of Anderson acceleration to find a fixed point of a
9 * function g(x):
10 *
11 * @f$ g(x^\star) - x^\star = 0 @f$
12 *
13 * Updates the QR factorization of @f$ \mathcal{R}_k = QR @f$, solves the least
14 * squares problem to find @f$ \gamma_\text{LS} @f$, computes the next
15 * iterate @f$ x_{k+1} @f$, and stores the current function value @f$ g_k @f$
16 * in the matrix @f$ G @f$, which is used as a circular buffer.
17 * @f[ \begin{aligned}
18 * \def\gammaLS{\gamma_\text{LS}}
19 * m_k &= \min \{k, m\} \\
20 * g_i &= g(x_i) \\
21 * r_i &= r(x_i) g_i - x_i \\
22 * \Delta r_i &= r_i - r_{i-1} \\
23 * \mathcal{R}_k &= \begin{pmatrix} \Delta r_{k-m_k+1} & \dots & \Delta r_k \end{pmatrix} \in \R^{n\times m_k}
24 * \\
25 * \gammaLS &= \argmin_{\gamma \in \R^{m_k}}\; \norm{\mathcal{R}_k \gamma - r_k}_2 \\
26 * \alpha_i &= \begin{cases} \gammaLS[0] & i = 0 \\
27 * \gammaLS[i] - \gammaLS[i-1] & 0 \lt i \lt m_k \\
28 * 1 - \gammaLS[m_k - 1] & i = m_k \end{cases} \\
29 * \tilde G_k &= \begin{pmatrix} g_{k - m_k} & \dots & g_{k-1} \end{pmatrix} \\
30 * G_k &= \begin{pmatrix} g_{k - m_k} & \dots & g_{k} \end{pmatrix} \\
31 * &= \begin{pmatrix} \tilde G_k & g_{k} \end{pmatrix} \\
32 * x_{k+1} &= \sum_{i=0}^{m_k} \alpha_i\,g_{k - m_k + i} \\
33 * &= G_k \alpha \\
34 * \end{aligned} @f]
35 */
37 /// [inout] QR factorization of @f$ \mathcal{R}_k @f$
39 /// [inout] Matrix of previous function values @f$ \tilde G_k @f$
40 /// (stored as ring buffer with the same indices as `qr`)
41 rmat G̃,
42 /// [in] Current residual @f$ r_k @f$
43 crvec rₖ,
44 /// [in] Previous residual @f$ r_{k-1} @f$
45 crvec rₗₐₛₜ,
46 /// [in] Current function value @f$ g_k @f$
47 crvec gₖ,
48 /// [in] Minimum divisor when solving close to singular systems,
49 /// scaled by the maximum eigenvalue of R
50 real_t min_div,
51 /// [out] Solution to the least squares system
52 rvec γ_LS,
53 /// [out] Next Anderson iterate
54 rvec xₖ_aa) {
55
56 // Update QR factorization for Anderson acceleration
57 if (qr.num_columns() == qr.m()) // if the history buffer is full
58 qr.remove_column();
59 qr.add_column(rₖ - rₗₐₛₜ);
60
61 // Solve least squares problem Anderson acceleration
62 // γ = argmin ‖ ΔR γ - rₖ ‖²
63 qr.solve_col(rₖ, γ_LS, qr.get_max_eig() * min_div);
64
65 // Iterate over columns of G, whose indices match the indices of the matrix
66 // R in the QR factorization, stored as a circular buffer.
67 auto g_it = qr.ring_iter().begin();
68 auto g_end = qr.ring_iter().end();
69 assert(g_it != g_end);
70
71 // Compute Anderson acceleration next iterate yₑₓₜ = ∑ₙ₌₀ αₙ gₙ
72 // α₀ = γ₀ if n = 0
73 // αₙ = γₙ - γₙ₋₁ if 0 < n < mₖ
74 // αₘ = 1 - γₘ₋₁ if n = mₖ
75 real_t α = γ_LS(0);
76 xₖ_aa = α * G̃.col((*g_it).circular);
77 while (++g_it != g_end) {
78 auto [i, g_idx] = *g_it; // [zero based index, circular index]
79 α = γ_LS(i) - γ_LS(i - 1);
80 xₖ_aa += α * G̃.col(g_idx);
81 }
82 α = 1 - γ_LS(qr.num_columns() - 1);
83 xₖ_aa += α * gₖ;
84
85 // Add the new column to G
86 G̃.col(qr.ring_tail()) = gₖ; // TODO: avoid copy, make G an array of vectors
87}
88
89} // namespace quala
Incremental QR factorization using modified Gram-Schmidt with reorthogonalization.
CircularRange< index_t > ring_iter() const
Get iterators in the circular buffer.
length_t num_columns() const
Get the number of columns that are currently stored.
void remove_column()
Remove the leftmost column.
void add_column(const VecV &v)
Add the given column to the right.
void solve_col(const VecB &b, VecX &x, real_t tol=0) const
Solve the least squares problem Ax = b.
index_t ring_tail() const
Get the tail index of the circular buffer (points to one past the most recent element).
real_t get_max_eig() const
Get the maximum eigenvalue of R.
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
Eigen::Ref< mat > rmat
Default type for mutable references to matrices.
Definition: vec.hpp:22
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