pub trait Heuristic<S, C = u32> {
// Required method
fn estimate(&mut self, state: &S) -> Option<C>;
}Expand description
An estimate of the cost from a state of type S to the nearest goal, in cost type C.
estimate takes &mut self, so a heuristic can keep a reusable workspace (scratch
buffers, caches) without interior mutability. Searches take the heuristic by value and own
it for the run; pass &mut h to keep h and its workspace afterwards. Share read-only
tables by reference and create one heuristic per thread.
C defaults to u32 and is deliberately unbounded: scalar searches use a Cost,
multi-objective searches a cost vector such as [u32; 2], and probabilistic planners an
expected cost such as f64. Each algorithm states what it requires (admissibility,
consistency).
Implemented for &mut H and (feature alloc) Box<H> of any heuristic H, so
Box<dyn Heuristic<S, C>> works; for Zero; and for closures through
heuristic_fn. A blanket impl for closures cannot coexist with the one for &mut H,
because &mut F is itself a closure (E0119).
§Examples
A heuristic with a workspace:
use search_core::Heuristic;
/// Counts how often it was asked; a real heuristic would reuse buffers here.
struct Distance {
target: u32,
calls: usize,
}
impl Heuristic<u32> for Distance {
fn estimate(&mut self, state: &u32) -> Option<u32> {
self.calls += 1;
self.target.checked_sub(*state)
}
}
let mut h = Distance { target: 10, calls: 0 };
assert_eq!(h.estimate(&4), Some(6));
assert_eq!((&mut h).estimate(&11), None);
assert_eq!(h.calls, 2);Required Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Implementors§
impl<S, C, F> Heuristic<S, C> for HeuristicFn<F>
impl<S, C: Cost, const N: usize> Heuristic<S, [C; N]> for ZeroHeuristic
impl<S, C> Heuristic<S, C> for Zerowhere
C: Cost,
impl<S: Ord, C: Cost + WideMul, const N: usize> Heuristic<S, [C; N]> for Towards<'_, S, C, N>
impl<S: Ord, C: Cost, const N: usize> Heuristic<S, [C; N]> for &IdealPoint<S, C, N>
impl<S: Ord, C: Cost, const N: usize> Heuristic<S, [C; N]> for IdealPoint<S, C, N>
impl<S> Heuristic<S, f64> for Zero
Raw f64 estimates: f64 is not a Cost, so this does not overlap the impl above.