use heuristic_search::Limits;
use mapf_search::{
AdjacencyList, Agent, CbsConfig, Graph, Grid, Instance, Lns2Config, MStarConfig, MapfResult,
Objective, Outcome, Weight, cbs_with, ecbs, epea, icts, lns2, mstar_with, pbs, pibt,
};
use crate::oracle::{assert_valid_plan, optimal_cost};
#[derive(Debug, Clone, Copy)]
pub enum Solver {
Cbs(CbsConfig),
Ecbs(Objective, Weight),
Pbs(Objective),
Icts(Objective),
MStar(MStarConfig),
Epea(Objective),
Lns2(Objective),
Pibt(Objective),
}
impl Solver {
pub fn all() -> Vec<Self> {
[Objective::SumOfCosts, Objective::Makespan]
.into_iter()
.flat_map(Self::all_for)
.collect()
}
pub fn all_for(objective: Objective) -> [Self; 11] {
[
Self::Cbs(CbsConfig::plain(objective)),
Self::Cbs(CbsConfig::improved(objective)),
Self::Ecbs(objective, Weight::new(3, 2)),
Self::Pbs(objective),
Self::Icts(objective),
Self::MStar(MStarConfig::basic(objective)),
Self::MStar(MStarConfig::recursive(objective)),
Self::MStar(MStarConfig::od_recursive(objective)),
Self::Epea(objective),
Self::Lns2(objective),
Self::Pibt(objective),
]
}
pub fn run<G: Graph>(self, instance: &Instance<G>, budget: u64) -> MapfResult {
let limits = Limits::expansions(budget);
match self {
Self::Cbs(config) => cbs_with(instance, config, limits),
Self::Ecbs(objective, weight) => ecbs(instance, objective, weight, limits),
Self::Pbs(objective) => pbs(instance, objective, limits),
Self::Icts(objective) => icts(instance, objective, limits),
Self::MStar(config) => mstar_with(instance, config, limits),
Self::Epea(objective) => epea(instance, objective, limits),
Self::Lns2(_) => lns2(
instance,
Lns2Config::default(),
Limits::expansions(budget.min(500)),
),
Self::Pibt(_) => pibt(instance, Limits::expansions(budget.min(500))),
}
}
pub fn objective(self) -> Objective {
match self {
Self::Cbs(config) => config.objective,
Self::MStar(config) => config.objective,
Self::Ecbs(objective, _)
| Self::Pbs(objective)
| Self::Icts(objective)
| Self::Epea(objective)
| Self::Lns2(objective)
| Self::Pibt(objective) => objective,
}
}
pub fn bound(self) -> Option<(u32, u32)> {
match self {
Self::Cbs(_) | Self::Icts(_) | Self::MStar(_) | Self::Epea(_) => Some((1, 1)),
Self::Ecbs(_, weight) => Some((weight.numerator(), weight.denominator())),
Self::Pbs(_) | Self::Lns2(_) | Self::Pibt(_) => None,
}
}
pub fn is_complete(self) -> bool {
!matches!(self, Self::Pbs(_) | Self::Lns2(_) | Self::Pibt(_))
}
}
pub fn assert_within(solver: Solver, cost: u32, optimum: u32) {
if let Some((numerator, denominator)) = solver.bound() {
assert!(
u64::from(cost) * u64::from(denominator) <= u64::from(optimum) * u64::from(numerator),
"{solver:?}: cost {cost} exceeds the bound over the optimum {optimum}"
);
}
assert!(
cost >= optimum,
"{solver:?}: cost {cost} below the optimum {optimum}"
);
}
fn check_all<G: Graph + Clone>(graph: &G, agents: &[Agent]) {
check_solvers(graph, agents, Solver::all());
}
fn check_solvers<G: Graph + Clone>(graph: &G, agents: &[Agent], solvers: Vec<Solver>) {
let instance = Instance::new(graph.clone(), agents.to_vec()).expect("valid");
for solver in solvers {
let optimum = optimal_cost(graph, agents, solver.objective()).expect("solvable");
let result = solver.run(&instance, 1_000);
let solution = result
.solution()
.unwrap_or_else(|| panic!("{solver:?}: {result:?}"));
let (soc, makespan) = assert_valid_plan(graph, agents, &solution.paths);
assert_eq!((soc, makespan), (solution.sum_of_costs, solution.makespan));
assert_within(solver, solution.cost(solver.objective()), optimum);
}
}
#[test]
fn every_solver_steps_off_a_goal() {
let graph = AdjacencyList::undirected(4, &[(0, 1), (1, 2), (1, 3)]);
let agents = [Agent { start: 1, goal: 1 }, Agent { start: 0, goal: 2 }];
let solvers = Solver::all()
.into_iter()
.filter(|solver| !matches!(solver, Solver::Pibt(_)))
.collect();
check_solvers(&graph, &agents, solvers);
}
#[test]
fn every_solver_passes_a_finished_goal() {
let graph = AdjacencyList::undirected(5, &[(0, 1), (1, 2), (2, 3), (1, 4)]);
let agents = [Agent { start: 4, goal: 1 }, Agent { start: 0, goal: 3 }];
assert_eq!(
optimal_cost(&graph, &agents, Objective::SumOfCosts),
Some(5)
);
check_all(&graph, &agents);
}
#[test]
fn every_solver_handles_a_one_way_detour() {
let graph = AdjacencyList::directed(5, &[(0, 4), (0, 1), (1, 2), (2, 3), (3, 4)]);
check_all(&graph, &[Agent { start: 0, goal: 4 }]);
check_all(
&graph,
&[Agent { start: 0, goal: 4 }, Agent { start: 1, goal: 3 }],
);
}
#[test]
fn every_solver_proves_an_unreachable_goal() {
let grid = Grid::parse(&[".@.", "@@@", "..."]);
let agents = vec![Agent { start: 0, goal: 2 }, Agent { start: 6, goal: 8 }];
let instance = Instance::new(grid, agents).expect("valid");
for solver in Solver::all() {
assert_eq!(
solver.run(&instance, 100).outcome,
Outcome::Unsolvable,
"{solver:?}"
);
}
}
#[test]
fn no_solver_solves_a_corridor_swap() {
let grid = Grid::new(3, 1);
let agents = vec![Agent { start: 0, goal: 2 }, Agent { start: 2, goal: 0 }];
let instance = Instance::new(grid, agents).expect("valid");
for solver in Solver::all() {
let outcome = solver.run(&instance, 100).outcome;
assert!(
!matches!(outcome, Outcome::Solved(_)),
"{solver:?}: {outcome:?}"
);
}
}