| 1 |
""" cellphone_OO.py |
| 2 |
|
| 3 |
Simulate the operation of a BCC cellphone system. |
| 4 |
|
| 5 |
Calls arrive at random to a cellphone hub with a fixed number of |
| 6 |
channels. Service times are assumed exponential. The objective |
| 7 |
is to determine the statistics of busy periods in the operation of a |
| 8 |
BCC cellphone system. |
| 9 |
|
| 10 |
The required measurements are |
| 11 |
(1) the total busy time (all channels full) in each 1-hour period and |
| 12 |
(2) the total number of busy times in a 1-hour period. |
| 13 |
|
| 14 |
The simulation is continuous but the observing Process, a |
| 15 |
Statistician, breaks the time into 1-hour observation periods |
| 16 |
separated by 15-minute gaps to reduce autocorrelation. The total busy |
| 17 |
time and number of busy times in each interval is printed. |
| 18 |
|
| 19 |
""" |
| 20 |
from SimPy.Simulation import * |
| 21 |
import random as ran |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class CallSource(Process): |
| 27 |
""" generates a sequence of calls """ |
| 28 |
|
| 29 |
def execute(self, maxN, lam,cell): |
| 30 |
for i in range(maxN): |
| 31 |
j = Call("Call%03d"%(i,),sim=self.sim) |
| 32 |
self.sim.activate(j,j.execute(cell)) |
| 33 |
yield hold,self,ran.expovariate(lam) |
| 34 |
|
| 35 |
|
| 36 |
class Call(Process): |
| 37 |
""" Calls arrive at random at the cellphone hub""" |
| 38 |
def execute(self,cell): |
| 39 |
self.trace("arrived") |
| 40 |
if cell.Nfree == 0: self.trace("blocked and left") |
| 41 |
else: |
| 42 |
self.trace("got a channel") |
| 43 |
cell.Nfree -= 1 |
| 44 |
if cell.Nfree == 0: |
| 45 |
self.trace("start busy period======") |
| 46 |
cell.busyStartTime = self.sim.now() |
| 47 |
cell.totalBusyVisits += 1 |
| 48 |
yield hold,self,ran.expovariate(mu) |
| 49 |
self.trace("finished") |
| 50 |
if cell.Nfree == 0: |
| 51 |
self.trace("end busy period++++++") |
| 52 |
cell.busyEndTime = self.sim.now() |
| 53 |
busy = self.sim.now() - cell.busyStartTime |
| 54 |
self.trace(" busy = %9.4f"%(busy,)) |
| 55 |
cell.totalBusyTime +=busy |
| 56 |
cell.Nfree += 1 |
| 57 |
|
| 58 |
def trace(self,message): |
| 59 |
if TRACING: |
| 60 |
print "%7.4f %13s %s "%(self.sim.now(), message, self.name) |
| 61 |
|
| 62 |
class Cell: |
| 63 |
""" Holds global measurements""" |
| 64 |
Nfree = 0 |
| 65 |
totalBusyTime = 0.0 |
| 66 |
totalBusyVisits = 0 |
| 67 |
result=() |
| 68 |
|
| 69 |
class Statistician(Process): |
| 70 |
""" observes the system at intervals """ |
| 71 |
|
| 72 |
def execute(self,Nperiods,obsPeriod,obsGap,cell): |
| 73 |
cell.busyEndTime = self.sim.now() |
| 74 |
if STRACING: print "Busy time Number" |
| 75 |
for i in range(Nperiods): |
| 76 |
yield hold,self,obsGap |
| 77 |
cell.totalBusyTime = 0.0 |
| 78 |
cell.totalBusyVisits = 0 |
| 79 |
if cell.Nfree == 0: cell.busyStartTime = self.sim.now() |
| 80 |
yield hold,self,obsPeriod |
| 81 |
if cell.Nfree == 0: cell.totalBusyTime += self.sim.now()-cell.busyStartTime |
| 82 |
if STRACING: |
| 83 |
print "%7.3f %5d"%(cell.totalBusyTime,cell.totalBusyVisits) |
| 84 |
self.sim.m.tally(cell.totalBusyTime) |
| 85 |
self.sim.bn.tally(cell.totalBusyVisits) |
| 86 |
self.sim.stopSimulation() |
| 87 |
cell.result= (self.sim.m.mean(),self.sim.m.var(),self.sim.bn.mean(),self.sim.bn.var()) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
NChannels = 4 |
| 92 |
maxN = 10000 |
| 93 |
ranSeed = 3333333 |
| 94 |
lam = 1.0 |
| 95 |
mu = 0.6667 |
| 96 |
Nperiods = 10 |
| 97 |
obsPeriod = 60.0 |
| 98 |
obsGap = 15.0 |
| 99 |
|
| 100 |
TRACING = False |
| 101 |
STRACING = True |
| 102 |
|
| 103 |
|
| 104 |
class CellphoneModel(Simulation): |
| 105 |
def run(self): |
| 106 |
self.initialize() |
| 107 |
self.m = Monitor(sim=self) |
| 108 |
self.bn =Monitor(sim=self) |
| 109 |
ran.seed(ranSeed) |
| 110 |
self.cell = Cell() |
| 111 |
self.cell.Nfree = NChannels |
| 112 |
s = Statistician('Statistician',sim=self) |
| 113 |
self.activate(s,s.execute(Nperiods,obsPeriod,obsGap,self.cell)) |
| 114 |
g = CallSource('CallSource',sim=self) |
| 115 |
self.activate(g,g.execute(maxN, lam,self.cell)) |
| 116 |
self.simulate(until=10000.0) |
| 117 |
|
| 118 |
|
| 119 |
modl=CellphoneModel() |
| 120 |
modl.run() |
| 121 |
|
| 122 |
|
| 123 |
print 'cellphone' |
| 124 |
|
| 125 |
print "lambda mu s Nperiods obsPeriod obsGap" |
| 126 |
FMT= "%7.4f %6.4f %4d %4d %6.2f %6.2f" |
| 127 |
print FMT%(lam,mu,NChannels,Nperiods,obsPeriod,obsGap) |
| 128 |
|
| 129 |
sr = modl.cell.result |
| 130 |
print "Busy Time: mean = %6.3f var= %6.3f"%sr[0:2] |
| 131 |
print "Busy Number: mean = %6.3f var= %6.3f"%sr[2:4] |