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