Skip to main content

multiobjective_search/
indicators.rs

1//! Quality indicators for approximate and partial Pareto fronts.
2
3use alloc::vec::Vec;
4
5use search_core::{BigUint, ExactArith, ExactNumber};
6
7/// The hypervolume of `points`: the volume of the region they dominate
8/// below `reference`, exactly (Zitzler and Thiele; the convergence-
9/// diversity indicator Domínguez-Ríos et al. use to compare anytime
10/// fronts). Points not strictly below `reference` in every objective add
11/// nothing.
12///
13/// Computed by slicing along the last objective, which is exponential in
14/// the number of objectives but exact; meant for evaluation, not for the
15/// inner loop of a search.
16///
17/// # Examples
18///
19/// ```
20/// use multiobjective_search::hypervolume;
21/// use search_core::BigUint;
22///
23/// // Two points below (10, 10): [2,10)×[8,10) and [6,10)×[3,10) overlap in [6,10)×[8,10).
24/// let hv = hypervolume(&[[2_u32, 8], [6, 3]], [10, 10]);
25/// assert_eq!(hv, BigUint::from(16 + 28 - 8));
26/// ```
27#[must_use]
28pub fn hypervolume<C: ExactArith<Exact = BigUint>, const N: usize>(
29    points: &[[C; N]],
30    reference: [C; N],
31) -> BigUint {
32    let inside: Vec<Vec<BigUint>> = points
33        .iter()
34        .filter(|p| (0..N).all(|i| p[i] < reference[i]))
35        .map(|p| p.iter().map(|c| c.exact()).collect())
36        .collect();
37    let reference: Vec<BigUint> = reference.iter().map(|c| c.exact()).collect();
38
39    slices(inside, &reference)
40}
41
42/// The hypervolume of `points` (all below `reference`) in `reference.len()`
43/// dimensions.
44fn slices(mut points: Vec<Vec<BigUint>>, reference: &[BigUint]) -> BigUint {
45    let d = reference.len();
46    if points.is_empty() || d == 0 {
47        return if points.is_empty() {
48            BigUint::zero()
49        } else {
50            BigUint::one()
51        };
52    }
53
54    points.sort_by(|a, b| a[d - 1].cmp(&b[d - 1]));
55    let mut total = BigUint::zero();
56    for k in 0..points.len() {
57        let next = points.get(k + 1).map_or(&reference[d - 1], |p| &p[d - 1]);
58        let height = next.abs_diff(&points[k][d - 1]);
59        if height == BigUint::zero() {
60            continue;
61        }
62
63        let base: Vec<Vec<BigUint>> = points[..=k].iter().map(|p| p[..d - 1].to_vec()).collect();
64        total += slices(base, &reference[..d - 1]) * height;
65    }
66
67    total
68}