From 921e094858427780ec78148433be767f40c6f2a6 Mon Sep 17 00:00:00 2001 From: coolneng Date: Mon, 17 May 2021 20:42:17 +0200 Subject: [PATCH] Implement element evaluation --- src/genetic_algorithm.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/genetic_algorithm.py b/src/genetic_algorithm.py index 9357842..68027aa 100644 --- a/src/genetic_algorithm.py +++ b/src/genetic_algorithm.py @@ -1,3 +1,4 @@ +from numpy import put, sum, append, where, zeros from numpy.random import choice, seed @@ -9,6 +10,17 @@ def get_first_random_solution(n, m): return solution +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]: + 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) + distances = distances.query(f"source != {item} and destination != {item}") + return sum(fitness) def element_in_dataframe(solution, element):