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

code and comments clean-up, minor adjustments

parent ef390baa
......@@ -34,25 +34,29 @@ from ObjectInterruption import ObjectInterruption
class Failure(ObjectInterruption):
def __init__(self, victim, dist, MTTF, MTTR, availability, index, repairman):
Process.__init__(self)
self.distType=dist #the distribution that the failure duration follows
self.MTTF=MTTF #the MTTF
self.MTTR=MTTR #the MTTR
self.availability=availability #the availability
self.victim=victim #the victim of the failure (work center)
#Process.__init__(self)
ObjectInterruption.__init__(self,victim)
self.distType=dist # the distribution that the failure duration follows
self.MTTF=MTTF # the MTTF
self.MTTR=MTTR # the MTTR
self.availability=availability # the availability
#self.victim=victim # the victim of the failure (work center)
self.name="F"+str(index)
self.repairman=repairman #the resource that may be needed to fix the failure
#if now resource is needed this will be "None"
self.repairman=repairman # the resource that may be needed to fix the failure
# if now resource is needed this will be "None"
self.type="Failure"
self.id=0
if(self.distType=="Availability"):
#the following are used if we have availability defined (as in plant)
#the erlang is a special case of Gamma.
#To model the Mu and sigma that is given in plant as alpha and beta for gamma you should do the following:
#beta=(sigma^2)/Mu
#alpha=Mu/beta
# --------------------------------------------------------------
# the following are used if we have availability defined
# (as in plant) the erlang is a special case of Gamma.
# To model the Mu and sigma (that is given in plant)
# as alpha and beta for gamma you should do the following:
# beta=(sigma^2)/Mu
# alpha=Mu/beta
# --------------------------------------------------------------
self.AvailabilityMTTF=MTTR*(float(availability)/100)/(1-(float(availability)/100))
self.sigma=0.707106781185547*MTTR
self.theta=(pow(self.sigma,2))/float(MTTR)
......@@ -65,39 +69,44 @@ class Failure(ObjectInterruption):
self.rngTTR=RandomNumberGenerator(self, "Erlang")
self.rngTTR.alpha=self.alpha
self.rngTTR.beta=self.beta
else: #if the distribution is fixed
else:
# --------------------------------------------------------------
# if the distribution is fixed
# --------------------------------------------------------------
self.rngTTF=RandomNumberGenerator(self, self.distType)
self.rngTTF.avg=MTTF
self.rngTTR=RandomNumberGenerator(self, self.distType)
self.rngTTR.avg=MTTR
# =======================================================================
# The run method for the failure which has to served by a repairman
# =======================================================================
def run(self):
while 1:
yield hold,self,self.rngTTF.generateNumber() #wait until a failure happens
yield hold,self,self.rngTTF.generateNumber() # wait until a failure happens
try:
if(len(self.getVictimQueue())>0):
self.interrupt(self.victim) #when a Machine gets failure while in process it is interrupted
if(len(self.getVictimQueue())>0): # when a Machine gets failure
self.interrupt(self.victim) # while in process it is interrupted
self.victim.Up=False
self.victim.timeLastFailure=now()
self.outputTrace("is down")
except AttributeError:
print "AttributeError1"
# update the failure time
failTime=now()
if(self.repairman!="None"): #if the failure needs a resource to be fixed, the machine waits until the
#resource is available
yield request,self,self.repairman.getResource()
# update the time that the repair started
timeRepairStarted=now()
self.repairman.timeLastRepairStarted=now()
yield hold,self,self.rngTTR.generateNumber() #wait until the repairing process is over
yield hold,self,self.rngTTR.generateNumber() # wait until the repairing process is over
self.victim.totalFailureTime+=now()-failTime
try:
if(len(self.getVictimQueue())>0):
reactivate(self.victim) #since repairing is over, the Machine is reactivated
reactivate(self.victim) # since repairing is over, the Machine is reactivated
self.victim.Up=True
self.outputTrace("is up")
if(self.repairman!="None"): #if a resource was used, it is now released
......
......@@ -35,33 +35,36 @@ from random import Random, expovariate, gammavariate, normalvariate
# globals
class G:
seed=1450 #the seed of the random number generator
Rnd = Random(seed) #random number generator
seed=1450 #the seed of the random number generator
Rnd = Random(seed) #random number generator
ObjList=[] #a list that holds all the CoreObjects
EntityList=[] #a list that holds all the Entities
ObjList=[] #a list that holds all the CoreObjects
EntityList=[] #a list that holds all the Entities
numberOfReplications=1 #the number of replications default=1
confidenceLevel=0.9 #the confidence level default=90%
Base=1 #the Base time unit. Default =1 minute
maxSimTime=0 #the total simulation time
numberOfReplications=1 #the number of replications default=1
confidenceLevel=0.9 #the confidence level default=90%
Base=1 #the Base time unit. Default =1 minute
maxSimTime=0 #the total simulation time
#data for the trace output in excel
trace="" #this is written from input. If it is "Yes" then you write to trace, else we do not
traceIndex=0 #index that shows in what row we are
sheetIndex=1 #index that shows in what sheet we are
# data for the trace output in excel
# =======================================================================
trace="" #this is written from input. If it is "Yes" then you write to trace, else we do not
traceIndex=0 #index that shows in what row we are
sheetIndex=1 #index that shows in what sheet we are
traceFile = xlwt.Workbook() #create excel file
traceSheet = traceFile.add_sheet('sheet '+str(sheetIndex), cell_overwrite_ok=True) #create excel sheet
#variables for excel output
outputIndex=0 #index that shows in what row we are
sheetIndex=1 #index that shows in what sheet we are
outputFile = xlwt.Workbook() #create excel file
# variables for excel output
# =======================================================================
outputIndex=0 #index that shows in what row we are
sheetIndex=1 #index that shows in what sheet we are
outputFile = xlwt.Workbook() #create excel file
outputSheet = outputFile.add_sheet('sheet '+str(sheetIndex), cell_overwrite_ok=True) #create excel sheet
#variables for json output
# =======================================================================
outputJSON={}
outputJSONFile=None
......
......@@ -31,6 +31,7 @@ from SimPy.Simulation import Process, Resource
class ObjectInterruption(Process):
def __init__(self, victim):
Process.__init__(self)
self.victim=victim
#the main process of the core object
......
......@@ -26,37 +26,52 @@ Class that acts as an abstract. It should have no instances. All the Resources s
'''
from SimPy.Simulation import Resource
#the resource that repairs the machines
# ===========================================================================
# the resource that repairs the machines
# ===========================================================================
class ObjectResource(object):
def initialize(self):
self.totalWorkingTime=0 #holds the total working time
self.totalWaitingTime=0 #holds the total waiting time
self.totalWorkingTime=0 #holds the total working time
self.totalWaitingTime=0 #holds the total waiting time
self.timeLastRepairStarted=0 #holds the time that the last repair was started
self.Res=Resource(self.capacity)
#checks if the worker is available
# =======================================================================
# checks if the worker is available
# =======================================================================
def checkIfResourceIsAvailable(self):
return len(self.Res.activeQ)<self.capacity
#actions to be taken after the simulation ends
# =======================================================================
# actions to be taken after the simulation ends
# =======================================================================
def postProcessing(self, MaxSimtime=None):
pass
#outputs message to the trace.xls
# =======================================================================
# outputs message to the trace.xls
# =======================================================================
def outputTrace(self, message):
pass
#outputs data to "output.xls"
# =======================================================================
# outputs data to "output.xls"
# =======================================================================
def outputResultsXL(self, MaxSimtime=None):
pass
#outputs results to JSON File
# =======================================================================
# outputs results to JSON File
# =======================================================================
def outputResultsJSON(self):
pass
#takes the array and checks if all its values are identical (returns false) or not (returns true)
#needed because if somebody runs multiple runs in deterministic case it would crash!
# =======================================================================
# takes the array and checks if all its values are identical
# (returns false) or not (returns true) needed because if somebody
# runs multiple runs in deterministic case it would crash!
# =======================================================================
def checkIfArrayHasDifValues(self, array):
difValuesFlag=False
for i in range(1, len(array)):
......@@ -64,10 +79,14 @@ class ObjectResource(object):
difValuesFlag=True
return difValuesFlag
#returns the resource
# =======================================================================
# returns the resource
# =======================================================================
def getResource(self):
return self.Res
#returns the active queue of the resource
# =======================================================================
# returns the active queue of the resource
# =======================================================================
def getResourceQueue(self):
return self.Res.activeQ
\ No newline at end of file
......@@ -31,55 +31,61 @@ import xlwt
import scipy.stats as stat
from ObjectResource import ObjectResource
#the resource that repairs the machines
# ===========================================================================
# the resource that repairs the machines
# ===========================================================================
class Repairman(ObjectResource):
def __init__(self, id, name, capacity=1):
self.id=id
self.objName=name
self.capacity=capacity #repairman is an instance of resource
self.capacity=capacity # repairman is an instance of resource
self.type="Repairman"
self.Res=Resource(self.capacity)
#lists to hold statistics of multiple runs
self.Waiting=[]
self.Working=[]
self.coreObjectIds=[] #list with the coreObjects that the repairman repairs
#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 repairman repairs
self.coreObjectIds=[]
#actions to be taken after the simulation ends
# =======================================================================
# 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 the repairman is currently working we have to count the time of this work
if len(self.getResourceQueue())>0:
self.totalWorkingTime+=now()-self.timeLastRepairStarted
#Repairman was idle when he was not in any other state
# 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"
# =======================================================================
# outputs data to "output.xls"
# =======================================================================
def outputResultsXL(self, MaxSimtime=None):
from Globals import G
if MaxSimtime==None:
MaxSimtime=G.maxSimTime
if(G.numberOfReplications==1): #if we had just one replication output the results to excel
# 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
else: #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
#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])
......@@ -102,21 +108,25 @@ class Repairman(ObjectResource):
G.outputIndex+=1
G.outputIndex+=1
#outputs results to JSON File
# =======================================================================
# outputs results to JSON File
# =======================================================================
def outputResultsJSON(self):
from Globals import G
if(G.numberOfReplications==1): #if we had just one replication output the results to excel
# 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
else: #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
#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)
......
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