-
Notifications
You must be signed in to change notification settings - Fork 7
/
cbs.py
217 lines (176 loc) · 7.85 KB
/
cbs.py
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
import numpy as np
import time
import matplotlib.pyplot as plt
import random
from heapq import heappop, heappush
from MAPF import Map
from MAPF import read_map_from_movingai_file, read_tasks_from_movingai_file
from CT import HighNode, LowNode
from OpenClosed import OpenHigh, OpenLow, ClosedLow
from Heuristics import ManhattanDistance
from CT import MakePath
from AstarTimeSteps import AstarTimesteps
def CBS(gridMap, Starts, Goals, subset=[], vertexCons=[], edgeCons=[]): # subset, vertex/edge- Cons - нужны только при вызове CBS в качестве нижнего уровня MACBS
tic = time.perf_counter() # начало работы функции
gen = 0
exp = 0
root = HighNode(vertexCons={}, edgeCons={}, sol={}, k=gen)
OPEN = OpenHigh()
agents = list(range(len(Starts)))
if len(subset) > 0:
agents = subset.copy()
for vc in vertexCons:
# перебираем агентов из подмножества
for a in vc[0]:
if a in root.vertexCons:
root.vertexCons[a].append((vc[1], vc[2]))
else:
root.vertexCons[a] = [(vc[1], vc[2])]
for ec in edgeCons:
# перебираем агентов из подмножества
for a in ec[0]:
if a in root.edgeCons:
root.edgeCons[a].append((ec[1], ec[2], ec[3]))
else:
root.edgeCons[a] = [(ec[1], ec[2], ec[3])]
for a in agents:
VC = []
EC = []
if a in root.vertexCons:
VC = root.vertexCons[a]
if a in root.edgeCons:
EC = root.edgeCons[a]
planner = AstarTimesteps(gridMap, Starts[a][0], Starts[a][1], Goals[a][0], Goals[a][1], VC, EC)
res = planner.FindPath()
if res[0]:
path = MakePath(res[1])
root.sol[a], _ = path
else:
return (False, None, gen, exp)
root.g = sum([len(path) for path in root.sol.values()])
OPEN.AddNode(root)
gen += 1
toc = time.perf_counter()
while toc - tic < 120:
s = OPEN.GetBestNode()
exp += 1
newVertexCons = []
newEdgeCons = []
for i, a in enumerate(agents):
for b in agents[i + 1 :]:
for step in range(min(len(s.sol[a]), len(s.sol[b]))):
if s.sol[a][step].i == s.sol[b][step].i and s.sol[a][step].j == s.sol[b][step].j:
newVertexCons.append((a, b, (s.sol[a][step].i, s.sol[a][step].j), step))
if step + 1 < min(len(s.sol[a]), len(s.sol[b])) and \
s.sol[a][step].i == s.sol[b][step + 1].i and s.sol[a][step].j == s.sol[b][step + 1].j and \
s.sol[a][step + 1].i == s.sol[b][step].i and s.sol[a][step + 1].i == s.sol[b][step].i:
newEdgeCons.append((
a,
b,
(s.sol[a][step].i, s.sol[a][step].j),
(s.sol[a][step + 1].i, s.sol[a][step + 1].j),
step,
))
if len(newVertexCons) == 0 and len(newEdgeCons) == 0:
return (True, s, gen, exp)
# Сейчас сначала разрешаются вершинные конфликты, потом реберные
if len(newVertexCons) > 0:
a, b, (i, j), t = newVertexCons[0]
# Разбиваем CT на ноды A и B, разрешая вершинный конфликт
tmp = s.vertexCons.copy()
if a in tmp:
tmp[a].append(((i, j), t))
else:
tmp[a] = [((i, j), t)]
A = HighNode(
vertexCons=tmp,
edgeCons=s.edgeCons.copy(),
sol=s.sol.copy(),
parent=s,
k=gen,
)
ec = []
if a in A.edgeCons:
ec = A.edgeCons[a]
planner = AstarTimesteps(gridMap, Starts[a][0], Starts[a][1], Goals[a][0], Goals[a][1], A.vertexCons[a], ec)
res = planner.FindPath()
if res[0]:
path = MakePath(res[1])
A.sol[a], _ = path
A.g = sum([len(path) for path in A.sol.values()]) # SIC, можно использовать другой cost; добавить подсчет h, F
OPEN.AddNode(A)
gen += 1
tmp = s.vertexCons.copy()
if b in tmp:
tmp[b].append(((i, j), t))
else:
tmp[b] = [((i, j), t)]
B = HighNode(
vertexCons=tmp,
edgeCons=s.edgeCons.copy(),
sol=s.sol.copy(),
parent=s,
k=gen,
)
ec = []
if b in B.edgeCons:
ec = B.edgeCons[b]
planner = AstarTimesteps(gridMap, Starts[b][0], Starts[b][1], Goals[b][0], Goals[b][1], B.vertexCons[b], ec)
res = planner.FindPath()
if res[0]:
path = MakePath(res[1])
B.sol[b], _ = path
B.g = sum([len(path) for path in B.sol.values()]) # SIC, можно использовать другой cost; добавить подсчет h, F
OPEN.AddNode(B)
gen += 1
elif len(newEdgeCons) > 0:
a, b, (i1, j1), (i2, j2), t = newEdgeCons[0]
# Разбиваем CT на ноды A и B, разрешая реберный конфликт
tmp = s.edgeCons.copy()
if a in tmp:
tmp[a].append(((i1, j1), (i2, j2), t))
else:
tmp[a] = [((i1, j1), (i2, j2), t)]
A = HighNode(
vertexCons=s.vertexCons.copy(),
edgeCons=tmp,
sol=s.sol.copy(),
parent=s,
k=gen,
)
vc = []
if a in A.vertexCons:
vc = A.vertexCons[a]
planner = AstarTimesteps(gridMap, Starts[a][0], Starts[a][1], Goals[a][0], Goals[a][1], vc, A.edgeCons[a])
res = planner.FindPath()
if res[0]:
path = MakePath(res[1])
A.sol[a], _ = path
A.g = sum([len(path) for path in A.sol.values()]) # SIC, можно использовать другой cost; добавить подсчет h, F
OPEN.AddNode(A)
gen += 1
tmp = s.edgeCons.copy()
if b in tmp:
tmp[b].append(((i1, j1), (i2, j2), t))
else:
tmp[b] = [((i1, j1), (i2, j2), t)]
B = HighNode(
vertexCons=s.vertexCons.copy(),
edgeCons=tmp,
sol=s.sol.copy(),
parent=s,
k=gen,
)
vc = []
if b in B.vertexCons:
vc = B.vertexCons[b]
planner = AstarTimesteps(gridMap, Starts[b][0], Starts[b][1], Goals[b][0], Goals[b][1], vc, B.edgeCons[b])
res = planner.FindPath()
if res[0]:
path = MakePath(res[1])
B.sol[b], _ = path
B.g = sum([len(path) for path in B.sol.values()]) # SIC, можно использовать другой cost; добавить подсчет h, F
OPEN.AddNode(B)
gen += 1
toc = time.perf_counter()
return (False, None, gen, exp)