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//! - [`cbs_ta()`]: CBS with task assignment over a [`TaskInstance`], whose agents each have
18//!   candidate goals; optimal over all assignments, with every [`CbsConfig`] improvement
19//!   ([`cbs_ta_with`]).
20//! - [`ecbs()`]: enhanced CBS, bounded-suboptimal within a [`Weight`] `w`, with focal search
21//!   at both levels.
22//! - [`icts()`]: increasing cost tree search with pairwise pruning, optimal.
23//! - [`mstar()`]: recursive M* with operator decomposition (subdimensional expansion),
24//!   optimal, and able to prove unsolvability; [`mstar_with`] and [`MStarConfig`] select
25//!   basic M*, recursive M* without operator decomposition, and OD-M*.
26//! - [`epea()`]: enhanced partial expansion A* in the joint space, optimal, and able to
27//!   prove unsolvability; it generates only the children at a node's stored `f`.
28//! - [`lns2()`]: MAPF-LNS2, large neighborhood search that repairs a plan with collisions
29//!   a few agents at a time; seeded ([`Lns2Config`]), neither optimal nor complete.
30//! - [`pibt()`]: priority inheritance with backtracking, one time step for all agents at a
31//!   time; very fast, neither optimal nor complete.
32//! - [`pbs()`]: priority-based search, fast but neither optimal nor complete.
33//! - `mo_cbs`, `mo_cbs_with` and `mo_cbs_per_agent`, with the `multi-objective` feature:
34//!   multi-objective CBS (MO-CBS and MO-CBS-t, over BOA* or NAMOA*dr), every
35//!   Pareto-optimal plan when moves and waits cost vectors, shared or per agent, with an
36//!   opt-in feasibility check, conflict prioritization, bypass and disjoint splitting.
37//!
38//! Every solver but [`lns2()`] and [`pibt()`], which only seek a feasible plan, takes an
39//! [`Objective`]: sum of costs or makespan.
40//! - [`space_time_astar`]: the single-agent low level under [`Constraint`]s, built on the
41//!   `heuristic-search` crate's A*.
42//! - [`first_conflict`] and [`conflicts`]: conflict detection on any set of paths.
43//! - [`parse_map`], [`parse_scenario`] and [`scenario_instance`]: Moving AI benchmarks.
44//!
45//! Every solver returns a [`MapfResult`]: an [`Outcome`] (with a [`Solution`] if solved)
46//! and high-level [`Stats`]. [`Budget`], [`Limits`] and [`Stats`] are re-exported from
47//! `heuristic-search`; they bound and count the high-level expansions. [`Weight`] is too.
48//!
49//! The crate is `no_std` and needs only `alloc`.
50//!
51//! ## Example
52//!
53//! ```
54//! use mapf_search::{Agent, Grid, Instance, Limits, Objective, cbs, first_conflict};
55//!
56//! // Two agents swap the ends of a corridor that has one side pocket.
57//! let grid = Grid::parse(&["...", "@.@"]);
58//! let agents = vec![
59//!     Agent { start: grid.vertex(0, 0), goal: grid.vertex(2, 0) },
60//!     Agent { start: grid.vertex(2, 0), goal: grid.vertex(0, 0) },
61//! ];
62//! let instance = Instance::new(grid, agents)?;
63//!
64//! let result = cbs(&instance, Objective::SumOfCosts, Limits::expansions(1_000));
65//! let solution = result.solution().expect("the pocket lets them pass");
66//!
67//! assert_eq!(first_conflict(&solution.paths), None);
68//! assert_eq!(solution.sum_of_costs, 7);
69//! # Ok::<(), mapf_search::InstanceError>(())
70//! ```
71//!
72//! Repository: <https://github.com/sunsided/mapf-search>
73
74#![no_std]
75#![forbid(unsafe_code)]
76
77extern crate alloc;
78
79/// Doctests for the code examples in README.md (not part of the API).
80#[cfg(doctest)]
81#[doc = include_str!("../README.md")]
82mod readme_doctests {}
83
84mod assignment;
85mod cbs;
86mod conflict;
87mod conflict_table;
88mod constraint;
89mod ecbs;
90mod epea;
91mod focal;
92mod graph;
93mod icts;
94mod instance;
95mod lns2;
96mod low_level;
97mod mdd;
98#[cfg(feature = "multi-objective")]
99mod mo_cbs;
100mod movingai;
101mod mstar;
102mod path;
103mod pbs;
104mod pibt;
105mod solution;
106
107pub use cbs::{CbsConfig, CbsHeuristic, Splitting, cbs, cbs_ta, cbs_ta_with, cbs_with};
108pub use conflict::{Conflict, conflicts, first_conflict};
109pub use constraint::{Constraint, ConstraintSet};
110pub use ecbs::ecbs;
111pub use epea::epea;
112pub use graph::{AdjacencyList, Graph, Grid, Vertex};
113pub use heuristic_search::{Budget, Limits, Stats, Weight};
114pub use icts::icts;
115pub use instance::{Agent, AgentId, Instance, InstanceError, TaskAgent, TaskInstance};
116pub use lns2::{Lns2Config, lns2};
117pub use low_level::space_time_astar;
118#[cfg(feature = "multi-objective")]
119pub use mo_cbs::{
120    LowLevel, MoCbsConfig, ParetoOutcome, ParetoResult, ParetoSolution, mo_cbs, mo_cbs_per_agent,
121    mo_cbs_with,
122};
123pub use movingai::{
124    ParseError, ParseErrorKind, ScenarioEntry, ScenarioError, parse_map, parse_scenario,
125    scenario_instance,
126};
127pub use mstar::{MStarConfig, mstar, mstar_with};
128pub use path::{Objective, Path, Time, makespan, path_cost, position_at, sum_of_costs};
129pub use pbs::pbs;
130pub use pibt::pibt;
131pub use solution::{MapfResult, Outcome, Solution};