Skip to content

Instantly share code, notes, and snippets.

@danong
Last active February 26, 2016 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danong/08c9efffeeec30ad429e to your computer and use it in GitHub Desktop.
Save danong/08c9efffeeec30ad429e to your computer and use it in GitHub Desktop.
Single server queue simulation
import random
import numpy as np
# for graphing
from mpl_toolkits.mplot3d.axes3d import Axes3D
import matplotlib.pyplot as plt
if __name__ == "__main__":
# setting up scatterplot
Xa = []
Ya = []
Za = []
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# iterate through various lambda variables
for lambd_in in np.arange(0.2,2,0.1):
for lambd_out in np.arange(0.2,2,0.1):
# define and initialize queue variables
T = 1000 # 'closing' time
t = 0 # time variable
na = 0 # number of arrivals (by time t)
nd = 0 # number of departures (by time t)
n = 0 # number of customers in the system at time t
td = float('inf') # service completion time of the customer presently being served
ta = random.expovariate(lambd_in) # time of the next arrival (after t)
D = [] # list of departure times
A = [] # list of customer arrival times
Tp = 0 # time past T that the last customer departs
max_line_length = 0 # maximum line length
while t < T or n > 0:
# case 1 - arrival before next departure
if ta <= td and ta <= T:
t = ta # move along to time ta
na += 1 # one additional arrival
n += 1 # one additional customer
# generate time of next arrival
ta = random.expovariate(lambd_in) + t
# generate time of departure if queue has been empty
if n == 1:
Y = random.expovariate(lambd_out)
td = t + Y
A.append(t)
# print("Arrival ", na, "at time ", t)
#case 2 - departure before next arrival
elif td < ta and td <= T:
# advance time to next departure
t = td
# one less person in line
n -= 1
# one more person served
nd += 1
# if queue is empty, infinite time of next departure
if n == 0:
td = float('inf')
# if queue isn't empty, generate next time of departure
else:
Y = random.expovariate(lambd_out)
td = t + Y
D.append(t)
# print("Departure ", nd, "at time ", t)
# case 3 - next arrival/departure happens after closing time and there are people still in queue
elif min(ta, td) > T and n > 0:
# advance time to next departure
t = td
# one less person in line
n -= 1
# one more person served
nd += 1
# if line isn't empty, generate time of next departure
if n > 0:
Y = random.expovariate(lambd_out)
td = t + Y
D.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(ta, td) > T and n == 0:
# calculate overtime
Tp = max(t-T, 0)
break
# update max_line_length
if n > max_line_length:
max_line_length = n
# save statistics to be graphed later
Xa.append(lambd_in)
Ya.append(lambd_out)
Za.append(max_line_length)
# display graph
ax.scatter(Xa, Ya, Za, c='r', marker='o')
ax.set_xlabel('Arrival Lambda')
ax.set_ylabel('Departure Lambda')
ax.set_zlabel('Maximum Line Length')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment