I am trying to run this code I found, but it gives me the following error. Any idea what the problem is and how I can fix it?
#!/usr/bin/env python
# simulates NMachines machines, plus a queue of jobs waiting to use them 4
# usage: python MMk.py NMachines ArvRate SrvRate MaxSimtime
from random import Random,expovariate
from simpy.simulation import *
# globals
class G:
Rnd = Random(12345)
class MachineClass(Process):
SrvRate = None # reciprocal of mean service time
Busy = [] # busy machines
Idle = [] # idle machines
Queue = [] # queue for the machines
NDone = 0 # number of jobs done so far
TotWait = 0.0 # total wait time of all jobs done so far, including
# both queuing and service times
def __init__(self):
Process.__init__(self)
MachineClass.Idle.append(self) # starts idle
def Run(self):
while 1:
# "sleep" until this machine awakened
yield passivate,self
MachineClass.Idle.remove(self)
MachineClass.Busy.append(self)
# take jobs from the queue as long as there are some there
while MachineClass.Queue != []:
# get the job
J = MachineClass.Queue.pop(0)
# do the work
yield hold,self,G.Rnd.expovariate(MachineClass.SrvRate)
# bookkeeping
MachineClass.NDone += 1
MachineClass.TotWait += now() - J.ArrivalTime
MachineClass.Busy.remove(self)
MachineClass.Idle.append(self)
class JobClass:
def __init__(self):
self.ArrivalTime = now()
class ArrivalClass(Process):
ArvRate = None
def __init__(self):
Process.__init__(self)
def Run(self):
while 1:
# wait for arrival of next job
yield hold,self,G.Rnd.expovariate(ArrivalClass.ArvRate)
J = JobClass()
MachineClass.Queue.append(J)
# any machine ready?
if MachineClass.Idle != []:
reactivate(MachineClass.Idle[0])
def main():
NMachines = 1 #int(sys.argv[1])
ArrivalClass.ArvRate =2 #float(sys.argv[2])
MachineClass.SrvRate =10 #float(sys.argv[3])
# initialize()
for I in range(NMachines):
M = MachineClass()
activate(M,M.Run())
A = ArrivalClass()
activate(A,A.Run())
MaxSimtime =60 #float(sys.argv[4])
simulate(until=MaxSimtime)
print MachineClass.TotWait/MachineClass.NDone
if __name__ == "__main__": main()