1use alloc::vec::Vec;
8
9use crate::graph::Vertex;
10use crate::instance::AgentId;
11use crate::path::{Time, position_at};
12
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
15pub enum Conflict {
16 Vertex {
18 agents: (AgentId, AgentId),
20 vertex: Vertex,
22 time: Time,
24 },
25 Edge {
28 agents: (AgentId, AgentId),
30 from: Vertex,
32 to: Vertex,
34 time: Time,
36 },
37}
38
39impl Conflict {
40 #[must_use]
42 pub fn agents(&self) -> (AgentId, AgentId) {
43 match *self {
44 Self::Vertex { agents, .. } | Self::Edge { agents, .. } => agents,
45 }
46 }
47
48 #[must_use]
50 pub fn time(&self) -> Time {
51 match *self {
52 Self::Vertex { time, .. } | Self::Edge { time, .. } => time,
53 }
54 }
55}
56
57#[must_use]
66pub fn first_conflict<P: AsRef<[Vertex]>>(paths: &[P]) -> Option<Conflict> {
67 let mut found = None;
68 scan(paths, |conflict| {
69 found = Some(conflict);
70 false
71 });
72
73 found
74}
75
76#[must_use]
85pub fn conflicts<P: AsRef<[Vertex]>>(paths: &[P]) -> Vec<Conflict> {
86 let mut found = Vec::new();
87 scan(paths, |conflict| {
88 found.push(conflict);
89 true
90 });
91
92 found
93}
94
95pub(crate) fn update_conflicts<P: AsRef<[Vertex]>>(
106 found: &[Conflict],
107 paths: &[P],
108 changed: &[AgentId],
109) -> Vec<Conflict> {
110 let paths: Vec<&[Vertex]> = paths.iter().map(AsRef::as_ref).collect();
111 let involved = |conflict: &Conflict| {
112 let (a, b) = conflict.agents();
113 changed.contains(&a) || changed.contains(&b)
114 };
115
116 let mut updated: Vec<Conflict> = found.iter().filter(|c| !involved(c)).copied().collect();
117 for time in 0..horizon(&paths) {
118 for &a in changed {
119 let others = (0..paths.len()).filter(|&b| b != a && !(b < a && changed.contains(&b)));
121 for b in others {
122 let pair = (a.min(b), a.max(b));
123 let found = conflicts_at(paths[pair.0], paths[pair.1], pair, time);
124 updated.extend(found.into_iter().flatten());
125 }
126 }
127 }
128
129 updated.sort_unstable_by_key(scan_order);
130
131 updated
132}
133
134fn scan_order(conflict: &Conflict) -> (Time, (AgentId, AgentId), bool) {
137 (
138 conflict.time(),
139 conflict.agents(),
140 matches!(conflict, Conflict::Edge { .. }),
141 )
142}
143
144fn horizon(paths: &[&[Vertex]]) -> Time {
146 let longest = paths.iter().map(|path| path.len()).max().unwrap_or(0);
147
148 Time::try_from(longest).expect("path lengths fit in u32 time steps")
149}
150
151fn scan<P: AsRef<[Vertex]>>(paths: &[P], mut visit: impl FnMut(Conflict) -> bool) {
153 let paths: Vec<&[Vertex]> = paths.iter().map(AsRef::as_ref).collect();
154
155 for time in 0..horizon(&paths) {
156 if !scan_time(&paths, time, &mut visit) {
157 return;
158 }
159 }
160}
161
162fn scan_time(paths: &[&[Vertex]], time: Time, visit: &mut impl FnMut(Conflict) -> bool) -> bool {
165 for (i, a) in paths.iter().enumerate() {
166 for (j, b) in paths.iter().enumerate().skip(i + 1) {
167 let pair = conflicts_at(a, b, (i, j), time);
168 if pair.into_iter().flatten().any(|conflict| !visit(conflict)) {
169 return false;
170 }
171 }
172 }
173
174 true
175}
176
177fn conflicts_at(
180 a: &[Vertex],
181 b: &[Vertex],
182 agents: (AgentId, AgentId),
183 time: Time,
184) -> [Option<Conflict>; 2] {
185 let (a_now, b_now) = (position_at(a, time), position_at(b, time));
186 let (a_next, b_next) = (position_at(a, time + 1), position_at(b, time + 1));
187
188 let vertex = (a_now == b_now).then_some(Conflict::Vertex {
189 agents,
190 vertex: a_now,
191 time,
192 });
193 let swap = (a_now != a_next && a_now == b_next && a_next == b_now).then_some(Conflict::Edge {
194 agents,
195 from: a_now,
196 to: a_next,
197 time,
198 });
199
200 [vertex, swap]
201}
202
203#[cfg(test)]
204mod tests {
205 use super::*;
208 use alloc::vec;
209
210 #[test]
212 fn update_matches_a_full_scan() {
213 let before = [vec![0, 1, 2], vec![2, 1, 0], vec![5, 1, 6], vec![7, 6, 5]];
214 let after = [
215 vec![0, 1, 2],
216 vec![2, 2, 1, 0],
217 vec![5, 1, 6],
218 vec![6, 6, 5],
219 ];
220 let found = conflicts(&before);
221
222 for changed in [&[][..], &[1], &[3], &[1, 3], &[3, 1], &[0, 1, 2, 3]] {
223 let paths: Vec<_> = (0..4)
224 .map(|agent| {
225 if changed.contains(&agent) {
226 &after[agent]
227 } else {
228 &before[agent]
229 }
230 })
231 .collect();
232
233 assert_eq!(
234 update_conflicts(&found, &paths, changed),
235 conflicts(&paths),
236 "changed {changed:?}"
237 );
238 }
239 }
240}