Implement mutation operator
This commit is contained in:
parent
f2584f87cb
commit
84a165ea6f
|
@ -1,5 +1,5 @@
|
||||||
from numpy import sum, append, arange, delete, where
|
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
|
from pandas import DataFrame
|
||||||
|
|
||||||
|
|
||||||
|
@ -115,6 +115,18 @@ def crossover(mode, parents, m):
|
||||||
return position_crossover(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):
|
def element_in_dataframe(solution, element):
|
||||||
duplicates = solution.query(f"point == {element}")
|
duplicates = solution.query(f"point == {element}")
|
||||||
return not duplicates.empty
|
return not duplicates.empty
|
||||||
|
|
Loading…
Reference in New Issue