Guest User

Untitled

a guest
May 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.00 KB | None | 0 0
  1. import math
  2. import random
  3.  
  4. #---------------QUESTION 1A---------------
  5. def nextTime(mean):
  6.     for i in range(mean):
  7.         x=random.random()
  8.         arrivaltime = (-(mean)) * (math.log(1-x))
  9.         print arrivaltime
  10.  
  11.    
  12. #---------------------------------------------------------------------------------------------------------------------------------------------------------------------
  13. #---------------QUESTION 1B---------------
  14.    
  15. def singlequeue():
  16.  
  17.     #Initialising the variables used for calculating the average
  18.     totalqueue=0
  19.     count=0
  20.  
  21.     #Asking the user what they wish to use for alpha and beta
  22.     a = raw_input('Please enter the average interarrival time (alpha): ')
  23.     b = raw_input('Please enter the average service times (beta): ')
  24.  
  25.     #Setting alpha and beta to integer numbers
  26.     a =int(a)
  27.     b=int(b)
  28.  
  29.     #For 1 -> n times, do
  30.     for i in range(1, 201):
  31.  
  32.         c=0                                 #c to store the current time        
  33.         ta=0                                #ta to store the time to the next customer arrival
  34.         ts=0                                #ts to store the time until the customer has finished being served
  35.         q=1                                 #Q to store the current length of the queue
  36.         maxqueue=0                          #maxqueue is used to show the maximum queue after the 8 hours
  37.  
  38.        
  39.         while c < 480:                      #While the time is less than 8 hours
  40.             if q!=0:                        #If the length of the queue is not 0
  41.            
  42.                 if ta<ts:
  43.                     ts = ts - ta
  44.                     c = c + ta
  45.                     q=q+1
  46.                     x=random.random()
  47.                     ta = -a * (math.log(1-x))
  48.  
  49.                     if maxqueue < q:
  50.                         maxqueue = q
  51.  
  52.                 else:              
  53.                     ta = ta - ts
  54.                     c = c + ts
  55.                     q=q-1
  56.                     x=random.random()
  57.                     ts = -b * (math.log(1-x))
  58.  
  59.        
  60.             elif q==0:                      #If the length of the queue is 0
  61.                 c = c +ta
  62.                 q=q+1
  63.                 x=random.random()
  64.                 ta = -a * (math.log(1-x))
  65.  
  66.                 if maxqueue < q:
  67.                     maxqueue = q
  68.  
  69.  
  70.         if totalqueue == 0:                 #If totalqueue equals 0, then the total queue is the maximum length
  71.             totalqueue = maxqueue
  72.         else:                               #If a totalqueue has already been set, then the maximum queue is simply added to the total
  73.             totalqueue = totalqueue + maxqueue
  74.  
  75.         count += 1                          #Counting how many times the simulation is run
  76.        
  77.     avgqueue = totalqueue / float(count)    #Working out that the average length is the totalqueue divided by the amount of times the simulation is run
  78.  
  79.     print 'Average queue was : ', avgqueue  #Printing the value of the Average Queue Length
Add Comment
Please, Sign In to add comment