use alloc::collections::{BTreeMap, BinaryHeap};
use alloc::vec::Vec;
use core::cmp::Reverse;
use search_core::{Cost, Heuristic};
use crate::cost::add;
use crate::epsilon::Epsilon;
use crate::graph::Graph;
use crate::output::{Output, Solution, Stats};
use crate::pareto::Front;
#[derive(Debug, Clone, Copy)]
pub struct Aggregation<X, A> {
pub extend: X,
pub aggregate: A,
}
pub type Aggregated<S, C, const M: usize, const K: usize> = (Output<S, [C; K]>, Vec<[C; M]>);
type WithHidden<S, C, const M: usize, const K: usize> = (Solution<S, [C; K]>, [C; M]);
pub fn aggregated_search<G, H, X, A, C, P, const M: usize, const K: usize>(
graph: &G,
mut heuristic: H,
start: G::State,
is_goal: P,
functions: Aggregation<X, A>,
eps: [Epsilon; K],
) -> Aggregated<G::State, C, M, K>
where
G: Graph,
H: Heuristic<G::State, [C; M]>,
X: Fn(&[C; M], &G::Weight) -> [C; M],
A: Fn(&[C; M]) -> [C; K],
C: Cost,
P: Fn(&G::State) -> bool,
{
let Aggregation { extend, aggregate } = functions;
let mut nodes: Vec<(G::State, [C; M], Option<usize>)> = Vec::new();
let mut open = BinaryHeap::new();
let mut closed: BTreeMap<G::State, Front<C, M>> = BTreeMap::new();
let mut found: Vec<[C; K]> = Vec::new();
let mut solutions: Vec<WithHidden<G::State, C, M, K>> = Vec::new();
let mut successors = Vec::new();
let mut stats = Stats::default();
let covered = |found: &[[C; K]], f: &[C; K]| {
found
.iter()
.any(|s| (0..K).all(|i| eps[i].within(s[i], f[i])))
};
if let Some(h) = heuristic.estimate(&start) {
let g = [C::ZERO; M];
nodes.push((start, g, None));
open.push(Reverse((aggregate(&add(g, h)), add(g, h), 0)));
stats.generated += 1;
}
while let Some(Reverse((key, _, index))) = open.pop() {
let (here, g) = (nodes[index].0.clone(), nodes[index].1);
if covered(&found, &key) || closed.get(&here).is_some_and(|c| c.weakly_dominates(&g)) {
continue;
}
closed.entry(here.clone()).or_default().insert(g);
if is_goal(&here) {
found.push(aggregate(&g));
solutions.push((rebuild(&nodes, index, aggregate(&g)), g));
continue;
}
stats.expanded += 1;
graph.successors(&here, &mut successors);
for (next, weight) in successors.drain(..) {
let g_next = extend(&g, &weight);
if closed
.get(&next)
.is_some_and(|c| c.weakly_dominates(&g_next))
{
continue;
}
let Some(h) = heuristic.estimate(&next) else {
continue;
};
let f = add(g_next, h);
let key = aggregate(&f);
if !covered(&found, &key) {
nodes.push((next, g_next, Some(index)));
open.push(Reverse((key, f, nodes.len() - 1)));
stats.generated += 1;
}
}
}
solutions.sort_by_key(|(s, _)| s.cost);
let mut front = Front::new();
solutions.retain(|(s, _)| front.insert(s.cost));
let (solutions, hidden) = solutions.into_iter().unzip();
(Output { solutions, stats }, hidden)
}
fn rebuild<S: Clone, C, const M: usize, const K: usize>(
nodes: &[(S, [C; M], Option<usize>)],
index: usize,
cost: [C; K],
) -> Solution<S, [C; K]> {
let mut path = Vec::new();
let mut current = Some(index);
while let Some(i) = current {
path.push(nodes[i].0.clone());
current = nodes[i].2;
}
path.reverse();
Solution { cost, path }
}