use polyzonotope::{Containment, IntervalBox, Polytope, SparsePolyZonotope};
pub trait Goal<const N: usize> {
fn center(&self) -> [f64; N];
fn containment(&self, set: &SparsePolyZonotope) -> Containment;
fn radius(&self) -> Option<f64> {
None
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct PolytopeGoal<const N: usize> {
polytope: Polytope,
center: [f64; N],
max_splits: usize,
}
impl<const N: usize> PolytopeGoal<N> {
#[must_use]
pub fn new(polytope: Polytope, center: [f64; N], max_splits: usize) -> Self {
for facet in &polytope.facets {
assert_eq!(facet.normal.len(), N, "facet normals must have {N} entries");
assert!(
facet.normal.iter().all(|a| a.is_finite()) && facet.offset.is_finite(),
"facet normals and offsets must be finite"
);
}
Self {
polytope,
center,
max_splits,
}
}
#[must_use]
pub fn from_box(b: &IntervalBox, max_splits: usize) -> Self {
assert_eq!(b.dim(), N, "the box must have {N} dimensions");
let center = core::array::from_fn(|i| b.0[i].lo + b.0[i].width() / 2.0);
Self::new(Polytope::from_box(b), center, max_splits)
}
#[must_use]
pub const fn polytope(&self) -> &Polytope {
&self.polytope
}
#[must_use]
pub const fn max_splits(&self) -> usize {
self.max_splits
}
}
impl<const N: usize> Goal<N> for PolytopeGoal<N> {
fn center(&self) -> [f64; N] {
self.center
}
fn radius(&self) -> Option<f64> {
let (lower, upper) = axis_bounds::<N>(&self.polytope);
let squared: f64 = (0..N)
.map(|i| {
let reach = (self.center[i] - lower[i]).max(upper[i] - self.center[i]);
reach * reach
})
.sum();
let radius = squared.sqrt() * (1.0 + 8.0 * f64::EPSILON);
radius.is_finite().then_some(radius)
}
fn containment(&self, set: &SparsePolyZonotope) -> Containment {
set.containment(&self.polytope, self.max_splits)
}
}
fn axis_bounds<const N: usize>(polytope: &Polytope) -> ([f64; N], [f64; N]) {
let mut lower = [f64::NEG_INFINITY; N];
let mut upper = [f64::INFINITY; N];
for facet in &polytope.facets {
let mut nonzero = facet.normal.iter().enumerate().filter(|(_, a)| **a != 0.0);
let (Some((axis, &a)), None) = (nonzero.next(), nonzero.next()) else {
continue;
};
let bound = facet.offset / a;
if a > 0.0 {
upper[axis] = upper[axis].min(bound);
} else {
lower[axis] = lower[axis].max(bound);
}
}
(lower, upper)
}
#[cfg(test)]
mod tests {
use nalgebra::DVector;
use polyzonotope::{Halfspace, Interval, Zonotope};
use super::*;
fn square_set(lo: f64, hi: f64) -> SparsePolyZonotope {
SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&IntervalBox(vec![
Interval::new(
lo, hi
);
2
])))
}
fn unit_goal() -> PolytopeGoal<2> {
PolytopeGoal::from_box(&IntervalBox(vec![Interval::new(0.0, 1.0); 2]), 8)
}
fn triangle() -> PolytopeGoal<2> {
let facet = |a: f64, b: f64, offset: f64| Halfspace {
normal: DVector::from_column_slice(&[a, b]),
offset,
};
let polytope = Polytope {
facets: vec![
facet(-1.0, 0.0, 0.0),
facet(0.0, -1.0, 0.0),
facet(1.0, 1.0, 1.0),
],
};
PolytopeGoal::new(polytope, [0.25, 0.25], 8)
}
#[test]
fn set_well_inside_is_contained() {
assert_eq!(unit_goal().center(), [0.5, 0.5]);
assert_eq!(
unit_goal().containment(&square_set(0.25, 0.75)),
Containment::Inside
);
}
#[test]
fn straddling_set_is_not_contained() {
assert_ne!(
unit_goal().containment(&square_set(0.5, 1.5)),
Containment::Inside
);
}
#[test]
fn set_one_ulp_outside_is_not_contained() {
assert_ne!(
unit_goal().containment(&square_set(0.5, 1.0_f64.next_up())),
Containment::Inside
);
}
#[test]
fn slanted_facet_decides_containment() {
let goal = triangle();
assert_eq!(goal.containment(&square_set(0.1, 0.3)), Containment::Inside);
assert_ne!(
goal.containment(&square_set(0.35, 0.55)),
Containment::Inside,
"the corner (0.55, 0.55) is beyond x + y <= 1"
);
}
#[test]
#[should_panic(expected = "finite")]
fn nan_offset_is_rejected() {
let mut polytope = unit_goal().polytope().clone();
polytope.facets[0].offset = f64::NAN;
let _ = PolytopeGoal::new(polytope, [0.5, 0.5], 8);
}
#[test]
#[should_panic(expected = "entries")]
fn wrong_normal_length_is_rejected() {
let polytope = Polytope::from_box(&IntervalBox(vec![Interval::new(0.0, 1.0); 3]));
let _ = PolytopeGoal::<2>::new(polytope, [0.5, 0.5], 8);
}
#[test]
fn radius_bounds_the_box_and_needs_axis_facets() {
let unit = unit_goal().radius().expect("a box bounds every axis");
assert!(unit >= 0.5f64.sqrt() && unit < 0.5f64.sqrt() + 1e-12);
assert_eq!(
triangle().radius(),
None,
"x + y <= 1 bounds no axis from above"
);
let mut cut = unit_goal();
cut.polytope.facets.extend(triangle().polytope.facets);
let radius = cut.radius().expect("the box facets are still there");
assert!(
radius >= 0.5f64.sqrt(),
"extra facets never shrink the bound below the box"
);
}
}