Debian Codegraph @

docs/examples/jacksonnetwork_OO.py

raw symbols SCIP
1 """
2 jacksonnetwork_OO.py
3
4 Messages arrive randomly at rate 1.5 per second
5 at a communication network with 3 nodes
6 (computers). Each computer (node) can queue
7 messages. Service-times are exponential with
8 mean m_i at node i. These values are given in
9 the column headed n_i in the table below. On
10 completing service at node i a message transfers
11 to node j with probability p_ij and leaves the
12 system with probability p_i3.
13
14 These transition probabilities are as follows:
15 Node m_i p_i0 p_i1 p_i2 p_i3
16 i (leave)
17 0 1.0 0 0.5 0.5 0
18 1 2.0 0 0 0.8 0.2
19 2 1.0 0.2 0 0 0.8
20
21 Your task is to estimate
22 (1) the average time taken for jobs going
23 through the system and
24 (2) the average number of jobs in the system.
25
26 """
27 from SimPy.Simulation import *
28 import random as ran
29
30 ## Model components ------------------------
31
32 def choose2dA(i,P):
33 """ return a random choice from a set j = 0..n-1
34 with probs held in list of lists P[j] (n by n)
35 using row i
36 call: next = choose2d(i,P)
37 """
38 U = ran.random()
39 sumP = 0.0
40 for j in range(len(P[i])): # j = 0..n-1
41 sumP += P[i][j]
42 if U < sumP: break
43 return(j)
44
45 class Msg(Process):
46 """a message"""
47 noInSystem=0
48 def execute(self,i):
49 """ executing a message """
50 startTime = self.sim.now()
51 Msg.noInSystem += 1
52 ##print "DEBUG noInSystm = ",Msg.noInSystem
53 self.sim.NoInSystem.observe(Msg.noInSystem)
54 self.trace("Arrived node %d"%(i,))
55 while i <> 3:
56 yield request,self,self.sim.node[i]
57 self.trace("Got node %d"%(i,))
58 st = ran.expovariate(1.0/mean[i])
59 yield hold,self,st
60 yield release,self,self.sim.node[i]
61 self.trace("Finished with %d"%(i,))
62 i = choose2dA(i,P)
63 self.trace("Transfer to %d"%(i,))
64 self.sim.TimeInSystem.tally(self.sim.now()-startTime)
65 self.trace( "leaving %d %d in system"%(i,Msg.noInSystem))
66 Msg.noInSystem -= 1
67 self.sim.NoInSystem.accum(Msg.noInSystem)
68
69 def trace(self,message):
70 if MTRACING: print "%7.4f %3d %10s"%(self.sim.now(),self.name, message)
71
72
73 class MsgSource(Process):
74 """ generates a sequence of msgs """
75
76 def execute(self,rate,maxN):
77 self.count=0 # hold number of messages generated
78 while (self.count < maxN):
79 self.count+=1
80 p = Msg("Message %d"%(self.count,),sim=self.sim)
81 self.sim.activate(p,p.execute(i=startNode))
82 yield hold,self,ran.expovariate(rate)
83 self.trace("generator finished with "+`self.count`+" ========")
84
85 def trace(self,message):
86 if GTRACING: print "%7.4f \t%s"%(self.sim.now(), message)
87
88 ## Experiment data -------------------------
89
90 rate = 1.5 ## arrivals per second
91 maxNumber = 1000 ## of Messages
92 GTRACING = False ## tracing Messages Source?
93
94 startNode = 0 ## Messages always enter at node 0
95 ran.seed(77777)
96 MTRACING = False ## tracing Message action?
97
98
99
100 mean=[1.0, 2.0, 1.0] ## service times, seconds
101 P = [[0, 0.5, 0.5, 0 ], ## transition matrix P_ij
102 [0, 0, 0.8, 0.2],
103 [0.2, 0, 0, 0.8]]
104
105 ## Model -----------------------------------
106 class JacksonnetworkModel(Simulation):
107 def run(self):
108 self.initialize()
109 self.TimeInSystem = Monitor("time",sim=self)
110 self.NoInSystem = Monitor("Number",sim=self)
111 self.node = [Resource(1,sim=self),Resource(1,sim=self),Resource(1,sim=self)]
112 self.g = MsgSource("MsgSource",sim=self)
113 self.activate(self.g,self.g.execute(rate,maxNumber))
114 self.simulate(until=5000.0)
115
116 ## Experiment ------------------------------
117 modl=JacksonnetworkModel()
118 modl.run()
119
120 ## Analysis/output -------------------------
121
122 print 'jacksonnetwork'
123 print "Mean number in system = %10.4f"%(modl.NoInSystem.timeAverage(),)
124 print "Mean delay in system = %10.4f"%(modl.TimeInSystem.mean(),)
125 print "Total time run = %10.4f"%(modl.now(),)
126 print "Total jobs arrived = %10d"%(modl.g.count)
127 print "Total jobs completed = %10d"%(modl.TimeInSystem.count(),)
128 print "Average arrival rate = %10.4f"%(modl.g.count/modl.now(),)
129
130 # $Author$ $Revision$
131 # $Date$