Debian Codegraph @

docs/examples/cellphone.py

raw symbols SCIP
1 """ cellphone.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 ## Model components ------------------------
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,))
32 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 = 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 = now()
53 busy = 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 "%(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 = now() # simulation start time
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 = now()
80 yield hold,self,obsPeriod
81 if cell.Nfree == 0: cell.totalBusyTime += now()-cell.busyStartTime
82 if STRACING:
83 print "%7.3f %5d"%(cell.totalBusyTime,cell.totalBusyVisits)
84 m.tally(cell.totalBusyTime)
85 bn.tally(cell.totalBusyVisits)
86 stopSimulation()
87 cell.result= (m.mean(),m.var(),bn.mean(),bn.var())
88
89 ## Experiment data -------------------------
90
91 NChannels = 4 # number of channels in the cell
92 maxN = 10000
93 ranSeed = 3333333
94 lam = 1.0 # per minute
95 mu = 0.6667 # per minute
96 Nperiods = 10
97 obsPeriod = 60.0 # minutes
98 obsGap = 15.0 # gap between observation periods
99
100 TRACING = False
101 STRACING = True
102
103
104 ## Experiment ------------------------------
105
106 m = Monitor()
107 bn =Monitor()
108 ran.seed(ranSeed)
109
110 cell=Cell() # the cellphone tower
111 cell.Nfree = NChannels
112
113 initialize()
114 s = Statistician('Statistician')
115 activate(s,s.execute(Nperiods,obsPeriod,obsGap,cell))
116 g = CallSource('CallSource')
117 activate(g,g.execute(maxN, lam,cell))
118 simulate(until=10000.0)
119
120 ## Output -------------------------
121 print 'cellphone'
122 # input data:
123 print "lambda mu s Nperiods obsPeriod obsGap"
124 FMT= "%7.4f %6.4f %4d %4d %6.2f %6.2f"
125 print FMT%(lam,mu,NChannels,Nperiods,obsPeriod,obsGap)
126
127
128 sr = cell.result
129 print "Busy Time: mean = %6.3f var= %6.3f"%sr[0:2]
130 print "Busy Number: mean = %6.3f var= %6.3f"%sr[2:4]