multiobjective_search/anytime_boa/
factor.rs1use core::cmp::Ordering;
5
6use search_core::{Cost, WideMul};
7
8#[derive(Debug, Clone, Copy)]
19pub enum ApproxFactor<C> {
20 Zero,
22 Ratio {
24 num: C,
26 den: C,
28 },
29 Infinite,
32}
33
34impl<C: Cost + WideMul> ApproxFactor<C> {
35 #[must_use]
37 pub fn is_zero(self) -> bool {
38 matches!(self, Self::Zero)
39 }
40
41 #[must_use]
43 pub fn covers(self, x: C, y: C) -> bool {
44 if x <= y {
45 return true;
46 }
47
48 match self {
49 Self::Zero => false,
50 Self::Infinite => y > C::ZERO,
51 Self::Ratio { num, den } => x.abs_diff(y).wide_mul(den) <= num.wide_mul(y),
53 }
54 }
55
56 pub(crate) fn excess(by: C, of: C) -> Self {
59 if by <= of {
60 Self::Zero
61 } else if of == C::ZERO {
62 Self::Infinite
63 } else {
64 Self::Ratio {
65 num: by.abs_diff(of),
66 den: of,
67 }
68 }
69 }
70
71 pub(crate) fn domination(by: [C; 2], of: [C; 2]) -> Self {
74 Self::excess(by[0], of[0]).max(Self::excess(by[1], of[1]))
75 }
76
77 pub(crate) fn divided(self, d: u32) -> Self {
80 match self {
81 Self::Ratio { num, den } => {
82 let den = den.saturating_times(d);
83 if den.saturating_add(den) == den {
84 Self::Zero
85 } else {
86 Self::Ratio { num, den }
87 }
88 }
89 other => other,
90 }
91 }
92}
93
94impl<C: Cost + WideMul> PartialEq for ApproxFactor<C> {
95 fn eq(&self, other: &Self) -> bool {
96 self.cmp(other) == Ordering::Equal
97 }
98}
99
100impl<C: Cost + WideMul> Eq for ApproxFactor<C> {}
101
102impl<C: Cost + WideMul> PartialOrd for ApproxFactor<C> {
103 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
104 Some(self.cmp(other))
105 }
106}
107
108impl<C: Cost + WideMul> Ord for ApproxFactor<C> {
109 fn cmp(&self, other: &Self) -> Ordering {
110 match (*self, *other) {
111 (Self::Zero, Self::Zero) | (Self::Infinite, Self::Infinite) => Ordering::Equal,
112 (Self::Zero, _) | (_, Self::Infinite) => Ordering::Less,
113 (_, Self::Zero) | (Self::Infinite, _) => Ordering::Greater,
114 (Self::Ratio { num: a, den: b }, Self::Ratio { num: c, den: d }) => {
115 a.wide_mul(d).cmp(&c.wide_mul(b))
116 }
117 }
118 }
119}
120
121#[cfg(test)]
122mod tests {
123 use super::ApproxFactor;
126
127 #[test]
129 fn order_is_numeric() {
130 let half = ApproxFactor::Ratio { num: 1_u32, den: 2 };
131 let two_quarters = ApproxFactor::Ratio { num: 2, den: 4 };
132 let third = ApproxFactor::Ratio { num: 1, den: 3 };
133 assert_eq!(half, two_quarters);
134 assert!(third < half);
135 assert!(ApproxFactor::Zero < third);
136 assert!(half < ApproxFactor::Infinite);
137 }
138
139 #[test]
141 fn domination_factor() {
142 let df = ApproxFactor::domination([2_u32, 3], [3, 2]);
143 assert_eq!(df, ApproxFactor::Ratio { num: 1, den: 2 });
144 assert!(ApproxFactor::domination([1_u32, 4], [1, 4]).is_zero());
145 assert_eq!(
146 ApproxFactor::domination([1_u32, 1], [1, 0]),
147 ApproxFactor::Infinite
148 );
149 }
150
151 #[test]
153 fn covers_is_exact() {
154 let half = ApproxFactor::Ratio { num: 1_u32, den: 2 };
155 assert!(half.covers(15, 10));
156 assert!(!half.covers(16, 10));
157 assert!(!ApproxFactor::Zero.covers(11_u32, 10));
158 assert!(ApproxFactor::Infinite.covers(100_u32, 1));
159 assert!(!ApproxFactor::Infinite.covers(1_u32, 0));
160 }
161}