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
//! M* ([`mstar`], Wagner and Choset 2015): A* in the joint space of all agents with
//! subdimensional expansion, optimal for sum of costs or makespan ([`Objective`]), in its
//! basic and its recursive form, each with or without operator decomposition
//! ([`MStarConfig`]).
//!
//! Each agent follows its individual optimal policy (a fixed shortest path to its goal)
//! until it collides. Colliding agents join the *collision set* of the state where the
//! collision was found, and that set is passed back to every predecessor. Only agents in a
//! state's collision set leave their policy; the others keep following it. So the search
//! stays low-dimensional where agents do not interact.
//!
//! - **Basic M\*** keeps one collision set per state, and its agents branch over all their
//! moves.
//! - **Recursive M\* (rM\*)** keeps the collision set as disjoint groups: two collisions
//! that share no agent stay apart. A group that holds every agent of the search branches
//! over all moves, as in basic M\*. A smaller group moves along its own optimal joint
//! path, which a separate M\* search over just that group computes, recursively, and
//! caches. Only when a group's path collides with another agent do the two merge. So
//! two pairs of interacting agents far apart cost two small searches, not one over all
//! four.
//! - **Operator decomposition** (`OD-M*` and `ODrM*`, Ferner, Wagner and Choset 2013): where
//! agents would branch over all combinations of their moves, they move one at a time
//! instead, through intermediate states that have their own collision sets. An agent
//! branches at an intermediate state only once a collision put it into that state's set.
//!
//! Goal-stay pricing, the same as an exact joint search: a joint state is every agent's
//! vertex plus a *finished* flag. An unfinished agent at its goal may finish; from then on
//! it stays there for free. Every other unfinished agent pays 1 per step, move or wait. So
//! waiting at the goal before leaving it again is paid for. The heuristic, the sum of the
//! unfinished agents' distances to their goals, is consistent.
//!
//! **Makespan** is solved through sum of costs under a deadline. The sum-of-costs plan's
//! makespan `U` bounds the optimum from above, the largest individual distance `L` from
//! below. For each deadline `M` from `L` up to `U - 1`, a sum-of-costs search over states
//! that carry their time, pruning every state from which an unfinished agent cannot reach
//! its goal by `M`, either finds a plan (whose makespan is then optimal) or proves there is
//! none; if none succeeds, the sum-of-costs plan is optimal. So the plan returned for
//! makespan also has the least sum of costs among the makespan-optimal plans. (Pricing a
//! joint step at 1 whenever any agent pays, as the oracle does, is also exact but leaves
//! the search wide plateaus of equal `f`; it took 83 s on the benchmark instance.)
//!
//! ```mermaid
//! flowchart TD
//! pop[pop the state with the lowest f] -->|all agents finished| solved[Solved]
//! pop -->|open list empty| unsolvable[Unsolvable]
//! pop --> succ[successors: a group of all agents branches, a smaller group follows its own optimal path, the rest follow their policy]
//! succ -.->|rM*: a smaller group's path is not cached| sub[M* over that group alone]
//! sub -.-> succ
//! succ -->|transition collides| back[merge the colliders into the collision set, pass it back to predecessors, reopen them]
//! succ -->|collision-free| relax[record predecessor, pass back its collision set, relax g]
//! back --> pop
//! relax --> pop
//! ```
use BTreeMap;
use Vec;
use ;
use ;
use Planner;
use Key;
use crate;
use crateInstance;
use cratedistance_tables;
use crate;
use crate;
/// What [`mstar_with`] optimizes and which form of M* it runs.
///
/// Every form returns a plan with the optimal value of the objective; they differ in
/// speed.
/// Finds a conflict-free plan with the optimal `objective` value with recursive M* and
/// operator decomposition ([`MStarConfig::od_recursive`]).
///
/// See [`mstar_with`] for the budget and what is counted.
///
/// # Examples
///
/// ```
/// use mapf_search::{Agent, Grid, Instance, Limits, MStarConfig, Objective, Outcome, mstar};
///
/// // Two agents swap the ends of a corridor with one side pocket.
/// let grid = Grid::parse(&["...", "@.@"]);
/// let agents = vec![
/// Agent { start: grid.vertex(0, 0), goal: grid.vertex(2, 0) },
/// Agent { start: grid.vertex(2, 0), goal: grid.vertex(0, 0) },
/// ];
/// let instance = Instance::new(grid.clone(), agents)?;
/// let result = mstar(&instance, Objective::SumOfCosts, Limits::NONE);
/// assert_eq!(MStarConfig::default(), MStarConfig::od_recursive(Objective::SumOfCosts));
/// assert_eq!(result.solution().expect("solvable").sum_of_costs, 7);
///
/// // Without the pocket they cannot pass, and M* proves it.
/// let corridor = mapf_search::Grid::parse(&["..."]);
/// let agents = vec![Agent { start: 0, goal: 2 }, Agent { start: 2, goal: 0 }];
/// let instance = Instance::new(corridor, agents)?;
/// let result = mstar(&instance, Objective::Makespan, Limits::NONE);
/// assert_eq!(result.outcome, Outcome::Unsolvable);
/// # Ok::<(), mapf_search::InstanceError>(())
/// ```
/// Finds a conflict-free plan with the optimal `config.objective` value with the form of M*
/// that `config` selects.
///
/// The joint space is finite, so M* proves unsolvability by exhausting it
/// ([`Outcome::Unsolvable`]), also where CBS and ICTS only stop at their budget, such as
/// two agents swapping the ends of a corridor. `budget` is asked before every expansion, in
/// the top-level search and in every subgroup search of recursive M*;
/// [`MapfResult::stats`] counts expansions (re-expansions included) in
/// [`Stats::expanded`], generated joint states in [`Stats::generated`] and expansions after
/// a collision set grew in [`Stats::reopened`], all summed over every search.
///
/// For [`Objective::Makespan`] the plan has the optimal makespan and, among such plans, the
/// least sum of costs; the counters sum over the searches of the module docs. On the
/// benchmark's 8-agent instance recursive M* takes about 30-60 ms for either objective.
///
/// # Examples
///
/// ```
/// use mapf_search::{Agent, Grid, Instance, Limits, MStarConfig, Objective, mstar_with};
///
/// // Two pairs of agents swap places in two rooms, one sooner than the other.
/// let grid = Grid::parse(&["...@.....", "@.@@@@@.@"]);
/// let at = |x, y| grid.vertex(x, y);
/// let agents = vec![
/// Agent { start: at(0, 0), goal: at(2, 0) },
/// Agent { start: at(2, 0), goal: at(0, 0) },
/// Agent { start: at(4, 0), goal: at(8, 0) },
/// Agent { start: at(8, 0), goal: at(4, 0) },
/// ];
/// let instance = Instance::new(grid, agents)?;
///
/// let soc = Objective::SumOfCosts;
/// let basic = mstar_with(&instance, MStarConfig::basic(soc), Limits::NONE);
/// let recursive = mstar_with(&instance, MStarConfig::recursive(soc), Limits::NONE);
/// let cost = |result: &mapf_search::MapfResult| result.solution().map(|s| s.sum_of_costs);
/// assert_eq!(cost(&basic), Some(18));
/// assert_eq!(cost(&recursive), Some(18));
/// assert!(recursive.stats.expanded < basic.stats.expanded);
/// # Ok::<(), mapf_search::InstanceError>(())
/// ```
/// One [`mstar_with`] call: what its searches share, and the counters over all of them.
/// Each agent's path along the standard `states`, trimmed of trailing waits.
/// Each agent's individual optimal policy: from every vertex, the smallest-numbered
/// neighbour one step closer to its goal.