-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimulation.py
More file actions
245 lines (153 loc) · 6.96 KB
/
Simulation.py
File metadata and controls
245 lines (153 loc) · 6.96 KB
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
from Elevator import Elevator, Retail_Elevator, Residential_Elevator_Lower, Residential_Elevator_Luxury, Office_Elevator_Lower, Office_Elevator_Luxury, Mechanical_Elevator, Observatory_Elevator, Key_Card_Elevator
#from people import People, Group
#import queue
class Simulation:
#elevators = []
#elevators.append( Retail_Elevator )
#elevators.append( Residential_Elevator_Lower )
#elevators.append( Residential_Elevator_Luxury )
#elevators.append( Office_Elevator_Lower )
#elevators.append( Office_Elevator_Luxury )
#elevators.append( Mechanical_Elevator )
#elevators.append( Observatory_Elevator )
#elevators.append( Key_Card_Elevator )
AllWaitingList = [ [], [], [], [], [], [], [], [] ]
AllWaitingPickUp = [ [], [], [], [], [], [], [], [] ]
AllElevators = [ [], [], [], [], [], [], [], [] ]
cntTime = [ 0, 0, 0, 0, 0, 0, 0, 0]
cntTrips = [ 0, 0, 0, 0, 0, 0, 0, 0]
maxTime = [ 0, 0, 0, 0, 0, 0, 0, 0]
print Key_Card_Elevator.currentFloor
for it in range( 0, 11 ):
AllElevators[ 0 ].append( Key_Card_Elevator )
AllWaitingPickUp[ 0 ].append( [] )
for it in range( 0, 4 ):
AllElevators[ 1 ].append( Retail_Elevator )
AllWaitingPickUp[ 1 ].append( [] )
for it in range( 0, 7 ):
AllElevators[ 2 ].append( Office_Elevator_Lower )
AllWaitingPickUp[ 2 ].append( [] )
for it in range( 0, 3 ):
AllElevators[ 3 ].append( Residential_Elevator_Lower )
AllWaitingPickUp[ 3 ].append( [] )
for it in range( 0, 7 ):
AllElevators[ 4 ].append( Office_Elevator_Luxury )
AllWaitingPickUp[ 4 ].append( [] )
for it in range( 0, 2 ):
AllElevators[ 5 ].append( Residential_Elevator_Luxury )
AllWaitingPickUp[ 5 ].append( [] )
for it in range( 0, 2 ):
AllElevators[ 5 ].append( Observatory_Elevator )
AllWaitingPickUp[ 6 ].append( [] )
for it in range( 0, 4 ):
AllElevators[ 7 ].append( Mechanical_Elevator )
AllWaitingPickUp[ 7 ].append( [] )
flag = False
# Check from 7:00 to 10:00
for moment in range( 0, 1080 ):
if flag == False:
print "banter"
#str_test = raw_input()
#tim, current, destination, people, request = str_test.split()
else:
flag = False
#if tim != moment:
#flag = True
#continue
request = 5
WaitingList = AllWaitingList[ request ]
WaitingPickup = AllWaitingPickUp[ request ] # curent, destination, people
Elevators = AllElevators[ request ]
# DONT FORGET TO ADD IN
#waitingGroup = ( tim, current, destination, people )
#WaitingList.append( waitingGroup )
# Check all of the elevators
for typeElevator in range( 0, 7 ):
WaitingList = AllWaitingList[ typeElevator ]
WaitingPickup = AllWaitingPickUp[ typeElevator ]
Elevators = AllElevators[ typeElevator ]
elevatorsLen = len( Elevators )
# Have people get off at this floor
for idx in range( 0, elevatorsLen ):
particularElevator = Elevators[ idx ]
particularWaitingPickup = WaitingPickup[ idx ]
# Avoid checking elevators that are empty
if particularElevator.holding == 0:
# Update elevators that are empty, but have already been assigned somebody to pick up
if len( WaitingPickup[ idx ] ) > 0:
increment = particularWaitingPickup[ 0 ] - particularWaitingPickup[ 1 ]
particularElevator.currentFloor -= abs( increment ) / increment
continue
# Update current floor
if particularElevator.currentFloor < particularElevator.destinations[ 0 ][ 0 ]:
particularElevator.currentFloor = particularElevator.currentFloor - 1
else:
particularElevator.currentFloor = particularElevator.currentFloor + 1
# Simulate people getting off at this floor
for group in range( len( particularElevator.destinations ) ):
if particularElevator.destinations[ group ][ 0 ] == particularElevator.currentFloor:
particularElevator.holding = particularElevator.holding - particularElevator.destinations[ group ][ 1 ]
particularElevator.remove( ( particularElevator.destinations[ group ] ) )
particularElevator.doorsOpen = True
# Have people get on at this floor
for idx in range( 0, elevatorsLen ):
particularElevator = Elevators[ idx ]
particularWaitingPickup = WaitingPickup[ idx ]
# Check if the elevator can still carry more people, and if the elevator open
if particularElevator.holding == 12 or particularElevator.doorsOpen == False:
continue
# Check if the open elevator will pickup anybody on this floor
for group in range( 0, len( WaitingList ) ):
particularGroup = WaitingList[ group ]
# Check if elevator is currently on a floor with people waiting to be picked up
if particularElevator.currentFloor != particularGroup[ 0 ]:
continue
if len( particularElevator.destinations ) != 0:
dir1 = particularElevator.currentFloor - particularElevator.destinations[ 0 ][ 0 ]
dir2 = particularGroup[ 0 ] - particularGroup[ 1 ]
if dir1 != dir2:
continue
willGetOn = min( particularGroup[ 2 ], 12 - particularElevator.holding )
stillWaiting = particularGroup[ 2 ] - willGetOn
particularElevator.holding += willGetOn
particularElevator.destinations.append( ( particularGroup[ 1 ], willGetOn ) )
addTime = 10 * ( moment - particularGroup[ 0 ] + abs( particularGroup[ 1 ] - particularGroup[ 2 ] ) )
cntTime[ idx ] += addTime
cntTrips[ idx ] += 1
maxTime[ idx ] = max( maxTime[ idx ], addTime )
if stillWaiting == 0:
WaitingList[ idx ].remove( particularGroup )
else:
particularGroup[ 2 ] = stillWaiting
if particularElevator.holding == 12:
break
# Check if there are currently people waiting to be picked up that have not yet been assigned an elevator
# This also takes care of empty elevators
if len( WaitingList ) != 0:
# Make empty elevators head move towards people that have been waiting the longest (i.e. first element of WaitingList)
for idx in range( 0, elevatorsLen ):
particularElevator = Elevators[ idx ]
particularWaitingPickup = WaitingPickup[ idx ]
# Avoid considering elevators that are already occupied, or headed towards somebody
if len( particularElevator.destinations ) != 0 or len( particularWaitingPickup ) != 0:
continue
nextPickUp = -1
# Check to see if there are people waiting to be picked up, that have not yet been assigned an elevator
for PickUpIdx in range( 0, WaitingList ):
takenCareOf = False
for takenCareOfIdx in range( 0, len( Elevators ) ):
if WaitingPickup[ takenCareOfIdx ][ 0 ] == WaitingList[ PickUpIdx ]:
takenCareOf = True
break
if takenCareOf == True:
continue
nextPickUp = PickUpIdx
break
# If there are no people waiting to be picked up, or all of them have already been assigned an elevator
if nextPickUp == -1:
break
particularWaitingPickup.append( WaitingList[ nextPickUp ] )
increment = WaitingPickup[ 0 ][ 0 ] - WaitingPickup[ 0 ][ 1 ]
particularElevator.currentFloor -= abs( increment ) / increment
doorsOpen = False
print "PROJECT COMPILED!"