use alloc::rc::Rc;
use alloc::vec;
use alloc::vec::Vec;
use multiobjective_search::add;
use super::Child;
use crate::constraint::Constraint;
use crate::graph::Vertex;
pub(super) type PricedPath<const N: usize> = (Rc<[Vertex]>, [u32; N]);
pub(super) type ParetoSet<const N: usize> = Rc<[PricedPath<N>]>;
#[derive(Clone)]
pub(super) struct Node<const N: usize> {
pub(super) constraints: Vec<Constraint>,
pub(super) paths: Vec<Rc<[Vertex]>>,
pub(super) costs: Vec<[u32; N]>,
pub(super) cost: [u32; N],
pub(super) pareto: Vec<ParetoSet<N>>,
}
impl<const N: usize> Node<N> {
pub(super) fn new(
constraints: Vec<Constraint>,
paths: Vec<Rc<[Vertex]>>,
costs: Vec<[u32; N]>,
pareto: Vec<ParetoSet<N>>,
) -> Self {
let cost = costs.iter().fold([0; N], |sum, &c| add(sum, c));
Self {
constraints,
paths,
costs,
cost,
pareto,
}
}
}
pub(super) struct Combinations<const N: usize> {
constraints: Vec<Constraint>,
pareto: Vec<ParetoSet<N>>,
choices: Vec<ParetoSet<N>>,
next: Option<Vec<usize>>,
}
impl<const N: usize> Combinations<N> {
pub(super) fn roots(sets: Vec<ParetoSet<N>>) -> Self {
Self::new(Vec::new(), sets.clone(), sets)
}
pub(super) fn children(parent: &Node<N>, child: Child<N>) -> Self {
let mut pareto = parent.pareto.clone();
let mut choices: Vec<ParetoSet<N>> = parent
.paths
.iter()
.zip(&parent.costs)
.map(|(path, &cost)| Rc::from([(Rc::clone(path), cost)]))
.collect();
for (agent, set) in child.replanned {
if let Some(slot) = pareto.get_mut(agent) {
slot.clone_from(&set);
}
choices[agent] = set;
}
Self::new(child.constraints, pareto, choices)
}
fn new(
constraints: Vec<Constraint>,
pareto: Vec<ParetoSet<N>>,
choices: Vec<ParetoSet<N>>,
) -> Self {
let next = Some(vec![0; choices.len()]);
Self {
constraints,
pareto,
choices,
next,
}
}
fn advance(&self, indices: &mut [usize]) -> bool {
for (agent, index) in indices.iter_mut().enumerate().rev() {
*index += 1;
if *index < self.choices[agent].len() {
return true;
}
*index = 0;
}
false
}
}
impl<const N: usize> Iterator for Combinations<N> {
type Item = Node<N>;
fn next(&mut self) -> Option<Node<N>> {
let mut indices = self.next.take()?;
let (paths, costs) = indices
.iter()
.zip(&self.choices)
.map(|(&index, choices)| choices[index].clone())
.unzip();
if self.advance(&mut indices) {
self.next = Some(indices);
}
Some(Node::new(
self.constraints.clone(),
paths,
costs,
self.pareto.clone(),
))
}
}
#[cfg(test)]
mod tests {
use super::*;
fn priced(vertex: Vertex, cost: u32) -> PricedPath<1> {
(Rc::from(vec![vertex]), [cost])
}
#[test]
fn roots_enumerate_the_product() {
let roots = Combinations::roots(vec![
Rc::from([priced(0, 1), priced(1, 2)]),
Rc::from([priced(5, 10), priced(6, 20), priced(7, 30)]),
]);
let costs: Vec<[u32; 1]> = roots.map(|node| node.cost).collect();
assert_eq!(costs, [[11], [21], [31], [12], [22], [32]]);
}
#[test]
fn children_vary_only_the_replanned_agents() {
let sets: Vec<ParetoSet<1>> = vec![
Rc::from([priced(0, 1), priced(1, 2)]),
Rc::from([priced(5, 10), priced(6, 20)]),
];
let parent = Combinations::roots(sets).nth(1).expect("four roots");
let child = Child {
constraints: Vec::new(),
replanned: vec![(1, Rc::from([priced(8, 40), priced(9, 50)]))],
};
let costs: Vec<[u32; 1]> = Combinations::children(&parent, child)
.map(|node| node.cost)
.collect();
assert_eq!(costs, [[41], [51]]);
}
}