robust_planning/models/linear.rs
1//! Linear systems `x' = A x + B u` on `polyzonotope-reach`'s [`LinearReach`].
2
3use polyzonotope::{Reduction, SparsePolyZonotope, Zonotope};
4use polyzonotope_reach::{LinearReach, LinearSystem, ReachError};
5
6use crate::Reach;
7
8/// A [`Reach`] model of the linear system `x' = A x + B u` with `N` states.
9///
10/// The control of an edge is one input set `U_k` per time step of the configured length;
11/// see the [module docs](super). Every step is proven, so [`Reach::propagate`] always
12/// returns an end set. The carried time-point set is reduced to the configured order after
13/// each step (Girard), so the sets stay small along long edges.
14///
15/// # Examples
16///
17/// ```
18/// use robust_planning::models::Linear;
19/// use robust_planning::polyzonotope::{SparsePolyZonotope, Zonotope};
20/// use robust_planning::polyzonotope_reach::LinearSystem;
21/// use robust_planning::Reach;
22/// use nalgebra::{DMatrix, DVector};
23///
24/// // A single integrator `x' = u` with inputs `u ∈ [0.9, 1.1]²`, 4 steps of 0.25.
25/// let system = LinearSystem { a: DMatrix::zeros(2, 2), b: DMatrix::identity(2, 2) };
26/// let model = Linear::<2>::new(system, 0.25, 4, 8.0)?;
27/// let input = Zonotope::new(DVector::from_element(2, 1.0), DMatrix::identity(2, 2) * 0.1);
28/// let start = SparsePolyZonotope::from_zonotope(&Zonotope::new(
29/// DVector::zeros(2),
30/// DMatrix::zeros(2, 0),
31/// ));
32///
33/// let mut cover = Vec::new();
34/// let end = model.propagate(&start, &vec![input; 4], &mut cover).expect("linear steps are proven");
35/// let hull = end.interval_hull();
36///
37/// assert_eq!(cover.len(), 4);
38/// assert!(hull.0[0].lo <= 0.9 && hull.0[0].hi >= 1.1);
39/// # Ok::<(), robust_planning::polyzonotope_reach::ReachError>(())
40/// ```
41pub struct Linear<const N: usize> {
42 /// One step of the system, precomputed for the configured step length.
43 reach: LinearReach,
44 /// Zonotope order (generators per dimension, at least 1) the time-point set is reduced
45 /// to after every step.
46 order: f64,
47}
48
49impl<const N: usize> Linear<N> {
50 /// The model of `system` in steps of `step`, with `taylor_terms` terms of the matrix
51 /// exponential after the first, reducing to zonotope order `order` after every step.
52 ///
53 /// # Errors
54 ///
55 /// [`ReachError::InvalidStep`] if `step` is negative, infinite or NaN, and
56 /// [`ReachError::StepTooLarge`] if the Taylor remainder bound does not hold for this step
57 /// and term count; see [`LinearReach::new`].
58 ///
59 /// # Panics
60 ///
61 /// Panics if `A` is not `N × N`, if `B` does not have `N` rows, or if `order` is below 1
62 /// or NaN.
63 pub fn new(
64 system: LinearSystem,
65 step: f64,
66 taylor_terms: usize,
67 order: f64,
68 ) -> Result<Self, ReachError> {
69 assert_eq!(system.a.nrows(), N, "A needs one row per state");
70 assert!(order >= 1.0, "order must be at least 1");
71
72 Ok(Self {
73 reach: LinearReach::new(system, step, taylor_terms)?,
74 order,
75 })
76 }
77}
78
79impl<const N: usize> Reach<N> for Linear<N> {
80 /// One input set per time step, each with one dimension per column of `B`.
81 type Control = Vec<Zonotope>;
82
83 /// # Panics
84 ///
85 /// Panics if `initial` does not have `N` dimensions or an input set does not have one
86 /// per column of `B`.
87 fn propagate(
88 &self,
89 initial: &SparsePolyZonotope,
90 control: &Self::Control,
91 cover: &mut Vec<SparsePolyZonotope>,
92 ) -> Option<SparsePolyZonotope> {
93 super::chain(initial, control, cover, |set, input| {
94 let mut step = self.reach.step(set, input);
95 step.time_point = step.time_point.reduce(self.order, Reduction::Girard);
96
97 Some(step)
98 })
99 }
100}