1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! Cost-to-go estimates: the [`Heuristic`] trait, [`heuristic_fn`] for closures and the blind
//! heuristic [`Zero`].
use Box;
use crateCost;
/// 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);
/// ```
/// Turns a closure `FnMut(&S) -> Option<C>` into a [`Heuristic`].
///
/// # Examples
///
/// ```
/// use search_core::{Heuristic, heuristic_fn};
///
/// let mut calls = 0;
/// let mut h = heuristic_fn(|n: &u32| {
/// calls += 1;
/// 10_u32.checked_sub(*n)
/// });
///
/// assert_eq!(h.estimate(&3), Some(7));
/// assert_eq!(h.estimate(&12), None);
/// drop(h);
/// assert_eq!(calls, 2);
/// ```
/// A closure used as a [`Heuristic`]; created by [`heuristic_fn`].
;
/// The blind heuristic: estimates [`Cost::ZERO`] everywhere. Turns A* into Dijkstra's
/// algorithm and is admissible and consistent for every non-negative cost.
///
/// Implemented for every scalar [`Cost`] and for raw `f64` values, which are not a `Cost`
/// (they are not `Ord`) but are what value-iteration style searches estimate in. Cost
/// vectors keep their own zero heuristic (see `MIGRATION.md`).
///
/// # Examples
///
/// ```
/// use search_core::{Decimal, Heuristic, Zero};
///
/// assert_eq!(Heuristic::<&str>::estimate(&mut Zero, &"any"), Some(0_u32));
/// assert_eq!(Heuristic::<u8, Decimal<2>>::estimate(&mut Zero, &1), Some(Decimal::from_raw(0)));
/// assert_eq!(Heuristic::<u8, f64>::estimate(&mut Zero, &1), Some(0.0));
/// ```
;
/// Raw `f64` estimates: `f64` is not a [`Cost`], so this does not overlap the impl above.