-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBaselineAStar.py
More file actions
152 lines (123 loc) · 4.98 KB
/
Copy pathBaselineAStar.py
File metadata and controls
152 lines (123 loc) · 4.98 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
from Astar import AstarBaseline
from DNQ import TrafficRoutingEnv
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
import numpy as np
GRID_ROWS = 8 # 网格行数
GRID_COLS = 8 # 网格列数
NUM_AGENTS = 4 # 智能体个数
def plot_astar_trajectories(trajectories, goals, grid_size):
"""
在一个网格图上绘制所有智能体的 A* 轨迹,并标出起点和终点。
参数:
trajectories: {agent_id: [(r0, c0), (r1, c1), ...]} 字典
goals: {agent_id: (goal_r, goal_c)} 字典
grid_size: (H, W)
"""
H, W = grid_size
plt.figure(figsize=(H, W))
# 先画出灰色网格线
for x in range(H + 1):
plt.plot([0, W], [x, x], color='gray', linewidth=0.5)
for y in range(W + 1):
plt.plot([y, y], [0, H], color='gray', linewidth=0.5)
# 颜色列表,最多支持 6 个智能体
colors = ['r', 'g', 'b', 'c', 'm', 'y']
# 绘制每个智能体的轨迹
for i, traj in trajectories.items():
# 将 (row, col) 转换为画图坐标 (x=col+0.5, y=(H-1-row)+0.5)
xs = [pos[1] + 0.5 for pos in traj]
ys = [(H - 1 - pos[0]) + 0.5 for pos in traj]
plt.plot(xs, ys, marker='o', color=colors[i], label=f"Agent {i}")
# 标记起点(方块)
start = traj[0]
sx, sy = start[1] + 0.5, (H - 1 - start[0]) + 0.5
plt.scatter([sx], [sy], color=colors[i], marker='s', s=80)
# 标记终点(星号)
goal = goals[i]
gx, gy = goal[1] + 0.5, (H - 1 - goal[0]) + 0.5
plt.scatter([gx], [gy], color=colors[i], marker='*', s=120)
plt.xlim(0, W)
plt.ylim(0, H)
plt.gca().set_aspect('equal')
plt.xticks([]);
plt.yticks([])
plt.legend(bbox_to_anchor=(1.01, 1), loc='upper left', fontsize=9)
plt.title("A* Baseline Multi-Agent Trajectories")
plt.tight_layout()
plt.show()
def animate(trajectories, goals, grid_size):
"""
在一个网格图上动画展示所有智能体的 A* 轨迹,逐步显示每一步移动。
参数:
trajectories: {agent_id: [(r0, c0), (r1, c1), ...]} 字典
goals: {agent_id: (goal_r, goal_c)} 字典
grid_size: (H, W)
"""
H, W = grid_size
fig, ax = plt.subplots(figsize=(H, W))
# 画灰色网格线
for x in range(H + 1):
ax.plot([0, W], [x, x], color='gray', linewidth=0.5)
for y in range(W + 1):
ax.plot([y, y], [0, H], color='gray', linewidth=0.5)
# 颜色列表,最多支持 6 个智能体
colors = ['r', 'g', 'b', 'c', 'm', 'y']
# 绘制起点和终点(静态元素)
for i, traj in trajectories.items():
# 标记起点(方块)
start = traj[0]
sx, sy = start[1] + 0.5, (H - 1 - start[0]) + 0.5
ax.scatter([sx], [sy], color=colors[i], marker='s', s=80)
# 标记终点(星号)
goal = goals[i]
gx, gy = goal[1] + 0.5, (H - 1 - goal[0]) + 0.5
ax.scatter([gx], [gy], color=colors[i], marker='*', s=120)
# 初始化轨迹线
lines = []
for i in range(len(trajectories)):
line, = ax.plot([], [], marker='o', color=colors[i], label=f"Agent {i}")
lines.append(line)
# 计算最大步数
max_steps = max(len(traj) for traj in trajectories.values())
def init():
for line in lines:
line.set_data([], [])
return lines
def update(frame):
for i, line in enumerate(lines):
# 获取到当前帧为止的轨迹
current_traj = trajectories[i][:frame+1]
if len(current_traj) > 0:
xs = [pos[1] + 0.5 for pos in current_traj]
ys = [(H - 1 - pos[0]) + 0.5 for pos in current_traj]
line.set_data(xs, ys)
return lines
ax.set_xlim(0, W)
ax.set_ylim(0, H)
ax.set_aspect('equal')
ax.set_xticks([])
ax.set_yticks([])
ax.legend(bbox_to_anchor=(1.01, 1), loc='upper left', fontsize=9)
ax.set_title("A* Baseline Multi-Agent Trajectories Animation")
# 创建动画,每步间隔500毫秒
ani = FuncAnimation(fig, update, frames=max_steps, init_func=init,
interval=500, blit=True, repeat=False)
plt.tight_layout()
plt.show()
# 保存为GIF
ani.save('animate_results/greedy_trajectories_BaselineAStar.gif', writer='pillow', fps=2, dpi=100)
return ani # 返回动画对象以便保存
if __name__ == "__main__":
# 1. 初始化环境和基线算法
env = TrafficRoutingEnv(grid_size=(GRID_ROWS, GRID_COLS), num_agents=NUM_AGENTS)
baseline = AstarBaseline(env)
# 2. 运行 A* 基线,获取轨迹和目标
trajectories, goals = baseline.run_baseline()
# 3. 打印每个智能体的轨迹
print("A* Baseline Trajectories:")
for i, traj in trajectories.items():
print(f"Agent {i}: {traj}")
# 4. 调用可视化函数,绘制所有智能体的路径
plot_astar_trajectories(trajectories, goals, env.grid_size)
#animate(trajectories, goals, env.grid_size)