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
//! BOBA*: bi-objective bidirectional A*.
//!
//! Ahmadi, Tack, Harabor, Kilby: "Bi-Objective Search with Bi-Directional
//! A*", ESA 2021 (`LIPIcs` 204).
//!
//! BOBA* runs two BOA* searches with opposite directions and opposite
//! objective orders. The forward search, from the start in `(f1, f2)`
//! order, finds solutions from the cheapest-first end of the front; the
//! backward search, from the goal over reversed edges in `(f2, f1)` order,
//! finds them from the cheapest-second end. Neither joins partial paths:
//! each search runs all the way to the opposite end, with exact
//! single-objective distances as heuristics (or the caller's own, through
//! [`boba_star_with`]). Each search's last solution
//! bounds the other's primary objective, so they split the front between
//! them and stop where they meet.
//!
//! ```mermaid
//! graph TD
//! dists[Cost-bounded searches per objective:<br/>to the goal and from the start] --> fwd[Forward BOA*, order f1 f2]
//! dists --> bwd[Backward BOA*, order f2 f1]
//! fwd -- "last c2 bounds backward f2" --> bwd
//! bwd -- "last c1 bounds forward f1" --> fwd
//! fwd --> merge[Forward solutions, then backward ones reversed]
//! bwd --> merge
//! ```
//!
//! With its own heuristics, BOBA* also uses the paper's two enhancements
//! (Algorithm 2): the *early solution update* joins a popped label with the
//! known optimal completion of its state, records the joined path as a
//! solution if its second cost is the smallest so far, and does not expand
//! the label at all when that completion is optimal in both objectives;
//! *heuristic tuning* raises the other direction's secondary heuristic at a
//! state to the primary `g` of its first expansion here. Tuning also runs
//! with caller-supplied heuristics ([`boba_star_with`]).
//!
//! Differences from the paper, all of which only cost speed:
//!
//! - The searches alternate one label at a time instead of running in
//! parallel threads (the crate is `no_std`), unless the `parallel`
//! feature is on and `boba_star_parallel` or `boba_star_with_parallel`
//! is used.
//! - The four cost-bounded preliminary searches (see `bounds`) run one
//! after another instead of two at a time, again unless in parallel.
//! - The search stops as soon as either direction is done, instead of
//! letting both run out; at that point every solution has been found
//! (see [`boba_star`]).
use Vec;
use ;
use crate;
use crate;
use crate;
pub use ;
/// Finds one path for every Pareto-optimal cost vector from `start` to
/// `goal` by searching from both ends.
///
/// Requirements, not checked at run time:
///
/// - edge costs are non-negative;
/// - [`ReverseGraph::predecessors`] mirrors [`Graph::successors`] exactly,
/// parallel edges included;
/// - finitely many states lie within the bounds `ub1`, `ub2` (the first
/// cost of the `(c2, c1)`-optimal path and the second cost of the
/// `(c1, c2)`-optimal one) of the start and of the goal: the preliminary
/// searches expand exactly those. An unreachable goal is detected by a
/// search from the start, which must then terminate too.
///
/// No heuristic is needed: BOBA* computes exact per-objective distances
/// itself, with four cost-bounded searches that skip states on no
/// Pareto-optimal path; see [`boba_star_with`] to supply your own.
/// Solutions are sorted as in [`boa_star`](crate::boa_star): first
/// objective strictly ascending, second strictly descending. The counters
/// in [`Output::stats`] add up both directions and leave out the
/// preliminary searches.
///
/// # Why stopping early is safe
///
/// The forward search stops once its best open label has `f1 >= ub1`,
/// where `ub1` is the first cost of the backward search's last solution
/// `b`. By then it has found every Pareto-optimal solution with a first
/// cost below `ub1`. Every other Pareto-optimal solution has a first cost
/// above `b`'s, so a second cost below it, and the backward search, which
/// finds solutions in ascending second cost, already has it. The same
/// holds with the roles swapped.
///
/// # Examples
///
/// ```
/// use multiobjective_search::{Graph, ReverseGraph, boba_star};
///
/// /// Two routes from 0 to 2: fast but costly, or slow but cheap.
/// struct Routes;
///
/// /// The edges as `(from, to, cost)`.
/// const EDGES: [(u8, u8, [u32; 2]); 3] = [(0, 1, [1, 5]), (1, 2, [1, 5]), (0, 2, [4, 1])];
///
/// impl Graph for Routes {
/// type State = u8;
/// type Weight = [u32; 2];
///
/// fn successors(&self, state: &u8, out: &mut Vec<(u8, [u32; 2])>) {
/// out.extend(EDGES.iter().filter(|e| e.0 == *state).map(|e| (e.1, e.2)));
/// }
/// }
///
/// impl ReverseGraph for Routes {
/// fn predecessors(&self, state: &u8, out: &mut Vec<(u8, [u32; 2])>) {
/// out.extend(EDGES.iter().filter(|e| e.1 == *state).map(|e| (e.0, e.2)));
/// }
/// }
///
/// let out = boba_star(&Routes, 0, 2);
/// let costs: Vec<_> = out.solutions.iter().map(|s| s.cost).collect();
/// assert_eq!(costs, [[2, 10], [4, 1]]);
/// assert_eq!(out.solutions[0].path, [0, 1, 2]);
/// ```
/// [`boba_star`] with caller-supplied heuristics instead of the internal
/// preliminary searches.
///
/// - `forward_h(s)` bounds the cost from `s` to `goal` per objective;
/// - `backward_h(s)` bounds the cost from `start` to `s` per objective, in
/// the graph's objective order (the backward search swaps them itself).
///
/// Requirements, in addition to those of [`boba_star`] (finiteness aside):
/// both heuristics are consistent in both objectives, along the edges
/// they are used on (`forward_h(p) <= c(p, s) + forward_h(s)` and
/// `backward_h(s) <= backward_h(p) + c(p, s)` for every edge `p -> s`), and
/// zero at `goal` and at `start` respectively. A heuristic may return
/// `None` only for states on no Pareto-optimal path. Both directions'
/// stopping rule only needs these lower bounds, so the output equals
/// [`boa_star`](crate::boa_star)'s in cost vectors for any such pair,
/// [`ZeroHeuristic`](crate::ZeroHeuristic) included.
///
/// # Examples
///
/// ```
/// use multiobjective_search::{Graph, ReverseGraph, ZeroHeuristic, boba_star, boba_star_with};
///
/// /// Two routes from 0 to 2: fast but costly, or slow but cheap.
/// struct Routes;
///
/// /// The edges as `(from, to, cost)`.
/// const EDGES: [(u8, u8, [u32; 2]); 3] = [(0, 1, [1, 5]), (1, 2, [1, 5]), (0, 2, [4, 1])];
///
/// impl Graph for Routes {
/// type State = u8;
/// type Weight = [u32; 2];
///
/// fn successors(&self, state: &u8, out: &mut Vec<(u8, [u32; 2])>) {
/// out.extend(EDGES.iter().filter(|e| e.0 == *state).map(|e| (e.1, e.2)));
/// }
/// }
///
/// impl ReverseGraph for Routes {
/// fn predecessors(&self, state: &u8, out: &mut Vec<(u8, [u32; 2])>) {
/// out.extend(EDGES.iter().filter(|e| e.1 == *state).map(|e| (e.0, e.2)));
/// }
/// }
///
/// let blind = boba_star_with(&Routes, 0, 2, ZeroHeuristic, ZeroHeuristic);
/// assert_eq!(blind.solutions, boba_star(&Routes, 0, 2).solutions);
/// ```
/// Runs the two BOA* directions in lockstep, exchanging their bounds and,
/// for heuristic tuning, their first expansions. `completions` are the
/// known completions of both directions for the early solution update:
/// forward to the goal, backward to the start in swapped order.
/// Heuristic tuning (Ahmadi et al., line 10 of Algorithm 2): the first
/// expansion of a state in one direction, at primary cost `g`, raises the
/// other direction's secondary heuristic there to `g`.
///
/// Every cheaper path to the state was pruned in `from`, so its
/// completions are weakly dominated by solutions `from` already has; the
/// other direction need not find them.
/// The reversed graph with swapped objectives, searched by the backward
/// direction: its successors are the original predecessors.
;
/// A predicate that matches exactly `target`.
/// Swaps the two objectives of a cost vector.
/// Joins the two directions' outputs into one, in forward terms.
///
/// The forward solutions cover the low end of the first objective, the
/// backward ones the high end, in descending first-objective order; their
/// paths run from the goal to the start with swapped costs.
///
/// The two ends can overlap by one cost vector: a direction's last
/// solution may come from an early solution update, made before the other
/// direction's bound arrived, and be dominated by (or equal to) the other
/// direction's first. The join keeps only the non-dominated ones.