Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import math
- import csv
- #Graphs imports
- import matplotlib.pyplot as plt
- import numpy as npy
- def nextTime(mean):
- return -mean * math.log(1 - random.random())
- def theoreticalMeanQueueLength(alpha, beta):
- """
- >>> theoreticalMeanQueueLength(10, 2)
- 0.25
- >>> theoreticalMeanQueueLength(5, 1)
- 0.25
- >>> theoreticalMeanQueueLength(4, 2)
- 1.0
- >>> theoreticalMeanQueueLength(5.5, 1.3)
- 0.3095238095238095
- >>> theoreticalMeanQueueLength(5.5, 0)
- 0.0
- >>> theoreticalMeanQueueLength(1, 1)
- -1
- >>> type(theoreticalMeanQueueLength(10, 2))
- <class 'float'>
- """
- #The initial if statement to adhere to the ZeroDivisionError, so it outputs -1 instead.
- if beta == alpha:
- return -1
- #This will perform the equation if alpha != beta.
- else:
- return (beta/alpha)/(1-(beta/alpha))
- def checkMean(mean, iterations=10000):
- """
- >>> random.seed(57)
- >>> checkMean(5, 10)
- 6.309113224728108
- >>> random.seed(57)
- >>> checkMean(5, 1000)
- 4.973347344130324
- >>> random.seed(57)
- >>> checkMean(5, 100000)
- 4.988076126529703
- >>> random.seed(57)
- >>> checkMean(195, 100000)
- 194.53496893466047
- >>> random.seed(57)
- >>> checkMean(195)
- 196.71853828860912
- >>> random.seed(57)
- >>> checkMean(31)
- 31.273203522804728
- >>> type(checkMean(31, 5))
- <class 'float'>
- """
- #This sets the initial value to 0.
- sum_x = 0
- #Used to iterate for the data, based on the chosen amount of iteration in a step of 1.
- for i in range(0, iterations, 1):
- #Uses the premises of how mean is calculated and to make sure the data is outputted as a float.
- sum_x += nextTime(mean)
- return float(sum_x / iterations)
- def readExperimentParameters(filename):
- """
- >>> readExperimentParameters('experiments.csv')[0]
- (10, 2, 480)
- >>> len(readExperimentParameters('experiments.csv'))
- 5
- >>> readExperimentParameters('experiments.csv')[3]
- (20, 2, 480)
- >>> readExperimentParameters('experiments.csv')[2]
- (20, 15, 240)
- >>> type(readExperimentParameters('experiments.csv')[1])
- <class 'tuple'>
- """
- #This creates a temporary list that will be apended for the output.
- output = []
- with open('experiments.csv','r') as csvfile:
- rdr = csv.reader(csvfile)
- #This is used to skip the header, allowing the data to be read correctly in relation to the doctests.
- next(rdr, None)
- for data in rdr:
- #A temporary variable used for storing the appropriate values.
- newData = []
- #Used to iterate through the data for the chosen row.
- for i in data:
- try:
- newData.append(int(i))
- except ValueError:
- #The if statement is used to correctly append the data if it needs a format conversion.
- if i == ' h':
- newData[2] = newData[2] * 60
- #Used to append the output variable as the chosen format of a tuple, adding it to the list.
- output.append(tuple(newData))
- return output
- def singleQueue(alpha, beta, time=480):
- """
- >>> random.seed(57)
- >>> singleQueue(10, 3, 480)
- 3
- >>> random.seed(101)
- >>> singleQueue(5, 3, 480)
- 6
- >>> random.seed(101)
- >>> singleQueue(5, 3)
- 6
- >>> random.seed(935)
- >>> singleQueue(10, 9, 280)
- 10
- >>> type(singleQueue(10, 9, 280))
- <class 'int'>
- """
- #Sets the initial variables to correct starting point.
- c = 0
- ta = 0
- ts = 0
- Q = 1
- maxQ = 0
- #Used to loop the first clause of the alogrithim until c < simTime is no.
- while c < time:
- #Asks if ta < ts to allow the code to follow the correct path.
- if ta < ts:
- #The code will go down the 'left path' updating all information and variables.
- ts = ts - ta
- c = c + ta
- Q = Q + 1
- #Used to update the max length of the queue.
- if Q >maxQ:
- maxQ = Q
- #Update a random time for the next customer arrival.
- ta = nextTime(alpha)
- else:
- #Similair to the 'if' statement, however goes down the 'right path'.
- ta = ta - ts
- c = c + ts
- Q = Q - 1
- #Update the time to be served.
- ts = nextTime(beta)
- #To fulfill the final conditions of Q = 0?
- while Q == 0:
- c = c + ta
- Q = Q + 1
- if Q > maxQ:
- maxQ = Q
- ta = nextTime(alpha)
- return maxQ
- #Question 2:
- #The stated range is 11 to 101 in a step of 1, however i/10 makes the range appropriate for the question.
- x = [i/10 for i in range(11,100,1)]
- #Used to plot y-coordinates based on an earlier algorithim increments of 1.
- y = [theoreticalMeanQueueLength(i,1) for i in x]
- #Used to plot the data, title and axis labels.
- plt.plot(x,y,'r')
- plt.title('Gap Between Customer Arrival vs Queue Length')
- plt.xlabel('Mean Gap Between Customer Arrival')
- plt.ylabel('Theoretical Mean Queue Length')
- #Provide a visual output of the graph.
- plt.show()
- #Question 3:
- #3a:
- #Initial variables.
- #Crates an initial array of values from 1.1 - 10 in increments of 0.1, similair scenario for variable 'bet'.
- alp = npy.arange(1.1,10,0.1)
- bet = npy.arange(1,0,-0.1)
- #Used to create the base plot at 100%.
- percentage = 100
- #Iterations of the curves
- for b in bet:
- points = []
- for a in alp:
- maxQ = 0
- for i in range(100):
- maxQ += singleQueue(a,b)
- points.append(maxQ / 100)
- plt.plot(alp,points, label = "" + str(percentage) + "% of β")
- #Used to plot further reducations in mean
- percentage = percentage - 10
- plt.title('Outcome of new equipment (reducing mean service per customer)')
- plt.xlabel('Average Customer Service Time')
- plt.ylabel('Mean Queue length')
- #Creates a legend for the graph, which is a key for the different curves.
- plt.legend()
- plt.show()
- #3b:
- def doubleQueue(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:
- ts = ts - ta
- ts2 = ts2 - ta
- c = c + ta
- Q = Q + 1
- if Q > maxQ:
- maxQ = Q
- ta = nextTime(alpha)
- else:
- if ts1 < ts2:
- ta = ta - ts1
- ts2 = ts2 - ts1
- c = c + ts1
- Q = Q - 1
- ts1 = nextTime(beta)
- else:
- ta = ta - ts2
- ts1 = ts1 - ts2
- c = c + ts1
- Q = Q - 1
- ts2 = nextTime(beta)
- while Q == 0:
- c = c + ta
- Q = Q + 1
- if Q > maxQ:
- maxQ = Q
- ta = nextTime(alpha)
- return maxQ
- #3b:
- #Plot points on a graph
- #Assign graph parameters
- plt.title('The relationship between the introduction of a second teller and maximum queue times')
- plt.ylabel('Max Queue length')
- plt.xlabel('Alpha / Beta values')
- #Declare required vars
- alphavars = npy.arange(1.1,10,0.1)
- betavars = npy.arange(1,0,-0.1)
- b = 1
- plotpoints =[]
- for a in alphavars:
- maxQ_value = 0
- for i in range(100):
- maxQ_value += singleQueue(a,b)
- plotpoints.append(maxQ_value / 100)
- b = b-0.1
- plt.plot(alphavars,plotpoints,label ="Unaltered Queue")
- for a in alphavars:
- maxQ_value = 0
- for i in range(100):
- maxQ_value += doubleQueue(a,b)
- plotpoints.append(maxQ_value / 100)
- b = b-0.1
- plt.plot(alphavars,plotpoints,label ="Double Queue")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement