use alloc::vec::Vec;
use core::hash::Hash;
use search_core::{Budget, Cost, Goal, Heuristic};
use crate::outcome::Stats;
use crate::problem::{Successors, collect_successors};
use crate::real_time::{End, LearnedHeuristic, Trajectory, TrialResult, TrialStats};
type GraphTrial<G> =
TrialResult<<G as Successors>::State, <G as Successors>::Action, <G as Successors>::Cost>;
pub fn lrta_star<G, T, H>(
graph: &G,
start: G::State,
goal: &T,
table: &mut LearnedHeuristic<G::State, G::Cost, H>,
budget: impl Budget<Stats>,
) -> GraphTrial<G>
where
G: Successors,
G::State: Clone + Hash + Eq,
T: Goal<G::State> + ?Sized,
H: Heuristic<G::State, G::Cost>,
{
run(graph, start, goal, table, Rule::Lrta, budget)
}
pub fn rta_star<G, T, H>(
graph: &G,
start: G::State,
goal: &T,
table: &mut LearnedHeuristic<G::State, G::Cost, H>,
budget: impl Budget<Stats>,
) -> GraphTrial<G>
where
G: Successors,
G::State: Clone + Hash + Eq,
T: Goal<G::State> + ?Sized,
H: Heuristic<G::State, G::Cost>,
{
run(graph, start, goal, table, Rule::Rta, budget)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Rule {
Lrta,
Rta,
}
struct Choice<C> {
best: usize,
best_value: C,
second_value: Option<C>,
}
impl<C: Cost> Choice<C> {
fn learned(&self, rule: Rule) -> C {
match rule {
Rule::Lrta => self.best_value,
Rule::Rta => self.second_value.unwrap_or(self.best_value),
}
}
}
fn run<G, T, H>(
graph: &G,
start: G::State,
goal: &T,
table: &mut LearnedHeuristic<G::State, G::Cost, H>,
rule: Rule,
mut budget: impl Budget<Stats>,
) -> GraphTrial<G>
where
G: Successors,
G::State: Clone + Hash + Eq,
T: Goal<G::State> + ?Sized,
H: Heuristic<G::State, G::Cost>,
{
let mut path = Trajectory::new(start);
let mut stats = Stats::default();
let mut trial = TrialStats::default();
let mut buffer = Vec::new();
let end = loop {
if goal.is_goal(path.current()) {
break End::Goal;
}
if budget.exhausted(&stats) {
break End::Limit;
}
stats.expanded += 1;
stats.generated += collect_successors(graph, path.current(), &mut buffer);
let choice = evaluate(&buffer, table);
let value = choice.as_ref().map(|choice| choice.learned(rule));
let changed = table.set(path.current().clone(), value);
trial.record_step(1, u64::from(changed));
let Some(choice) = choice else {
break End::Unsolvable;
};
let (action, next, cost) = buffer.swap_remove(choice.best);
path.travel(action, next, cost);
trial.moves += 1;
buffer.clear();
};
path.finish(end, stats, trial)
}
fn evaluate<S: Hash + Eq, A, C: Cost, H: Heuristic<S, C>>(
buffer: &[(A, S, C)],
table: &mut LearnedHeuristic<S, C, H>,
) -> Option<Choice<C>> {
let mut choice: Option<Choice<C>> = None;
for (index, (_, next, cost)) in buffer.iter().enumerate() {
let Some(h) = table.get(next) else {
continue;
};
let value = cost.saturating_add(h);
choice = Some(match choice {
None => Choice {
best: index,
best_value: value,
second_value: None,
},
Some(old) if value < old.best_value => Choice {
best: index,
best_value: value,
second_value: Some(old.best_value),
},
Some(old) => Choice {
second_value: Some(old.second_value.map_or(value, |second| second.min(value))),
..old
},
});
}
choice
}