mod multi;
mod ratio;
use alloc::collections::{BTreeMap, BinaryHeap};
use alloc::vec::Vec;
use core::cmp::Reverse;
use search_core::{Cost, Heuristic};
use crate::graph::Graph;
use crate::output::{Output, Solution, Stats};
pub use multi::{SumsAndWidths, minsum_maxmin_multi};
pub use ratio::{WidePath, min_cost_capacity_ratio, quickest_path};
pub type Bottleneck<K> = Option<K>;
pub fn minsum_maxmin<G, H, C, K, P>(
graph: &G,
mut heuristic: H,
start: G::State,
is_goal: P,
) -> Output<G::State, (C, Bottleneck<K>)>
where
G: Graph<Weight = (C, K)>,
H: Heuristic<G::State, C>,
C: Cost,
K: Copy + Ord,
P: Fn(&G::State) -> bool,
{
let mut nodes: Vec<Node<G::State, C, K>> = Vec::new();
let mut open = BinaryHeap::new();
let mut best: BTreeMap<G::State, Bottleneck<K>> = BTreeMap::new();
let mut solved: Option<Bottleneck<K>> = None;
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,
cost: C::ZERO,
width: None,
parent: None,
});
open.push(Reverse((h, Reverse(Width(None)), 0)));
stats.generated += 1;
}
while let Some(Reverse((_, _, index))) = open.pop() {
let (here, cost, width) = {
let node = &nodes[index];
(node.state.clone(), node.cost, node.width)
};
if covers(solved, width) || best.get(&here).is_some_and(|&b| covers(Some(b), width)) {
continue;
}
best.insert(here.clone(), width);
if is_goal(&here) {
solved = Some(width);
solutions.push(rebuild(&nodes, index));
continue;
}
stats.expanded += 1;
graph.successors(&here, &mut successors);
for (next, (edge_cost, capacity)) in successors.drain(..) {
let narrowed = Some(width.map_or(capacity, |w| w.min(capacity)));
if covers(solved, narrowed)
|| best.get(&next).is_some_and(|&b| covers(Some(b), narrowed))
{
continue;
}
let Some(h) = heuristic.estimate(&next) else {
continue;
};
let g = cost + edge_cost;
nodes.push(Node {
state: next,
cost: g,
width: narrowed,
parent: Some(index),
});
open.push(Reverse((g + h, Reverse(Width(narrowed)), nodes.len() - 1)));
stats.generated += 1;
}
}
Output { solutions, stats }
}
struct Node<S, C, K> {
state: S,
cost: C,
width: Bottleneck<K>,
parent: Option<usize>,
}
#[derive(Clone, Copy, PartialEq, Eq)]
struct Width<K>(Bottleneck<K>);
impl<K: Ord> PartialOrd for Width<K> {
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<K: Ord> Ord for Width<K> {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
match (&self.0, &other.0) {
(None, None) => core::cmp::Ordering::Equal,
(None, Some(_)) => core::cmp::Ordering::Greater,
(Some(_), None) => core::cmp::Ordering::Less,
(Some(a), Some(b)) => a.cmp(b),
}
}
}
fn covers<K: Ord + Copy>(known: Option<Bottleneck<K>>, width: Bottleneck<K>) -> bool {
known.is_some_and(|k| Width(k) >= Width(width))
}
fn rebuild<S: Clone, C: Copy, K: Copy>(
nodes: &[Node<S, C, K>],
index: usize,
) -> Solution<S, (C, Bottleneck<K>)> {
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].cost, nodes[index].width),
path,
}
}