use mapf_search::{
Agent, CbsConfig, CbsHeuristic, Grid, Instance, MStarConfig, Objective, Outcome, Splitting,
Vertex, Weight, first_conflict,
};
use proptest::prelude::*;
use crate::oracle::{assert_valid_plan, optimal_cost};
use crate::solvers::{Solver, assert_within};
const SOLVABLE_BUDGET: u64 = 20_000;
const UNSOLVABLE_BUDGET: u64 = 60;
pub(crate) fn instance() -> impl Strategy<Value = (Grid, Vec<Agent>)> {
(2..=3_usize)
.prop_flat_map(|agents| {
let height = if agents == 2 { 2..=4_usize } else { 2..=3 };
(Just(agents), 2..=3_usize, height)
})
.prop_flat_map(|(agents, width, height)| {
let blocked = prop::collection::vec(prop::bool::weighted(0.15), width * height);
(Just(agents), Just(width), Just(height), blocked)
})
.prop_filter("enough free cells", |(agents, _, _, blocked)| {
blocked.iter().filter(|&&b| !b).count() >= *agents
})
.prop_flat_map(|(agents, width, height, blocked)| {
let grid = grid(width, height, &blocked);
let free: Vec<Vertex> = (0..blocked.len()).filter(|&v| !blocked[v]).collect();
let starts = Just(free.clone()).prop_shuffle();
let goals = Just(free).prop_shuffle();
(Just(grid), Just(agents), starts, goals)
})
.prop_map(|(grid, count, starts, goals)| {
let agents = starts
.iter()
.zip(&goals)
.take(count)
.map(|(&start, &goal)| Agent { start, goal })
.collect();
(grid, agents)
})
}
fn objective() -> impl Strategy<Value = Objective> {
prop::sample::select(vec![Objective::SumOfCosts, Objective::Makespan])
}
fn grid(width: usize, height: usize, blocked: &[bool]) -> Grid {
let mut grid = Grid::new(width, height);
for (vertex, _) in blocked.iter().enumerate().filter(|(_, b)| **b) {
let (x, y) = grid.cell(vertex);
grid.block(x, y);
}
grid
}
fn check(grid: &Grid, agents: &[Agent], solver: Solver) -> Result<(), TestCaseError> {
let optimum = optimal_cost(grid, agents, solver.objective());
let budget = if optimum.is_some() {
SOLVABLE_BUDGET
} else {
UNSOLVABLE_BUDGET
};
let instance = Instance::new(grid.clone(), agents.to_vec()).expect("valid by construction");
let result = solver.run(&instance, budget);
match (optimum, result.outcome) {
(Some(optimum), Outcome::Solved(solution)) => {
let (soc, makespan) = assert_valid_plan(grid, agents, &solution.paths);
prop_assert_eq!(first_conflict(&solution.paths), None);
prop_assert_eq!((solution.sum_of_costs, solution.makespan), (soc, makespan));
assert_within(solver, solution.cost(solver.objective()), optimum);
}
(None, Outcome::Unsolvable | Outcome::LimitReached | Outcome::Exhausted) => {}
(Some(_), Outcome::LimitReached | Outcome::Exhausted) if !solver.is_complete() => {}
(Some(_), Outcome::LimitReached) if solver.is_complete() => {
prop_assume!(false, "needs more than {} expansions", SOLVABLE_BUDGET);
}
(optimum, outcome) => {
prop_assert!(false, "oracle {optimum:?}, {solver:?}: {outcome:?}");
}
}
Ok(())
}
proptest! {
#![proptest_config(ProptestConfig {
cases: 64,
// Bounds the hard instances discarded in `check`, so a regression cannot hide.
max_global_rejects: 8,
..ProptestConfig::default()
})]
#[test]
fn plain_cbs_matches_joint_search_for_sum_of_costs((grid, agents) in instance()) {
check(&grid, &agents, Solver::Cbs(CbsConfig::plain(Objective::SumOfCosts)))?;
}
#[test]
fn plain_cbs_matches_joint_search_for_makespan((grid, agents) in instance()) {
check(&grid, &agents, Solver::Cbs(CbsConfig::plain(Objective::Makespan)))?;
}
#[test]
fn improved_cbs_matches_joint_search_for_sum_of_costs((grid, agents) in instance()) {
check(&grid, &agents, Solver::Cbs(CbsConfig::improved(Objective::SumOfCosts)))?;
}
#[test]
fn improved_cbs_with_wdg_matches_joint_search((grid, agents) in instance()) {
let config = CbsConfig {
heuristic: CbsHeuristic::Wdg,
..CbsConfig::improved(Objective::SumOfCosts)
};
check(&grid, &agents, Solver::Cbs(config))?;
}
#[test]
fn improved_cbs_matches_joint_search_for_makespan((grid, agents) in instance()) {
check(&grid, &agents, Solver::Cbs(CbsConfig::improved(Objective::Makespan)))?;
}
#[test]
fn each_cbs_improvement_alone_matches_joint_search(
(grid, agents) in instance(),
improvement in 0..9_usize,
makespan in any::<bool>(),
) {
let objective = if makespan { Objective::Makespan } else { Objective::SumOfCosts };
let mut config = CbsConfig::plain(objective);
match improvement {
0 => config.prioritize_conflicts = true,
1 => config.bypass = true,
2 => config.heuristic = CbsHeuristic::Cg,
3 => config.conflict_avoidance = true,
4 => config.splitting = Splitting::Disjoint,
5 => config.heuristic = CbsHeuristic::Dg,
6 => config.heuristic = CbsHeuristic::Wdg,
7 => {
config.heuristic = CbsHeuristic::Wdg;
config.splitting = Splitting::Disjoint;
}
_ => {
config.bypass = true;
config.splitting = Splitting::Disjoint;
}
}
check(&grid, &agents, Solver::Cbs(config))?;
}
#[test]
fn icts_matches_joint_search((grid, agents) in instance(), objective in objective()) {
check(&grid, &agents, Solver::Icts(objective))?;
}
#[test]
fn mstar_matches_joint_search(
(grid, agents) in instance(),
objective in objective(),
recursive in any::<bool>(),
operator_decomposition in any::<bool>(),
) {
let config = MStarConfig { objective, recursive, operator_decomposition };
check(&grid, &agents, Solver::MStar(config))?;
}
#[test]
fn epea_matches_joint_search((grid, agents) in instance(), objective in objective()) {
check(&grid, &agents, Solver::Epea(objective))?;
}
#[test]
fn lns2_plans_are_valid((grid, agents) in instance(), objective in objective()) {
check(&grid, &agents, Solver::Lns2(objective))?;
}
#[test]
fn pibt_plans_are_valid((grid, agents) in instance(), objective in objective()) {
check(&grid, &agents, Solver::Pibt(objective))?;
}
#[test]
fn pbs_plans_are_valid((grid, agents) in instance(), objective in objective()) {
check(&grid, &agents, Solver::Pbs(objective))?;
}
#[test]
fn ecbs_with_weight_one_is_optimal((grid, agents) in instance(), objective in objective()) {
check(&grid, &agents, Solver::Ecbs(objective, Weight::ONE))?;
}
#[test]
fn ecbs_stays_within_its_bound(
(grid, agents) in instance(),
objective in objective(),
numerator in prop::sample::select(vec![11_u32, 15, 20, 30]),
) {
check(&grid, &agents, Solver::Ecbs(objective, Weight::new(numerator, 10)))?;
}
}