polyzonotope-reach 0.1.3

Validated reachable-set over-approximation of linear and nonlinear dynamics with sparse polynomial zonotopes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! Splitting a set that is too large for one step, and merging pieces back
//! when that costs little.
//!
//! A split cuts the domain of one factor in half
//! ([`SparsePolyZonotope::split`], exact), either of the piece itself, which
//! keeps its dependencies, or of its oriented box, whichever leaves the
//! smaller halves. A merge encloses the convex hull of two pieces with a
//! polynomial zonotope over their factors, paired up so that two halves of
//! a factor split merge back into their parent.

use alloc::vec::Vec;

use super::Splitting;
use nalgebra::{DMatrix, DVector};
use polyzonotope::{FactorId, Interval, IntervalBox, Reduction, SparsePolyZonotope, Zonotope};

/// `piece` in two halves whose larger `cost` is as small as one split can
/// make it, or `None` if no split lowers the cost.
///
/// The caller's cost is the size that makes a step fail: for
/// [`NonlinearReach`](super::NonlinearReach), the second-order error bound
/// of the piece. The width alone would pick the wrong factor: a set wide
/// along a direction where `f` is nearly linear does not need splitting
/// there.
///
/// Two families of splits compete:
///
/// - Factor splits keep every dependency. After reduction most of a set's
///   size sits in its independent generators, which no factor split can
///   touch, so they are first enclosed in their oriented box
///   ([`Reduction::Pca`] of the independent part alone, which loses no
///   dependency, since they have none), and the box's `n` generators
///   become dependent factors of their own. That is exact: `g β` with
///   `β ∈ [-1, 1]` is the monomial `g α` of a factor that appears nowhere
///   else. Without the box, a split would cut one of dozens of small
///   generators and barely shrink the set.
/// - Box splits enclose the whole piece in its oriented box first and cut
///   that along one axis. They lose the dependencies but halve the extent
///   along the axis, where a factor split halves one generator's share of
///   it.
///
/// On the cubic drift `x₀' = x₀³` from a box of radius 0.5, linearization
/// with factor splits alone needs 4 pieces where the cheaper split needs 2,
/// so the cheaper of the two wins, split by split, unless `splitting` is
/// [`Splitting::Factors`].
pub(super) fn halves(
    piece: &SparsePolyZonotope,
    cost: impl Fn(&SparsePolyZonotope) -> f64,
    splitting: Splitting,
) -> Option<(SparsePolyZonotope, SparsePolyZonotope)> {
    let lifted = lift_independent(&box_independent(piece));
    let oriented = match splitting {
        Splitting::Factors => None,
        _ => Some(SparsePolyZonotope::from_zonotope(
            &piece.reduce(1.0, Reduction::Pca).to_zonotope(),
        )),
    };
    let candidates = core::iter::once(&lifted)
        .chain(oriented.as_ref())
        .flat_map(|set| set.ids().iter().map(move |&id| (set, id)))
        .filter(|&(set, id)| set.can_split(id));
    let best = candidates
        .map(|(set, id)| {
            let (lower, upper) = set.split(id);
            (cost(&lower).max(cost(&upper)), lower, upper)
        })
        .min_by(|a, b| a.0.total_cmp(&b.0))?;

    (best.0 < cost(piece)).then_some((best.1, best.2))
}

/// `pz` with its independent generators replaced by their oriented box:
/// an enclosure that keeps every dependent monomial as it is.
fn box_independent(pz: &SparsePolyZonotope) -> SparsePolyZonotope {
    let n = pz.dim();
    let independent = Zonotope::new(DVector::zeros(n), pz.independent_generators().clone())
        .reduce(1.0, Reduction::Pca);

    SparsePolyZonotope::new(
        pz.center().clone(),
        pz.dependent_generators().clone(),
        independent.generators().clone(),
        pz.exponents().clone(),
        pz.ids().to_vec(),
    )
    .expect("same shapes")
    .translate(independent.center())
}

/// `pz` with every independent generator turned into a dependent factor of
/// its own; the same set.
pub(super) fn lift_independent(pz: &SparsePolyZonotope) -> SparsePolyZonotope {
    let (dependent, independent, powers) = (
        pz.dependent_generators(),
        pz.independent_generators(),
        pz.exponents(),
    );
    let (deps, lifts, factors) = (dependent.ncols(), independent.ncols(), pz.ids().len());
    let generators = DMatrix::from_fn(pz.dim(), deps + lifts, |i, j| {
        if j < deps {
            dependent[(i, j)]
        } else {
            independent[(i, j - deps)]
        }
    });
    let exponents = DMatrix::from_fn(factors + lifts, deps + lifts, |k, j| {
        match (k < factors, j < deps) {
            (true, true) => powers[(k, j)],
            (false, false) => u32::from(k - factors == j - deps),
            _ => 0,
        }
    });
    let mut ids = pz.ids().to_vec();
    ids.extend(FactorId::fresh_n(lifts));

    SparsePolyZonotope::new(
        pz.center().clone(),
        generators,
        DMatrix::zeros(pz.dim(), 0),
        exponents,
        ids,
    )
    .expect("lifted shapes fit")
}

