forked from alan-turing-institute/stochastic-gp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BusSim_deterministic.py
420 lines (373 loc) · 20.3 KB
/
BusSim_deterministic.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
"""
This is a DETERMINISTIC and STATIC BusSim ABM to simulate the bus operation, the different to BusSim-truth is that BusSim truth is both stochastic and dynamic
"""
# Import
import numpy as np
import matplotlib.pyplot as plt
#from copy import deepcopy
#import time
class Bus:
def __init__(self, bus_params):
# Parameters to be defined
[setattr(self, key, value) for key, value in bus_params.items()]
# Fixed parameters
self.visited = 0 # visited bus stop
self.states = [] # noisy states: [status,position,velocity]
self.groundtruth = [] # ground-truth location of the buses (no noise)
self.trajectory = [] # store the location of each buses
#self.trajectory_noise = [] # store the location of each buses
return
def move(self):
self.position += self.velocity * self.dt
return
class BusStop:
def __init__(self, position, busstopID, arrival_rate, departure_rate,activation):
# Parameters to be defined
self.busstopID = busstopID
self.position = position
self.arrival_rate = arrival_rate
self.departure_rate = departure_rate
self.actual_headway = [] # store departure times of buses
self.arrival_time = [0] # store arrival time of buses
self.visited = [] # store all visited buses
self.activation = activation
class Model:
def __init__(self, model_params, TrafficSpeed,ArrivalRate,DepartureRate):
#self.params = (params, fixed_params, ArrivalRate, DepartureRate, TrafficSpeed)
[setattr(self, key, value) for key, value in model_params.items()]
# Initial Condition
#self.maxDemand=maxDemand
self.TrafficSpeed = TrafficSpeed
self.ArrivalRate = ArrivalRate
#no passengers board from the first and last stops
self.ArrivalRate[-1] = 0
self.ArrivalRate[0] = 0
self.DepartureRate = DepartureRate
#no passengers alight from the first two stops, but all alight from the last stop
self.DepartureRate[0] = 0
self.DepartureRate[1] = 0
self.DepartureRate[-1] = 1
#Fixed parameters
self.current_time = 0 # current time step of the simulation
self.GeoFence = self.dt * TrafficSpeed + 5 # an area to tell if the bus reaches a bus stop
self.StopList = np.arange(0, self.NumberOfStop * self.LengthBetweenStop, self.LengthBetweenStop) # this must be a list
self.FleetSize = int(self.EndTime / self.Headway)
self.initialise_busstops()
self.initialise_buses()
# self.initialise_states()
return
def agents2state(self, do_measurement=False):
'''
This function stores the system state of all agents in a state vector with format:
[bus_status1,bus_position1,bus_velocity1,...,bus_statusN,bus_positionN,bus_velocityN,busstop_arrivalrate1,busstop_departurerate1,...,busstop_arrivalrateM,busstop_departurerateM,TrafficSpeed]
'''
state_bus = np.ravel([(bus.status, bus.position, bus.velocity, bus.occupancy) for bus in self.buses])
if do_measurement:
state = state_bus # other data not provided
else:
state_busstop = np.ravel([(busstop.arrival_rate, busstop.departure_rate) for busstop in self.busstops])
# print(state_busstop)
state_traffic = np.ravel(self.TrafficSpeed)
state = np.concatenate((state_bus, state_busstop, state_traffic))
return state
def state2agents(self, state):
'''
This function converts the stored system state vectir back into each agent state
'''
# buses status, position and velocity
for i in range(len(self.buses)):
self.buses[i].status = int(state[4 * i])
self.buses[i].position = state[4 * i + 1]
self.buses[i].velocity = state[4 * i + 2]
self.buses[i].occupancy = int(state[4 * i + 3])
# bus stop arrival and departure rate
for i in range(len(self.busstops)):
#print([4 * self.FleetSize + 2 * i])
self.busstops[i].arrival_rate = state[4 * self.FleetSize + 2 * i]
self.busstops[i].departure_rate = state[4 * self.FleetSize + 2 * i + 1]
# traffic speed
self.TrafficState = state[-1]
return
def mask(self):
return 1, None
def step(self):
'''
This function moves the whole state one time step ahead
'''
# This is the main step function to move the model forward
self.current_time += self.dt
# Loop through each bus and let it moves or dwells
for bus in self.buses:
#print('now looking at bus ',bus.busID)
# CASE 1: INACTIVE BUS (not yet dispatched)
if bus.status == 0: # inactive bus (not yet dispatched)
# check if it's time to dispatch yet?
# if the bus is dispatched at the next time step
if self.current_time >= (bus.dispatch_time - self.dt):
bus.status = 1 # change the status to moving bus
bus.velocity = min(self.TrafficSpeed, bus.velocity + bus.acceleration * self.dt)
if bus.status == 1: # moving bus
bus.velocity = min(self.TrafficSpeed, bus.velocity + bus.acceleration * self.dt)
bus.move()
if bus.position > self.NumberOfStop * self.LengthBetweenStop:
bus.status = 3 # this is to stop bus after they reach the last stop
bus.velocity = 0
# if after moving, the bus enters a bus stop with passengers on it, then we move the status to dwelling
def dns(x, y): return abs(x - y)
if min(dns(self.StopList, bus.position)) <= self.GeoFence: # reached a bus stop
# Investigate the info from the current stop
Current_StopID = int(min(range(len(self.StopList)), key=lambda x: abs(self.StopList[x] - bus.position))) # find the nearest bus stop
#print('Current_StopID:',Current_StopID)
if Current_StopID != bus.visited:
#Store the visited stop
bus.visited = Current_StopID
#print('Current_StopID:',Current_StopID)
# passenger arrival rate
arrival_rate = self.busstops[Current_StopID].arrival_rate
if arrival_rate<0: arrival_rate=0
#print('Arrival rate: ',arrival_rate)
# passenger departure rate
departure_rate = self.busstops[Current_StopID].departure_rate
#print('Departure rate: ',departure_rate)
# Now calculate the number of boarding and alighting
boarding_count = 0
# if the bus is the first bus to arrive at the bus stop
if self.busstops[Current_StopID].arrival_time[-1] == 0:
if self.busstops[Current_StopID].activation <= self.current_time:
#boarding_count = min(np.random.poisson(arrival_rate * self.BurnIn), (bus.size - bus.occupancy))
#boarding_count = min(np.random.poisson(arrival_rate * (self.current_time-self.busstops[Current_StopID].activation)), (bus.size - bus.occupancy))
boarding_count = min(int(arrival_rate * (self.current_time-self.busstops[Current_StopID].activation)), (bus.size - bus.occupancy))
#if boarding_count>99:
#print('Boarding passengers = ', boarding_count)
alighting_count = int(bus.occupancy * departure_rate)
else:
timegap = self.current_time - self.busstops[Current_StopID].arrival_time[-1]
#boarding_count = min(np.random.poisson(arrival_rate*timegap/2), bus.size - bus.occupancy)
boarding_count = min(int(arrival_rate*timegap/2), bus.size - bus.occupancy)
#print('Boarding passengers = ', boarding_count)
alighting_count = int(bus.occupancy * departure_rate)
#print('Alighting passengers = ', alighting_count)
# If there is at least 1 boarding or alighting passenger
if boarding_count > 0 or alighting_count > 0: # there is at least 1 boarding or alighting passenger
# change the bus status to dwelling
bus.status = 2 # change the status of the bus to dwelling
bus.velocity = 0
bus.leave_stop_time = self.current_time + boarding_count * self.BoardTime + alighting_count * self.AlightTime + self.StoppingTime # total time for dwelling
bus.occupancy = min(bus.occupancy - alighting_count + boarding_count, bus.size)
# store the headway and arrival times
self.busstops[Current_StopID].arrival_time.extend([self.current_time]) # store the arrival time of the bus
if self.busstops[Current_StopID].arrival_time[-1] != 0:
#print([self.current_time - self.busstops[Current_StopID].arrival_time[-1]])
self.busstops[Current_StopID].actual_headway.extend([self.current_time - self.busstops[Current_StopID].arrival_time[-1]])# store the headway to the previous bus
# CASE 3: DWELLING BUS (waiting for people to finish boarding and alighting)
if bus.status == 2:
# check if people has finished boarding/alighting or not?
# if the bus hasn't left and can leave at the next time step
if self.current_time >= (bus.leave_stop_time - self.dt):
bus.status = 1 # change the status to moving bus
bus.velocity = min(self.TrafficSpeed, bus.velocity + bus.acceleration * self.dt)
bus.groundtruth.append([bus.status, bus.position, bus.velocity, bus.occupancy])
bus.trajectory.extend([bus.position])
#bus.trajectory_noise.extend([bus.position + np.random.normal(0, self.Noise_std)])
return
def initialise_busstops(self):
self.busstops = []
for busstopID in range(len(self.StopList)):
position = self.StopList[busstopID] # set up bus stop location
# set up bus stop location
arrival_rate = self.ArrivalRate[busstopID]
# set up bus stop location
departure_rate = self.DepartureRate[busstopID]
#text0 = ['Departure rate of stop:', busstopID, 'is: ',departure_rate ]
#print(text0)
activation = busstopID * int(self.LengthBetweenStop/(self.TrafficSpeed)) - 1*60
#text0 = ['Activation time of stop:', busstopID, 'is: ',activation ]
#print(text0)
# define the bus stop object and add to the model
busstop = BusStop(position, busstopID, arrival_rate, departure_rate,activation)
self.busstops.append(busstop)
return
def initialise_buses(self):
self.buses = []
for busID in range(self.FleetSize):
bus_params = {
"dt": self.dt,
"busID": busID,
# all buses starts at the first stop,
"position": -self.TrafficSpeed * self.dt,
"occupancy": 0,
"size": 100,
"acceleration": self.BusAcceleration / self.dt,
"velocity": 0, # staring speed is 0
"leave_stop_time": 9999, # this shouldn't matter but just in case
"dispatch_time": busID * self.Headway,
"status": 0 # 0 for inactive bus, 1 for moving bus, and 2 for dwelling bus, 3 for finished bus
}
# define the bus object and add to the model
bus = Bus(bus_params)
self.buses.append(bus)
return
def run_model(model_params, TrafficSpeed,ArrivalRate,DepartureRate,do_ani,do_spacetime_plot,do_reps,uncalibrated):
'''
Model runing and plotting
'''
model = Model(model_params, TrafficSpeed,ArrivalRate,DepartureRate)
if do_ani or do_spacetime_plot or uncalibrated:
for time_step in range(int(model.EndTime / model.dt)):
model.step()
if do_ani:
text0 = ['Traffic Speed: %.2f m/s' % (model.TrafficSpeed)]
plt.text(0, 0.025, "".join(text0))
text2 = ['Alighted passengers: %d' % (len(model.alighted_passengers))]
plt.text(0, 0.02, "".join(text2))
plt.text(0, 0.015, 'Number of waiting passengers')
plt.plot(model.StopList, np.zeros_like(model.StopList), "|", markersize=20, alpha=0.3)
plt.plot(model.StopList, np.zeros_like(model.StopList), alpha=0.3)
for busstop in model.busstops:
# this is the value where you want the data to appear on the y-axis.
val = 0.
plt.plot(busstop.position, np.zeros_like(busstop.position) + val, 'ok', alpha=0.2)
plt.text(busstop.position, np.zeros_like(busstop.position) + val + 0.01, len(busstop.passengers))
for bus in model.buses:
plt.plot(bus.position, np.zeros_like(bus.position) + val, '>', markersize=10)
text1 = ['BusID= %d, occupancy= %d, speed= %.2f m/s' % (bus.busID + 1, len(bus.passengers), bus.velocity)]
# text1 = ['BusID= ',str(bus.busID+1),', occupancy= ',str(len(bus.passengers))]
if bus.status != 0:
plt.text(0, -bus.busID * 0.003 - 0.01, "".join(text1))
# plt.axis([-10, 31000, -0.1, 0.1])
plt.axis('off')
plt.pause(1 / 30)
plt.clf()
plt.show()
if do_spacetime_plot :
plt.figure(3, figsize=(16 / 2, 9 / 2))
#plt.clf()
x = np.array([bus.trajectory for bus in model.buses]).T
t = np.arange(0, model.EndTime, model.dt)
x[x <= 0 ] = np.nan
x[x >= (model.NumberOfStop * model.LengthBetweenStop)] = np.nan
plt.ylabel('Distance (m)')
plt.xlabel('Time (s)')
plt.plot(t, x, linewidth=2)
plt.pause(1 / 300)
if uncalibrated:
plt.figure(3, figsize=(16 / 2, 9 / 2))
#plt.clf()
x = np.array([bus.trajectory for bus in model.buses]).T
t = np.arange(0, model.EndTime, model.dt)
x[x <= 0 ] = np.nan
x[x >= (model.NumberOfStop * model.LengthBetweenStop)] = np.nan
plt.ylabel('Distance (m)')
plt.xlabel('Time (s)')
plt.plot(t, x, linewidth=.5,linestyle = '--')
plt.pause(1 / 300)
'''
Now export the output data
'''
GPSData = model.buses[0].trajectory
for b in range(1, model.FleetSize):
GPSData = np.vstack((GPSData, model.buses[b].trajectory))
GPSData[GPSData < 0] = 0
GPSData=np.transpose(GPSData)
StateData = model.buses[0].states
for b in range(1, model.FleetSize):
StateData = np.hstack((StateData, model.buses[b].states))
StateData[StateData < 0] = 0
import pandas as pd
GroundTruth = pd.DataFrame(model.buses[0].groundtruth)
for b in range(1, model.FleetSize):
GroundTruth = np.hstack((GroundTruth, model.buses[b].groundtruth))
GroundTruth[GroundTruth < 0] = 0
ArrivalData = ()
for b in range(len(model.busstops)):
ArrivalData += tuple([model.busstops[b].arrival_time])
return model, model_params, ArrivalData, StateData, GroundTruth, GPSData
if do_reps: #make a lot of replications
RepGPS = []
for time_step in range(int(model.EndTime / model.dt)):
model.step()
RepGPS = model.buses[0].trajectory
for b in range(1, model.FleetSize):
RepGPS = np.vstack((RepGPS, model.buses[b].trajectory))
RepGPS[RepGPS < 0] = 0
RepGPS=np.transpose(RepGPS)
return RepGPS
if __name__ == '__main__':
NumberOfStop=20
minDemand=0.5
maxDemand=2
#Initialise the ArrivalRate and DepartureRate
ArrivalRate = np.random.uniform(minDemand / 60, maxDemand / 60, NumberOfStop)
DepartureRate = np.sort(np.random.uniform(0.05, 0.5,NumberOfStop))
TrafficSpeed=14
#Initialise the model parameters
model_params = {
"dt": 10,
"minDemand":minDemand,
"NumberOfStop": NumberOfStop,
"LengthBetweenStop": 2000,
"EndTime": 6000,
"Headway": 5 * 60,
"BurnIn": 1 * 60,
"AlightTime": 1,
"BoardTime": 3,
"StoppingTime": 3,
"BusAcceleration": 3, # m/s
}
#runing parameters
do_reps = True #whether we should export the distribution of headways
do_spacetime_plot = False
do_ani = False
do_spacetime_rep_plot = False
uncalibrated=False
if do_ani or do_spacetime_plot:
model, model_params, ArrivalData, StateData, GroundTruth,GPSData = run_model(model_params, TrafficSpeed,ArrivalRate,DepartureRate,do_ani,do_spacetime_plot,do_reps,uncalibrated)
plt.show()
import pickle
with open('BusSim_data_static.pkl', 'wb') as f:
pickle.dump([model_params, ArrivalRate, ArrivalData, DepartureRate, StateData, GroundTruth,GPSData], f)
GPSData = []
if do_reps:
NumReps = 100
#GPSData = [run_model(model_params,do_ani,do_spacetime_plot,do_reps,uncalibrated)]
for r in range(NumReps):
RepGPS = run_model(model_params,TrafficSpeed,ArrivalRate,DepartureRate,do_ani,do_spacetime_plot,do_reps,uncalibrated)
GPSData.append(RepGPS)
meanGPS = np.mean(GPSData,axis=0)
stdGPS = np.std(GPSData,axis=0)
import pickle
with open('BusSim_headway_100reps_static_deterministic.pkl', 'wb') as f:
pickle.dump([model_params,meanGPS,stdGPS], f)
if do_spacetime_rep_plot: #plot a few replications using the same data to see the spread
NumReps = 10
do_spacetime_plot = True
uncalibrated = False
for r in range(NumReps):
run_model(model_params,do_ani,do_spacetime_plot,do_reps,uncalibrated)
do_spacetime_plot = False
uncalibrated = True
if uncalibrated: #run and plot the model when it is uncalibrated to see further spread
for s in range(NumReps):
#Initialise the ArrivalRate and DepartureRate
ArrivalRate = np.random.uniform(minDemand / 60, maxDemand / 60, NumberOfStop)
DepartureRate = np.sort(np.random.uniform(0.05, 0.5,NumberOfStop))
TrafficSpeed = np.random.uniform(11, 17)
#Initialise the model parameters
model_params = {
"dt": 10,
"NumberOfStop": NumberOfStop,
"LengthBetweenStop": 2000,
"EndTime": 6000,
"Headway": 5 * 60,
"BurnIn": 1 * 60,
"AlightTime": 1,
"BoardTime": 3,
"StoppingTime": 3,
"BusAcceleration": 3, # m/s
"TrafficSpeed": TrafficSpeed, # m/s
"ArrivalRate": ArrivalRate,
"DepartureRate": DepartureRate
}
run_model(model_params,do_ani,do_spacetime_plot,do_reps,uncalibrated)
#print('model run')
plt.show()