mapf_search/solution.rs
1//! What every solver returns: a [`Solution`], how the run ended ([`Outcome`]) and its
2//! counters ([`MapfResult`]).
3
4use alloc::vec::Vec;
5
6use heuristic_search::Stats;
7
8use crate::path::{Objective, Path, makespan, sum_of_costs};
9
10/// A conflict-free plan: one path per agent.
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Solution {
13 /// The path of each agent, indexed by [`AgentId`](crate::AgentId). Each starts at the
14 /// agent's start and ends at its goal, where the agent stays.
15 pub paths: Vec<Path>,
16 /// The sum of costs of [`Solution::paths`] (see [`sum_of_costs`]).
17 pub sum_of_costs: u32,
18 /// The makespan of [`Solution::paths`] (see [`makespan`](crate::makespan())).
19 pub makespan: u32,
20}
21
22impl Solution {
23 /// A solution of `paths`, with both objectives computed.
24 pub(crate) fn new(paths: Vec<Path>) -> Self {
25 Self {
26 sum_of_costs: sum_of_costs(&paths),
27 makespan: makespan(&paths),
28 paths,
29 }
30 }
31
32 /// The value of `objective` for this solution.
33 #[must_use]
34 pub fn cost(&self, objective: Objective) -> u32 {
35 match objective {
36 Objective::SumOfCosts => self.sum_of_costs,
37 Objective::Makespan => self.makespan,
38 }
39 }
40}
41
42/// How a solver run ended.
43#[derive(Debug, Clone, PartialEq, Eq)]
44pub enum Outcome {
45 /// A conflict-free solution. Its quality is what the solver guarantees: optimal for
46 /// CBS, CBS-TA, ICTS, M* and EPEA*, within the suboptimality factor for ECBS, none for PBS,
47 /// PIBT and LNS2.
48 Solved(Solution),
49 /// Proven unsolvable: an agent cannot reach its goal even alone, or a complete solver
50 /// ran out of options.
51 Unsolvable,
52 /// The [`Budget`](crate::Budget) stopped the search. The instance may be unsolvable:
53 /// for some unsolvable instances, such as two agents swapping the ends of a corridor,
54 /// the search space is infinite and complete solvers only ever stop this way.
55 LimitReached,
56 /// An incomplete solver ([`pbs`](crate::pbs())) ran out of options before its budget.
57 /// This proves nothing: the instance may still be solvable.
58 Exhausted,
59}
60
61/// The result of a solver run.
62#[derive(Debug, Clone, PartialEq, Eq)]
63pub struct MapfResult {
64 /// How the run ended.
65 pub outcome: Outcome,
66 /// High-level counters; each solver documents what it counts.
67 pub stats: Stats,
68}
69
70impl MapfResult {
71 /// The solution, if one was found.
72 #[must_use]
73 pub fn solution(&self) -> Option<&Solution> {
74 match &self.outcome {
75 Outcome::Solved(solution) => Some(solution),
76 Outcome::Unsolvable | Outcome::LimitReached | Outcome::Exhausted => None,
77 }
78 }
79}