Skip to main content

robust_planning/models/
nonlinear.rs

1//! Nonlinear systems `x' = f(x, u)` on `polyzonotope-reach`'s [`NonlinearReach`].
2
3use polyzonotope::{SparsePolyZonotope, Zonotope};
4use polyzonotope_reach::{Dynamics, NonlinearReach, NonlinearSettings};
5
6use crate::Reach;
7
8/// A [`Reach`] model of the nonlinear system `x' = f(x, u)` with `N` states.
9///
10/// The control of an edge is one input set `U_k` per time step of
11/// [`NonlinearSettings::step`]; see the [module docs](super). Each step linearizes or
12/// polynomializes `f` around the current set ([`NonlinearSettings::method`]) and encloses
13/// the abstraction error. A step polyzonotope-reach cannot prove (the set or the step is
14/// too large for the abstraction, or the step for the Taylor bound) makes
15/// [`Reach::propagate`] return `None`, so the edge is `Indeterminate`, never `Safe`.
16///
17/// Sets are stepped whole: [`NonlinearSettings::max_pieces`], `splitting`,
18/// `merge_tolerance` and `adaptive` do not apply, since a split set has no single end set
19/// to hand to the next edge.
20///
21/// # Examples
22///
23/// ```
24/// use robust_planning::models::Nonlinear;
25/// use nalgebra::{DMatrix, DVector};
26/// use robust_planning::polyzonotope::{SparsePolyZonotope, Zonotope};
27/// use robust_planning::polyzonotope_reach::{NonlinearSettings, Polynomial};
28/// use robust_planning::Reach;
29///
30/// // `x' = u - x³` per axis, from the box `[-0.1, 0.1]²` under inputs `u ∈ [0.9, 1.1]²`.
31/// let drag = Polynomial::new(2, 2)
32///     .term(0, 1.0, &[0, 0, 1, 0])
33///     .term(0, -1.0, &[3, 0, 0, 0])
34///     .term(1, 1.0, &[0, 0, 0, 1])
35///     .term(1, -1.0, &[0, 3, 0, 0]);
36/// let model = Nonlinear::<_, 2>::new(drag, NonlinearSettings::new(0.1));
37/// let start = SparsePolyZonotope::from_zonotope(&Zonotope::new(
38///     DVector::zeros(2),
39///     DMatrix::identity(2, 2) * 0.1,
40/// ));
41/// let input = Zonotope::new(DVector::from_element(2, 1.0), DMatrix::identity(2, 2) * 0.1);
42///
43/// let mut cover = Vec::new();
44/// let end = model.propagate(&start, &vec![input; 5], &mut cover).expect("short steps are proven");
45///
46/// assert_eq!(cover.len(), 5);
47/// assert!(end.interval_hull().0[0].lo > 0.0);
48/// ```
49pub struct Nonlinear<D, const N: usize> {
50    /// The dynamics with their step settings.
51    reach: NonlinearReach<D>,
52}
53
54impl<D: Dynamics, const N: usize> Nonlinear<D, N> {
55    /// The model of `dynamics`, stepped with `settings`.
56    ///
57    /// # Panics
58    ///
59    /// Panics if `dynamics` does not have `N` states, or if `settings` are out of range; see
60    /// [`NonlinearReach::new`].
61    #[must_use]
62    pub fn new(dynamics: D, settings: NonlinearSettings) -> Self {
63        assert_eq!(dynamics.states(), N, "the dynamics need N states");
64
65        Self {
66            reach: NonlinearReach::new(dynamics, settings),
67        }
68    }
69}
70
71impl<D: Dynamics, const N: usize> Reach<N> for Nonlinear<D, N> {
72    /// One input set per time step, each with one dimension per input of the dynamics.
73    type Control = Vec<Zonotope>;
74
75    /// # Panics
76    ///
77    /// Panics if `initial` does not have `N` dimensions or an input set does not have one
78    /// per input of the dynamics.
79    fn propagate(
80        &self,
81        initial: &SparsePolyZonotope,
82        control: &Self::Control,
83        cover: &mut Vec<SparsePolyZonotope>,
84    ) -> Option<SparsePolyZonotope> {
85        // shortcut: whole sets, fixed steps; split and adapt once polyzonotope can enclose
86        // the union of pieces in one set.
87        super::chain(initial, control, cover, |set, input| {
88            self.reach.step(set, input).ok()
89        })
90    }
91}