bank11Plot.py :  » Development » SimPy » SimPy-2.1.0beta » SimPyModels » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » SimPy 
SimPy » SimPy 2.1.0beta » SimPyModels » bank11Plot.py
""" bank11.py: Simulate customers arriving
    at random, using a Source, requesting service
    from two counters each with their own queue
    random servicetime.
    Uses a Monitor object to record waiting times
"""
from SimPy.Simulation import *
from SimPy.SimPlot import *
from random import Random

class Bank11(SimPlot):
    def __init__(self,**p):
        SimPlot.__init__(self,**p)
    
    def NoInSystem(self,R):
        """ The number of customers in the resource R
        in waitQ and active Q"""
        return (len(R.waitQ)+len(R.activeQ))

    def model(self):
        self.counterRV = Random(self.params["counterseed"])
        self.sourceseed = self.params["sourceseed"]
        nrRuns=self.params["nrRuns"]
        self.lastLeave=0
        self.noRunYet=True
        for runNr in range(nrRuns):
            self.noRunYet=False
            self.Nc = 2
            self.counter = [Resource(name="Clerk0",monitored=False),
                            Resource(name="Clerk1",monitored=False)]
            self.waitMonitor = Monitor(name='Waiting Times')
            self.waitMonitor.xlab='Time'
            self.waitMonitor.ylab='Waiting time'
            self.serviceMonitor = Monitor(name='Service Times')
            self.serviceMonitor.xlab='Time'
            self.serviceMonitor.ylab='wait+service'
            initialize()
            source = Source(self,seed = self.sourceseed*1000)
            activate(source,source.generate(self.params["numberCustomers"],
                                            self.params["interval"]),0.0)
            simulate(until=self.params['endtime'])
            self.lastLeave+=now()
        print "%s run(s) completed"%(nrRuns)
        print("Parameters:\n%s"%self.params)

class Source(Process):
    """ Source generates customers randomly"""
    def __init__(self,modInst,seed=333):
        Process.__init__(self)
        self.modInst=modInst
        self.SEED = seed

    def generate(self,number,interval):       
        rv = Random(self.SEED)
        for i in range(number):
            c = Customer(self.modInst,name = "Customer%02d"%(i,))
            activate(c,c.visit(timeInBank=12.0))
            t = rv.expovariate(1.0/interval)
            yield hold,self,t

class Customer(Process):
    """ Customer arrives, is served and leaves """
    def __init__(self,modInst,**p):
        Process.__init__(self,**p)
        self.modInst=modInst
        
    def visit(self,timeInBank=0):       
        arrive=now()
        Qlength = [self.modInst.NoInSystem(self.modInst.counter[i])\
                   for i in range(self.modInst.Nc)]
        for i in range(self.modInst.Nc):
            if Qlength[i] ==0 or Qlength[i]==min(Qlength): join =i ; break
        yield request,self,self.modInst.counter[join]
        wait=now()-arrive
        self.modInst.waitMonitor.observe(wait,t=now())
        ##print "%7.4f %s: Waited %6.3f"%(now(),self.name,wait)
        tib = self.modInst.counterRV.expovariate(1.0/timeInBank)
        yield hold,self,tib
        yield release,self,self.modInst.counter[join]
        self.modInst.serviceMonitor.observe(now()-arrive,t=now())

root=Tk()
plt=Bank11()
plt.params={"endtime":2000,
       "sourceseed":1133,
       "counterseed":3939393,
       "numberCustomers":50,
       "interval":10.0,
       "trace":0,
       "nrRuns":1}
plt.model()
plt.plotLine(plt.waitMonitor,color='blue',width=2)
plt.plotLine(plt.serviceMonitor,color='red',width=2)
root.mainloop()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.