Loading...
Searching...
No Matches
AcquisitionStrategy.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) gpridge developers
3// See LICENSE for details.
4
6
7#include <algorithm>
8#include <cmath>
9#include <limits>
10
11namespace gpr {
12namespace neb {
13
14namespace {
15
16double nonNegativeFinite(double value)
17{
18 return std::isfinite(value) && value > 0.0 ? value : 0.0;
19}
20
21double jointVarianceTrace(const ObservationVariance& variance)
22{
23 double trace = nonNegativeFinite(variance.energy);
24 for (Eigen::Index d = 0; d < variance.gradient.size(); ++d) {
25 trace += nonNegativeFinite(variance.gradient(d));
26 }
27 return trace;
28}
29
30double forceVarianceTrace(const ObservationVariance& variance)
31{
32 double trace = 0.0;
33 for (Eigen::Index d = 0; d < variance.gradient.size(); ++d) {
34 trace += nonNegativeFinite(variance.gradient(d));
35 }
36 return trace;
37}
38
39} // namespace
40
41double perpendicularSigma(const Eigen::VectorXd& gradient_variance,
42 const Eigen::VectorXd& tangent)
43{
44 // sigma_perp = sqrt(sum_d sigma_d^2 * (1 - tau_d^2))
45 double sum = 0.0;
46 for (Eigen::Index d = 0; d < gradient_variance.size(); ++d) {
47 double tau_d = (d < tangent.size()) ? tangent(d) : 0.0;
48 const double perpendicular_weight = std::max(0.0, 1.0 - tau_d * tau_d);
49 sum += nonNegativeFinite(gradient_variance(d)) * perpendicular_weight;
50 }
51 return std::sqrt(std::max(0.0, sum));
52}
53
54int selectNextImage(const std::vector<Eigen::VectorXd>& neb_forces,
55 const std::vector<ObservationVariance>& variances,
56 const std::vector<Eigen::VectorXd>& tangents,
57 const std::vector<bool>& evaluated,
58 AcquisitionType strategy, double kappa, std::mt19937* rng)
59{
60 int n = static_cast<int>(neb_forces.size());
61 int best_idx = -1;
62 double best_score = -std::numeric_limits<double>::infinity();
63
64 for (int i = 0; i < n; ++i) {
65 if (evaluated[i]) continue;
66
67 double score = 0.0;
68 double f_norm = neb_forces[i].norm();
69 const double joint_variance = jointVarianceTrace(variances[i]);
70 const double force_sigma =
71 perpendicularSigma(variances[i].gradient, tangents[i]);
72
73 switch (strategy) {
75 score = forceVarianceTrace(variances[i]);
76 break;
77
82 // Per-image fallback: score the joint energy+gradient
83 // variance trace. This function does not run chain-level
84 // TPE, PS-BAX, or IMSPE.
85 score = joint_variance;
86 break;
87
89 // EI resolves saddle energy, but its variance is a projection
90 // of the same joint posterior used by every acquisition.
91 score = nonNegativeFinite(variances[i].energy);
92 break;
93
95 score = f_norm;
96 break;
97
99 score = f_norm + kappa * force_sigma;
100 break;
101 }
102
104 // Sample force from GP posterior
105 if (rng && force_sigma > 0.0) {
106 std::normal_distribution<double> dist(f_norm, force_sigma);
107 score = dist(*rng);
108 } else {
109 score = f_norm;
110 }
111 break;
112 }
113 }
114
115 if (score > best_score) {
116 best_score = score;
117 best_idx = i;
118 }
119 }
120
121 // Fallback: pick first unevaluated
122 if (best_idx < 0) {
123 for (int i = 0; i < n; ++i) {
124 if (!evaluated[i]) {
125 best_idx = i;
126 break;
127 }
128 }
129 }
130
131 return best_idx;
132}
133
134std::vector<int> expandToTriplet(int center, int n_total)
135{
136 std::vector<int> triplet;
137 int lo = std::max(1, center - 1);
138 int hi = std::min(n_total - 2, center + 1);
139 for (int i = lo; i <= hi; ++i) {
140 triplet.push_back(i);
141 }
142 return triplet;
143}
144
145} // namespace neb
146} // namespace gpr
Acquisition strategies for GP-NEB OIE image selection.
The nudged elastic band: path, GP-NEB driver, configuration and result, acquisition,...
int selectNextImage(const std::vector< Eigen::VectorXd > &neb_forces, const std::vector< ObservationVariance > &variances, const std::vector< Eigen::VectorXd > &tangents, const std::vector< bool > &evaluated, AcquisitionType strategy, double kappa, std::mt19937 *rng)
Select the next unevaluated intermediate image by a per-image score.
std::vector< int > expandToTriplet(int center, int n_total)
Expand a single image index to a triplet {i-1, i, i+1}.
double perpendicularSigma(const Eigen::VectorXd &gradient_variance, const Eigen::VectorXd &tangent)
Compute perpendicular GP variance (variance orthogonal to path tangent).
AcquisitionType
Available acquisition strategies for image selection.
double variance(const Weights &w, const Matrix &covariance)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
Predictive variance of one joint energy-and-gradient observation.