-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlbfgs.cpp
288 lines (249 loc) · 6.3 KB
/
lbfgs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain this notice you
* can do whatever you want with this stuff. If we meet some day, and you think
* this stuff is worth it, you can buy me a beer in return Jacob Stevenson
* ----------------------------------------------------------------------------
*/
#include "lbfgs.h"
#include <vector>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <assert.h>
using namespace LBFGS_ns;
using std::vector;
using std::cout;
/**
* compute the dot product of two vectors
*/
double vecdot(std::vector<double> const v1, std::vector<double> const v2)
{
assert(v1.size() == v2.size());
size_t i;
double dot = 0.;
for (i=0; i<v1.size(); ++i) {
dot += v1[i] * v2[i];
}
return dot;
}
/**
* compute the L2 norm of a vector
*/
double vecnorm(std::vector<double> const v)
{
return sqrt(vecdot(v, v));
}
LBFGS::LBFGS(
double (*func)(double *, double *, size_t),
double const * x0,
size_t N,
int M
//double tol,
//double maxstep,
//double max_f_rise,
//double H0,
//int maxiter
)
:
func_f_grad_(func),
M_(M),
tol_(1e-4),
maxstep_(0.2),
max_f_rise_(1e-4),
maxiter_(1000),
iprint_(-1),
iter_number_(0),
nfev_(0),
H0_(0.1),
k_(0)
{
// set the precision of the printing
cout << std::setprecision(12);
// allocate arrays
x_ = std::vector<double>(N);
g_ = std::vector<double>(N);
y_ = std::vector<vector<double> >(M_, vector<double>(N));
s_ = std::vector<vector<double> >(M_, vector<double>(N));
rho_ = std::vector<double>(M_);
step_ = std::vector<double>(N);
for (size_t j2 = 0; j2 < N; ++j2){
x_[j2] = x0[j2];
}
compute_func_gradient(x_, f_, g_);
rms_ = vecnorm(g_) / sqrt(N);
}
/**
* Do one iteration iteration of the optimization algorithm
*/
void LBFGS::one_iteration()
{
std::vector<double> x_old = x_;
std::vector<double> g_old = g_;
compute_lbfgs_step();
double stepsize = backtracking_linesearch();
update_memory(x_old, g_old, x_, g_);
if ((iprint_ > 0) && (iter_number_ % iprint_ == 0)){
cout << "lbgs: " << iter_number_
<< " f " << f_
<< " rms " << rms_
<< " stepsize " << stepsize << "\n";
}
iter_number_ += 1;
}
void LBFGS::run()
{
// iterate until the stop criterion is satisfied or maximum number of
// iterations is reached
while (iter_number_ < maxiter_)
{
if (stop_criterion_satisfied()){
break;
}
one_iteration();
}
}
void LBFGS::update_memory(
std::vector<double> & xold,
std::vector<double> & gold,
std::vector<double> & xnew,
std::vector<double> & gnew
)
{
// update the lbfgs memory
// This updates s_, y_, rho_, and H0_, and k_
int klocal = k_ % M_;
for (size_t j2 = 0; j2 < x_.size(); ++j2){
y_[klocal][j2] = gnew[j2] - gold[j2];
s_[klocal][j2] = xnew[j2] - xold[j2];
}
double ys = vecdot(y_[klocal], s_[klocal]);
if (ys == 0.) {
// should print a warning here
cout << "warning: resetting YS to 1.\n";
ys = 1.;
}
rho_[klocal] = 1. / ys;
double yy = vecdot(y_[klocal], y_[klocal]);
if (yy == 0.) {
// should print a warning here
cout << "warning: resetting YY to 1.\n";
yy = 1.;
}
H0_ = ys / yy;
// cout << " setting H0 " << H0_
// << " ys " << ys
// << " yy " << yy
// << " rho[i] " << rho_[klocal]
// << "\n";
// increment k
k_ += 1;
}
void LBFGS::compute_lbfgs_step()
{
if (k_ == 0){
double gnorm = vecnorm(g_);
if (gnorm > 1.) gnorm = 1. / gnorm;
for (size_t j2 = 0; j2 < x_.size(); ++j2){
step_[j2] = - gnorm * H0_ * g_[j2];
}
return;
}
step_ = g_;
int jmin = std::max(0, k_ - M_);
int jmax = k_;
int i;
double beta;
vector<double> alpha(M_);
// loop backwards through the memory
for (int j = jmax - 1; j >= jmin; --j){
i = j % M_;
//cout << " i " << i << " j " << j << "\n";
alpha[i] = rho_[i] * vecdot(s_[i], step_);
for (size_t j2 = 0; j2 < step_.size(); ++j2){
step_[j2] -= alpha[i] * y_[i][j2];
}
}
// scale the step size by H0
for (size_t j2 = 0; j2 < step_.size(); ++j2){
step_[j2] *= H0_;
}
// loop forwards through the memory
for (int j = jmin; j < jmax; ++j){
i = j % M_;
//cout << " i " << i << " j " << j << "\n";
beta = rho_[i] * vecdot(y_[i], step_);
for (size_t j2 = 0; j2 < step_.size(); ++j2){
step_[j2] += s_[i][j2] * (alpha[i] - beta);
}
}
// invert the step to point downhill
for (size_t j2 = 0; j2 < x_.size(); ++j2){
step_[j2] *= -1;
}
}
double LBFGS::backtracking_linesearch()
{
vector<double> xnew(x_.size());
vector<double> gnew(x_.size());
double fnew;
// if the step is pointing uphill, invert it
if (vecdot(step_, g_) > 0.){
cout << "warning: step direction was uphill. inverting\n";
for (size_t j2 = 0; j2 < step_.size(); ++j2){
step_[j2] *= -1;
}
}
double factor = 1.;
double stepsize = vecnorm(step_);
// make sure the step is no larger than maxstep_
if (factor * stepsize > maxstep_){
factor = maxstep_ / stepsize;
}
int nred;
int nred_max = 10;
for (nred = 0; nred < nred_max; ++nred){
for (size_t j2 = 0; j2 < xnew.size(); ++j2){
xnew[j2] = x_[j2] + factor * step_[j2];
}
compute_func_gradient(xnew, fnew, gnew);
double df = fnew - f_;
if (df < max_f_rise_){
break;
} else {
factor /= 10.;
cout
<< "function increased: " << df
<< " reducing step size to " << factor * stepsize
<< " H0 " << H0_ << "\n";
}
}
if (nred >= nred_max){
// possibly raise an error here
cout << "warning: the line search backtracked too many times\n";
}
x_ = xnew;
g_ = gnew;
f_ = fnew;
rms_ = vecnorm(gnew) / sqrt(gnew.size());
return stepsize * factor;
}
bool LBFGS::stop_criterion_satisfied()
{
return rms_ <= tol_;
}
void LBFGS::compute_func_gradient(std::vector<double> & x, double & func,
std::vector<double> & gradient)
{
nfev_ += 1;
func = (*func_f_grad_)(&x[0], &gradient[0], x.size());
}
void LBFGS::set_H0(double H0)
{
if (iter_number_ > 0){
cout << "warning: setting H0 after the first iteration.\n";
}
H0_ = H0;
}