Loading...
Searching...
No Matches
gpr::TrustRadius Class Reference

Trust radius enforcement for GP surrogate models. More...

#include <gpr/ml/TrustRadius.h>

Public Member Functions

 TrustRadius ()=default
void setTrainingData (const EigenMatrix &R_train)
 Store training positions for distance queries.
void setAtomsConfiguration (const AtomsConfiguration &atoms_config)
 Store atomic metadata for permutation-aware distance metrics.
void setDistanceMetric (DistanceMetricType metric)
 Select the atomic distance metric for trust checks.
double distanceToTraining (const Eigen::VectorXd &x_new) const
 Minimum Euclidean distance from x_new to any training point.
bool isWithinTrust (const Eigen::VectorXd &x_new, double radius) const
 Check whether x_new is within the trust radius of training data.
bool projectLineToTrustBoundary (const Eigen::VectorXd &x_current, const Eigen::VectorXd &x_trial, double radius, Eigen::VectorXd &x_projected) const
 Project a trial point along a line segment to the trust boundary.
double certifiedLineStep (const Eigen::VectorXd &x_from, const Eigen::VectorXd &x_to, double radius) const
 Largest certified fraction of the segment x_from -> x_to that stays within the trust radius, without bisection.
std::vector< int > kNearestIndices (const Eigen::VectorXd &x_new, int k) const
 Indices of the k training points nearest to x_new.
int nTraining () const

Detailed Description

Trust radius enforcement for GP surrogate models.

Stores training data positions and provides distance queries and displacement clipping relative to a trust radius boundary.

Definition at line 36 of file TrustRadius.h.

Constructor & Destructor Documentation

◆ TrustRadius()

gpr::TrustRadius::TrustRadius ( )
default

Member Function Documentation

◆ certifiedLineStep()

double gpr::TrustRadius::certifiedLineStep ( const Eigen::VectorXd & x_from,
const Eigen::VectorXd & x_to,
double radius ) const

Largest certified fraction of the segment x_from -> x_to that stays within the trust radius, without bisection.

Along x(t) = x_from + t * (x_to - x_from), the configured distance metric is Lipschitz in t: d(x(t)) <= d(x_from) + t * L(step), where L(step) is the EMD rate: the maximum over atom types T of the mean displacement norm within T (per-atom triangle inequality, grouped by type exactly as dist_emd) for the EMD metric, or ||step||_2 for the plain Euclidean fallback. Solving d(x(t*)) <= radius for the worst case gives t* = clamp((radius - d(x_from)) / L(step), 0, 1), which is a conservative (never unsafe) certificate: the true boundary may sit further along the segment, since a specific training-row assignment can only move less than the worst-case bound.

The certificate only bounds an INCREASE in distance along the segment: when x_from itself is already outside radius, t* evaluates to 0 rather than certifying any recovery. Callers must check distanceToTraining(x_from) <= radius themselves if they need that distinction.

Returns 0.0 for RMSD/MAX_1D_LOG (no such rate is established for those metrics), for non-finite radius/distances, or for mismatched x_from/x_to sizes.

Returns
Certified alpha in [0, 1].

Definition at line 169 of file TrustRadius.cpp.

172{
173 if (!(radius >= 0.0) || !std::isfinite(radius)) return 0.0;
174 if (x_from.size() != x_to.size() || x_from.size() == 0) return 0.0;
175
176 if (use_atomic_metric_ && has_atoms_config_ &&
177 metric_ != DistanceMetricType::EMD) {
178 // The Lipschitz rate above is derived for EMD (and the plain
179 // Euclidean fallback); RMSD/MAX_1D_LOG are not covered, so refuse
180 // to certify rather than ship an unverified bound.
181 return 0.0;
182 }
183
184 const double d_from = distanceToTraining(x_from);
185 if (!std::isfinite(d_from)) return 0.0;
186
187 const Eigen::VectorXd step = x_to - x_from;
188 const double rate = (use_atomic_metric_ && has_atoms_config_)
189 ? emdLipschitzRate(step)
190 : step.norm();
191 if (rate <= 0.0) {
192 return d_from <= radius ? 1.0 : 0.0;
193 }
194 return std::clamp((radius - d_from) / rate, 0.0, 1.0);
195}
double distanceToTraining(const Eigen::VectorXd &x_new) const
Minimum Euclidean distance from x_new to any training point.

◆ distanceToTraining()

double gpr::TrustRadius::distanceToTraining ( const Eigen::VectorXd & x_new) const

Minimum Euclidean distance from x_new to any training point.

Parameters
x_newQuery point
Returns
Minimum distance (infinity if no training data)

Definition at line 33 of file TrustRadius.cpp.

