mod lookahead;
mod single;
mod table;
pub use lookahead::{lhb_lrta_star, lhb_update, lss_lrta_star, rtaa_star};
pub use single::{lrta_star, rta_star};
pub use table::LearnedHeuristic;
use alloc::vec::Vec;
use search_core::Cost;
#[cfg(doc)]
use search_core::Budget;
use crate::outcome::{Outcome, Solution, Stats};
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct TrialStats {
pub moves: u64,
pub steps: u64,
pub updates: u64,
pub max_step_expansions: u64,
pub max_step_updates: u64,
}
impl TrialStats {
fn record_step(&mut self, expansions: u64, updates: u64) {
self.steps += 1;
self.updates += updates;
self.max_step_expansions = self.max_step_expansions.max(expansions);
self.max_step_updates = self.max_step_updates.max(updates);
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TrialResult<S, A, C> {
pub outcome: Outcome<S, A, C>,
pub stats: Stats,
pub trial: TrialStats,
}
impl<S, A, C> TrialResult<S, A, C> {
#[must_use]
pub fn solution(&self) -> Option<&Solution<S, A, C>> {
match &self.outcome {
Outcome::Found(solution) => Some(solution),
Outcome::Unsolvable | Outcome::LimitReached | Outcome::Cutoff => None,
}
}
}
struct Trajectory<S, A, C> {
states: Vec<S>,
actions: Vec<A>,
cost: C,
}
impl<S: Clone, A, C: Cost> Trajectory<S, A, C> {
fn new(start: S) -> Self {
Self {
states: alloc::vec![start],
actions: Vec::new(),
cost: C::ZERO,
}
}
fn current(&self) -> &S {
self.states.last().expect("a trajectory is never empty")
}
fn travel(&mut self, action: A, next: S, cost: C) {
self.actions.push(action);
self.states.push(next);
self.cost = self.cost + cost;
}
fn finish(self, end: End, stats: Stats, trial: TrialStats) -> TrialResult<S, A, C> {
let outcome = match end {
End::Goal => Outcome::Found(Solution {
states: self.states,
actions: self.actions,
cost: self.cost,
}),
End::Unsolvable => Outcome::Unsolvable,
End::Limit => Outcome::LimitReached,
};
TrialResult {
outcome,
stats,
trial,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum End {
Goal,
Unsolvable,
Limit,
}