Skip to main content

mapf_search/
lib.rs

1//! # Multi-agent pathfinding
2//!
3//! Multi-agent pathfinding (MAPF): one path per agent on a shared graph, without
4//! collisions, minimizing a joint objective.
5//!
6//! An [`Instance`] is a [`Graph`] (such as a [`Grid`] or an [`AdjacencyList`]) and a list
7//! of [`Agent`]s with distinct starts and goals. Time is discrete; each step an agent moves
8//! along an edge or waits, at cost 1. After its path ends, an agent stays at its goal and
9//! keeps blocking it. Two agents [`Conflict`] if they share a vertex at the same time or
10//! swap vertices along an edge.
11//!
12//! - [`cbs()`]: conflict-based search, optimal for [`sum_of_costs`] or [`makespan`]
13//!   ([`Objective`]), with prioritized conflicts, bypass, the CG, DG or WDG heuristic
14//!   ([`CbsHeuristic`]), a conflict
15//!   avoidance table and disjoint [`Splitting`]; [`cbs_with`] and [`CbsConfig`] switch
16//!   them individually.
17//! - [`ecbs()`]: enhanced CBS, bounded-suboptimal within a [`Weight`] `w`, with focal search
18//!   at both levels.
19//! - [`icts()`]: increasing cost tree search with pairwise pruning, optimal.
20//! - [`mstar()`]: recursive M* with operator decomposition (subdimensional expansion),
21//!   optimal, and able to prove unsolvability; [`mstar_with`] and [`MStarConfig`] select
22//!   basic M*, recursive M* without operator decomposition, and OD-M*.
23//! - [`pbs()`]: priority-based search, fast but neither optimal nor complete.
24//! - `mo_cbs`, `mo_cbs_with` and `mo_cbs_per_agent`, with the `multi-objective` feature:
25//!   multi-objective CBS (MO-CBS and MO-CBS-t, over BOA* or NAMOA*dr), every
26//!   Pareto-optimal plan when moves and waits cost vectors, shared or per agent, with an
27//!   opt-in feasibility check, conflict prioritization, bypass and disjoint splitting.
28//!
29//! Every solver takes an [`Objective`]: sum of costs or makespan.
30//! - [`space_time_astar`]: the single-agent low level under [`Constraint`]s, built on the
31//!   `heuristic-search` crate's A*.
32//! - [`first_conflict`] and [`conflicts`]: conflict detection on any set of paths.
33//! - [`parse_map`], [`parse_scenario`] and [`scenario_instance`]: Moving AI benchmarks.
34//!
35//! Every solver returns a [`MapfResult`]: an [`Outcome`] (with a [`Solution`] if solved)
36//! and high-level [`Stats`]. [`Budget`], [`Limits`] and [`Stats`] are re-exported from
37//! `heuristic-search`; they bound and count the high-level expansions. [`Weight`] is too.
38//!
39//! The crate is `no_std` and needs only `alloc`.
40//!
41//! ## Example
42//!
43//! ```
44//! use mapf_search::{Agent, Grid, Instance, Limits, Objective, cbs, first_conflict};
45//!
46//! // Two agents swap the ends of a corridor that has one side pocket.
47//! let grid = Grid::parse(&["...", "@.@"]);
48//! let agents = vec![
49//!     Agent { start: grid.vertex(0, 0), goal: grid.vertex(2, 0) },
50//!     Agent { start: grid.vertex(2, 0), goal: grid.vertex(0, 0) },
51//! ];
52//! let instance = Instance::new(grid, agents)?;
53//!
54//! let result = cbs(&instance, Objective::SumOfCosts, Limits::expansions(1_000));
55//! let solution = result.solution().expect("the pocket lets them pass");
56//!
57//! assert_eq!(first_conflict(&solution.paths), None);
58//! assert_eq!(solution.sum_of_costs, 7);
59//! # Ok::<(), mapf_search::InstanceError>(())
60//! ```
61//!
62//! Not yet implemented: CBS-TA, MAPF-LNS2 and EPEA*.
63//!
64//! Repository: <https://github.com/sunsided/mapf-search>
65
66#![no_std]
67#![forbid(unsafe_code)]
68
69extern crate alloc;
70
71/// Doctests for the code examples in README.md (not part of the API).
72#[cfg(doctest)]
73#[doc = include_str!("../README.md")]
74mod readme_doctests {}
75
76mod cbs;
77mod conflict;
78mod conflict_table;
79mod constraint;
80mod ecbs;
81mod focal;
82mod graph;
83mod icts;
84mod instance;
85mod low_level;
86mod mdd;
87#[cfg(feature = "multi-objective")]
88mod mo_cbs;
89mod movingai;
90mod mstar;
91mod path;
92mod pbs;
93mod solution;
94
95pub use cbs::{CbsConfig, CbsHeuristic, Splitting, cbs, cbs_with};
96pub use conflict::{Conflict, conflicts, first_conflict};
97pub use constraint::{Constraint, ConstraintSet};
98pub use ecbs::ecbs;
99pub use graph::{AdjacencyList, Graph, Grid, Vertex};
100pub use heuristic_search::{Budget, Limits, Stats, Weight};
101pub use icts::icts;
102pub use instance::{Agent, AgentId, Instance, InstanceError};
103pub use low_level::space_time_astar;
104#[cfg(feature = "multi-objective")]
105pub use mo_cbs::{
106    LowLevel, MoCbsConfig, ParetoOutcome, ParetoResult, ParetoSolution, mo_cbs, mo_cbs_per_agent,
107    mo_cbs_with,
108};
109pub use movingai::{
110    ParseError, ParseErrorKind, ScenarioEntry, ScenarioError, parse_map, parse_scenario,
111    scenario_instance,
112};
113pub use mstar::{MStarConfig, mstar, mstar_with};
114pub use path::{Objective, Path, Time, makespan, path_cost, position_at, sum_of_costs};
115pub use pbs::pbs;
116pub use solution::{MapfResult, Outcome, Solution};