-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_pathfinding_world.py
More file actions
328 lines (248 loc) · 11.7 KB
/
my_pathfinding_world.py
File metadata and controls
328 lines (248 loc) · 11.7 KB
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
from base import Agent, Action, Perception
from representation import GridRelativeOrientation, GridOrientation, GridPosition
from pathfinding import PathfindingEnvironment, PathfindingAgentData, PathfindingAgent
from enum import Enum
import sys
import time, random
from heapq import heappop, heappush
class MyAction(Action, Enum):
"""
Physical actions for wildlife agents.
"""
# The agent must move north (up)
NORTH = 0
# The agent must move north-east (up-right)
NORTH_EAST = 1
# The agent must move north-west (up-left)
NORTH_WEST = 2
# The agent must move east (right).
EAST = 3
# The agent must move south (down).
SOUTH = 4
# The agent must move south-east (down-right).
SOUTH_EAST = 5
# The agent must move south-west (down-left).
SOUTH_WEST = 6
# The agent must move west (left).
WEST = 7
# The agent must hold its position
WAIT = 8
class MyAgentPerception(Perception):
"""
The perceptions of a wildlife agent.
"""
def __init__(self, agent_position, absolute_orientation, obstacles, goal, moves):
"""
Default constructor
:param agent_position: agents's position.
:param obstacles: visible obstacles
:param messages: incoming messages, may be None
"""
self.agent_position = agent_position
self.absolute_orientation = absolute_orientation
self.obstacles = obstacles
self.goal = goal
self.moves = moves
def print_path(path):
for pos in path:
print(pos)
def process_relative_pos(relative_pos):
if relative_pos == GridRelativeOrientation.FRONT:
return MyAction.NORTH
elif relative_pos == GridRelativeOrientation.FRONT_LEFT:
return MyAction.NORTH_WEST
elif relative_pos == GridRelativeOrientation.FRONT_RIGHT:
return MyAction.NORTH_EAST
elif relative_pos == GridRelativeOrientation.LEFT:
return MyAction.WEST
elif relative_pos == GridRelativeOrientation.RIGHT:
return MyAction.EAST
elif relative_pos == GridRelativeOrientation.BACK:
return MyAction.SOUTH
elif relative_pos == GridRelativeOrientation.BACK_LEFT:
return MyAction.SOUTH_WEST
elif relative_pos == GridRelativeOrientation.BACK_RIGHT:
return MyAction.SOUTH_EAST
def check_diagonal_conflict(pre_move1, post_move1, pre_move2, post_move2):
pre_x = pre_move1.get_x() - pre_move2.get_x()
pre_y = pre_move1.get_y() - pre_move2.get_y()
post_x = post_move1.get_x() - post_move2.get_x()
post_y = post_move1.get_y() - post_move2.get_y()
# only nearby
if abs(pre_x) > 1 or abs(pre_y) > 1 or abs(post_x) > 1 or abs(post_y) > 1:
return False
pre_diff = pre_x + pre_y
post_diff = post_x + post_y
# when parallel diagonals
if pre_diff == 0 and post_diff == 0:
return False
if abs(pre_diff) > 1 or abs(post_diff) > 1:
return False
if pre_diff + post_diff == 0:
return True
else:
return False
def a_star(start, end, absolute_orientation, obstacles, moves, agent_name):
frontier = []
heappush(frontier, (0 + start.get_euclidean_distance_to(end), start))
discovered = {start: (None, 0)}
curr_node = None
expanded_node = False
while frontier:
if expanded_node:
break
(_, curr_node) = heappop(frontier)
if curr_node == end:
break
# get available cells
neighbour_cells = curr_node.get_neighbours(absolute_orientation)
free_neighbour_cells = [cell_pos for cell_pos in neighbour_cells if cell_pos not in obstacles]
agent_pre_move, agent_post_move = moves[agent_name]
# filter the moves that do not due to conflict with other agents
invalid_neighbour_cells = []
for pre_move, post_move in moves.values():
if post_move != None:
for new_post_move in free_neighbour_cells:
ok = True
# check constraints between moves
# when 2 agents go through each other
if new_post_move == pre_move and agent_pre_move == post_move:
ok = False
# when 2 agents go on the same cell
elif new_post_move == post_move:
ok = False
# when 2 agents intersect on diagonal
elif check_diagonal_conflict(agent_pre_move, new_post_move, pre_move, post_move):
ok = False
# keep that move, to remove it later
if not ok:
invalid_neighbour_cells.append(new_post_move)
# keep only the good moves
free_neighbour_cells = [cell for cell in free_neighbour_cells if cell not in invalid_neighbour_cells]
# expand current node
# I used chebyshev distance, because the agents can move on diagonal, too (Wikipedia)
for neighbour in free_neighbour_cells:
new_cost = discovered[curr_node][1] + neighbour.get_euclidean_distance_to(curr_node)
if neighbour not in discovered:
discovered[neighbour] = (curr_node, new_cost)
heappush(frontier, (discovered[neighbour][1] + neighbour.get_euclidean_distance_to(end), neighbour))
expanded_node = True # expand once only
else:
if new_cost >= discovered[neighbour][1]:
continue
discovered[neighbour] = (curr_node, new_cost)
heappush(frontier, (discovered[neighbour][1] + neighbour.get_euclidean_distance_to(end), neighbour))
expanded_node = True
# return admissible moves
return frontier
class AStarPFAgent(PathfindingAgent):
def __init__(self):
super(AStarPFAgent, self).__init__(PathfindingAgentData.PATHFINDER)
def response(self, perceptions):
agent_pos = perceptions.agent_position
absolute_orientation = perceptions.absolute_orientation
obstacles = perceptions.obstacles
goal_name, goal_pos = perceptions.goal
moves = perceptions.moves
# expand current node by running A*
admissible_moves = a_star(agent_pos, goal_pos, absolute_orientation, obstacles, moves, self.agent_name)
return admissible_moves
class MyEnvironment(PathfindingEnvironment):
PF_AGENT_RANGE = 2
def __init__(self, w, h, num_pf_agents, random_seed):
"""
Default constructor. This should call the initialize methods offered by the super class.
"""
rand_seed = random_seed
print("Seed = %i" % rand_seed)
super(MyEnvironment, self).__init__()
pf_agents = []
for i in range(num_pf_agents):
agent = AStarPFAgent()
pf_agents.append(agent)
# initialize the environment
self.initialize_world(w=w, h=h, pathfinding_agents=pf_agents, rand_seed=rand_seed)
def step(self):
"""
This method should iterate through all agents, provide them with perceptions, and apply the action they return.
"""
"""
STAGE 1: generate perceptions for all agents, based on the state of the environment at the beginning of this
turn
"""
# keep the moves of all agents as tuples of (pre_move, post_move)
# initially all have post_move = None
moves = {}
for pf_agent_data in self._pathfinding_agents:
moves[pf_agent_data.linked_agent.agent_name] = (pf_agent_data.grid_position, None)
agent_perceptions = {}
# create perceptions for pathfinding agents
for pf_agent_data in self._pathfinding_agents:
if not pf_agent_data.linked_agent.finished:
# get obstacles and goal for this agent
nearby_obstacles = self.get_nearby_obstacles(pf_agent_data.grid_position, MyEnvironment.PF_AGENT_RANGE)
pf_agent_goal = self.agent_goal_map[pf_agent_data.linked_agent.agent_name]
agent_perceptions[pf_agent_data] = MyAgentPerception(agent_position=pf_agent_data.grid_position,
absolute_orientation=pf_agent_data.current_orientation,
obstacles=nearby_obstacles, goal=pf_agent_goal,
moves=moves)
"""
STAGE 2: call response for each agent to obtain desired actions
"""
agent_actions = {}
# get actions for all agents
# first agent expand its node, then we save the best pos as post_move in moves for that agent
# the next agent will use the updated map to get available moves, and save as well their best move
# and so on
# so, there is a branch factor of 8, instead of 8 ^ n, because WAIT is processed here
for pf_agent_data in self._pathfinding_agents:
# print(pf_agent_data.linked_agent.agent_name, "->", pf_agent_data.linked_agent.finished)
if not pf_agent_data.linked_agent.finished:
# update the map of moves for the next agent and set it in its perceptions
agent_perceptions[pf_agent_data].moves = moves
agent_actions[pf_agent_data] = pf_agent_data.linked_agent.response(agent_perceptions[pf_agent_data])
pre_move, post_move = moves[pf_agent_data.linked_agent.agent_name]
if agent_actions[pf_agent_data]:
_, new_post_move = heappop(agent_actions[pf_agent_data])
moves[pf_agent_data.linked_agent.agent_name] = (pre_move, new_post_move)
else: # in this case, the agent WAITS
moves[pf_agent_data.linked_agent.agent_name] = (pre_move, pre_move)
"""
STAGE 3: apply the agents' actions in the environment
"""
for pf_agent_data in self._pathfinding_agents:
if not pf_agent_data.linked_agent.finished:
_, post_move = moves[pf_agent_data.linked_agent.agent_name]
if post_move:
pf_agent_data.grid_position = post_move
# set all the agents that reached their destinations to finished
for pf_agent_data in self._pathfinding_agents:
if not pf_agent_data.linked_agent.finished:
if pf_agent_data.grid_position == self.agent_goal_map[pf_agent_data.linked_agent.agent_name][1]:
pf_agent_data.linked_agent.finished = True
class Tester(object):
WIDTH = 10
HEIGHT = 10
DELAY = 0.1
def __init__(self, num_agents, random_seed):
self.env = MyEnvironment(Tester.WIDTH, Tester.HEIGHT, num_agents, random_seed)
print("\n### INITIAL GRID ###")
print(self.env)
self.make_steps()
def make_steps(self):
steps = 0
while not self.env.goals_completed():
self.env.step()
print(self.env)
time.sleep(Tester.DELAY)
steps += 1
print("\nTHE AGENTS HAVE REACHED DESTINATIONS AFTER %s STEPS!!!" % str(steps))
if __name__ == "__main__":
print('Number of arguments:', len(sys.argv), 'arguments.')
if len(sys.argv) < 3:
print("[Usage] -> python3 my_pathfinding_world.py $nr_agents $random_seed")
sys.exit(1)
print('Argument List:', str(sys.argv[1:]))
num_agents = int(sys.argv[1])
random_seed = int(sys.argv[2])
tester = Tester(num_agents, random_seed)