mapf-search 0.1.0

Multi-agent pathfinding on grids and graphs: CBS with its improvements, ECBS, ICTS, PBS, M* and multi-objective MO-CBS
Documentation
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
//! Constraints on one agent ([`Constraint`]), negative (forbid a vertex or move) and
//! positive (require one, for disjoint splitting), and the per-agent lookup structure the
//! low-level search uses ([`ConstraintSet`]).
//!
//! A positive constraint on one agent implies negative ones on every other agent: if `a`
//! must be at `v` at `t`, nobody else may be. [`ConstraintSet::for_agent`] adds those.

use alloc::collections::{BTreeMap, BTreeSet};
use alloc::vec::Vec;

use crate::conflict::Conflict;
use crate::graph::Vertex;
use crate::instance::AgentId;
use crate::path::{Time, position_at};

/// Forbids or requires one agent one vertex or one move at one time.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Constraint {
    /// `agent` must not occupy `vertex` at `time`.
    Vertex {
        /// The constrained agent.
        agent: AgentId,
        /// The forbidden vertex.
        vertex: Vertex,
        /// The time step at which it is forbidden.
        time: Time,
    },
    /// `agent` must not move `from -> to` between `time` and `time + 1`.
    Edge {
        /// The constrained agent.
        agent: AgentId,
        /// The vertex the forbidden move leaves.
        from: Vertex,
        /// The vertex the forbidden move enters.
        to: Vertex,
        /// The time step at which the forbidden move starts.
        time: Time,
    },
    /// `agent` must occupy `vertex` at `time`; no other agent may (disjoint splitting, Li
    /// et al. 2019).
    PositiveVertex {
        /// The constrained agent.
        agent: AgentId,
        /// The required vertex.
        vertex: Vertex,
        /// The time step at which it is required.
        time: Time,
    },
    /// `agent` must move `from -> to` between `time` and `time + 1`, with `from != to`; no
    /// other agent may be at `from` at `time` or at `to` at `time + 1`, or move `to -> from`
    /// then.
    PositiveEdge {
        /// The constrained agent.
        agent: AgentId,
        /// The vertex the required move leaves.
        from: Vertex,
        /// The vertex the required move enters.
        to: Vertex,
        /// The time step at which the required move starts.
        time: Time,
    },
}

impl Constraint {
    /// The two constraints that resolve `conflict`, one per agent.
    pub(crate) fn resolving(conflict: Conflict) -> [Self; 2] {
        match conflict {
            Conflict::Vertex {
                agents: (a, b),
                vertex,
                time,
            } => [a, b].map(|agent| Self::Vertex {
                agent,
                vertex,
                time,
            }),
            Conflict::Edge {
                agents: (a, b),
                from,
                to,
                time,
            } => [
                Self::Edge {
                    agent: a,
                    from,
                    to,
                    time,
                },
                Self::Edge {
                    agent: b,
                    from: to,
                    to: from,
                    time,
                },
            ],
        }
    }

    /// The two children of disjoint splitting (Li et al. 2019) for `conflict`, split on
    /// the agent at `side` (`0` for the conflict's first agent, else its second): the
    /// negative constraint on it, then the positive one.
    pub(crate) fn disjoint(conflict: Conflict, side: usize) -> [Self; 2] {
        let negative = Self::resolving(conflict)[usize::from(side != 0)];

        [negative, negative.positive()]
    }

    /// The positive counterpart of a negative constraint; positive ones stay as they are.
    fn positive(self) -> Self {
        match self {
            Self::Vertex {
                agent,
                vertex,
                time,
            } => Self::PositiveVertex {
                agent,
                vertex,
                time,
            },
            Self::Edge {
                agent,
                from,
                to,
                time,
            } => Self::PositiveEdge {
                agent,
                from,
                to,
                time,
            },
            positive => positive,
        }
    }

    /// The constrained agent.
    #[must_use]
    pub fn agent(&self) -> AgentId {
        match *self {
            Self::Vertex { agent, .. }
            | Self::Edge { agent, .. }
            | Self::PositiveVertex { agent, .. }
            | Self::PositiveEdge { agent, .. } => agent,
        }
    }

    /// Returns `true` for [`Constraint::PositiveVertex`] and [`Constraint::PositiveEdge`].
    #[must_use]
    pub fn is_positive(&self) -> bool {
        matches!(
            self,
            Self::PositiveVertex { .. } | Self::PositiveEdge { .. }
        )
    }

    /// The negative constraints that this positive constraint implies for `other`, an
    /// agent other than the constrained one; nothing for a negative constraint.
    #[must_use]
    pub fn implied_for(&self, other: AgentId) -> Vec<Self> {
        match *self {
            Self::PositiveVertex { vertex, time, .. } => Vec::from([Self::Vertex {
                agent: other,
                vertex,
                time,
            }]),
            Self::PositiveEdge { from, to, time, .. } => Vec::from([
                Self::Vertex {
                    agent: other,
                    vertex: from,
                    time,
                },
                Self::Vertex {
                    agent: other,
                    vertex: to,
                    time: time.saturating_add(1),
                },
                Self::Edge {
                    agent: other,
                    from: to,
                    to: from,
                    time,
                },
            ]),
            Self::Vertex { .. } | Self::Edge { .. } => Vec::new(),
        }
    }

