52 t.evaluate_residual(x);
53 t.evaluate_jacobian(x);
57 deque<vec> hist_ninja, hist_residual;
58 deque<double> hist_factor;
61 const unsigned max_hist;
62 const unsigned max_iteration;
67 explicit LBFGS(
const unsigned MH = 10,
const unsigned MI = 500,
const double AT = 1
E-12,
const double RT = 1
E-12)
73 template<Differentiable F>
int optimize(F& func, vec& x) {
76 hist_residual.clear();
80 const auto ref_magnitude = norm(x);
85 const auto residual = func.evaluate_residual(x);
87 if(0 == counter) ninja = solve(func.evaluate_jacobian(x), residual);
91 alpha.reserve(hist_ninja.size());
93 hist_residual.back() += residual;
95 hist_factor.emplace_back(dot(hist_ninja.back(), hist_residual.back()));
100 for(
auto J =
static_cast<int>(hist_factor.size()) - 1; J >= 0; --J) {
102 alpha.emplace_back(dot(hist_ninja[J], ninja) / hist_factor[J]);
104 ninja -= alpha.back() * hist_residual[J];
107 ninja *= dot(hist_residual.back(), hist_ninja.back()) / dot(hist_residual.back(), hist_residual.back());
109 for(
size_t I = 0, J = hist_factor.size() - 1; I < hist_factor.size(); ++I, --J) ninja += (alpha[J] - dot(hist_residual[I], ninja) / hist_factor[I]) * hist_ninja[I];
113 hist_ninja.emplace_back(-ninja);
114 hist_residual.emplace_back(-residual);
116 const auto error = norm(ninja);
117 const auto ref_error = error / ref_magnitude;
118 suanpan_debug(
"Local iteration error: {:.5E}.\n", ref_error);
123 if(counter > max_hist) {
124 hist_ninja.pop_front();
125 hist_residual.pop_front();
126 hist_factor.pop_front();
The LBFGS class defines a solver using LBFGS iteration method.
Definition LBFGS.hpp:56
int optimize(F &func, vec &x)
Definition LBFGS.hpp:73
LBFGS(const unsigned MH=10, const unsigned MI=500, const double AT=1E-12, const double RT=1E-12)
Definition LBFGS.hpp:67
#define suanpan_debug(...)
Definition suanPan.h:307
constexpr auto SUANPAN_SUCCESS
Definition suanPan.h:172
constexpr auto SUANPAN_FAIL
Definition suanPan.h:173