use alloc::collections::{BTreeMap, BTreeSet, BinaryHeap};
use alloc::vec::Vec;
use core::cmp::{Ordering, Reverse};
use search_core::{Cost, Heuristic, WideMul};
use crate::anytime_boa::lex::lex_first;
use crate::constrained::rebuild;
use crate::cost::add;
use crate::graph::Graph;
use crate::output::{Output, Solution, Stats};
#[expect(
clippy::needless_pass_by_value,
reason = "takes the start by value like every other search in the crate"
)]
pub fn supported_solutions<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 + WideMul,
P: Fn(&G::State) -> bool,
{
let mut stats = Stats::default();
let solutions = phase_one(graph, &mut heuristic, &start, &is_goal, &mut stats);
Output { solutions, stats }
}
#[expect(
clippy::needless_pass_by_value,
reason = "takes the start by value like every other search in the crate"
)]
pub fn two_phase<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 + WideMul,
P: Fn(&G::State) -> bool,
{
let mut stats = Stats::default();
let supported = phase_one(graph, &mut heuristic, &start, &is_goal, &mut stats);
let mut solutions = Vec::new();
for pair in supported.windows(2) {
let (a, b) = (pair[0].cost, pair[1].cost);
solutions.extend(in_box(
graph,
&mut heuristic,
&start,
&is_goal,
a,
b,
&mut stats,
));
}
solutions.extend(supported);
solutions.sort_by_key(|s| s.cost);
Output { solutions, stats }
}
fn phase_one<G, H, C, P>(
graph: &G,
heuristic: &mut H,
start: &G::State,
is_goal: &P,
stats: &mut Stats,
) -> Vec<Solution<G::State, [C; 2]>>
where
G: Graph<Weight = [C; 2]>,
H: Heuristic<G::State, [C; 2]>,
C: Cost + WideMul,
P: Fn(&G::State) -> bool,
{
let Some(left) = lex_first(graph, heuristic, start, is_goal, 0, stats) else {
return Vec::new();
};
let right = lex_first(graph, heuristic, start, is_goal, 1, stats)
.expect("reachable in one order, reachable in the other");
if left.cost == right.cost {
return alloc::vec![left];
}
let mut found = alloc::vec![left];
let mut pending = alloc::vec![right];
while let Some(b) = pending.pop() {
let a = found.last().expect("starts with the left optimum").cost;
let weights = [a[1].abs_diff(b.cost[1]), b.cost[0].abs_diff(a[0])];
let best = weighted_best(graph, heuristic, start, is_goal, weights, stats)
.expect("a solution exists");
if weighted_cmp(weights, best.cost, a) == Ordering::Less {
pending.push(b);
pending.push(best);
} else {
found.push(b);
}
}
found
}
fn weighted_cmp<C: Cost + WideMul>(w: [C; 2], x: [C; 2], y: [C; 2]) -> Ordering {
let part = |i: usize| (x[i].cmp(&y[i]), w[i].wide_mul(x[i].abs_diff(y[i])));
let ((s1, m1), (s2, m2)) = (part(0), part(1));
match (s1, s2) {
(Ordering::Equal, s) | (s, Ordering::Equal) => s,
(left, right) if left == right => left,
(Ordering::Greater, _) => m1.cmp(&m2),
(Ordering::Less, _) => m2.cmp(&m1),
}
}
fn weighted_best<G, H, C, P>(
graph: &G,
heuristic: &mut H,
start: &G::State,
is_goal: &P,
weights: [C; 2],
stats: &mut Stats,
) -> Option<Solution<G::State, [C; 2]>>
where
G: Graph<Weight = [C; 2]>,
H: Heuristic<G::State, [C; 2]>,
C: Cost + WideMul,
P: Fn(&G::State) -> bool,
{
let mut nodes: Vec<(G::State, [C; 2], Option<usize>)> = Vec::new();
let mut open = BinaryHeap::new();
let mut closed = BTreeSet::new();
let mut successors = Vec::new();
let h = heuristic.estimate(start)?;
nodes.push((start.clone(), [C::ZERO; 2], None));
open.push(Reverse(Weighted {
weights,
f: h,
index: 0,
}));
stats.generated += 1;
while let Some(Reverse(Weighted { index, .. })) = open.pop() {
let (here, g, _) = nodes[index].clone();
if !closed.insert(here.clone()) {
continue;
}
if is_goal(&here) {
return Some(rebuild(&nodes, index));
}
stats.expanded += 1;
graph.successors(&here, &mut successors);
for (next, weight) in successors.drain(..) {
if closed.contains(&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(Weighted {
weights,
f: add(g_next, h),
index: nodes.len() - 1,
}));
stats.generated += 1;
}
}
None
}
struct Weighted<C> {
weights: [C; 2],
f: [C; 2],
index: usize,
}
impl<C: Cost + WideMul> PartialEq for Weighted<C> {
fn eq(&self, other: &Self) -> bool {
self.cmp(other) == Ordering::Equal
}
}
impl<C: Cost + WideMul> Eq for Weighted<C> {}
impl<C: Cost + WideMul> PartialOrd for Weighted<C> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<C: Cost + WideMul> Ord for Weighted<C> {
fn cmp(&self, other: &Self) -> Ordering {
weighted_cmp(self.weights, self.f, other.f)
.then(self.f.cmp(&other.f))
.then(self.index.cmp(&other.index))
}
}
fn in_box<G, H, C, P>(
graph: &G,
heuristic: &mut H,
start: &G::State,
is_goal: &P,
left: [C; 2],
right: [C; 2],
stats: &mut Stats,
) -> Vec<Solution<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 nodes: Vec<(G::State, [C; 2], Option<usize>)> = Vec::new();
let mut open = BinaryHeap::new();
let mut g2_min: BTreeMap<G::State, C> = BTreeMap::new();
let mut bound = left[1];
let mut found = Vec::new();
let mut successors = Vec::new();
let outside = |f: [C; 2], bound: C| f[0] >= right[0] || f[1] >= bound;
let Some(h) = heuristic.estimate(start) else {
return found;
};
nodes.push((start.clone(), [C::ZERO; 2], None));
open.push(Reverse((h, 0)));
stats.generated += 1;
while let Some(Reverse((f, index))) = open.pop() {
let (here, g, _) = nodes[index].clone();
if outside(f, bound) || g2_min.get(&here).is_some_and(|&m| g[1] >= m) {
continue;
}
g2_min.insert(here.clone(), g[1]);
if is_goal(&here) {
bound = g[1];
found.push(rebuild(&nodes, index));
continue;
}
stats.expanded += 1;
graph.successors(&here, &mut successors);
for (next, weight) in successors.drain(..) {
let g_next = add(g, weight);
if g2_min.get(&next).is_some_and(|&m| g_next[1] >= m) {
continue;
}
let Some(h) = heuristic.estimate(&next) else {
continue;
};
let f = add(g_next, h);
if !outside(f, bound) {
nodes.push((next, g_next, Some(index)));
open.push(Reverse((f, nodes.len() - 1)));
stats.generated += 1;
}
}
}
found.retain(|s| s.cost[0] > left[0] && s.cost[1] > right[1]);
found
}