-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_agent_planner.py
282 lines (234 loc) · 10.2 KB
/
single_agent_planner.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
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
import heapq
def move(loc, dir):
directions = [(0, -1), (1, 0), (0, 1), (-1, 0), (0, 0)]
return loc[0] + directions[dir][0], loc[1] + directions[dir][1]
def get_sum_of_cost(paths, goals,starts):
rst = 0
i = 0
for path in paths:
print(path)
t = 1
first_goal_index = 0
j = 0
for p in path:
if p == goals[i]:
first_goal_index = j
break
j +=1
for loc in path:
# print(loc)
# print(goals[i])
if loc != goals[i] and t <= first_goal_index:
rst +=1
t +=1
i +=1
# rst += len(path) - 1
return rst
# rst = 0
# for path in paths:
# rst += len(path) - 1
# print(rst)
# return rst
def compute_heuristics(my_map, goal):
# Use Dijkstra to build a shortest-path tree rooted at the goal location
open_list = []
closed_list = dict()
root = {'loc': goal, 'cost': 0}
heapq.heappush(open_list, (root['cost'], goal, root))
closed_list[goal] = root
while len(open_list) > 0:
(cost, loc, curr) = heapq.heappop(open_list)
for dir in range(4):
child_loc = move(loc, dir)
child_cost = cost + 1
if child_loc[0] < 0 or child_loc[0] >= len(my_map) \
or child_loc[1] < 0 or child_loc[1] >= len(my_map[0]):
continue
if my_map[child_loc[0]][child_loc[1]]:
continue
child = {'loc': child_loc, 'cost': child_cost}
if child_loc in closed_list:
existing_node = closed_list[child_loc]
if existing_node['cost'] > child_cost:
closed_list[child_loc] = child
# open_list.delete((existing_node['cost'], existing_node['loc'], existing_node))
heapq.heappush(open_list, (child_cost, child_loc, child))
else:
closed_list[child_loc] = child
heapq.heappush(open_list, (child_cost, child_loc, child))
# build the heuristics table
h_values = dict()
for loc, node in closed_list.items():
h_values[loc] = node['cost']
return h_values
"""build_constraint_table: gets constraints list and agent and returns a dict with constraints for the given agent
with time steps as keys """
def build_constraint_table(constraints, agent):
constraints_table_for_agent = {}
list_constraints_for_agent = []
for constraint in constraints:
if constraint['agent'] == agent:
list_constraints_for_agent.append(constraint)
for constraint in list_constraints_for_agent:
if constraint['timestep'] in constraints_table_for_agent:
# print(constraint)
new_list_constraints = constraints_table_for_agent[constraint['timestep']]
new_list_constraints.append(constraint)
constraints_table_for_agent[constraint['timestep']] = new_list_constraints
# print(constraints_table_for_agent[constraint['timestep']])
else:
constraints_table_for_agent[constraint['timestep']] = [constraint]
# print(constraints_table_for_agent)
return constraints_table_for_agent
##############################
# Task 1.2/1.3: Return a table that contains the list of constraints of
# the given agent for each time step. The table can be used
# for a more efficient constraint violation check in the
# is_constrained function.
def get_location(path, time):
if time < 0:
return path[0]
elif time < len(path):
return path[time]
else:
return path[-1] # wait at the goal location
def get_path(goal_node):
path = []
curr = goal_node
while curr is not None:
path.append(curr['loc'])
curr = curr['parent']
path.reverse()
return path
""" checks if the loc is within map boundries """
def not_within_map(my_map, loc):
if loc[0] < 0 or loc[1] < 0:
return True
num_of_rows = len(my_map)
num_of_col = len(my_map[0])
if loc[0] > num_of_rows - 1 or loc[1] > num_of_col - 1:
return True
return False
""" checks if the constraint is a positive one or negative one"""
def is_constrained_positive(curr_loc, next_loc, next_time, constraint_table):
if next_time in constraint_table:
for constraint in constraint_table[next_time]:
if len(constraint['loc']) == 1:
if next_loc == constraint['loc'][0]:
if constraint['positive']:
return True
if len(constraint['loc']) == 2:
if curr_loc == constraint['loc'][0] and next_loc == constraint['loc'][1]:
if constraint['positive']:
return True
return False
""" checks if there is a negative constraint on next location """
def is_constrained_negative(curr_loc, next_loc, next_time, constraint_table):
minus_next_time = -next_time
if next_time in constraint_table:
for constraint in constraint_table[next_time]:
if len(constraint['loc']) == 1:
if next_loc == constraint['loc'][0]:
if not constraint['positive']:
return True
if len(constraint['loc']) == 2:
if curr_loc == constraint['loc'][0] and next_loc == constraint['loc'][1]:
if not constraint['positive']:
return True
# if other agent in its goal
for key in constraint_table:
if 0 > key >= minus_next_time:
for constraint in constraint_table[key]:
if len(constraint['loc']) == 1:
if constraint['loc'][0] == next_loc:
if not constraint['positive']:
return True
##############################
# Task 1.2/1.3: Check if a move from curr_loc to next_loc at time step next_time violates
# any given constraint. For efficiency the constraints are indexed in a constraint_table
# by time step, see build_constraint_table.
return False
"""checks if there is a goal constraint for the agent's goal location """
def goal_constraint(next_loc, goal, next_time, table_of_constraints):
for key in table_of_constraints:
if key == next_time:
for constraint in table_of_constraints[key]:
if len(constraint['loc']) == 1:
if constraint['loc'][0] == goal and next_loc == goal:
if not constraint['positive']:
return True
return False
def goal_constraint_later_time(next_loc, goal, next_time, table_of_constraints):
for key in table_of_constraints:
if key >= next_time:
for constraint in table_of_constraints[key]:
if len(constraint['loc']) == 1:
if constraint['loc'][0] == goal and next_loc == goal:
if not constraint['positive']:
return True
if len(constraint['loc']) == 2:
if constraint['loc'][1] == goal or constraint['loc'][0] == goal:
if not constraint['positive']:
return True
return False
def push_node(open_list, node):
heapq.heappush(open_list, (node['g_val'] + node['h_val'], node['h_val'], node['loc'], node))
def pop_node(open_list):
_, _, _, curr = heapq.heappop(open_list)
return curr
def compare_nodes(n1, n2):
"""Return true is n1 is better than n2."""
return n1['g_val'] + n1['h_val'] < n2['g_val'] + n2['h_val']
def a_star(my_map, start_loc, goal_loc, h_values, agent, constraints):
""" my_map - binary obstacle map
start_loc - start position
goal_loc - goal position
agent - the agent that is being re-planned
constraints - constraints defining where robot should or cannot go at each timestep
"""
##############################
# Task 1.1: Extend the A* search to search in the space-time domain
# rather than space domain, only.
open_list = []
closed_list = dict()
earliest_goal_timestep = 0
h_value = h_values[start_loc]
constraints_for_agent = build_constraint_table(constraints, agent)
root = {'loc': start_loc, 'g_val': 0, 'h_val': h_value, 'parent': None, 'timestep': 0}
push_node(open_list, root)
closed_list[(root['loc'], root['timestep'])] = root
while len(open_list) > 0:
curr = pop_node(open_list)
#############################
# Task 1.4: Adjust the goal test condition to handle goal constraints
if curr['loc'] == goal_loc:
if not goal_constraint_later_time(curr['loc'],goal_loc,curr['timestep'],constraints_for_agent):
return get_path(curr)
# we have 5 direction , left, right, up, down and stay in place
for dir in range(5):
child_loc = move(curr['loc'], dir)
# constraint for checking map boundaries
if not_within_map(my_map, child_loc):
continue
if my_map[child_loc[0]][child_loc[1]]:
continue
# checking if we have a vertex or edge or future goal constraint
if is_constrained_negative(curr['loc'], child_loc, curr['timestep'] + 1,
constraints_for_agent):
continue
if goal_constraint(child_loc,goal_loc,curr['timestep']+1,constraints_for_agent):
continue
child = {'loc': child_loc,
'g_val': curr['g_val'] + 1,
'h_val': h_values[child_loc],
'parent': curr,
'timestep': curr['timestep'] + 1}
if (child['loc'], child['timestep']) in closed_list:
existing_node = closed_list[(child['loc'], child['timestep'])]
if compare_nodes(child, existing_node):
closed_list[(child['loc'], child['timestep'])] = child
push_node(open_list, child)
else:
closed_list[(child['loc']), child['timestep']] = child
push_node(open_list, child)
return None # Failed to find solutions