    /// Returns `true` if `path` (with goal-stay) breaks this constraint, whichever agent it
    /// names.
    #[must_use]
    pub fn is_violated_by(&self, path: &[Vertex]) -> bool {
        let at = |time: Time| position_at(path, time);

        match *self {
            Self::Vertex { vertex, time, .. } => at(time) == vertex,
            Self::Edge { from, to, time, .. } => at(time) == from && at(time + 1) == to,
            Self::PositiveVertex { vertex, time, .. } => at(time) != vertex,
            Self::PositiveEdge { from, to, time, .. } => at(time) != from || at(time + 1) != to,
        }
    }
}

/// The constraints of one agent, indexed for the checks of a space-time search.
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ConstraintSet {
    /// Forbidden `(time, vertex)` pairs.
    vertices: BTreeSet<(Time, Vertex)>,
    /// Forbidden `(time, from, to)` moves.
    edges: BTreeSet<(Time, Vertex, Vertex)>,
    /// Required `(time, vertex)` pairs: at such a time, every other vertex is forbidden.
    /// Two different vertices at one time make the set unsatisfiable.
    required: BTreeSet<(Time, Vertex)>,
    /// Vertices forbidden from a time on, forever: where another agent stays for good
    /// ([`ConstraintSet::block_from`]). The earliest such time per vertex.
    blocked_from: BTreeMap<Vertex, Time>,
    /// The latest time step any constraint touches; an edge constraint at `t` touches
    /// `t + 1`, a permanent block touches its first time. `0` while empty.
    latest_step: Time,
}

impl ConstraintSet {
    /// Creates an empty set.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// The set of what `constraints` mean for `agent`: those that name it, and the
    /// negative constraints implied by the positive ones on other agents
    /// ([`Constraint::implied_for`]).
    ///
    /// # Examples
    ///
    /// ```
    /// use mapf_search::{Constraint, ConstraintSet};
    ///
    /// // Agent 0 must be at vertex 4 at time 2, so agent 1 must not.
    /// let positive = Constraint::PositiveVertex { agent: 0, vertex: 4, time: 2 };
    /// let first = ConstraintSet::for_agent(0, &[positive]);
    /// let second = ConstraintSet::for_agent(1, &[positive]);
    ///
    /// assert!(!first.allows_vertex(3, 2));
    /// assert!(first.allows_vertex(4, 2));
    /// assert!(!second.allows_vertex(4, 2));
    /// assert!(second.allows_vertex(3, 2));
    /// ```
    #[must_use]
    pub fn for_agent(agent: AgentId, constraints: &[Constraint]) -> Self {
        let mut set = Self::new();
        for constraint in constraints {
            if constraint.agent() == agent {
                set.insert(constraint);
            } else {
                constraint
                    .implied_for(agent)
                    .iter()
                    .for_each(|implied| set.insert(implied));
            }
        }

        set
    }

    /// Adds `constraint`, ignoring which agent it names.
    pub fn insert(&mut self, constraint: &Constraint) {
        match *constraint {
            Constraint::Vertex { vertex, time, .. } => {
                self.vertices.insert((time, vertex));
                self.latest_step = self.latest_step.max(time);
            }
            Constraint::Edge { from, to, time, .. } => {
                self.edges.insert((time, from, to));
                self.latest_step = self.latest_step.max(time.saturating_add(1));
            }
            Constraint::PositiveVertex { vertex, time, .. } => self.require(vertex, time),
            Constraint::PositiveEdge { from, to, time, .. } => {
                self.require(from, time);
                self.require(to, time.saturating_add(1));
            }
        }
    }

    /// Requires `vertex` at `time`.
    fn require(&mut self, vertex: Vertex, time: Time) {
        self.required.insert((time, vertex));
        self.latest_step = self.latest_step.max(time);
    }

