jsonData=json.loads(ant['resultJSON'])#read the result as JSON
elementList=jsonData['elementList']#find the route of JSON
#loop through the elements
forelementinelementList:
elementClass=element['_class']#get the class
#id the class is Job
ifelementClass=='Dream.Job':
results=element['results']
delay=float(results.get('delay',"0"))
totalDelay+=delay
returntotalDelay
#creates a light coded form of the ant and tests it
defcheckIfAntTested(ant):
#code the ant in a lighter string form
codedAnt=''
forkeyinant:
codedAnt+=ant[key]
#if it already exists return true so that it will not be tested
ifcodedAntinG.CodedAnstsList:
returnTrue
#else add it to the list and return false
else:
G.CodedAnstsList.append(codedAnt)
returnFalse
'''
Below are initial lists of scheduling rules to be considered for each of the queues/machine, these could be entered at GUI level
I believe the ability to attribute each of these lists to a queue object is the only requirements at GUI level. Everything henceforth is a ManPy and Optimization business.
'''
M1Options=['EDD','WINQ','LPT','SPT','ERD','PCO','MS']#initial list of scheduling rules that are to be randomly used for each of the machines
#Optimization takes over from here and it calls ManPy simulation at intervals using the sim function
defmain():
G.CodedAnstsList=[]#a list to keep all the solutions in a coded form
start=time.time()# start counting execution time
modelJSONFile=open("C:\Users\George\dream\dream\simulation\JSONInputs\Topology20.JSON","r")#the JSON of the model. To be sent from GUI
modelJSONData=modelJSONFile.read()
modelJSON=json.loads(modelJSONData)
modelJSON=json.dumps(modelJSON,indent=True)
#collated = {'Q1':M1Options,'Q2':M2Options,'Q3':M3Options,'Q4':M4Options,'Q5':M5Options} #the list of options collated into a dictionary for ease of referencing in ManPy
collated={'Q1':M1Options,'Q2':M2Options,'Q3':M3Options}#the list of options collated into a dictionary for ease of referencing in ManPy
ants=[]#list of ants for keeping track of their performance
foriinrange(10):#Number of times new ants are to be created, i.e. number of generations (a generation can have more than 1 ant)
forjinrange(20):#number of ants created per generation
ant={}#an ant dictionary to contain rule to queue assignment information
forkincollated.keys():#for each of the machines, rules are randomly picked from the options list
ant[str(k)]=random.choice(collated[str(k)])
#if the ant was not already tested, only then test it
ifnotcheckIfAntTested(ant):
ants.append(ant)#the current ant to be simulated (evaluated) is added to the ants list
ants=ranking(ants,4)#the ants in this generation are ranked based on their scores and the best 4 are selected
forlinants:#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
formincollated.keys():#e.g. if using EDD gave good performance for Queue 1, then another 'EDD' is added to M1Options so there is a higher chance that it is selected by the next ants.