new sorter for Router

parent 5e3129ab
...@@ -280,7 +280,7 @@ class Router(ObjectInterruption): ...@@ -280,7 +280,7 @@ class Router(ObjectInterruption):
# self.victim.routerCycleOver.signal('router has implemented its logic') # self.victim.routerCycleOver.signal('router has implemented its logic')
#======================================================================= #=======================================================================
# Sort pendingEntities # Sort pendingEntities
# TODO: sorting them according to the operators schedulingRule # TODO: sorting them according to the operators schedulingRule
#======================================================================= #=======================================================================
def sortPendingEntities(self): def sortPendingEntities(self):
...@@ -291,7 +291,9 @@ class Router(ObjectInterruption): ...@@ -291,7 +291,9 @@ class Router(ObjectInterruption):
# TODO: move that piece of code elsewhere, it doesn't look nice here. and there is not point in doing it here # TODO: move that piece of code elsewhere, it doesn't look nice here. and there is not point in doing it here
# maybe it's better in findCandidateOperators method # maybe it's better in findCandidateOperators method
if self.candidateOperators: if self.candidateOperators:
self.activePendingQSorter(criterion=self.schedulingRule) from Globals import G
candidateList=G.pendingEntities
self.activeQSorter(criterion=self.schedulingRule,candList=candidateList)
#======================================================================= #=======================================================================
# Sort candidateOperators # Sort candidateOperators
...@@ -303,7 +305,8 @@ class Router(ObjectInterruption): ...@@ -303,7 +305,8 @@ class Router(ObjectInterruption):
#if we have sorting according to multiple criteria we have to call the sorter many times #if we have sorting according to multiple criteria we have to call the sorter many times
# TODO: find out what happens in case of multiple criteria # TODO: find out what happens in case of multiple criteria
if self.candidateOperators: if self.candidateOperators:
self.activeOperatorQSorter(criterion=self.schedulingRule) candidateList=self.candidateOperators
self.activeQSorter(criterion=self.schedulingRule,candList=candidateList)
#======================================================================== #========================================================================
# Find candidate Operators # Find candidate Operators
...@@ -458,7 +461,6 @@ class Router(ObjectInterruption): ...@@ -458,7 +461,6 @@ class Router(ObjectInterruption):
if operator.candidateEntity: if operator.candidateEntity:
operator.candidateEntity.candidateReceiver=findCandidateReceiver() operator.candidateEntity.candidateReceiver=findCandidateReceiver()
#======================================================================= #=======================================================================
# Sort Givers # Sort Givers
# TODO: the queues of the candidate givers are sorted only if their receiver is not in activeCallersList # TODO: the queues of the candidate givers are sorted only if their receiver is not in activeCallersList
...@@ -482,194 +484,150 @@ class Router(ObjectInterruption): ...@@ -482,194 +484,150 @@ class Router(ObjectInterruption):
else: else:
break break
# =======================================================================
# sorts the Entities of the Queue according to the scheduling rule
# TODO: refine the criteria
# =======================================================================
def activePendingQSorter(self, criterion=None):
from Globals import G
activeObjectQ=G.pendingEntities
if not activeObjectQ:
assert False, "empty candidate list"
if criterion==None:
criterion=self.schedulingRule
#if the schedulingRule is first in first out
if criterion=="FIFO":
# TODO: FIFO sorting has no meaning when sorting candidateEntities
self.activePendingQSorter('WT')
# added for testing
# print 'there is no point of using FIFO scheduling rule for operators candidateEntities,\
# WT scheduling rule used instead'
#if the schedulingRule is based on a pre-defined priority
elif criterion=="Priority":
activeObjectQ.sort(key=lambda x: x.priority)
#if the scheduling rule is time waiting (time waiting of machine
# TODO: consider that the timeLastEntityEnded is not a
# indicative identifier of how long the station was waiting
elif criterion=='WT':
activeObjectQ.sort(key=lambda x: x.schedule[-1][1])
#if the schedulingRule is earliest due date
elif criterion=="EDD":
activeObjectQ.sort(key=lambda x: x.dueDate)
#if the schedulingRule is earliest order date
elif criterion=="EOD":
activeObjectQ.sort(key=lambda x: x.orderDate)
#if the schedulingRule is to sort Entities according to the stations they have to visit
elif criterion=="NumStages":
activeObjectQ.sort(key=lambda x: len(x.remainingRoute), reverse=True)
#if the schedulingRule is to sort Entities according to the their remaining processing time in the system
elif criterion=="RPC":
for entity in activeObjectQ:
RPT=0
for step in entity.remainingRoute:
processingTime=step.get('processingTime',None)
if processingTime:
RPT+=float(processingTime.get('mean',0))
entity.remainingProcessingTime=RPT
activeObjectQ.sort(key=lambda x: x.remainingProcessingTime, reverse=True)
#if the schedulingRule is to sort Entities according to longest processing time first in the next station
elif criterion=="LPT":
for entity in activeObjectQ:
processingTime = entity.remainingRoute[0].get('processingTime',None)
entity.processingTimeInNextStation=float(processingTime.get('mean',0))
if processingTime:
entity.processingTimeInNextStation=float(processingTime.get('mean',0))
else:
entity.processingTimeInNextStation=0
activeObjectQ.sort(key=lambda x: x.processingTimeInNextStation, reverse=True)
#if the schedulingRule is to sort Entities according to shortest processing time first in the next station
elif criterion=="SPT":
for entity in activeObjectQ:
processingTime = entity.remainingRoute[0].get('processingTime',None)
if processingTime:
entity.processingTimeInNextStation=float(processingTime.get('mean',0))
else:
entity.processingTimeInNextStation=0
activeObjectQ.sort(key=lambda x: x.processingTimeInNextStation)
#if the schedulingRule is to sort Entities based on the minimum slackness
elif criterion=="MS":
for entity in activeObjectQ:
RPT=0
for step in entity.remainingRoute:
processingTime=step.get('processingTime',None)
if processingTime:
RPT+=float(processingTime.get('mean',0))
entity.remainingProcessingTime=RPT
activeObjectQ.sort(key=lambda x: (x.dueDate-x.remainingProcessingTime))
#if the schedulingRule is to sort Entities based on the length of the following Queue
elif criterion=="WINQ":
from Globals import G
for entity in activeObjectQ:
nextObjIds=entity.remainingRoute[1].get('stationIdsList',[])
for obj in G.ObjList:
if obj.id in nextObjIds:
nextObject=obj
entity.nextQueueLength=len(nextObject.getActiveObjectQueue())
activeObjectQ.sort(key=lambda x: x.nextQueueLength)
else:
assert False, "Unknown scheduling criterion %r" % (criterion, )
# ======================================================================= # =======================================================================
# sorts the Operators of the Queue according to the scheduling rule # sorts the Operators of the Queue according to the scheduling rule
# ======================================================================= # =======================================================================
def activeOperatorQSorter(self, criterion=None): def activeQSorter(self, criterion=None, candList=[]):
activeObjectQ=self.candidateOperators activeObjectQ=candList
if not activeObjectQ: if not activeObjectQ:
assert False, "empty candidateOperators list" assert False, "empty candidateOperators list"
if criterion==None: if criterion==None:
criterion=self.multipleCriterionList[0] criterion=self.multipleCriterionList[0]
#if the schedulingRule is first in first out #if the schedulingRule is first in first out
if criterion=="FIFO": if criterion=="FIFO":
# FIFO sorting has no meaning when sorting candidateEntities # FIFO sorting has no meaning when sorting candidateEntities
self.activeOperatorQSorter('WT') self.activeQSorter(criterion='WT',candList=activeObjectQ)
# added for testing
# print 'there is no point of using FIFO scheduling rule for operators candidateEntities,\
# WT scheduling rule used instead'
#if the schedulingRule is based on a pre-defined priority #if the schedulingRule is based on a pre-defined priority
elif criterion=="Priority": elif criterion=="Priority":
# if the activeObjectQ is a list of entities then perform the default sorting
activeObjectQ.sort(key=lambda x: x.candidateEntity.priority) try:
activeObjectQ.sort(key=lambda x: x.priority)
# if the activeObjectQ is a list of operators then sort them according to their candidateEntities
except:
activeObjectQ.sort(key=lambda x: x.candidateEntity.priority)
#if the scheduling rule is time waiting (time waiting of machine #if the scheduling rule is time waiting (time waiting of machine
# TODO: consider that the timeLastEntityEnded is not a # TODO: consider that the timeLastEntityEnded is not a
# indicative identifier of how long the station was waiting # indicative identifier of how long the station was waiting
elif criterion=='WT': elif criterion=='WT':
try:
activeObjectQ.sort(key=lambda x: x.candidateEntity.schedule[-1][1]) activeObjectQ.sort(key=lambda x: x.schedule[-1][1])
except:
activeObjectQ.sort(key=lambda x: x.candidateEntity.schedule[-1][1])
#if the schedulingRule is earliest due date #if the schedulingRule is earliest due date
elif criterion=="EDD": elif criterion=="EDD":
try:
activeObjectQ.sort(key=lambda x: x.candidateEntity.dueDate) activeObjectQ.sort(key=lambda x: x.dueDate)
except:
activeObjectQ.sort(key=lambda x: x.candidateEntity.dueDate)
#if the schedulingRule is earliest order date #if the schedulingRule is earliest order date
elif criterion=="EOD": elif criterion=="EOD":
try:
activeObjectQ.sort(key=lambda x: x.candidateEntity.orderDate) activeObjectQ.sort(key=lambda x: x.orderDate)
except:
activeObjectQ.sort(key=lambda x: x.candidateEntity.orderDate)
#if the schedulingRule is to sort Entities according to the stations they have to visit #if the schedulingRule is to sort Entities according to the stations they have to visit
elif criterion=="NumStages": elif criterion=="NumStages":
try:
activeObjectQ.sort(key=lambda x: len(x.candidateEntity.remainingRoute), reverse=True) activeObjectQ.sort(key=lambda x: len(x.remainingRoute), reverse=True)
except:
activeObjectQ.sort(key=lambda x: len(x.candidateEntity.remainingRoute), reverse=True)
#if the schedulingRule is to sort Entities according to the their remaining processing time in the system #if the schedulingRule is to sort Entities according to the their remaining processing time in the system
elif criterion=="RPC": elif criterion=="RPC":
try:
for entity in [operator.candidateEntity for operator in activeObjectQ]: for entity in activeObjectQ:
RPT=0 RPT=0
for step in entity.remainingRoute: for step in entity.remainingRoute:
processingTime=step.get('processingTime',None) processingTime=step.get('processingTime',None)
if processingTime: if processingTime:
RPT+=float(processingTime.get('mean',0)) RPT+=float(processingTime.get('mean',0))
entity.remainingProcessingTime=RPT entity.remainingProcessingTime=RPT
activeObjectQ.sort(key=lambda x: x.candidateEntity.remainingProcessingTime, reverse=True) activeObjectQ.sort(key=lambda x: x.remainingProcessingTime, reverse=True)
except:
for entity in [operator.candidateEntity for operator in activeObjectQ]:
RPT=0
for step in entity.remainingRoute:
processingTime=step.get('processingTime',None)
if processingTime:
RPT+=float(processingTime.get('mean',0))
entity.remainingProcessingTime=RPT
activeObjectQ.sort(key=lambda x: x.candidateEntity.remainingProcessingTime, reverse=True)
#if the schedulingRule is to sort Entities according to longest processing time first in the next station #if the schedulingRule is to sort Entities according to longest processing time first in the next station
elif criterion=="LPT": elif criterion=="LPT":
try:
for entity in [operator.candidateEntity for operator in activeObjectQ]: for entity in activeObjectQ:
processingTime = entity.remainingRoute[0].get('processingTime',None) processingTime = entity.remainingRoute[0].get('processingTime',None)
entity.processingTimeInNextStation=float(processingTime.get('mean',0))
if processingTime:
entity.processingTimeInNextStation=float(processingTime.get('mean',0)) entity.processingTimeInNextStation=float(processingTime.get('mean',0))
else: if processingTime:
entity.processingTimeInNextStation=0 entity.processingTimeInNextStation=float(processingTime.get('mean',0))
activeObjectQ.sort(key=lambda x: x.candidateEntity.processingTimeInNextStation, reverse=True) else:
entity.processingTimeInNextStation=0
activeObjectQ.sort(key=lambda x: x.processingTimeInNextStation, reverse=True)
except:
for entity in [operator.candidateEntity for operator in activeObjectQ]:
processingTime = entity.remainingRoute[0].get('processingTime',None)
entity.processingTimeInNextStation=float(processingTime.get('mean',0))
if processingTime:
entity.processingTimeInNextStation=float(processingTime.get('mean',0))
else:
entity.processingTimeInNextStation=0
activeObjectQ.sort(key=lambda x: x.candidateEntity.processingTimeInNextStation, reverse=True)
#if the schedulingRule is to sort Entities according to shortest processing time first in the next station #if the schedulingRule is to sort Entities according to shortest processing time first in the next station
elif criterion=="SPT": elif criterion=="SPT":
try:
for entity in [operator.candidateEntity for operator in activeObjectQ]: for entity in activeObjectQ:
processingTime = entity.remainingRoute[0].get('processingTime',None) processingTime = entity.remainingRoute[0].get('processingTime',None)
if processingTime: if processingTime:
entity.processingTimeInNextStation=float(processingTime.get('mean',0)) entity.processingTimeInNextStation=float(processingTime.get('mean',0))
else: else:
entity.processingTimeInNextStation=0 entity.processingTimeInNextStation=0
activeObjectQ.sort(key=lambda x: x.candidateEntity.processingTimeInNextStation) activeObjectQ.sort(key=lambda x: x.processingTimeInNextStation)
except:
for entity in [operator.candidateEntity for operator in activeObjectQ]:
processingTime = entity.remainingRoute[0].get('processingTime',None)
if processingTime:
entity.processingTimeInNextStation=float(processingTime.get('mean',0))
else:
entity.processingTimeInNextStation=0
activeObjectQ.sort(key=lambda x: x.candidateEntity.processingTimeInNextStation)
#if the schedulingRule is to sort Entities based on the minimum slackness #if the schedulingRule is to sort Entities based on the minimum slackness
elif criterion=="MS": elif criterion=="MS":
try:
for entity in [operator.candidateEntity for operator in activeObjectQ]: for entity in activeObjectQ:
RPT=0 RPT=0
for step in entity.remainingRoute: for step in entity.remainingRoute:
processingTime=step.get('processingTime',None) processingTime=step.get('processingTime',None)
if processingTime: if processingTime:
RPT+=float(processingTime.get('mean',0)) RPT+=float(processingTime.get('mean',0))
entity.remainingProcessingTime=RPT entity.remainingProcessingTime=RPT
activeObjectQ.sort(key=lambda x: (x.candidateEntity.dueDate-x.candidateEntity.remainingProcessingTime)) activeObjectQ.sort(key=lambda x: (x.dueDate-x.remainingProcessingTime))
except:
for entity in [operator.candidateEntity for operator in activeObjectQ]:
RPT=0
for step in entity.remainingRoute:
processingTime=step.get('processingTime',None)
if processingTime:
RPT+=float(processingTime.get('mean',0))
entity.remainingProcessingTime=RPT
activeObjectQ.sort(key=lambda x: (x.candidateEntity.dueDate-x.candidateEntity.remainingProcessingTime))
#if the schedulingRule is to sort Entities based on the length of the following Queue #if the schedulingRule is to sort Entities based on the length of the following Queue
elif criterion=="WINQ": elif criterion=="WINQ":
try:
from Globals import G from Globals import G
for entity in [operator.candidateEntity for operator in activeObjectQ]: for entity in activeObjectQ:
nextObjIds=entity.remainingRoute[1].get('stationIdsList',[]) nextObjIds=entity.remainingRoute[1].get('stationIdsList',[])
for obj in G.ObjList: for obj in G.ObjList:
if obj.id in nextObjIds: if obj.id in nextObjIds:
nextObject=obj nextObject=obj
entity.nextQueueLength=len(nextObject.getActiveObjectQueue()) entity.nextQueueLength=len(nextObject.getActiveObjectQueue())
activeObjectQ.sort(key=lambda x: x.candidateEntity.nextQueueLength) activeObjectQ.sort(key=lambda x: x.nextQueueLength)
except:
from Globals import G
for entity in [operator.candidateEntity for operator in activeObjectQ]:
nextObjIds=entity.remainingRoute[1].get('stationIdsList',[])
for obj in G.ObjList:
if obj.id in nextObjIds:
nextObject=obj
entity.nextQueueLength=len(nextObject.getActiveObjectQueue())
activeObjectQ.sort(key=lambda x: x.candidateEntity.nextQueueLength)
else: else:
assert False, "Unknown scheduling criterion %r" % (criterion, ) assert False, "Unknown scheduling criterion %r" % (criterion, )
\ No newline at end of file
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