Implement uniform generational genetic algorithm

This commit is contained in:
coolneng 2021-06-20 05:18:36 +02:00
parent b4a299cbf7
commit 1e5f02e5ce
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 22 additions and 13 deletions

View File

@ -4,6 +4,7 @@ from pandas import DataFrame
from math import ceil from math import ceil
from functools import partial from functools import partial
from multiprocessing import Pool from multiprocessing import Pool
from copy import deepcopy
from preprocessing import parse_file from preprocessing import parse_file
@ -169,7 +170,7 @@ def mutate(offspring, data, probability=0.001):
return offspring return offspring
def get_individual_index(population, element): def get_individual_index(element, population):
for index in range(len(population)): for index in range(len(population)):
if population[index].fitness.values[0] == element.fitness.values[0]: if population[index].fitness.values[0] == element.fitness.values[0]:
return index return index
@ -178,25 +179,33 @@ def get_individual_index(population, element):
def tournament_selection(population): def tournament_selection(population):
individuals = [population[randint(len(population))] for _ in range(2)] individuals = [population[randint(len(population))] for _ in range(2)]
best_element = max(individuals, key=lambda x: x.fitness.values[0]) best_element = max(individuals, key=lambda x: x.fitness.values[0])
population_index = get_individual_index(population, best_element) population_index = get_individual_index(best_element, population)
return best_element, population_index return best_element, population_index
def generational_replacement(previous_population, current_population): def check_element_population(element, population):
for item in population:
if all(element.point.values) == all(item.point.values):
return True
return False
def generational_replacement(prev_population, current_population):
new_population = current_population new_population = current_population
best_previous_individual = max(previous_population, key=lambda x: all(x.fitness)) best_previous_individual = max(prev_population, key=lambda x: x.fitness.values[0])
if best_previous_individual not in new_population: if check_element_population(best_previous_individual, new_population):
worst_index = new_population.index( worst_element = min(new_population, key=lambda x: x.fitness.values[0])
min(new_population, key=lambda x: all(x.fitness)) worst_index = get_individual_index(worst_element, new_population)
)
new_population[worst_index] = best_previous_individual new_population[worst_index] = best_previous_individual
return new_population return new_population
def get_best_elements(population): def get_best_elements(population):
first_index = population.index(max(population, key=lambda x: all(x.fitness))) first_element = max(population, key=lambda x: x.fitness.values[0])
first_index = get_individual_index(first_element, population)
population.pop(first_index) population.pop(first_index)
second_index = population.index(max(population, key=lambda x: all(x.fitness))) second_element = max(population, key=lambda x: x.fitness.values[0])
second_index = get_individual_index(second_element, population)
return first_index, second_index return first_index, second_index
@ -231,7 +240,7 @@ def evaluate_population(population, data, cores=4):
def select_parents(population, n, mode): def select_parents(population, n, mode):
select_population = population select_population = deepcopy(population)
parents = [] parents = []
if mode == "generational": if mode == "generational":
for _ in range(n): for _ in range(n):
@ -255,8 +264,8 @@ def genetic_algorithm(n, m, data, select_mode, crossover_mode, max_iterations=10
offspring = mutate(offspring, data) offspring = mutate(offspring, data)
population = replace_population(population, offspring, select_mode) population = replace_population(population, offspring, select_mode)
population = evaluate_population(population, data) population = evaluate_population(population, data)
best_solution, _ = get_best_elements(population) best_index, _ = get_best_elements(population)
return best_solution return population[best_index]
n, m, data = parse_file("data/GKD-c_11_n500_m50.txt") n, m, data = parse_file("data/GKD-c_11_n500_m50.txt")