    /// The required `(time, vertex)` pairs ([`Constraint::PositiveVertex`] and both ends of
    /// a [`Constraint::PositiveEdge`]), sorted by time.
    pub(crate) fn landmarks(&self) -> impl Iterator<Item = (Time, Vertex)> + '_ {
        self.required.iter().copied()
    }

    /// Forbids `vertex` at `time` and at every later time, as for a vertex where another
    /// agent stays for good after arriving at `time`.
    ///
    /// # Examples
    ///
    /// ```
    /// use mapf_search::ConstraintSet;
    ///
    /// let mut constraints = ConstraintSet::new();
    /// constraints.block_from(4, 2);
    ///
    /// assert!(constraints.allows_vertex(4, 1));
    /// assert!(!constraints.allows_vertex(4, 9));
    /// assert_eq!(constraints.blocked_until(4), Some(u32::MAX));
    /// ```
    pub fn block_from(&mut self, vertex: Vertex, time: Time) {
        let from = self.blocked_from.entry(vertex).or_insert(time);
        *from = (*from).min(time);
        self.latest_step = self.latest_step.max(time);
    }

    /// Returns `true` if the agent may move (or wait, when `from == to`) from `from` at
    /// `time` to `to` at `time + 1`.
    #[must_use]
    pub fn allows(&self, from: Vertex, to: Vertex, time: Time) -> bool {
        let next = time.saturating_add(1);

        self.allows_vertex(to, next) && !self.edges.contains(&(time, from, to))
    }

    /// Returns `true` if the agent may occupy `vertex` at `time`.
    #[must_use]
    pub fn allows_vertex(&self, vertex: Vertex, time: Time) -> bool {
        let blocked = self
            .blocked_from
            .get(&vertex)
            .is_some_and(|&from| from <= time);

        let elsewhere = self
            .required
            .range((time, 0)..=(time, Vertex::MAX))
            .any(|&(_, required)| required != vertex);

        !blocked && !elsewhere && !self.vertices.contains(&(time, vertex))
    }

    /// The latest time step any constraint touches; `0` while empty.
    ///
    /// After this step the constraints no longer change: the agent is free except for the
    /// vertices blocked forever ([`ConstraintSet::block_from`]).
    #[must_use]
    pub fn latest_step(&self) -> Time {
        self.latest_step
    }

    /// The latest time `vertex` is forbidden, if ever: by a negative constraint, or by a
    /// positive one that requires another vertex then.
    ///
    /// For the goal vertex, an agent must not finish at or before this time: goal-stay
    /// semantics would put it there. [`Time::MAX`] if the vertex is blocked forever
    /// ([`ConstraintSet::block_from`]).
    #[must_use]
    pub fn blocked_until(&self, vertex: Vertex) -> Option<Time> {
        if self.blocked_from.contains_key(&vertex) {
            return Some(Time::MAX);
        }

        let forbidden = self
            .vertices
            .iter()
            .rev()
            .find(|&&(_, v)| v == vertex)
            .map(|&(time, _)| time);
        let elsewhere = self
            .required
            .iter()
            .rev()
            .find(|&&(_, v)| v != vertex)
            .map(|&(time, _)| time);

        forbidden.max(elsewhere)
    }
}

#[cfg(test)]
mod tests {
    //! Unit tests for positive constraints.

    use super::*;
    use alloc::vec;

    /// A positive edge requires both ends and implies three negatives for the others.
    #[test]
    fn positive_edges_require_both_ends() {
        let positive = Constraint::PositiveEdge {
            agent: 0,
            from: 1,
            to: 2,
            time: 3,
        };
        let own = ConstraintSet::for_agent(0, &[positive]);

        assert!(own.allows(1, 2, 3));
        assert!(!own.allows(1, 1, 3), "must leave 1");
        assert!(!own.allows_vertex(0, 3));
        assert_eq!(own.latest_step(), 4);
        assert_eq!(own.blocked_until(2), Some(3), "elsewhere at 3");
        assert_eq!(own.blocked_until(9), Some(4));

        let other = ConstraintSet::for_agent(1, &[positive]);
        assert!(!other.allows_vertex(1, 3));
        assert!(!other.allows_vertex(2, 4));
        assert!(!other.allows(2, 1, 3));
        assert!(other.allows(0, 0, 3));
    }

    /// Two different required vertices at one time forbid everything then.
    #[test]
    fn conflicting_requirements_forbid_everything() {
        let at = |vertex| Constraint::PositiveVertex {
            agent: 0,
            vertex,
            time: 2,
        };
        let set = ConstraintSet::for_agent(0, &[at(1), at(2)]);

        assert!((0..4).all(|vertex| !set.allows_vertex(vertex, 2)));
    }

    /// Violation checks follow goal-stay, for negative and positive constraints alike.
    #[test]
    fn violations_follow_goal_stay() {
        let path = vec![0, 1, 2];
        let vertex = |vertex, time| Constraint::Vertex {
            agent: 0,
            vertex,
            time,
        };

        assert!(vertex(2, 7).is_violated_by(&path));
        assert!(!vertex(1, 7).is_violated_by(&path));
        assert!(vertex(1, 7).positive().is_violated_by(&path));
        assert!(!vertex(2, 7).positive().is_violated_by(&path));

        let swap = Constraint::Edge {
            agent: 0,
            from: 1,
            to: 2,
            time: 1,
        };
        assert!(swap.is_violated_by(&path));
        assert!(!swap.positive().is_violated_by(&path));
    }

    /// Disjoint splitting yields the negative and the positive constraint on one agent.
    #[test]
    fn disjoint_split_constrains_one_agent() {
        let conflict = Conflict::Edge {
            agents: (3, 5),
            from: 1,
            to: 2,
            time: 4,
        };
        let [negative, positive] = Constraint::disjoint(conflict, 1);

        assert_eq!(
            negative,
            Constraint::Edge {
                agent: 5,
                from: 2,
                to: 1,
                time: 4
            }
        );
        assert_eq!(positive, negative.positive());
        assert!(positive.is_positive() && !negative.is_positive());
        assert_eq!(negative.implied_for(3), []);
    }
}