34{
35 const int n = static_cast<int>(R_train_.rows());
36 if (n == 0) {
37 return std::numeric_limits<double>::infinity();
38 }
39
40 if (use_atomic_metric_) {
41 if (!has_atoms_config_) {
42 return std::numeric_limits<double>::infinity();
43 }
44
45 Coord query(1, x_new.size());
46 query.row(0) = x_new.transpose();
47
48 FieldMatrixd dist;
49 aux::Distance distance;
50 switch (metric_) {
52 distance.dist_max1Dlog(query, R_train_, atoms_config_, dist);
53 break;
55 distance.dist_rmsd(query, R_train_, atoms_config_, dist);
56 break;
58 distance.dist_emd(query, R_train_, atoms_config_, dist);
59 break;
60 }
61 if (dist.size() == 0) {
62 return std::numeric_limits<double>::infinity();
63 }
64 return dist.minCoeff();
65 }
66
67 double min_dist = std::numeric_limits<double>::infinity();
68 for (int i = 0; i < n; ++i) {
69 double d = (R_train_.row(i).transpose() - x_new).norm();
70 if (d < min_dist) {
71 min_dist = d;
72 }
73 }
74 return min_dist;
75}
void dist_max1Dlog(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, gpr::FieldMatrixd &dist)
Give the maximum difference in the logarithmic inter-atomic distances between atomic configurations C...
Definition Distance.cpp:462
void dist_emd(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, gpr::FieldMatrixd &dist)
Calculates the Earth Mover's Distance between sets of configurations.
void dist_rmsd(const gpr::Coord &x1, const gpr::Coord &x2, const gpr::AtomsConfiguration &conf_info, gpr::FieldMatrixd &dist)
Calculates the aligned Root Mean Square Deviation (RMSD) between sets of configurations.
Definition Distance.cpp:410
FieldMatrixd Coord
Definition Macros.h:35
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition Macros.h:25

◆ isWithinTrust()

bool gpr::TrustRadius::isWithinTrust ( const Eigen::VectorXd & x_new,
double radius ) const

Check whether x_new is within the trust radius of training data.

Parameters
x_newQuery point
radiusTrust radius threshold
Returns
True if distanceToTraining(x_new) <= radius

Definition at line 197 of file TrustRadius.cpp.

199{
200 return distanceToTraining(x_new) <= radius;
201}

◆ kNearestIndices()

std::vector< int > gpr::TrustRadius::kNearestIndices ( const Eigen::VectorXd & x_new,
int k ) const

Indices of the k training points nearest to x_new.

Ranks every stored training row by the configured distance metric (EMD/RMSD/MAX_1D_LOG with an AtomsConfiguration, or the Euclidean vector norm otherwise) and returns the indices of the k smallest, sorted ascending so the result is a stable set. The result has min(k, nTraining()) entries; it is empty when there is no training data.

Parameters
x_newQuery point
kNeighbourhood size
Returns
Ascending-sorted row indices of the k nearest points

Definition at line 77 of file TrustRadius.cpp.

79{
80 const int n = static_cast<int>(R_train_.rows());
81 std::vector<int> result;
82 if (n == 0 || k <= 0) {
83 return result;
84 }
85
86 std::vector<double> dists(static_cast<size_t>(n),
87 std::numeric_limits<double>::infinity());
88 bool dists_filled = false;
89
90 if (use_atomic_metric_ && has_atoms_config_) {
91 Coord query(1, x_new.size());
92 query.row(0) = x_new.transpose();
93
94 FieldMatrixd dist;
95 aux::Distance distance;
96 switch (metric_) {
98 distance.dist_max1Dlog(query, R_train_, atoms_config_, dist);
99 break;
101 distance.dist_rmsd(query, R_train_, atoms_config_, dist);
102 break;
104 distance.dist_emd(query, R_train_, atoms_config_, dist);
105 break;
106 }
107 if (static_cast<int>(dist.size()) == n) {
108 for (int i = 0; i < n; ++i) {
109 dists[static_cast<size_t>(i)] = *(dist.data() + i);
110 }
111 dists_filled = true;
112 }
113 }
114
115 if (!dists_filled) {
116 // No atomic metric, or the distance backend returned no rows:
117 // rank by the Euclidean vector norm, mirroring distanceToTraining.
118 for (int i = 0; i < n; ++i) {
119 dists[static_cast<size_t>(i)] =
120 (R_train_.row(i).transpose() - x_new).norm();
121 }
122 }
123
124 result.resize(static_cast<size_t>(n));
125 for (int i = 0; i < n; ++i) {
126 result[static_cast<size_t>(i)] = i;
127 }
128 const int kk = std::min(k, n);
129 std::partial_sort(result.begin(), result.begin() + kk, result.end(),
130 [&](int a, int b) {
131 return dists[static_cast<size_t>(a)] <
132 dists[static_cast<size_t>(b)];
133 });
134 result.resize(static_cast<size_t>(kk));
135 // Sort ascending by index so the result is a stable set: callers
136 // compare it against a cached selection to decide whether the
137 // neighbourhood membership actually turned over.
138 std::sort(result.begin(), result.end());
139 return result;
140}

