def next_generation(self): """ Modify the population for the next generation. """ child_pop = [copy.deepcopy(self.elite_ind)] # Perform tournament selection. for i in range(len(self.genomes) - 1): tourn = random.sample(self.genomes, 2) if tourn[0]['fitness'] < tourn[1]['fitness']: child_pop.append(copy.deepcopy(tourn[0])) else: child_pop.append(copy.deepcopy(tourn[1])) child_pop[-1]['id'] = self.child_id self.child_id += 1 #Crossover child_pop = single_point_crossover(child_pop, cross_over_prob) # Mutate genes in the child genomes. child_pop = random_value_mutation(child_pop, mutation_prob) for child in child_pop: child['generation'] = CURRENT_GEN + 1 self.genomes = child_pop self.id_map = { k: v for k, v in zip([x['id'] for x in self.genomes], [i for i in range(self.pop_size)]) }
def next_generation(self): """ Modify the population for the next generation. """ child_pop = [copy.deepcopy(self.elite_ind)] # Perform tournament selection. for i in range(len(self.genomes) - 1): tourn = random.sample(self.genomes, TOURNAMENT_SIZE) fitness_list = [] for ind in tourn: fitness_list.append(ind['fitness']) winner_index = fitness_list.index(max(fitness_list)) child_pop.append(copy.deepcopy(tourn[winner_index])) child_pop[-1]['id'] = self.child_id self.child_id += 1 #Crossover child_pop = single_point_crossover(child_pop, CROSS_OVER_PROB) # Mutate genes in the child genomes. child_pop = random_value_mutation(child_pop, MUTATION_PROB) for child in child_pop: child['generation'] = CURRENT_GEN + 1 self.genomes = child_pop self.id_map = { k: v for k, v in zip([x['id'] for x in self.genomes], [i for i in range(self.pop_size)]) }