1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Cost vectors and Pareto dominance.
//!
//! A cost vector is a plain array `[C; N]` of a scalar [`Cost`] from
//! `search-core`. Arrays already provide `Copy` and lexicographic `Ord`, and
//! `[C::ZERO; N]` is the zero vector, so no newtype is needed.
use search_core::Cost;
/// Pareto dominance between cost vectors.
pub trait Dominates {
/// Returns `true` if `self` is no worse than `other` in every objective
/// and strictly better in at least one.
fn dominates(&self, other: &Self) -> bool;
}
impl<C: Cost, const N: usize> Dominates for [C; N] {
fn dominates(&self, other: &Self) -> bool {
weakly_dominates(self, other) && self != other
}
}
/// Returns `true` if `a` is no worse than `b` in every objective.
///
/// Unlike [`Dominates::dominates`], equal vectors weakly dominate each other.
/// Pruning uses this form: a label equal to a known one adds no new cost
/// vector to the result.
#[must_use]
pub fn weakly_dominates<C: Cost, const N: usize>(a: &[C; N], b: &[C; N]) -> bool {
a.iter().zip(b).all(|(x, y)| x <= y)
}
/// Adds two cost vectors component-wise.
#[must_use]
pub fn add<C: Cost, const N: usize>(a: [C; N], b: [C; N]) -> [C; N] {
core::array::from_fn(|i| a[i] + b[i])
}
#[cfg(test)]
mod tests {
//! Unit tests for dominance and vector addition.
use super::{Dominates, add, weakly_dominates};
/// Strict dominance needs one strictly better component.
#[test]
fn dominance_is_strict() {
assert!([1_u32, 2].dominates(&[1, 3]));
assert!(![1_u32, 2].dominates(&[1, 2]));
assert!(![1_u32, 4].dominates(&[2, 3]));
}
/// Weak dominance accepts equal vectors.
#[test]
fn weak_dominance_accepts_equal() {
assert!(weakly_dominates(&[1_u32, 2], &[1, 2]));
assert!(!weakly_dominates(&[1_u32, 4], &[2, 3]));
}
/// Addition is component-wise.
#[test]
fn add_is_componentwise() {
assert_eq!(add([1_u8, 2, 3], [4, 5, 6]), [5, 7, 9]);
}
}