Skip to main content

pounce_cli/minima/
archive.rs

1//! Archive of accepted minima with per-dimension-scaled dedup, mirroring
2//! `MinimaArchive` in `python/pounce/_minima.py`.
3//!
4//! Two points are "the same" when their Euclidean distance in the
5//! per-dimension scaled space `‖(a−b)/L‖` is within `dedup`, where `L` is
6//! the box width per variable (1.0 for unbounded dims). This makes `dedup`
7//! scale-free and keeps it consistent with the anisotropic repulsion widths.
8
9use pounce_common::types::Number;
10
11/// Scaled Euclidean distance `‖(a−b)/L‖`.
12pub fn scaled_distance(a: &[Number], b: &[Number], l: &[Number]) -> Number {
13    let mut acc = 0.0;
14    for i in 0..a.len() {
15        let d = (a[i] - b[i]) / l[i];
16        acc += d * d;
17    }
18    acc.sqrt()
19}
20
21/// Accepted minima plus the dedup test.
22pub struct Archive {
23    dedup: Number,
24    /// Per-dimension scale `L` for the dedup metric.
25    l: Vec<Number>,
26    pub xs: Vec<Vec<Number>>,
27    pub fs: Vec<Number>,
28}
29
30impl Archive {
31    pub fn new(dedup: Number, l: Vec<Number>) -> Self {
32        Self {
33            dedup,
34            l,
35            xs: Vec::new(),
36            fs: Vec::new(),
37        }
38    }
39
40    pub fn len(&self) -> usize {
41        self.xs.len()
42    }
43
44    pub fn is_empty(&self) -> bool {
45        self.xs.is_empty()
46    }
47
48    /// Is `x` within `dedup` of any already-accepted minimum?
49    pub fn is_known(&self, x: &[Number]) -> bool {
50        self.xs
51            .iter()
52            .any(|m| scaled_distance(x, m, &self.l) <= self.dedup)
53    }
54
55    /// Is `x` within `radius` of any accepted minimum (MLSL clustering)?
56    pub fn near_any(&self, x: &[Number], radius: Number) -> bool {
57        self.xs
58            .iter()
59            .any(|m| scaled_distance(x, m, &self.l) <= radius)
60    }
61
62    pub fn add(&mut self, x: Vec<Number>, f: Number) {
63        self.xs.push(x);
64        self.fs.push(f);
65    }
66
67    /// Indices of the accepted minima ordered by ascending objective.
68    pub fn order_by_objective(&self) -> Vec<usize> {
69        let mut idx: Vec<usize> = (0..self.fs.len()).collect();
70        idx.sort_by(|&a, &b| {
71            self.fs[a]
72                .partial_cmp(&self.fs[b])
73                .unwrap_or(std::cmp::Ordering::Equal)
74        });
75        idx
76    }
77}