Advertisement
Guest User

3B FINAL

a guest
Dec 14th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.60 KB | None | 0 0
  1. #Import neccessary libraries needed
  2. from question1and2 import singleQueue,nextTime # Imports the singleQueue function from the 1st set of tasks.
  3. import math
  4. import random
  5. import matplotlib.pyplot as bplot
  6. import numpy as np
  7.  
  8. def DoubleQueue(alpha, beta, time=480):
  9.     #Assign initial variables
  10.     ta = 0
  11.     ts = 0
  12.     ts2 = 0 # A secondary variable to measure mean time until the next customer is served by the other teller
  13.     c = 0
  14.     maxQ = 0
  15.     Q = 1
  16.     Q2 = 1 #A secondary Q value is created to measure the length of the second queue
  17.     while c < time: #Following the flow chart, checks if c < simtime
  18.         if ta < ts:#Following the flow chart, the code nested in this condition is executed if ta < ts
  19.             if Q <= Q2:
  20.                 ts2 = ts2 - ta
  21.                 ts = ts - ta
  22.                 c = c + ta
  23.                 Q = Q + 1
  24.             else:
  25.                 ts2 = ts2 - ta
  26.                 ts = ts - ta
  27.                 c = c + ta
  28.                 Q2 = Q2 + 1
  29.             if (Q > Q2 and Q > maxQ): # Condition to update maxQ, if the value of Q exceeds that of maxQ
  30.                     maxQ = Q # Updates the value of maxQ to the value of Q
  31.             elif (Q2 > Q and Q2 > maxQ):
  32.                     maxQ = Q2
  33.             ta = nextTime(alpha)
  34.         else:
  35.             if ts2 < ts:
  36.                 ta = ta - ts
  37.                 c = c + ts
  38.                 Q2 = Q2 - 1
  39.             else:
  40.                 ta = ta - ts
  41.                 c = c + ts
  42.                 Q = Q - 1
  43.             ts = nextTime(beta)
  44.         if Q == 0:
  45.             c = c + ta
  46.             Q = Q + 1
  47.             if Q > maxQ:
  48.                 maxQ = Q
  49.             ta = nextTime(alpha)
  50.         if Q2 == 0:
  51.             c = c + ta
  52.             Q2 = Q2 +1
  53.             if Q2 > maxQ:
  54.                 maxQ = Q2
  55.             ta = nextTime(alpha)
  56.     return maxQ
  57. def doubleTellerSingleQueue(alpha, beta, time=480):
  58.     ta = 0
  59.     ts1 = 0
  60.     ts2 = 0
  61.     c = 0
  62.     maxQ = 0
  63.     Q = 1
  64.     while c < time:
  65.         if ta < ts1 and ta < ts2:
  66.             ts1 -= ta
  67.             ts2 -= ta
  68.             c += ta
  69.             Q += 1
  70.             if Q > maxQ:
  71.                 maxQ = Q
  72.             ta = nextTime(alpha)
  73.         else:
  74.             if ts1 < ts2:
  75.                 ta -= ts1
  76.                 ts2 -= ts1
  77.                 c += ts1
  78.                 Q -= 1
  79.                 ts1 = nextTime(beta)
  80.             else:
  81.                 ta -= ts2
  82.                 ts1 -= ts2
  83.                 c += ts1
  84.                 Q -= 1
  85.                 ts2 = nextTime(beta)
  86.         while Q == 0:
  87.             c = c + ta
  88.             Q += 1
  89.             if Q > maxQ:
  90.                 maxQ = Q
  91.             ta = nextTime(alpha)
  92.     return maxQ
  93.  
  94. #Plot points on a graph
  95.  
  96. #Assign graph parameters.
  97. bplot.axis('auto')
  98. bplot.title('The relationship between the introduction of a second teller and maximum queue times')
  99. bplot.ylabel('Max Queue length')
  100. bplot.xlabel('Alpha / Beta values')
  101.  
  102. #Declare required variables for the graph plots.
  103. 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.
  104. 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.
  105. plotpoints =[] # Resets the plotpoints array to empty.
  106.  
  107. #Plot original values
  108. for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
  109.     plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
  110.     for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
  111.         maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
  112.         for i in range(0,500):
  113.             maxQ_value += singleQueue(a,b) # Adds the result of singleQueue(a,b) to maxQ_value
  114.         plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
  115. bplot.plot(alphavars,plotpoints, label = "Unaltered Queue") #Plots the mean averages generated against the alpha values, giving a unique label to each line.
  116.  
  117. #NOTE: A lot of the below code is repeated and therefore follows the same principle as the above plotting, so the code is identical.
  118.  
  119. #Plot machine introduction -30% reduction
  120. for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
  121.     plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
  122.     for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
  123.         maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
  124.         for i in range(0,500):
  125.             maxQ_value += singleQueue(a,b*0.7) # Adds the result of singleQueue(a,b) to maxQ_value
  126.         plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
  127. 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.
  128.  
  129. #Plot points for Two tellers, one queue
  130. for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
  131.     plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
  132.     for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
  133.         maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
  134.         for i in range(0,500):
  135.             maxQ_value += doubleTellerSingleQueue(a,b) # Adds the result of singleQueue(a,b) to maxQ_value
  136.         plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
  137. 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.
  138. #Plot points for worst case, two tellers, one queue
  139. for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
  140.     plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
  141.     for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
  142.         maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
  143.         for i in range(0,500):
  144.             maxQ_value += doubleTellerSingleQueue(a,b*1.3) # Adds the result of singleQueue(a,b) to maxQ_value
  145.         plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
  146. 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.
  147. #Plot points for Two tellers, Two queues
  148.  
  149. #Plot points for worst case, two tellers, two queues
  150. for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
  151.     plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
  152.     for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
  153.         maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
  154.         for i in range(0,500):
  155.             maxQ_value += DoubleQueue(a,b) # Adds the result of singleQueue(a,b) to maxQ_value
  156.         plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
  157. 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.
  158.  
  159. #Plot points for worst case,single teller, sinlge queue.
  160. for b in betavars: # Declares a for loop to iterate over all of the values in betavars.
  161.     plotpoints = [] # Assigns the array plotpoints to be empty. This is so a new line is plotted per iteration of b.
  162.     for a in alphavars: #Declares a for loop to iterate over all of the values in alphavars.
  163.         maxQ_value = 0 # Sets / Resets the maxQ value to 0, this is so a fresh mean can be set each iteration.
  164.         for i in range(0,500):
  165.             maxQ_value += singleQueue(a,b*1.3) # Adds the result of singleQueue(a,b) to maxQ_value
  166.         plotpoints.append(maxQ_value / 500) # Adds the mean average of the iteration to a list
  167. 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.
  168.  
  169. #Display graph
  170. bplot.legend() # Creates a legend / key for the graph
  171. bplot.show() # Displays the graph
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement