You are on page 1of 7

Queuing Theory

Project

Professor : Tran Hoang Nguyen


Student Name : Student ID :

M/M/2 Queueing System


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 33 The average customers in M/M/2 system is L= + 2 12 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. MM2 The code is written as follows: """ bank12: Multiple runs of the bank with a Monitor""" from SimPy.Simulation import * from random import expovariate, seed from 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)) t = expovariate( interval) yield hold, self, t

#1

class Customer(Process): """ Customer arrives, is served and leaves """ def visit(self, b,M, M2): 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 = 10000 maxTime = 10000000.0 # minutes timeInBank =0.1 # service rate mu, minutes ARRint = 0.5 # mean, minutes Nc = 2 # number of servers #theSeed = 99997 thelambda =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()) ## Experiment/Result ----------------------------------

#5

thelambdas = [0.02, 0.04, 0.06, 0.08, 0.10, 0.12, 0.14 , 0.16 ,0.18, 0.19] #6 SimTimeResults=[] SimNumResults=[] for Sd in thelambdas: result = model(Sd) SimTimeResults.append(result[0]) SimNumResults.append(result[1]) print SimTimeResults #7 print SimNumResults ### Numerical result timeAver=[] 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()

///////////////////////////////////////////////////// Cai epd http://www.enthought.com/products/epd.php go lenh run: ipython notebook chay: shift + enter Cai simpy. http://simpy.sourceforge.net/ MM33

""" bank12: Multiple runs of the bank with a Monitor""" from SimPy.Simulation import * from random import expovariate, seed from 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 ## Experiment data ------------------------maxNumber = 10000 maxTime = 10000000.0 # minutes timeInBank =0.1 # service rate mu, minutes ARRint = 0.5 # mean, minutes Nc = 3 # number of servers #theSeed = 99997 thelambda =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] SimTimeResults=[] #6

SimNumResults=[] for Sd in thelambdas: result = model(Sd) SimTimeResults.append(result) #SimNumResults.append(result[1]) print SimTimeResults #7 print SimNumResults ### Numerical result 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()

You might also like