From 2e12296f2c94fb1805ae70be4e35f9465a38fd44 Mon Sep 17 00:00:00 2001 From: coolneng Date: Mon, 17 May 2021 20:50:26 +0200 Subject: [PATCH] Refactor genotype obtention into a function --- src/genetic_algorithm.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/genetic_algorithm.py b/src/genetic_algorithm.py index 68027aa..5c96427 100644 --- a/src/genetic_algorithm.py +++ b/src/genetic_algorithm.py @@ -10,12 +10,16 @@ def get_first_random_solution(n, m): return solution +def get_genotype(element): + genotype = where(element == True) + return genotype[0] + + def evaluate_element(element, data): fitness = [] - genotype = where(element == True) - indexes = genotype[0] - distances = data.query(f"source in @indexes and destination in @indexes") - for item in indexes[:-1]: + genotype = get_genotype(element) + distances = data.query(f"source in @genotype and destination in @genotype") + for item in genotype[:-1]: element_df = distances.query(f"source == {item} or destination == {item}") max_distance = element_df["distance"].astype(float).max() fitness = append(arr=fitness, values=max_distance)