GPRidge v1.0.0
GPR-accelerated saddle point searches on potential energy surfaces
☾
Toggle main menu visibility
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
8
namespace
gpr
{
9
namespace
optim
{
10
class
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
24
namespace
gpr
{
25
namespace
optim
{
26
41
class
RgminStep
{
42
public
:
49
enum class
Objective
:
int
{
Unspecified
= 0,
Surrogate
= 1,
Exact
= 2 };
50
53
struct
PairRecord
{
54
Objective
from
;
55
Objective
to
;
56
};
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
;
67
~RgminStep
()
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,
124
Objective
objective =
Objective::Unspecified
)
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
185
private
:
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_;
195
::gpr::rgmin_abi::requireCompatibleLibrary
();
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
@ x
Definition
Enums.h:28
RgminAbi.h
gpr::optim::RgminStep
Definition
RgminStep.h:41
gpr::optim::RgminStep::pairCount
std::size_t pairCount() const noexcept
Curvature pairs the session currently holds.
Definition
RgminStep.h:88
gpr::optim::RgminStep::operator=
RgminStep & operator=(const RgminStep &)=delete
gpr::optim::RgminStep::~RgminStep
~RgminStep()
Definition
RgminStep.h:67
gpr::optim::RgminStep::ready
bool ready() const noexcept
Definition
RgminStep.h:82
gpr::optim::RgminStep::pairTrace
static std::vector< PairRecord > *& pairTrace() noexcept
Definition
RgminStep.h:60
gpr::optim::RgminStep::RgminStep
RgminStep()=default
gpr::optim::RgminStep::Objective
Objective
Definition
RgminStep.h:49
gpr::optim::RgminStep::Objective::Exact
@ Exact
Definition
RgminStep.h:49
gpr::optim::RgminStep::Objective::Unspecified
@ Unspecified
Definition
RgminStep.h:49
gpr::optim::RgminStep::Objective::Surrogate
@ Surrogate
Definition
RgminStep.h:49
gpr::optim::RgminStep::RgminStep
RgminStep(const RgminStep &)=delete
gpr::optim::RgminStep::step
bool step(Eigen::VectorXd &x, const Eigen::VectorXd &gradient, double max_move, double empty_scale, Objective objective=Objective::Unspecified)
Definition
RgminStep.h:122
gpr::optim::RgminStep::forget
void forget()
Definition
RgminStep.h:76
gpr::optim
Definition
ConsistentForceModel.h:13
gpr::rgmin_abi::requireCompatibleLibrary
void requireCompatibleLibrary()
Definition
RgminAbi.h:90
gpr
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition
Macros.h:16
gpr::optim::RgminStep::PairRecord
Definition
RgminStep.h:53
gpr::optim::RgminStep::PairRecord::from
Objective from
Definition
RgminStep.h:54
gpr::optim::RgminStep::PairRecord::to
Objective to
Definition
RgminStep.h:55
gpr
optimization
RgminStep.h
Generated by
1.17.0
Generated by
Doxygen 1.17.0
Analytics by
Antics
provided by
TurtleTech ehf