Implement mutation operator

This commit is contained in:
coolneng 2021-05-31 18:12:23 +02:00
parent f2584f87cb
commit 84a165ea6f
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 13 additions and 1 deletions

View File

@ -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