mod low_level;
use alloc::vec::Vec;
use heuristic_search::{Budget, Stats, Weight};
use self::low_level::{FocalPath, Problem, focal_plan};
use crate::conflict::{Conflict, conflicts};
use crate::conflict_table::ConflictTable;
use crate::constraint::{Constraint, ConstraintSet};
use crate::focal::FocalList;
use crate::graph::Graph;
use crate::instance::{AgentId, Instance};
use crate::low_level::distance_tables;
use crate::path::{Objective, Path, makespan, path_cost, sum_of_costs};
use crate::solution::{MapfResult, Outcome, Solution};
pub fn ecbs<G: Graph>(
instance: &Instance<G>,
objective: Objective,
weight: Weight,
mut budget: impl Budget<Stats>,
) -> MapfResult {
assert!(
weight.numerator() >= weight.denominator(),
"the suboptimality factor must be at least 1"
);
let mut search = Search {
instance,
objective,
weight,
distances: distance_tables(instance.graph(), instance.agents()),
open: FocalList::new(weight),
stats: Stats::default(),
};
let outcome = search.run(&mut budget);
MapfResult {
outcome,
stats: search.stats,
}
}
struct Node {
constraints: Vec<Constraint>,
paths: Vec<Path>,
lower_bounds: Vec<u32>,
conflict: Option<Conflict>,
conflicting_pairs: usize,
}
impl Node {
fn new(constraints: Vec<Constraint>, paths: Vec<Path>, lower_bounds: Vec<u32>) -> Self {
let found = conflicts(&paths);
let mut pairs: Vec<(AgentId, AgentId)> = found.iter().map(Conflict::agents).collect();
pairs.sort_unstable();
pairs.dedup();
Self {
constraints,
paths,
lower_bounds,
conflict: found.first().copied(),
conflicting_pairs: pairs.len(),
}
}
}
struct Search<'a, G> {
instance: &'a Instance<G>,
objective: Objective,
weight: Weight,
distances: Vec<Vec<Option<u32>>>,
open: FocalList<(usize, u32), Node>,
stats: Stats,
}
impl<G: Graph> Search<'_, G> {
fn run(&mut self, budget: &mut impl Budget<Stats>) -> Outcome {
let Some(root) = self.root() else {
return Outcome::Unsolvable;
};
self.push(root);
while let Some((node, _)) = self.open.pop() {
let Some(conflict) = node.conflict else {
return Outcome::Solved(Solution::new(node.paths));
};
if budget.exhausted(&self.stats) {
return Outcome::LimitReached;
}
self.stats.expanded += 1;
for constraint in Constraint::resolving(conflict) {
if let Some(child) = self.child(&node, constraint) {
self.push(child);
}
}
}
Outcome::Unsolvable
}
fn root(&self) -> Option<Node> {
let count = self.instance.agents().len();
let mut paths: Vec<Path> = Vec::with_capacity(count);
let mut lower_bounds = Vec::with_capacity(count);
for agent in 0..count {
let found = self.replan(agent, &[], &paths)?;
paths.push(found.path);
lower_bounds.push(found.lower_bound);
}
Some(Node::new(Vec::new(), paths, lower_bounds))
}
fn child(&self, parent: &Node, constraint: Constraint) -> Option<Node> {
let mut constraints = parent.constraints.clone();
constraints.push(constraint);
let agent = constraint.agent();
let found = self.replan(agent, &constraints, &parent.paths)?;
let mut paths = parent.paths.clone();
paths[agent] = found.path;
let mut lower_bounds = parent.lower_bounds.clone();
lower_bounds[agent] = lower_bounds[agent].max(found.lower_bound);
Some(Node::new(constraints, paths, lower_bounds))
}
fn replan(
&self,
agent: AgentId,
constraints: &[Constraint],
others: &[Path],
) -> Option<FocalPath> {
let set = ConstraintSet::for_agent(agent, constraints);
let table = ConflictTable::without(others, agent);
let problem = Problem {
graph: self.instance.graph(),
agent: self.instance.agents()[agent],
constraints: &set,
distances: &self.distances[agent],
table: &table,
};
focal_plan(&problem, self.weight)
}
fn push(&mut self, node: Node) {
let (lower_bound, cost) = match self.objective {
Objective::SumOfCosts => (node.lower_bounds.iter().sum(), sum_of_costs(&node.paths)),
Objective::Makespan => (
node.lower_bounds.iter().copied().max().unwrap_or(0),
makespan(&node.paths),
),
};
debug_assert!(
node.paths
.iter()
.zip(&node.lower_bounds)
.all(|(p, &lb)| path_cost(p) >= lb),
"bounds are below path costs"
);
self.stats.generated += 1;
self.open
.push(lower_bound, cost, (node.conflicting_pairs, cost), node);
}
}