def selectTwoIndividuals(fitnessScores, rankedWeights): ind1 = roulette(fitnessScores) ind2 = roulette(fitnessScores) # Variation: is this an unneccessary bottleneck? while ind1 == ind2: ind2 = roulette(fitnessScores) ch1 = deepcopy(rankedWeights[ind1]) ch2 = deepcopy(rankedWeights[ind2]) return ch1, ch2
def selectTwoIndividuals(fitnessScores, rankedWeights): ind1 = roulette(fitnessScores) ind2 = roulette(fitnessScores) # Variation: is this an unneccessary bottleneck? # while ind1 == ind2: # ind2 = roulette(fitnessScores) # need to check the numpy version is equivalent # ch1 = deepcopy(rankedWeights[ind1]) # ch2 = deepcopy(rankedWeights[ind2]) ch1 = [x.copy() for x in rankedWeights[ind1]] ch2 = [x.copy() for x in rankedWeights[ind2]] return ch1, ch2
def setUp(self): self.pop = createPop() self.pairedPop = pairPop(self.pop) self.rankedPop = sorted(self.pairedPop, key=itemgetter(-1), reverse=True) self.rankedWeights = [x[0] for x in self.rankedPop] self.fitnessScores = [x[-1] for x in self.rankedPop] self.copy = array(self.fitnessScores).copy() self.index = roulette(self.fitnessScores)
def __select(): """ select some individuals as new parents, popped from population, and the remnants are kept :return: new parents """ if len( self.population ) <= self.sel_quota: # quota is large enough for the whole population return self.population score_list = [node.score for node in self.population] i_list = [i for i in range(len(self.population))] parents = [] # new parents for _ in range(self.sel_quota): try: i_to_pop, i_population = roulette( i_list, score_list, return_ind=True ) # return (the ith item in i_list, the ith node in population) i_list.pop(i_to_pop) score_list.pop(i_to_pop) assert len(i_list) == len(score_list) parents.append( self.population.pop(i_to_pop) ) # one individual of the population is chosen as new parent and picked out from population except: print('illegal situation in selection') break with open('evolve_logger.txt', 'a') as f: f.write(f'\nafter selection: {len(self.population)}') for node in self.population: f.write(f'{node.url}\n') return parents