robust_planning/verdict.rs
1//! The three-valued result of a conservative safety check.
2
3use polyzonotope::Intersection;
4use sampling_planning::Validity;
5
6/// The result of a conservative safety check.
7///
8/// Over-approximations can only prove safety. A check that cannot prove it
9/// returns [`SafetyVerdict::Indeterminate`], never [`SafetyVerdict::Safe`].
10#[derive(Debug, Clone, PartialEq)]
11#[must_use]
12pub enum SafetyVerdict {
13 /// The reachable set provably avoids every unsafe region.
14 ///
15 /// A proof only with the `sound` feature; without it, up to floating-point rounding.
16 Safe,
17 /// Some reachable state provably lies in an unsafe region; carries that state.
18 ///
19 /// Never produced from an over-approximated set alone: a point of the enclosure inside
20 /// an obstacle need not be reachable. Only a model's
21 /// [`Reach::certify_collision`](crate::Reach::certify_collision) hook can produce it;
22 /// without a certified witness the verdict stays [`SafetyVerdict::Indeterminate`].
23 Unsafe(Witness),
24 /// Neither could be proven; refine or reject.
25 Indeterminate,
26}
27
28/// A certified collision: a state the real system reaches that lies in an obstacle.
29///
30/// Produced only from a point returned by
31/// [`Reach::certify_collision`](crate::Reach::certify_collision).
32#[derive(Debug, Clone, PartialEq)]
33pub struct Witness {
34 /// The colliding state; has the sets' dimension.
35 pub point: Vec<f64>,
36 /// Index of the cover set the state lies in, in the edge's time order.
37 pub set: usize,
38 /// Index of the obstacle in [`Obstacles::polytopes`](crate::Obstacles::polytopes).
39 pub obstacle: usize,
40}
41
42impl SafetyVerdict {
43 /// The verdict for one over-approximated set against one obstacle.
44 ///
45 /// Only [`Intersection::Disjoint`] proves anything, so it is the only answer that maps to
46 /// [`SafetyVerdict::Safe`]. [`Intersection::Intersects`] maps to
47 /// [`SafetyVerdict::Indeterminate`]: its witness is evaluated in plain `f64` and lies in
48 /// the enclosure, not necessarily in the true reachable set.
49 pub const fn from_intersection(intersection: Intersection) -> Self {
50 match intersection {
51 Intersection::Disjoint => Self::Safe,
52 Intersection::Intersects | Intersection::Unknown => Self::Indeterminate,
53 }
54 }
55
56 /// Whether a planner may accept this result. Only [`SafetyVerdict::Safe`] is accepted.
57 #[must_use]
58 pub const fn is_safe(&self) -> bool {
59 matches!(self, Self::Safe)
60 }
61
62 /// Combines two verdicts for a compound check: [`SafetyVerdict::Unsafe`] dominates
63 /// [`SafetyVerdict::Indeterminate`], which dominates [`SafetyVerdict::Safe`].
64 /// The left witness wins if both are unsafe.
65 pub fn and(self, other: Self) -> Self {
66 match (self, other) {
67 (Self::Unsafe(witness), _) | (_, Self::Unsafe(witness)) => Self::Unsafe(witness),
68 (Self::Indeterminate, _) | (_, Self::Indeterminate) => Self::Indeterminate,
69 (Self::Safe, Self::Safe) => Self::Safe,
70 }
71 }
72}
73
74impl From<SafetyVerdict> for Validity {
75 /// Safe becomes [`Validity::Valid`], Unsafe [`Validity::Invalid`] and Indeterminate
76 /// [`Validity::Unknown`], which `sampling-planning`'s planners reject.
77 fn from(verdict: SafetyVerdict) -> Self {
78 match verdict {
79 SafetyVerdict::Safe => Self::Valid,
80 SafetyVerdict::Unsafe(_) => Self::Invalid,
81 SafetyVerdict::Indeterminate => Self::Unknown,
82 }
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn only_disjoint_is_safe() {
92 assert_eq!(
93 SafetyVerdict::from_intersection(Intersection::Disjoint),
94 SafetyVerdict::Safe
95 );
96 assert_eq!(
97 SafetyVerdict::from_intersection(Intersection::Intersects),
98 SafetyVerdict::Indeterminate
99 );
100 assert_eq!(
101 SafetyVerdict::from_intersection(Intersection::Unknown),
102 SafetyVerdict::Indeterminate
103 );
104 }
105
106 #[test]
107 fn unsafe_dominates_indeterminate_dominates_safe() {
108 use SafetyVerdict::{Indeterminate, Safe, Unsafe};
109
110 let unsafe_at = |obstacle| {
111 Unsafe(Witness {
112 point: vec![0.0, 0.0],
113 set: 0,
114 obstacle,
115 })
116 };
117
118 assert_eq!(Safe.and(Safe), Safe);
119 assert_eq!(Safe.and(Indeterminate), Indeterminate);
120 assert_eq!(Indeterminate.and(unsafe_at(1)), unsafe_at(1));
121 assert_eq!(unsafe_at(1).and(Safe), unsafe_at(1));
122 assert_eq!(unsafe_at(1).and(unsafe_at(2)), unsafe_at(1));
123 }
124
125 #[test]
126 fn maps_to_validity() {
127 assert_eq!(Validity::from(SafetyVerdict::Safe), Validity::Valid);
128 let witness = Witness {
129 point: vec![0.0],
130 set: 0,
131 obstacle: 0,
132 };
133
134 assert_eq!(
135 Validity::from(SafetyVerdict::Unsafe(witness)),
136 Validity::Invalid
137 );
138 assert_eq!(
139 Validity::from(SafetyVerdict::Indeterminate),
140 Validity::Unknown
141 );
142 }
143}