def scheduleSex(baseParent, donorParent, randGen): #A new child schedule is created by first cloning the baseParent. The child #is then modified via accepting a few "traits" of the donorParent. newSched = copy.deepcopy(baseParent) searchingForEntry = 1 swapsCount = randGen.randint(1, MAX_SWAPS_PER_RHOMP) index1 = 0 index2 = -1 tempEntry = {} #start = time.time() for x in list(range(swapsCount)): searchingForEntry = 1 while searchingForEntry: index1 = randGen.randint(0, (len(newSched['lst'])) - 1) if 'entry' in newSched['lst'][index1]: searchingForEntry = 0 #end loop #print('Searching %s %s' % (newSched['lst'][index1]['entry']['school'], newSched['lst'][index1]['entry']['entryTitle'])) if 'entry' in donorParent['lst'][index1]: tempEntry = donorParent['lst'][index1]['entry'] #Now find where the entry from parent 2 is located in parent 1 for y in list(range(len(newSched['lst']))): if 'entry' in newSched['lst'][y] and \ newSched['lst'][y]['entry']['index'] == tempEntry['index']: index2 = y break else: print('newSched[\'lst\'][index1] :') print(newSched['lst'][index1]) schedIO.printSched(newSched, 'newSched.txt') #WILL FAIL schedIO.printSched(donorParent, 'donorParent.txt') checkSchedForDupes(donorParent) sys.exit("No match in donorParent") else: #donorParent has an unfilled session in that spot. Find a corresponding one in newSched newSchedIndexList = list(range(len(newSched['lst']))) randGen.shuffle(newSchedIndexList) #Randomize the list for z in newSchedIndexList: if 'entry' not in newSched['lst'][z] and \ newSched['lst'][z]['isBreak'] == False and \ newSched['lst'][z]['catShort'] == newSched['lst'][index1]['catShort']: index2 = z break #end if if index1 == index2: #The random entry to swap is already in the same place #in both parents, no need to do anything #This is possibly not needed since the swap algorithm won't #harm anything. continue if newSched['lst'][index1]['catShort'] != newSched['lst'][index2][ 'catShort']: #This algorithm assumes that for any index (q) in baseParent array, #the catagory of baseParent(q) is equal to the catagory of donorParent(q) sys.exit("Woa nelly sumthins mixed up") #Its possible that index2 doesn't have an entry. if 'entry' in newSched['lst'][index2]: tempEntry = newSched['lst'][index1]['entry'] newSched['lst'][index1]['entry'] = newSched['lst'][index2]['entry'] newSched['lst'][index2]['entry'] = tempEntry else: newSched['lst'][index2]['entry'] = newSched['lst'][index1]['entry'] del (newSched['lst'][index1]['entry']) #if checkSchedForDupes(newSched): # schedIO.printSched(newSched, 'newSched.txt') # sys.exit('Got dupes printed newSched') #entryCount = 0 #for sched in newSched: # if 'entry' in sched: # entryCount += 1 #if entryCount != len(entriesList): # sys.exit('Entry count fail after 2') #end loop schedFitness.fitnessTest(newSched, False) #end = time.time() #print ('Sex %d %f' % (swapsCount,(end - start))) return newSched
if swapSchool2 == 'E': schedule['lst'][choice2ListIdx]['entry'] = schedule['lst'][choice1ListIdx]['entry'] del schedule['lst'][choice1ListIdx]['entry'] else: temp1Copy = schedule['lst'][choice1ListIdx]['entry'] schedule['lst'][choice1ListIdx]['entry'] = schedule['lst'][choice2ListIdx]['entry'] schedule['lst'][choice2ListIdx]['entry'] = temp1Copy schedFitness.fitnessTest (schedl = schedule, \ saveReport = False) newSchedFolder = os.path.join(schedDir, 'edits', str(int(schedule['score']))) tempDir = newSchedFolder while os.path.isdir(tempDir): tempDir = newSchedFolder + folderChar folderChar = chr(ord(folderChar) + 1) newSchedFolder = tempDir schedIO.printSched (schedule = schedule, \ schoolInf = schoolInfo, \ entriesLst = entryList, \ outFolder = newSchedFolder) schedFitness.fitnessTest (schedl = schedule, \ saveReport = True, \ fileName = os.path.join(newSchedFolder,'fitnessReport.txt')) print ('New Score %d' % schedule['score']) userIn = input('Press [x] to exit, [ENTER] to continue editing schedule: ') userContinue = userIn.find('x') == -1
taskQueue.put({'cmd':'Mutate', 'idx':mutateIdx, 'sch1':rdm[mutateIdx]}) taskQueue.put({'cmd':'Done'}) #End of job marker jobCurrentSize = min(jobCurrentSize + jobSizeIncr, jobMaxSize) if jobCurrentSize == jobMaxSize - 10: logger.msg('*') #This is a good time to do work while waiting for the child procs to finish #Print out the best score every so often if time.time() - lastScorePrintTime > config['BEST_SCORE_PRINT_MINS'] * 60: if stageNum == 1 and len(topScores) > 0: (lowestScore, lowestScoreIdx) = computeTopScore (topScores) scorePrintFolder = os.path.join(outFolder, str(math.floor(lowestScore))) if not os.path.exists(scorePrintFolder): schedIO.printSched (schedule = topScores[lowestScoreIdx], \ schoolInf = schoolInfo, \ outFolder = scorePrintFolder) schedFitness.fitnessTest (schedl = topScores[lowestScoreIdx], \ saveReport = True, \ fileName = os.path.join(scorePrintFolder, 'FitnessReport.txt')) validateSched(topScores[lowestScoreIdx], sessionList, entriesList, logger) elif stageNum == 2 and len(rdm) > 0: (lowestScore, lowestScoreIdx) = computeTopScore (rdm) scorePrintFolder = os.path.join(outFolder, str(math.floor(lowestScore))) if not os.path.exists(scorePrintFolder): schedIO.printSched (schedule = rdm[lowestScoreIdx], \ schoolInf = schoolInfo, \ outFolder = scorePrintFolder) schedFitness.fitnessTest (schedl = rdm[lowestScoreIdx], \ saveReport = True, \ fileName = os.path.join(scorePrintFolder, 'FitnessReport.txt'))
def scheduleSex(baseParent, donorParent, randGen): #A new child schedule is created by first cloning the baseParent. The child #is then modified via accepting a few "traits" of the donorParent. newSched = copy.deepcopy(baseParent) searchingForEntry = 1 swapsCount = randGen.randint(1,MAX_SWAPS_PER_RHOMP) index1 = 0 index2 = -1 tempEntry = {} #start = time.time() for x in list(range(swapsCount)): searchingForEntry = 1 while searchingForEntry: index1 = randGen.randint(0, (len(newSched['lst']))-1) if 'entry' in newSched['lst'][index1]: searchingForEntry = 0 #end loop #print('Searching %s %s' % (newSched['lst'][index1]['entry']['school'], newSched['lst'][index1]['entry']['entryTitle'])) if 'entry' in donorParent['lst'][index1]: tempEntry = donorParent['lst'][index1]['entry'] #Now find where the entry from parent 2 is located in parent 1 for y in list(range(len(newSched['lst']))): if 'entry' in newSched['lst'][y] and \ newSched['lst'][y]['entry']['index'] == tempEntry['index']: index2 = y break else: print ('newSched[\'lst\'][index1] :') print (newSched['lst'][index1]) schedIO.printSched(newSched, 'newSched.txt') #WILL FAIL schedIO.printSched(donorParent, 'donorParent.txt') checkSchedForDupes(donorParent) sys.exit ("No match in donorParent") else: #donorParent has an unfilled session in that spot. Find a corresponding one in newSched newSchedIndexList = list(range(len(newSched['lst']))) randGen.shuffle (newSchedIndexList) #Randomize the list for z in newSchedIndexList: if 'entry' not in newSched['lst'][z] and \ newSched['lst'][z]['isBreak'] == False and \ newSched['lst'][z]['catShort'] == newSched['lst'][index1]['catShort']: index2 = z break #end if if index1 == index2: #The random entry to swap is already in the same place #in both parents, no need to do anything #This is possibly not needed since the swap algorithm won't #harm anything. continue if newSched['lst'][index1]['catShort'] != newSched['lst'][index2]['catShort']: #This algorithm assumes that for any index (q) in baseParent array, #the catagory of baseParent(q) is equal to the catagory of donorParent(q) sys.exit ("Woa nelly sumthins mixed up") #Its possible that index2 doesn't have an entry. if 'entry' in newSched['lst'][index2]: tempEntry = newSched['lst'][index1]['entry'] newSched['lst'][index1]['entry'] = newSched['lst'][index2]['entry'] newSched['lst'][index2]['entry'] = tempEntry else: newSched['lst'][index2]['entry'] = newSched['lst'][index1]['entry'] del(newSched['lst'][index1]['entry']) #if checkSchedForDupes(newSched): # schedIO.printSched(newSched, 'newSched.txt') # sys.exit('Got dupes printed newSched') #entryCount = 0 #for sched in newSched: # if 'entry' in sched: # entryCount += 1 #if entryCount != len(entriesList): # sys.exit('Entry count fail after 2') #end loop schedFitness.fitnessTest(newSched, False) #end = time.time() #print ('Sex %d %f' % (swapsCount,(end - start))) return newSched
prevBestScore = 999999999999 bestPrintedScore = 999999999999 scoreLastImprovedTime = time.time() jobMaxSize = 1000 jobMinSize = 10 jobSizeIncr = 10 lastScorePrintTime = time.time() logger.msg('Start Main Loop') jobCurrentSize = jobMinSize #DEBUG for getting a quick printout schedIO.printSched (schedule = rdm[2], \ schoolInf = schoolInfo, \ entriesLst = entriesList, \ outFolder = outFolder + '/xx') schedFitness.fitnessTest (schedl = rdm[2], \ saveReport = True, \ fileName = os.path.join(outFolder + '/xx', 'FitnessReport.txt')) if dryRunMode: logger.msg('Finished dry run') exit() while True: parentsList = findParentSchedules(rdm, jobCurrentSize, randGenMain) for x in parentsList: taskQueue.put({ 'cmd': 'XOver', 'idx': x[0],