use alloc::collections::BinaryHeap;
use alloc::vec::Vec;
use core::cmp::Reverse;
use search_core::{Cost, Heuristic};
use crate::anytime_boa::lex::lex_first;
use crate::constrained::rebuild;
use crate::cost::add;
use crate::graph::Graph;
use crate::output::{Output, Stats};
pub fn ranking<G, H, C, P>(
graph: &G,
mut heuristic: H,
start: G::State,
is_goal: P,
) -> Output<G::State, [C; 2]>
where
G: Graph<Weight = [C; 2]>,
H: Heuristic<G::State, [C; 2]>,
C: Cost,
P: Fn(&G::State) -> bool,
{
let mut stats = Stats::default();
let mut solutions = Vec::new();
let Some(last) = lex_first(graph, &mut heuristic, &start, &is_goal, 1, &mut stats) else {
return Output { solutions, stats };
};
let bound = last.cost[0];
let mut nodes: Vec<(G::State, [C; 2], Option<usize>)> = Vec::new();
let mut open = BinaryHeap::new();
let mut best_c2: Option<C> = None;
let mut successors = Vec::new();
if let Some(h) = heuristic.estimate(&start) {
nodes.push((start, [C::ZERO; 2], None));
open.push(Reverse((h, 0)));
stats.generated += 1;
}
while let Some(Reverse((f, index))) = open.pop() {
if f[0] > bound {
break;
}
let (here, g, _) = nodes[index].clone();
if is_goal(&here) {
if best_c2.is_none_or(|b| g[1] < b) {
best_c2 = Some(g[1]);
solutions.push(rebuild(&nodes, index));
}
continue;
}
stats.expanded += 1;
graph.successors(&here, &mut successors);
for (next, weight) in successors.drain(..) {
if on_path(&nodes, index, &next) {
continue;
}
let Some(h) = heuristic.estimate(&next) else {
continue;
};
let g_next = add(g, weight);
nodes.push((next, g_next, Some(index)));
open.push(Reverse((add(g_next, h), nodes.len() - 1)));
stats.generated += 1;
}
}
Output { solutions, stats }
}
fn on_path<S: PartialEq, C>(nodes: &[(S, [C; 2], Option<usize>)], index: usize, state: &S) -> bool {
let mut current = Some(index);
while let Some(i) = current {
if nodes[i].0 == *state {
return true;
}
current = nodes[i].2;
}
false
}