-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintersection.sage
265 lines (242 loc) · 9.13 KB
/
intersection.sage
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
# Pedestrians and cars
from sage.plot.plot import adjust_figsize_for_aspect_ratio
adjust_figsize_for_aspect_ratio([3,5], 2, 0, 1000, 0, 2)
# Data structure for this intersection for cars is:
# South-to-north, south-to-east, west-to-east.
# Structure for pedestrians is:
# South-to-north(east side), north-to-south(east side),
# east-to-west(north side), west-to-east(north side),
# north-to-south(west side), south-to-north(west side),
# west-to-east(south side), east-to-west(south side)
# This is an intersection of two one-way streets.
ped_arrival_distribution = [10, 10, 7, 5, 3, 2, 1, 1, 1, 1]
car_arrival_distribution = [10, 10, 7, 5, 3, 2, 1, 1, 1, 1]
X = GeneralDiscreteDistribution(ped_arrival_distribution)
Y = GeneralDiscreteDistribution(car_arrival_distribution)
plot_collection = []
set_random_seed( 0 )
cars = {'stn': [[],0,0], 'ste': [[],0,0], 'wte': [0,0,0]}
peds = {'stn_e': [0,0,0],'nts_e': [0,0,0], 'etw_n': [0,0,0], 'wte_n': [0,0,0], 'nts_w': [0,0,0],'stn_w': [0,0,0], 'wte_s': [0,0,0], 'etw_s': [0,0,0]}
overview = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
plots = []
matrices = []
iterations = 2
green_ticks = 15
red_ticks = 5
flashing_ticks = 1
car_green_ticks = green_ticks + flashing_ticks
yellow_ticks = red_ticks
def queuer():
if randint(0,1):
cars['stn'][0].insert(0,1)
cars['ste'][0].insert(0,0)
else:
cars['stn'][0].insert(0,1)
cars['ste'][0].insert(0,0)
cars['wte'][0]+=1
for key in peds.keys():
peds[key][0]+=X.get_random_element()
def fill_overview():
global overview
overview = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
for key in peds.keys():
if key == 'stn_e':
overview[3][3] += peds[key][0]
overview[2][3] += peds[key][1]
#overview[0][3] += peds[key][2] # People in the third spot are finished and not waiting
elif key == 'nts_e':
overview[1][3] += peds[key][0]
overview[2][3] += peds[key][1]
#overview[4][3] += peds[key][2]
elif key == 'etw_n':
overview[1][3] += peds[key][0]
overview[1][2] += peds[key][1]
#overview[1][0] += peds[key][2]
elif key == 'wte_n':
overview[1][1] += peds[key][0]
overview[1][2] += peds[key][1]
#overview[1][4] += peds[key][2]
elif key == 'nts_w':
overview[1][1] += peds[key][0]
overview[2][1] += peds[key][1]
#overview[4][1] += peds[key][2]
elif key == 'stn_w':
overview[3][1] += peds[key][0]
overview[2][1] += peds[key][1]
#overview[0][1] += peds[key][2]
elif key == 'wte_s':
overview[3][1] += peds[key][0]
overview[3][2] += peds[key][1]
#overview[3][4] += peds[key][2]
else:
overview[3][3] += peds[key][0]
overview[3][2] += peds[key][1]
#overview[3][0] += peds[key][2]
for key in cars.keys():
if key == 'wte':
overview[2][0] += cars[key][0]
overview[2][2] += cars[key][1]
overview[2][4] += cars[key][2] # People in the third spot are finished and not waiting
elif key == 'stn':
overview[4][2] += len(cars[key][0])
overview[2][2] += cars[key][1]
overview[0][2] += cars[key][2]
else:
overview[4][2] += len(cars[key][0])
overview[2][2] += cars[key][1]
overview[2][4] += cars[key][2]
plot_M = matrix_plot( matrix(overview).matrix_from_rows_and_columns([0,1,2,3,4], [0,1,2,3,4]), cmap='Oranges', frame=false ) # , figsize=[7,7]
#show( plot_M )
plots.append(plot_M)
matrix_M = matrix(overview).matrix_from_rows_and_columns([0,1,2,3,4], [0,1,2,3,4])
matrices.append( matrix_M )
#print matrix_M
#print
def move_ped_into_street(dirs, mode):
threshold = 7 # Why should this be seven while that for moving out is only five?
if mode == 'flashing':
multiplier = .5
else:
multiplier = 1
for dir in dirs:
if peds[dir][0] > threshold:
peds[dir][0] -= threshold*multiplier
peds[dir][1] += threshold*multiplier
else:
peds[dir][1] += peds[dir][0]*multiplier
peds[dir][0] -= peds[dir][0]*multiplier
def move_ped_out_of_street(dirs):
threshold = 5
for dir in dirs:
if peds[dir][1] > threshold:
peds[dir][1] -= threshold
peds[dir][2] += threshold
else:
peds[dir][2] += peds[dir][1]
peds[dir][1] = 0
def move_car_into_street(dirs):
for dir in dirs:
if dir == 'wte':
if cars[dir][1] == 0 and cars[dir][0] != 0:
cars[dir][1] += 1
cars[dir][0] -= 1
else:
if cars['stn'][1] == 0 and cars['ste'][1] == 0 and cars[dir][0][len(cars[dir][0])-1] != 0:
cars[dir][1] += 1
cars[dir][0].pop()
def move_car_out_of_street(dirs):
for dir in dirs:
if dir == 'wte':
if cars[dir][2] == 0 and cars[dir][1] != 0:
cars[dir][2] += 1
cars[dir][1] -= 1
else:
if cars[dir][2] == 0 and cars[dir][1] != 0:
cars[dir][2] += 1
cars[dir][1] -= 1
for i in range(1,iterations):
queuer()
# Pedestrian walk
#print 'Green north and south'
for tick in range(1,green_ticks):
queuer()
move_ped_into_street( ('stn_e','nts_e','stn_w','nts_w'), 'green' )
fill_overview()
move_ped_out_of_street( ('stn_e','nts_e','stn_w','nts_w') )
fill_overview()
#print "Flashing red hand"
for tick in range(1,flashing_ticks):
queuer()
move_ped_into_street( ('stn_e','nts_e','stn_w','nts_w'), 'flashing' )
fill_overview()
move_ped_out_of_street( ('stn_e','nts_e','stn_w','nts_w') )
fill_overview()
#print "Red hand"
for tick in range(1,red_ticks):
queuer()
move_ped_out_of_street( ('stn_e','nts_e','stn_w','nts_w') )
fill_overview()
# Car drive
# East and west
for tick in range(1,green_ticks):
queuer()
move_car_into_street( ('wte',) )
fill_overview()
move_car_out_of_street( ('wte',) )
fill_overview()
#print "Yellow"
for tick in range(1,yellow_ticks):
queuer()
move_car_out_of_street( ('wte',) )
fill_overview()
# Pedestrian walk
#print 'Green east and west'
for tick in range(1,green_ticks):
queuer()
move_ped_into_street( ('etw_n','wte_n','wte_s','etw_s'), 'green' )
fill_overview()
move_ped_out_of_street( ('etw_n','wte_n','wte_s','etw_s') )
fill_overview()
#print "Flashing red hand"
for tick in range(1,flashing_ticks):
queuer()
move_ped_into_street( ('etw_n','wte_n','wte_s','etw_s'), 'flashing' )
fill_overview()
move_ped_out_of_street( ('etw_n','wte_n','wte_s','etw_s') )
fill_overview()
#print "Red hand"
for tick in range(1,red_ticks):
queuer()
move_ped_out_of_street( ('etw_n','wte_n','wte_s','etw_s') )
fill_overview()
# Car drive
# North and south
for tick in range(1,green_ticks):
queuer()
move_car_into_street( ('stn','ste') )
fill_overview()
move_car_out_of_street( ('stn','ste') )
fill_overview()
#print "Yellow"
for tick in range(1,yellow_ticks):
queuer()
move_car_out_of_street( ('stn','ste') )
fill_overview()
a = animate(plots)
a.show()
nw = []
ne = []
sw = []
se = []
avg = []
for m in matrices:
#print m[1,1]
nw.append( m[0,0] )
ne.append( m[0,2] )
sw.append( m[2,0] )
se.append( m[2,2] )
avg.append( ( m[0,0] + m[0,2] + m[2,0] + m[2,2] ) / 4 )
p0 = false
p1 = false
p2 = false
p3 = false
pavg = false
fig = 19
thick = .4
p0 = list_plot( nw, plotjoined=True, color=hue(.1), figsize=[fig,1], thickness=thick )
p1 = list_plot( ne, plotjoined=True, color=hue(.4), figsize=[fig,1], thickness=thick )
p2 = list_plot( sw, plotjoined=True, color=hue(.6), figsize=[fig,1], thickness=thick )
p3 = list_plot( se, plotjoined=True, color=hue(.8), figsize=[fig,1], thickness=thick )
print "All corners:"
pavg = list_plot( avg, plotjoined=True, color='black', figsize=[fig,1], thickness=thick )
(p0 + p1 + p2 + p3 + pavg).show()
"""print "NW corner:"
p0.show()
print "NE corner:"
p1.show()
print "SW corner:"
p2.show()
print "SE corner:"
p3.show()
print "AVG:"
pavg.show()"""