alpaqa 0.0.1
Nonconvex constrained optimization
decl/lbfgs.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <alpaqa/util/box.hpp>
4#include <alpaqa/util/vec.hpp>
5
8
9namespace alpaqa {
10
11/// Parameters for the @ref LBFGS and @ref SpecializedLBFGS classes.
13 /// Length of the history to keep.
14 unsigned memory = 10;
15 struct {
16 real_t α = 1;
17 real_t ϵ = 0;
18 }
19 /// Parameters in the cautious BFGS update condition
20 /// @f[ \frac{y^\top s}{s^\top s} \ge \epsilon \| g \|^\alpha @f]
21 /// @see https://epubs.siam.org/doi/10.1137/S1052623499354242
23
25};
26
27/// Limited memory Broyden–Fletcher–Goldfarb–Shanno (L-BFGS) algorithm
28/// @ingroup grp_PANOCDirectionProviders
29class LBFGS {
30 public:
32
33 /// The sign of the vectors @f$ p @f$ passed to the @ref LBFGS::update
34 /// method.
35 enum class Sign {
36 Positive, ///< @f$ p \sim \nabla \psi(x) @f$
37 Negative, ///< @f$ p \sim -\nabla \psi(x) @f$
38 };
39
42
43 /// Check if the new vectors s and y allow for a valid BFGS update that
44 /// preserves the positive definiteness of the Hessian approximation.
45 static bool update_valid(LBFGSParams params, real_t yᵀs, real_t sᵀs,
46 real_t pᵀp);
47
48 /// Update the inverse Hessian approximation using the new vectors xₖ₊₁
49 /// and pₖ₊₁.
50 bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁,
51 Sign sign, bool forced = false);
52
53 /// Apply the inverse Hessian approximation to the given vector q.
54 template <class Vec>
55 bool apply(Vec &&q, real_t γ);
56
57 /// Apply the inverse Hessian approximation to the given vector q, applying
58 /// only the columns and rows of the Hessian in the index set J.
59 template <class Vec, class IndexVec>
60 bool apply(Vec &&q, real_t γ, const IndexVec &J);
61
62 /// Throw away the approximation and all previous vectors s and y.
63 void reset();
64 /// Re-allocate storage for a problem with a different size. Causes
65 /// a @ref reset.
66 void resize(size_t n);
67
68 /// Scale the stored y vectors by the given factor.
69 void scale_y(real_t factor);
70
71 std::string get_name() const { return "LBFGS"; }
72
73 const Params &get_params() const { return params; }
74
75 /// Get the size of the s and y vectors in the buffer.
76 size_t n() const { return sto.rows() - 1; }
77 /// Get the number of previous vectors s and y stored in the buffer.
78 size_t history() const { return sto.cols() / 2; }
79 /// Get the next index in the circular buffer of previous s and y vectors.
80 size_t succ(size_t i) const { return i + 1 < history() ? i + 1 : 0; }
81
82 auto s(size_t i) { return sto.col(2 * i).topRows(n()); }
83 auto s(size_t i) const { return sto.col(2 * i).topRows(n()); }
84 auto y(size_t i) { return sto.col(2 * i + 1).topRows(n()); }
85 auto y(size_t i) const { return sto.col(2 * i + 1).topRows(n()); }
86 real_t &ρ(size_t i) { return sto.coeffRef(n(), 2 * i); }
87 const real_t &ρ(size_t i) const { return sto.coeff(n(), 2 * i); }
88 real_t &α(size_t i) { return sto.coeffRef(n(), 2 * i + 1); }
89 const real_t &α(size_t i) const { return sto.coeff(n(), 2 * i + 1); }
90
91 private:
92 using storage_t = Eigen::Matrix<real_t, Eigen::Dynamic, Eigen::Dynamic>;
93
95 size_t idx = 0;
96 bool full = false;
98};
99
100template <>
104 PANOCDirection(const LBFGS &lbfgs) : lbfgs(lbfgs) {}
105 PANOCDirection(LBFGS &&lbfgs) : lbfgs(std::move(lbfgs)) {}
106
107 void initialize(crvec x₀, crvec x̂₀, crvec p₀,
108 crvec grad₀);
109 bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁,
110 crvec grad_new, const Box &C, real_t γ_new);
111 bool apply(crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ);
112 void changed_γ(real_t γₖ, real_t old_γₖ);
113 void reset();
114 std::string get_name() const;
115 LBFGSParams get_params() const;
116};
117
118} // namespace alpaqa
Limited memory Broyden–Fletcher–Goldfarb–Shanno (L-BFGS) algorithm.
Definition: decl/lbfgs.hpp:29
auto s(size_t i) const
Definition: decl/lbfgs.hpp:83
std::string get_name() const
Definition: decl/lbfgs.hpp:71
size_t succ(size_t i) const
Get the next index in the circular buffer of previous s and y vectors.
Definition: decl/lbfgs.hpp:80
auto y(size_t i) const
Definition: decl/lbfgs.hpp:85
bool apply(Vec &&q, real_t γ)
Apply the inverse Hessian approximation to the given vector q.
Definition: lbfgs.hpp:59
bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, Sign sign, bool forced=false)
Update the inverse Hessian approximation using the new vectors xₖ₊₁ and pₖ₊₁.
Definition: lbfgs.hpp:33
void resize(size_t n)
Re-allocate storage for a problem with a different size.
Definition: lbfgs.hpp:188
size_t history() const
Get the number of previous vectors s and y stored in the buffer.
Definition: decl/lbfgs.hpp:78
auto s(size_t i)
Definition: decl/lbfgs.hpp:82
Eigen::Matrix< real_t, Eigen::Dynamic, Eigen::Dynamic > storage_t
Definition: decl/lbfgs.hpp:92
real_t & α(size_t i)
Definition: decl/lbfgs.hpp:88
LBFGS(Params params)
Definition: decl/lbfgs.hpp:40
const Params & get_params() const
Definition: decl/lbfgs.hpp:73
static bool update_valid(LBFGSParams params, real_t yᵀs, real_t sᵀs, real_t pᵀp)
Check if the new vectors s and y allow for a valid BFGS update that preserves the positive definitene...
Definition: lbfgs.hpp:9
auto y(size_t i)
Definition: decl/lbfgs.hpp:84
size_t n() const
Get the size of the s and y vectors in the buffer.
Definition: decl/lbfgs.hpp:76
const real_t & α(size_t i) const
Definition: decl/lbfgs.hpp:89
void reset()
Throw away the approximation and all previous vectors s and y.
Definition: lbfgs.hpp:183
storage_t sto
Definition: decl/lbfgs.hpp:94
Sign
The sign of the vectors passed to the LBFGS::update method.
Definition: decl/lbfgs.hpp:35
void scale_y(real_t factor)
Scale the stored y vectors by the given factor.
Definition: lbfgs.hpp:195
real_t & ρ(size_t i)
Definition: decl/lbfgs.hpp:86
const real_t & ρ(size_t i) const
Definition: decl/lbfgs.hpp:87
LBFGS(Params params, size_t n)
Definition: decl/lbfgs.hpp:41
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
unsigned memory
Length of the history to keep.
Definition: decl/lbfgs.hpp:14
struct alpaqa::LBFGSParams::@0 cbfgs
Parameters in the cautious BFGS update condition.
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 LBFGS and SpecializedLBFGS classes.
Definition: decl/lbfgs.hpp:12
PANOCDirection(const LBFGSParams &params)
Definition: decl/lbfgs.hpp:103
PANOCDirection(const LBFGS &lbfgs)
Definition: decl/lbfgs.hpp:104
static bool update(DirectionProviderT &dp, crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec gradₖ₊₁, const Box &C, real_t γₖ₊₁)=delete
static bool apply(DirectionProviderT &dp, crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ)=delete
Apply the direction estimation in the current point.
static void changed_γ(DirectionProviderT &dp, real_t γₖ, real_t old_γₖ)=delete
static void initialize(DirectionProviderT &dp, crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀)=delete