use heuristic_search::Limits;
use mapf_search::{
AdjacencyList, Agent, CbsConfig, CbsHeuristic, Conflict, Constraint, ConstraintSet, Grid,
Instance, InstanceError, MStarConfig, Objective, Outcome, Splitting, cbs, cbs_with, conflicts,
first_conflict, icts, mstar_with, space_time_astar,
};
use crate::oracle::{assert_valid_plan, optimal_cost};
#[test]
fn icts_handles_many_independent_agents() {
let count = 40;
let grid = Grid::new(3 * count + 2, 3);
let agents = (0..count)
.map(|i| Agent {
start: grid.vertex(3 * i, 0),
goal: grid.vertex(3 * i + 1, 1),
})
.collect();
let instance = Instance::new(grid, agents).expect("distinct starts and goals");
let result = icts(&instance, Objective::SumOfCosts, Limits::expansions(10));
assert_eq!(result.solution().map(|s| s.sum_of_costs), Some(80));
}
#[test]
fn swap_is_detected() {
let paths = [vec![0, 1], vec![1, 0]];
assert_eq!(
first_conflict(&paths),
Some(Conflict::Edge {
agents: (0, 1),
from: 0,
to: 1,
time: 0,
})
);
}
#[test]
fn following_is_not_a_conflict() {
let paths = [vec![0, 1, 2], vec![1, 2, 3]];
assert_eq!(first_conflict(&paths), None);
}
#[test]
fn vertex_conflict_is_detected() {
let paths = [vec![0, 1, 2], vec![2, 1, 0]];
assert_eq!(
first_conflict(&paths),
Some(Conflict::Vertex {
agents: (0, 1),
vertex: 1,
time: 1,
})
);
}
#[test]
fn finished_agent_blocks_its_goal() {
let paths = [vec![0, 1], vec![3, 3, 3, 1]];
assert_eq!(
conflicts(&paths),
[Conflict::Vertex {
agents: (0, 1),
vertex: 1,
time: 3,
}]
);
}
#[test]
fn low_level_respects_late_goal_constraint() {
let grid = Grid::new(3, 1);
let agent = Agent { start: 0, goal: 2 };
let mut constraints = ConstraintSet::new();
constraints.insert(&Constraint::Vertex {
agent: 0,
vertex: 2,
time: 5,
});
let path = space_time_astar(&grid, agent, &constraints).expect("solvable");
assert_eq!(path.len(), 7, "arrives at time 6: {path:?}");
assert_ne!(path[5], 2);
assert_eq!(path.last(), Some(&2));
}
#[test]
fn low_level_respects_edge_constraint() {
let grid = Grid::new(2, 1);
let agent = Agent { start: 0, goal: 1 };
let mut constraints = ConstraintSet::new();
constraints.insert(&Constraint::Edge {
agent: 0,
from: 0,
to: 1,
time: 0,
});
assert_eq!(
space_time_astar(&grid, agent, &constraints),
Some(vec![0, 0, 1])
);
}
#[test]
fn agent_steps_off_its_goal() {
let graph = AdjacencyList::undirected(4, &[(0, 1), (1, 2), (1, 3)]);
let agents = vec![Agent { start: 1, goal: 1 }, Agent { start: 0, goal: 2 }];
let instance = Instance::new(graph.clone(), agents.clone()).expect("valid");
let result = cbs(&instance, Objective::SumOfCosts, Limits::expansions(1_000));
let solution = result.solution().expect("solvable");
assert_eq!(solution.sum_of_costs, 4);
assert_eq!(assert_valid_plan(&graph, &agents, &solution.paths).0, 4);
assert_eq!(
optimal_cost(&graph, &agents, Objective::SumOfCosts),
Some(4)
);
}
#[test]
fn unreachable_goal_is_unsolvable() {
let grid = Grid::parse(&[".@."]);
let agents = vec![Agent { start: 0, goal: 2 }];
let instance = Instance::new(grid, agents).expect("valid");
assert_eq!(
cbs(&instance, Objective::SumOfCosts, Limits::NONE).outcome,
Outcome::Unsolvable
);
}
#[test]
fn corridor_swap_hits_the_budget() {
let grid = Grid::new(3, 1);
let agents = vec![Agent { start: 0, goal: 2 }, Agent { start: 2, goal: 0 }];
let instance = Instance::new(grid.clone(), agents.clone()).expect("valid");
let result = cbs(&instance, Objective::SumOfCosts, Limits::expansions(200));
assert_eq!(result.outcome, Outcome::LimitReached);
assert_eq!(result.stats.expanded, 200);
assert_eq!(optimal_cost(&grid, &agents, Objective::SumOfCosts), None);
}
#[test]
fn invalid_instances_are_rejected() {
let grid = Grid::new(2, 2);
let at = |start, goal| Agent { start, goal };
assert_eq!(
Instance::new(grid.clone(), vec![at(0, 1), at(0, 2)]),
Err(InstanceError::SharedStart(0, 1))
);
assert_eq!(
Instance::new(grid.clone(), vec![at(0, 1), at(2, 1)]),
Err(InstanceError::SharedGoal(0, 1))
);
assert_eq!(
Instance::new(grid, vec![at(0, 4)]),
Err(InstanceError::OutOfRange { agent: 0 })
);
}
fn hard_instance() -> (Grid, Vec<Agent>) {
let grid = Grid::parse(&["@.@", "...", ".@."]);
let agents = vec![
Agent { start: 1, goal: 3 },
Agent { start: 6, goal: 5 },
Agent { start: 4, goal: 1 },
];
(grid, agents)
}
#[test]
#[ignore = "slow: plain CBS needs about 63 000 expansions"]
fn hard_instance_needs_many_expansions() {
let (grid, agents) = hard_instance();
let instance = Instance::new(grid.clone(), agents.clone()).expect("valid");
let result = cbs_with(
&instance,
CbsConfig::plain(Objective::SumOfCosts),
Limits::expansions(200_000),
);
let solution = result.solution().expect("solvable");
assert_eq!(solution.sum_of_costs, 19);
assert_eq!(assert_valid_plan(&grid, &agents, &solution.paths).0, 19);
assert_eq!(
optimal_cost(&grid, &agents, Objective::SumOfCosts),
Some(19)
);
}
#[test]
fn improved_cbs_solves_the_hard_instance() {
let (grid, agents) = hard_instance();
let instance = Instance::new(grid.clone(), agents.clone()).expect("valid");
let improved = CbsConfig::improved(Objective::SumOfCosts);
let standard = CbsConfig {
splitting: Splitting::Standard,
..improved
};
let result = cbs_with(&instance, improved, Limits::expansions(200_000));
let solution = result.solution().expect("solvable");
assert_eq!(assert_valid_plan(&grid, &agents, &solution.paths).0, 19);
assert!(result.stats.expanded < 1_500, "{:?}", result.stats);
let without = cbs_with(&instance, standard, Limits::expansions(200_000));
let solution = without.solution().expect("solvable");
assert_eq!(assert_valid_plan(&grid, &agents, &solution.paths).0, 19);
assert!(
result.stats.expanded * 5 < without.stats.expanded,
"{:?} vs {:?}",
result.stats,
without.stats
);
}
#[test]
fn stronger_heuristics_need_fewer_expansions() {
let (grid, agents) = hard_instance();
let instance = Instance::new(grid.clone(), agents.clone()).expect("valid");
let expansions = [
CbsHeuristic::None,
CbsHeuristic::Cg,
CbsHeuristic::Dg,
CbsHeuristic::Wdg,
]
.map(|heuristic| {
let config = CbsConfig {
heuristic,
..CbsConfig::improved(Objective::SumOfCosts)
};
let result = cbs_with(&instance, config, Limits::expansions(200_000));
let solution = result.solution().expect("solvable");
assert_eq!(assert_valid_plan(&grid, &agents, &solution.paths).0, 19);
result.stats.expanded
});
assert!(expansions.is_sorted_by(|a, b| a > b), "{expansions:?}");
}
#[test]
fn low_level_respects_positive_constraints() {
let grid = Grid::new(5, 1);
let agent = Agent { start: 2, goal: 4 };
let required = Constraint::PositiveVertex {
agent: 0,
vertex: 0,
time: 3,
};
let own = ConstraintSet::for_agent(0, &[required]);
let path = space_time_astar(&grid, agent, &own).expect("detour");
assert_eq!(
path.len(),
8,
"to 0 by time 3, then 4 steps to the goal: {path:?}"
);
assert_eq!(path[3], 0);
let others = ConstraintSet::for_agent(1, &[required]);
let agent = Agent { start: 1, goal: 0 };
let path = space_time_astar(&grid, agent, &others).expect("arrives after 3");
assert_eq!(path.len(), 5, "may not sit at 0 at time 3: {path:?}");
}
#[test]
fn recursive_mstar_plans_independent_pairs_apart() {
let grid = Grid::parse(&["...@.....", "@.@@@@@.@"]);
let at = |x, y| grid.vertex(x, y);
let agents = vec![
Agent {
start: at(0, 0),
goal: at(2, 0),
},
Agent {
start: at(2, 0),
goal: at(0, 0),
},
Agent {
start: at(4, 0),
goal: at(8, 0),
},
Agent {
start: at(8, 0),
goal: at(4, 0),
},
];
let instance = Instance::new(grid.clone(), agents.clone()).expect("valid");
for objective in [Objective::SumOfCosts, Objective::Makespan] {
let optimum = optimal_cost(&grid, &agents, objective);
let basic = mstar_with(&instance, MStarConfig::basic(objective), Limits::NONE);
let recursive = mstar_with(&instance, MStarConfig::recursive(objective), Limits::NONE);
let decomposed = MStarConfig {
operator_decomposition: true,
..MStarConfig::basic(objective)
};
let decomposed = mstar_with(&instance, decomposed, Limits::NONE);
let od_recursive = MStarConfig::od_recursive(objective);
let od_recursive = mstar_with(&instance, od_recursive, Limits::NONE);
for result in [&basic, &recursive, &decomposed, &od_recursive] {
let solution = result.solution().expect("solvable");
assert_valid_plan(&grid, &agents, &solution.paths);
assert_eq!(Some(solution.cost(objective)), optimum, "{objective:?}");
}
assert!(
recursive.stats.expanded < basic.stats.expanded,
"{objective:?}: {:?} vs {:?}",
recursive.stats,
basic.stats
);
}
}
pub fn benchmark_instance() -> Instance<Grid> {
let grid = Grid::parse(&[
"........", "..@@....", "......@.", ".@......", "....@...", "..@.....", "......@.",
"........",
]);
let at = |(sx, sy), (gx, gy)| Agent {
start: grid.vertex(sx, sy),
goal: grid.vertex(gx, gy),
};
let agents = vec![
at((0, 0), (7, 7)),
at((7, 0), (0, 7)),
at((0, 7), (7, 0)),
at((7, 7), (0, 0)),
at((3, 0), (3, 7)),
at((0, 4), (7, 4)),
at((7, 3), (0, 3)),
at((4, 7), (4, 0)),
];
Instance::new(grid, agents).expect("valid")
}
#[test]
fn conflict_avoidance_reduces_expansions() {
let instance = benchmark_instance();
for objective in [Objective::SumOfCosts, Objective::Makespan] {
let plain = CbsConfig::plain(objective);
let avoiding = CbsConfig {
conflict_avoidance: true,
..plain
};
let without = cbs_with(&instance, plain, Limits::NONE);
let with = cbs_with(&instance, avoiding, Limits::NONE);
let cost = |result: &mapf_search::MapfResult| {
let solution = result.solution().expect("solvable");
assert_valid_plan(instance.graph(), instance.agents(), &solution.paths);
solution.cost(objective)
};
assert_eq!(cost(&with), cost(&without), "{objective:?}");
assert!(
with.stats.expanded < without.stats.expanded,
"{objective:?}: {:?} vs {:?}",
with.stats,
without.stats
);
}
}