mod landmarks;
use alloc::vec::Vec;
use heuristic_search::{Cost, Limits, Successors, a_star, heuristic_fn};
use search_core::Lexicographic;
use self::landmarks::Landmarks;
use crate::conflict_table::ConflictTable;
use crate::constraint::ConstraintSet;
use crate::graph::{Graph, Vertex, distances_to};
use crate::instance::Agent;
use crate::path::{Path, Time};
type State = (Vertex, Time);
#[must_use]
pub fn space_time_astar<G: Graph>(
graph: &G,
agent: Agent,
constraints: &ConstraintSet,
) -> Option<Path> {
let distances = distances_to(graph, agent.goal);
plan(graph, agent, constraints, &distances)
}
pub(crate) fn plan<G: Graph>(
graph: &G,
agent: Agent,
constraints: &ConstraintSet,
distances: &[Option<u32>],
) -> Option<Path> {
search(graph, agent, constraints, distances, Plain)
}
pub(crate) fn plan_avoiding<G: Graph>(
graph: &G,
agent: Agent,
constraints: &ConstraintSet,
distances: &[Option<u32>],
table: &ConflictTable,
) -> Option<Path> {
search(graph, agent, constraints, distances, Avoiding(table))
}
trait Pricing: Copy {
type Cost: Cost;
fn step(self, from: Vertex, to: Vertex, time: Time) -> Self::Cost;
fn estimate(steps: u32) -> Self::Cost;
}
#[derive(Debug, Clone, Copy)]
struct Plain;
impl Pricing for Plain {
type Cost = u32;
fn step(self, _: Vertex, _: Vertex, _: Time) -> u32 {
1
}
fn estimate(steps: u32) -> u32 {
steps
}
}
#[derive(Debug, Clone, Copy)]
struct Avoiding<'a>(&'a ConflictTable);
impl Pricing for Avoiding<'_> {
type Cost = Lexicographic<u32, u32>;
fn step(self, from: Vertex, to: Vertex, time: Time) -> Lexicographic<u32, u32> {
Lexicographic {
primary: 1,
secondary: self.0.count(from, to, time),
}
}
fn estimate(steps: u32) -> Lexicographic<u32, u32> {
Lexicographic {
primary: steps,
secondary: 0,
}
}
}
fn search<G: Graph, P: Pricing>(
graph: &G,
agent: Agent,
constraints: &ConstraintSet,
distances: &[Option<u32>],
pricing: P,
) -> Option<Path> {
if !constraints.allows_vertex(agent.start, 0) {
return None;
}
let space = SpaceTime::new(graph, agent, constraints, pricing);
let earliest_finish = space.earliest_finish;
let landmarks = Landmarks::new(graph, constraints, distances, earliest_finish)?;
let goal = |&(vertex, time): &State| vertex == agent.goal && time >= earliest_finish;
let heuristic = heuristic_fn(|&(vertex, time): &State| {
let direct = distances[vertex]?.max(earliest_finish.saturating_sub(time));
let via = landmarks.estimate(vertex, time)?;
Some(P::estimate(direct.max(via)))
});
let result = a_star(&space, (agent.start, 0), &goal, heuristic, Limits::NONE);
let solution = result.into_solution()?;
Some(
solution
.states
.into_iter()
.map(|(vertex, _)| vertex)
.collect(),
)
}
struct SpaceTime<'a, G, P> {
graph: &'a G,
constraints: &'a ConstraintSet,
pricing: P,
earliest_finish: Time,
horizon: Time,
}
impl<'a, G: Graph, P> SpaceTime<'a, G, P> {
fn new(graph: &'a G, agent: Agent, constraints: &'a ConstraintSet, pricing: P) -> Self {
let vertices = Time::try_from(graph.vertex_count()).unwrap_or(Time::MAX);
Self {
graph,
constraints,
pricing,
earliest_finish: constraints
.blocked_until(agent.goal)
.map_or(0, |t| t.saturating_add(1)),
horizon: constraints.latest_step().saturating_add(vertices),
}
}
}
impl<G: Graph, P: Pricing> Successors for SpaceTime<'_, G, P> {
type State = State;
type Action = ();
type Cost = P::Cost;
fn successors(&self, &(vertex, time): &State, mut emit: impl FnMut((), State, P::Cost)) {
if time >= self.horizon {
return;
}
let mut step = |next: Vertex| {
if self.constraints.allows(vertex, next, time) {
emit((), (next, time + 1), self.pricing.step(vertex, next, time));
}
};
step(vertex);
self.graph.neighbors(vertex, step);
}
}
pub(crate) fn distance_tables<G: Graph>(graph: &G, agents: &[Agent]) -> Vec<Vec<Option<u32>>> {
agents
.iter()
.map(|agent| distances_to(graph, agent.goal))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::vec;
use crate::conflict::conflicts;
use crate::graph::Grid;
#[test]
fn avoids_conflicts_among_the_cheapest_paths() {
let grid = Grid::new(3, 3);
let agent = Agent { start: 0, goal: 8 };
let distances = distances_to(&grid, agent.goal);
let others = [vec![4, 4, 4, 4, 2]];
let table = ConflictTable::without(&others, 9);
let path = plan_avoiding(&grid, agent, &ConstraintSet::new(), &distances, &table)
.expect("reachable");
assert_eq!(path.len(), 5, "still a cheapest path: {path:?}");
assert_eq!(conflicts(&[path, others[0].clone()]), []);
}
}