use alloc::vec;
use alloc::vec::Vec;
use nalgebra::{DMatrix, DVector};
use crate::round;
use crate::{FactorId, Halfspace, Interval, IntervalBox, Polytope, SparsePolyZonotope};
#[derive(Clone, Debug, PartialEq)]
pub struct ConstrainedPolyZonotope {
stacked: SparsePolyZonotope,
dim: usize,
}
impl From<SparsePolyZonotope> for ConstrainedPolyZonotope {
fn from(set: SparsePolyZonotope) -> Self {
Self {
dim: set.dim(),
stacked: set,
}
}
}
impl ConstrainedPolyZonotope {
#[must_use]
pub fn new(set: &SparsePolyZonotope, constraints: &SparsePolyZonotope) -> Self {
Self {
stacked: set.cartesian_product(constraints),
dim: set.dim(),
}
}
#[must_use]
pub fn dim(&self) -> usize {
self.dim
}
#[must_use]
pub fn num_constraints(&self) -> usize {
self.stacked.dim() - self.dim
}
#[must_use]
pub fn stacked(&self) -> &SparsePolyZonotope {
&self.stacked
}
#[must_use]
pub fn unconstrained(&self) -> SparsePolyZonotope {
self.stacked
.linear_map(&DMatrix::from_fn(self.dim, self.stacked.dim(), |i, j| {
f64::from(u8::from(i == j))
}))
}
#[must_use]
pub fn linear_map(&self, m: &DMatrix<f64>) -> Self {
assert_eq!(
m.ncols(),
self.dim,
"matrix columns must match the dimension"
);
let k = self.num_constraints();
let block = DMatrix::from_fn(m.nrows() + k, self.dim + k, |i, j| {
match (i < m.nrows(), j < self.dim) {
(true, true) => m[(i, j)],
(false, false) => f64::from(u8::from(i - m.nrows() == j - self.dim)),
_ => 0.0,
}
});
Self {
stacked: self.stacked.linear_map(&block),
dim: m.nrows(),
}
}
#[must_use]
pub fn translate(&self, v: &DVector<f64>) -> Self {
assert_eq!(v.len(), self.dim, "translation must match the dimension");
let padded = DVector::from_fn(
self.stacked.dim(),
|i, _| if i < self.dim { v[i] } else { 0.0 },
);
Self {
stacked: self.stacked.translate(&padded),
dim: self.dim,
}
}
#[must_use]
pub fn minkowski_sum(&self, other: &Self) -> Self {
assert_eq!(self.dim, other.dim, "sets must have the same dimension");
let (k1, k2) = (self.num_constraints(), other.num_constraints());
let first = self.embed(self.dim + k1 + k2, self.dim);
let second = other
.with_fresh_ids()
.embed(self.dim + k1 + k2, self.dim + k1);
Self {
stacked: first.exact_plus(&second),
dim: self.dim,
}
}
#[must_use]
pub fn intersection(&self, other: &Self) -> Self {
assert_eq!(self.dim, other.dim, "sets must have the same dimension");
let (n, k1, k2) = (self.dim, self.num_constraints(), other.num_constraints());
let total = n + k1 + k2 + n;
let first = self.embed_with(
total,
|i, row| match row {
r if r < n => (r == i).then_some(1.0),
r if r >= n + k1 + k2 => (r - (n + k1 + k2) == i).then_some(1.0),
_ => None,
},
|c, row| (row == n + c).then_some(1.0),
);
let second = other.embed_with(
total,
|i, row| (row >= n + k1 + k2 && row - (n + k1 + k2) == i).then_some(-1.0),
|c, row| (row == n + k1 + c).then_some(1.0),
);
Self {
stacked: first.exact_plus(&second),
dim: n,
}
}
#[must_use]
pub fn intersect_halfspace(&self, h: &Halfspace) -> Self {
assert_eq!(h.normal.len(), self.dim, "normal must match the dimension");
let rows = self.stacked.dim();
let extended = self
.stacked
.linear_map(&DMatrix::from_fn(rows + 1, rows, |i, j| {
if i < rows {
f64::from(u8::from(i == j))
} else if j < self.dim {
h.normal[j]
} else {
0.0
}
}));
let range = extended.interval_hull().0[rows];
if range.hi <= h.offset {
return self.clone();
}
let last = |v: f64| DVector::from_fn(rows + 1, |i, _| if i == rows { v } else { 0.0 });
if range.lo > h.offset {
let zeroed = DMatrix::from_fn(rows + 1, rows, |i, j| f64::from(u8::from(i == j)));
return Self {
stacked: self.stacked.linear_map(&zeroed).translate(&last(1.0)),
dim: self.dim,
};
}
let width = round::add_up(h.offset, -range.lo);
let mut half = 0.5 * width;
if 2.0 * half < width {
half = half.next_up();
}
let slack = SparsePolyZonotope::new(
last(-h.offset),
DMatrix::from_fn(rows + 1, 2, |i, _| if i == rows { half } else { 0.0 }),
DMatrix::zeros(rows + 1, 0),
DMatrix::from_row_slice(1, 2, &[0, 1]),
vec![FactorId::fresh()],
)
.expect("consistent shapes");
Self {
stacked: extended.exact_plus(&slack),
dim: self.dim,
}
}
#[must_use]
pub fn intersect_polytope(&self, p: &Polytope) -> Self {
p.facets
.iter()
.fold(self.clone(), |acc, h| acc.intersect_halfspace(h))
}
#[must_use]
pub fn interval_hull(&self) -> IntervalBox {
IntervalBox(self.stacked.interval_hull().0[..self.dim].to_vec())
}
#[must_use]
pub fn interval_hull_split(&self, max_splits: usize) -> Option<IntervalBox> {
let pieces = self.feasible_pieces(max_splits);
let hulls: Vec<IntervalBox> = pieces
.iter()
.map(SparsePolyZonotope::interval_hull)
.collect();
let first = hulls.first()?;
Some(IntervalBox(
(0..self.dim)
.map(|i| {
let lo = hulls
.iter()
.map(|h| h.0[i].lo)
.fold(first.0[i].lo, f64::min);
let hi = hulls
.iter()
.map(|h| h.0[i].hi)
.fold(first.0[i].hi, f64::max);
Interval::new(lo, hi)
})
.collect(),
))
}
#[must_use]
pub fn proven_empty(&self, max_splits: usize) -> bool {
self.feasible_pieces(max_splits).is_empty()
}
fn feasible_pieces(&self, max_splits: usize) -> Vec<SparsePolyZonotope> {
let width = |pz: &SparsePolyZonotope| {
pz.interval_hull()
.0
.iter()
.map(Interval::width)
.sum::<f64>()
};
let mut pieces: Vec<SparsePolyZonotope> = vec![self.stacked.clone()];
for _ in 0..max_splits {
pieces.retain(|p| self.might_be_feasible(p));
let candidates =
(0..pieces.len()).filter_map(|i| Some((i, pieces[i].heaviest_factor_overall()?)));
let Some((i, factor)) =
candidates.max_by(|a, b| width(&pieces[a.0]).total_cmp(&width(&pieces[b.0])))
else {
break;
};
let (lower, upper) = pieces.swap_remove(i).split(factor);
pieces.push(lower);
pieces.push(upper);
}
pieces.retain(|p| self.might_be_feasible(p));
pieces
}
fn might_be_feasible(&self, piece: &SparsePolyZonotope) -> bool {
let hull = piece.interval_hull();
let slack = if cfg!(feature = "sound") {
0.0
} else {
let magnitude = piece
.center()
.iter()
.chain(piece.dependent_generators().iter())
.chain(piece.independent_generators().iter());
1e3 * f64::EPSILON * round::abs_sum_up(magnitude)
};
hull.0[self.dim..]
.iter()
.all(|iv| iv.lo <= slack && -slack <= iv.hi)
}
fn with_fresh_ids(&self) -> Self {
Self {
stacked: self.stacked.with_fresh_ids(),
dim: self.dim,
}
}
fn embed(&self, total: usize, first_constraint: usize) -> SparsePolyZonotope {
self.embed_with(
total,
|i, row| (i == row).then_some(1.0),
|c, row| (first_constraint + c == row).then_some(1.0),
)
}
fn embed_with(
&self,
total: usize,
set_weight: impl Fn(usize, usize) -> Option<f64>,
constraint_weight: impl Fn(usize, usize) -> Option<f64>,
) -> SparsePolyZonotope {
let m = DMatrix::from_fn(total, self.stacked.dim(), |row, j| {
let w = if j < self.dim {
set_weight(j, row)
} else {
constraint_weight(j - self.dim, row)
};
w.unwrap_or(0.0)
});
self.stacked.linear_map(&m)
}
}