-
Notifications
You must be signed in to change notification settings - Fork 0
/
frequency and timetable(1).py
310 lines (203 loc) · 7.79 KB
/
frequency and timetable(1).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
#!/usr/bin/env python
# coding: utf-8
# In[11]:
import pandas as pd
import numpy as np
import math, random, os
# timetable = pd.read_csv("final timetable.csv")
routesCSV = pd.read_csv("routes.csv")
path = "dummy4/2020-06-23/"
routesCount = routesCSV.shape[0]
# routesCount = 45
busOcc = np.ones(24)*30 #array of max occupancy each hour
occUpperT = np.ones(24)*5 #tolerable increase in bus occupancy
occLowerT = np.ones(24)*10 #tolerable decrease in bus occupancy
dosLoad = 5 #denial of service for loads below certain value
minHeadway = 3 #max time the passenger can wait before giving up
serviceStartHr = 0
serviceEndHr = 23
# In[15]:
#redistribute passengers in integer values
intRouteLoad = [] #route day Loads
for routeNo in range(routesCount):
routeLoad = pd.read_csv(path + str(routeNo) +".csv")
routeLoad = routeLoad.drop("Unnamed: 0", axis = 1)
routeLoad = routeLoad.transpose()
intRouteLoad.append(routeLoad)
# print(routeNo)
# In[16]:
#load accumalation as per min headway.
allHeadwayRL = []
for routeNo in range(len(intRouteLoad)):
route = intRouteLoad[routeNo]
headwayRL = pd.DataFrame()
for stopNo in range(route.shape[1]):
stopNo = stopNo
stopLoad = []
for hours in range(0,route.shape[0],minHeadway):
load = 0
for hour in range(hours, hours+minHeadway):
# print(routeNo, stopNo, hour)
load += route[stopNo][hour]
stopLoad.append(load)
headwayRL[stopNo] = stopLoad
allHeadwayRL.append(headwayRL)
# allHeadwayRL
# ## frequency calculation
# 1. if route is among lowFreqRoute - calc assign busses as per load
# 2. else use freq formula.
# a. if dosLoad < load bw headway < 30 - give one bus at the end of 3hrs
# b. if load bw headway < dosLoad - deny service.
# c. if load by headway > 30 - calc slope of load increase and send buses when load = 30
# if some ppl remain after busses, add them to next hr load and calculate the freq for
# In[17]:
# allHeadwayRL[lowFreqRoute[3]].plot()
len(allHeadwayRL)
# In[18]:
# method 1
# find the stop where max total load is observed
def freq1(hourLoad, headLoad):
freqOP = []
for routeNo in range(routesCount):
dailyMP = 0 # daily max point
temp = 0
for stopNo in range(hourLoad[routeNo].shape[1]):
hrLoad = hourLoad[routeNo][stopNo].sum()
if hrLoad > temp:
temp = hourLoad[routeNo][stopNo].sum()
dailyMP = stopNo
#freq = bus/hour
freq = []
# if routeNo not in lowFreqRoute:
hr = 0
for hr in range(int(24/minHeadway)):
# headHr = int(hr/minHeadway)
headHrLoad = headLoad[routeNo][dailyMP][hr]
if headHrLoad <= dosLoad: # f = 0
f = headHrLoad/busOcc[hr]
freq.append(0)
elif headHrLoad <= busOcc[hr]: # f = 1
freq.append(1)
elif headHrLoad > busOcc[hr]: #more buses than minHeadway ie more ppl than 30
f = headHrLoad/busOcc[hr]
freq.append(f)
# print(routeNo, hr, headLoad)
freqOP.append(freq)
# else:
# # calc the busses required. assign to 2 peak hours first. then to rest.
# intRouteLoad[routeNo].sum().plot()
print("khatam")
return freqOP
freq1 = freq1(intRouteLoad,allHeadwayRL)
freq1 = pd.DataFrame(freq1)
freq1 = freq1.transpose()
# In[19]:
#method 2
def freq2(hourLoad, headLoad):
freqOP = []
for routeNo in range(routesCount):
maxL = []
for hour in range(int(24/minHeadway)):
# print(routeNo, hour , headLoad[routeNo])
maxL.append(headLoad[routeNo].iloc[hour].max())
freq = []
# if routeNo not in lowFreqRoute:
for hr in range(int(24/minHeadway)):
# headHr = int(hr/minHeadway)
headHrLoad = maxL[hr]
if headHrLoad <= dosLoad: # f < 1
f = headHrLoad/busOcc[hr]
freq.append(0)
elif headHrLoad <= busOcc[hr]: # f = 1
freq.append(1)
elif headHrLoad > busOcc[hr]: # f > 1
f = headHrLoad/busOcc[hr]
freq.append(f)
# print(routeNo, hr, headLoad)
freqOP.append(freq)
# else:
# # calc the busses required. assign to 2 peak hours first. then to rest.
# intRouteLoad[routeNo].sum().plot()
print("khatam")
return freqOP
freq2 = freq2(intRouteLoad,allHeadwayRL)
freq2 = pd.DataFrame(freq2)
freq2 = freq2.transpose()
# - timetable for one month
# - take average of one month.
# - different avg of weekends and normal days
# In[60]:
def arrivals(freq):
routeArrivals = []
for routeNo in range(routesCount):
arrivalTimes = []
hr = 0
while hr < 24:
headFreq = freq[routeNo][hr//minHeadway]
if headFreq ==0:
hr+=minHeadway
continue
time = hr*60
headway = minHeadway*60/headFreq
while time < (hr+minHeadway)*60:
time += headway
if time <= (hr+minHeadway)*60:
# print(hr*60, headFreq, time)
# print(routeNo , int(time))
arrivalTimes.append(time)
# print(routeNo , time,hr, hrHeadway)
hr += minHeadway
routeArrivals.append(arrivalTimes)
return routeArrivals
# In[ ]:
# In[61]:
# Headway to timetable
arrivalTimes1 = arrivals(freq1)
arrivalTimes1 = pd.DataFrame(arrivalTimes1)
arrivalTimes1 = arrivalTimes1.transpose()
arrivalTimes2 = arrivals(freq2)
arrivalTimes2 = pd.DataFrame(arrivalTimes2)
arrivalTimes2 = arrivalTimes2.transpose()
# allHeadwayRL[6][6].sum()
# In[ ]:
# In[74]:
# In[83]:
def formTimetable(arrivalTimes):
timetable = pd.DataFrame(columns=["routeCode","From","To","direction","startTime","duration","endTime"])
rowNo = 0
for routeNo in range(routesCount):
routeInfo = routesCSV[routesCSV["routeCode"] == routeNo].iloc[0]
fromTerm = routeInfo.From
toTerm = routeInfo.To
dur = routeInfo.duration
routeName = routeInfo.routeName
direction = routeInfo.direction
# print(arrivalTimes[routeNo])
tripsCount = len(arrivalTimes[routeNo])
# print(arrivalTimes[routeNo])
for trip in range(tripsCount):
startTime = arrivalTimes[routeNo][trip]
# print(startTime)
if not arrivalTimes[routeNo].isnull()[trip]:
endTime = startTime + dur
startTime = startTime/60
endTime = endTime/60
# print(routeNo , startTime, endTime)
startTime2 = str(int(startTime)) + ":" + str(int((startTime - int(startTime))*60))
endTime2 = str(int(endTime)) + ":" + str(int((endTime - int(endTime))*60))
# print(startTime2, endTime2)
row = [routeName, fromTerm, toTerm, direction, startTime, dur, endTime]
timetable.loc[rowNo] = row
rowNo += 1
print("khatam")
return timetable
# In[84]:
# formTimetable(arrivalTimes1)
# formTimetable(arrivalTimes2)
# In[87]:
newTimetable1 = formTimetable(arrivalTimes1)
newTimetable1.to_csv("newTT1.csv")
# In[88]:
newTimetable2 = formTimetable(arrivalTimes2)
newTimetable2.to_csv("newTT2.csv")
# In[ ]: