Debian Codegraph @

docs/examples/bcc_OO.py

raw symbols SCIP
1 """ bcc_OO.py
2
3 Queue with blocked customers cleared
4 Jobs (e.g messages) arrive randomly at rate 1.0 per minute at a
5 2-server system. Mean service time is 0.75 minutes and the
6 service-time distribution is (1) exponential, (2) Erlang-5, or (3)
7 hyperexponential with p=1/8,m1=2.0, and m2=4/7. However no
8 queue is allowed; a job arriving when all the servers are busy is
9 rejected.
10
11 Develop and run a simulation program to estimate the probability of
12 rejection (which, in steady-state, is the same as p(c)) Measure
13 and compare the probability for each service time distribution.
14 Though you should test the program with a trace, running just a few
15 jobs, the final runs should be of 10000 jobs without a trace. Stop
16 the simulation when 10000 jobs have been generated.
17 """
18
19 from SimPy.Simulation import *
20 from random import seed,Random,expovariate,uniform
21
22 ## Model components ------------------------
23
24 dist = ""
25 def bcc(lam, mu,s):
26 """ bcc - blocked customers cleared model
27
28 - returns p[i], i = 0,1,..s.
29 - ps = p[s] = prob of blocking
30 - lameff = effective arrival rate = lam*(1-ps)
31
32 See Winston 22.11 for Blocked Customers Cleared Model (Erlang B formula)
33 """
34 rho = lam/mu
35 n = range(s+1)
36 p = [0]*(s+1)
37 p[0] = 1
38 sump = 1.0
39 for i in n[1:]:
40 p[i] = (rho/i)*p[i-1]
41 sump = sump + p[i]
42 p0 = 1.0/sump
43 for i in n:
44 p[i] = p[i]*p0
45 p0 = p[0]
46 ps = p[s]
47 lameff = lam*(1-ps)
48 L = rho*(1-ps)
49 return {'lambda':lam,'mu':mu,'s':s,
50 'p0':p0,'p[i]':p,'ps':ps, 'L':L}
51
52 def ErlangVariate(mean,K):
53 """ Erlang random variate
54
55 mean = mean
56 K = shape parameter
57 g = rv to be used
58 """
59 sum = 0.0 ; mu = K/mean
60 for i in range(K):
61 sum += expovariate(mu)
62 return (sum)
63
64 def HyperVariate(p,m1,m2):
65 """ Hyperexponential random variate
66
67 p = prob of branch 1
68 m1 = mean of exponential, branch 1
69 m2 = mean of exponential, branch 2
70 g = rv to be used
71 """
72 if random() < p:
73 return expovariate(1.0/m1)
74 else: return expovariate(1.0/m2)
75
76 def testHyperVariate():
77 """ tests the HyerVariate rv generator"""
78 ERR=0
79 x = (1.0981,1.45546,5.7470156)
80 p = 0.0, 1.0 ,0.5
81 g = Random(1113355)
82 for i in range(3):
83 x1 = HyperVariate(p[i],1.0,10.0,g)
84 #print p[i], x1
85 assert abs(x1 - x[i]) < 0.001,'HyperVariate error'
86
87 def erlangB(rho,c):
88 """ Erlang's B formula for probabilities in no-queue
89
90 Returns p[n] list
91 see also SPlus and R version in que.q mmcK
92 que.py has bcc.
93 """
94 n = range(c+1) ; pn = range(c+1)
95 term = 1
96 pn[0] = 1
97 sum = 1 ; term = 1.0
98 i=1
99 while i < (c+1):
100 term *= rho/i
101 pn[i] = term
102 sum += pn[i]
103 i += 1
104 for i in n: pn[i] = pn[i]/sum
105 return(pn)
106
107
108 class JobGen(Process):
109 """ generates a sequence of Jobs
110 """
111
112 def execute(self,JobRate,MaxJob,mu):
113 global NoInService, Busy
114 for i in range(MaxJob):
115 j = Job(sim=self.sim)
116 self.sim.activate(j,j.execute(i,mu),delay=0.0)
117 t = expovariate(JobRate)
118 MT.tally(t)
119 yield hold,self,t
120 self.trace("Job generator finished")
121
122 def trace(self,message):
123 if JobGenTRACING: print "%8.4f \t%s"%(self.sim.now(), message)
124
125 class Job(Process):
126 """ Jobs that are either accepted or rejected
127 """
128
129 def execute(self,i,mu):
130 """ Job execution, only if accepted"""
131 global NoInService,Busy,dist,NoRejected
132 if NoInService < c:
133 self.trace("Job %2d accepted b=%1d"%(i,Busy))
134 NoInService +=1
135 if NoInService == c:
136 Busy =1
137 try: BM.accum(Busy,now())
138 except: "accum error BM=",BM
139 #yield hold,self,Job.g.expovariate(self.mu); dist= "Exponential"
140 yield hold,self,ErlangVariate(1.0/mu,5); dist= "Erlang "
141 #yield hold,self,HyperVariate(1.0/8,m1=2.0,m2=4.0/7,g=Job.g); dist= "HyperExpon "
142 NoInService -=1
143 Busy =0
144 BM.accum(Busy,self.sim.now())
145 self.trace("Job %2d leaving b=%1d"%(i,Busy))
146 else:
147 self.trace("Job %2d REJECT b=%1d"%(i,Busy))
148 NoRejected +=1
149
150 def trace(self,message):
151 if JobTRACING: print "%8.4f \t%s"%(self.sim.now(), message)
152
153
154 ## Experiment data -------------------------
155 c = 2
156 lam = 1.0 ## per minute
157 mu = 1.0/0.75 ## per minute
158 p = 1.0/8 ; m1= 2.0; m2 = 4.0/7.0
159 K = 5
160 rho = lam/mu
161
162 NoRejected = 0
163 NoInService = 0
164 Busy = 0
165
166 JobRate = lam
167 JobMax = 10000
168
169 JobTRACING = 0
170 JobGenTRACING = 0
171
172
173 ## Model/Experiment ------------------------------
174
175 seed(111333)
176 s=Simulation()
177 BM=Monitor(sim=s)
178 MT = Monitor(sim=s)
179
180 s.initialize()
181 jbg = JobGen(sim=s)
182 s.activate(jbg,jbg.execute(1.0,JobMax,mu),0.0)
183 s.simulate(until=20000.0)
184
185 ## Analysis/output -------------------------
186
187 print 'bcc'
188 print "time at the end =",s.now()
189 print "now=",s.now(), " startTime ",BM.startTime
190 print "No Rejected = %d, ratio= %s"%(NoRejected,(1.0*NoRejected)/JobMax)
191 print "Busy proportion (%6s) = %8.6f"%(dist,BM.timeAverage(s.now()),)
192 print "Erlang pc (th) = %8.6f"%(erlangB(rho,c)[c],)
193
194
195