Refactor best point selection

This commit is contained in:
coolneng 2021-06-21 00:47:48 +02:00
parent efd511e070
commit 48737fd6f0
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 14 additions and 12 deletions

View File

@ -1,11 +1,10 @@
from numpy import sum, append, arange, delete, intersect1d from numpy import sum, append, intersect1d
from numpy.random import randint, choice, shuffle from numpy.random import randint, choice, shuffle
from pandas import DataFrame from pandas import DataFrame
from math import ceil from math import ceil
from functools import partial from functools import partial
from multiprocessing import Pool from multiprocessing import Pool
from copy import deepcopy from copy import deepcopy
from itertools import chain, repeat
from preprocessing import parse_file from preprocessing import parse_file
@ -59,7 +58,7 @@ def select_distinct_genes(matching_genes, parents, m):
return first_parent_genes, second_parent_genes return first_parent_genes, second_parent_genes
def select_random_genes(matching_genes, parents): def select_shuffled_genes(matching_genes, parents):
first_parent = parents[0].query("point not in @matching_genes") first_parent = parents[0].query("point not in @matching_genes")
second_parent = parents[1].query("point not in @matching_genes") second_parent = parents[1].query("point not in @matching_genes")
first_genes = first_parent.point.values first_genes = first_parent.point.values
@ -78,20 +77,23 @@ def select_random_parent(parents):
return random_parent return random_parent
def get_best_point(parents, offspring):
while True:
random_parent = deepcopy(select_random_parent(parents))
best_index = random_parent["distance"].idxmax()
best_point = random_parent["point"].iloc[best_index]
random_parent.drop(index=best_index, inplace=True)
if best_point not in offspring.point.values:
return best_point
def repair_offspring(offspring, parents, m): def repair_offspring(offspring, parents, m):
while len(offspring) != m: while len(offspring) != m:
if len(offspring) > m: if len(offspring) > m:
best_index = offspring["distance"].idxmax() best_index = offspring["distance"].idxmax()
offspring.drop(index=best_index, inplace=True) offspring.drop(index=best_index, inplace=True)
elif len(offspring) < m: elif len(offspring) < m:
# NOTE Refactor into its own function best_point = get_best_point(parents, offspring)
while True:
random_parent = select_random_parent(parents)
best_index = random_parent["distance"].idxmax()
best_point = random_parent["point"].loc[best_index]
random_parent.drop(index=best_index, inplace=True)
if best_point not in offspring.point.values:
break
offspring = offspring.append( offspring = offspring.append(
{"point": best_point, "distance": 0, "fitness": 0}, ignore_index=True {"point": best_point, "distance": 0, "fitness": 0}, ignore_index=True
) )
@ -125,7 +127,7 @@ def uniform_crossover(parents, m):
def position_crossover(parents): def position_crossover(parents):
matching_genes = get_matching_genes(parents) matching_genes = get_matching_genes(parents)
first_genes, second_genes = select_random_genes(matching_genes, parents) first_genes, second_genes = select_shuffled_genes(matching_genes, parents)
first_offspring = populate_offspring(values=[matching_genes, first_genes]) first_offspring = populate_offspring(values=[matching_genes, first_genes])
second_offspring = populate_offspring(values=[matching_genes, second_genes]) second_offspring = populate_offspring(values=[matching_genes, second_genes])
return first_offspring, second_offspring return first_offspring, second_offspring