use alloc::collections::{BTreeMap, BinaryHeap};
use alloc::vec::Vec;
use core::cmp::Reverse;
use search_core::{Cost, Heuristic};
use crate::bottleneck::{Bottleneck, Width};
use crate::cost::{add, weakly_dominates};
use crate::graph::Graph;
use crate::output::{Output, Solution, Stats};
pub type SumsAndWidths<C, K, const N: usize, const M: usize> = ([C; N], [Bottleneck<K>; M]);
pub fn minsum_maxmin_multi<G, H, C, K, P, const N: usize, const M: usize>(
graph: &G,
mut heuristic: H,
start: G::State,
is_goal: P,
) -> Output<G::State, SumsAndWidths<C, K, N, M>>
where
G: Graph<Weight = ([C; N], [K; M])>,
H: Heuristic<G::State, [C; N]>,
C: Cost,
K: Copy + Ord,
P: Fn(&G::State) -> bool,
{
let mut nodes: Vec<Node<G::State, C, K, N, M>> = Vec::new();
let mut open = BinaryHeap::new();
let mut closed: BTreeMap<G::State, Vec<Label<C, K, N, M>>> = BTreeMap::new();
let mut found: Vec<Label<C, K, N, M>> = Vec::new();
let mut solutions = Vec::new();
let mut successors = Vec::new();
let mut stats = Stats::default();
if let Some(h) = heuristic.estimate(&start) {
nodes.push(Node {
state: start,
label: ([C::ZERO; N], [None; M]),
parent: None,
});
open.push(Reverse((h, Reverse([Width(None); M]), 0)));
stats.generated += 1;
}
while let Some(Reverse((f, _, index))) = open.pop() {
let (here, label) = (nodes[index].state.clone(), nodes[index].label);
if covered(&found, &(f, label.1)) || closed.get(&here).is_some_and(|c| covered(c, &label)) {
continue;
}
closed.entry(here.clone()).or_default().push(label);
if is_goal(&here) {
found.push(label);
solutions.push(rebuild(&nodes, index));
continue;
}
stats.expanded += 1;
graph.successors(&here, &mut successors);
for (next, (sums, capacities)) in successors.drain(..) {
let child: Label<C, K, N, M> = (
add(label.0, sums),
core::array::from_fn(|i| {
Some(label.1[i].map_or(capacities[i], |w| w.min(capacities[i])))
}),
);
if closed.get(&next).is_some_and(|c| covered(c, &child)) {
continue;
}
let Some(h) = heuristic.estimate(&next) else {
continue;
};
let f = add(child.0, h);
if covered(&found, &(f, child.1)) {
continue;
}
nodes.push(Node {
state: next,
label: child,
parent: Some(index),
});
open.push(Reverse((f, Reverse(child.1.map(Width)), nodes.len() - 1)));
stats.generated += 1;
}
}
Output { solutions, stats }
}
type Label<C, K, const N: usize, const M: usize> = SumsAndWidths<C, K, N, M>;
struct Node<S, C, K, const N: usize, const M: usize> {
state: S,
label: Label<C, K, N, M>,
parent: Option<usize>,
}
fn covered<C: Cost, K: Copy + Ord, const N: usize, const M: usize>(
known: &[Label<C, K, N, M>],
label: &Label<C, K, N, M>,
) -> bool {
known.iter().any(|k| {
weakly_dominates(&k.0, &label.0) && (0..M).all(|i| Width(k.1[i]) >= Width(label.1[i]))
})
}
fn rebuild<S: Clone, C: Copy, K: Copy, const N: usize, const M: usize>(
nodes: &[Node<S, C, K, N, M>],
index: usize,
) -> Solution<S, Label<C, K, N, M>> {
let mut path = Vec::new();
let mut current = Some(index);
while let Some(i) = current {
path.push(nodes[i].state.clone());
current = nodes[i].parent;
}
path.reverse();
Solution {
cost: nodes[index].label,
path,
}
}