-
Notifications
You must be signed in to change notification settings - Fork 0
/
traffic.py
149 lines (124 loc) · 5.76 KB
/
traffic.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
from sys import exit
from car import Car
from math import ceil
import numpy as np
"""
Represents different traffic strategies.
"""
class Traffic:
def __init__(self, env, intersections, links, monitor):
self.env = env
self.intersections = intersections
self.links = links
self.monitor = monitor
self.traffics = {
"nyc_weekday": self.run_nyc_weekday,
"nyc_weekend": self.run_nyc_weekend,
"kanamori": self.run_kanamori,
"cambridge": self.run_cambridge
}
def get_traffic_strategy(self, name, param={}):
if name not in self.traffics:
print("Strategy", name, "does not exist")
exit()
return self.traffics[name](param)
"""
Helper generator method that terminates once all the given processes are terminated as well.
We use this so that the simulation terminates when all the cars finish.
This is necessary, because the stigmergy caches' processes never end,
so we must clue the environment when the simulation should really end.
"""
def __wait_for_all_procs_to_finish(self, procs):
while len(procs) > 0:
active_procs = []
for proc in procs:
if not proc.processed:
active_procs.append(proc)
procs = active_procs
yield self.env.timeout(1)
def run_nyc_weekday(self, param):
print("start nyc weekday traffic")
procs = []
num = param["num"] if param["num"] else 5
omega = param["omega"]
alpha = param["alpha"]
beta = param["beta"]
perc = param["perc"]
strategy = param["strategy"]
hourly_traffic = [0.46, 0.29, 0.19, 0.16, 0.16, 0.27, 0.58, 0.94, 1.00, 0.93,\
0.85, 0.84, 0.84, 0.83, 0.86, 0.88, 0.86, 0.87, 0.87, 0.88, 0.82,\
0.78, 0.76, 0.71] #Traffic as % of max, hourly from 12-1am to 11pm-12am
for i in range(1440 * 5):
day = i // 1440
hour = (i // 60) % 24 #Hour on 24-hour clock
n_cars = ceil(num * hourly_traffic[hour]) #number of cars per minute
for _ in range(n_cars): #Loop to generate multiple cars
procs.append(self.env.process(Car.generate_nyc_car(strategy, day, param["verbose"], self.monitor, self.env, self.links, self.intersections, omega, alpha, beta, perc)))
yield self.env.timeout(1)
yield from self.__wait_for_all_procs_to_finish(procs)
def run_nyc_weekend(self, param):
print("start nyc weekend traffic")
procs = []
num = param["num"] if param["num"] else 5
omega = param["omega"]
alpha = param["alpha"]
beta = param["beta"]
perc = param["perc"]
strategy = param["strategy"]
hourly_traffic = [0.67, 0.53, 0.43, 0.35, 0.28, 0.20, 0.25, 0.36, 0.52, \
0.65, 0.74, 0.80, 0.84, 0.84, 0.83, 0.80, 0.80, 0.81, 0.84, 0.80, \
0.73, 0.68, 0.65, 0.61] # Traffic as % of max, hourly from 12-1am to 11pm-12am
for i in range(1440 * 5):
day = i // 1440
hour = (i // 60) % 24 # Hour on 24-hour clock
n_cars = ceil(num * hourly_traffic[hour]) # number of cars per minute
for _ in range(n_cars): # Loop to generate multiple cars
procs.append(self.env.process(
Car.generate_nyc_car(strategy, day, param["verbose"], self.monitor, self.env, self.links, self.intersections,
omega, alpha, beta, perc)))
yield self.env.timeout(1)
yield from self.__wait_for_all_procs_to_finish(procs)
def run_kanamori(self, param):
print("start kanamori traffic")
procs = []
num = param["num"] if param["num"] else 5
omega = param["omega"]
alpha = param["alpha"]
beta = param["beta"]
perc = param["perc"]
strategy = param["strategy"]
for i in range(300):
day = i // 1440
hour = (i // 60) % 24 #Hour on 24-hour clock
minute = i % 1440
if minute < 100:
if np.random.uniform() < .25:
procs.append(self.env.process(Car.generate_car("case0", "v0", "v24", day, param["verbose"], self.monitor, self.env, self.links, self.intersections, omega, alpha, beta, perc)))
else:
procs.append(self.env.process(Car.generate_car(strategy, "v0", "v24", day, param["verbose"], self.monitor, self.env, self.links, self.intersections, omega, alpha, beta, perc)))
if np.random.uniform() < .25:
procs.append(self.env.process(Car.generate_car("case0", "v2", "v22", day, param["verbose"], self.monitor, self.env, self.links, self.intersections, omega, alpha, beta, perc)))
else:
procs.append(self.env.process(Car.generate_car(strategy, "v2", "v22", day, param["verbose"], self.monitor, self.env, self.links, self.intersections, omega, alpha, beta, perc)))
if np.random.uniform() < .25:
procs.append(self.env.process(Car.generate_car("case0", "v4", "v20", day, param["verbose"], self.monitor, self.env, self.links, self.intersections, omega, alpha, beta, perc)))
else:
procs.append(self.env.process(Car.generate_car(strategy, "v4", "v20", day, param["verbose"], self.monitor, self.env, self.links, self.intersections, omega, alpha, beta, perc)))
yield self.env.timeout(1)
yield from self.__wait_for_all_procs_to_finish(procs)
def run_cambridge(self, param):
print("start run_cambridge traffic")
procs = []
num = param["num"] if param["num"] else 5
omega = param["omega"]
alpha = param["alpha"]
beta = param["beta"]
perc = param["perc"]
strategy = param["strategy"]
for i in range(60):
day = i // 1440
n_cars = ceil(num) #number of cars per minute
for _ in range(n_cars): #Loop to generate multiple cars
procs.append(self.env.process(Car.generate_cambridge_car(strategy, day, param["verbose"], self.monitor, self.env, self.links, self.intersections, omega, alpha, beta, perc)))
yield self.env.timeout(1)
yield from self.__wait_for_all_procs_to_finish(procs)