Skip to content

Instantly share code, notes, and snippets.

@parsons-Jsr
Created November 9, 2012 02:20
Show Gist options
  • Save parsons-Jsr/4043318 to your computer and use it in GitHub Desktop.
Save parsons-Jsr/4043318 to your computer and use it in GitHub Desktop.
Python Queue Similation (Python COMPSCI Coursework)
import sys, os
import math
import random
def nextTime(mean):
from random import choice
# Giving x a random number from 0 to 1
x = random.random()
# return -alpha ln(1-x)
# where (mean = alpha) (ln = math.log)
return -mean * math.log(1-x)
#Checking code works:
# Create an empty variable to store average
# Add value of nextTime to average
# Hence making nt_average = nextTime
# Divide total by number of runs to get the value
# Print result
# Loop this loop 5 times for accuracy (hence total of 50,000 simulations)
nt_average = 0.0
print "Q1.(a). Where mean = 1"
for i in range (5):
for i in range (10000):
nt_average += nextTime(1)
nt_average = nt_average/10000
#for i in range (5):
print "Result: %s" % nt_average
print " "
print "Q1.(a). Where mean = 10"
for i in range (10000):
nt_average += nextTime(10)
nt_average = nt_average/10000
for i in range (5):
print "Result: %s" % nt_average
print " "
print "Q1.(a). Where mean = 50"
for i in range (10000):
nt_average += nextTime(50)
nt_average = nt_average/10000
for i in range (5):
print "Result: %s" % nt_average
print " "
print "Q1.(a). Where mean = 100"
for i in range (10000):
nt_average += nextTime(100)
nt_average = nt_average/10000
for i in range (5):
print "Result: %s" % nt_average
print " "
print " "
def SingleQueueSimulation(alpha, beta, time):
# alpha is average time for arival
# beta is average time for service
# c = curent time in mins
# ta = time to the next customer arival
# ts = time until current customer is served
# q = current length of queue
new_q = 0.0
c = 0.0
ta = 0.0
ts = 0.0
q = 1.0
while c < time:
if ta < ts:
ts -= ta
c = c + ta
q += 1
ta = nextTime(alpha)
else:
ta -= ts
c += ts
q -= 1
ts = nextTime(beta)
if q == 0:
c = c + ta
q += 1
ta = nextTime(alpha)
# If queue length becomes bigger than the initial queue length
# Then make the initial queue length become this new length
# Don't need else on "if q ==0" because if q != 0 then the loop repeats
if q > new_q:
new_q = q
return new_q
# Testing test data (email) (20 experiements)
# alpha 20, beta 5, time 480...expected ~2.85
# alpha 20, beta 10, time 480...expected ~4.55
# Testing data from question (10,000 experiments)
# alpha 10, beta 4, time 480
# alpha 10, beta 2, time 480
# alpha 5, beta 4, time 480
# alpha 5, beta 2, time 480
sqs_average = 0.0
print "Q1.(b). Where (alpha = 20), (beta = 5), (time = 480), 20 experiments only"
for i in range (5):
for i in range (20):
sqs_average += SingleQueueSimulation(20, 5, 480)
sqs_average = sqs_average/20
print "Result: %s" % sqs_average
print " "
print "Q1.(b). Where (alpha = 20), (beta = 10), (time = 480), 20 experiments only"
for i in range (5):
for i in range (20):
sqs_average += SingleQueueSimulation(20, 10, 480)
sqs_average = sqs_average/20
print "Result: %s" % sqs_average
print " "
print "Q1.(b). Where (alpha = 10), (beta = 4), (time = 480)"
for i in range (5):
for i in range (10000):
sqs_average += SingleQueueSimulation(20, 5, 480)
sqs_average = sqs_average/10000
print "Result: %s" % sqs_average
print " "
print "Q1.(b). Where (alpha = 10), (beta = 2), (time = 480)"
for i in range (5):
for i in range (10000):
sqs_average += SingleQueueSimulation(20, 5, 480)
sqs_average = sqs_average/10000
print "Result: %s" % sqs_average
print " "
print "Q1.(b). Where (alpha = 5), (beta = 2), (time = 480)"
for i in range (5):
for i in range (10000):
sqs_average += SingleQueueSimulation(20, 5, 480)
sqs_average = sqs_average/10000
print "Result: %s" % sqs_average
print " "
print "Q1.(b). Where (alpha = 20), (beta = 5), (time = 480)"
for i in range (5):
for i in range (20):
sqs_average += SingleQueueSimulation(20, 5, 480)
sqs_average = sqs_average/20
print "Result: %s" % sqs_average
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment