9
Queuing Theory Project Professor : Tran Hoang Nguyen Student Name : Student ID :

MM2 Queue Project

Embed Size (px)

Citation preview

Page 1: MM2 Queue Project

Queuing Theory

Project

Professor : Tran Hoang NguyenStudent Name : Student ID :

M/M/2 Queueing System

Page 2: MM2 Queue Project

In this project, we simulate the M/M/2 ququeing system with the customer arrival process is Poisson process with rate λ and the service time follows the exponential distribution with the mean 1/μ. We define ρ=λ/2 μ

1. Numerical Analysis

The average customers in M/M/2 system is L¿ 3 ρ3

1−ρ2 +2ρ

The average waiting time in the system in M/M/2 system is T=L/ λ2. Simulation We use SimPy package to simulate M/M/2 queue.

The figure shows the comparison between the numerical result (the red line) and the simulation results (the dot). One can see the accuracy of the simulation compare to the numerical result.

MM2The code is written as follows:

""" bank12: Multiple runs of the bank with a Monitor"""from SimPy.Simulation import *from random import expovariate, seedfrom SimPy.SimPlot import *import pylab## Model components ------------------------theSeed = [99997,46464,646546,13132,7737,4646,7479797,131348,2649413,79871]class Source(Process): """ Source generates customers randomly"""

def generate(self, number, interval, resource, mon, mon2): for i in range(number): c = Customer(name="Customer%02d" % (i)) activate(c, c.visit(b=resource,M=mon, M2 = mon2)) #1 t = expovariate( interval) yield hold, self, t

class Customer(Process): """ Customer arrives, is served and leaves """

def visit(self, b,M, M2):

Page 3: MM2 Queue Project

arrive = now() M2.observe(len(b.waitQ)+len(b.activeQ) ) yield request, self, b tib = expovariate( timeInBank) yield hold, self, tib wait = now() - arrive M.observe(wait) yield release, self, b

## Experiment data -------------------------

maxNumber = 10000maxTime = 10000000.0 # minutestimeInBank =0.1 # service rate mu, minutesARRint = 0.5 # mean, minutesNc = 2 # number of servers#theSeed = 99997thelambda =0.5 # arrival rate lambda## Model ----------------------------------

def model(thelambda): #2 nM = Monitor() wM=Monitor() for i in range(10): seed(theSeed[i]) k = Resource(capacity=Nc, name="Clerk", monitored=True, monitorType=Monitor) #3 initialize() s = Source('Source') activate(s, s.generate(number=maxNumber, interval=thelambda, resource=k, mon = wM, mon2 = nM), at=0.0) #4 simulate(until=maxTime) #return ( k.waitMon.mean()+k.actMon.mean(), nM.mean() ) return(wM.mean(),nM.mean()) #print('(TimeAverage no. waiting: %s' % k.waitMon.mean()) #return ( k.waitMon.mean()+k.actMon.mean()) #5## Experiment/Result ----------------------------------

thelambdas = [0.02, 0.04, 0.06, 0.08, 0.10, 0.12, 0.14 , 0.16 ,0.18, 0.19] #6SimTimeResults=[]

Page 4: MM2 Queue Project

SimNumResults=[]for Sd in thelambdas: result = model(Sd) SimTimeResults.append(result[0]) SimNumResults.append(result[1])print SimTimeResults #7print SimNumResults ### Numerical resulttimeAver=[]numAver=[]time2=[]for Sd in thelambdas: ro=Sd/(2*timeInBank) timeAver.append((1/Sd)*(2*ro+(2*ro**3)/(1-ro**2))) # time average of customer in M/M/2 numAver.append(2*ro+(2*ro**3)/(1-ro**2)) # number of customer average in customer in M/M/2

###Ploting

p1,=pylab.plot(thelambdas,numAver,'rs' )p2,=pylab.plot(thelambdas,SimNumResults,'y^' )pylab.xlabel("Arrival rate")pylab.ylabel("The average number of customer in the system" )pylab.grid(True)pylab.legend([p1,p2], [" Analysis", "Simulation"],'left upper')pylab.show()

