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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
//! Incremental search on graphs whose edge costs change: [`LpaStar`] (Lifelong Planning
//! A*), [`DStarLite`] and [`AdStar`] (Anytime D*), over a [`DynamicGraph`].
//!
//! Both keep their search tree between calls. After the caller changes edge costs in the
//! graph, it reports each changed edge (`edge_changed`) and asks for a new path
//! (`compute`); only the part of the tree the changes affect is repaired. Edges are
//! identified by their endpoints; parallel edges count with their cheapest cost.
//!
//! | Planner | Searches | Fixed | Can move |
//! | -------------- | ----------------- | ----------- | ------------ |
//! | [`LpaStar`] | start to goal | both ends | nothing |
//! | [`DStarLite`] | goal to start | the goal | the start |
//! | [`AdStar`] | goal to start | the goal | the start |
//!
//! [`AdStar`] is the anytime variant of [`DStarLite`]: it inflates the heuristic by a
//! weight the caller lowers over time. [`LpaStarMulti`], [`DStarLiteMulti`] and
//! [`AdStarMulti`] take several fixed starts, through a virtual super-start.
//!
//! `FieldDStar` (feature `float`) is D* Lite over the corners of a grid with per-cell costs
//! rather than over a [`DynamicGraph`]: its `rhs` interpolates along cell edges, and its
//! paths run through points on them.
//!
//! Both need strictly positive edge costs, as in the papers: a zero-cost cycle can keep
//! supporting its own stale distances after the edge that made them real disappears. And
//! both need a consistent heuristic `h(a, b)` that estimates the cost from `a` to `b`, for
//! the costs at every point in time; `|_, _| 0` always works.
use Hash;
use ;
use crateWeight;
use crate;
pub use ;
use Incremental;
pub use ;
/// A graph with stable vertices whose edge costs may change between searches.
///
/// Both directions must agree and reflect the current costs: `to` is reported as a
/// successor of `from` with cost `c` exactly when `from` is reported as a predecessor of
/// `to` with cost `c`.
/// The result of an incremental search: a path of vertices. Edges carry no labels, so the
/// solution's actions are `()`.
pub type DynamicResult<G> =
;
/// Lifelong Planning A* (LPA*, Koenig, Likhachev and Furcy 2004): A* that repairs its
/// search tree after edge-cost changes instead of starting over.
///
/// The first [`LpaStar::compute`] costs about as much as A*. After the caller changes the
/// graph and reports every changed edge with [`LpaStar::edge_changed`], the next
/// `compute` re-expands only vertices whose distance from the start may have changed.
/// Returns cheapest paths with a consistent `heuristic`, where `heuristic(v, goal)`
/// estimates the cost from `v` to the goal.
///
/// # Examples
///
/// ```
/// use heuristic_search::{DynamicGraph, Limits, LpaStar};
///
/// /// A directed graph as `(from, to, cost)` triples.
/// struct Edges(Vec<(u8, u8, u32)>);
///
/// impl DynamicGraph for Edges {
/// type Vertex = u8;
/// type Cost = u32;
///
/// fn successors(&self, v: &u8, mut emit: impl FnMut(u8, u32)) {
/// self.0.iter().filter(|e| e.0 == *v).for_each(|e| emit(e.1, e.2));
/// }
///
/// fn predecessors(&self, v: &u8, mut emit: impl FnMut(u8, u32)) {
/// self.0.iter().filter(|e| e.1 == *v).for_each(|e| emit(e.0, e.2));
/// }
/// }
///
/// let mut graph = Edges(vec![(0, 1, 1), (1, 3, 1), (0, 2, 2), (2, 3, 2)]);
/// let mut planner = LpaStar::new(&graph, 0, 3, |_: &u8, _: &u8| 0);
/// assert_eq!(planner.compute(&graph, Limits::NONE).solution().map(|s| s.cost), Some(2));
///
/// // The edge 1 -> 3 gets expensive; report it and repair.
/// graph.0[1].2 = 10;
/// planner.edge_changed(&graph, &1, &3);
/// let solution = planner.compute(&graph, Limits::NONE).into_solution().expect("3 is reachable");
/// assert_eq!(solution.states, [0, 2, 3]);
/// assert_eq!(solution.cost, 4);
/// ```
/// D* Lite (Koenig and Likhachev 2002): incremental search towards a fixed goal from a
/// start that moves, as when a robot replans while it drives and discovers obstacles.
///
/// Searches backwards from the goal, so the tree stays valid as the start moves. Move the
/// start with [`DStarLite::move_start`], report changed edges with
/// [`DStarLite::edge_changed`], then [`DStarLite::compute`] a path from the current start.
/// Returns cheapest paths with a consistent `heuristic`, where `heuristic(start, v)`
/// estimates the cost from the start to `v`.
///
/// # Examples
///
/// ```
/// use heuristic_search::{DStarLite, DynamicGraph, Limits};
///
/// /// A directed graph as `(from, to, cost)` triples.
/// struct Edges(Vec<(u8, u8, u32)>);
///
/// impl DynamicGraph for Edges {
/// type Vertex = u8;
/// type Cost = u32;
///
/// fn successors(&self, v: &u8, mut emit: impl FnMut(u8, u32)) {
/// self.0.iter().filter(|e| e.0 == *v).for_each(|e| emit(e.1, e.2));
/// }
///
/// fn predecessors(&self, v: &u8, mut emit: impl FnMut(u8, u32)) {
/// self.0.iter().filter(|e| e.1 == *v).for_each(|e| emit(e.0, e.2));
/// }
/// }
///
/// let mut graph = Edges(vec![(0, 1, 1), (1, 2, 1), (2, 3, 1), (1, 3, 5)]);
/// let mut planner = DStarLite::new(&graph, 0, 3, |_: &u8, _: &u8| 0);
/// assert_eq!(planner.compute(&graph, Limits::NONE).solution().map(|s| s.cost), Some(3));
///
/// // Drive to 1, then find 2 -> 3 blocked.
/// planner.move_start(1);
/// graph.0.remove(2);
/// planner.edge_changed(&graph, &2, &3);
/// let solution = planner.compute(&graph, Limits::NONE).into_solution().expect("3 is reachable");
/// assert_eq!(solution.states, [1, 3]);
/// assert_eq!(solution.cost, 5);
/// ```
/// Anytime Dynamic A* (AD*, Likhachev, Ferguson, Gordon, Stentz and Thrun 2005): D* Lite
/// with an inflated heuristic, so a first path comes fast and improves as time allows,
/// while the start moves and edges change.
///
/// Each [`AdStar::compute`] is one iteration: it returns a path that costs at most the
/// current weight times the optimum (with a consistent `heuristic`). Lower the weight with
/// [`AdStar::set_weight`] between calls to improve the path, ending with
/// [`Weight::ONE`](crate::Weight::ONE) for an optimal one; move the start and report
/// changed edges as with [`DStarLite`]. After large changes, raising the weight again
/// gets a new path faster. Vertices that become inconsistent after their expansion in an
/// iteration wait for the next one (AD*'s `INCONS`). Start moves shift keys by D* Lite's
/// `km`; only lowering the weight re-keys the whole queue, which is linear in the vertices
/// seen so far.
///
/// # Panics
///
/// [`AdStar::new`] and [`AdStar::set_weight`] panic if the weight is below `1`.
///
/// # Examples
///
/// ```
/// use heuristic_search::{AdStar, DynamicGraph, Limits, Weight};
///
/// /// A line `0..=9`; each step moves one to the left or right at cost 1.
/// struct Line;
///
/// impl DynamicGraph for Line {
/// type Vertex = u8;
/// type Cost = u32;
///
/// fn successors(&self, v: &u8, mut emit: impl FnMut(u8, u32)) {
/// if *v > 0 {
/// emit(v - 1, 1);
/// }
/// if *v < 9 {
/// emit(v + 1, 1);
/// }
/// }
///
/// fn predecessors(&self, v: &u8, emit: impl FnMut(u8, u32)) {
/// self.successors(v, emit);
/// }
/// }
///
/// let distance = |a: &u8, b: &u8| u32::from(a.abs_diff(*b));
/// let mut planner = AdStar::new(&Line, 2, 9, distance, Weight::integer(3));
/// assert_eq!(planner.compute(&Line, Limits::NONE).solution().map(|s| s.cost), Some(7));
///
/// planner.set_weight(Weight::ONE);
/// assert_eq!(planner.compute(&Line, Limits::NONE).solution().map(|s| s.cost), Some(7));
/// ```
/// Panics unless `weight` is at least `1`.
pub