Commit 6c5577e4 authored by Georgios Dagkakis's avatar Georgios Dagkakis

first version working with both layers of stochastic evaluation. Debugging needed

parent 91540be5
...@@ -42,7 +42,6 @@ class BatchesStochasticACO(BatchesACO): ...@@ -42,7 +42,6 @@ class BatchesStochasticACO(BatchesACO):
if distribution: if distribution:
mean=distribution['mean'] mean=distribution['mean']
if mean: if mean:
print node['id']
processingTime.pop('Fixed',None) processingTime.pop('Fixed',None)
processingTime['Triangular']={ processingTime['Triangular']={
"mean":mean, "mean":mean,
...@@ -51,10 +50,25 @@ class BatchesStochasticACO(BatchesACO): ...@@ -51,10 +50,25 @@ class BatchesStochasticACO(BatchesACO):
} }
return data return data
def calculateStochasticAntScore(self, ant):
"""Calculate the score of this ant.
"""
result, = ant['result']['result_list'] #read the result as JSON
#loop through the elements
for element in result['elementList']:
element_family = element.get('family', None)
#id the class is Exit get the unitsThroughput
if element_family == 'Exit':
unitsThroughput=element['results'].get('unitsThroughput',None)
averageUnitsThroughput=sum(unitsThroughput)/float(len(unitsThroughput))
# return the negative value since they are ranked this way. XXX discuss this
return -averageUnitsThroughput
def run(self, data): def run(self, data):
"""Preprocess the data. """Preprocess the data.
""" """
print 'I am IN' print 'I am IN'
distributor_url = data['general'].get('distributorURL') distributor_url = data['general'].get('distributorURL')
distributor = None distributor = None
if distributor_url: if distributor_url:
...@@ -77,8 +91,12 @@ class BatchesStochasticACO(BatchesACO): ...@@ -77,8 +91,12 @@ class BatchesStochasticACO(BatchesACO):
numberOfAntsForNextGeneration=int(data['general'].get('numberOfAntsForNextGeneration',1)) numberOfAntsForNextGeneration=int(data['general'].get('numberOfAntsForNextGeneration',1))
# this is for how many ants should be evaluated stochastically in every generation # this is for how many ants should be evaluated stochastically in every generation
numberOfAntsForStochasticEvaluationInGeneration=int(data['general'].get('numberOfAntsForStochasticEvaluationInGeneration',2)) numberOfAntsForStochasticEvaluationInGeneration=int(data['general'].get('numberOfAntsForStochasticEvaluationInGeneration',2))
# number of replications for stochastic ants inside the generation
numberOfReplicationsInGeneration=data['general'].get('numberOfReplicationsInGeneration',3)
# this is for how many ants should be evaluated stochastically in the end # this is for how many ants should be evaluated stochastically in the end
numberOfAntsForStochasticEvaluationInTheEnd=int(data['general'].get('numberOfAntsForStochasticEvaluationInTheEnd',2)) numberOfAntsForStochasticEvaluationInTheEnd=int(data['general'].get('numberOfAntsForStochasticEvaluationInTheEnd',2))
# number of replications for stochastic ants in the end
numberOfReplicationsInTheEnd=data['general'].get('numberOfReplicationsInTheEnd',6)
assert max_results >= 1 assert max_results >= 1
...@@ -91,6 +109,7 @@ class BatchesStochasticACO(BatchesACO): ...@@ -91,6 +109,7 @@ class BatchesStochasticACO(BatchesACO):
# generation can have more than 1 ant) # generation can have more than 1 ant)
seedPlus = 0 seedPlus = 0
for i in range(int(data["general"]["numberOfGenerations"])): for i in range(int(data["general"]["numberOfGenerations"])):
print 'Generation',i+1
antsInCurrentGeneration=[] antsInCurrentGeneration=[]
scenario_list = [] # for the distributor scenario_list = [] # for the distributor
# number of ants created per generation # number of ants created per generation
...@@ -109,15 +128,19 @@ class BatchesStochasticACO(BatchesACO): ...@@ -109,15 +128,19 @@ class BatchesStochasticACO(BatchesACO):
# TODO: function to calculate ant id. Store ant id in ant dict # TODO: function to calculate ant id. Store ant id in ant dict
ant_key = repr(ant) ant_key = repr(ant)
# if the ant was not already tested, only then test it # if the ant was not already tested, only then test it
print 'ants created'
if ant_key not in tested_ants: if ant_key not in tested_ants:
tested_ants.add(ant_key) tested_ants.add(ant_key)
ant_data=deepcopy(self.createAntData(data, ant)) ant_data=deepcopy(self.createAntData(data, ant))
ant['key'] = ant_key ant['key'] = ant_key
ant['input'] = ant_data ant['input'] = ant_data
scenario_list.append(ant) scenario_list.append(ant)
print ant['key']
# run the deterministic ants # run the deterministic ants
for ant in scenario_list: for ant in scenario_list:
print 'running deterministic'
print ant['key']
ant['result'] = self.runOneScenario(ant['input'])['result'] ant['result'] = self.runOneScenario(ant['input'])['result']
...@@ -134,10 +157,8 @@ class BatchesStochasticACO(BatchesACO): ...@@ -134,10 +157,8 @@ class BatchesStochasticACO(BatchesACO):
uniqueAntsInThisGeneration = dict() uniqueAntsInThisGeneration = dict()
for ant in antsInCurrentGeneration: for ant in antsInCurrentGeneration:
ant_result, = copy(ant['result']['result_list']) ant_result, = copy(ant['result']['result_list'])
# ant_result['general'].pop('totalExecutionTime', None)
ant_result = json.dumps(ant_result, sort_keys=True) ant_result = json.dumps(ant_result, sort_keys=True)
uniqueAntsInThisGeneration[ant_result] = ant uniqueAntsInThisGeneration[ant_result] = ant
print ant_result
# The ants in this generation are ranked based on their scores and the # The ants in this generation are ranked based on their scores and the
# best (numberOfAntsForStochasticEvaluationInGeneration) are selected to # best (numberOfAntsForStochasticEvaluationInGeneration) are selected to
...@@ -145,21 +166,37 @@ class BatchesStochasticACO(BatchesACO): ...@@ -145,21 +166,37 @@ class BatchesStochasticACO(BatchesACO):
antsForStochasticEvaluationInGeneration = sorted(uniqueAntsInThisGeneration.values(), antsForStochasticEvaluationInGeneration = sorted(uniqueAntsInThisGeneration.values(),
key=operator.itemgetter('score'))[:numberOfAntsForStochasticEvaluationInGeneration] key=operator.itemgetter('score'))[:numberOfAntsForStochasticEvaluationInGeneration]
# # The ants in this generation are ranked based on their scores and the for ant in antsForStochasticEvaluationInGeneration:
# # best (numberOfAntsForNextGeneration) are selected to carry their pheromones to next generation ant['input']=self.createStochasticData(ant['input'])
# antsForNextGeneration = sorted(uniqueAntsInThisGeneration.values(), ant['input']['general']['numberOfReplications']=numberOfReplicationsInGeneration
# key=operator.itemgetter('score'))[:numberOfAntsForNextGeneration] print 'running stochastic for',numberOfReplicationsInGeneration,'replications'
# print ant['key']
# for l in antsForNextGeneration: ant['result'] = self.runOneScenario(ant['input'])['result']
# # update the options list to ensure that good performing queue-rule ant['score'] = self.calculateStochasticAntScore(ant)
# # combinations have increased representation and good chance of
# # being selected in the next generation # if we had stochastic evaluation keep only those ants in sorting
# for m in collated.keys(): if numberOfAntsForStochasticEvaluationInGeneration:
# # e.g. if using EDD gave good performance for Q1, then another uniqueAntsInThisGeneration = dict()
# # 'EDD' is added to Q1 so there is a higher chance that it is for ant in antsForStochasticEvaluationInGeneration:
# # selected by the next ants. ant_result, = copy(ant['result']['result_list'])
# collated[m].append(l[m]) ant_result = json.dumps(ant_result, sort_keys=True)
uniqueAntsInThisGeneration[ant_result] = ant
antsForNextGeneration = sorted(uniqueAntsInThisGeneration.values(),
key=operator.itemgetter('score'))[:numberOfAntsForNextGeneration]
for l in antsForNextGeneration:
print l['key'], 'will carry pheromone next generation'
# update the options list to ensure that good performing queue-rule
# combinations have increased representation and good chance of
# being selected in the next generation
for m in collated.keys():
# e.g. if using EDD gave good performance for Q1, then another
# 'EDD' is added to Q1 so there is a higher chance that it is
# selected by the next ants.
collated[m].append(l[m])
print 'ACO Ended, post processing to follow for ',numberOfAntsForStochasticEvaluationInTheEnd,'Ants'
# from all the ants in the experiment remove ants that outputs the same schedules # from all the ants in the experiment remove ants that outputs the same schedules
# XXX we in fact remove ants that produce the same output json # XXX we in fact remove ants that produce the same output json
uniqueAnts = dict() uniqueAnts = dict()
...@@ -169,8 +206,21 @@ class BatchesStochasticACO(BatchesACO): ...@@ -169,8 +206,21 @@ class BatchesStochasticACO(BatchesACO):
ant_result = json.dumps(ant_result, sort_keys=True) ant_result = json.dumps(ant_result, sort_keys=True)
uniqueAnts[ant_result] = ant uniqueAnts[ant_result] = ant
# The ants in this generation are ranked based on their scores and the # The ants are ranked based on their scores and the
# best (max_results) are selected # best (max_results) are selected to be returned
if numberOfAntsForStochasticEvaluationInTheEnd > 0:
ants = sorted(uniqueAnts.values(),
key=operator.itemgetter('score'))[:numberOfAntsForStochasticEvaluationInTheEnd]
for ant in ants:
ant['input']=self.createStochasticData(ant['input'])
ant['input']['general']['numberOfReplications']=numberOfReplicationsInTheEnd
print 'running stochastic for',numberOfReplicationsInTheEnd,'replications'
print ant['key']
ant['result'] = self.runOneScenario(ant['input'])['result']
ant['score'] = self.calculateStochasticAntScore(ant)
# The ants are ranked based on their scores and the
# best (max_results) are selected to be returned
ants = sorted(uniqueAnts.values(), ants = sorted(uniqueAnts.values(),
key=operator.itemgetter('score'))[:max_results] key=operator.itemgetter('score'))[:max_results]
......
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