I believe it is possible to have yield work from a nested function. In all the examples I have reviewed all yield request, yield hold, and yield release statements occur in the function called by the activate statement. I would like the function called by the activate statement call other functions that would then execute the yield statements. I've attached an example based roughly on the penny fab example from "Factory Physics" by Hopp and Spearman. What I'm after here is the ability to easily code re-work sequences or a job shop environment where there are many different possible routes through a production line.
Travis
from SimPy.Simulation import *
from random import uniform
class pennyfab(Process):
def __init__(self,name):
self.start=0
self.finish=0
Process.__init__(self,name)
def fab_punch(self):
print self.name, ',request,1,punch,',now()
yield request, self, punching
print self.name, ',start,1,punch,',now()
yield hold, self, 2+float(int(uniform(1,2/4*10)))/100
yield release, self, punching
print self.name,',finish,1,punch,',now()
def fab_stamp(self):
print self.name, ',request,1,stamp,',now()
yield request, self, stamping
print self.name, ',start,1,stamp,',now()
yield hold, self, 4+float(int(uniform(1,4/4*10)))/100
yield release, self, stamping
print self.name,',finish,1,stamp,',now()
def fab_rim(self):
print self.name, ',request,1,rim,',now()
yield request, self, rimming
print self.name, ',start,1,rim,',now()
yield hold, self, 10+float(int(uniform(1,10/4*10)))/100
yield release, self, rimming
print self.name,',finish,1,rim,',now()
def fab_deburr(self):
print self.name, ',request,1,deburr,',now()
yield request, self, deburring
print self.name, ',start,1,deburr,',now()
yield hold, self, 3+float(int(uniform(1,3/4*10)))/100
yield release, self, deburring
print self.name,',finish,1,deburr,',now()
def fab(self):
yield request, self, wip
self.start=now()
self.fab_punch()
if uniform(1,4)<2:
self.fab_punch() # rework punch
self.fab_stamp()
if uniform(1,4)<2:
self.fab_punch() # rework punch and stamp
self.fab_stamp()
self.fab_rim()
self.fab_deburr()
if uniform(1,4)<2:
self.fab_stamp() # rework stamp and deburr
self.fab_deburr()
self.finish=now()
yield release, self, wip
initialize()
punching=Resource(capacity=1,monitored=True,monitorType=Monitor)
stamping=Resource(capacity=2,monitored=True,monitorType=Monitor)
rimming=Resource(capacity=3,monitored=True,monitorType=Monitor)
deburring=Resource(capacity=1,monitored=True,monitorType=Monitor)
wip=Resource(capacity=6,monitored=True,monitorType=Monitor) # implements CONWIP
penny=range(20)
for j in range(20):
penny[j]=pennyfab(name='penny'+str(j))
activate(penny[j],penny[j].fab())
simulate(until=500)
print ' '
print '*'*40
print '*'*11,' END SIMULATION ','*'*11
print '*'*40
print ' '
print 'Time history for WIP'
print '[time, activeQ]'
for item in wip.actMon:
print item
print '-'*40
print 'Cycle Times'
average=0
for j in range(20):
print penny[j].name,',',penny[j].finish-penny[j].start
average=average+penny[j].finish-penny[j].start
print 'Average Cycle Time'
print average/20
print 'Lot Cycle Time'
print penny[19].finish-penny[0].start
print ' '
print '*'*40
print '*'*13,' END STATS ','*'*14
print '*'*40