multiobjective_search/epsilon.rs
1//! Rational approximation factors `1 + ε` for the approximate searches.
2
3use search_core::Cost;
4
5/// A rational approximation tolerance `ε = num / den >= 0`, used as the
6/// factor `1 + ε`.
7///
8/// [`Cost`] has no division or fractional scaling, only the saturating
9/// integer product [`Cost::saturating_times`]. A rational `ε` lets the
10/// searches test `x <= (1 + ε) · y` exactly as `den · x <= (den + num) · y`.
11///
12/// # Overflow
13///
14/// The products are saturating. When `(den + num) · y` saturates, the test
15/// falls back to `x <= y`, which is the exact (`ε = 0`) answer. The searches
16/// then prune or merge less but keep their guarantees. Keep `den + num`
17/// small relative to the headroom of the cost type to get the full benefit.
18///
19/// # Examples
20///
21/// ```
22/// use multiobjective_search::Epsilon;
23///
24/// let ten_percent = Epsilon::new(1, 10);
25/// assert!(ten_percent.within(110_u32, 100));
26/// assert!(!ten_percent.within(111_u32, 100));
27/// assert!(Epsilon::ZERO.within(100_u32, 100));
28/// assert!(!Epsilon::ZERO.within(101_u32, 100));
29/// ```
30#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31pub struct Epsilon {
32 /// Numerator of `ε`.
33 num: u32,
34 /// Denominator of `ε`. Never zero; `num + den` fits in `u32`.
35 den: u32,
36}
37
38impl Default for Epsilon {
39 fn default() -> Self {
40 Self::ZERO
41 }
42}
43
44impl Epsilon {
45 /// No tolerance: `ε = 0`, which turns the approximate searches into
46 /// exact ones.
47 pub const ZERO: Self = Self { num: 0, den: 1 };
48
49 /// Creates `ε = num / den`.
50 ///
51 /// # Panics
52 ///
53 /// Panics if `den` is zero or `num + den` overflows `u32`.
54 #[must_use]
55 pub const fn new(num: u32, den: u32) -> Self {
56 assert!(den > 0, "Epsilon denominator must be positive");
57 assert!(
58 num.checked_add(den).is_some(),
59 "Epsilon num + den must fit in u32"
60 );
61
62 Self { num, den }
63 }
64
65 /// The numerator of `ε`.
66 #[must_use]
67 pub const fn num(self) -> u32 {
68 self.num
69 }
70
71 /// The denominator of `ε`. Never zero.
72 #[must_use]
73 pub const fn den(self) -> u32 {
74 self.den
75 }
76
77 /// Returns `true` if `x <= (1 + ε) · y`.
78 ///
79 /// Exact unless `(den + num) · y` saturates the cost type; then it
80 /// answers `x <= y`, which implies the exact answer.
81 #[must_use]
82 pub fn within<C: Cost>(self, x: C, y: C) -> bool {
83 let scaled = y.saturating_times(self.den + self.num);
84 if is_saturated(scaled) {
85 return x <= y;
86 }
87
88 x.saturating_times(self.den) <= scaled
89 }
90}
91
92/// Returns `true` if `value` is the largest value of its cost type, as a
93/// saturating product reports an overflow.
94fn is_saturated<C: Cost>(value: C) -> bool {
95 value != C::ZERO && value.saturating_add(value) == value
96}
97
98#[cfg(test)]
99mod tests {
100 //! Unit and property tests for [`Epsilon`].
101
102 use super::Epsilon;
103 use proptest::prelude::*;
104
105 /// A saturating product falls back to the exact comparison.
106 #[test]
107 fn saturation_falls_back_to_exact() {
108 let half = Epsilon::new(1, 2);
109 assert!(!half.within(u8::MAX, 200));
110 assert!(half.within(15_u8, 10));
111 assert!(!half.within(16_u8, 10));
112 // 3 * 100 saturates u8, so the answer is the exact `150 <= 100`.
113 assert!(!half.within(150_u8, 100));
114 assert!(half.within(100_u8, 100));
115 assert!(half.within(u8::MAX, u8::MAX));
116 }
117
118 /// A zero denominator is rejected.
119 #[test]
120 #[should_panic(expected = "denominator")]
121 fn zero_denominator_panics() {
122 let _ = Epsilon::new(1, 0);
123 }
124
125 proptest! {
126 /// Without saturation, `within` matches exact rational arithmetic.
127 #[test]
128 fn matches_rational(x in 0..1_000_u32, y in 0..1_000_u32, num in 0..50_u32, den in 1..50_u32) {
129 let exact = u64::from(den) * u64::from(x) <= (u64::from(den) + u64::from(num)) * u64::from(y);
130 prop_assert_eq!(Epsilon::new(num, den).within(x, y), exact);
131 }
132
133 /// With saturation, a `true` answer is still correct.
134 #[test]
135 fn never_wrongly_true(x: u16, y: u16, num in 0..1_000_u32, den in 1..1_000_u32) {
136 let exact = u64::from(den) * u64::from(x) <= (u64::from(den) + u64::from(num)) * u64::from(y);
137 if Epsilon::new(num, den).within(x, y) {
138 prop_assert!(exact);
139 }
140 }
141 }
142}