8#include "LBFGSpp/Param.h"
9#include "LBFGSpp/BFGSMat.h"
10#include "LBFGSpp/LineSearchBacktracking.h"
11#include "LBFGSpp/LineSearchBracketing.h"
12#include "LBFGSpp/LineSearchNocedalWright.h"
13#include "LBFGSpp/LineSearchMoreThuente.h"
20template <
typename Scalar,
21 template <
class>
class LineSearch = LineSearchNocedalWright>
25 using Vector = Eigen::Matrix<Scalar, Eigen::Dynamic, 1>;
26 using Matrix = Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>;
27 using MapVec = Eigen::Map<Vector>;
30 BFGSMat<Scalar> m_bfgs;
40 inline void reset(
int n)
42 const int m = m_param.
m;
49 m_fx.resize(m_param.
past);
78 template <
typename Foo>
79 inline int minimize(Foo& f, Vector& x, Scalar& fx)
84 const int n = x.size();
88 const int fpast = m_param.
past;
92 m_gnorm = m_grad.norm();
106 m_drt.noalias() = -m_grad;
108 Scalar step = Scalar(1) / m_drt.norm();
110 constexpr Scalar eps = std::numeric_limits<Scalar>::epsilon();
112 Vector vecs(n), vecy(n);
122 m_gradp.noalias() = m_grad;
123 Scalar dg = m_grad.dot(m_drt);
124 const Scalar step_max = m_param.
max_step;
127 LineSearch<Scalar>::LineSearch(f, m_param, m_xp, m_drt, step_max, step, fx, m_grad, dg, x);
130 m_gnorm = m_grad.norm();
144 const Scalar fxd = m_fx[k % fpast];
145 if (k >= fpast && abs(fxd - fx) <= m_param.
delta * std::max(std::max(abs(fx), abs(fxd)), Scalar(1)))
148 m_fx[k % fpast] = fx;
159 vecs.noalias() = x - m_xp;
160 vecy.noalias() = m_grad - m_gradp;
161 if (vecs.dot(vecy) > eps * vecy.squaredNorm())
162 m_bfgs.add_correction(vecs, vecy);
165 m_bfgs.apply_Hv(m_grad, -Scalar(1), m_drt);
LBFGSSolver(const LBFGSParam< Scalar > ¶m)
const Vector & final_grad() const
int minimize(Foo &f, Vector &x, Scalar &fx)
Scalar final_grad_norm() const