| 1 |
from SimPy.Simulation import * |
| 2 |
from random import uniform,seed |
| 3 |
import string |
| 4 |
|
| 5 |
|
| 6 |
def theTime(time): |
| 7 |
|
| 8 |
hrs=int(time/60) |
| 9 |
min=int(time-hrs*60) |
| 10 |
return "%s:%s" %(string.zfill(str(hrs),2),string.zfill(str(min),2)) |
| 11 |
|
| 12 |
class worker(Process): |
| 13 |
def __init__(self,id,sim): |
| 14 |
Process.__init__(self,sim=sim) |
| 15 |
self.id = id |
| 16 |
self.output = 0 |
| 17 |
self.idle = 0 |
| 18 |
self.total_idle = 0 |
| 19 |
|
| 20 |
def working_day(self,foobar): |
| 21 |
print "%s Worker %s arrives in factory" %(theTime(self.sim.now()),self.id) |
| 22 |
while self.sim.now()<17*60: |
| 23 |
yield hold,self,uniform(3,10) |
| 24 |
|
| 25 |
foobar.queue.append(self) |
| 26 |
if foobar.idle: |
| 27 |
self.sim.reactivate(foobar) |
| 28 |
else: |
| 29 |
self.idle = 1 |
| 30 |
start_idle = self.sim.now() |
| 31 |
|
| 32 |
yield passivate,self |
| 33 |
self.output += 1 |
| 34 |
if self.idle: |
| 35 |
self.total_idle += self.sim.now()-start_idle |
| 36 |
self.idle=0 |
| 37 |
print "%s %s goes home, having built %d widgets today." %(theTime(self.sim.now()),self.id,self.output) |
| 38 |
print "Worker %s was idle waiting for foobar machine for %3.1f hours" %(self.id,self.total_idle/60) |
| 39 |
|
| 40 |
class foobar_machine(Process): |
| 41 |
def __init__(self,sim): |
| 42 |
Process.__init__(self,sim=sim) |
| 43 |
self.queue=[] |
| 44 |
self.idle=1 |
| 45 |
|
| 46 |
def foobar_Process(self): |
| 47 |
yield passivate,self |
| 48 |
while 1: |
| 49 |
while len(self.queue) > 0: |
| 50 |
self.idle=0 |
| 51 |
yield hold,self,3 |
| 52 |
served=self.queue.pop(0) |
| 53 |
self.sim.reactivate(served) |
| 54 |
self.idle=1 |
| 55 |
yield passivate,self |
| 56 |
|
| 57 |
|
| 58 |
class SimPy_Worker_Extend_Model(Simulation): |
| 59 |
def run(self): |
| 60 |
print 'SimPy_worker_extend' |
| 61 |
self.initialize() |
| 62 |
foo=foobar_machine(sim=self) |
| 63 |
self.activate(foo,foo.foobar_Process(),delay=0) |
| 64 |
john=worker("John",sim=self) |
| 65 |
self.activate(john,john.working_day(foo),at=510) |
| 66 |
eve=worker("Eve",sim=self) |
| 67 |
self.activate(eve,eve.working_day(foo),at=510) |
| 68 |
self.simulate(until=18*60) |
| 69 |
|
| 70 |
|
| 71 |
seed(111333555) |
| 72 |
SimPy_Worker_Extend_Model().run() |