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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421 | /*************************************************************************
*
* Project
* _____ _____ __ __ _____
* / ____| __ \| \/ | __ \
* ___ _ __ ___ _ __ | | __| |__) | \ / | |__) |
* / _ \| '_ \ / _ \ '_ \| | |_ | ___/| |\/| | ___/
*| (_) | |_) | __/ | | | |__| | | | | | | |
* \___/| .__/ \___|_| |_|\_____|_| |_| |_|_|
* | |
* |_|
*
* Copyright (C) Akiel Aries, <akiel@akiel.org>, et al.
*
* This software is licensed as described in the file LICENSE, which
* you should have received as part of this distribution. The terms
* among other details are referenced in the official documentation
* seen here : https://akielaries.github.io/openGPMP/ along with
* important files seen in this project.
*
* You may opt to use, copy, modify, merge, publish, distribute
* and/or sell copies of the Software, and permit persons to whom
* the Software is furnished to do so, under the terms of the
* LICENSE file.
*
*
*
* This software is distributed on an AS IS basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied.
*
************************************************************************/
#include <cmath>
#include <functional>
#include <iostream>
#include <numeric>
#include <openGPMP/optim/quasi.hpp>
#include <tuple>
#include <vector>
std::vector<double> gpmp::optim::QuasiNewton::vector_subtraction(
const std::vector<double> &a,
const std::vector<double> &b) const {
if (a.size() != b.size()) {
throw std::invalid_argument(
"Error: Vector dimensions do not match for subtraction.");
}
std::vector<double> result;
result.reserve(a.size());
for (size_t i = 0; i < a.size(); ++i) {
result.push_back(a[i] - b[i]);
}
return result;
}
std::vector<double> gpmp::optim::QuasiNewton::bhhh_optimize(
const std::function<double(const std::vector<double> &)> &func,
const std::vector<double> &initial_point,
double tolerance,
size_t max_iterations) {
std::vector<double> current_point = initial_point;
size_t n = initial_point.size();
for (size_t iteration = 0; iteration < max_iterations; ++iteration) {
// Calculate the gradient
std::vector<double> gradient =
calculate_gradient(func, current_point, 1e-6);
// Check convergence
double gradient_norm = 0.0;
for (size_t i = 0; i < n; ++i) {
gradient_norm += gradient[i] * gradient[i];
}
gradient_norm = std::sqrt(gradient_norm);
if (gradient_norm < tolerance) {
std::cout << "Converged after " << iteration << " iterations."
<< std::endl;
return current_point;
}
// Calculate the BHHH matrix
std::vector<std::vector<double>> bhhh_matrix =
calculate_bhhh_matrix(gradient);
// Update the current point
current_point = update_point(current_point, gradient, bhhh_matrix);
}
std::cout << "Reached maximum iterations without convergence." << std::endl;
return current_point;
}
std::vector<double> gpmp::optim::QuasiNewton::calculate_gradient(
const std::function<double(const std::vector<double> &)> &func,
const std::vector<double> &point,
double epsilon) {
size_t n = point.size();
std::vector<double> gradient(n);
for (size_t i = 0; i < n; ++i) {
std::vector<double> perturbed_point = point;
perturbed_point[i] += epsilon;
double perturbed_value = func(perturbed_point);
double original_value = func(point);
gradient[i] = (perturbed_value - original_value) / epsilon;
}
return gradient;
}
std::vector<std::vector<double>>
gpmp::optim::QuasiNewton::calculate_bhhh_matrix(
const std::vector<double> &gradient) {
size_t n = gradient.size();
std::vector<std::vector<double>> bhhh_matrix(n, std::vector<double>(n));
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) {
bhhh_matrix[i][j] = gradient[i] * gradient[j];
}
}
return bhhh_matrix;
}
std::vector<double> gpmp::optim::QuasiNewton::update_point(
const std::vector<double> ¤t_point,
const std::vector<double> &gradient,
const std::vector<std::vector<double>> &bhhh_matrix) {
size_t n = current_point.size();
std::vector<double> updated_point(n);
for (size_t i = 0; i < n; ++i) {
updated_point[i] = current_point[i] - gradient[i] / bhhh_matrix[i][i];
}
return updated_point;
}
std::vector<double> gpmp::optim::QuasiNewton::bfgs_optimize(
const std::function<double(const std::vector<double> &)> &func,
const std::vector<double> &initial_point,
double tolerance,
size_t max_iterations) {
std::vector<double> current_point = initial_point;
size_t n = initial_point.size();
// Initialize Hessian approximation as the identity matrix
std::vector<std::vector<double>> hessian_inverse(
n,
std::vector<double>(n, 0.0));
for (size_t i = 0; i < n; ++i) {
hessian_inverse[i][i] = 1.0;
}
for (size_t iteration = 0; iteration < max_iterations; ++iteration) {
// Calculate the gradient
std::vector<double> gradient =
calculate_gradient(func, current_point, 1e-6);
// Check convergence
double gradient_norm = 0.0;
for (size_t i = 0; i < n; ++i) {
gradient_norm += gradient[i] * gradient[i];
}
gradient_norm = std::sqrt(gradient_norm);
if (gradient_norm < tolerance) {
std::cout << "Converged after " << iteration << " iterations."
<< std::endl;
return current_point;
}
// Calculate search direction
std::vector<double> search_direction =
calculate_search_direction(gradient, hessian_inverse);
// Perform line search to find the step size
double step_size = line_search(func, current_point, search_direction);
// Update the current point
std::vector<double> next_point =
update_point(current_point, search_direction, step_size);
// Update the Hessian approximation
std::vector<double> gradient_difference =
calculate_gradient_difference(next_point, current_point, gradient);
hessian_inverse = update_hessian_inverse(hessian_inverse,
gradient_difference,
search_direction);
// Move to the next iteration
current_point = next_point;
}
std::cout << "Reached maximum iterations without convergence." << std::endl;
return current_point;
}
std::vector<double> gpmp::optim::QuasiNewton::calculate_search_direction(
const std::vector<double> &gradient,
const std::vector<std::vector<double>> &hessian_inverse) {
size_t n = gradient.size();
std::vector<double> search_direction(n);
for (size_t i = 0; i < n; ++i) {
search_direction[i] = 0.0;
for (size_t j = 0; j < n; ++j) {
search_direction[i] -= hessian_inverse[i][j] * gradient[j];
}
}
return search_direction;
}
double gpmp::optim::QuasiNewton::line_search(
const std::function<double(const std::vector<double> &)> &func,
const std::vector<double> ¤t_point,
const std::vector<double> &search_direction) {
const double alpha = 0.001; // Step size multiplier
const double beta = 0.5; // Factor for reducing the step size
const int maxIterations = 100; // Maximum number of iterations
const double minStepSize = 1e-6; // Minimum step size
double step_size = 1.0; // Initial step size
std::vector<double> updated_point = current_point;<--- Variable 'updated_point' is assigned a value that is never used.
// Evaluate the objective function at the current point
double f_current = func(current_point);
// Calculate the directional derivative (gradient dot search_direction)
double directional_derivative =
dot_product(calculate_gradient(func, current_point, 1e-6),
search_direction);
int iteration = 0;
while (step_size > minStepSize && iteration < maxIterations) {
updated_point =
update_point(current_point, search_direction, step_size);
double f_updated = func(updated_point);
if (f_updated <=
f_current + alpha * step_size * directional_derivative) {
break; // Stop if Armijo condition is satisfied
}
step_size *= beta; // Reduce the step size
++iteration;
}
return step_size;
}
std::vector<double> gpmp::optim::QuasiNewton::update_point(
const std::vector<double> ¤t_point,
const std::vector<double> &search_direction,
double step_size) {
size_t n = current_point.size();
std::vector<double> updated_point(n);
for (size_t i = 0; i < n; ++i) {
updated_point[i] = current_point[i] + step_size * search_direction[i];
}
return updated_point;
}
std::vector<double> gpmp::optim::QuasiNewton::calculate_gradient_difference(
const std::vector<double> &next_point,
const std::vector<double> ¤t_point,
const std::vector<double> &gradient) {
size_t n = next_point.size();
std::vector<double> gradient_difference(n);
for (size_t i = 0; i < n; ++i) {
gradient_difference[i] =
gradient[i] * (next_point[i] - current_point[i]);
}
return gradient_difference;
}
std::vector<std::vector<double>>
gpmp::optim::QuasiNewton::update_hessian_inverse(
const std::vector<std::vector<double>> &hessian_inverse,
const std::vector<double> &gradient_difference,
const std::vector<double> &search_direction) {
size_t n = hessian_inverse.size();
std::vector<std::vector<double>> updated_hessian_inverse(
n,
std::vector<double>(n));
// Update Hessian using BFGS update formula
double rho = dot_product(gradient_difference, search_direction);
for (size_t i = 0; i < n; ++i) {
for (size_t j = 0; j < n; ++j) {
updated_hessian_inverse[i][j] =
hessian_inverse[i][j] +
rho * gradient_difference[i] * gradient_difference[j];
}
}
return updated_hessian_inverse;
}
double gpmp::optim::QuasiNewton::dot_product(const std::vector<double> &a,
const std::vector<double> &b) {
// Ensure vectors have the same size
if (a.size() != b.size()) {
throw std::invalid_argument(
"Vectors must have the same size for dot product.");
}
double result = 0.0;
for (size_t i = 0; i < a.size(); ++i) {
result += a[i] * b[i];
}
return result;
}
std::tuple<std::vector<double>, double>
gpmp::optim::QuasiNewton::lbfgs_optimize(
const std::function<double(const std::vector<double> &)> &f,
const std::vector<double> &initial_point,
double tolerance,
size_t max_iterations,
size_t memory_size) {
const double eps = 1e-8;
size_t n = initial_point.size();
std::vector<double> x = initial_point;
std::vector<double> g(n); // Gradient vector
std::vector<std::vector<double>> s(memory_size,
std::vector<double>(n)); // s vectors
std::vector<std::vector<double>> y(memory_size,
std::vector<double>(n)); // y vectors
std::vector<double> rho(memory_size); // rho values
// Evaluate the objective function and gradient at initial_point
double fx = f(x);
// Calculate gradient at initial_point
// Gradient calculation logic to be implemented
// Assign gradient to 'g'
for (size_t iter = 0; iter < max_iterations; ++iter) {
// Check for convergence
double norm_grad = 0.0;
for (size_t i = 0; i < n; ++i) {
norm_grad += g[i] * g[i];
}
norm_grad = sqrt(norm_grad);
if (norm_grad < tolerance) {
break;
}
// Compute search direction (use initial guess)
std::vector<double> d = g;
// L-BFGS two-loop recursion
size_t start = std::min(iter, memory_size);
// for (size_t i = start - 1; i >= 0; --i) {
for (size_t i = start; i > 0; --i) {
rho[i] = 1.0 /
inner_product(s[i].begin(), s[i].end(), y[i].begin(), 0.0);
double alpha =
rho[i] *
inner_product(s[i].begin(), s[i].end(), d.begin(), 0.0);
for (size_t j = 0; j < n; ++j) {
d[j] -= alpha * y[i][j];
}
}
// Perform scaling
for (size_t i = 0; i < n; ++i) {
d[i] *= rho[i];
}
// Compute gradient of the objective function along the search direction
// Gradient calculation logic to be implemented
// Assign gradient to 'dg'
double dg = inner_product(d.begin(), d.end(), g.begin(), 0.0);
// Limit curvature
if (dg > 0) {
break;
}
// Line search
double step_size = 1.0;
std::vector<double> x_new = x;
for (size_t i = 0; i < n; ++i) {
x_new[i] += step_size * d[i];
}
double fx_new = f(x_new);
if (fx_new < fx + eps * step_size * dg) {
// Update x
x = x_new;
fx = fx_new;
// Evaluate gradient at new point
// Gradient calculation logic to be implemented
// Assign gradient to 'g'
// Update s and y
for (size_t i = 0; i < n; ++i) {
s[iter % memory_size][i] = x_new[i] - x[i];
y[iter % memory_size][i] = g[i] - d[i];
}
}
}
return std::make_tuple(x, fx);
}
|