/// The sum of the widths of the interval hull: the size measure of both
/// splitting and merging.
pub(super) fn width(pz: &SparsePolyZonotope) -> f64 {
    pz.interval_hull().0.iter().map(Interval::width).sum()
}

/// Merges pairs of `pieces` while some pair's merged enclosure has a hull
/// of at most `1 + tolerance` times the volume of the two pieces' hulls
/// together, and is no wider than `cap`. The pair with the smallest ratio
/// goes first. Volume, not width: two far-apart boxes span a box barely
/// wider than the two, but mostly empty.
///
/// `cap` keeps a merge from undoing a split: a piece is split because it is
/// too large to step, so the caller passes a width below the pieces that
/// failed.
/// `finish` reduces each merged piece, since a merge adds generators; `cap`
/// applies to the finished piece, which reduction may have widened.
pub(super) fn merge(
    mut pieces: Vec<SparsePolyZonotope>,
    tolerance: f64,
    cap: f64,
    finish: impl Fn(&SparsePolyZonotope) -> SparsePolyZonotope,
) -> Vec<SparsePolyZonotope> {
    while let Some((i, j, merged)) = best_merge(&pieces, tolerance, cap, &finish) {
        pieces.swap_remove(j);
        pieces[i] = merged;
    }

    pieces
}

/// The pair `(i, j)`, `i < j`, of `pieces` whose merge is cheapest within
/// `tolerance` and `cap`, with that merge after `finish`.
fn best_merge(
    pieces: &[SparsePolyZonotope],
    tolerance: f64,
    cap: f64,
    finish: impl Fn(&SparsePolyZonotope) -> SparsePolyZonotope,
) -> Option<(usize, usize, SparsePolyZonotope)> {
    let hulls: Vec<_> = pieces
        .iter()
        .map(SparsePolyZonotope::interval_hull)
        .collect();
    let mut best: Option<(f64, usize, usize, SparsePolyZonotope)> = None;
    for j in 1..pieces.len() {
        for i in 0..j {
            let merged = hull_of_pair(&pieces[i], &pieces[j]);
            let ratio = volume(&merged.interval_hull()) / (volume(&hulls[i]) + volume(&hulls[j]));
            let fits = width(&merged) <= cap && ratio <= 1.0 + tolerance;
            if !fits || best.as_ref().is_some_and(|b| ratio >= b.0) {
                continue;
            }
            // Only candidates that would become the best get finished.
            let finished = finish(&merged);
            if width(&finished) <= cap {
                best = Some((ratio, i, j, finished));
            }
        }
    }

    best.map(|(_, i, j, merged)| (i, j, merged))
}

/// The volume of `hull`, each side at least `1e-12` so that a flat box
/// still compares.
fn volume(hull: &IntervalBox) -> f64 {
    hull.0.iter().map(|iv| iv.width().max(1e-12)).product()
}

/// An enclosure of the convex hull of `a ∪ b` that correlates their
/// factors:
///
/// `λ a + (1 − λ) b = ½ (a + b) + ν ½ (a − b)`, `ν = 2λ − 1 ∈ [-1, 1]`.
///
/// For a point of `a`, any point of `b` will do at `ν = 1`, so `b` may be
/// evaluated at whatever factor values suit: every value in `[-1, 1]` is
/// allowed for every factor. Shared factors keep one value on both sides,
/// and [`pair_factors`] gives the factors only one side has the values of
/// the other side's. For two halves of a split, that turns the merge back
/// into the parent. Both halves are added with
/// [`SparsePolyZonotope::exact_plus`], and `ν` becomes a fresh dependent
/// factor that multiplies every monomial of `½ (a − b)`. At `ν = 1` the
/// independent generators of `b` cancel when both sides use the same
/// coefficients, and those of `a` add up to their full size; the same at
/// `ν = −1` for `b`. So both pieces lie in the result.
fn hull_of_pair(a: &SparsePolyZonotope, b: &SparsePolyZonotope) -> SparsePolyZonotope {
    let b = pair_factors(a, b);
    let n = a.dim();
    let half = DMatrix::from_diagonal_element(n, n, 0.5);
    let mean = a.linear_map(&half).exact_plus(&b.linear_map(&half));
    let difference = a.linear_map(&half).exact_plus(&b.linear_map(&-&half));

    mean.exact_plus(&scaled_by_fresh_factor(&difference))
}

/// `b` with the factors it does not share with `a` renamed, in order, to
/// those of `a` that `b` does not have, as far as both lists go. The same
/// set; only its correlation with `a` changes.
fn pair_factors(a: &SparsePolyZonotope, b: &SparsePolyZonotope) -> SparsePolyZonotope {
    let mut only_a = a.ids().iter().filter(|id| !b.ids().contains(id));
    let ids = b
        .ids()
        .iter()
        .map(|id| {
            if a.ids().contains(id) {
                *id
            } else {
                only_a.next().copied().unwrap_or(*id)
            }
        })
        .collect();

    SparsePolyZonotope::new(
        b.center().clone(),
        b.dependent_generators().clone(),
        b.independent_generators().clone(),
        b.exponents().clone(),
        ids,
    )
    .expect("renamed ids stay distinct")
}

/// `{ ν p | p ∈ pz, ν ∈ [-1, 1] }` with `ν` a fresh dependent factor: the
/// center and every dependent monomial gain a power of `ν`, and the
/// independent generators stay, since `ν β` again ranges over `[-1, 1]`.
fn scaled_by_fresh_factor(pz: &SparsePolyZonotope) -> SparsePolyZonotope {
    let (dependent, powers) = (pz.dependent_generators(), pz.exponents());
    let (deps, factors) = (dependent.ncols(), pz.ids().len());
    let generators = DMatrix::from_fn(pz.dim(), deps + 1, |i, j| {
        if j < deps {
            dependent[(i, j)]
        } else {
            pz.center()[i]
        }
    });
    let exponents = DMatrix::from_fn(factors + 1, deps + 1, |k, j| {
        match (k < factors, j < deps) {
            (true, true) => powers[(k, j)],
            (true, false) => 0,
            (false, _) => 1,
        }
    });
    let mut ids = pz.ids().to_vec();
    ids.push(FactorId::fresh());

    SparsePolyZonotope::new(
        DVector::zeros(pz.dim()),
        generators,
        pz.independent_generators().clone(),
        exponents,
        ids,
    )
    .expect("scaled shapes fit")
    .compact()
}

#[cfg(test)]
mod tests {
    use super::*;

    /// A box with one dependent and one independent generator.
    fn mixed() -> SparsePolyZonotope {
        SparsePolyZonotope::new(
            DVector::from_vec(alloc::vec![1.0, 2.0]),
            DMatrix::from_row_slice(2, 1, &[1.0, 0.5]),
            DMatrix::from_row_slice(2, 1, &[0.0, 3.0]),
            DMatrix::from_element(1, 1, 1),
            FactorId::fresh_n(1),
        )
        .unwrap()
    }

    /// Lifting keeps the set: evaluating with the new factor at `β` gives
    /// the point of the original at `β`.
    #[test]
    fn lifting_is_exact() {
        let pz = mixed();
        let lifted = lift_independent(&pz);
        assert_eq!(lifted.independent_generators().ncols(), 0);
        for (a, b) in [(-1.0, 0.5), (0.3, -0.7), (1.0, 1.0)] {
            let gap = lifted.evaluate(&[a, b], &[]) - pz.evaluate(&[a], &[b]);
            assert!(gap.amax() < 1e-12, "{gap}");
        }
    }

    /// The split goes along the independent generator, the widest part, and
    /// the halves are narrower than the set.
    #[test]
    fn split_takes_the_widest_factor() {
        let pz = mixed();
        let (lower, upper) = halves(&pz, width, Splitting::Cheapest).expect("splittable");
        assert!(width(&lower) < width(&pz) && width(&upper) < width(&pz));
        let hull = lower.interval_hull();
        assert!((hull.0[1].width() - 4.0).abs() < 1e-12, "{hull:?}");
    }

    /// A point cannot be split.
    #[test]
    fn a_point_has_no_halves() {
        let point = SparsePolyZonotope::from_zonotope(&Zonotope::new(
            DVector::from_element(2, 1.0),
            DMatrix::zeros(2, 0),
        ));
        assert!(halves(&point, width, Splitting::Cheapest).is_none());
    }

