PANOC-ALM  quadratic-penalty
Nonconvex constrained optimization
decl/lbfgs.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <panoc-alm/util/box.hpp>
4 #include <panoc-alm/util/vec.hpp>
5 
8 
9 namespace pa {
10 
12 struct LBFGSParams {
14  unsigned memory = 10;
15  struct {
16  real_t α = 1;
17  real_t ϵ = 0;
18  }
23 
25 };
26 
29 class LBFGS {
30  public:
32 
35  enum class Sign {
36  Positive,
37  Negative,
38  };
39 
41  LBFGS(Params params, size_t n) : params(params) { resize(n); }
42 
45  static bool update_valid(LBFGSParams params, real_t yᵀs, real_t sᵀs,
46  real_t pᵀp);
47 
50  bool update(crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁,
51  Sign sign, bool forced = false);
52 
54  template <class Vec>
55  bool apply(Vec &&q, real_t γ);
56 
59  template <class Vec, class IndexVec>
60  bool apply(Vec &&q, real_t γ, const IndexVec &J);
61 
63  void reset();
66  void resize(size_t n);
67 
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 
76  size_t n() const { return sto.rows() - 1; }
78  size_t history() const { return sto.cols() / 2; }
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 
100 template <>
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 pa
pa::LBFGS::update_valid
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:8
pa::LBFGS::y
auto y(size_t i)
Definition: decl/lbfgs.hpp:84
pa::LBFGS::sto
storage_t sto
Definition: decl/lbfgs.hpp:94
pa::LBFGS::n
size_t n() const
Get the size of the s and y vectors in the buffer.
Definition: decl/lbfgs.hpp:76
pa::LBFGS::Sign::Negative
@ Negative
lbfgs-fwd.hpp
pa::PANOCDirection::apply
static bool apply(DirectionProviderT &dp, crvec xₖ, crvec x̂ₖ, crvec pₖ, real_t γ, rvec qₖ)=delete
Apply the direction estimation in the current point.
pa::LBFGS::idx
size_t idx
Definition: decl/lbfgs.hpp:95
pa::rvec
Eigen::Ref< vec > rvec
Default type for mutable references to vectors.
Definition: vec.hpp:16
pa::LBFGS::LBFGS
LBFGS(Params params)
Definition: decl/lbfgs.hpp:40
pa::LBFGS::ρ
real_t & ρ(size_t i)
Definition: decl/lbfgs.hpp:86
box.hpp
pa::LBFGS::α
real_t & α(size_t i)
Definition: decl/lbfgs.hpp:88
pa::LBFGS::α
const real_t & α(size_t i) const
Definition: decl/lbfgs.hpp:89
pa::PANOCDirection< LBFGS >::PANOCDirection
PANOCDirection(LBFGS &&lbfgs)
Definition: decl/lbfgs.hpp:105
pa::LBFGSParams::cbfgs
struct pa::LBFGSParams::@0 cbfgs
Parameters in the cautious BFGS update condition.
pa::LBFGSParams::rescale_when_γ_changes
bool rescale_when_γ_changes
Definition: decl/lbfgs.hpp:24
pa
Definition: alm.hpp:10
pa::LBFGS::s
auto s(size_t i)
Definition: decl/lbfgs.hpp:82
pa::LBFGS::get_name
std::string get_name() const
Definition: decl/lbfgs.hpp:71
pa::LBFGS::reset
void reset()
Throw away the approximation and all previous vectors s and y.
Definition: lbfgs.hpp:182
pa::PANOCDirection::initialize
static void initialize(DirectionProviderT &dp, crvec x₀, crvec x̂₀, crvec p₀, crvec grad₀)=delete
pa::LBFGS
Limited memory Broyden–Fletcher–Goldfarb–Shanno (L-BFGS) algorithm.
Definition: decl/lbfgs.hpp:29
pa::Box
Definition: box.hpp:7
pa::LBFGS::y
auto y(size_t i) const
Definition: decl/lbfgs.hpp:85
pa::LBFGS::update
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:32
pa::LBFGS::succ
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
pa::LBFGS::scale_y
void scale_y(real_t factor)
Scale the stored y vectors by the given factor.
Definition: lbfgs.hpp:192
pa::LBFGS::resize
void resize(size_t n)
Re-allocate storage for a problem with a different size.
Definition: lbfgs.hpp:187
pa::crvec
Eigen::Ref< const vec > crvec
Default type for immutable references to vectors.
Definition: vec.hpp:18
panoc-direction-update.hpp
pa::LBFGS::get_params
const Params & get_params() const
Definition: decl/lbfgs.hpp:73
panocpy.test.params
params
Definition: test.py:217
pa::LBFGSParams
Parameters for the LBFGS and SpecializedLBFGS classes.
Definition: decl/lbfgs.hpp:12
pa::PANOCDirection::update
static bool update(DirectionProviderT &dp, crvec xₖ, crvec xₖ₊₁, crvec pₖ, crvec pₖ₊₁, crvec gradₖ₊₁, const Box &C, real_t γₖ₊₁)=delete
panocpy.test.α
α
Definition: test.py:31
pa::LBFGS::s
auto s(size_t i) const
Definition: decl/lbfgs.hpp:83
panocpy.test.C
C
Definition: test.py:204
pa::LBFGS::storage_t
Eigen::Matrix< real_t, Eigen::Dynamic, Eigen::Dynamic > storage_t
Definition: decl/lbfgs.hpp:92
pa::LBFGS::params
Params params
Definition: decl/lbfgs.hpp:97
pa::LBFGS::ρ
const real_t & ρ(size_t i) const
Definition: decl/lbfgs.hpp:87
pa::PANOCDirection
Definition: panoc-direction-update.hpp:8
vec.hpp
pa::LBFGS::LBFGS
LBFGS(Params params, size_t n)
Definition: decl/lbfgs.hpp:41
pa::LBFGS::history
size_t history() const
Get the number of previous vectors s and y stored in the buffer.
Definition: decl/lbfgs.hpp:78
pa::PANOCDirection< LBFGS >::PANOCDirection
PANOCDirection(const LBFGSParams &params)
Definition: decl/lbfgs.hpp:103
pa::LBFGSParams::memory
unsigned memory
Length of the history to keep.
Definition: decl/lbfgs.hpp:14
pa::PANOCDirection::changed_γ
static void changed_γ(DirectionProviderT &dp, real_t γₖ, real_t old_γₖ)=delete
pa::PANOCDirection< LBFGS >::PANOCDirection
PANOCDirection(const LBFGS &lbfgs)
Definition: decl/lbfgs.hpp:104
pa::LBFGS::full
bool full
Definition: decl/lbfgs.hpp:96
pa::PANOCDirection< LBFGS >::lbfgs
LBFGS lbfgs
Definition: decl/lbfgs.hpp:102
pa::LBFGS::apply
bool apply(Vec &&q, real_t γ)
Apply the inverse Hessian approximation to the given vector q.
Definition: lbfgs.hpp:58
pa::LBFGS::Sign
Sign
The sign of the vectors passed to the LBFGS::update method.
Definition: decl/lbfgs.hpp:35
pa::real_t
double real_t
Default floating point type.
Definition: vec.hpp:8
pa::LBFGS::Sign::Positive
@ Positive