alpaqa 0.0.1
Nonconvex constrained optimization
anderson-acceleration.hpp
Go to the documentation of this file.
1#pragma once
2
5
6namespace alpaqa {
7
8/// Anderson Acceleration.
9///
10/// @ingroup grp_PANOCDirectionProviders
12 public:
13 void resize(size_t n, size_t m) {
14 size_t m_AA = std::min(n, m); // TODO: support m > n?
15 qr.resize(n, m_AA);
16 G.resize(n, m_AA);
17 rₖ₋₁.resize(n);
18 γ_LS.resize(m_AA);
19 }
20
21 void initialize(crvec g₀, crvec r₀) {
22 G.col(0) = g₀;
23 rₖ₋₁ = r₀;
24 }
25
26 void compute(crvec gₖ, crvec rₖ, rvec xₖ_aa) {
27 minimize_update_anderson(qr, G, rₖ, rₖ₋₁, gₖ, γ_LS, xₖ_aa);
28 }
29
30 std::string get_name() const { return "AndersonAccel"; }
31
32 void reinitialize(real_t γₖ, real_t old_γₖ) {
33 reset();
34 // TODO: show mathematically that this is a sensible thing to do:
35 rₖ₋₁ *= γₖ / old_γₖ;
36 }
37
38 void reset() {
39 size_t newest_g_idx = qr.ring_tail();
40 if (newest_g_idx != 0)
41 G.col(0) = G.col(newest_g_idx);
42 qr.reset();
43 }
44
45 private:
50};
51
52template <>
54
55 static void initialize(AndersonAccel &aa, crvec x₀, crvec x̂₀,
56 crvec p₀, crvec grad₀) {
57 aa.initialize(x̂₀, p₀);
58 (void)aa;
59 (void)x₀;
60 (void)grad₀;
61 }
62
63 static bool update(AndersonAccel &aa, crvec xₖ, crvec xₖ₊₁,
64 crvec pₖ, crvec pₖ₊₁, crvec grad_new,
65 const Box &C, real_t γ_new) {
66 (void)aa;
67 (void)xₖ;
68 (void)xₖ₊₁;
69 (void)pₖ;
70 (void)pₖ₊₁;
71 (void)grad_new;
72 (void)C;
73 (void)γ_new;
74 return true;
75 }
76
77 static bool apply(AndersonAccel &aa, crvec xₖ, crvec x̂ₖ,
78 crvec pₖ, real_t γ, rvec qₖ) {
79 (void)xₖ;
80 (void)x̂ₖ;
81 (void)γ;
82 qₖ = pₖ;
83 aa.compute(x̂ₖ, pₖ, qₖ);
84 return true;
85 }
86
87 static void changed_γ(AndersonAccel &aa, real_t γₖ, real_t old_γₖ) {
88 // TODO: Do we really want to flush every time γ changes?
89 aa.reinitialize(γₖ, old_γₖ);
90 }
91};
92
93} // namespace alpaqa
Anderson Acceleration.
std::string get_name() const
void initialize(crvec g₀, crvec r₀)
void compute(crvec gₖ, crvec rₖ, rvec xₖ_aa)
void resize(size_t n, size_t m)
void reinitialize(real_t γₖ, real_t old_γₖ)
Incremental QR factorization using modified Gram-Schmidt with reorthogonalization.
size_t ring_tail() const
Get the tail index of the circular buffer (points to one past the most recent element).
void resize(size_t n, size_t m)
Re-allocate storage for a problem with a different size.
void reset()
Reset all indices, clearing the Q and R matrices.
int m
Definition: test.py:41
int n
Definition: test.py:40
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< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
realmat mat
Default type for matrices.
Definition: vec.hpp:20
realvec vec
Default type for vectors.
Definition: vec.hpp:14
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
static void initialize(AndersonAccel &aa, crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀)
static bool apply(AndersonAccel &aa, crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ)
static bool update(AndersonAccel &aa, crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec grad_new, const Box &C, real_t γ_new)
static void changed_γ(AndersonAccel &aa, real_t γₖ, real_t old_γₖ)