use alloc::vec::Vec;
mod planar;
use crate::mo::simplex::{dot, is_covered};
pub(crate) const NO_ACTION: u32 = u32::MAX;
#[derive(Debug, Clone, Copy, PartialEq)]
pub(crate) struct Point<const N: usize> {
pub(crate) vector: [f64; N],
pub(crate) action: u32,
}
#[derive(Debug, Clone, Default, PartialEq)]
pub(crate) struct ValueSet<const N: usize> {
pub(crate) points: Vec<Point<N>>,
pub(crate) bound: Vec<u32>,
}
impl<const N: usize> ValueSet<N> {
pub(crate) fn goal() -> Self {
Self {
points: alloc::vec![Point {
vector: [0.0; N],
action: NO_ACTION,
}],
bound: Vec::new(),
}
}
pub(crate) fn dead_end() -> Self {
Self {
points: Vec::new(),
bound: alloc::vec![NO_ACTION],
}
}
pub(crate) fn has_bound(&self) -> bool {
!self.bound.is_empty()
}
pub(crate) fn vectors(&self) -> impl Iterator<Item = &[f64; N]> {
self.points.iter().map(|point| &point.vector)
}
}
pub(crate) fn dominates<const N: usize>(u: &[f64; N], v: &[f64; N]) -> bool {
u.iter().zip(v).all(|(a, b)| a <= b)
}
#[derive(Debug, Default)]
pub(crate) struct Scratch<const N: usize> {
others: Vec<[f64; N]>,
tableau: Vec<f64>,
}
pub(crate) fn prune<const N: usize>(
points: &mut Vec<Point<N>>,
margin: f64,
scratch: &mut Scratch<N>,
) {
match N {
1 => keep_minimum(points),
2 => planar::prune(points, margin),
_ => prune_by_programs(points, margin, scratch),
}
}
fn keep_minimum<const N: usize>(points: &mut Vec<Point<N>>) {
let best = points
.iter()
.enumerate()
.min_by(|(i, a), (j, b)| a.vector[0].total_cmp(&b.vector[0]).then(i.cmp(j)))
.map(|(index, _)| index);
if let Some(best) = best {
points.swap(0, best);
points.truncate(1);
}
}
fn prune_by_programs<const N: usize>(
points: &mut Vec<Point<N>>,
margin: f64,
scratch: &mut Scratch<N>,
) {
remove_dominated(points);
if points.len() <= 1 {
return;
}
points.sort_by(|a, b| sum(&b.vector).total_cmp(&sum(&a.vector)));
let mut index = 0;
while index < points.len() {
scratch.others.clear();
scratch.others.extend(
points
.iter()
.enumerate()
.filter(|&(other, _)| other != index)
.map(|(_, point)| point.vector),
);
if is_covered(
&points[index].vector,
&scratch.others,
margin,
&mut scratch.tableau,
) {
points.remove(index);
} else {
index += 1;
}
}
}
fn remove_dominated<const N: usize>(points: &mut Vec<Point<N>>) {
let mut index = 0;
while index < points.len() {
let v = points[index].vector;
let beaten = points.iter().enumerate().any(|(other, point)| {
other != index
&& dominates(&point.vector, &v)
&& (!same(&point.vector, &v) || other < index)
});
if beaten {
points.remove(index);
} else {
index += 1;
}
}
}
fn same<const N: usize>(u: &[f64; N], v: &[f64; N]) -> bool {
dominates(u, v) && dominates(v, u)
}
pub(crate) fn sum<const N: usize>(v: &[f64; N]) -> f64 {
v.iter().sum()
}
pub(crate) fn add_scaled<const N: usize>(
acc: &[Point<N>],
set: &ValueSet<N>,
probability: f64,
bound: &[f64; N],
out: &mut Vec<Point<N>>,
) -> bool {
out.clear();
let mut exceeded = set.has_bound();
for left in acc {
for right in set.vectors() {
let mut vector = left.vector;
for (component, add) in vector.iter_mut().zip(right) {
*component += probability * add;
}
if dominates(&vector, bound) {
out.push(Point {
vector,
action: left.action,
});
} else {
exceeded = true;
}
}
}
exceeded
}
pub(crate) fn hausdorff<const N: usize>(a: &ValueSet<N>, b: &ValueSet<N>, bound: &[f64; N]) -> f64 {
directed(a, b, bound).max(directed(b, a, bound))
}
fn directed<const N: usize>(a: &ValueSet<N>, b: &ValueSet<N>, bound: &[f64; N]) -> f64 {
members(a, bound)
.map(|u| {
members(b, bound)
.map(|v| distance(u, v))
.fold(f64::INFINITY, f64::min)
})
.fold(0.0, f64::max)
}
fn members<'a, const N: usize>(
set: &'a ValueSet<N>,
bound: &'a [f64; N],
) -> impl Iterator<Item = &'a [f64; N]> {
set.vectors().chain(set.has_bound().then_some(bound))
}
fn distance<const N: usize>(u: &[f64; N], v: &[f64; N]) -> f64 {
u.iter()
.zip(v)
.map(|(a, b)| libm::fabs(a - b))
.fold(0.0, f64::max)
}
pub(crate) fn best_under<const N: usize>(set: &ValueSet<N>, weight: &[f64; N]) -> Option<usize> {
set.points
.iter()
.enumerate()
.min_by(|(_, a), (_, b)| dot(weight, &a.vector).total_cmp(&dot(weight, &b.vector)))
.map(|(index, _)| index)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::rng::SplitMix64;
fn points<const N: usize>(vectors: &[[f64; N]]) -> Vec<Point<N>> {
vectors
.iter()
.map(|&vector| Point {
vector,
action: NO_ACTION,
})
.collect()
}
fn lower_left_hull(input: &[[f64; 2]]) -> Vec<[f64; 2]> {
let mut front: Vec<[f64; 2]> = input
.iter()
.copied()
.filter(|v| !input.iter().any(|u| dominates(u, v) && !same(u, v)))
.collect();
front.sort_by(|a, b| a[0].total_cmp(&b[0]));
front.dedup();
let mut hull: Vec<[f64; 2]> = Vec::new();
for point in front {
while hull.len() >= 2 {
let (a, b) = (hull[hull.len() - 2], hull[hull.len() - 1]);
let cross = (b[0] - a[0]) * (point[1] - a[1]) - (b[1] - a[1]) * (point[0] - a[0]);
if cross <= 1e-12 {
hull.pop();
} else {
break;
}
}
hull.push(point);
}
hull
}
#[test]
fn prune_matches_the_monotone_chain_hull() {
let mut rng = SplitMix64::new(7);
let mut scratch = Scratch::default();
for _ in 0..300 {
let count = 1 + (rng.next_u64() % 12) as usize;
let vectors: Vec<[f64; 2]> = (0..count)
.map(|_| {
[
f64::from(u8::try_from(rng.next_u64() % 20).unwrap()) / 2.0,
f64::from(u8::try_from(rng.next_u64() % 20).unwrap()) / 2.0,
]
})
.collect();
let mut planar = points(&vectors);
prune(&mut planar, 1e-9, &mut scratch);
let mut general = points(&vectors);
prune_by_programs(&mut general, 1e-9, &mut scratch);
for set in [planar, general] {
let mut kept: Vec<[f64; 2]> = set.iter().map(|point| point.vector).collect();
kept.sort_by(|a, b| a[0].total_cmp(&b[0]));
assert_eq!(kept, lower_left_hull(&vectors), "input {vectors:?}");
}
}
}
#[test]
fn prune_drops_edge_points_and_one_twin() {
let mut scratch = Scratch::default();
let mut set = points(&[[2.0, 0.0], [1.0, 1.0], [0.0, 2.0], [0.0, 2.0 + 1e-12]]);
prune(&mut set, 1e-9, &mut scratch);
assert_eq!(set.len(), 2);
let mut twins = points(&[[1.0, 1.0], [1.0 + 1e-12, 1.0 - 1e-12]]);
prune(&mut twins, 1e-9, &mut scratch);
assert_eq!(twins.len(), 1);
let mut set = points(&[[2.0, 0.0], [1.0, 1.0], [0.0, 2.0], [0.0, 2.0 + 1e-12]]);
prune_by_programs(&mut set, 1e-9, &mut scratch);
assert_eq!(set.len(), 2);
let mut twins = points(&[[1.0, 1.0], [1.0 + 1e-12, 1.0 - 1e-12]]);
prune_by_programs(&mut twins, 1e-9, &mut scratch);
assert_eq!(twins.len(), 1);
}
#[test]
fn prune_single_objective_keeps_the_minimum() {
let mut scratch = Scratch::default();
let mut set = points(&[[3.0], [1.0], [1.0 + 1e-12], [2.0]]);
prune(&mut set, 1e-9, &mut scratch);
assert_eq!(set.len(), 1);
assert_eq!(set[0].vector, [1.0]);
}
#[test]
fn bounded_sum() {
let acc = points(&[[1.0, 0.0]]);
let set = ValueSet {
points: points(&[[2.0, 2.0], [20.0, 0.0]]),
bound: Vec::new(),
};
let mut out = Vec::new();
let exceeded = add_scaled(&acc, &set, 0.5, &[5.0, 5.0], &mut out);
assert!(exceeded);
assert_eq!(out.len(), 1);
assert_eq!(out[0].vector, [2.0, 1.0]);
}
#[test]
fn hausdorff_with_bound() {
let bound = [10.0, 10.0];
let a = ValueSet {
points: points(&[[1.0, 1.0]]),
bound: Vec::new(),
};
let mut b = a.clone();
assert_eq!(hausdorff(&a, &b, &bound), 0.0);
b.bound.push(0);
assert_eq!(hausdorff(&a, &b, &bound), 9.0);
assert!(hausdorff(&a, &ValueSet::default(), &bound).is_infinite());
}
}