use nalgebra::{DMatrix, DVector};
use polyzonotope::{Intersection, Interval, IntervalBox, Polytope, SparsePolyZonotope, Zonotope};
use polyzonotope_reach::{LinearSystem, NonlinearSettings, Polynomial};
use rand_pcg::Pcg64Mcg;
use robust_planning::models::{Linear, Nonlinear};
use robust_planning::{Obstacles, Reach, SafetyVerdict, check_edge};
use sampling_planning::{rand_core::SeedableRng, unit_f64};
use crate::affine::Affine;
const STEP: f64 = 0.1;
const SUBSTEPS: u32 = 10;
const TOLERANCE: f64 = 1e-7;
const SPLITS: usize = 2;
type Field = fn(&[f64; 2], &[f64; 2]) -> [f64; 2];
fn rotation() -> LinearSystem {
LinearSystem {
a: DMatrix::from_row_slice(2, 2, &[-0.2, 1.0, -1.0, -0.2]),
b: DMatrix::identity(2, 2),
}
}
fn rotation_field(x: &[f64; 2], u: &[f64; 2]) -> [f64; 2] {
[-0.2 * x[0] + x[1] + u[0], -x[0] - 0.2 * x[1] + u[1]]
}
fn drag() -> Polynomial {
Polynomial::new(2, 2)
.term(0, 1.0, &[0, 0, 1, 0])
.term(0, -1.0, &[3, 0, 0, 0])
.term(1, 1.0, &[0, 0, 0, 1])
.term(1, -1.0, &[0, 3, 0, 0])
}
fn drag_field(x: &[f64; 2], u: &[f64; 2]) -> [f64; 2] {
[u[0] - x[0].powi(3), u[1] - x[1].powi(3)]
}
fn square(center: [f64; 2], half_width: f64) -> IntervalBox {
IntervalBox(
center
.iter()
.map(|c| Interval::new(c - half_width, c + half_width))
.collect(),
)
}
fn set(center: [f64; 2], half_width: f64) -> SparsePolyZonotope {
SparsePolyZonotope::from_zonotope(&Zonotope::from_box(&square(center, half_width)))
}
fn input(center: [f64; 2], half_width: f64) -> Zonotope {
Zonotope::new(
DVector::from_column_slice(¢er),
DMatrix::from_diagonal_element(2, 2, half_width),
)
}
fn draw(center: f64, half_width: f64, rng: &mut Pcg64Mcg) -> f64 {
let t = 2.0 * unit_f64(rng) - 1.0;
let t = if unit_f64(rng) < 0.25 { t.signum() } else { t };
center + half_width * t
}
fn draw_point(center: [f64; 2], half_width: f64, rng: &mut Pcg64Mcg) -> [f64; 2] {
[
draw(center[0], half_width, rng),
draw(center[1], half_width, rng),
]
}
fn rk4(field: Field, x: &[f64; 2], u: &[f64; 2], h: f64) -> [f64; 2] {
let at = |x: &[f64; 2], k: &[f64; 2], s: f64| [x[0] + s * k[0], x[1] + s * k[1]];
let k1 = field(x, u);
let k2 = field(&at(x, &k1, h / 2.0), u);
let k3 = field(&at(x, &k2, h / 2.0), u);
let k4 = field(&at(x, &k3, h), u);
core::array::from_fn(|i| x[i] + h / 6.0 * (k1[i] + 2.0 * k2[i] + 2.0 * k3[i] + k4[i]))
}
fn assert_meets(set: &SparsePolyZonotope, x: &[f64; 2], what: &str) {
let point = Polytope::from_box(&square(*x, TOLERANCE));
assert_ne!(
set.intersection(&point, SPLITS),
Intersection::Disjoint,
"{what}: state {x:?} escaped"
);
}
fn assert_trajectories_inside(
model: &impl Reach<2, Control = Vec<Zonotope>>,
field: Field,
(start, start_radius): ([f64; 2], f64),
(center, error, steps): ([f64; 2], f64, usize),
trajectories: u64,
) {
let mut cover = Vec::new();
let end = model
.propagate(
&set(start, start_radius),
&vec![input(center, error); steps],
&mut cover,
)
.expect("every step is proven");
assert_eq!(cover.len(), steps);
for seed in 0..trajectories {
let rng = &mut Pcg64Mcg::seed_from_u64(seed);
let mut x = draw_point(start, start_radius, rng);
for (k, cover_set) in cover.iter().enumerate() {
assert_meets(cover_set, &x, &format!("seed {seed}, start of step {k}"));
for _ in 0..SUBSTEPS {
let u = draw_point(center, error, rng);
x = rk4(field, &x, &u, STEP / f64::from(SUBSTEPS));
assert_meets(cover_set, &x, &format!("seed {seed}, step {k}"));
}
}
assert_meets(&end, &x, &format!("seed {seed}, end set"));
}
}
#[test]
fn linear_sets_contain_sampled_trajectories() {
let model = Linear::<2>::new(rotation(), STEP, 4, 20.0).expect("the step is valid");
assert_trajectories_inside(
&model,
rotation_field,
([1.0, 0.0], 0.1),
([0.5, 0.0], 0.1, 10),
8,
);
}
#[test]
fn nonlinear_sets_contain_sampled_trajectories() {
let model = Nonlinear::<_, 2>::new(drag(), NonlinearSettings::new(STEP));
assert_trajectories_inside(
&model,
drag_field,
([0.0, 0.5], 0.1),
([1.0, 0.0], 0.1, 8),
8,
);
}
#[test]
fn linear_end_set_contains_the_affine_stand_ins() {
let integrator = LinearSystem {
a: DMatrix::zeros(2, 2),
b: DMatrix::identity(2, 2),
};
let model = Linear::<2>::new(integrator, STEP, 4, 50.0).expect("the step is valid");
let stand_in = Affine::<2> {
a: DMatrix::identity(2, 2),
b: DMatrix::identity(2, 2) * STEP,
input_error: [0.1; 2],
disturbance: [0.0; 2],
step: 1.0,
max_steps: 6,
order: 50.0,
};
let start = set([1.0, 2.0], 0.1);
let nominal = [0.5, -0.3];
let (mut model_cover, mut stand_in_cover) = (Vec::new(), Vec::new());
let model_end = model
.propagate(&start, &vec![input(nominal, 0.1); 6], &mut model_cover)
.expect("linear steps are proven");
let stand_in_end = stand_in
.propagate(&start, &vec![nominal; 6], &mut stand_in_cover)
.expect("the stand-in always encloses its edges");
let (outer, inner) = (model_end.interval_hull(), stand_in_end.interval_hull());
for (o, i) in outer.0.iter().zip(&inner.0) {
assert!(
o.lo <= i.lo + 1e-12 && o.hi >= i.hi - 1e-12,
"{o:?} misses {i:?}"
);
}
}
#[test]
fn edges_through_an_obstacle_are_never_safe() {
let model = Linear::<2>::new(rotation(), STEP, 4, 20.0).expect("the step is valid");
let control = vec![input([0.5, 0.0], 0.05); 10];
let start = set([1.0, 0.0], 0.05);
let mut cover = Vec::new();
let check = |obstacle: IntervalBox, cover: &mut Vec<SparsePolyZonotope>| {
let obstacles = Obstacles::new(vec![Polytope::from_box(&obstacle)], 8);
check_edge(&model, &obstacles, &start, &control, cover).0
};
let midway = model
.propagate(&start, &control[..5].to_vec(), &mut Vec::new())
.expect("linear steps are proven")
.interval_hull();
let on_path = IntervalBox(
midway
.0
.iter()
.map(|r| Interval::new(r.lo.midpoint(r.hi) - 0.01, r.lo.midpoint(r.hi) + 0.01))
.collect(),
);
assert_ne!(check(on_path, &mut cover), SafetyVerdict::Safe);
assert_eq!(
check(square([5.0, 5.0], 0.5), &mut cover),
SafetyVerdict::Safe
);
}
#[test]
fn refused_steps_leave_the_edge_indeterminate() {
let model = Nonlinear::<_, 2>::new(drag(), NonlinearSettings::new(2.0));
let start = set([0.0, 0.0], 3.0);
let control = vec![input([1.0, 0.0], 0.1); 3];
let clear = Obstacles::new(vec![Polytope::from_box(&square([50.0, 50.0], 1.0))], 4);
assert_eq!(model.propagate(&start, &control, &mut Vec::new()), None);
let (verdict, end) = check_edge(&model, &clear, &start, &control, &mut Vec::new());
assert_eq!(verdict, SafetyVerdict::Indeterminate);
assert_eq!(end, None);
}