Loading...
Searching...
No Matches
RgminStep.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
5#ifndef GPRIDGEIZATION_RGMIN_STEP_H_
6#define GPRIDGEIZATION_RGMIN_STEP_H_
7
8namespace gpr {
9namespace optim {
10class RgminStep;
11}
12} // namespace gpr
13
14#include <rgmin.h>
15
16#include <Eigen/Dense>
17#include <cmath>
18#include <cstddef>
19#include <cstdint>
20#include <vector>
21
22#include "../ml/RgminAbi.h"
23
24namespace gpr {
25namespace optim {
26
41class RgminStep {
42public:
49 enum class Objective : int { Unspecified = 0, Surrogate = 1, Exact = 2 };
50
57
60 static std::vector<PairRecord>*& pairTrace() noexcept
61 {
62 thread_local std::vector<PairRecord>* trace = nullptr;
63 return trace;
64 }
65
66 RgminStep() = default;
68 {
69 release();
70 }
71 RgminStep(const RgminStep&) = delete;
72 RgminStep& operator=(const RgminStep&) = delete;
73
76 void forget()
77 {
78 if (solver_ != nullptr) rgmin_solver_forget(solver_);
79 resetHistory();
80 }
81
82 [[nodiscard]] bool ready() const noexcept
83 {
84 return solver_ != nullptr;
85 }
86
88 [[nodiscard]] std::size_t pairCount() const noexcept
89 {
90 return solver_ != nullptr ? rgmin_solver_pair_count(solver_) : 0;
91 }
92
122 bool step(Eigen::VectorXd& x, const Eigen::VectorXd& gradient,
123 double max_move, double empty_scale,
125 {
126 if (x.size() <= 0 || gradient.size() != x.size()) return false;
127 if (!x.allFinite() || !gradient.allFinite()) return false;
128 ensure(static_cast<std::size_t>(x.size()));
129 if (solver_ == nullptr) return false;
130 const std::size_t n = static_cast<std::size_t>(x.size());
131
132 if (have_previous_ && previous_objective_ != objective) forget();
133
134 // The pair from the caller's own previous outer. Recording it
135 // here rather than letting the session evaluate for it is what
136 // keeps a frozen gradient from writing y = 0. A step that is
137 // not applied forgets the whole session: every stored pair is
138 // cleared, not only the pair just pushed, and the previous-point
139 // anchor is cleared with them. Otherwise a retry pushes the
140 // same (s, y) and two-loops a step the caller did not take.
141 const bool pushed_pair = have_previous_;
142 if (pushed_pair) {
143 const Eigen::VectorXd s_vec = x - previous_x_;
144 const Eigen::VectorXd y_vec = gradient - previous_g_;
145 rgmin_solver_push_pair(solver_, s_vec.data(), y_vec.data(), n);
146 if (auto* trace = pairTrace()) {
147 trace->push_back({previous_objective_, objective});
148 }
149 }
150
151 const auto refuse = [&]() {
152 if (pushed_pair) forget();
153 return false;
154 };
155
156 Eigen::VectorXd direction(x.size());
157 const rgmin_status_t st = rgmin_solver_search_direction(
158 solver_, gradient.data(), direction.data(), n);
159 if (st != RGMIN_SUCCESS || !direction.allFinite()) return refuse();
160
161 if (rgmin_solver_pair_count(solver_) == 0) direction *= empty_scale;
162
163 // norm() squares each entry, so a finite component near 1e308
164 // has an infinite length. The length itself is finite; the sum
165 // with x is what can overflow.
166 const double length = direction.stableNorm();
167 if (!std::isfinite(length) || !(length > 0.0)) return refuse();
168 if (max_move > 0.0 && length > max_move) {
169 direction *= max_move / length;
170 }
171
172 // Finite addends can still overflow. Refuse before x or the
173 // anchor is written, so the caller keeps the point it offered.
174 const Eigen::VectorXd stepped = x + direction;
175 if (!stepped.allFinite()) return refuse();
176
177 previous_x_ = x;
178 previous_g_ = gradient;
179 previous_objective_ = objective;
180 have_previous_ = true;
181 x = stepped;
182 return true;
183 }
184
185private:
186 void ensure(std::size_t n)
187 {
188 if (solver_ != nullptr && n_ == n) return;
189 release();
190 rgmin_control_t ctrl{};
191 ctrl.maxiter = 1;
192 ctrl.gtol = 0.0;
193 ctrl.istep = 1.0;
194 ctrl.memory = memory_;
196 solver_ = rgmin_solver_create(RGMIN_LBFGS, &ctrl, n);
197 // The caller bounds the Cartesian step by max_move, so clear the
198 // per-atom cap that rgmin installs on a Cartesian session by default.
199 rgmin_solver_set_atom_maxmove(solver_, 0.0);
200 n_ = n;
201 }
202
203 void release()
204 {
205 if (solver_ != nullptr) {
206 rgmin_solver_free(solver_);
207 solver_ = nullptr;
208 }
209 n_ = 0;
210 resetHistory();
211 }
212
213 void resetHistory()
214 {
215 have_previous_ = false;
216 previous_x_.resize(0);
217 previous_g_.resize(0);
218 previous_objective_ = Objective::Unspecified;
219 }
220
221 rgmin_solver_t* solver_ = nullptr;
222 std::size_t n_ = 0;
223 std::size_t memory_ = 20;
224 Eigen::VectorXd previous_x_;
225 Eigen::VectorXd previous_g_;
226 Objective previous_objective_ = Objective::Unspecified;
227 bool have_previous_ = false;
228};
229
230} // namespace optim
231} // namespace gpr
232
233#endif // GPRIDGEIZATION_RGMIN_STEP_H_
@ x
Definition Enums.h:28
std::size_t pairCount() const noexcept
Curvature pairs the session currently holds.
Definition RgminStep.h:88
RgminStep & operator=(const RgminStep &)=delete
bool ready() const noexcept
Definition RgminStep.h:82
static std::vector< PairRecord > *& pairTrace() noexcept
Definition RgminStep.h:60
RgminStep(const RgminStep &)=delete
bool step(Eigen::VectorXd &x, const Eigen::VectorXd &gradient, double max_move, double empty_scale, Objective objective=Objective::Unspecified)
Definition RgminStep.h:122
void requireCompatibleLibrary()
Definition RgminAbi.h:90
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16