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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//! Bidirectional search that meets in the middle ([`mm`], Holte, Felner, Sharon and
//! Sturtevant 2016).
use Hash;
use ;
use crateGraphResult;
use crate;
use crate;
use crate;
use crateCostExt;
use crateStats;
use cratePredecessors;
use crateSuccessors;
/// MM: bidirectional heuristic search that is guaranteed to meet in the middle (Holte,
/// Felner, Sharon and Sturtevant 2016).
///
/// Orders each side by `pr = max(f, 2g)` with `f = g + h`, and expands the side whose
/// smallest `pr` is smaller. With `C = min(pr_min_F, pr_min_B)`, the smaller of the two
/// sides' smallest `pr`, it stops once the cheapest meet
/// `mu <= max(C, f_min_F, f_min_B, g_min_F + g_min_B + epsilon)`; each term is a lower
/// bound on the optimal cost. `epsilon` is the
/// smallest edge cost, which a generic [`Cost`](crate::Cost) cannot know; it is taken as
/// zero, which is correct for every cost type but may expand more states than a positive
/// `epsilon` would.
///
/// Every state MM expands has `pr <= C*`, the optimal cost, and since `pr >= 2g` neither
/// side expands a state whose `g` exceeds `C* / 2`: the frontiers meet in the middle.
/// `forward` estimates the cost to the nearest goal, `backward` the cost from the start;
/// with admissible estimates the solution is optimal. With [`Zero`](crate::Zero) for both,
/// this is MM0, a blind bidirectional search with the same guarantee.
///
/// # Examples
///
/// ```
/// use heuristic_search::{Limits, Predecessors, Successors, heuristic_fn, mm};
///
/// /// A line of integers; each step moves one to the left or right at cost 1.
/// struct Line;
///
/// impl Successors for Line {
/// type State = i32;
/// type Action = i32;
/// type Cost = u32;
///
/// fn successors(&self, state: &i32, mut emit: impl FnMut(i32, i32, u32)) {
/// emit(-1, state - 1, 1);
/// emit(1, state + 1, 1);
/// }
/// }
///
/// impl Predecessors for Line {
/// type Node = i32;
/// type Action = i32;
/// type Cost = u32;
///
/// fn predecessors(&self, state: &i32, mut emit: impl FnMut(i32, i32, u32)) {
/// emit(1, state - 1, 1);
/// emit(-1, state + 1, 1);
/// }
/// }
///
/// let to_goal = heuristic_fn(|s: &i32| Some(9_i32.abs_diff(*s)));
/// let from_start = heuristic_fn(|s: &i32| Some(0_i32.abs_diff(*s)));
/// let result = mm(&Line, 0, [9], to_goal, from_start, Limits::NONE);
///
/// assert_eq!(result.solution().map(|solution| solution.cost), Some(9));
/// ```
/// [`mm`] from several start states at once.
///
/// Every start has path cost zero on the forward side, just as every goal has on the
/// backward side. Repeated starts count once, an empty set of starts finds nothing, and a
/// start that is a goal is returned as a zero-cost path. The backward heuristic must estimate
/// the cost back to the nearest start, which a heuristic towards one start overestimates.
///
/// # Examples
///
/// ```
/// use heuristic_search::{Limits, Predecessors, Successors, Zero, heuristic_fn, mm_multi};
///
/// /// Counting up by one at cost 1, and back down for the backward search.
/// struct Up;
///
/// impl Successors for Up {
/// type State = u32;
/// type Action = ();
/// type Cost = u32;
///
/// fn successors(&self, n: &u32, mut emit: impl FnMut((), u32, u32)) {
/// emit((), n + 1, 1);
/// }
/// }
///
/// impl Predecessors for Up {
/// type Node = u32;
/// type Action = ();
/// type Cost = u32;
///
/// fn predecessors(&self, n: &u32, mut emit: impl FnMut((), u32, u32)) {
/// if let Some(previous) = n.checked_sub(1) {
/// emit((), previous, 1);
/// }
/// }
/// }
///
/// let h = heuristic_fn(|n: &u32| 8_u32.checked_sub(*n));
/// let result = mm_multi(&Up, [0, 6, 3], [8], h, Zero, Limits::NONE);
///
/// assert_eq!(result.solution().map(|s| s.cost), Some(2));
/// ```
/// [`mm`] that meets by a [`MeetRelation`] instead of state equality, from several starts
/// at once.
///
/// `goals` are backward nodes of the graph's [`Predecessors::Node`](crate::Predecessors)
/// type, such as subgoals (partial states), and `meet` decides which forward states meet
/// which backward nodes (see [`MeetRelation`] for the contract). The returned path is
/// rebuilt from the meeting state by replaying the backward actions forwards, through the
/// first successor edge with an equal label, so every state on it is a real state and its
/// cost the true cost.
///
/// Both heuristics are evaluated on states and on backward nodes, so they implement
/// [`Heuristic`] for both types ([`Zero`](crate::Zero) does). On a backward node such as a
/// subgoal, each must stay admissible when read as the cheapest value over the states
/// that satisfy the node.
///
/// # Errors
///
/// [`ReplayError`](crate::ReplayError) if a replay step finds no successor edge with the backward action's
/// label: the relation or the predecessors disagree with the successors.
///
/// The backward heuristic estimates the cost from the starts to a backward node.
///
/// # Examples
///
/// ```
/// use heuristic_search::{Limits, Predecessors, Successors, Zero, meet_fn, mm_meet};
///
/// /// Three facts as bits; action `k` makes fact `k` true at cost 1.
/// struct Facts;
///
/// impl Successors for Facts {
/// type State = u8;
/// type Action = u8;
/// type Cost = u32;
///
/// fn successors(&self, state: &u8, mut emit: impl FnMut(u8, u8, u32)) {
/// for k in 0..3 {
/// if state & (1 << k) == 0 {
/// emit(k, state | (1 << k), 1);
/// }
/// }
/// }
/// }
///
/// // Regression: a subgoal is a set of facts that must hold; action `k` achieves fact `k`.
/// impl Predecessors for Facts {
/// type Node = u8;
/// type Action = u8;
/// type Cost = u32;
///
/// fn predecessors(&self, subgoal: &u8, mut emit: impl FnMut(u8, u8, u32)) {
/// for k in 0..3 {
/// if subgoal & (1 << k) != 0 {
/// emit(k, subgoal & !(1 << k), 1);
/// }
/// }
/// }
/// }
///
/// // A state meets a subgoal it satisfies.
/// let satisfies = meet_fn(|state: &u8, subgoal: &u8| state & subgoal == *subgoal);
/// let result = mm_meet(&Facts, [0], [0b110], Zero, Zero, satisfies, Limits::NONE);
///
/// let solution = result.expect("the model replays").into_solution().expect("facts 1 and 2 are reachable");
/// assert_eq!(solution.cost, 2);
/// assert_eq!(solution.states.len(), 3);
/// assert_eq!(solution.goal() & 0b110, 0b110);
/// ```