alpaqa 1.0.0a16
Nonconvex constrained optimization
Loading...
Searching...
No Matches
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.
13/// @ingroup grp_Parameters
14template <Config Conf = DefaultConfig>
17 /// Length of the history to keep (the number of columns in the QR
18 /// factorization).
19 /// If this number is greater than the problem dimension, the memory is set
20 /// to the problem dimension (otherwise the system is underdetermined).
22 /// Minimum divisor when solving close to singular systems,
23 /// scaled by the maximum eigenvalue of R.
24 real_t min_div_fac = real_t(1e2) * std::numeric_limits<real_t>::epsilon();
25};
26
27/**
28 * Anderson Acceleration.
29 *
30 * Algorithm for accelerating fixed-point iterations for finding fixed points
31 * of a function @f$ g @f$, i.e. @f$ g(x^\star) = x^\star @f$, or equivalently,
32 * roots of the residual @f$ r(x) \triangleq g(x) - x @f$.
33 *
34 * @todo Condition estimation of the QR factorization.
35 *
36 * @ingroup grp_Accelerators
37 */
38template <Config Conf = DefaultConfig>
40 public:
43
44 AndersonAccel() = default;
45 /// @param params
46 /// Parameters.
48 /// @param params
49 /// Parameters.
50 /// @param n
51 /// Problem dimension (size of the vectors).
53
54 /// Change the problem dimension. Flushes the history.
55 /// @param n
56 /// Problem dimension (size of the vectors).
58 length_t m_AA = std::min(n, params.memory); // TODO: support m > n?
59 qr.resize(n, m_AA);
60 G.resize(n, m_AA);
61 rₗₐₛₜ.resize(n);
62 γ_LS.resize(m_AA);
63 initialized = false;
64 }
65
66 /// Call this function on the first iteration to initialize the accelerator.
68 assert(g_0.size() == n());
69 assert(r_0.size() == n());
70 G.col(0) = g_0;
72 qr.reset();
73 initialized = true;
74 }
75
76 /// Compute the accelerated iterate @f$ x^k_\text{AA} @f$, given the
77 /// function value at the current iterate @f$ g^k = g(x^k) @f$ and the
78 /// corresponding residual @f$ r^k = g^k - x^k @f$.
80 if (!initialized)
81 throw std::logic_error("AndersonAccel::compute() called before "
82 "AndersonAccel::initialize()");
85 γ_LS, xₖ_aa); // out
87 }
88 /// @copydoc compute(crvec, crvec, rvec)
90 if (!initialized)
91 throw std::logic_error("AndersonAccel::compute() called before "
92 "AndersonAccel::initialize()");
95 γ_LS, xₖ_aa); // out
96 rₗₐₛₜ = std::move(rₖ);
97 }
98
99 /// Reset the accelerator (but keep the last function value and residual, so
100 /// calling @ref initialize is not necessary).
101 void reset() {
103 if (newest_g_idx != 0)
104 G.col(0) = G.col(newest_g_idx);
105 qr.reset();
106 }
107
108 /// Get the problem dimension.
109 length_t n() const { return qr.n(); }
110 /// Get the maximum number of stored columns.
111 length_t history() const { return qr.m(); }
112 /// Get the number of columns currently stored in the buffer.
114
115 /// Get the parameters.
116 const Params &get_params() const { return params; }
117
118 std::string get_name() const {
119 return "AndersonAccel<" + std::string(config_t::get_name()) + '>';
120 }
121
122 /// Scale the factorization
124
125 /// For testing purposes
126 const LimitedMemoryQR<config_t> &get_QR() const { return qr; }
127
128 private:
134 bool initialized = false;
135};
136
137} // namespace alpaqa
Anderson Acceleration.
Definition anderson.hpp:39
std::string get_name() const
Definition anderson.hpp:118
AndersonAccel(Params params, length_t n)
Definition anderson.hpp:52
length_t current_history() const
Get the number of columns currently stored in the buffer.
Definition anderson.hpp:113
LimitedMemoryQR< config_t > qr
Definition anderson.hpp:130
length_t n() const
Get the problem dimension.
Definition anderson.hpp:109
void compute(crvec gₖ, crvec rₖ, rvec xₖ_aa)
Compute the accelerated iterate , given the function value at the current iterate and the correspond...
Definition anderson.hpp:79
const Params & get_params() const
Get the parameters.
Definition anderson.hpp:116
length_t history() const
Get the maximum number of stored columns.
Definition anderson.hpp:111
void initialize(crvec g_0, crvec r_0)
Call this function on the first iteration to initialize the accelerator.
Definition anderson.hpp:67
AndersonAccel(Params params)
Definition anderson.hpp:47
void resize(length_t n)
Change the problem dimension.
Definition anderson.hpp:57
void reset()
Reset the accelerator (but keep the last function value and residual, so calling initialize is not ne...
Definition anderson.hpp:101
void scale_R(real_t scal)
Scale the factorization.
Definition anderson.hpp:123
void compute(crvec gₖ, vec &&rₖ, rvec xₖ_aa)
Compute the accelerated iterate , given the function value at the current iterate and the correspond...
Definition anderson.hpp:89
const LimitedMemoryQR< config_t > & get_QR() const
For testing purposes.
Definition anderson.hpp:126
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:63
length_t memory
Length of the history to keep (the number of columns in the QR factorization).
Definition anderson.hpp:21
real_t min_div_fac
Minimum divisor when solving close to singular systems, scaled by the maximum eigenvalue of R.
Definition anderson.hpp:24
Parameters for the AndersonAccel class.
Definition anderson.hpp:15
typename Conf::mat mat
Definition config.hpp:79
typename Conf::real_t real_t
Definition config.hpp:72
typename Conf::index_t index_t
Definition config.hpp:90
typename Conf::length_t length_t
Definition config.hpp:89
constexpr const auto inf
Definition config.hpp:98
typename Conf::rvec rvec
Definition config.hpp:77
typename Conf::crvec crvec
Definition config.hpp:78
typename Conf::vec vec
Definition config.hpp:74
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):