From ac300129ce9fe12cf09dcf72cbde915493a2349e Mon Sep 17 00:00:00 2001 From: coolneng Date: Thu, 17 Jun 2021 19:25:16 +0200 Subject: [PATCH] Remove deprecated code --- src/genetic_algorithm.py | 39 +++------------------------------------ 1 file changed, 3 insertions(+), 36 deletions(-) diff --git a/src/genetic_algorithm.py b/src/genetic_algorithm.py index 89de321..f7eb2ee 100644 --- a/src/genetic_algorithm.py +++ b/src/genetic_algorithm.py @@ -1,5 +1,5 @@ from numpy import sum, append, arange, delete, where -from numpy.random import randint, choice, shuffle, random +from numpy.random import randint, choice, shuffle from pandas import DataFrame from math import ceil @@ -132,7 +132,8 @@ def mutate(population, n, probability=0.001): genes = [] for _ in range(ceil(expected_mutations)): individuals.append(randint(n)) - genes.append(population[individuals[-1]].sample().index) + current_individual = individuals[-1] + genes.append(population[current_individual].sample().index) for ind, gen in zip(individuals, genes): individual = population[ind] individual["point"].iloc[gen] = select_new_gene(individual, n) @@ -146,39 +147,5 @@ def tournament_selection(solution): return individuals.iloc[best_index] -def replace_worst_element(previous, n, data): - solution = previous.copy() - worst_index = solution["distance"].astype(float).idxmin() - random_element = randint(n) - while element_in_dataframe(solution=solution, element=random_element): - random_element = randint(n) - solution["point"].loc[worst_index] = random_element - solution["distance"].loc[worst_index] = compute_distance( - element=solution["point"].loc[worst_index], solution=solution, data=data - ) - return solution - - -def get_random_solution(previous, n, data): - solution = replace_worst_element(previous, n, data) - while solution["distance"].sum() <= previous["distance"].sum(): - solution = replace_worst_element(previous=solution, n=n, data=data) - return solution - - -def explore_neighbourhood(element, n, data, max_iterations=100000): - neighbourhood = [] - neighbourhood.append(element) - for _ in range(max_iterations): - previous_solution = neighbourhood[-1] - neighbour = get_random_solution(previous=previous_solution, n=n, data=data) - neighbourhood.append(neighbour) - return neighbour - - def genetic_algorithm(n, m, data): first_solution = generate_first_solution(n, m, data) - best_solution = explore_neighbourhood( - element=first_solution, n=n, data=data, max_iterations=100 - ) - return best_solution