diff --git a/src/genetic_algorithm.py b/src/genetic_algorithm.py index a34fbf8..9cf519c 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 +from numpy.random import randint, choice, shuffle, random from pandas import DataFrame @@ -115,6 +115,18 @@ def crossover(mode, parents, m): return position_crossover(parents, m) +def mutate(solution, n, probability=0.001): + if random() > probability: + return solution + row = solution.sample() + random_element = randint(n) + while element_in_dataframe(solution=solution, element=random_element): + random_element = randint(n) + solution["point"].iloc[row.index] = random_element + solution["distance"].loc[row.index] = 0 + return solution + + def element_in_dataframe(solution, element): duplicates = solution.query(f"point == {element}") return not duplicates.empty