multiobjective_search/
cost.rs1use search_core::Cost;
8
9pub trait Dominates {
11 fn dominates(&self, other: &Self) -> bool;
14}
15
16impl<C: Cost, const N: usize> Dominates for [C; N] {
17 fn dominates(&self, other: &Self) -> bool {
18 weakly_dominates(self, other) && self != other
19 }
20}
21
22#[must_use]
28pub fn weakly_dominates<C: Cost, const N: usize>(a: &[C; N], b: &[C; N]) -> bool {
29 a.iter().zip(b).all(|(x, y)| x <= y)
30}
31
32#[must_use]
34pub fn add<C: Cost, const N: usize>(a: [C; N], b: [C; N]) -> [C; N] {
35 core::array::from_fn(|i| a[i] + b[i])
36}
37
38#[cfg(test)]
39mod tests {
40 use super::{Dominates, add, weakly_dominates};
43
44 #[test]
46 fn dominance_is_strict() {
47 assert!([1_u32, 2].dominates(&[1, 3]));
48 assert!(![1_u32, 2].dominates(&[1, 2]));
49 assert!(![1_u32, 4].dominates(&[2, 3]));
50 }
51
52 #[test]
54 fn weak_dominance_accepts_equal() {
55 assert!(weakly_dominates(&[1_u32, 2], &[1, 2]));
56 assert!(!weakly_dominates(&[1_u32, 4], &[2, 3]));
57 }
58
59 #[test]
61 fn add_is_componentwise() {
62 assert_eq!(add([1_u8, 2, 3], [4, 5, 6]), [5, 7, 9]);
63 }
64}