Created
February 28, 2016 23:18
-
-
Save danong/32d162d3b9aec5739a62 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
if __name__ == "__main__": | |
# define and initialize queue variables | |
lambd_in = 0.5 | |
lambd_out = 0.4 | |
closing_time = 100 | |
t = 0 | |
num_arrivals = 0 | |
num_departures = 0 | |
n = 0 | |
time_depart = float('inf') | |
time_arrive = random.expovariate(lambd_in) | |
departures = [] | |
arrivals = [] | |
overtime = 0 | |
max_line_length = 0 | |
while t < closing_time or n > 0: | |
# case 1 - arrival before next departure | |
if time_arrive <= time_depart and time_arrive <= closing_time: | |
t = time_arrive # move along to time ta | |
num_arrivals += 1 # one additional arrival | |
n += 1 # one additional customer | |
# generate time of next arrival | |
time_arrive = random.expovariate(lambd_in) + t | |
# generate time of departure if queue has been empty | |
if n == 1: | |
Y = random.expovariate(lambd_out) | |
time_depart = t + Y | |
arrivals.append(t) | |
print("Arrival ", num_arrivals, "at time ", t) | |
# case 2 - departure before next arrival | |
elif time_depart < time_arrive and time_depart <= closing_time: | |
# advance time to next departure | |
t = time_depart | |
# one less person in line | |
n -= 1 | |
# one more person served | |
num_departures += 1 | |
# if queue is empty, infinite time of next departure | |
if n == 0: | |
time_depart = float('inf') | |
# if queue isn't empty, generate next time of departure | |
else: | |
Y = random.expovariate(lambd_out) | |
time_depart = t + Y | |
departures.append(t) | |
print("Departure ", num_departures, "at time ", t) | |
# case 3 - next arrival/departure happens after closing time and there are people still in queue | |
elif min(time_arrive, time_depart) > closing_time and n > 0: | |
# advance time to next departure | |
t = time_depart | |
# one less person in line | |
n -= 1 | |
# one more person served | |
num_departures += 1 | |
# if line isn't empty, generate time of next departure | |
if n > 0: | |
Y = random.expovariate(lambd_out) | |
time_depart = t + Y | |
departures.append(t) | |
# print("Departure ", nd, "at time ", t) | |
# case 4 - next arrival/departure happens after closing time and there is nobody left in the queue | |
elif min(time_arrive, time_depart) > closing_time and n == 0: | |
# calculate overtime | |
overtime = max(t-closing_time, 0) | |
break | |
print("Overtime: ", overtime) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment