Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.83 KB | None | 0 0
  1. import random
  2. import math
  3. import csv
  4. #Graphs imports
  5. import matplotlib.pyplot as plt
  6. import numpy as npy
  7.  
  8.  
  9. def nextTime(mean):
  10.     return -mean * math.log(1 - random.random())
  11.  
  12.  
  13. def theoreticalMeanQueueLength(alpha, beta):
  14.     """
  15.    >>> theoreticalMeanQueueLength(10, 2)
  16.    0.25
  17.    >>> theoreticalMeanQueueLength(5, 1)
  18.    0.25
  19.    >>> theoreticalMeanQueueLength(4, 2)
  20.    1.0
  21.    >>> theoreticalMeanQueueLength(5.5, 1.3)
  22.    0.3095238095238095
  23.    >>> theoreticalMeanQueueLength(5.5, 0)
  24.    0.0
  25.    >>> theoreticalMeanQueueLength(1, 1)
  26.    -1
  27.    >>> type(theoreticalMeanQueueLength(10, 2))
  28.    <class 'float'>
  29.    """
  30.     #The initial if statement to adhere to the ZeroDivisionError, so it outputs -1 instead.
  31.     if beta == alpha:
  32.         return -1
  33.     #This will perform the equation if alpha != beta.
  34.     else:
  35.         return (beta/alpha)/(1-(beta/alpha))
  36.  
  37. def checkMean(mean, iterations=10000):
  38.     """
  39.    >>> random.seed(57)
  40.    >>> checkMean(5, 10)
  41.    6.309113224728108
  42.    >>> random.seed(57)
  43.    >>> checkMean(5, 1000)
  44.    4.973347344130324
  45.    >>> random.seed(57)
  46.    >>> checkMean(5, 100000)
  47.    4.988076126529703
  48.    >>> random.seed(57)
  49.    >>> checkMean(195, 100000)
  50.    194.53496893466047
  51.    >>> random.seed(57)
  52.    >>> checkMean(195)
  53.    196.71853828860912
  54.    >>> random.seed(57)
  55.    >>> checkMean(31)
  56.    31.273203522804728
  57.    >>> type(checkMean(31, 5))
  58.    <class 'float'>
  59.    """
  60.     #This sets the initial value to 0.
  61.     sum_x = 0
  62.     #Used to iterate for the data, based on the chosen amount of iteration in a step of 1.
  63.     for i in range(0, iterations, 1):
  64.     #Uses the premises of how mean is calculated and to make sure the data is outputted as a float.
  65.         sum_x += nextTime(mean)
  66.     return float(sum_x / iterations)
  67.  
  68.  
  69. def readExperimentParameters(filename):
  70.     """
  71.    >>> readExperimentParameters('experiments.csv')[0]
  72.    (10, 2, 480)
  73.    >>> len(readExperimentParameters('experiments.csv'))
  74.    5
  75.    >>> readExperimentParameters('experiments.csv')[3]
  76.    (20, 2, 480)
  77.    >>> readExperimentParameters('experiments.csv')[2]
  78.    (20, 15, 240)
  79.    >>> type(readExperimentParameters('experiments.csv')[1])
  80.    <class 'tuple'>
  81.    """
  82.     #This creates a temporary list that will be apended for the output.
  83.     output = []
  84.     with open('experiments.csv','r') as csvfile:
  85.         rdr = csv.reader(csvfile)
  86.         #This is used to skip the header, allowing the data to be read correctly in relation to the doctests.
  87.         next(rdr, None)        
  88.         for data in rdr:
  89.             #A temporary variable used for storing the appropriate values.
  90.             newData = []
  91.             #Used to iterate through the data for the chosen row.
  92.             for i in data:
  93.                 try:
  94.                     newData.append(int(i))
  95.                 except ValueError:
  96.                     #The if statement is used to correctly append the data if it needs a format conversion.
  97.                     if i == ' h':
  98.                         newData[2] = newData[2] * 60
  99.             #Used to append the output variable as the chosen format of a tuple, adding it to the list.
  100.             output.append(tuple(newData))
  101.     return output
  102.  
  103.  
  104.  
  105. def singleQueue(alpha, beta, time=480):
  106.     """
  107.    >>> random.seed(57)
  108.    >>> singleQueue(10, 3, 480)
  109.    3
  110.    >>> random.seed(101)
  111.    >>> singleQueue(5, 3, 480)
  112.    6
  113.    >>> random.seed(101)
  114.    >>> singleQueue(5, 3)
  115.    6
  116.    >>> random.seed(935)
  117.    >>> singleQueue(10, 9, 280)
  118.    10
  119.    >>> type(singleQueue(10, 9, 280))
  120.    <class 'int'>
  121.    """
  122.     #Sets the initial variables to correct starting point.
  123.     c = 0
  124.     ta = 0
  125.     ts = 0
  126.     Q = 1
  127.     maxQ = 0
  128.  
  129.     #Used to loop the first clause of the alogrithim until c < simTime is no.
  130.     while c < time:
  131.         #Asks if ta < ts to allow the code to follow the correct path.
  132.         if ta < ts:
  133.             #The code will go down the 'left path' updating all information and variables.
  134.             ts = ts - ta
  135.             c = c + ta
  136.             Q = Q + 1
  137.             #Used to update the max length of the queue.
  138.             if Q >maxQ:
  139.                 maxQ = Q
  140.             #Update a random time for the next customer arrival.
  141.             ta = nextTime(alpha)
  142.         else:
  143.             #Similair to the 'if' statement, however goes down the 'right path'.
  144.             ta = ta - ts
  145.             c = c + ts
  146.             Q = Q - 1
  147.             #Update the time to be served.
  148.             ts = nextTime(beta)
  149.         #To fulfill the final conditions of Q = 0?
  150.         while Q == 0:
  151.             c = c + ta
  152.             Q = Q + 1
  153.             if Q > maxQ:
  154.                 maxQ = Q
  155.             ta = nextTime(alpha)
  156.     return maxQ
  157.  
  158.  
  159. #Question 2:
  160.  
  161. #The stated range is 11 to 101 in a step of 1, however i/10 makes the range appropriate for the question.
  162. x = [i/10 for i in range(11,100,1)]
  163. #Used to plot y-coordinates based on an earlier algorithim increments of 1.
  164. y = [theoreticalMeanQueueLength(i,1) for i in x]
  165. #Used to plot the data, title and axis labels.
  166. plt.plot(x,y,'r')
  167. plt.title('Gap Between Customer Arrival vs Queue Length')
  168. plt.xlabel('Mean Gap Between Customer Arrival')
  169. plt.ylabel('Theoretical Mean Queue Length')
  170. #Provide a visual output of the graph.
  171. plt.show()
  172.  
  173. #Question 3:
  174.  
  175. #3a:
  176.  
  177. #Initial variables.
  178. #Crates an initial array of values from 1.1 - 10 in increments of 0.1, similair scenario for variable 'bet'.
  179. alp = npy.arange(1.1,10,0.1)
  180. bet = npy.arange(1,0,-0.1)
  181. #Used to create the base plot at 100%.
  182. percentage = 100
  183.  
  184. #Iterations of the curves
  185. for b in bet:
  186.     points = []
  187.     for a in alp:
  188.         maxQ = 0
  189.         for i in range(100):
  190.             maxQ += singleQueue(a,b)
  191.         points.append(maxQ / 100)
  192.     plt.plot(alp,points, label = "" + str(percentage) + "% of β")
  193.     #Used to plot further reducations in mean
  194.     percentage = percentage - 10
  195.  
  196. plt.title('Outcome of new equipment (reducing mean service per customer)')
  197. plt.xlabel('Average Customer Service Time')
  198. plt.ylabel('Mean Queue length')
  199. #Creates a legend for the graph, which is a key for the different curves.
  200. plt.legend()
  201. plt.show()
  202.  
  203. #3b:
  204.  
  205. def doubleQueue(alpha, beta, time=480):
  206.     ta = 0
  207.     ts1 = 0
  208.     ts2 = 0
  209.     c = 0
  210.     maxQ = 0
  211.     Q = 1
  212.     while c < time:
  213.         if ta < ts1 and ta < ts2:
  214.             ts = ts - ta
  215.             ts2 = ts2 - ta
  216.             c = c + ta
  217.             Q = Q + 1
  218.             if Q > maxQ:
  219.                 maxQ = Q
  220.             ta = nextTime(alpha)
  221.         else:
  222.             if ts1 < ts2:
  223.                 ta = ta - ts1
  224.                 ts2 = ts2 - ts1
  225.                 c = c + ts1
  226.                 Q = Q - 1
  227.                 ts1 = nextTime(beta)
  228.             else:
  229.                 ta = ta - ts2
  230.                 ts1 = ts1 - ts2
  231.                 c = c + ts1
  232.                 Q = Q - 1
  233.                 ts2 = nextTime(beta)
  234.         while Q == 0:
  235.             c = c + ta
  236.             Q = Q + 1
  237.             if Q > maxQ:
  238.                 maxQ = Q
  239.             ta = nextTime(alpha)
  240.     return maxQ
  241.  
  242. #3b:
  243.  
  244. #Plot points on a graph
  245. #Assign graph parameters
  246. plt.title('The relationship between the introduction of a second teller and maximum queue times')
  247. plt.ylabel('Max Queue length')
  248. plt.xlabel('Alpha / Beta values')
  249. #Declare required vars
  250. alphavars = npy.arange(1.1,10,0.1)
  251. betavars = npy.arange(1,0,-0.1)
  252. b = 1
  253. plotpoints =[]
  254.  
  255. for a in alphavars:
  256.         maxQ_value = 0
  257.         for i in range(100):
  258.             maxQ_value += singleQueue(a,b)
  259.         plotpoints.append(maxQ_value / 100)
  260.         b = b-0.1
  261. plt.plot(alphavars,plotpoints,label ="Unaltered Queue")
  262.  
  263. for a in alphavars:
  264.         maxQ_value = 0
  265.         for i in range(100):
  266.             maxQ_value += doubleQueue(a,b)
  267.         plotpoints.append(maxQ_value / 100)
  268.         b = b-0.1
  269. plt.plot(alphavars,plotpoints,label ="Double Queue")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement