Skip to main content

mapf_search/
path.rs

1//! Single-agent paths over discrete time and the joint objectives over them
2//! ([`Objective`]): [`sum_of_costs`] and [`makespan`].
3//!
4//! A path lists the vertex an agent occupies at each time step, so `path[t]` is the
5//! position at time `t`. After its last entry the agent stays at that vertex forever
6//! (goal-stay semantics), see [`position_at`].
7
8use alloc::vec::Vec;
9
10use crate::graph::Vertex;
11
12/// A time step. Time starts at `0`.
13pub type Time = u32;
14
15/// The positions of one agent at `t = 0, 1, ...`. Never empty in a solution.
16pub type Path = Vec<Vertex>;
17
18/// What a multi-agent search minimizes.
19#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20pub enum Objective {
21    /// The sum over agents of the final arrival time at the goal ([`sum_of_costs`]).
22    SumOfCosts,
23    /// The time the last agent arrives at its goal for good ([`makespan`]).
24    Makespan,
25}
26
27/// The vertex `path` occupies at `time`, counting the stay at the last vertex.
28///
29/// # Panics
30///
31/// Panics if `path` is empty.
32#[must_use]
33pub fn position_at(path: &[Vertex], time: Time) -> Vertex {
34    let index = usize::try_from(time).unwrap_or(usize::MAX);
35
36    path.get(index)
37        .or(path.last())
38        .copied()
39        .expect("a path has at least one position")
40}
41
42/// The cost of one path: the time of its final arrival at its last vertex.
43///
44/// Waits before the final arrival count, also waits at the goal before leaving it again.
45/// Trailing waits at the last vertex do not. An empty path costs `0`.
46///
47/// # Panics
48///
49/// Panics if a path has more than `u32::MAX` positions.
50#[must_use]
51pub fn path_cost(path: &[Vertex]) -> u32 {
52    let Some(&last) = path.last() else {
53        return 0;
54    };
55
56    let trailing = path.iter().rev().take_while(|&&v| v == last).count();
57    let arrival = path.len() - trailing;
58
59    u32::try_from(arrival).expect("path lengths fit in u32 time steps")
60}
61
62/// The sum of the [`path_cost`]s of all paths.
63///
64/// # Panics
65///
66/// Panics if a path has more than `u32::MAX` positions.
67#[must_use]
68pub fn sum_of_costs<P: AsRef<[Vertex]>>(paths: &[P]) -> u32 {
69    paths.iter().map(|path| path_cost(path.as_ref())).sum()
70}
71
72/// The largest [`path_cost`] of all paths: when the last agent arrives for good. `0` for
73/// no paths.
74///
75/// # Panics
76///
77/// Panics if a path has more than `u32::MAX` positions.
78#[must_use]
79pub fn makespan<P: AsRef<[Vertex]>>(paths: &[P]) -> u32 {
80    paths
81        .iter()
82        .map(|path| path_cost(path.as_ref()))
83        .max()
84        .unwrap_or(0)
85}
86
87#[cfg(test)]
88mod tests {
89    //! Unit tests for path costs.
90
91    use super::*;
92    use alloc::vec;
93
94    /// Trailing waits are free; waits before the final arrival are not.
95    #[test]
96    fn cost_counts_until_final_arrival() {
97        assert_eq!(path_cost(&[3]), 0);
98        assert_eq!(path_cost(&[0, 1, 2]), 2);
99        assert_eq!(path_cost(&[0, 1, 1, 2, 2, 2]), 3);
100        assert_eq!(path_cost(&[2, 1, 2]), 2);
101        assert_eq!(sum_of_costs(&[vec![0, 1], vec![5, 5, 4]]), 3);
102        assert_eq!(makespan(&[vec![0, 1], vec![5, 5, 4]]), 2);
103    }
104
105    /// The last position holds forever.
106    #[test]
107    fn position_stays_at_goal() {
108        assert_eq!(position_at(&[0, 1, 2], 1), 1);
109        assert_eq!(position_at(&[0, 1, 2], 10), 2);
110    }
111}