Loading...
Searching...
No Matches
ModeRefitPolicy.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 GPR_ML_MODE_REFIT_POLICY_H_
6#define GPR_ML_MODE_REFIT_POLICY_H_
7
8#include <Eigen/Core>
9#include <algorithm>
10#include <cmath>
11#include <numeric>
12#include <vector>
13
16
17namespace gpr {
18
22public:
23 std::vector<bool> select(const Coord& positions,
24 const AtomsConfiguration& configuration,
25 const FieldMatrixd& lengths,
26 const Eigen::VectorXd& mode, double force_sd,
27 double force_error, double force_tolerance,
28 bool require_parameter_reduction = true)
29 {
30 const double norm = mode.norm();
31 if (positions.rows() != 1 || positions.cols() != mode.size() ||
32 positions.cols() != 3 * configuration.atoms_mov.type.size() ||
33 !positions.allFinite() || !mode.allFinite() || !(norm > 0.0) ||
34 !std::isfinite(norm) || lengths.size() == 0 ||
35 (require_parameter_reduction && lengths.size() == 1) ||
36 lengths.size() != configuration.n_pt || !lengths.allFinite() ||
37 !(lengths.minCoeff() > 0.0) || !(force_tolerance > 0.0) ||
38 !std::isfinite(force_tolerance) || !std::isfinite(force_sd) ||
39 !std::isfinite(force_error) || force_sd < 0.0 ||
40 force_error < 0.0 || force_sd >= force_tolerance ||
41 force_error >= force_tolerance) {
42 reset();
43 return {};
44 }
45 const Eigen::VectorXd direction = mode / norm;
46 const bool changed = previous_mode_.size() == direction.size() &&
47 std::abs(previous_mode_.dot(direction)) < 0.9;
48 previous_mode_ = direction;
49 if (changed || restricted_refits_ >= 7) {
50 restricted_refits_ = 0;
51 return {};
52 }
53
54 Eigen::VectorXd features, derivative;
55 std::vector<int> pairtypes;
57 positions, configuration, direction, features, derivative,
58 pairtypes);
59 Eigen::VectorXd scores = Eigen::VectorXd::Zero(lengths.size());
60 for (Eigen::Index i = 0; i < derivative.size(); ++i) {
61 const auto type = pairtypes[static_cast<std::size_t>(i)];
62 const double scaled = derivative(i) / lengths.data()[type];
63 scores(type) += scaled * scaled;
64 }
65 const double total = scores.sum();
66 if (!scores.allFinite() || !(total > 0.0) || !std::isfinite(total)) {
67 reset();
68 return {};
69 }
70 std::vector<Eigen::Index> order(
71 static_cast<std::size_t>(scores.size()));
72 std::iota(order.begin(), order.end(), 0);
73 std::stable_sort(order.begin(), order.end(),
74 [&](auto a, auto b) { return scores(a) > scores(b); });
75 std::vector<bool> selected(order.size(), false);
76 double covered = 0.0;
77 for (const auto index: order) {
78 selected[static_cast<std::size_t>(index)] = true;
79 covered += scores(index);
80 if (covered >= 0.99 * total) break;
81 }
82 // A conditional posterior still factors only new observations when
83 // every length is active. A sparse full-fit mask needs a reduction.
84 if (require_parameter_reduction &&
85 std::all_of(selected.begin(), selected.end(),
86 [](bool active) { return active; })) {
87 restricted_refits_ = 0;
88 return {};
89 }
90 ++restricted_refits_;
91 return selected;
92 }
93
94 void reset()
95 {
96 previous_mode_.resize(0);
97 restricted_refits_ = 0;
98 }
99
100private:
101 Eigen::VectorXd previous_mode_;
102 int restricted_refits_ = 0;
103};
104
105} // namespace gpr
106
107#endif
static void computeDirectionalFeatures(const Coord &x, const AtomsConfiguration &conf_info, const Eigen::VectorXd &direction, Eigen::VectorXd &phi, Eigen::VectorXd &rates, std::vector< int > &pairtypes)
std::vector< bool > select(const Coord &positions, const AtomsConfiguration &configuration, const FieldMatrixd &lengths, const Eigen::VectorXd &mode, double force_sd, double force_error, double force_tolerance, bool require_parameter_reduction=true)
Gaussian-process regression, kernels, priors, and the searches that run on the surrogate.
Definition Macros.h:16
FieldMatrixd Coord
Definition Macros.h:35
Eigen::Matrix< double, EigenDynamic, EigenDynamic, FieldMatrixStorage > FieldMatrixd
Definition Macros.h:25
Stores configurations of all atoms in the system.
Definition Structures.h:832
AtomsPositionAndType atoms_mov
Info on moving atoms.
Definition Structures.h:844