p3,=pylab.plot(thelambdas,timeAver,'rx' )p4,=pylab.plot(thelambdas,SimTimeResults,'yo' )pylab.xlabel("Arrival rate")pylab.ylabel("The average waiting time in the system" )pylab.grid(True)

pylab.legend([p3,p4], [" Analysis", "Simulation"],'left upper')pylab.show()

/////////////////////////////////////////////////////

Page 5: MM2 Queue Project

Cai epdhttp://www.enthought.com/products/epd.phpgo lenh run: ipython notebookchay: shift + enterCai simpy.http://simpy.sourceforge.net/ MM33

""" bank12: Multiple runs of the bank with a Monitor"""from SimPy.Simulation import *from random import expovariate, seedfrom SimPy.SimPlot import *import pylab## Model components ------------------------theSeed = [99997,46464,646546,13132,7737,4646,7479797,131348,2649413,79871]class Source(Process): """ Source generates customers randomly"""

def generate(self, number, interval, resource, mon): for i in range(number): c = Customer(name="Customer%02d" % (i)) activate(c, c.visit(b=resource,M=mon)) #1 t = expovariate( interval) yield hold, self, t

class Customer(Process): """ Customer arrives, is served and leaves """

def visit(self, b,M): arrive = now() if b.n > 0: yield request, self, b tib = expovariate( timeInBank) yield hold, self, tib wait = now() - arrive M.observe(wait) yield release, self, b

Page 6: MM2 Queue Project

## Experiment data -------------------------

maxNumber = 10000maxTime = 10000000.0 # minutestimeInBank =0.1 # service rate mu, minutesARRint = 0.5 # mean, minutesNc = 3 # number of servers#theSeed = 99997thelambda =0.5 # arrival rate lambda## Model ----------------------------------

def model(thelambda): #2 # nM = Monitor() wM=Monitor() for i in range(10): seed(theSeed[i]) k = Resource(capacity=Nc, name="Clerk", monitored=True, monitorType=Monitor) #3 initialize() s = Source('Source') activate(s, s.generate(number=maxNumber, interval=thelambda, resource=k, mon = wM), at=0.0) #4 simulate(until=maxTime) #return ( k.waitMon.mean()+k.actMon.mean(), nM.mean() ) return(wM.mean()) #print('(TimeAverage no. waiting: %s' % k.waitMon.mean()) #return ( k.waitMon.mean()+k.actMon.mean()) #5## Experiment/Result ----------------------------------

thelambdas = [0.02, 0.06, 0.10, 0.14, 0.18, 0.22, 0.26 , 0.30 ,0.34, 0.38] #6SimTimeResults=[]SimNumResults=[]for Sd in thelambdas: result = model(Sd) SimTimeResults.append(result) #SimNumResults.append(result[1])print SimTimeResults #7print SimNumResults ### Numerical result

Page 7: MM2 Queue Project

timeAver=[]numAver=[]time2=[]for Sd in thelambdas: # ro=Sd/(2*timeInBank) timeAver.append(1/timeInBank) # time average of customer in M/M/2 #numAver.append(2*ro+(2*ro**3)/(1-ro**2)) # number of customer average in customer in M/M/2

###Ploting """p1,=pylab.plot(thelambdas,numAver,'rs' )p2,=pylab.plot(thelambdas,SimNumResults,'y^' )pylab.xlabel("Arrival rate")pylab.ylabel("The average number of customer in the system" )pylab.grid(True)pylab.legend([p1,p2], [" Analysis", "Simulation"],'left upper')pylab.show()

"""p3,=pylab.plot(thelambdas,timeAver,'rx' )p4,=pylab.plot(thelambdas,SimTimeResults,'yo' )pylab.xlabel("Arrival rate")pylab.ylabel("The average waiting time in the system" )pylab.grid(True)

pylab.legend([p3,p4], [" Analysis", "Simulation"],'left upper')pylab.show()