Bank_Customer_Arrival_Model
Queuing Simulation – Bank Customer Arrival Model¶
In this notebook we describe a simple bank customer arrival model.
Copyright By PowCoder代写 加微信 powcoder
We use Poisson process to model the arrival and departure times. In a Poission process the size of the interval between consecutive events is exponential.
random.expovariate generates random floating point numbers that are exponentialy distributed.
import random
#define and initialize the parameters of the Poisson distributions
lambd_in = 0.5
lambd_out = 0.4
# bank variables
closing_time = 100 #initialize the bank closing time
overtime = 0 #overtime the employees need to be paid for
# queue variables
num_arrivals = 0 #number of people in the queue
num_departures = 0 #number of people who have been served
n = 0 #length of the queue
max_line_length = 0 #the maximum length of the waiting line:
# time variables
t = 0 #set the time of first arrival to 0
time_depart = float(‘inf’) #set the first time of departure to infinity
time_arrive = random.expovariate(lambd_in) #generate the first arrival
Next we initialize two arrays to keep track of the customers who arrive and who depart:
departures = []
arrivals = []
We simulate the arrivals and departures in a bank branch using a while loop
while t < closing_time or n >= 0:
# case 1 – within business hours, a customer arrives before any customer leaves the queue
if time_arrive <= time_depart and time_arrive <= closing_time:
t = time_arrive # move time along to the time of the new arrival
num_arrivals += 1 # increase the number of customers with the additional arrival
n += 1 # we have an additional customer, increase the size of the waiting line by 1
# generate time of next arrival
time_arrive = random.expovariate(lambd_in) + t
#append the new customer to the arrival list
arrivals.append(t)
print("Arrival ", num_arrivals, "at time ", t)
# generate time of departure
if n == 1:
Y = random.expovariate(lambd_out)
time_depart = t + Y
print('Arrivals', arrivals)
print('Departures', departures)
# case 2 - within business hours, a customer departs before the next arrival
elif time_depart < time_arrive and time_depart <= closing_time:
# advance time to the next departure time
t = time_depart
# one more person served -> increase the count of clients who have been served
num_departures += 1
#update the departure list
departures.append(t)
print(“Departure “, num_departures, “at time “, t)
# one less person in line -> decrease the size of the waiting line
# if the queue is empt -> set the time of the next departure to infinity
if n == 0:
time_depart = float(‘inf’)
# if the queue isn’t empty, generate the next time of departure
Y = random.expovariate(lambd_out)
time_depart = t + Y
print(‘Arrivals’, arrivals)
print(‘Departures’, departures)
# case 3 – next arrival/departure happens after closing time and there are people still in the queue
elif min(time_arrive, time_depart) > closing_time and n > 0:
# advance time to next departure
t = time_depart
#update the departure list
departures.append(t)
#update the number of departures/clients served
num_departures += 1 # one more person served
print(“Departure “, num_departures, “at time “, t)
#update the queue
n -= 1 # one less person in the waiting line
# if line isn’t empty, generate the time of the next departure
Y = random.expovariate(lambd_out)
time_depart = t + Y
print(‘Arrivals’, arrivals)
print(‘Departures’, departures)
# 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)
print(‘Overtime = ‘, overtime)
print(‘Arrivals’, arrivals)
print(‘Departures’, departures)
Arrival 1 at time 1.8040884921326368
Arrival 2 at time 4.826150429117647
Departure 1 at time 5.015672378634762
Departure 2 at time 5.540799664417487
Arrival 3 at time 8.101042680573263
Arrival 4 at time 9.510040358540076
Departure 3 at time 9.83401364966088
Departure 4 at time 9.951292781086979
Arrival 5 at time 11.32553465817762
Departure 5 at time 14.44501856418869
Arrival 6 at time 23.37221552843263
Arrival 7 at time 27.345405300838
Arrival 8 at time 29.92824210501689
Departure 6 at time 31.628971795372834
Arrival 9 at time 31.64737629233889
Departure 7 at time 32.40957650601938
Departure 8 at time 33.12675699971574
Arrival 10 at time 35.56214175688245
Arrival 11 at time 35.89970511653362
Departure 9 at time 36.39293796881851
Arrival 12 at time 36.97255929113863
Arrival 13 at time 39.767543695236654
Arrival 14 at time 44.52173763425588
Arrival 15 at time 44.569443693066894
Arrival 16 at time 45.2414949064783
Departure 10 at time 46.612227160822066
Departure 11 at time 48.59640175108223
Departure 12 at time 49.444103880758036
Arrival 17 at time 49.9930097314598
Arrival 18 at time 51.088590911369174
Arrival 19 at time 53.5166606591768
Arrival 20 at time 53.569903103153
Arrival 21 at time 54.26740037208236
Departure 13 at time 57.90157216393853
Arrival 22 at time 58.23726634914982
Departure 14 at time 61.21039109598741
Departure 15 at time 63.10664260662026
Arrival 23 at time 64.24001471576784
Departure 16 at time 64.41625061812454
Arrival 24 at time 64.76735333845313
Arrival 25 at time 64.92343367792374
Arrival 26 at time 65.25635458736417
Arrival 27 at time 68.24058304643182
Arrival 28 at time 68.79349268017421
Departure 17 at time 71.31209436727781
Arrival 29 at time 71.79285274844868
Arrival 30 at time 72.39853463682391
Departure 18 at time 76.24223190125642
Arrival 31 at time 77.19200438872512
Arrival 32 at time 78.13695798207536
Arrival 33 at time 78.88822711390047
Departure 19 at time 80.21125964746977
Arrival 34 at time 81.0052667831326
Departure 20 at time 81.7986279559991
Arrival 35 at time 82.6147333497925
Departure 21 at time 84.02629530377475
Arrival 36 at time 84.98228979043103
Departure 22 at time 85.34843876761714
Departure 23 at time 85.5229087530495
Departure 24 at time 85.55589396935741
Departure 25 at time 88.33080101926888
Departure 26 at time 88.4017482583789
Arrival 37 at time 90.13586821633244
Arrival 38 at time 91.8168741090905
Departure 27 at time 92.59194336046386
Departure 28 at time 93.83304470870516
Arrival 39 at time 94.33036069516446
Arrival 40 at time 94.43811714053507
Arrival 41 at time 95.17641223464403
Departure 29 at time 96.23121981806622
Departure 30 at time 97.84774481251186
Departure 31 at time 99.15665730711055
Departure 32 at time 101.12759156215513
Departure 33 at time 103.08721516851584
Departure 34 at time 105.72135837301465
Departure 35 at time 109.86323123909935
Departure 36 at time 110.19244552143789
Departure 37 at time 111.59588710122362
Departure 38 at time 114.71876845650087
Departure 39 at time 118.23596933956057
Departure 40 at time 119.41427391948834
Departure 41 at time 130.86753992402845
Overtime = 30.86753992402845
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com