    /// Two halves of a split merge back into a set no wider than their
    /// parent, so the merge fires.
    #[test]
    fn halves_merge_back() {
        let pz = mixed();
        let (lower, upper) = halves(&pz, width, Splitting::Factors).unwrap();
        let merged = merge(
            alloc::vec![lower.clone(), upper.clone()],
            0.05,
            f64::INFINITY,
            Clone::clone,
        );
        assert_eq!(merged.len(), 1);
        assert!(width(&merged[0]) <= width(&pz) + 1e-12);
    }

    /// The cap stops a merge into a set wider than the caller can step.
    #[test]
    fn cap_blocks_the_merge() {
        let (lower, upper) = halves(&mixed(), width, Splitting::Factors).unwrap();
        let cap = width(&lower).max(width(&upper));
        assert_eq!(
            merge(alloc::vec![lower, upper], 0.05, cap, Clone::clone).len(),
            2
        );
    }

    /// The cap holds for the finished piece: a merge that fits only before
    /// `finish` widens it is not taken.
    #[test]
    fn cap_applies_after_finish() {
        let pz = mixed();
        let (lower, upper) = halves(&pz, width, Splitting::Factors).unwrap();
        let widen = |m: &SparsePolyZonotope| {
            m.linear_map(&DMatrix::from_diagonal_element(m.dim(), m.dim(), 2.0))
        };
        let cap = width(&pz) + 1e-12;
        assert_eq!(merge(alloc::vec![lower, upper], 0.05, cap, widen).len(), 2);
    }

    /// Far-apart pieces stay separate: their hull is mostly empty space.
    #[test]
    fn distant_pieces_stay_apart() {
        let at = |x: f64| {
            SparsePolyZonotope::from_zonotope(&Zonotope::new(
                DVector::from_vec(alloc::vec![x, 0.0]),
                DMatrix::from_diagonal_element(2, 2, 0.1),
            ))
        };
        assert_eq!(
            merge(
                alloc::vec![at(0.0), at(5.0)],
                0.1,
                f64::INFINITY,
                Clone::clone
            )
            .len(),
            2
        );
    }
}

#[cfg(test)]
mod properties {
    use super::*;
    use proptest::prelude::*;

    /// A random 2-D set with two dependent factors of degree up to 2 and one
    /// independent generator.
    fn arb_set() -> impl Strategy<Value = SparsePolyZonotope> {
        (
            proptest::collection::vec(-2.0..2.0f64, 2),
            proptest::collection::vec(-1.0..1.0f64, 6),
            proptest::collection::vec(0u32..3, 6),
            proptest::collection::vec(-0.5..0.5f64, 2),
        )
            .prop_map(|(c, g, e, gi)| {
                SparsePolyZonotope::new(
                    DVector::from_vec(c),
                    DMatrix::from_vec(2, 3, g),
                    DMatrix::from_vec(2, 1, gi),
                    DMatrix::from_vec(2, 3, e),
                    FactorId::fresh_n(2),
                )
                .unwrap()
            })
    }

    /// Whether `p` lies in the interval hull of `pz` and of every piece of
    /// a few splits of it: a cheap but sharp necessary test for membership.
    fn near(pz: &SparsePolyZonotope, p: &DVector<f64>) -> bool {
        pz.split_pieces(6)
            .iter()
            .any(|piece| piece.interval_hull().contains(p, 1e-9))
    }

    proptest! {
        #![proptest_config(ProptestConfig::with_cases(64))]

        /// The halves of a split cover the set: each sampled point of the
        /// set is in one of them, at the same factor values.
        #[test]
        fn halves_cover_the_set(pz in arb_set(), a in -1.0..1.0f64, b in -1.0..1.0f64, beta in -1.0..1.0f64) {
            let p = pz.evaluate(&[a, b], &[beta]);
            if let Some((lower, upper)) = halves(&pz, width, Splitting::Cheapest) {
                prop_assert!(near(&lower, &p) || near(&upper, &p));
            }
        }

        /// A merge holds sampled points of both pieces, also of pieces that
        /// share factors and of unrelated ones.
        #[test]
        fn merge_holds_both_pieces(
            x in arb_set(),
            y in arb_set(),
            shared in proptest::bool::ANY,
            a in -1.0..1.0f64, b in -1.0..1.0f64, beta in -1.0..1.0f64,
        ) {
            let y = if shared { pair_factors(&x, &y) } else { y };
            let merged = hull_of_pair(&x, &y);
            prop_assert!(near(&merged, &x.evaluate(&[a, b], &[beta])));
            prop_assert!(near(&merged, &y.evaluate(&[a, b], &[beta])));
        }
    }
}