Professional Documents
Culture Documents
MM2 Queue Project
MM2 Queue Project
Project
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())
#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)
///////////////////////////////////////////////////// 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()