use alloc::collections::BTreeSet;
use alloc::vec::Vec;
use search_core::Cost;
use crate::boa::boa_star;
use crate::disjoint::{CostedPair, Disjointness, PathPair};
use crate::graph::{EdgeGraph, Graph, ZeroHeuristic};
use crate::output::Solution;
pub fn disjoint_pair_front<G, C>(
graph: &G,
start: &G::State,
target: &G::State,
kind: Disjointness,
) -> Vec<CostedPair<G::State, G::Edge, [C; 2]>>
where
G: EdgeGraph<Weight = [C; 2]>,
C: Cost,
{
let mode = match kind {
Disjointness::Edges => Mode::Edges,
Disjointness::States => Mode::States,
};
solve(graph, start, target, mode, |w: &[C; 2]| *w, C::ZERO)
}
pub fn maximally_disjoint_pairs<G, C, F>(
graph: &G,
start: &G::State,
target: &G::State,
cost: F,
) -> Vec<SharedPair<G::State, G::Edge, C>>
where
G: EdgeGraph,
C: Cost + From<u8>,
F: Fn(&G::Weight) -> C,
{
let pairs = solve(
graph,
start,
target,
Mode::Shared,
|w| [cost(w), C::ZERO],
C::from(1),
);
pairs
.into_iter()
.map(|(pair, [total, _])| {
let shared = pair
.first_edges
.iter()
.filter(|e| pair.second_edges.contains(e))
.count();
(pair, total, shared)
})
.collect()
}
pub type SharedPair<S, E, C> = (PathPair<S, E>, C, usize);
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Mode {
Edges,
States,
Shared,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
struct Partial<S, E> {
second: bool,
at: S,
first_edges: BTreeSet<E>,
second_edges: BTreeSet<E>,
first_states: BTreeSet<S>,
second_states: BTreeSet<S>,
via: Option<E>,
}
struct Pairs<'a, G: Graph, F, C> {
graph: &'a G,
start: G::State,
target: G::State,
mode: Mode,
cost: F,
one: C,
}
impl<G, F, C> Graph for Pairs<'_, G, F, C>
where
G: EdgeGraph,
F: Fn(&G::Weight) -> [C; 2],
C: Cost,
{
type State = Partial<G::State, G::Edge>;
type Weight = [C; 2];
fn successors(&self, state: &Self::State, out: &mut Vec<(Self::State, [C; 2])>) {
if state.at == self.target {
if !state.second {
let mut next = state.clone();
next.second = true;
next.at = self.start.clone();
next.via = None;
next.second_states.insert(self.start.clone());
out.push((next, [C::ZERO; 2]));
}
return;
}
let mut edges = Vec::new();
self.graph.edges(&state.at, &mut edges);
for (edge, to, weight) in edges {
let mut cost = (self.cost)(&weight);
let mut next = state.clone();
if state.second {
if next.second_states.contains(&to) {
continue;
}
let inner = to != self.target && to != self.start;
let blocked = match self.mode {
Mode::Edges => next.first_edges.contains(&edge),
Mode::States => {
next.first_edges.contains(&edge)
|| (inner && next.first_states.contains(&to))
}
Mode::Shared => false,
};
if blocked {
continue;
}
if self.mode == Mode::Shared && next.first_edges.contains(&edge) {
cost[1] = self.one;
}
next.second_edges.insert(edge.clone());
next.second_states.insert(to.clone());
} else {
if next.first_states.contains(&to) {
continue;
}
next.first_edges.insert(edge.clone());
next.first_states.insert(to.clone());
}
next.at = to;
next.via = Some(edge);
out.push((next, cost));
}
}
}
fn solve<G, F, C>(
graph: &G,
start: &G::State,
target: &G::State,
mode: Mode,
cost: F,
one: C,
) -> Vec<CostedPair<G::State, G::Edge, [C; 2]>>
where
G: EdgeGraph,
F: Fn(&G::Weight) -> [C; 2],
C: Cost,
{
if start == target {
return Vec::new();
}
let pairs = Pairs {
graph,
start: start.clone(),
target: target.clone(),
mode,
cost,
one,
};
let root = Partial {
second: false,
at: start.clone(),
first_edges: BTreeSet::new(),
second_edges: BTreeSet::new(),
first_states: BTreeSet::from([start.clone()]),
second_states: BTreeSet::new(),
via: None,
};
let goal = |s: &Partial<G::State, G::Edge>| s.second && s.at == *target;
boa_star(&pairs, ZeroHeuristic, root, goal)
.solutions
.into_iter()
.map(|solution| (unpack(&solution), solution.cost))
.collect()
}
fn unpack<S: Clone, E: Clone, C>(solution: &Solution<Partial<S, E>, C>) -> PathPair<S, E> {
let mut pair = PathPair {
first: Vec::new(),
second: Vec::new(),
first_edges: Vec::new(),
second_edges: Vec::new(),
};
for state in &solution.path {
let (states, edges) = if state.second {
(&mut pair.second, &mut pair.second_edges)
} else {
(&mut pair.first, &mut pair.first_edges)
};
states.push(state.at.clone());
edges.extend(state.via.clone());
}
pair
}