-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetro.py
267 lines (239 loc) · 8.8 KB
/
metro.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
# IIQ 3763 Project - 2018-1
# Staircases and Mechanical Staircases, TASEP
# Single staircase sim
# Pablo Bravo ptbravo@uc.cl
import numpy as np
import argparse
import matplotlib.pyplot as plt
from scipy.stats import truncnorm
#Classes
class person():
# 'Particles' that move on staircases
# They can have different speeds
def __init__(self, id, av_speed, type):
self.speed = 0
self.av_speed = av_speed
self.lane = 0 # Lane on set
self.id = id # identification
self.start = 0 # timestep in which they joined staircase
self.end = 0 # timestep in which they finished staircase
self.type = type # Walker or stopper
self.stype = 0 # Normal or mechanical stair
def __str__(self):
id = 'id: '+ str(self.id)
av_speed = 'av_speed: ' + str(round(self.av_speed, 2))
p_type = 'type: ' + str(self.type)
return (id + '\t' + av_speed + '\t' + p_type)
def to_reserve(self, reserve, t):
# Sends particle to a reserve() and updates time
self.end = t
reserve.current.append(self.id)
def roll_speed(self):
# Proxy for average speed
if (self.type == 'S') and (self.stype == 'M'):
self.speed = 0
else:
delta = self.av_speed - 1
roll = np.random.rand()
if delta <= 0:
if roll <= np.abs(delta):
self.speed = 0
else:
self.speed = 1
if delta > 0:
if roll <= np.abs(delta):
self.speed = 2
else:
self.speed = 1
class reserve():
# Keeps track and sets order for incoming "waves of people"
def __init__(self, length, IN, OUT, reserves = []):
self.IN = IN
self.OUT = OUT
self.reserves = reserves
self.current = []
self.positions = np.zeros(n_of_persons)
self.positions += length
self.flux = 0
def update(self, t):
# Move particles on the reserve
temporal = []
for index in self.current:
self.positions[index - 1] -= P[index - 1].speed
for index in self.current:
if self.positions[index - 1] <= 0:
temporal.append(index)
self.current.remove(index)
self.positions[index - 1] = 0
self.flux += len(temporal)
self.OUT.feed(temporal, t)
def platform(self, N, train_doors, door_pos):
# Generate intiial distribution
for p in P:
self.current.append(p.id)
doors = list(np.arange(8)+1)
D = []
for p in self.current:
dist = np.abs(4*np.random.choice(doors) - door_pos)
D.append(dist)
self.positions = np.asarray(D)
class staircase_set():
def __init__(self, length = 40, id = 1, mspeed = [1,1,0,0,0,0]):
self.length = length
self.mspeed = np.asarray(mspeed) # Mechanical staircase speed
self.matrix = np.zeros([len(self.mspeed), self.length], int)
self.id = id
self.wait_line = []
def max_mov(self,l, i, id):
# Calculates the position that a particle can move without "jumping"
if id == 0:
tspeed = self.mspeed[l]
else:
tspeed = P[int(id-1)].speed + self.mspeed[l] #id[1..N],list[0,,N.1]
mov = 0
if tspeed != 0:
seg = self.matrix[l][i+1: i+tspeed+1]
for j in seg:
if j != 0:
break
else:
mov += 1
return mov
def update(self, r_in, r_out, t):
# Updating of positions should take in account speed of walker and
# speed of mechanical stair
for l in range(len(self.mspeed)): #For each lane
# End position l
if self.matrix[l, self.length - 1] != 0:
if np.random.rand() <= Beta:
value = self.matrix[l, self.length - 1]
P[value - 1].to_reserve(r_out, t)
self.matrix[l, self.length - 1] = 0
# Update middle
for i in reversed(range(0, self.length - 1)): #Middle of stair
pmov = self.max_mov(l, i, self.matrix[l,i])
if np.random.rand() <= p_move:
if pmov != 0:
self.matrix[l, i + pmov] = self.matrix[l, i]
self.matrix[l,i] = 0
def walkers(self):
# Types of walkers in staircase_set()
n = 0
m = 0
for i in range(len(self.mspeed)):
for j in range(self.length):
if self.mspeed[i] == 1:
if self.matrix[i,j] != 0:
m += 1
if self.mspeed[i] == 0:
if self.matrix[i,j] != 0:
n += 1
return n,m
def feed(self, lista, t):
# Fills the beginning positions, waitlist is only for mechanical stairs
# The persons in the lista roll for waitlist or normal stairs.
for index in lista:
P[index-1].start = t
# Fill Mechanical stairs
for f in range(len(self.mspeed)):
if (self.matrix[f,0] == 0) and (self.mspeed[f] == 1):
if self.wait_line != []:
self.matrix[f,0] = self.wait_line.pop(0)
P[self.matrix[f,0] - 1].lane = f
P[self.matrix[f,0] - 1].stype = 'M'
if (lista != []) and (self.matrix[f,0] == 0):
self.matrix[f,0] = lista.pop(0)
P[self.matrix[f,0] - 1].lane = f
P[self.matrix[f,0] - 1].stype = 'M'
while lista != []: # Iterate until we clear it
roll = np.random.rand()
if roll > w:
cont = 1
for l in range(len(self.mspeed)):
if cont == 1:
if (self.matrix[l,0] == 0) and (self.mspeed[l] == 0):
self.matrix[l,0] = lista.pop(0)
P[self.matrix[l,0] - 1].lane = l
P[self.matrix[l,0] - 1].stype = 'N'
cont = 0
if cont == 1:
v = lista.pop(0)
self.wait_line.append(v)
else:
v = lista.pop(0)
self.wait_line.append(v)
def generate_persons(N):
# Generate persons
P = [] # List of all the people in simulation
person_type = [] # List of person types
speed_distr = np.random.normal(1, 0.15, n_of_persons)
for i in range(n_of_persons):
roll = np.random.rand()
if roll <= s:
person_type.append('S')
else:
person_type.append('W')
for i in reversed(range(1, n_of_persons)):
P.append(person(i, speed_distr[i], person_type[i]))
return P
""" MAIN MAIN MAIN MAIN """
# Argparse variables
parser = argparse.ArgumentParser()
parser.add_argument("stair_length", help = "Steps of stair", type=int)
parser.add_argument("w", help = "Probability of going to waitline", type=float)
#parser.add_argument("s", help = "Probability of stopper", type = float)
args = parser.parse_args()
# Variables
alpha = 1
Beta = 1
p_move = 1
w = args.w
s = 0
#s = args.s
n_of_persons = 300
stair_length = args.stair_length
T = 800
iterations = 20
#outfile = outfile to export data
Starting_times = np.zeros([iterations, n_of_persons])
Ending_times = np.zeros([iterations, n_of_persons])
Mech_start = []
Mech_end = []
Norm_start = []
Norm_end = []
for iter in range(iterations):
# Generate things for simulation
P = generate_persons(n_of_persons) #list of persons
S1 = staircase_set(stair_length)
R0 = reserve(20, [], S1)
R1 = reserve(20, S1, [])
R0.platform(n_of_persons,4,1) # Generate platform in starting reserve
# Update in time
for t in range(T):
# Update simulation
for persona in P:
persona.roll_speed()
S1.update(R0, R1, t)
R0.update(t)
# Get starting and finishing times
for p in P:
Starting_times[iter, p.id-1] = p.start
Ending_times[iter, p.id-1] = p.end
if p.stype == 'N':
Norm_start.append(p.start)
Norm_end.append(p.end)
if p.stype == 'M':
Mech_start.append(p.start)
Mech_end.append(p.end)
# To arrays
Norm = np.asarray([Norm_start, Norm_end])
Mech = np.asarray([Mech_start, Mech_end])
#np.savez(outfile, [stair_length, w], Norm, Mech) #Export
plt.figure()
plt.title('N:'+str(n_of_persons)+' '+'L:'+str(stair_length)+ ' '+'w:'+str(w))
plt.scatter(Norm[0], Norm[1] - Norm[0], label = "Normal", alpha = 0.3)
plt.scatter(Mech[0], Mech[1] - Mech[0], label = "Mechanical", alpha = 0.3)
plt.legend()
plt.xlabel("Time to door")
plt.ylabel("Travel time")
plt.show()