suanPan
Loading...
Searching...
No Matches
LBFGS.hpp
Go to the documentation of this file.
1/*******************************************************************************
2 * Copyright (C) 2017-2023 Theodore Chang
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
41#ifndef LBFGS_HPP
42#define LBFGS_HPP
43
44#include <suanPan.h>
45#include <deque>
46
47using std::deque;
48using std::vector;
49
50template<typename T> concept Differentiable = requires(T t, const vec& x)
51{
52 t.evaluate_residual(x);
53 t.evaluate_jacobian(x);
54};
55
56class LBFGS final {
57 deque<vec> hist_ninja, hist_residual;
58 deque<double> hist_factor;
59 vector<double> alpha;
60
61 const unsigned max_hist;
62 const unsigned max_iteration;
63 const double abs_tol;
64 const double rel_tol;
65
66public:
67 explicit LBFGS(const unsigned MH = 10, const unsigned MI = 500, const double AT = 1E-12, const double RT = 1E-12)
68 : max_hist(MH)
69 , max_iteration(MI)
70 , abs_tol(AT)
71 , rel_tol(RT) {}
72
73 template<Differentiable F> int optimize(F& func, vec& x) {
74 // clear container
75 hist_ninja.clear();
76 hist_residual.clear();
77 hist_factor.clear();
78
79 vec ninja;
80 const auto ref_magnitude = norm(x);
81
82 // iteration counter
83 auto counter = 0u;
84 while(true) {
85 const auto residual = func.evaluate_residual(x);
86
87 if(0 == counter) ninja = solve(func.evaluate_jacobian(x), residual);
88 else {
89 // clear temporary factor container
90 alpha.clear();
91 alpha.reserve(hist_ninja.size());
92 // commit current residual
93 hist_residual.back() += residual;
94 // commit current factor after obtaining ninja and residual
95 hist_factor.emplace_back(dot(hist_ninja.back(), hist_residual.back()));
96 // copy current residual to ninja
97 ninja = residual;
98 // perform two-step recursive loop
99 // right side loop
100 for(auto J = static_cast<int>(hist_factor.size()) - 1; J >= 0; --J) {
101 // compute and commit alpha
102 alpha.emplace_back(dot(hist_ninja[J], ninja) / hist_factor[J]);
103 // update ninja
104 ninja -= alpha.back() * hist_residual[J];
105 }
106 // apply the Hessian from the factorization in the first iteration
107 ninja *= dot(hist_residual.back(), hist_ninja.back()) / dot(hist_residual.back(), hist_residual.back());
108 // left side loop
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];
110 }
111
112 // commit current displacement increment
113 hist_ninja.emplace_back(-ninja);
114 hist_residual.emplace_back(-residual);
115
116 const auto error = norm(ninja);
117 const auto ref_error = error / ref_magnitude;
118 suanpan_debug("Local iteration error: {:.5E}.\n", ref_error);
119 if(error <= abs_tol && ref_error <= rel_tol) return SUANPAN_SUCCESS;
120 if(++counter > max_iteration) return SUANPAN_FAIL;
121
122 // check if the maximum record number is hit (LBFGS)
123 if(counter > max_hist) {
124 hist_ninja.pop_front();
125 hist_residual.pop_front();
126 hist_factor.pop_front();
127 }
128
129 x -= ninja;
130 }
131 }
132};
133
134#endif
135
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
Definition: LBFGS.hpp:50
#define suanpan_debug(...)
Definition: suanPan.h:295
constexpr auto SUANPAN_SUCCESS
Definition: suanPan.h:162
constexpr auto SUANPAN_FAIL
Definition: suanPan.h:163