Skip to main content

Heuristic

Trait Heuristic 

Source
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§

Source

fn estimate(&mut self, state: &S) -> Option<C>

Estimates the remaining cost from state to a goal.

None means no goal is reachable from state (a proven dead end). Searches drop such states, so a heuristic must only return None when that is certain.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<S, C, H> Heuristic<S, C> for &mut H
where H: Heuristic<S, C> + ?Sized,

Source§

fn estimate(&mut self, state: &S) -> Option<C>

Source§

impl<S, C, H> Heuristic<S, C> for Box<H>
where H: Heuristic<S, C> + ?Sized,

Available on crate feature alloc only.
Source§

fn estimate(&mut self, state: &S) -> Option<C>

Implementors§

Source§

impl<S, C, F> Heuristic<S, C> for HeuristicFn<F>
where F: FnMut(&S) -> Option<C>,

Source§

impl<S, C: Cost, const N: usize> Heuristic<S, [C; N]> for ZeroHeuristic

Source§

impl<S, C> Heuristic<S, C> for Zero
where C: Cost,

Source§

impl<S: Ord, C: Cost + WideMul, const N: usize> Heuristic<S, [C; N]> for Towards<'_, S, C, N>

Source§

impl<S: Ord, C: Cost, const N: usize> Heuristic<S, [C; N]> for &IdealPoint<S, C, N>

Source§

impl<S: Ord, C: Cost, const N: usize> Heuristic<S, [C; N]> for IdealPoint<S, C, N>

Source§

impl<S> Heuristic<S, f64> for Zero

Raw f64 estimates: f64 is not a Cost, so this does not overlap the impl above.