use alloc::vec::Vec;
use heuristic_search::{Budget, Stats};
use super::{CbsConfig, Node, Search};
use crate::constraint::Constraint;
use crate::flow::{Blocker, Routing, route};
use crate::graph::{Graph, Vertex};
use crate::instance::UaInstance;
use crate::low_level::distance_tables;
use crate::path::{Objective, Path};
use crate::solution::{Outcome, Solution};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UaSolution {
pub assigned: Solution,
pub unassigned: Vec<Path>,
pub unassigned_moves: u32,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum UaOutcome {
Solved(UaSolution),
Unsolvable,
LimitReached,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UaResult {
pub outcome: UaOutcome,
pub stats: Stats,
}
impl UaResult {
#[must_use]
pub fn solution(&self) -> Option<&UaSolution> {
match &self.outcome {
UaOutcome::Solved(solution) => Some(solution),
UaOutcome::Unsolvable | UaOutcome::LimitReached => None,
}
}
}
pub fn focbs<G: Graph>(instance: &UaInstance<G>, budget: impl Budget<Stats>) -> UaResult {
focbs_with(instance, CbsConfig::improved(Objective::SumOfCosts), budget)
}
pub fn focbs_with<G: Graph>(
instance: &UaInstance<G>,
config: CbsConfig,
mut budget: impl Budget<Stats>,
) -> UaResult {
assert_eq!(
config.objective,
Objective::SumOfCosts,
"FOCBS minimizes the sum of costs"
);
let assigned = instance.assigned();
let mut search = Search::forest(
instance.graph(),
assigned.iter().map(|agent| agent.start).collect(),
assigned.iter().map(|agent| agent.goal).collect(),
distance_tables(instance.graph(), assigned),
None,
config,
);
search.trees.push((0..assigned.len()).collect());
search.unassigned = Some(Unassigned {
starts: instance.unassigned().to_vec(),
routing: None,
});
let outcome = match search.run(&mut budget) {
Outcome::Solved(solution) => {
let routing = search
.unassigned
.and_then(|unassigned| unassigned.routing)
.expect("a solved node was routed");
UaOutcome::Solved(UaSolution {
assigned: solution,
unassigned: routing.paths,
unassigned_moves: routing.moves,
})
}
Outcome::Unsolvable => UaOutcome::Unsolvable,
Outcome::LimitReached | Outcome::Exhausted => UaOutcome::LimitReached,
};
UaResult {
outcome,
stats: search.stats,
}
}
pub(super) struct Unassigned {
starts: Vec<Vertex>,
routing: Option<Routing>,
}
impl<G: Graph> Search<'_, G> {
pub(super) fn is_final(&self, node: &Node) -> bool {
let nobody = self
.unassigned
.as_ref()
.is_none_or(|unassigned| unassigned.starts.is_empty());
node.conflict.is_none() && nobody
}
pub(super) fn route_unassigned(&mut self, node: &Node) -> Option<Vec<Blocker>> {
let unassigned = self.unassigned.as_mut()?;
match route(self.graph, &node.paths, &unassigned.starts) {
Ok(routing) => {
unassigned.routing = Some(routing);
None
}
Err(blockers) => Some(blockers),
}
}
pub(super) fn expand_blockers(&mut self, parent: &Node, blockers: &[Blocker]) {
self.stats.expanded += 1;
for constraint in blockers.iter().map(|&blocker| releasing(blocker)) {
debug_assert!(
!parent.constraints.contains(&constraint),
"a blocker is a reservation the node allows"
);
if let Some(child) = self.child(parent, constraint) {
self.push(child.node);
}
}
}
}
fn releasing(blocker: Blocker) -> Constraint {
match blocker {
Blocker::Vertex {
agent,
vertex,
time,
} => {
debug_assert!(time > 0, "the starts are never cut");
Constraint::Vertex {
agent,
vertex,
time,
}
}
Blocker::Move {
agent,
from,
to,
time,
} => Constraint::Edge {
agent,
from,
to,
time,
},
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::collections::{BTreeMap, BinaryHeap};
use alloc::vec;
use core::cmp::Reverse;
use heuristic_search::Limits;
use proptest::prelude::*;
use crate::cbs::cbs_with;
use crate::conflict::first_conflict;
use crate::graph::Grid;
use crate::instance::{Agent, Instance};
use crate::path::{Time, path_cost, position_at};
const BUDGET: u64 = 5_000;
const UNSOLVABLE_BUDGET: u64 = 60;
type Joint = (Vec<(Vertex, bool)>, Vec<Vertex>);
fn optimal_sst(instance: &UaInstance<Grid>) -> Option<u32> {
let start: Joint = (
instance
.assigned()
.iter()
.map(|a| (a.start, false))
.collect(),
instance.unassigned().to_vec(),
);
let mut best = BTreeMap::from([(start.clone(), 0_u32)]);
let mut open = BinaryHeap::from([Reverse((0_u32, start))]);
while let Some(Reverse((cost, state))) = open.pop() {
if best.get(&state).is_some_and(|&known| known < cost) {
continue;
}
if state.0.iter().all(|&(_, finished)| finished) {
return Some(cost);
}
for (next, step) in successors(instance, &state) {
let total = cost + step;
if best.get(&next).is_none_or(|&known| total < known) {
best.insert(next.clone(), total);
open.push(Reverse((total, next)));
}
}
}
None
}
fn successors(instance: &UaInstance<Grid>, state: &Joint) -> Vec<(Joint, u32)> {
let grid = instance.graph();
let options = |vertex: Vertex| {
let mut next = vec![vertex];
grid.neighbors(vertex, |n| next.push(n));
next
};
let mut choices: Vec<Vec<(Vertex, bool, u32)>> = Vec::new();
for (&(vertex, finished), agent) in state.0.iter().zip(instance.assigned()) {
choices.push(if finished {
vec![(vertex, true, 0)]
} else {
let mut list: Vec<_> = options(vertex).into_iter().map(|n| (n, false, 1)).collect();
if vertex == agent.goal {
list.push((vertex, true, 0));
}
list
});
}
for &vertex in &state.1 {
choices.push(options(vertex).into_iter().map(|n| (n, false, 0)).collect());
}
let from: Vec<Vertex> = state
.0
.iter()
.map(|&(v, _)| v)
.chain(state.1.iter().copied())
.collect();
let mut found = Vec::new();
let mut pick = vec![0; choices.len()];
loop {
let to: Vec<Vertex> = pick
.iter()
.zip(&choices)
.map(|(&i, list)| list[i].0)
.collect();
if is_free(&from, &to) {
let assigned = instance.assigned().len();
let next = (
(0..assigned)
.map(|a| (to[a], choices[a][pick[a]].1))
.collect(),
to[assigned..].to_vec(),
);
let cost = (0..assigned).map(|a| choices[a][pick[a]].2).sum();
found.push((next, cost));
}
let Some(position) = (0..pick.len()).find(|&k| pick[k] + 1 < choices[k].len()) else {
break;
};
pick[position] += 1;
pick[..position].fill(0);
}
found
}
fn is_free(from: &[Vertex], to: &[Vertex]) -> bool {
(0..to.len()).all(|a| {
(a + 1..to.len()).all(|b| {
to[a] != to[b] && !(from[a] == to[b] && from[b] == to[a] && from[a] != to[a])
})
})
}
fn fewest_moves(instance: &UaInstance<Grid>, assigned: &[Path]) -> Option<u32> {
let horizon: Time = assigned.iter().map(|p| path_cost(p)).max().unwrap_or(0);
let idle = |here: &[Vertex]| {
UaInstance::new(instance.graph().clone(), Vec::new(), here.to_vec()).expect("valid")
};
let start = (0, instance.unassigned().to_vec());
let mut best = BTreeMap::from([(start.clone(), 0_u32)]);
let mut open = BinaryHeap::from([Reverse((0_u32, start))]);
while let Some(Reverse((cost, (time, here)))) = open.pop() {
if time == horizon {
return Some(cost);
}
let fixed = |at: Time| assigned.iter().map(move |p| position_at(p, at));
let from: Vec<Vertex> = fixed(time).chain(here.iter().copied()).collect();
for ((_, next), _) in successors(&idle(&here), &(Vec::new(), here.clone())) {
let to: Vec<Vertex> = fixed(time + 1).chain(next.iter().copied()).collect();
if !is_free(&from, &to) {
continue;
}
let moves = here.iter().zip(&next).filter(|(a, b)| a != b).count();
let total = cost + u32::try_from(moves).expect("few agents");
let key = (time + 1, next);
if best.get(&key).is_none_or(|&known| total < known) {
best.insert(key.clone(), total);
open.push(Reverse((total, key)));
}
}
}
None
}
fn instance() -> impl Strategy<Value = UaInstance<Grid>> {
(
2_usize..=3,
2_usize..=3,
prop::collection::vec(prop::bool::weighted(0.15), 9),
1_usize..=2,
0_usize..=2,
)
.prop_flat_map(|(width, height, blocked, assigned, unassigned)| {
let cells = width * height;
let free: Vec<Vertex> = (0..cells).filter(|&v| !blocked[v]).collect();
(
Just((width, height, blocked)),
Just(free.clone()).prop_shuffle(),
Just(free).prop_shuffle(),
Just((assigned, unassigned)),
)
})
.prop_filter_map(
"enough free cells",
|((width, height, blocked), starts, goals, (assigned, unassigned))| {
if starts.len() < assigned + unassigned || assigned + unassigned > 3 {
return None;
}
let mut grid = Grid::new(width, height);
for vertex in (0..width * height).filter(|&v| blocked[v]) {
let (x, y) = grid.cell(vertex);
grid.block(x, y);
}
let agents = (0..assigned)
.map(|i| Agent {
start: starts[i],
goal: goals[i],
})
.collect();
let idle = starts[assigned..assigned + unassigned].to_vec();
UaInstance::new(grid, agents, idle).ok()
},
)
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(256))]
#[test]
fn matches_a_joint_search(instance in instance(), improved in any::<bool>()) {
let soc = Objective::SumOfCosts;
let config = if improved { CbsConfig::improved(soc) } else { CbsConfig::plain(soc) };
let expected = optimal_sst(&instance);
let budget = if expected.is_some() { BUDGET } else { UNSOLVABLE_BUDGET };
let result = focbs_with(&instance, config, Limits::expansions(budget));
match (&result.outcome, expected) {
(UaOutcome::Solved(solution), Some(cost)) => {
prop_assert_eq!(solution.assigned.sum_of_costs, cost);
let mut all = solution.assigned.paths.clone();
all.extend(solution.unassigned.iter().cloned());
prop_assert_eq!(first_conflict(&all), None);
for (path, agent) in solution.assigned.paths.iter().zip(instance.assigned()) {
prop_assert_eq!((path.first(), path.last()), (Some(&agent.start), Some(&agent.goal)));
}
for (path, &start) in solution.unassigned.iter().zip(instance.unassigned()) {
prop_assert_eq!(path.first(), Some(&start));
}
prop_assert_eq!(Some(solution.unassigned_moves), fewest_moves(&instance, &solution.assigned.paths));
}
(UaOutcome::Unsolvable | UaOutcome::LimitReached, None) | (UaOutcome::LimitReached, Some(_)) => {}
(outcome, cost) => prop_assert!(false, "{outcome:?}, expected {cost:?}"),
}
}
}
#[test]
fn branches_on_blockers() {
let grid = Grid::parse(&["...", "@.@"]);
let instance =
UaInstance::new(grid, vec![Agent { start: 0, goal: 2 }], vec![2]).expect("valid");
for config in [
CbsConfig::plain(Objective::SumOfCosts),
CbsConfig::improved(Objective::SumOfCosts),
] {
let result = focbs_with(&instance, config, Limits::expansions(BUDGET));
let solution = result.solution().expect("solvable");
assert_eq!(Some(solution.assigned.sum_of_costs), optimal_sst(&instance));
assert_eq!(solution.assigned.sum_of_costs, 3);
assert_eq!(solution.unassigned, [vec![2, 1, 4]]);
assert_eq!(solution.unassigned_moves, 2);
}
}
#[test]
fn follows_directed_edges() {
let ring = crate::graph::AdjacencyList::directed(3, &[(0, 1), (1, 2), (2, 0)]);
let instance =
UaInstance::new(ring, vec![Agent { start: 0, goal: 1 }], vec![1]).expect("valid");
let result = focbs(&instance, Limits::expansions(BUDGET));
let solution = result.solution().expect("solvable");
assert_eq!(solution.assigned.paths, [vec![0, 1]]);
assert_eq!(solution.unassigned, [vec![1, 2]]);
}
#[test]
fn without_unassigned_agents_is_cbs() {
let grid = Grid::parse(&["...", "@.@"]);
let agents = vec![Agent { start: 0, goal: 2 }, Agent { start: 2, goal: 0 }];
let ua = UaInstance::new(grid.clone(), agents.clone(), Vec::new()).expect("valid");
let plain = Instance::new(grid, agents).expect("valid");
for config in [
CbsConfig::plain(Objective::SumOfCosts),
CbsConfig::improved(Objective::SumOfCosts),
] {
let needed = cbs_with(&plain, config, Limits::NONE).stats.expanded;
for budget in 0..=needed {
let focbs = focbs_with(&ua, config, Limits::expansions(budget));
let cbs = cbs_with(&plain, config, Limits::expansions(budget));
assert_eq!(
focbs.solution().map(|s| &s.assigned),
cbs.solution(),
"budget {budget}"
);
assert_eq!(focbs.stats, cbs.stats);
}
}
}
#[test]
fn trivial_instances() {
let empty = UaInstance::new(Grid::new(2, 2), Vec::new(), Vec::new()).expect("valid");
let idle = UaInstance::new(Grid::new(2, 2), Vec::new(), vec![0, 3]).expect("valid");
let none = focbs(&empty, Limits::NONE);
let parked = focbs(&idle, Limits::NONE);
assert_eq!(none.solution().map(|s| s.unassigned_moves), Some(0));
assert_eq!(
parked.solution().map(|s| s.unassigned.clone()),
Some(vec![vec![0], vec![3]])
);
}
#[test]
#[should_panic(expected = "FOCBS minimizes the sum of costs")]
fn rejects_makespan() {
let instance = UaInstance::new(Grid::new(2, 1), Vec::new(), Vec::new()).expect("valid");
let _ = focbs_with(
&instance,
CbsConfig::plain(Objective::Makespan),
Limits::NONE,
);
}
}