-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·89 lines (74 loc) · 2.47 KB
/
main.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
#!/usr/bin/python
import time
import pygame
from vehicle import Vehicle
from simulation import SimulationWithHandlers
from animation import max_road_len
from animation_opengl import Animation
from animation_base import AnimationInterrupt
from animation_opengl import Animation
from sim_event_handler import *
class Config:
fps = 60
nb_lanes = 3
road_len = 600 # meter
spawn_rate = 3.0 # cars per second
speed_range = (25, 35) # (min, max) speed in meter/sec
speedup = 1 # int speed up factor: 1 sec in anim = speedup sec in sim
# Animation
window_height = 370
rows = 3 # number of wrapped roads vertically
window_width = 1800
sound = True
# Non-OpenGL animation specific configuration
#window_height = 500
#scale = 10
#road_len = -1
def start_sim():
conf = Config()
sim = SimulationWithHandlers(conf)
anim = Animation(sim, conf)
dt = 1./conf.fps
stats = StatsEvHandler()
sim.add_handler(stats)
avgspeed = AverageSpeedHandler()
sim.add_handler(avgspeed)
throughput = ThroughPutHandler()
sim.add_handler(throughput)
traveltime = TravelTimeHandler()
sim.add_handler(traveltime)
vehicle_count = VehicleCountHandler()
sim.add_handler(vehicle_count)
slow_zone1 = SlowZoneEvHandler(300, 450, max_velocity=7)
slow_zone1.enabled = False # disabled by default, enable by pressing O
sim.add_handler(slow_zone1)
anim.register_interactive_sim_handler(slow_zone1, pygame.K_o)
slow_zone2 = SlowZoneEvHandler(400, 500, max_velocity=0)
slow_zone2.enabled = False # disabled by default, enable by pressing P
sim.add_handler(slow_zone2)
anim.register_interactive_sim_handler(slow_zone2, pygame.K_p)
try:
while True:
for i in range(conf.speedup):
sim.time_step(conf.speedup*dt)
anim.draw_frame()
except (KeyboardInterrupt, AnimationInterrupt):
print()
print("Simulation interrupted.")
finally:
anim.destroy()
print(stats)
plt.figure()
ax1 = plt.subplot(5,1,1)
avgspeed.plot(subplot = True)
plt.subplot(5,1,2, sharex = ax1)
slow_zone2.plot(subplot = True)
plt.subplot(5,1,3, sharex = ax1)
throughput.plot(subplot = True)
plt.subplot(5,1,4, sharex = ax1)
traveltime.plot(subplot = True)
plt.subplot(5,1,5, sharex = ax1)
vehicle_count.plot(subplot = True)
plt.show()
if __name__ == "__main__":
start_sim()