Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Import neccessary libraries needed
- from question1and2 import singleQueue,nextTime # Imports the singleQueue function from the 1st set of tasks.
- import math
- import random
- import matplotlib.pyplot as bplot
- import numpy as np
- def DoubleQueue(alpha, beta, time=480):
- #Assign initial variables
- ta = 0
- ts = 0
- ts2 = 0 # A secondary variable to measure mean time until the next customer is served by the other teller
- c = 0
- maxQ = 0
- Q = 1
- Q2 = 1 #A secondary Q value is created to measure the length of the second queue
- while c < time: #Following the flow chart, checks if c < simtime
- if ta < ts:#Following the flow chart, the code nested in this condition is executed if ta < ts
- if Q <= Q2:
- ts2 = ts2 - ta
- ts = ts - ta
- c = c + ta
- Q = Q + 1
- else:
- ts2 = ts2 - ta
- ts = ts - ta
- c = c + ta
- Q2 = Q2 + 1
- if (Q > Q2 and Q > maxQ): # Condition to update maxQ, if the value of Q exceeds that of maxQ
- maxQ = Q # Updates the value of maxQ to the value of Q
- elif (Q2 > Q and Q2 > maxQ):
- maxQ = Q2
- ta = nextTime(alpha)
- else:
- if ts2 < ts:
- ta = ta - ts
- c = c + ts
- Q2 = Q2 - 1
- else:
- ta = ta - ts
- c = c + ts
- Q = Q - 1
- ts = nextTime(beta)
- if Q == 0:
- c = c + ta
- Q = Q + 1
- if Q > maxQ:
- maxQ = Q
- ta = nextTime(alpha)
- if Q2 == 0:
- c = c + ta
- Q2 = Q2 +1
- if Q2 > maxQ:
- maxQ = Q2
- ta = nextTime(alpha)
- return maxQ
- def doubleTellerSingleQueue(alpha, beta, time=480):
- ta = 0
- ts1 = 0
- ts2 = 0
- c = 0
- maxQ = 0
- Q = 1
- while c < time:
- if ta < ts1 and ta < ts2:
- ts1 -= ta
- ts2 -= ta
- c += ta
- Q += 1
- if Q > maxQ:
- maxQ = Q
- ta = nextTime(alpha)
- else:
- if ts1 < ts2:
- ta -= ts1
- ts2 -= ts1
- c += ts1
- Q -= 1
- ts1 = nextTime(beta)
- else:
- ta -= ts2
- ts1 -= ts2
- c += ts1
- Q -= 1
- ts2 = nextTime(beta)
- while Q == 0:
- c = c + ta
- Q += 1
- if Q > maxQ:
- maxQ = Q
- ta = nextTime(alpha)
- return maxQ
- #Plot points on a graph
- #Assign graph parameters.
- bplot.axis('auto')
- bplot.title('The relationship between the introduction of a second teller and maximum queue times')
- bplot.ylabel('Max Queue length')
- bplot.xlabel('Alpha / Beta values')
- #Declare required variables for the graph plots.
- alphavars = np.arange(1.1,10,0.1) # Produces a numpy array known as alphavars which will store incrementing values by 0.1 between 1.1 and 10.
- betavars = np.arange(1,0,-0.1) # Produces a numpy array known as betavars which will store incrementing values by 0.1 between 1.1 and 10.
- plotpoints =[] # Resets the plotpoints array to empty.
- #Plot original values
- for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
- plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
- for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
- maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
- for i in range(0,500):
- maxQ_value += singleQueue(a,b) # Adds the result of singleQueue(a,b) to maxQ_value
- plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
- bplot.plot(alphavars,plotpoints, label = "Unaltered Queue") #Plots the mean averages generated against the alpha values, giving a unique label to each line.
- #NOTE: A lot of the below code is repeated and therefore follows the same principle as the above plotting, so the code is identical.
- #Plot machine introduction -30% reduction
- for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
- plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
- for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
- maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
- for i in range(0,500):
- maxQ_value += singleQueue(a,b*0.7) # Adds the result of singleQueue(a,b) to maxQ_value
- plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
- bplot.plot(alphavars,plotpoints, label = "Introduction of a machine into a single queue , 30% decreease") #Plots the mean averages generated against the alpha values, giving a unique label to each line.
- #Plot points for Two tellers, one queue
- for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
- plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
- for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
- maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
- for i in range(0,500):
- maxQ_value += doubleTellerSingleQueue(a,b) # Adds the result of singleQueue(a,b) to maxQ_value
- plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
- bplot.plot(alphavars,plotpoints, label = "Introduction of two tellers with one queue") #Plots the mean averages generated against the alpha values, giving a unique label to each line.
- #Plot points for worst case, two tellers, one queue
- for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
- plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
- for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
- maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
- for i in range(0,500):
- maxQ_value += doubleTellerSingleQueue(a,b*1.3) # Adds the result of singleQueue(a,b) to maxQ_value
- plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
- bplot.plot(alphavars,plotpoints, label = "Worst case with two tellers with one queue") #Plots the mean averages generated against the alpha values, giving a unique label to each line.
- #Plot points for Two tellers, Two queues
- #Plot points for worst case, two tellers, two queues
- for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
- plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
- for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
- maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
- for i in range(0,500):
- maxQ_value += DoubleQueue(a,b) # Adds the result of singleQueue(a,b) to maxQ_value
- plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
- bplot.plot(alphavars,plotpoints, label = "Worst case with two tellers with two queues") #Plots the mean averages generated against the alpha values, giving a unique label to each line.
- #Plot points for worst case,single teller, sinlge queue.
- for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
- plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
- for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
- maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
- for i in range(0,500):
- maxQ_value += singleQueue(a,b*1.3) # Adds the result of singleQueue(a,b) to maxQ_value
- plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
- bplot.plot(alphavars,plotpoints, label = "Worst case with a single teller with a single queue ") #Plots the mean averages generated against the alpha values, giving a unique label to each line.
- #Display graph
- bplot.legend() # Creates a legend / key for the graph
- bplot.show() # Displays the graph
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement