1use polyzonotope::{Containment, IntervalBox, Polytope, SparsePolyZonotope};
5
6pub trait Goal<const N: usize> {
19 fn center(&self) -> [f64; N];
21
22 fn containment(&self, set: &SparsePolyZonotope) -> Containment;
27
28 fn radius(&self) -> Option<f64> {
35 None
36 }
37}
38
39#[derive(Debug, Clone, PartialEq)]
62pub struct PolytopeGoal<const N: usize> {
63 polytope: Polytope,
65 center: [f64; N],
67 max_splits: usize,
69}
70
71impl<const N: usize> PolytopeGoal<N> {
72 #[must_use]
80 pub fn new(polytope: Polytope, center: [f64; N], max_splits: usize) -> Self {
81 for facet in &polytope.facets {
82 assert_eq!(facet.normal.len(), N, "facet normals must have {N} entries");
83 assert!(
84 facet.normal.iter().all(|a| a.is_finite()) && facet.offset.is_finite(),
85 "facet normals and offsets must be finite"
86 );
87 }
88
89 Self {
90 polytope,
91 center,
92 max_splits,
93 }
94 }
95
96 #[must_use]
102 pub fn from_box(b: &IntervalBox, max_splits: usize) -> Self {
103 assert_eq!(b.dim(), N, "the box must have {N} dimensions");
104 let center = core::array::from_fn(|i| b.0[i].lo + b.0[i].width() / 2.0);
105
106 Self::new(Polytope::from_box(b), center, max_splits)
107 }
108
109 #[must_use]
111 pub const fn polytope(&self) -> &Polytope {
112 &self.polytope
113 }
114
115 #[must_use]
117 pub const fn max_splits(&self) -> usize {
118 self.max_splits
119 }
120}
121
122impl<const N: usize> Goal<N> for PolytopeGoal<N> {
123 fn center(&self) -> [f64; N] {
124 self.center
125 }
126
127 fn radius(&self) -> Option<f64> {
134 let (lower, upper) = axis_bounds::<N>(&self.polytope);
135 let squared: f64 = (0..N)
136 .map(|i| {
137 let reach = (self.center[i] - lower[i]).max(upper[i] - self.center[i]);
138
139 reach * reach
140 })
141 .sum();
142 let radius = squared.sqrt() * (1.0 + 8.0 * f64::EPSILON);
143
144 radius.is_finite().then_some(radius)
145 }
146
147 fn containment(&self, set: &SparsePolyZonotope) -> Containment {
153 set.containment(&self.polytope, self.max_splits)
154 }
155}
156
157fn axis_bounds<const N: usize>(polytope: &Polytope) -> ([f64; N], [f64; N]) {
160 let mut lower = [f64::NEG_INFINITY; N];
161 let mut upper = [f64::INFINITY; N];
162
163 for facet in &polytope.facets {
164 let mut nonzero = facet.normal.iter().enumerate().filter(|(_, a)| **a != 0.0);
165 let (Some((axis, &a)), None) = (nonzero.next(), nonzero.next()) else {
166 continue;
167 };
168 let bound = facet.offset / a;
169
170 if a > 0.0 {
171 upper[axis] = upper[axis].min(bound);
172 } else {
173 lower[axis] = lower[axis].max(bound);
174 }
175 }
176
177 (lower, upper)
178}
179
180#[cfg(test)]
181mod tests {
182 use nalgebra::DVector;
183 use polyzonotope::{Halfspace, Interval, Zonotope};
184
185 use super::*;
186
187 fn square_set(lo: f64, hi: f64) -> SparsePolyZonotope {
189 SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&IntervalBox(vec![
190 Interval::new(
191 lo, hi
192 );
193 2
194 ])))
195 }
196
197 fn unit_goal() -> PolytopeGoal<2> {
199 PolytopeGoal::from_box(&IntervalBox(vec![Interval::new(0.0, 1.0); 2]), 8)
200 }
201
202 fn triangle() -> PolytopeGoal<2> {
204 let facet = |a: f64, b: f64, offset: f64| Halfspace {
205 normal: DVector::from_column_slice(&[a, b]),
206 offset,
207 };
208 let polytope = Polytope {
209 facets: vec![
210 facet(-1.0, 0.0, 0.0),
211 facet(0.0, -1.0, 0.0),
212 facet(1.0, 1.0, 1.0),
213 ],
214 };
215
216 PolytopeGoal::new(polytope, [0.25, 0.25], 8)
217 }
218
219 #[test]
220 fn set_well_inside_is_contained() {
221 assert_eq!(unit_goal().center(), [0.5, 0.5]);
222 assert_eq!(
223 unit_goal().containment(&square_set(0.25, 0.75)),
224 Containment::Inside
225 );
226 }
227
228 #[test]
229 fn straddling_set_is_not_contained() {
230 assert_ne!(
231 unit_goal().containment(&square_set(0.5, 1.5)),
232 Containment::Inside
233 );
234 }
235
236 #[test]
237 fn set_one_ulp_outside_is_not_contained() {
238 assert_ne!(
239 unit_goal().containment(&square_set(0.5, 1.0_f64.next_up())),
240 Containment::Inside
241 );
242 }
243
244 #[test]
245 fn slanted_facet_decides_containment() {
246 let goal = triangle();
247
248 assert_eq!(goal.containment(&square_set(0.1, 0.3)), Containment::Inside);
249 assert_ne!(
250 goal.containment(&square_set(0.35, 0.55)),
251 Containment::Inside,
252 "the corner (0.55, 0.55) is beyond x + y <= 1"
253 );
254 }
255
256 #[test]
257 #[should_panic(expected = "finite")]
258 fn nan_offset_is_rejected() {
259 let mut polytope = unit_goal().polytope().clone();
260 polytope.facets[0].offset = f64::NAN;
261
262 let _ = PolytopeGoal::new(polytope, [0.5, 0.5], 8);
263 }
264
265 #[test]
266 #[should_panic(expected = "entries")]
267 fn wrong_normal_length_is_rejected() {
268 let polytope = Polytope::from_box(&IntervalBox(vec![Interval::new(0.0, 1.0); 3]));
269
270 let _ = PolytopeGoal::<2>::new(polytope, [0.5, 0.5], 8);
271 }
272
273 #[test]
274 fn radius_bounds_the_box_and_needs_axis_facets() {
275 let unit = unit_goal().radius().expect("a box bounds every axis");
276
277 assert!(unit >= 0.5f64.sqrt() && unit < 0.5f64.sqrt() + 1e-12);
278 assert_eq!(
279 triangle().radius(),
280 None,
281 "x + y <= 1 bounds no axis from above"
282 );
283
284 let mut cut = unit_goal();
285 cut.polytope.facets.extend(triangle().polytope.facets);
286 let radius = cut.radius().expect("the box facets are still there");
287 assert!(
288 radius >= 0.5f64.sqrt(),
289 "extra facets never shrink the bound below the box"
290 );
291 }
292}