-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha-star.py
More file actions
359 lines (301 loc) · 13.1 KB
/
a-star.py
File metadata and controls
359 lines (301 loc) · 13.1 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
"""A* algorithm for multiple agents in a grid world environment."""
import os
import time
from datetime import datetime
from heapq import heappop, heappush
from itertools import product
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from matplotlib import animation
from mpl_toolkits.axes_grid1 import make_axes_locatable
SEED = 42 # int or None, same seed creates same sequence of starts and goals
NUM_AGENTS = 9
NUM_EPISODES = 100
MAX_CPU_TIME = 60 # seconds
DRAW_PLOT = False
results = []
rng = np.random.default_rng(SEED) # set seed for reproducibility
# block layout 2.1
grid_list = [
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
]
# layout with dead ends 2.1 b
# grid_list = [
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
# [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
# [0, 0, 1, 1, 1, 1, 1,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1],
# [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
# [1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1],
# [1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# ]
# TODO: use get_grid function to get the grid
grid = np.array(grid_list)
# Create a heatmap to track the number of visits to each cell
visit_counts = np.zeros_like(grid)
# make one additional episode, because RL looses one episode for some reason
for episode in range(NUM_EPISODES + 1):
# Record the start time
start_time = time.process_time()
starts = {}
goals = {}
available_positions = np.argwhere(grid == 0)
# Generate unique starting positions
for i in range(NUM_AGENTS):
while True:
idx = rng.choice(len(available_positions))
# print("idx", idx, "from", len(available_positions))
start_pos = tuple(available_positions[idx])
if start_pos not in starts.values():
starts[f"agent_{i}"] = start_pos
break
# print("starts", starts)
# Generate unique goal positions
for i in range(NUM_AGENTS):
while True:
idx = rng.choice(len(available_positions))
goal_pos = tuple(available_positions[idx])
if goal_pos not in goals.values() and goal_pos not in starts.values():
goals[f"agent_{i}"] = goal_pos
break
starts_list = [starts[f"agent_{i}"] for i in range(NUM_AGENTS)]
goals_list = [goals[f"agent_{i}"] for i in range(NUM_AGENTS)]
# Define the start and goal positions for multiple agents
# blue green red pink
# starts = [(5, 0), (3, 12), (6, 5), (6, 14)]
# goals = [(6, 6), (9, 3), (6, 0), (3, 3)]
# Define directions for movement (left, right, stay, up, down)
directions = [
(0, -1),
(0, 1),
(0, 0),
(-1, 0),
(1, 0),
] # left, right, stay, up, down
def heuristic(pos, goal):
return abs(pos[0] - goal[0]) + abs(pos[1] - goal[1])
def a_star_multiple_agents(starts, goals, grid):
# Priority queue for A*
open_set = []
heappush(open_set, (0, starts, [starts]))
# Visited set to avoid processing the same state
visited = set([tuple(starts)])
while open_set:
if time.process_time() - start_time > MAX_CPU_TIME:
print(
f"A* search exceeded the time limit ({MAX_CPU_TIME}) with {time.process_time() - start_time} seconds."
)
return None
_, current_positions, paths = heappop(open_set)
# Check if all goals are reached
if all(current_positions[i] == goals[i] for i in range(len(goals))):
return paths
for moves in product(directions, repeat=len(starts)):
next_positions = [
(
current_positions[i][0] + moves[i][0],
current_positions[i][1] + moves[i][1],
)
for i in range(len(starts))
]
# Check for collisions and out-of-bound moves
if all(
0 <= next_positions[i][0] < len(grid)
and 0 <= next_positions[i][1] < len(grid[0])
and grid[next_positions[i][0]][next_positions[i][1]] != 1
for i in range(len(next_positions))
):
# Check for position collisions
if len(set(next_positions)) == len(next_positions):
# Check for swap collisions
if all(
next_positions[i] != current_positions[j]
or next_positions[j] != current_positions[i]
for i in range(len(starts))
for j in range(len(starts))
if i != j
):
new_state = tuple(next_positions)
if new_state not in visited:
visited.add(new_state)
g_cost = len(paths) + 1 # each move costs 1
f_cost = g_cost + sum(
heuristic(next_positions[i], goals[i])
for i in range(len(goals))
)
heappush(
open_set,
(f_cost, next_positions, paths + [next_positions]),
)
return None
# Find the simultaneous paths
paths = a_star_multiple_agents(starts_list, goals_list, grid_list)
# Record the end time
end_time = time.process_time()
# Calculate the CPU time
cpu_time = end_time - start_time
episode_data = {
"episode": episode,
"cpu_time": cpu_time,
"seed": SEED,
}
if paths:
max_steps = 0 # To find the max number of steps among agents
for agent_index, agent_path in enumerate(zip(*paths)):
agent_path = [step[agent_index] for step in paths]
steps_to_goal = 0
total_distance = 0
for step, position in enumerate(agent_path):
steps_to_goal += 1
# Compute the distance between consecutive positions
if step > 0:
previous_position = agent_path[step - 1]
manhattan_distance = abs(position[0] - previous_position[0]) + abs(
position[1] - previous_position[1]
)
total_distance += manhattan_distance
# Stop counting when the agent reaches the goal
if position == goals_list[agent_index]:
break
# Update max_steps if this agent took more steps
if steps_to_goal > max_steps:
max_steps = steps_to_goal
print(f"Path for agent {agent_index + 1}: {agent_path}")
print(f"Number of steps for agent {agent_index + 1}: {len(agent_path)}")
print(
f"Total distance traveled by agent {agent_index + 1}: {total_distance}"
)
# Collect per-agent data
episode_data["max_steps"] = max_steps
agent_id = agent_index
episode_data[f"agent_{agent_id}_steps"] = steps_to_goal
episode_data[f"agent_{agent_id}_total_distance"] = total_distance
start_pos = starts_list[agent_index]
goal_pos = goals_list[agent_index]
episode_data[f"agent_{agent_id}_start_x"] = start_pos[0]
episode_data[f"agent_{agent_id}_start_y"] = start_pos[1]
episode_data[f"agent_{agent_id}_goal_x"] = goal_pos[0]
episode_data[f"agent_{agent_id}_goal_y"] = goal_pos[1]
for path in zip(*paths):
for position in path:
visit_counts[position] += 1
else:
print("No path found.")
# Handle the case when no path is found
episode_data["max_steps"] = None
for agent_index in range(NUM_AGENTS):
agent_id = agent_index + 1
episode_data[f"agent_{agent_id}_steps"] = None
episode_data[f"agent_{agent_id}_total_distance"] = None
start_pos = starts_list[agent_index]
goal_pos = goals_list[agent_index]
episode_data[f"agent_{agent_id}_start_x"] = start_pos[0]
episode_data[f"agent_{agent_id}_start_y"] = start_pos[1]
episode_data[f"agent_{agent_id}_goal_x"] = goal_pos[0]
episode_data[f"agent_{agent_id}_goal_y"] = goal_pos[1]
print(f"CPU time: {cpu_time:.4f} seconds for episode {episode}.")
results.append(episode_data)
# After all episodes, create DataFrame and save to CSV
df = pd.DataFrame(results)
# Ensure the directory exists
os.makedirs("experiments/results", exist_ok=True)
# Generate a suitable filename with current date and time
current_time = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
output_file = os.path.join(
"experiments/results",
f"A_star_{NUM_AGENTS}agents_{current_time}.csv",
)
# Save the DataFrame to CSV
df.to_csv(output_file, index=False)
print(f"Results saved to {output_file}")
# Plot the heatmap
plt.figure()
ax = plt.gca()
plt.xlabel("X")
plt.ylabel("Y")
# Note: origin='upper' matches the indexing [y,x] with y=0 at top
heatmap_plot = plt.imshow(visit_counts, origin="upper")
# create an axes on the right side of ax. The width of cax will be 5%
# of ax and the padding between cax and ax will be fixed at 0.05 inch.
divider = make_axes_locatable(ax)
cax = divider.append_axes("right", size="5%", pad=0.05)
plt.colorbar(heatmap_plot, label="Number of visits", cax=cax)
# Save the heatmap
heatmap_file = os.path.join(
"experiments/results", f"A_star_{NUM_AGENTS}agents_{current_time}_heatmap.pdf"
)
plt.savefig(heatmap_file, bbox_inches="tight")
print(f"Heatmap saved to {heatmap_file}")
# Length of the largest sublist
if grid_list:
length_of_largest_sublist = max(len(sublist) for sublist in grid_list)
else:
length_of_largest_sublist = 0 # Define as 0 if the outer list is empty
if DRAW_PLOT:
fig, ax = plt.subplots(figsize=(10, 8))
# Initialize the lines for each agent
lines = [
ax.plot([], [], marker="o", label=f"Agent {i + 1}")[0]
for i in range(NUM_AGENTS)
]
# Set up the plot limits
ax.set_xlim(-1, len(grid_list))
ax.set_ylim(-1, length_of_largest_sublist)
ax.set_xlabel("X Coordinate")
ax.set_ylabel("Y Coordinate")
ax.set_title("Paths of Agents")
ax.legend()
ax.grid(True)
# Function to initialize the plot
def init():
for line in lines:
line.set_data([], [])
return lines
# Function to update the plot for each frame
def update(num):
for i, path in enumerate(zip(*paths)):
if num < len(path):
x, y = zip(*path[: num + 1])
lines[i].set_data(x, y)
return lines
# Create the animation
ani = animation.FuncAnimation(
fig,
update,
frames=max(len(path) for path in zip(*paths)),
init_func=init,
blit=True,
repeat=False,
)
plt.show()