From 04fd66425e3f4acdde5e2b120621beb94c088a18 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 18 Jun 2021 20:06:59 +0200 Subject: [PATCH] Fix max and min selection according to fitness --- src/genetic_algorithm.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/genetic_algorithm.py b/src/genetic_algorithm.py index 1a170be..dedba02 100644 --- a/src/genetic_algorithm.py +++ b/src/genetic_algorithm.py @@ -156,24 +156,26 @@ def tournament_selection(m, population): def generational_replacement(previous_population, current_population): new_population = current_population - best_previous_individual = max(previous_population, key=lambda x: x.fitness) + best_previous_individual = max(previous_population, key=lambda x: all(x.fitness)) if best_previous_individual not in new_population: - worst_index = new_population.index(min(new_population, key=lambda x: x.fitness)) + worst_index = new_population.index( + min(new_population, key=lambda x: all(x.fitness)) + ) new_population[worst_index] = best_previous_individual return new_population def get_best_elements(population): - first_index = population.index(max(population, key=lambda x: x.fitness)) + first_index = population.index(max(population, key=lambda x: all(x.fitness))) population.pop(first_index) - second_index = population.index(max(population, key=lambda x: x.fitness)) + second_index = population.index(max(population, key=lambda x: all(x.fitness))) return first_index, second_index def get_worst_elements(population): - first_index = population.index(min(population, key=lambda x: x.fitness)) + first_index = population.index(min(population, key=lambda x: all(x.fitness))) population.pop(first_index) - second_index = population.index(min(population, key=lambda x: x.fitness)) + second_index = population.index(min(population, key=lambda x: all(x.fitness))) return first_index, second_index