◆ nTraining()

int gpr::TrustRadius::nTraining ( ) const
inline

Definition at line 145 of file TrustRadius.h.

146 {
147 return static_cast<int>(R_train_.rows());
148 }

◆ projectLineToTrustBoundary()

bool gpr::TrustRadius::projectLineToTrustBoundary ( const Eigen::VectorXd & x_current,
const Eigen::VectorXd & x_trial,
double radius,
Eigen::VectorXd & x_projected ) const

Project a trial point along a line segment to the trust boundary.

The configured distance metric defines the metric ball. If x_trial is outside radius and x_current is inside radius, this returns the largest point on the line segment x_current + alpha * (x_trial - x_current), alpha in [0,1], that remains inside the trust set. For the EMD metric (and the plain Euclidean fallback with no atomic metric configured) alpha is the closed-form certifiedLineStep bound, evaluated once. For RMSD/MAX_1D_LOG, where that Lipschitz certificate is not established, this falls back to mantissa-width bisection.

Returns
true when x_projected was moved to a boundary point; false when no projection was needed or no trusted anchor exists.

Definition at line 203 of file TrustRadius.cpp.

207{
208 x_projected = x_trial;
209 if (!(radius >= 0.0) || !std::isfinite(radius)) return false;
210 if (x_current.size() != x_trial.size()) {
211 x_projected = x_current;
212 return false;
213 }
214
215 const double trial_distance = distanceToTraining(x_trial);
216 if (!std::isfinite(trial_distance) || trial_distance <= radius) {
217 return false;
218 }
219
220 const double current_distance = distanceToTraining(x_current);
221 if (!std::isfinite(current_distance) || current_distance > radius) {
222 x_projected = x_current;
223 return false;
224 }
225
226 const Eigen::VectorXd step = x_trial - x_current;
227 if (step.squaredNorm() == 0.0) {
228 x_projected = x_current;
229 return false;
230 }
231
232 auto project_by_bisection = [&](const double upper_alpha) {
233 double lo = 0.0;
234 double hi = upper_alpha;
235 for (int iter = 0; iter < std::numeric_limits<double>::digits; ++iter) {
236 const double alpha = 0.5 * (lo + hi);
237 const Eigen::VectorXd candidate = x_current + alpha * step;
238 const double candidate_distance = distanceToTraining(candidate);
239 if (std::isfinite(candidate_distance) &&
240 candidate_distance <= radius) {
241 lo = alpha;
242 } else {
243 hi = alpha;
244 }
245 }
246 x_projected = x_current + lo * step;
247 };
248
249 const bool rate_established =
250 !use_atomic_metric_ ||
251 (has_atoms_config_ && metric_ == DistanceMetricType::EMD);
252 if (rate_established) {
253 double alpha = certifiedLineStep(x_current, x_trial, radius);
254 if (alpha > 0.0) alpha = std::nextafter(alpha, 0.0);
255 x_projected = x_current + alpha * step;
256 const double projected_distance = distanceToTraining(x_projected);
257 if (!std::isfinite(projected_distance) || projected_distance > radius) {
258 project_by_bisection(alpha);
259 }
260 return true;
261 }
262
263 // RMSD/MAX_1D_LOG: no closed-form Lipschitz rate is established for
264 // these metrics, so fall back to bisection. The count is tied to the
265 // mantissa width of double precision and is therefore deterministic
266 // without a tunable tolerance.
267 project_by_bisection(1.0);
268 return true;
269}
double certifiedLineStep(const Eigen::VectorXd &x_from, const Eigen::VectorXd &x_to, double radius) const
Largest certified fraction of the segment x_from -> x_to that stays within the trust radius,...

◆ setAtomsConfiguration()

void gpr::TrustRadius::setAtomsConfiguration ( const AtomsConfiguration & atoms_config)

Store atomic metadata for permutation-aware distance metrics.

Definition at line 21 of file TrustRadius.cpp.

22{
23 atoms_config_ = atoms_config;
24 has_atoms_config_ = true;
25}

◆ setDistanceMetric()

void gpr::TrustRadius::setDistanceMetric ( DistanceMetricType metric)

Select the atomic distance metric for trust checks.

Calling this switches distanceToTraining away from Euclidean vector norm.

Definition at line 27 of file TrustRadius.cpp.

28{
29 metric_ = metric;
30 use_atomic_metric_ = true;
31}

◆ setTrainingData()

void gpr::TrustRadius::setTrainingData ( const EigenMatrix & R_train)

Store training positions for distance queries.

Parameters
R_trainMatrix of training positions (n_samples x dim). Each row is one training point.

Definition at line 16 of file TrustRadius.cpp.

17{
18 R_train_ = R_train;
19}

The documentation for this class was generated from the following files: