Replace solution instances with individual

This commit is contained in:
coolneng 2021-06-17 22:44:39 +02:00
parent ac300129ce
commit 49d8383133
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 16 additions and 16 deletions

View File

@ -12,9 +12,9 @@ def get_row_distance(source, destination, data):
return row["distance"].values[0] return row["distance"].values[0]
def compute_distance(element, solution, data): def compute_distance(element, individual, data):
accumulator = 0 accumulator = 0
distinct_elements = solution.query(f"point != {element}") distinct_elements = individual.query(f"point != {element}")
for _, item in distinct_elements.iterrows(): for _, item in distinct_elements.iterrows():
accumulator += get_row_distance( accumulator += get_row_distance(
source=element, destination=item.point, data=data source=element, destination=item.point, data=data
@ -22,18 +22,18 @@ def compute_distance(element, solution, data):
return accumulator return accumulator
def generate_first_solution(n, m, data): def generate_individual(n, m, data):
solution = DataFrame(columns=["point", "distance"]) individual = DataFrame(columns=["point", "distance", "fitness"])
solution["point"] = choice(n, size=m, replace=False) individual["point"] = choice(n, size=m, replace=False)
solution["distance"] = solution["point"].apply( individual["distance"] = individual["point"].apply(
func=compute_distance, solution=solution, data=data func=compute_distance, individual=individual, data=data
) )
return solution return individual
def evaluate_element(element, data): def evaluate_individual(individual, data):
fitness = [] fitness = []
genotype = element.point.values genotype = individual.point.values
distances = data.query(f"source in @genotype and destination in @genotype") distances = data.query(f"source in @genotype and destination in @genotype")
for item in genotype[:-1]: for item in genotype[:-1]:
element_df = distances.query(f"source == {item} or destination == {item}") element_df = distances.query(f"source == {item} or destination == {item}")
@ -114,15 +114,15 @@ def crossover(mode, parents, m):
return position_crossover(parents, m) return position_crossover(parents, m)
def element_in_dataframe(solution, element): def element_in_dataframe(individual, element):
duplicates = solution.query(f"point == {element}") duplicates = individual.query(f"point == {element}")
return not duplicates.empty return not duplicates.empty
def select_new_gene(individual, n): def select_new_gene(individual, n):
while True: while True:
new_gene = randint(n) new_gene = randint(n)
if not element_in_dataframe(solution=individual, element=new_gene): if not element_in_dataframe(individual=individual, element=new_gene):
return new_gene return new_gene
@ -141,9 +141,9 @@ def mutate(population, n, probability=0.001):
return population return population
def tournament_selection(solution): def tournament_selection(population):
individuals = solution.sample(n=2) individuals = population.sample(n=2)
best_index = solution["distance"].astype(float).idxmax() best_index = population["distance"].idxmax()
return individuals.iloc[best_index] return individuals.iloc[best_index]