use alloc::vec::Vec;
use crate::graph::Vertex;
use crate::instance::AgentId;
use crate::path::{Time, position_at};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Conflict {
Vertex {
agents: (AgentId, AgentId),
vertex: Vertex,
time: Time,
},
Edge {
agents: (AgentId, AgentId),
from: Vertex,
to: Vertex,
time: Time,
},
}
impl Conflict {
#[must_use]
pub fn agents(&self) -> (AgentId, AgentId) {
match *self {
Self::Vertex { agents, .. } | Self::Edge { agents, .. } => agents,
}
}
#[must_use]
pub fn time(&self) -> Time {
match *self {
Self::Vertex { time, .. } | Self::Edge { time, .. } => time,
}
}
}
#[must_use]
pub fn first_conflict<P: AsRef<[Vertex]>>(paths: &[P]) -> Option<Conflict> {
let mut found = None;
scan(paths, |conflict| {
found = Some(conflict);
false
});
found
}
#[must_use]
pub fn conflicts<P: AsRef<[Vertex]>>(paths: &[P]) -> Vec<Conflict> {
let mut found = Vec::new();
scan(paths, |conflict| {
found.push(conflict);
true
});
found
}
pub(crate) fn update_conflicts<P: AsRef<[Vertex]>>(
found: &[Conflict],
paths: &[P],
changed: &[AgentId],
) -> Vec<Conflict> {
let paths: Vec<&[Vertex]> = paths.iter().map(AsRef::as_ref).collect();
let involved = |conflict: &Conflict| {
let (a, b) = conflict.agents();
changed.contains(&a) || changed.contains(&b)
};
let mut updated: Vec<Conflict> = found.iter().filter(|c| !involved(c)).copied().collect();
for time in 0..horizon(&paths) {
for &a in changed {
let others = (0..paths.len()).filter(|&b| b != a && !(b < a && changed.contains(&b)));
for b in others {
let pair = (a.min(b), a.max(b));
let found = conflicts_at(paths[pair.0], paths[pair.1], pair, time);
updated.extend(found.into_iter().flatten());
}
}
}
updated.sort_unstable_by_key(scan_order);
updated
}
fn scan_order(conflict: &Conflict) -> (Time, (AgentId, AgentId), bool) {
(
conflict.time(),
conflict.agents(),
matches!(conflict, Conflict::Edge { .. }),
)
}
fn horizon(paths: &[&[Vertex]]) -> Time {
let longest = paths.iter().map(|path| path.len()).max().unwrap_or(0);
Time::try_from(longest).expect("path lengths fit in u32 time steps")
}
fn scan<P: AsRef<[Vertex]>>(paths: &[P], mut visit: impl FnMut(Conflict) -> bool) {
let paths: Vec<&[Vertex]> = paths.iter().map(AsRef::as_ref).collect();
for time in 0..horizon(&paths) {
if !scan_time(&paths, time, &mut visit) {
return;
}
}
}
fn scan_time(paths: &[&[Vertex]], time: Time, visit: &mut impl FnMut(Conflict) -> bool) -> bool {
for (i, a) in paths.iter().enumerate() {
for (j, b) in paths.iter().enumerate().skip(i + 1) {
let pair = conflicts_at(a, b, (i, j), time);
if pair.into_iter().flatten().any(|conflict| !visit(conflict)) {
return false;
}
}
}
true
}
fn conflicts_at(
a: &[Vertex],
b: &[Vertex],
agents: (AgentId, AgentId),
time: Time,
) -> [Option<Conflict>; 2] {
let (a_now, b_now) = (position_at(a, time), position_at(b, time));
let (a_next, b_next) = (position_at(a, time + 1), position_at(b, time + 1));
let vertex = (a_now == b_now).then_some(Conflict::Vertex {
agents,
vertex: a_now,
time,
});
let swap = (a_now != a_next && a_now == b_next && a_next == b_now).then_some(Conflict::Edge {
agents,
from: a_now,
to: a_next,
time,
});
[vertex, swap]
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
#[test]
fn update_matches_a_full_scan() {
let before = [vec![0, 1, 2], vec![2, 1, 0], vec![5, 1, 6], vec![7, 6, 5]];
let after = [
vec![0, 1, 2],
vec![2, 2, 1, 0],
vec![5, 1, 6],
vec![6, 6, 5],
];
let found = conflicts(&before);
for changed in [&[][..], &[1], &[3], &[1, 3], &[3, 1], &[0, 1, 2, 3]] {
let paths: Vec<_> = (0..4)
.map(|agent| {
if changed.contains(&agent) {
&after[agent]
} else {
&before[agent]
}
})
.collect();
assert_eq!(
update_conflicts(&found, &paths, changed),
conflicts(&paths),
"changed {changed:?}"
);
}
}
}