use alloc::rc::Rc;
use super::Child;
use super::node::{Node, ParetoSet};
use crate::cbs::Splitting;
use crate::conflict::{Conflict, conflicts, first_conflict};
use crate::constraint::Constraint;
use crate::graph::Vertex;
pub(super) fn choose<const N: usize>(node: &Node<N>, prioritize: bool) -> Option<Conflict> {
if !prioritize {
return first_conflict(&node.paths);
}
conflicts(&node.paths)
.into_iter()
.enumerate()
.min_by_key(|&(index, conflict)| (class(node, conflict), index))
.map(|(_, conflict)| conflict)
}
fn class<const N: usize>(node: &Node<N>, conflict: Conflict) -> usize {
let [a, b] = cardinal(node, conflict);
2 - usize::from(a) - usize::from(b)
}
fn cardinal<const N: usize>(node: &Node<N>, conflict: Conflict) -> [bool; 2] {
let (a, b) = conflict.agents();
let negatives = Constraint::resolving(conflict);
[a, b].map(|agent| {
let constraint = if agent == a {
negatives[0]
} else {
negatives[1]
};
node.pareto.get(agent).is_some_and(|set: &ParetoSet<N>| {
set.iter().all(|(path, _)| constraint.is_violated_by(path))
})
})
}
pub(super) fn constraints<const N: usize>(
node: &Node<N>,
conflict: Conflict,
splitting: Splitting,
) -> [Constraint; 2] {
match splitting {
Splitting::Standard => Constraint::resolving(conflict),
Splitting::Disjoint => {
let side = usize::from(cardinal(node, conflict) == [false, true]);
Constraint::disjoint(conflict, side)
}
}
}
pub(super) fn bypass<const N: usize>(parent: &Node<N>, child: &Child<N>) -> Option<Node<N>> {
let mut paths = parent.paths.clone();
let mut pareto = parent.pareto.clone();
for (agent, set) in &child.replanned {
let cost = parent.costs[*agent];
let (path, _) = set.iter().find(|(_, c)| *c == cost)?;
paths[*agent] = Rc::clone(path);
if let Some(slot) = pareto.get_mut(*agent) {
*slot = with_representative(slot, path, cost);
}
}
if conflicts(&paths).len() >= conflicts(&parent.paths).len() {
return None;
}
Some(Node::new(
parent.constraints.clone(),
paths,
parent.costs.clone(),
pareto,
))
}
fn with_representative<const N: usize>(
set: &ParetoSet<N>,
path: &Rc<[Vertex]>,
cost: [u32; N],
) -> ParetoSet<N> {
set.iter()
.map(|(p, c)| {
if *c == cost {
(Rc::clone(path), *c)
} else {
(Rc::clone(p), *c)
}
})
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
use alloc::vec::Vec;
fn path(vertices: &[Vertex]) -> Rc<[Vertex]> {
Rc::from(vertices)
}
#[test]
fn prioritization_prefers_cardinal_conflicts() {
let a = [path(&[0, 4, 2]), path(&[0, 5, 2])];
let b = path(&[4, 4, 2, 6]);
let pareto: Vec<ParetoSet<1>> = vec![
Rc::from([(Rc::clone(&a[0]), [2]), (Rc::clone(&a[1]), [3])]),
Rc::from([(Rc::clone(&b), [3])]),
];
let node = Node::new(
Vec::new(),
vec![Rc::clone(&a[0]), b],
vec![[2], [3]],
pareto,
);
let earliest = first_conflict(&node.paths).expect("they meet at 4");
let chosen = choose(&node, true).expect("they conflict");
assert_eq!(earliest.time(), 1);
assert_eq!(
chosen.time(),
2,
"vertex 2 at time 2 is on every path of both"
);
}
#[test]
fn bypass_adopts_an_equally_cheap_path() {
let parent = Node::new(
Vec::new(),
vec![path(&[0, 1, 2]), path(&[5, 1])],
vec![[2], [1]],
Vec::new(),
);
let child = Child {
constraints: Vec::new(),
replanned: vec![(0, Rc::from([(path(&[0, 4, 2]), [2])]))],
};
let adopted = bypass(&parent, &child).expect("fewer conflicts at the same cost");
assert_eq!(&*adopted.paths[0], [0, 4, 2]);
assert_eq!(adopted.cost, parent.cost);
assert_eq!(first_conflict(&adopted.paths), None);
}
}