-
Notifications
You must be signed in to change notification settings - Fork 4
/
visualize.py
113 lines (96 loc) · 3.92 KB
/
visualize.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
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Rectangle
from matplotlib import animation
import json
COLORS = {'goal': 'green', 'agent': 'blue', 'obstacle': 'red'}
class Animation:
def __init__(self, map_json, solution_json):
self.map = map_json
self.solution = solution_json
aspect = self.map["dimX"] / self.map["dimY"]
self.fig = plt.figure(frameon=False, figsize=(4 * aspect, 4))
self.ax = self.fig.add_subplot(111, aspect='equal')
self.fig.subplots_adjust(
left=0, right=1, bottom=0, top=1, wspace=None, hspace=None)
self.patches = []
self.agents = {}
xmin = -0.5
ymin = -0.5
xmax = self.map["dimX"] - 0.5
ymax = self.map["dimY"] - 0.5
plt.xlim(xmin, xmax)
plt.ylim(ymin, ymax)
self.patches.append(
Rectangle((xmin, ymin),
xmax - xmin,
ymax - ymin,
facecolor="none",
edgecolor='red'))
for o in map_json["obstacles"]:
x, y = o[0], o[1]
# red rectangle as obstacles
self.patches.append(
Rectangle((x - 0.5, y - 0.5),
1,
1,
facecolor=COLORS['obstacle'],
edgecolor='black'))
self.T = 0
for g in map_json['goals']:
self.patches.append(
Rectangle((g[0] - 0.4, g[1] - 0.4),
0.8,
0.8,
facecolor=COLORS['goal'],
edgecolor='black'))
for s, i in zip(map_json['starts'], range(len(map_json['starts']))):
self.agents[i] = Circle((s[0], s[1]),
0.3,
facecolor=COLORS['agent'],
edgecolor='black')
self.patches.append(self.agents[i])
self.T = max(self.T, len(self.solution['solutions'][i]['path']))
self.animation = animation.FuncAnimation(
self.fig,
self.animate_func,
init_func=self.init_func,
frames=int(self.T + 1) * 10,
interval=100,
blit=True)
def init_func(self):
for p in self.patches:
self.ax.add_patch(p)
return self.patches
def animate_func(self, i):
for j in range(self.map['numberofagents']):
agent = self.solution['solutions'][j]['agent']
pos = self.get_state(int(i / 10), agent)
p = (pos[0], pos[1])
self.agents[agent].center = p
for _, a in self.agents.items():
a.set_facecolor(COLORS['agent'])
return self.patches
def get_state(self, index, agent):
if self.solution['solutions'][agent]['agent'] == agent:
if index > len(self.solution['solutions'][agent]['path']) - 1:
return self.solution['solutions'][agent]['path'][-1]
else:
return self.solution['solutions'][agent]['path'][index]
else:
for i in range(len(self.solution['solutions'])):
if self.solution['solutions'][i]['agent'] == agent:
if index > len(
self.solution['solutions'][agent]['path']) - 1:
return self.solution['solutions'][agent]['path'][-1]
else:
return self.solution['solutions'][agent]['path'][index]
return [-1, -1]
def show(self):
plt.show()
if __name__ == "__main__":
with open("test/sampleSchedule.json") as solution_json_file:
solution_json = json.load(solution_json_file)
with open("test/sampleMap.json") as map_json_file:
map_json = json.load(map_json_file)
animation = Animation(map_json, solution_json)
animation.show()