Implement the generational replacement operator

This commit is contained in:
coolneng 2021-06-17 22:45:59 +02:00
parent 135d1c48b8
commit 7056534872
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 22 additions and 2 deletions

View File

@ -149,5 +149,25 @@ def tournament_selection(population):
return individuals.iloc[best_index] return individuals.iloc[best_index]
def genetic_algorithm(n, m, data): def generational_replacement(previous_population, current_population):
first_solution = generate_first_solution(n, m, data) new_population = current_population
best_previous_individual = max(previous_population, key=lambda x: x.fitness)
if best_previous_individual not in new_population:
worst_index = new_population.index(min(new_population, key=lambda x: x.fitness))
new_population[worst_index] = best_previous_individual
return new_population
def stationary_replacement(previous_population, current_population):
new_population = previous_population
return new_population
def replace_population(previous_population, current_population, mode):
if mode == "generational":
return generational_replacement(previous_population, current_population)
return stationary_replacement(previous_population, current_population)
def genetic_algorithm(n, m, data, mode):
population = [generate_individual(n, m, data) for _ in range(n)]