Commit f465f8e4 authored by Ioannis Papagiannopoulos's avatar Ioannis Papagiannopoulos Committed by Jérome Perrin

redundant code removed

parent 8e44904f
......@@ -91,9 +91,3 @@ class ObjectResource(object):
def getResourceQueue(self):
return self.Res.activeQ
# =======================================================================
# check availability of the resource
# =======================================================================
def isResourceFree(self):
if len(self.getResourceQueue())==0:
return True
\ No newline at end of file
......@@ -39,7 +39,7 @@ import scipy.stats as stat
# the Machine object
# ===========================================================================
# class Machine(CoreObject):
class TestOperatedMachine(Machine):
class OperatedMachine(Machine):
# =======================================================================
# initialize the id the capacity, of the resource and the distribution
# =======================================================================
......@@ -69,6 +69,7 @@ class TestOperatedMachine(Machine):
self.multOperationTypeList = OTlist
else:
self.multOperationTypeList.append(self.operationType)
# =======================================================================
......@@ -79,6 +80,7 @@ class TestOperatedMachine(Machine):
self.broker = Broker(self)
self.call=False
self.set=False
self.totalTimeWaitingForOperator=0
# =======================================================================
# the main process of the machine
......@@ -91,7 +93,7 @@ class TestOperatedMachine(Machine):
# canAcceptAndIsRequested is invoked to check when the machine requested to receive an entity
yield waituntil, self, self.canAcceptAndIsRequested
# request a resource
# ======= request a resource
if(self.operator!="None"):
# when it's ready to accept then inform the broker
self.invokeBroker()
......@@ -101,7 +103,7 @@ class TestOperatedMachine(Machine):
# get the entity
self.getEntity()
# release a resource
# ======= release a resource
if (self.operator!="None")\
and any(type=="Setup" for type in self.multOperationTypeList)\
and not any(type=="Processing" for type in self.multOperationTypeList):
......@@ -156,9 +158,7 @@ class TestOperatedMachine(Machine):
# start counting the down time at breatTime dummy variable
breakTime=now() # dummy variable that the interruption happened
# +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
# release the operator if there is failure
# +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
# =============== release the operator if there is failure
if (self.operator!="None")\
and any(type=="Processing" for type in self.multOperationTypeList):
self.invokeBroker()
......@@ -174,9 +174,7 @@ class TestOperatedMachine(Machine):
# output to trace that the Machine self.objName was passivated for the current failure time
self.outputTrace(self.getActiveObjectQueue()[0].name, "passivated in "+self.objName+" for "+str(now()-breakTime))
# +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
# request a resource after the repair
# +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
# =============== request a resource after the repair
if (self.operator!="None")\
and any(type=="Processing" for type in self.multOperationTypeList)\
and not interruption:
......@@ -189,9 +187,7 @@ class TestOperatedMachine(Machine):
# output to trace that the processing in the Machine self.objName ended
self.outputTrace(self.getActiveObjectQueue()[0].name,"ended processing in "+self.objName)
# +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
# release resource after the end of processing
# +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
# =============== release resource after the end of processing
if any(type=="Processing" for type in self.multOperationTypeList)\
and not iterruption:
self.invokeBroker()
......@@ -264,13 +260,13 @@ class TestOperatedMachine(Machine):
# this is done to achieve better (cpu) processing time
# then we can also use it as a filter for a yield method
if(len(activeObject.previous)==1 or callerObject==None):
return (activeObject.operator=='None' or activeObject.operator.isResourceFree())\
return (activeObject.operator=='None' or activeObject.operator.checkIfResourceIsAvailable())\
and activeObject.Up and len(activeObjectQueue)==0
thecaller=callerObject
# return True ONLY if the length of the activeOjbectQue is smaller than
# the object capacity, and the callerObject is not None but the giverObject
return (activeObject.operator=='None' or activeObject.operator.isResourceFree())\
return (activeObject.operator=='None' or activeObject.operator.checkIfResourceIsAvailable())\
and len(activeObjectQueue)<activeObject.capacity and (thecaller is giverObject)
# =======================================================================
......@@ -288,7 +284,7 @@ class TestOperatedMachine(Machine):
# the machine is up and the predecessor has an entity to dispose
# this is done to achieve better (cpu) processing time
if(len(activeObject.previous)==1):
return (activeObject.operator=='None' or activeObject.operator.isResourceFree())\
return (activeObject.operator=='None' or activeObject.operator.checkIfResourceIsAvailable())\
and activeObject.Up and len(activeObjectQueue)<activeObject.capacity\
and giverObject.haveToDispose(activeObject)
......@@ -311,7 +307,7 @@ class TestOperatedMachine(Machine):
activeObject.predecessorIndex=i # the object to deliver the Entity to the activeObject is set to the ith member of the previous list
maxTimeWaiting=timeWaiting
i+=1 # in the next loops, check the other predecessors in the previous list
return (activeObject.operator=='None' or activeObject.operator.isResourceFree())\
return (activeObject.operator=='None' or activeObject.operator.checkIfResourceIsAvailable())\
and activeObject.Up and len(activeObjectQueue)<activeObject.capacity and isRequested
# =======================================================================
......@@ -348,6 +344,7 @@ class Broker(Process):
self.operatedMachine=operatedMachine # the machine that is handled by the broker
self.setupTime = 0
self.timeOperationStarted = 0;
self.timeWaitForOperatorStarted=0
# =======================================================================
# exit the broker
......@@ -359,24 +356,26 @@ class Broker(Process):
def run(self):
while 1:
yield waituntil,self,self.operatedMachine.brokerIsCalled # wait until the broker is called
# request a resource
if self.operatedMachine.operator.isResourceFree()\
# ======= request a resource
if self.operatedMachine.operator.checkIfResourceIsAvailable()\
and any(type=="Setup" or type=="Processing" for type in self.operatedMachine.multOperationTypeList):
# update the time that the station is waiting for the operator
self.timeWaitForOperatorStarted=now()
yield request,self,self.operatedMachine.operator.getResource()
print self.operatedMachine.objName, 'requested', self.operatedMachine.operator.objName
print now()
self.operatedMachine.totalTimeWaitingForOperator+=now()-self.timeWaitForOperatorStarted
# update the time that the operation started
self.timeOperationStarted=now()
self.operatedMachine.operator.timeLastOperationStarted=now()
if any(type=="Setup" for type in self.operatedMachine.multOperationTypeList):
self.setupTime = self.operatedMachine.calculateSetupTime()
yield hold,self,self.setupTime
# release a resource
elif not self.operatedMachine.operator.isResourceFree():
# ======= release a resource
elif not self.operatedMachine.operator.checkIfResourceIsAvailable():
self.operatedMachine.operator.totalWorkingTime+=now()-self.timeOperationStarted
yield release,self,self.operatedMachine.operator.getResource()
print self.operatedMachine.objName, 'released', self.operatedMachine.operator.objName
print now()
else:
pass
......
......@@ -27,129 +27,16 @@ models a repairman that can fix a machine when it gets failures
'''
from SimPy.Simulation import Resource, now
import xlwt
import scipy.stats as stat
from ObjectResource import ObjectResource
from Repairman import Repairman
# ===========================================================================
# the resource that repairs the machines
# the resource that operates the machines
# ===========================================================================
class Operator(ObjectResource):
class Operator(Repairman):
def __init__(self, id, name, capacity=1):
self.id=id
self.objName=name
self.capacity=capacity # operator is an instance of resource
def __init__(self, id, name, capacity=1):
Repairman.__init__(self,id=id,name=name,capacity=capacity)
self.type="Operator"
# self.Res=Resource(self.capacity)
# lists to hold statistics of multiple runs
self.Waiting=[] # holds the percentage of waiting time
self.Working=[] # holds the percentage of working time
# list with the coreObjects that the Operator operates
self.coreObjectIds=[]
# =======================================================================
# actions to be taken after the simulation ends
# =======================================================================
def postProcessing(self, MaxSimtime=None):
if MaxSimtime==None:
from Globals import G
MaxSimtime=G.maxSimTime
# if the repairman is currently working we have to count the time of this work
if not self.isResourceFree():
# if len(self.getResourceQueue())>0:
self.totalWorkingTime+=now()-self.timeLastOperationStarted
# Repairman was idle when he was not in any other state
self.totalWaitingTime=MaxSimtime-self.totalWorkingTime
# update the waiting/working time percentages lists
self.Waiting.append(100*self.totalWaitingTime/MaxSimtime)
self.Working.append(100*self.totalWorkingTime/MaxSimtime)
# =======================================================================
# outputs data to "output.xls"
# =======================================================================
def outputResultsXL(self, MaxSimtime=None):
from Globals import G
if MaxSimtime==None:
MaxSimtime=G.maxSimTime
# if we had just one replication output the results to excel
if(G.numberOfReplications==1):
G.outputSheet.write(G.outputIndex,0, "The percentage of working of "+self.objName +" is:")
G.outputSheet.write(G.outputIndex,1,100*self.totalWorkingTime/MaxSimtime)
G.outputIndex+=1
G.outputSheet.write(G.outputIndex,0, "The percentage of waiting of "+self.objName +" is:")
G.outputSheet.write(G.outputIndex,1,100*self.totalWaitingTime/MaxSimtime)
G.outputIndex+=1
#if we had multiple replications we output confidence intervals to excel
# for some outputs the results may be the same for each run (eg model is stochastic but failures fixed
# so failurePortion will be exactly the same in each run). That will give 0 variability and errors.
# so for each output value we check if there was difference in the runs' results
# if yes we output the Confidence Intervals. if not we output just the fix value
else:
G.outputSheet.write(G.outputIndex,0, "CI "+str(G.confidenceLevel*100)+"% for the mean percentage of Working of "+self.objName +" is:")
if self.checkIfArrayHasDifValues(self.Working):
G.outputSheet.write(G.outputIndex,1,stat.bayes_mvs(self.Working, G.confidenceLevel)[0][1][0])
G.outputSheet.write(G.outputIndex,2,stat.bayes_mvs(self.Working, G.confidenceLevel)[0][0])
G.outputSheet.write(G.outputIndex,3,stat.bayes_mvs(self.Working, G.confidenceLevel)[0][1][1])
else:
G.outputSheet.write(G.outputIndex,1,self.Working[0])
G.outputSheet.write(G.outputIndex,2,self.Working[0])
G.outputSheet.write(G.outputIndex,3,self.Working[0])
G.outputIndex+=1
G.outputSheet.write(G.outputIndex,0, "CI "+str(G.confidenceLevel*100)+"% for the mean percentage of Waiting of "+self.objName +" is:")
if self.checkIfArrayHasDifValues(self.Waiting):
G.outputSheet.write(G.outputIndex,1,stat.bayes_mvs(self.Waiting, G.confidenceLevel)[0][1][0])
G.outputSheet.write(G.outputIndex,2,stat.bayes_mvs(self.Waiting, G.confidenceLevel)[0][0])
G.outputSheet.write(G.outputIndex,3,stat.bayes_mvs(self.Waiting, G.confidenceLevel)[0][1][1])
else:
G.outputSheet.write(G.outputIndex,1,self.Waiting[0])
G.outputSheet.write(G.outputIndex,2,self.Waiting[0])
G.outputSheet.write(G.outputIndex,3,self.Waiting[0])
G.outputIndex+=1
G.outputIndex+=1
# =======================================================================
# outputs results to JSON File
# =======================================================================
def outputResultsJSON(self):
from Globals import G
# if we had just one replication output the results to JSON
if(G.numberOfReplications==1):
json={}
json['_class'] = 'Dream.Repairman';
json['id'] = str(self.id)
json['results'] = {}
json['results']['working_ratio']=100*self.totalWorkingTime/G.maxSimTime
json['results']['waiting_ratio']=100*self.totalWaitingTime/G.maxSimTime
#if we had multiple replications we output confidence intervals to excel
# for some outputs the results may be the same for each run (eg model is stochastic but failures fixed
# so failurePortion will be exactly the same in each run). That will give 0 variability and errors.
# so for each output value we check if there was difference in the runs' results
# if yes we output the Confidence Intervals. if not we output just the fix value
else:
json={}
json['_class'] = 'Dream.Repairman';
json['id'] = str(self.id)
json['results'] = {}
json['results']['working_ratio']={}
if self.checkIfArrayHasDifValues(self.Working):
json['results']['working_ratio']['min']=stat.bayes_mvs(self.Working, G.confidenceLevel)[0][1][0]
json['results']['working_ratio']['avg']=stat.bayes_mvs(self.Working, G.confidenceLevel)[0][0]
json['results']['working_ratio']['max']=stat.bayes_mvs(self.Working, G.confidenceLevel)[0][1][1]
else:
json['results']['working_ratio']['min']=self.Working[0]
json['results']['working_ratio']['avg']=self.Working[0]
json['results']['working_ratio']['max']=self.Working[0]
json['results']['waiting_ratio']={}
if self.checkIfArrayHasDifValues(self.Waiting):
json['results']['waiting_ratio']['min']=stat.bayes_mvs(self.Waiting, G.confidenceLevel)[0][1][0]
json['results']['waiting_ratio']['avg']=stat.bayes_mvs(self.Waiting, G.confidenceLevel)[0][0]
json['results']['waiting_ratio']['max']=stat.bayes_mvs(self.Waiting, G.confidenceLevel)[0][1][1]
else:
json['results']['waiting_ratio']['min']=self.Waiting[0]
json['results']['waiting_ratio']['avg']=self.Waiting[0]
json['results']['waiting_ratio']['max']=self.Waiting[0]
G.outputJSON['elementList'].append(json)
#
......@@ -41,7 +41,7 @@ class Repairman(ObjectResource):
self.objName=name
self.capacity=capacity # repairman is an instance of resource
self.type="Repairman"
self.Res=Resource(self.capacity)
# self.Res=Resource(self.capacity)
# lists to hold statistics of multiple runs
self.Waiting=[] # holds the percentage of waiting time
self.Working=[] # holds the percentage of working time
......@@ -57,7 +57,7 @@ class Repairman(ObjectResource):
MaxSimtime=G.maxSimTime
# if the repairman is currently working we have to count the time of this work
# if len(self.getResourceQueue())>0:
if not self.isResourceFree():
if not self.checkIfResourceIsAvailable():
self.totalWorkingTime+=now()-self.timeLastOperationStarted
# Repairman was idle when he was not in any other state
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment