Skip to main content

multiobjective_search/anytime_boa/
factor.rs

1//! Exact approximation factors: the paper's domination factor `DF` and the
2//! approximation factor of an interval, as rationals of costs.
3
4use core::cmp::Ordering;
5
6use search_core::{Cost, WideMul};
7
8/// An approximation factor `ε >= 0`, kept exactly as a ratio of costs.
9///
10/// `ε` says how well a set of solutions covers the Pareto-optimal front:
11/// every Pareto-optimal cost `s` has a solution `r` with
12/// `r_i <= (1 + ε) · s_i` in both objectives. The searches derive `ε` from
13/// cost vectors (`(r_i - s_i) / s_i`), so it is a ratio of two costs and
14/// compared by exact cross-multiplication ([`WideMul`]). It is infinite
15/// when a solution must cover a zero cost with a positive one.
16///
17/// Equality and order are numeric: `1/2` equals `2/4`.
18#[derive(Debug, Clone, Copy)]
19pub enum ApproxFactor<C> {
20    /// `ε = 0`: the solutions include the whole Pareto-optimal front.
21    Zero,
22    /// `ε = num / den`, both positive.
23    Ratio {
24        /// Numerator, positive.
25        num: C,
26        /// Denominator, positive.
27        den: C,
28    },
29    /// `ε = ∞`: some Pareto-optimal cost with a zero component is not yet
30    /// matched by a solution.
31    Infinite,
32}
33
34impl<C: Cost + WideMul> ApproxFactor<C> {
35    /// Returns `true` for `ε = 0`.
36    #[must_use]
37    pub fn is_zero(self) -> bool {
38        matches!(self, Self::Zero)
39    }
40
41    /// Returns `true` if `x <= (1 + ε) · y`, exactly.
42    #[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            // x > y: (x - y) / y <= num / den.
52            Self::Ratio { num, den } => x.abs_diff(y).wide_mul(den) <= num.wide_mul(y),
53        }
54    }
55
56    /// The factor by which the cost `by` exceeds `of` in one objective,
57    /// `max((by - of) / of, 0)`.
58    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    /// The domination factor `DF(by, of)` of the paper: the smallest `ε`
72    /// with `by <= (1 + ε) · of` in both objectives.
73    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    /// `ε / d`. A denominator that saturates the cost type gives zero, so
78    /// that a schedule never stalls at the smallest representable ratio.
79    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    //! Unit tests for [`ApproxFactor`].
124
125    use super::ApproxFactor;
126
127    /// Ratios compare numerically, and zero and infinity bound them.
128    #[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    /// `DF` matches the paper's Example 1: `DF((2, 3), (3, 2)) = 0.5`.
140    #[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    /// `covers` is `x <= (1 + ε) y`.
152    #[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}