Loading...
Searching...
No Matches
LBFGS.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5/*
6 * LBFGS.cpp
7 *
8 * Created on: 31 Mar 2021
9 * Author: Maxim Masterov
10 * Company: SURFSara
11 */
12
13#include "LBFGS.h"
14
15#include <rgmin.h>
16
17#include <cmath>
18#include <stdexcept>
19#include <utility>
20#include <vector>
21
22namespace gpr {
23namespace optim {
24
25void excludeNonPositiveCurvatureBlock(Eigen::VectorXd& s, Eigen::VectorXd& y,
26 Eigen::Index offset, Eigen::Index length)
27{
28 if (s.size() != y.size() || offset < 0 || length <= 0 ||
29 length > s.size() || offset > s.size() - length)
30 return;
31 const double curvature =
32 s.segment(offset, length).dot(y.segment(offset, length));
33 if (std::isfinite(curvature) && curvature <= 0.0) {
34 s.segment(offset, length).setZero();
35 y.segment(offset, length).setZero();
36 }
37}
38
39double barzilaiBorweinInverseScale(const Eigen::VectorXd& s,
40 const Eigen::VectorXd& y, double fallback)
41{
42 const double sy = s.dot(y);
43 const double yy = y.squaredNorm();
44 const double scale = (yy > 0.0 && std::isfinite(yy)) ? sy / yy : fallback;
45 if (sy > 0.0 && std::isfinite(sy) && scale > 0.0 && std::isfinite(scale)) {
46 return scale;
47 }
48 return fallback;
49}
50
51Eigen::VectorXd twoLoopRecursion(const std::deque<Eigen::VectorXd>& s_hist,
52 const std::deque<Eigen::VectorXd>& y_hist,
53 const std::deque<double>& rho_hist,
54 const Eigen::VectorXd& force, double H0)
55{
56 return twoLoopRecursion(
57 s_hist, y_hist, rho_hist, force,
58 [H0](const Eigen::VectorXd& v) -> Eigen::VectorXd { return H0 * v; });
59}
60
61namespace {
62
63extern "C" int32_t gprLbfgsApplyH0(void* user, const double* v, double* out,
64 std::size_t n)
65{
66 const auto* apply = static_cast<
67 const std::function<Eigen::VectorXd(const Eigen::VectorXd&)>*>(user);
68 const Eigen::Map<const Eigen::VectorXd> in(v, static_cast<Eigen::Index>(n));
69 const Eigen::VectorXd applied = (*apply)(in);
70 if (static_cast<std::size_t>(applied.size()) != n) return 1;
71 for (std::size_t i = 0; i < n; ++i)
72 out[i] = applied[static_cast<Eigen::Index>(i)];
73 return 0;
74}
75
76} // namespace
77
78Eigen::VectorXd twoLoopRecursion(
79 const std::deque<Eigen::VectorXd>& s_hist,
80 const std::deque<Eigen::VectorXd>& y_hist,
81 const std::deque<double>& rho_hist, const Eigen::VectorXd& force,
82 const std::function<Eigen::VectorXd(const Eigen::VectorXd&)>& apply_H0)
83{
84 const auto n = static_cast<std::size_t>(force.size());
85 auto raise_rgmin = []() {
86 const char* msg = rgmin_last_error();
87 const std::string text = msg != nullptr && msg[0] != '\0'
88 ? msg
89 : "rgmin L-BFGS direction failed";
90 throw std::runtime_error(text);
91 };
92 if (n == 0 || s_hist.size() != y_hist.size() ||
93 s_hist.size() != rho_hist.size()) {
94 throw std::runtime_error(
95 "rgmin L-BFGS direction: history does not match the force");
96 }
97 rgmin_lbfgs_pairs_t* pairs = rgmin_lbfgs_pairs_create(n, s_hist.size());
98 if (pairs == nullptr) raise_rgmin();
99 for (std::size_t k = 0; k < s_hist.size(); ++k) {
100 const auto len = static_cast<std::size_t>(s_hist[k].size());
101 if (static_cast<std::size_t>(y_hist[k].size()) != len) {
102 rgmin_lbfgs_pairs_free(pairs);
103 throw std::runtime_error(
104 "rgmin L-BFGS direction: a pair is not one length");
105 }
106 int32_t stored = 0;
107 // stored == 0 is the relative-curvature gate (s·y <= 1e-8
108 // ||s|| ||y||) or a rho that is not finite and positive. The
109 // pair stays out of the handle. A failed call throws
110 // rgmin_last_error() and does not run another two-loop.
111 if (rgmin_lbfgs_pairs_push_rho(pairs, s_hist[k].data(),
112 y_hist[k].data(), rho_hist[k], len,
113 &stored) != RGMIN_SUCCESS) {
114 const char* msg = rgmin_last_error();
115 const std::string text = msg != nullptr ? msg : "";
116 rgmin_lbfgs_pairs_free(pairs);
117 throw std::runtime_error(
118 text.empty() ? "rgmin_lbfgs_pairs_push_rho failed" : text);
119 }
120 if (stored == 0) continue;
121 }
122 Eigen::VectorXd direction(force.size());
123 const auto status = rgmin_lbfgs_direction(
124 pairs, force.data(), nullptr, gprLbfgsApplyH0,
125 const_cast<void*>(static_cast<const void*>(&apply_H0)),
126 direction.data(), n);
127 rgmin_lbfgs_pairs_free(pairs);
128 if (status != RGMIN_SUCCESS) {
129 const char* msg = rgmin_last_error();
130 const std::string text = msg != nullptr ? msg : "";
131 throw std::runtime_error(text.empty() ? "rgmin_lbfgs_direction failed"
132 : text);
133 }
134 return direction;
135}
136
137} /* namespace optim */
138} /* namespace gpr */
@ y
Definition Enums.h:28
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
int status
eon_relax_band_t eon_relax_surface_fn void * user
double barzilaiBorweinInverseScale(const Eigen::VectorXd &s, const Eigen::VectorXd &y, double fallback)
Scalar BB2 inverse-curvature seed for L-BFGS H0.
Definition LBFGS.cpp:39
void excludeNonPositiveCurvatureBlock(Eigen::VectorXd &s, Eigen::VectorXd &y, Eigen::Index offset, Eigen::Index length)
Definition LBFGS.cpp:25
Eigen::VectorXd twoLoopRecursion(const std::deque< Eigen::VectorXd > &s_hist, const std::deque< Eigen::VectorXd > &y_hist, const std::deque< double > &rho_hist, const Eigen::VectorXd &force, double H0)
Nocedal two-loop recursion (the LBFGS kernel).
Definition LBFGS.cpp:51
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16