Implement binary tournament selection operator

This commit is contained in:
coolneng 2021-05-31 18:24:20 +02:00
parent 84a165ea6f
commit 9cafdc0536
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 9 additions and 3 deletions

View File

@ -115,6 +115,11 @@ def crossover(mode, parents, m):
return position_crossover(parents, m)
def element_in_dataframe(solution, element):
duplicates = solution.query(f"point == {element}")
return not duplicates.empty
def mutate(solution, n, probability=0.001):
if random() > probability:
return solution
@ -127,9 +132,10 @@ def mutate(solution, n, probability=0.001):
return solution
def element_in_dataframe(solution, element):
duplicates = solution.query(f"point == {element}")
return not duplicates.empty
def tournament_selection(solution):
individuals = solution.sample(n=2)
best_index = solution["distance"].astype(float).idxmax()
return individuals.iloc[best_index]
def replace_worst_element(previous, n, data):