-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_corridor_crossing.py
208 lines (177 loc) · 5.32 KB
/
test_corridor_crossing.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
import pathlib
import matplotlib.pyplot as plt
import jupedsim as jps
import pedpy
import numpy as np
from numpy.random import normal # normal distribution of free movement speed
from shapely import Polygon
## Setup geometries
area = Polygon([(0, 0), (1, 0), (1, 5), (0, 5)])
walkable_area = pedpy.WalkableArea(area)
# pedpy.plot_walkable_area(walkable_area=walkable_area).set_aspect("equal")
## Setup spawning area
spawning_area_list = [
Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]),
Polygon([(0, 4), (1, 4), (1, 5), (0, 5)]),
]
pos_in_spawning_areas = [
jps.distribute_until_filled(
polygon=spawning_area,
distance_to_agents=1,
distance_to_polygon=0.3,
seed=1,
)
for spawning_area in spawning_area_list
]
# flattening the list
pos_in_spawning_areas = [item for sublist in pos_in_spawning_areas for item in sublist]
exit_area_list = [
Polygon([(0, 4), (1, 4), (1, 5), (0, 5)]),
Polygon([(0, 0), (1, 0), (1, 1), (0, 1)]),
]
## Setup Simulation
trajectory_file = "test_HumanoidModelV0_corridor_crossing.sqlite" # output file
simulation = jps.Simulation(
model=jps.HumanoidModelV0(),
geometry=area,
trajectory_writer=jps.SqliteHumanoidTrajectoryWriter(
output_file=pathlib.Path(trajectory_file)
),
)
journey_id_list = []
exit_id_list = []
for exit_area in exit_area_list:
exit_id = simulation.add_exit_stage(exit_area.exterior.coords[:-1])
journey = jps.JourneyDescription([exit_id])
exit_id_list.append(exit_id)
journey_id_list.append(simulation.add_journey(journey))
## Spawn agents
v_distribution = normal(1.34, 0.5, len(pos_in_spawning_areas))
for pos, v0, journey_id, exit_id in zip(
pos_in_spawning_areas, v_distribution, journey_id_list, exit_id_list
):
simulation.add_agent(
jps.HumanoidModelV0AgentParameters(
journey_id=journey_id,
stage_id=exit_id,
position=pos,
head_position=pos,
heel_right_position=(pos[0] + 0.15, pos[1]),
heel_left_position=(pos[0] - 0.15, pos[1]),
desiredSpeed=v0,
height=1.7,
)
)
## run simulation
# print(isinstance(agent.model, jps.py_jupedsim.HumanoidModelV0State))
while simulation.agent_count() > 0:
simulation.iterate()
## Import Sqlite with PedPy
from sqlite_loader_moded_pepy_fun import *
TrajectoryData = load_trajectory_from_jupedsim_sqlite(pathlib.Path(trajectory_file))
traj = TrajectoryData.data
# print(TrajectoryData.data[TrajectoryData.data["frame"] == 10]) # .iloc[0:5])
# Plot y_position of right and left heel as a function of frames on the same figure
fig, axs = plt.subplots(1, 3, figsize=(15, 6)) # Two subplots
# Get unique agent IDs
unique_agent_ids = traj["id"].unique()
# Create a colormap
cmap = plt.get_cmap("rainbow", len(unique_agent_ids))
# Initialize a counter for the color index
color_index = 0
for agent_id in traj["id"].unique():
agent_data = traj[traj["id"] == agent_id]
color = cmap(color_index)
# Move to the next color index
color_index += 1
# Y-Position Subplot
axs[0].plot(
agent_data["frame"],
agent_data["heel_right_pos_y"],
label=f"Right Heel, Agent {agent_id}",
ls="-",
color=color,
)
axs[0].plot(
agent_data["frame"],
agent_data["heel_left_pos_y"],
label=f"Left Heel, Agent {agent_id}",
ls="--",
color=color,
)
axs[0].plot(
agent_data["frame"],
agent_data["head_pos_y"],
label=f"Head, Agent {agent_id}",
ls=":",
alpha=0.5,
color=color,
)
# X-Position Subplot
axs[1].plot(
agent_data["frame"],
agent_data["heel_right_pos_x"],
label=f"Right Heel, Agent {agent_id}",
ls="-",
color=color,
)
axs[1].plot(
agent_data["frame"],
agent_data["heel_left_pos_x"],
label=f"Left Heel, Agent {agent_id}",
ls="--",
color=color,
)
axs[1].plot(
agent_data["frame"],
agent_data["head_pos_x"],
label=f"Head, Agent {agent_id}",
ls=":",
alpha=0.5,
color=color,
)
# X-Y Subplot
axs[2] = pedpy.plot_walkable_area(walkable_area=walkable_area)
axs[2].plot(
agent_data["heel_right_pos_x"],
agent_data["heel_right_pos_y"],
label=f"Right Heel, Agent {agent_id}",
ls="-",
color=color,
)
axs[2].plot(
agent_data["heel_left_pos_x"],
agent_data["heel_left_pos_y"],
label=f"Left Heel, Agent {agent_id}",
ls="--",
color=color,
)
axs[2].plot(
agent_data["head_pos_x"],
agent_data["head_pos_y"],
label=f"Head, Agent {agent_id}",
ls=":",
alpha=0.5,
color=color,
)
# Y-Position Subplot Configuration
axs[0].set_title("Y-Positions")
axs[0].set_xlabel("Frames")
axs[0].set_ylabel("Y-Position (m)")
axs[0].legend()
axs[0].grid(True)
# X-Position Subplot Configuration
axs[1].set_title("X-Position")
axs[1].set_xlabel("Frames")
axs[1].set_ylabel("X-Position (m)")
axs[1].legend()
axs[1].grid(True)
# X-Position Subplot Configuration
axs[2].set_title("X-Y")
axs[2].set_xlabel("X-Positions (m)")
axs[2].set_ylabel("Y-Positions (m)")
# axs[2].legend()
axs[2].grid(True)
axs[2].set_aspect("equal")
plt.tight_layout() # Adjust subplots to fit the figure area
plt.show()