robust_planning/cost.rs
1//! Edge costs: nominal length, optionally weighted by how wide the reachable set gets.
2
3use polyzonotope::SparsePolyZonotope;
4
5/// How a planner prices one edge.
6///
7/// Both options are additive along a path and never below the nominal length, so a
8/// Euclidean distance stays an admissible heuristic for them and RRT* rewiring stays
9/// well-defined.
10///
11/// # Examples
12///
13/// ```
14/// use robust_planning::Cost;
15/// use robust_planning::polyzonotope::{Interval, IntervalBox, SparsePolyZonotope, Zonotope};
16///
17/// let set = SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&IntervalBox(vec![
18/// Interval::new(0.0, 0.2),
19/// Interval::new(0.0, 0.4),
20/// ])));
21///
22/// assert_eq!(Cost::Length.edge(1.5, &set), 1.5);
23/// let weighted = Cost::WidthWeighted { weight: 2.0 }.edge(1.5, &set);
24/// assert!((weighted - (1.5 + 2.0 * 0.4)).abs() < 1e-12);
25/// ```
26#[derive(Debug, Clone, Copy, PartialEq, Default)]
27pub enum Cost {
28 /// The nominal path length. The default.
29 #[default]
30 Length,
31 /// The nominal length plus `weight` times the width of the edge's end set.
32 ///
33 /// The width is the largest side of the set's interval hull, so a path that keeps its
34 /// sets narrow, and with them its distance to the obstacles that bounded the proof,
35 /// costs less. A robustness-weighted cost: `weight` is length per unit of width.
36 /// `weight` is finite and non-negative; `0` prices like [`Cost::Length`] up to the
37 /// added zero.
38 ///
39 /// The width term is charged per edge, not per unit of length, so the same path cut
40 /// into more edges costs more: the cost also prefers fewer, longer edges. Because of
41 /// that, RRT*'s optimality argument does not carry over to this cost.
42 WidthWeighted {
43 /// Length units charged per unit of end-set width. Finite and non-negative.
44 weight: f64,
45 },
46}
47
48impl Cost {
49 /// The cost of an edge of nominal `length` that ends in `end`.
50 ///
51 /// [`Cost::Length`] returns `length` unchanged, so costs summed from the root equal the
52 /// nominal path length bit for bit.
53 #[must_use]
54 pub fn edge(&self, length: f64, end: &SparsePolyZonotope) -> f64 {
55 match *self {
56 Self::Length => length,
57 Self::WidthWeighted { weight } => length + weight * width(end),
58 }
59 }
60
61 /// Whether the weight, if any, is finite and non-negative.
62 #[must_use]
63 pub fn is_valid(&self) -> bool {
64 match *self {
65 Self::Length => true,
66 Self::WidthWeighted { weight } => weight.is_finite() && weight >= 0.0,
67 }
68 }
69}
70
71/// The largest side of the interval hull of `set`.
72fn width(set: &SparsePolyZonotope) -> f64 {
73 set.interval_hull()
74 .0
75 .iter()
76 .map(polyzonotope::Interval::width)
77 .fold(0.0, f64::max)
78}
79
80#[cfg(test)]
81mod tests {
82 use polyzonotope::{Interval, IntervalBox, Zonotope};
83
84 use super::*;
85
86 /// The box `[0, w] x [0, h]` as a set.
87 fn rect(w: f64, h: f64) -> SparsePolyZonotope {
88 SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&IntervalBox(vec![
89 Interval::new(0.0, w),
90 Interval::new(0.0, h),
91 ])))
92 }
93
94 #[test]
95 fn length_ignores_the_set() {
96 assert_eq!(
97 Cost::Length.edge(0.75, &rect(3.0, 4.0)).to_bits(),
98 0.75f64.to_bits()
99 );
100 }
101
102 #[test]
103 fn width_weighted_adds_the_largest_hull_side() {
104 let cost = Cost::WidthWeighted { weight: 0.5 };
105
106 assert!((cost.edge(1.0, &rect(0.2, 0.6)) - 1.3).abs() < 1e-12);
107 assert!(
108 cost.edge(1.0, &rect(0.2, 0.6)) >= 1.0,
109 "never below the length"
110 );
111 }
112
113 #[test]
114 fn only_finite_non_negative_weights_are_valid() {
115 assert!(Cost::Length.is_valid());
116 assert!(Cost::WidthWeighted { weight: 0.0 }.is_valid());
117 assert!(!Cost::WidthWeighted { weight: -1.0 }.is_valid());
118 assert!(!Cost::WidthWeighted { weight: f64::NAN }.is_valid());
119 assert!(
120 !Cost::WidthWeighted {
121 weight: f64::INFINITY
122 }
123 .is_valid()
124 );
125 }
126}