| 1 |
""" |
| 2 |
jacksonnetwork.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 |
|
| 29 |
import random as ran |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
def choose2dA(i,P): |
| 34 |
""" return a random choice from a set j = 0..n-1 |
| 35 |
with probs held in list of lists P[j] (n by n) |
| 36 |
using row i |
| 37 |
call: next = choose2d(i,P) |
| 38 |
""" |
| 39 |
U = ran.random() |
| 40 |
sumP = 0.0 |
| 41 |
for j in range(len(P[i])): |
| 42 |
sumP += P[i][j] |
| 43 |
if U < sumP: break |
| 44 |
return(j) |
| 45 |
|
| 46 |
class Msg(Process): |
| 47 |
"""a message.""" |
| 48 |
noInSystem=0 |
| 49 |
def execute(self,i): |
| 50 |
""" executing a message """ |
| 51 |
startTime = now() |
| 52 |
Msg.noInSystem += 1 |
| 53 |
|
| 54 |
NoInSystem.observe(Msg.noInSystem) |
| 55 |
self.trace("Arrived node %d"%(i,)) |
| 56 |
while i <> 3: |
| 57 |
yield request,self,node[i] |
| 58 |
self.trace("Got node %d"%(i,)) |
| 59 |
st = ran.expovariate(1.0/mean[i]) |
| 60 |
yield hold,self,st |
| 61 |
yield release,self,node[i] |
| 62 |
self.trace("Finished with %d"%(i,)) |
| 63 |
i = choose2dA(i,P) |
| 64 |
self.trace("Transfer to %d"%(i,)) |
| 65 |
TimeInSystem.tally(now()-startTime) |
| 66 |
self.trace( "leaving %d %d in system"%(i,Msg.noInSystem)) |
| 67 |
Msg.noInSystem -= 1 |
| 68 |
NoInSystem.accum(Msg.noInSystem) |
| 69 |
|
| 70 |
def trace(self,message): |
| 71 |
if MTRACING: print "%7.4f %3s %10s"%(now(),self.name, message) |
| 72 |
|
| 73 |
|
| 74 |
class MsgSource(Process): |
| 75 |
""" generates a sequence of msgs """ |
| 76 |
|
| 77 |
def execute(self,rate,maxN): |
| 78 |
self.count=0 |
| 79 |
self.trace("starting MsgSource") |
| 80 |
while (self.count < maxN): |
| 81 |
self.count+=1 |
| 82 |
p = Msg("Message %d"%(self.count,)) |
| 83 |
activate(p,p.execute(startNode)) |
| 84 |
yield hold,self,ran.expovariate(rate) |
| 85 |
self.trace("generator finished with "+`self.count`+" ========") |
| 86 |
|
| 87 |
def trace(self,message): |
| 88 |
if GTRACING: print "%7.4f \t%s"%(now(), message) |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
rate = 1.5 |
| 93 |
maxNumber = 1000 |
| 94 |
GTRACING = False |
| 95 |
|
| 96 |
startNode = 0 |
| 97 |
ran.seed(77777) |
| 98 |
MTRACING = False |
| 99 |
|
| 100 |
TimeInSystem = Monitor("time") |
| 101 |
NoInSystem = Monitor("Number") |
| 102 |
|
| 103 |
node = [Resource(1),Resource(1),Resource(1)] |
| 104 |
|
| 105 |
mean=[1.0, 2.0, 1.0] |
| 106 |
P = [[0, 0.5, 0.5, 0 ], |
| 107 |
[0, 0, 0.8, 0.2], |
| 108 |
[0.2, 0, 0, 0.8]] |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
initialize() |
| 113 |
g = MsgSource(name="MsgSource") |
| 114 |
activate(g,g.execute(rate,maxNumber)) |
| 115 |
simulate(until=5000.0) |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
print 'jacksonnetwork' |
| 120 |
print "Mean number in system = %10.4f"%(NoInSystem.timeAverage(),) |
| 121 |
print "Mean delay in system = %10.4f"%(TimeInSystem.mean(),) |
| 122 |
print "Total time run = %10.4f"%(now(),) |
| 123 |
print "Total jobs arrived = %10d"%(g.count) |
| 124 |
print "Total jobs completed = %10d"%(TimeInSystem.count(),) |
| 125 |
print "Average arrival rate = %10.4f"%(g.count/now(),) |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|