Control what goes into the GP

gp_minimize, gp_neb and run_ocineb take a policy argument. AtomicDimer.set_training_policy takes the same object. None leaves every decision with the engine. A policy can decline a call or a refit the engine was about to make. It cannot force one the engine declined.

The four classes below are the recipes. The fields they read are on PolicyState, documented in the training-policy reference.

Call the oracle only above a variance

VarianceThresholdPolicy takes variance_threshold, the posterior energy variance in eV^2. A call goes ahead only when that variance is strictly greater. The initial design has no posterior, so its variance is not finite and the call still happens. A later step that leaves the trust region is measured by the engine and skipped by a threshold the model cannot reach.

import numpy as np
import gpridge
from gpridge import (
    MinimizationConfig,
    VarianceThresholdPolicy,
    gp_minimize,
    initialize_atoms_config,
    potential_lj,
)

atoms = gpridge.AtomsConfiguration()
atoms.positions = np.zeros((1, 6))
atoms.is_frozen = np.zeros((1, 2), dtype=np.uint8)
atoms.id = np.array([[0, 1]], dtype=np.uint32)
atoms.atomic_nrs = np.array([[18, 18]], dtype=np.int32)
initialize_atoms_config(
    atoms, np.array([0, 0], dtype=np.uint32), atoms.positions.copy(), -1.0, 1
)
config = MinimizationConfig()
config.verbose = False
config.max_iter = 2
config.max_oracle_calls = 8
config.gp_train_iter = 4
config.n_initial_perturb = 1
config.trust_radius = 1e-4
config.max_move = 0.05
config.conv_tol = 1e-8
config.seed = 42
start = np.array([0.0, 0.0, 0.0, 1.2, 0.0, 0.0])

def oracle(positions):
    return potential_lj(positions, 2)

plain = gp_minimize(oracle, start, atoms, config)
gated = gp_minimize(
    oracle, start, atoms, config, VarianceThresholdPolicy(1.0e6)
)
assert gated.oracle_calls < plain.oracle_calls
assert np.isfinite(gated.e_final)

Cap the training set

SizeCapPolicy takes max_size, an integer of at least 1. Rows are admitted, then the stored set is cut back by farthest-point selection. Row 0 stays. Each later row is the one farthest from the nearest kept row. Ties take the lowest index. The kept rows stay in their original order.

from gpridge import SizeCapPolicy

class CountingCap(SizeCapPolicy):
    def __init__(self):
        super().__init__(1)
        self.shrunk = False

    def retain_training_rows(self, rows):
        kept = super().retain_training_rows(rows)
        self.shrunk = self.shrunk or len(kept) < len(rows)
        return kept

counter = CountingCap()
capped = gp_minimize(oracle, start, atoms, config, counter)
assert counter.shrunk
assert np.isfinite(capped.e_final)

Drop a point that is too close

DistanceDedupPolicy takes min_distance, in the same length unit as the coordinates. A candidate whose Euclidean distance to a kept row is strictly smaller is not stored. A distance equal to min_distance is kept. The comparison includes the current training set and candidates accepted earlier in the same batch.

from gpridge import DistanceDedupPolicy

kept = DistanceDedupPolicy(0.5).select_training_points(
    [{"positions": np.array([0.25])}, {"positions": np.array([0.5])}],
    [np.array([0.0])],
)
assert kept == [1]

class CountingDedup(DistanceDedupPolicy):
    def __init__(self):
        super().__init__(10.0)
        self.dropped = 0

    def select_training_points(self, candidates, current_set):
        chosen = super().select_training_points(candidates, current_set)
        self.dropped += len(candidates) - len(chosen)
        return chosen

dedup = CountingDedup()
deduped = gp_minimize(oracle, start, atoms, config, dedup)
assert dedup.dropped > 0
assert np.isfinite(deduped.e_final)

Freeze the hyperparameters

FreezeHyperparametersPolicy takes freeze_after_oracle_calls, an integer of at least 0. A refit the engine wants still runs while state.oracle_calls is below that count. Zero keeps the seeded hyperparameters and does not start the optimiser. The factor is still built, at those hyperparameters.

from gpridge import FreezeHyperparametersPolicy

class CountingFreeze(FreezeHyperparametersPolicy):
    def __init__(self):
        super().__init__(0)
        self.asked = 0
        self.allowed = 0

    def should_retrain_hyperparameters(self, state):
        self.asked += 1
        answer = super().should_retrain_hyperparameters(state)
        self.allowed += int(answer)
        return answer

counter = CountingFreeze()
frozen = gp_minimize(oracle, start, atoms, config, counter)
assert counter.asked > 0
assert counter.allowed == 0
assert np.isfinite(frozen.e_final)