-
Notifications
You must be signed in to change notification settings - Fork 2
/
cbs.py
243 lines (212 loc) · 10.3 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
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
import random
import time as timer
import heapq
from single_agent_planner import compute_heuristics, a_star, get_location, get_sum_of_cost
DEBUG = False
def normalize_paths(pathA, pathB):
"""
given path1 and path2, finds the shortest path and pads it with the last location
"""
path1 = pathA.copy()
path2 = pathB.copy()
shortest, pad = (path1, len(path2) - len(path1)) if len(path1) < len(path2) else (path2, len(path1) - len(path2))
for _ in range(pad):
shortest.append(shortest[-1])
return path1, path2
def detect_collision(pathA, pathB):
##############################
# Task 3.1: Return the first collision that occurs between two robot paths (or None if there is no collision)
# There are two types of collisions: vertex collision and edge collision.
# A vertex collision occurs if both robots occupy the same location at the same timestep
# An edge collision occurs if the robots swap their location at the same timestep.
# You should use "get_location(path, t)" to get the location of a robot at time t.
# this function detects if an agent collides with another even after one of the two reached the goal
path1, path2 = normalize_paths(pathA, pathB)
length = len(path1)
for t in range(length):
# check for vertex collision
pos1 = get_location(path1, t)
pos2 = get_location(path2, t)
if pos1 == pos2:
# we return the vertex and the timestep causing the collision
return [pos1], t, 'vertex'
# check for edge collision (not if we are in the last timestep)
if t < length - 1:
next_pos1 = get_location(path1, t + 1)
next_pos2 = get_location(path2, t + 1)
if pos1 == next_pos2 and pos2 == next_pos1:
# we return the edge and timestep causing the collision
return [pos1, next_pos1], t + 1, 'edge'
return None
def detect_collisions(paths):
##############################
# Task 3.1: Return a list of first collisions between all robot pairs.
# A collision can be represented as dictionary that contains the id of the two robots, the vertex or edge
# causing the collision, and the timestep at which the collision occurred.
# You should use your detect_collision function to find a collision between two robots.
collisions = []
for i in range(len(paths)):
for j in range(i + 1, len(paths)):
coll_data = detect_collision(paths[i], paths[j])
# if coll_data is not None (collision detected)
if coll_data:
collisions.append({
'a1': i,
'a2': j,
'loc': coll_data[0], # vertex or edge
'timestep': coll_data[1], # timestep
'type': coll_data[2]
})
return collisions
def standard_splitting(collision):
##############################
# Task 3.2: Return a list of (two) constraints to resolve the given collision
# Vertex collision: the first constraint prevents the first agent to be at the specified location at the
# specified timestep, and the second constraint prevents the second agent to be at the
# specified location at the specified timestep.
# Edge collision: the first constraint prevents the first agent to traverse the specified edge at the
# specified timestep, and the second constraint prevents the second agent to traverse the
# specified edge at the specified timestep
# in this case, we can ignore final as all the paths are normalized
constraints = []
if collision['type'] == 'vertex':
constraints.append({
'agent': collision['a1'],
'loc': collision['loc'],
'timestep': collision['timestep'],
'final': False
})
constraints.append({
'agent': collision['a2'],
'loc': collision['loc'],
'timestep': collision['timestep'],
'final': False
})
elif collision['type'] == 'edge':
constraints.append({
'agent': collision['a1'],
'loc': collision['loc'],
'timestep': collision['timestep'],
'final': False
})
constraints.append({
'agent': collision['a2'],
# revesred returns an iterator. In python list == iterator returns false, not an error: nasty bug
'loc': list(reversed(collision['loc'])),
'timestep': collision['timestep'],
'final': False
})
return constraints
def disjoint_splitting(collision):
##############################
# Task 4.1: Return a list of (two) constraints to resolve the given collision
# Vertex collision: the first constraint enforces one agent to be at the specified location at the
# specified timestep, and the second constraint prevents the same agent to be at the
# same location at the timestep.
# Edge collision: the first constraint enforces one agent to traverse the specified edge at the
# specified timestep, and the second constraint prevents the same agent to traverse the
# specified edge at the specified timestep
# Choose the agent randomly
pass
class CBSSolver(object):
"""The high-level search of CBS."""
def __init__(self, my_map, starts, goals):
"""my_map - list of lists specifying obstacle positions
starts - [(x1, y1), (x2, y2), ...] list of start locations
goals - [(x1, y1), (x2, y2), ...] list of goal locations
"""
self.start_time = 0
self.my_map = my_map
self.starts = starts
self.goals = goals
self.num_of_agents = len(goals)
self.num_of_generated = 0
self.num_of_expanded = 0
self.CPU_time = 0
self.open_list = []
# compute heuristics for the low-level search
self.heuristics = []
for goal in self.goals:
self.heuristics.append(compute_heuristics(my_map, goal))
def push_node(self, node):
heapq.heappush(self.open_list, (node['cost'], len(node['collisions']), self.num_of_generated, node))
if DEBUG:
print("Generate node {}".format(self.num_of_generated))
self.num_of_generated += 1
def pop_node(self):
_, _, id, node = heapq.heappop(self.open_list)
if DEBUG:
print("Expand node {}".format(id))
self.num_of_expanded += 1
return node
def find_solution(self, disjoint=True):
""" Finds paths for all agents from their start locations to their goal locations
disjoint - use disjoint splitting or not
"""
self.start_time = timer.time()
# Generate the root node
# constraints - list of constraints
# paths - list of paths, one for each agent
# [[(x11, y11), (x12, y12), ...], [(x21, y21), (x22, y22), ...], ...]
# collisions - list of collisions in paths
root = {'cost': 0,
'constraints': [],
'paths': [],
'collisions': []}
for i in range(self.num_of_agents): # Find initial path for each agent
path = a_star(self.my_map, self.starts[i], self.goals[i], self.heuristics[i],
i, root['constraints'])
if path is None:
raise BaseException('No solutions')
root['paths'].append(path)
root['cost'] = get_sum_of_cost(root['paths'])
root['collisions'] = detect_collisions(root['paths'])
self.push_node(root)
# Task 3.1: Testing
if DEBUG:
print(root['collisions'])
# Task 3.2: Testing
if DEBUG:
for collision in root['collisions']:
print(standard_splitting(collision))
##############################
# Task 3.3: High-Level Search
# Repeat the following as long as the open list is not empty:
# 1. Get the next node from the open list (you can use self.pop_node())
# 2. If this node has no collision, return solution
# 3. Otherwise, choose the first collision and convert to a list of constraints (using your
# standard_splitting function). Add a new child node to your open list for each constraint
# Ensure to create a copy of any objects that your child nodes might inherit
while self.open_list:
p = self.pop_node()
# if there are no collisions, we found a solution
if not p['collisions']:
self.print_results(p)
return p['paths']
else:
# we choose a collision and turn it into constraints
collision = random.choice(p['collisions'])
constraints = standard_splitting(collision)
for c in constraints:
q = {'cost': 0,
'constraints': [*p['constraints'], c], # all constraints in p plus c
'paths': p['paths'].copy(),
'collisions': []}
agent = c['agent']
path = a_star(self.my_map, self.starts[agent], self.goals[agent], self.heuristics[agent],
agent, q['constraints'])
# if path not empty
if path:
q['paths'][agent] = path
q['collisions'] = detect_collisions(q['paths'])
q['cost'] = get_sum_of_cost(q['paths'])
self.push_node(q)
else:
raise BaseException('No solutions')
def print_results(self, node):
print("\n Found a solution! \n")
CPU_time = timer.time() - self.start_time
print("CPU time (s): {:.2f}".format(CPU_time))
print("Sum of costs: {}".format(get_sum_of_cost(node['paths'])))
print("Expanded nodes: {}".format(self.num_of_expanded))
print("Generated nodes: {}".format(self.num_of_generated))