-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.py
More file actions
278 lines (195 loc) · 11.6 KB
/
Copy pathSimulator.py
File metadata and controls
278 lines (195 loc) · 11.6 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
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
__author__ = 'Sorostitude'
from theater import Theater
from Clock import Clock
class Simulation:
def __init__(self):
self.length_of_simulation = 0
self.number_of_ticket_windows = 0
self.number_of_ticket_lines = 0
self.processing_time_for_window = []
self.number_of_customers = []
self.customer_arrival_time = []
self.theater = Theater(None, None)
self.individual_ticket_window = None
self.customer_has_arrived_in_line = False
# Window statistics
self.number_of_tickets_sold_at_window = []
# Figure out what to do with this.
self.idle_time = []
self.length_of_lines_at_the_end = 0
# This takes a look at our file, 'simulation' which describes the parameters needed
# for the simulation to run.
def parse_file(self):
sim = open('simulation.txt', 'r') # open the file with 'read'
file_list = sim.readlines() # file_list acts as our list of lines (each line composes a list of characters)
self.length_of_simulation = int((file_list[1])) * 60
self.number_of_ticket_windows = int(file_list[2])
self.number_of_ticket_lines = file_list[3].strip()
if self.number_of_ticket_lines == "S":
self.number_of_ticket_lines = 1
else:
self.number_of_ticket_lines = self.number_of_ticket_windows
# There are two ticket windows, so two processing line times.
# It still adds the newline character...
for n in file_list[4:4+self.number_of_ticket_windows]:
self.processing_time_for_window.append(int(n))
# So all we care about is the 6th index and the 7th index.
# If file_list is a list and the list contains strings...
current_position_in_list = file_list[4+self.number_of_ticket_windows:]
for n in current_position_in_list:
if n == 'END':
pass
else:
self.number_of_customers.append(int(n[0]))
self.customer_arrival_time.append(int(n[2:5]))
# Loads the appropriate information, given the data from our file
# and runs the simulation loop.
def run_simulation(self):
clock = Clock()
# Read the file.
self.parse_file()
# Set the processing times.
self.set_processing_times()
self.theater.add_lines()
# Jumps the simulation forward to when customers first arrive.
clock.time = self.customer_arrival_time[0]
print "Time %d - Simulation has started" % clock.time
for q in range(len(self.theater.ticket_window_list)):
self.idle_time.append(clock.time)
# While the program hasn't reached the end (simulation time)...
while clock.time != self.length_of_simulation + 1:
# Loop through the windows
for n in range(len(self.theater.ticket_window_list)):
# Set the processing time for each window to a local variable.
original_process_time = self.processing_time_for_window[n] - 1
# Check for the number of lines; 1 vs. n lines
if self.number_of_ticket_lines == 1:
y = 0
else:
y = n
# ISSUE:
# Customer gets added to the wrong window.
# This is a problem with matching index and windows inside move_customer() I think.
# If we expect arrivals and the customer(s) haven't been added to the window yet (double-counting).
if clock.time == self.customer_arrival_time[0] and self.customer_has_arrived_in_line is False:
# Move the customers to the appropriate lines
self.theater.delegate_line(self.number_of_customers[0])
self.customer_has_arrived_in_line = True
# For every customer, print how many have been added.
for q in range(self.number_of_customers[0]):
print "Time %d - Customer entered line %d" % \
(clock.time, self.theater.ticket_line_list[y].line_number)
# If there is no one at the window and we can add customers to the line (length >=1),
# move the customer from the line to the window.
# 1)
if self.theater.ticket_window_list[n].customer_occupied_window is False:
self.theater.move_customer()
print "Time %d - Customer exited line %d" % \
(clock.time, self.theater.ticket_line_list[y].line_number)
print "Time %d - Customer entered window %d" % \
(clock.time, self.theater.ticket_window_list[n].window_number)
self.theater.ticket_window_list[n].process_time -= 1
# Given that we add customers to the line, we already prove that len >= 1;
# If there is someone at the window, check to see if they're done. If
# they're not done, decrement the processing time for that window.
# 2)
elif self.theater.ticket_window_list[n].customer_occupied_window is True and \
self.theater.ticket_window_list[n].process_time != 0:
self.theater.ticket_window_list[n].process_time -= 1
# Given that we add customers to the line, we already prove that len >= 1
# If there is someone at the window, check to see if they're done. If
# they are done, remove them from the window and add someone from line to window.
# Reset the processing time for that window and decrement it, since one second has passed.
# 3)
elif self.theater.ticket_window_list[n].customer_occupied_window is True and \
self.theater.ticket_window_list[n].process_time == 0:
self.theater.move_customer()
print "Time %d - Customer left window %d" % \
(clock.time, self.theater.ticket_window_list[n].window_number)
print "Time %d - Customer exited line %d" % \
(clock.time, self.theater.ticket_line_list[y].line_number)
print "Time %d - Customer entered window %d" % \
(clock.time, self.theater.ticket_window_list[n].window_number)
self.theater.ticket_window_list[n].process_time = original_process_time
self.theater.ticket_window_list[n].process_time -= 1
# 4)
else:
self.idle_time[n] += 1
else:
# 1)
if self.theater.ticket_window_list[n].customer_occupied_window is False and \
len(self.theater.ticket_line_list[y]) >= 1:
self.theater.move_customer()
print "Time %d - Customer exited line %d" % \
(clock.time, self.theater.ticket_line_list[y].line_number)
print "Time %d - Customer entered window %d" % \
(clock.time, self.theater.ticket_window_list[n].window_number)
self.theater.ticket_window_list[n].process_time -= 1
# 2)
elif self.theater.ticket_window_list[n].customer_occupied_window is True and \
self.theater.ticket_window_list[n].process_time != 0:
self.theater.ticket_window_list[n].process_time -= 1
# 3)
elif self.theater.ticket_window_list[n].customer_occupied_window is True and \
self.theater.ticket_window_list[n].process_time == 0 and \
len(self.theater.ticket_line_list[y]) >= 1:
self.theater.move_customer()
print "Time %d - Customer left window %d" % \
(clock.time, self.theater.ticket_window_list[n].window_number)
print "Time %d - Customer exited line %d" % \
(clock.time, self.theater.ticket_line_list[y].line_number)
print "Time %d - Customer entered window %d" % \
(clock.time, self.theater.ticket_window_list[n].window_number)
self.theater.ticket_window_list[n].process_time = original_process_time
# 4)
elif self.theater.ticket_window_list[n].customer_occupied_window is True and \
self.theater.ticket_window_list[n].process_time == 0 and \
len(self.theater.ticket_line_list[y]) == 0:
self.theater.move_customer()
print "Time %d - Customer left window %d" % \
(clock.time, self.theater.ticket_window_list[n].window_number)
self.idle_time[n] += 1
# 5)
else:
self.idle_time[n] += 1
if len(self.theater.ticket_line_list[y]) >= 1 and clock.time == self.length_of_simulation:
self.length_of_lines_at_the_end += len(self.theater.ticket_line_list[y])
else:
pass
self.customer_has_arrived_in_line = False
# This is a way to control iterations such that we get rid of a list element
# once an arrival time happens.
if clock.time == self.customer_arrival_time[0] and len(self.customer_arrival_time) >= 2:
self.customer_arrival_time.pop(0)
self.number_of_customers.pop(0)
else:
pass
# Increment the time after we check all the windows.
clock.time += 1
def set_processing_times(self):
# Initialize the theater object with the right amount of lines/windows
self.theater = Theater(self.number_of_ticket_lines, self.number_of_ticket_windows)
# Add the lines and the windows
self.theater.add_windows()
for n in range(len(self.theater.ticket_window_list)):
# For every ticket window we have, set the processing
# time equivalent to the processing time attribute that Windows have.
self.theater.ticket_window_list[n].process_time = self.processing_time_for_window[n]
return self.theater.ticket_window_list
def statistics_for_simulation(self):
# Window loop
for a in range(len(self.theater.ticket_window_list)):
self.number_of_tickets_sold_at_window.append(
self.theater.ticket_window_list[a].tickets_sold())
print "Window %d" % self.theater.ticket_window_list[a].window_number
print "Tickets sold: %d" % self.number_of_tickets_sold_at_window[a]
print "Idle time: %.0f%%" % (100 * (float(self.idle_time[a]) / self.length_of_simulation))
for x in range(len(self.theater.ticket_line_list)):
print "Line %d" % self.theater.ticket_line_list[x].line_number
print "Customers waiting at simulation end: %d" % self.length_of_lines_at_the_end
def main():
simulation = Simulation()
simulation.run_simulation()
simulation.statistics_for_simulation()
if __name__ == "__main__":
main()