-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenario.py
370 lines (315 loc) · 17 KB
/
scenario.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# -*- coding: utf-8 -*-
"""
Copyright (C) Mon Aug 22 16:55:50 2016 Jianshan Zhou
Contact: [email protected] [email protected]
Website: <https://github.com/JianshanZhou>
This program is free software: you can redistribute
it and/or modify it under the terms of
the GNU General Public License as published
by the Free Software Foundation,
either version 3 of the License,
or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program.
If not, see <http://www.gnu.org/licenses/>.
This module defines some basic parameters that will be used for setting up the
VANET simulation scenarios.
"""
from scipy.stats import norm, expon, lognorm, fisk
import numpy as np
from vehicle import Vehicle, Mobility
class Speed_headway_random(object):
def __init__(self, scenario_flag = "Freeway_Free"):
"""
Totally five scenarios are supported here:
Freeway_Night, Freeway_Free, Freeway_Rush;
Urban_Peak, Urban_Nonpeak.
The PDFs of the vehicle speed and the inter-vehicle space are adapted
from existing references.
"""
if scenario_flag == "Freeway_Night":
self.headway_random = expon(0.0, 1.0/256.41)
meanSpeed = 30.93 #m/s
stdSpeed = 1.2 #m/s
elif scenario_flag == "Freeway_Free":
self.headway_random = lognorm(0.75, 0.0, np.exp(3.4))
meanSpeed = 29.15 #m/s
stdSpeed = 1.5 #m/s
elif scenario_flag == "Freeway_Rush":
self.headway_random = lognorm(0.5, 0.0, np.exp(2.5))
meanSpeed = 10.73 #m/s
stdSpeed = 2.0 #m/s
elif scenario_flag == "Urban_Peak":
scale = 1.096
c = 0.314
loc = 0.0
self.headway_random = fisk(c, loc, scale)
meanSpeed = 6.083 #m/s
stdSpeed = 1.2 #m/s
elif scenario_flag == "Urban_Nonpeak":
self.headway_random = lognorm(0.618, 0.0, np.exp(0.685))
meanSpeed = 12.86 #m/s
stdSpeed = 1.5 #m/s
else:
raise
self.speed_random = norm(meanSpeed, stdSpeed)
class Scenario(object):
def __init__(self,mobility,
road_length = 5.0*10**3, # the length of the road in meter
two_direction = True, # bool-type flag indicates the directions
lane_width = 3.0, # lane width
lane_num_per_direct = 2, # the number of lanes per direction
vehicle_length = 5.0, # length of a vehicle in meter
onehop_delay = 10.0*10**(-3), # the delay in one-hop communication in second
total_sim_time = 120.0, # total simulation time in second
scenario_flag = "Freeway_Free",
commR = 100.0):
self.ROAD_LENGTH = road_length
self.TWO_DIRECTION = two_direction
self.LANE_WIDTH = lane_width
self.LANE_NUM_PER_DIRECT = lane_num_per_direct
self.VEHICLE_LENGTH = vehicle_length
self.ONEHOP_DELAY = onehop_delay
self.TOTAL_SIM_TIME = total_sim_time
self.speed_headway_random = Speed_headway_random(scenario_flag)
self.COMMUNICATION_RANGE = commR
self.right_vehicle_count = np.zeros((self.LANE_NUM_PER_DIRECT,))
self.left_vehicle_count = np.zeros((self.LANE_NUM_PER_DIRECT,))
self.mobility = mobility
self.time_elapsed = 0.0
self.propagation_distance = 0.0
self.infected_ratio = 0.0
self.current_message_carrier = []
self.right_arrival_dt = []
self.left_arrival_dt = []
for laneInd in range(self.LANE_NUM_PER_DIRECT):
nextDt = (self.VEHICLE_LENGTH + \
self.speed_headway_random.headway_random.rvs())/(self.speed_headway_random.speed_random.rvs())
self.right_arrival_dt.append(nextDt)
nextDt1 = (self.VEHICLE_LENGTH + \
self.speed_headway_random.headway_random.rvs())/(self.speed_headway_random.speed_random.rvs())
self.left_arrival_dt.append(nextDt1)
def setup_vehicle_pools(self):
self.left_pool = []
self.right_pool = []
vehicle_id = 0
direct_flag = 1
lane_ID = 0
acceleration = np.array([0.0,0.0])
speed = np.array([0.0,0.0])
position = np.array([0.0,0.0])
position[1] = ((-1)**(direct_flag))*(self.LANE_WIDTH*0.5 + \
lane_ID*self.LANE_WIDTH)
speed[0] = self.speed_headway_random.speed_random.rvs()
vehicle = Vehicle(vehicle_id,direct_flag,lane_ID,
position,speed,acceleration)
vehicle.setup_communication_terminal(True)# the source
vehicle.communication_terminal.receive([vehicle_id])
self.current_message_carrier.append(vehicle)
self.propagation_distance = vehicle.position[0]
# the right pool
lane_pool = []
while position[0] < (self.ROAD_LENGTH - self.VEHICLE_LENGTH):
lane_pool.append(vehicle)
self.right_vehicle_count[lane_ID] += 1
vehicle_id += 1
acceleration = np.array([0.0,0.0])
speed = np.array([0.0,0.0])
speed[0] = self.speed_headway_random.speed_random.rvs()
position = np.array([position[0],0.0])
position[1] = ((-1)**(direct_flag))*(self.LANE_WIDTH*0.5 + \
lane_ID*self.LANE_WIDTH)
position[0] = position[0] + self.VEHICLE_LENGTH + self.speed_headway_random.headway_random.rvs()
vehicle = Vehicle(vehicle_id,direct_flag,lane_ID,
position,speed,acceleration)
vehicle.setup_communication_terminal(False)
self.right_pool.append(lane_pool)
for lane_ID in range(self.LANE_NUM_PER_DIRECT - 1):
vehicle_id = 0
acceleration = np.array([0.0,0.0])
speed = np.array([0.0,0.0])
position = np.array([0.0,0.0])
position[1] = ((-1)**(direct_flag))*(self.LANE_WIDTH*0.5 + \
(lane_ID + 1)*self.LANE_WIDTH)
speed[0] = self.speed_headway_random.speed_random.rvs()
vehicle = Vehicle(vehicle_id,direct_flag,(lane_ID + 1),
position,speed,acceleration)
vehicle.setup_communication_terminal(False)
lane_pool = []
while position[0] < (self.ROAD_LENGTH - self.VEHICLE_LENGTH):
lane_pool.append(vehicle)
self.right_vehicle_count[1+lane_ID] += 1
vehicle_id += 1
acceleration = np.array([0.0,0.0])
speed = np.array([0.0,0.0])
speed[0] = self.speed_headway_random.speed_random.rvs()
position = np.array([position[0],0.0])
position[1] = ((-1)**(direct_flag))*(self.LANE_WIDTH*0.5 + \
(lane_ID+1)*self.LANE_WIDTH)
position[0] = position[0] + self.VEHICLE_LENGTH + self.speed_headway_random.headway_random.rvs()
vehicle = Vehicle(vehicle_id,direct_flag,lane_ID,
position,speed,acceleration)
vehicle.setup_communication_terminal(False)
self.right_pool.append(lane_pool)
print "successfully set up the right vehicle pool!"
# the left pool
direct_flag = 2
for lane_ID in range(self.LANE_NUM_PER_DIRECT):
vehicle_id = 0
acceleration = np.array([0.0,0.0])
speed = np.array([0.0,0.0])
position = np.array([0.0,0.0])
position[1] = ((-1)**(direct_flag))*(self.LANE_WIDTH*0.5 + \
(lane_ID)*self.LANE_WIDTH)
position[0] = self.ROAD_LENGTH - position[0]
speed[0] = (-1)*self.speed_headway_random.speed_random.rvs()
vehicle = Vehicle(vehicle_id,direct_flag,(lane_ID + 1),
position,speed,acceleration)
vehicle.setup_communication_terminal(False)
lane_pool = []
while position[0] > (self.VEHICLE_LENGTH):
lane_pool.append(vehicle)
self.left_vehicle_count[lane_ID] += 1
vehicle_id += 1
acceleration = np.array([0.0,0.0])
speed = np.array([0.0,0.0])
speed[0] = (-1)*self.speed_headway_random.speed_random.rvs()
position = np.array([position[0],0.0])
position[1] = ((-1)**(direct_flag))*(self.LANE_WIDTH*0.5 + \
(lane_ID)*self.LANE_WIDTH)
position[0] = position[0] + (-1)*(self.VEHICLE_LENGTH + self.speed_headway_random.headway_random.rvs())
vehicle = Vehicle(vehicle_id,direct_flag,lane_ID,
position,speed,acceleration)
vehicle.setup_communication_terminal(False)
self.left_pool.append(lane_pool)
print "successfully set up the left vehicle pool!"
self.infected_ratio = 1.0*len(self.current_message_carrier)/(1.0*sum([len(self.right_pool[lane_ID]) for lane_ID in range(self.LANE_NUM_PER_DIRECT)]) \
+1.0*sum([len(self.left_pool[lane_ID]) for lane_ID in range(self.LANE_NUM_PER_DIRECT)]))
def input_vehicle(self, direct_flag, lane_ID):
if direct_flag == 1:
self.right_vehicle_count[lane_ID] += 1
vehicle_id = self.right_vehicle_count[lane_ID] - 1
acceleration = np.array([0.0,0.0])
speed = np.array([0.0,0.0])
speed[0] = self.speed_headway_random.speed_random.rvs()
position = np.array([0.0,0.0])
position[1] = ((-1)**(direct_flag))*(self.LANE_WIDTH*0.5 + \
(lane_ID)*self.LANE_WIDTH)
vehicle = Vehicle(vehicle_id,direct_flag,lane_ID,
position,speed,acceleration)
vehicle.setup_communication_terminal(False)
self.right_pool[lane_ID].insert(0,vehicle)
#update the arrival dt
self.right_arrival_dt[lane_ID] = (self.VEHICLE_LENGTH + \
self.speed_headway_random.headway_random.rvs())/(self.speed_headway_random.speed_random.rvs())
elif direct_flag == 2:
self.left_vehicle_count[lane_ID] += 1
vehicle_id = self.left_vehicle_count[lane_ID] - 1
acceleration = np.array([0.0,0.0])
speed = np.array([0.0,0.0])
speed[0] = (-1)*(self.speed_headway_random.speed_random.rvs())
position = np.array([0.0,0.0])
position[0] = self.ROAD_LENGTH - position[0]
position[1] = ((-1)**(direct_flag))*(self.LANE_WIDTH*0.5 + \
(lane_ID)*self.LANE_WIDTH)
vehicle = Vehicle(vehicle_id,direct_flag,lane_ID,
position,speed,acceleration)
vehicle.setup_communication_terminal(False)
self.left_pool[lane_ID].insert(0,vehicle)
# update the arrival dt
self.left_arrival_dt[lane_ID] = (self.VEHICLE_LENGTH + \
self.speed_headway_random.headway_random.rvs())/(self.speed_headway_random.speed_random.rvs())
def update_position(self):
# update the positions of all vehicles based on the mobility model
for lane_ID in range(self.LANE_NUM_PER_DIRECT):
#update right pool
for ind in range(len(self.right_pool[lane_ID])):
if ind == len(self.right_pool[lane_ID])-1:
front_vehicle = -1
else:
front_vehicle = self.right_pool[lane_ID][ind+1]
self.right_pool[lane_ID][ind].update_acceleration(self.mobility, front_vehicle)
self.right_pool[lane_ID][ind].update_speed(self.ONEHOP_DELAY)
self.right_pool[lane_ID][ind].update_position(self.ONEHOP_DELAY)
for vehicle in self.right_pool[lane_ID]:
if vehicle.position[0] > (self.ROAD_LENGTH-self.VEHICLE_LENGTH):
self.right_pool[lane_ID].remove(vehicle)
if vehicle in self.current_message_carrier:
self.current_message_carrier.remove(vehicle)
#update left pool
for ind in range(len(self.left_pool[lane_ID])):
if ind == len(self.left_pool[lane_ID])-1:
front_vehicle = -1
else:
front_vehicle = self.left_pool[lane_ID][ind+1]
self.left_pool[lane_ID][ind].update_acceleration(self.mobility, front_vehicle)
self.left_pool[lane_ID][ind].update_speed(self.ONEHOP_DELAY)
self.left_pool[lane_ID][ind].update_position(self.ONEHOP_DELAY)
for vehicle in self.left_pool[lane_ID]:
if vehicle.position[0] < (self.VEHICLE_LENGTH):
self.left_pool[lane_ID].remove(vehicle)
if vehicle in self.current_message_carrier:
self.current_message_carrier.remove(vehicle)
def communication(self):
new_message_carrier = []
for message_carrier in self.current_message_carrier:
for lane_ID in range(self.LANE_NUM_PER_DIRECT):
# the right pool situation
for vehicle in self.right_pool[lane_ID]:
if not vehicle.communication_terminal.received_flag:
if np.linalg.norm(message_carrier.position - vehicle.position) <=self.COMMUNICATION_RANGE:
message_carrier.communication_terminal.send(vehicle)
if vehicle.position[0] >= self.propagation_distance:
self.propagation_distance = vehicle.position[0]
#print "send a message from V-%d to V-%d"%(message_carrier.ID,vehicle.ID)
new_message_carrier.append(vehicle)
# the left pool situation
for vehicle in self.left_pool[lane_ID]:
if not vehicle.communication_terminal.received_flag:
if np.linalg.norm(message_carrier.position - vehicle.position) <= self.COMMUNICATION_RANGE:
message_carrier.communication_terminal.send(vehicle)
if vehicle.position[0] >= self.propagation_distance:
self.propagation_distance = vehicle.position[0]
#print "send a message from V-%d to V-%d"%(message_carrier.ID,vehicle.ID)
new_message_carrier.append(vehicle)
# update the message carrier pool
self.current_message_carrier.extend(new_message_carrier)
if self.infected_ratio < 1.0:
self.infected_ratio = 1.0*len(self.current_message_carrier)/(1.0*sum([len(self.right_pool[lane_ID]) for lane_ID in range(self.LANE_NUM_PER_DIRECT)]) \
+1.0*sum([len(self.left_pool[lane_ID]) for lane_ID in range(self.LANE_NUM_PER_DIRECT)]))
else:
self.infected_ratio = 1.0
self.time_elapsed += self.ONEHOP_DELAY
#%% testing
def test1():
mobility = Mobility()
scenario = Scenario(mobility)
scenario.setup_vehicle_pools()
def test2():
# simulation demo
# set up the scenario
scenario = Scenario(Mobility())
scenario.setup_vehicle_pools()
total_sim_rounds = int(np.ceil(scenario.TOTAL_SIM_TIME/scenario.ONEHOP_DELAY))
input_time_counts = np.zeros((2*scenario.LANE_NUM_PER_DIRECT,),dtype=float)
for time_flag in range(total_sim_rounds):
input_time_counts = input_time_counts \
+ np.ones((2*scenario.LANE_NUM_PER_DIRECT,),dtype=float)*scenario.ONEHOP_DELAY
scenario.update_position()
scenario.communication()
for lane_ID in range(scenario.LANE_NUM_PER_DIRECT):
# right pool
if input_time_counts[lane_ID] >= scenario.right_arrival_dt[lane_ID]:
scenario.input_vehicle(1,lane_ID)
input_time_counts[lane_ID] = 0.0
if input_time_counts[lane_ID+2] >= scenario.left_arrival_dt[lane_ID]:
scenario.input_vehicle(2,lane_ID)
input_time_counts[lane_ID+2] = 0.0
#%%
if __name__ == "__main__":
test2()