Debian Codegraph @

docs/examples/SimGUIStep.py

raw symbols SCIP
1 from SimPy.SimGUI import *
2 from SimPy.SimulationStep import *
3 from random import Random
4
5 __version__ = '$Revision$ $Date$ kgm'
6
7 if __name__ == '__main__':
8
9 class Source(Process):
10 """ Source generates customers randomly"""
11 def __init__(self,seed=333):
12 Process.__init__(self)
13 self.SEED = seed
14
15 def generate(self,number,interval):
16 rv = Random(self.SEED)
17 for i in range(number):
18 c = Customer(name = "Customer%02d"%(i,))
19 activate(c,c.visit(timeInBank=12.0))
20 t = rv.expovariate(1.0/interval)
21 yield hold,self,t
22
23 def NoInSystem(R):
24 """ The number of customers in the resource R
25 in waitQ and active Q"""
26 return (len(R.waitQ)+len(R.activeQ))
27
28 class Customer(Process):
29 """ Customer arrives, is served and leaves """
30 def __init__(self,name):
31 Process.__init__(self)
32 self.name = name
33
34 def visit(self,timeInBank=0):
35 arrive=now()
36 Qlength = [NoInSystem(counter[i]) for i in range(Nc)]
37 ##print "%7.4f %s: Here I am. %s "%(now(),self.name,Qlength)
38 for i in range(Nc):
39 if Qlength[i] ==0 or Qlength[i]==min(Qlength): join =i ; break
40 yield request,self,counter[join]
41 wait=now()-arrive
42 waitMonitor.observe(wait,t=now()) #Lmond
43 ##print "%7.4f %s: Waited %6.3f"%(now(),self.name,wait)
44 tib = counterRV.expovariate(1.0/timeInBank)
45 yield hold,self,tib
46 yield release,self,counter[join]
47 serviceMonitor.observe(now()-arrive,t=now())
48 if trace:
49 gui.writeConsole("Customer leaves at %.1d"%now())
50 ##print "%7.4f %s: Finished "%(now(),self.name)
51
52 def showtime():
53 gui.topconsole.config(text="time = %s"%now())
54 gui.root.update()
55
56 def runStep():
57 if gui.noRunYet:
58 showwarning("SimPy warning","Run 'Start run (stepping)' first")
59 return
60 showtime()
61 a=simulateStep(until=gui.params.endtime)
62 if a[1]=="notResumable":
63 gui.writeConsole(text="Run ended. Status: %s"%a[0])
64 showtime()
65
66 def runNoStep():
67 showtime()
68 for i in range(gui.params.nrRuns):
69 simulate(until=gui.param.sendtime)
70 showtime()
71 gui.writeConsole("%s simulation run(s) completed\n"%(i+1))
72
73
74 def contStep():
75 return
76
77 def model():
78 global Nc,counter,counterRV,waitMonitor,serviceMonitor,trace,lastLeave,noRunYet,initialized
79 counterRV = Random(gui.params.counterseed)
80 sourceseed = gui.params.sourceseed
81 nrRuns=gui.params.nrRuns
82 lastLeave=0
83 gui.noRunYet=True
84 for runNr in range(nrRuns):
85 gui.noRunYet=False
86 trace=gui.params.trace
87 if trace:
88 gui.writeConsole(text='\n** Run %s'%(runNr+1))
89 Nc = 2
90 counter = [Resource(name="Clerk0"),Resource(name="Clerk1")]
91 gui.waitMoni=waitMonitor = Monitor(name='Waiting Times')
92 waitMonitor.xlab='Time'
93 waitMonitor.ylab='Customer waiting time'
94 gui.serviceMoni=serviceMonitor = Monitor(name='Service Times')
95 serviceMonitor.xlab='Time'
96 serviceMonitor.ylab='Total service time = wait+service'
97 initialize()
98 source = Source(seed = sourceseed)
99 activate(source,source.generate(gui.params.numberCustomers,gui.params.interval),0.0)
100 simulate(showtime,until=gui.params.endtime)
101 showtime()
102 lastLeave+=now()
103 gui.writeConsole("%s simulation run(s) completed\n"%nrRuns)
104 gui.writeConsole("Parameters:\n%s"%gui.params)
105
106 def modelstep():
107 global Nc,counter,counterRV,waitMonitor,serviceMonitor,trace,lastLeave,noRunYet
108 counterRV = Random(gui.params.counterseed)
109 sourceseed = gui.params.sourceseed
110 nrRuns=gui.params.nrRuns
111 lastLeave=0
112 gui.noRunYet=True
113 trace=gui.params.trace
114 if trace:
115 gui.writeConsole(text='\n** Run %s'%(runNr+1))
116 Nc = 2
117 counter = [Resource(name="Clerk0"),Resource(name="Clerk1")]
118 gui.waitMoni=waitMonitor = Monitor(name='Waiting Times')
119 waitMonitor.xlab='Time'
120 waitMonitor.ylab='Customer waiting time'
121 gui.serviceMoni=serviceMonitor = Monitor(name='Service Times')
122 serviceMonitor.xlab='Time'
123 serviceMonitor.ylab='Total service time = wait+service'
124 initialize()
125 source = Source(seed = sourceseed)
126 activate(source,source.generate(gui.params.numberCustomers,gui.params.interval),0.0)
127 simulateStep(until=gui.params.endtime)
128 gui.noRunYet=False
129
130 def statistics():
131 if gui.noRunYet:
132 showwarning(title='SimPy warning',message="Run simulation first -- no data available.")
133 return
134 aver=lastLeave/gui.params.nrRuns
135 gui.writeConsole(text="Average time for %s customers to get through bank: %.1f\n(%s runs)\n"\
136 %(gui.params.numberCustomers,aver,gui.params.nrRuns))
137
138 __doc__="""
139 Modified bank11.py (from Bank Tutorial) with GUI.
140
141 Model: Simulate customers arriving
142 at random, using a Source, requesting service
143 from two counters each with their own queue
144 random servicetime.
145
146 Uses Monitor objects to record waiting times
147 and total service times."""
148
149 def showAuthors():
150 gui.showTextBox(text="Tony Vignaux\nKlaus Muller",title="Author information")
151 class MyGUI(SimGUI):
152 def __init__(self,win,**p):
153 SimGUI.__init__(self,win,**p)
154 self.help.add_command(label="Author(s)",
155 command=showAuthors,underline=0)
156 self.view.add_command(label="Statistics",
157 command=statistics,underline=0)
158 self.run.add_command(label="Start run (event stepping)",
159 command=modelstep,underline=0)
160 self.run.add_command(label="Next event",
161 command=runStep,underline=0)
162 self.run.add_command(label="Complete run (no stepping)",
163 command=model,underline=0)
164
165 root=Tk()
166 gui=MyGUI(root,title="SimPy GUI example",doc=__doc__)
167 gui.params=Parameters(endtime=2000,
168 sourceseed=1133,
169 counterseed=3939393,
170 numberCustomers=50,
171 interval=10.0,
172 trace=0,
173 nrRuns=1)
174 gui.mainloop()
175