use alloc::collections::VecDeque;
use alloc::vec;
use alloc::vec::Vec;
use crate::graph::{Heading, OrientedGraph, Vertex};
use crate::instance::Agent;
pub(super) struct Costs<'a, G> {
graph: &'a G,
headings: usize,
turns: Vec<u32>,
remaining: Vec<Vec<Option<u32>>>,
}
impl<'a, G: OrientedGraph> Costs<'a, G> {
pub(super) fn new(graph: &'a G, agents: &[Agent]) -> Self {
let headings = graph.heading_count();
let turns = turn_distances(graph, headings);
let predecessors = Predecessors::new(graph, headings);
let remaining = agents
.iter()
.map(|agent| predecessors.distances_to(agent.goal))
.collect();
Self {
graph,
headings,
turns,
remaining,
}
}
pub(super) fn remaining(&self, agent: usize, vertex: Vertex, heading: Heading) -> Option<u32> {
self.remaining[agent][vertex * self.headings + heading]
}
pub(super) fn least(&self, agent: usize, vertex: Vertex) -> Option<u32> {
(0..self.headings)
.filter_map(|heading| self.remaining(agent, vertex, heading))
.min()
}
pub(super) fn rank(
&self,
agent: usize,
vertex: Vertex,
heading: Heading,
target: Vertex,
) -> Option<u32> {
if target == vertex {
return self
.remaining(agent, vertex, heading)
.map(|steps| steps + 1);
}
self.facing(vertex, target)
.filter_map(|facing| {
let rest = self.remaining(agent, target, facing)?;
Some(self.turns(heading, facing) + 1 + rest)
})
.min()
}
pub(super) fn action(
&self,
vertex: Vertex,
heading: Heading,
target: Vertex,
) -> (Vertex, Heading) {
if target == vertex {
return (vertex, heading);
}
if self.graph.forward(vertex, heading) == Some(target) {
return (target, heading);
}
let goal = self
.facing(vertex, target)
.min_by_key(|&facing| self.turns(heading, facing))
.expect("the target is a neighbour");
let mut first = None;
self.graph.turns(heading, |next| {
let closer = self.turns(next, goal) + 1 == self.turns(heading, goal);
if first.is_none() && closer {
first = Some(next);
}
});
(vertex, first.expect("a turn leads closer to every heading"))
}
fn facing(&self, vertex: Vertex, target: Vertex) -> impl Iterator<Item = Heading> + '_ {
(0..self.headings)
.filter(move |&heading| self.graph.forward(vertex, heading) == Some(target))
}
fn turns(&self, from: Heading, to: Heading) -> u32 {
self.turns[from * self.headings + to]
}
}
fn turn_distances<G: OrientedGraph>(graph: &G, headings: usize) -> Vec<u32> {
let mut table = vec![u32::MAX; headings * headings];
for from in 0..headings {
let row = &mut table[from * headings..(from + 1) * headings];
row[from] = 0;
let mut queue = VecDeque::from([from]);
while let Some(heading) = queue.pop_front() {
let next_cost = row[heading] + 1;
graph.turns(heading, |next| {
if row[next] == u32::MAX {
row[next] = next_cost;
queue.push_back(next);
}
});
}
}
assert!(
table.iter().all(|&turns| turns != u32::MAX),
"turning reaches every heading"
);
table
}
struct Predecessors {
headings: usize,
moves: Vec<Vec<usize>>,
turns: Vec<Vec<Heading>>,
}
impl Predecessors {
fn new<G: OrientedGraph>(graph: &G, headings: usize) -> Self {
let mut moves = vec![Vec::new(); graph.vertex_count() * headings];
for vertex in 0..graph.vertex_count() {
for heading in 0..headings {
if let Some(next) = graph.forward(vertex, heading) {
moves[next * headings + heading].push(vertex * headings + heading);
}
}
}
let mut turns = vec![Vec::new(); headings];
for heading in 0..headings {
graph.turns(heading, |next| turns[next].push(heading));
}
Self {
headings,
moves,
turns,
}
}
fn distances_to(&self, goal: Vertex) -> Vec<Option<u32>> {
let mut distances = vec![None; self.moves.len()];
let mut queue = VecDeque::new();
for heading in 0..self.headings {
distances[goal * self.headings + heading] = Some(0);
queue.push_back(goal * self.headings + heading);
}
while let Some(state) = queue.pop_front() {
let next = distances[state].expect("queued states have a distance") + 1;
let (vertex, heading) = (state / self.headings, state % self.headings);
let behind = self.moves[state].iter().copied().chain(
self.turns[heading]
.iter()
.map(|&before| vertex * self.headings + before),
);
for previous in behind {
if distances[previous].is_none() {
distances[previous] = Some(next);
queue.push_back(previous);
}
}
}
distances
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::graph::Grid;
#[test]
fn counts_turns_and_moves() {
let grid = Grid::new(3, 1);
let costs = Costs::new(&grid, &[Agent { start: 0, goal: 2 }]);
assert_eq!(costs.remaining(0, 0, Grid::EAST), Some(2));
assert_eq!(costs.remaining(0, 0, Grid::NORTH), Some(3));
assert_eq!(costs.remaining(0, 0, Grid::WEST), Some(4));
assert_eq!(costs.least(0, 1), Some(1));
assert_eq!(costs.rank(0, 0, Grid::WEST, 1), Some(4));
assert_eq!(costs.rank(0, 0, Grid::WEST, 0), Some(5));
}
#[test]
fn computes_actions() {
let grid = Grid::new(3, 3);
let costs = Costs::new(&grid, &[Agent { start: 4, goal: 4 }]);
assert_eq!(costs.action(4, Grid::EAST, 5), (5, Grid::EAST));
assert_eq!(costs.action(4, Grid::EAST, 1), (4, Grid::NORTH));
assert_eq!(costs.action(4, Grid::EAST, 7), (4, Grid::SOUTH));
assert_eq!(costs.action(4, Grid::EAST, 3), (4, Grid::NORTH));
assert_eq!(costs.action(4, Grid::EAST, 4), (4, Grid::EAST));
}
}