From 550f0bb04336aa7267ab86452810d9a44bf44501 Mon Sep 17 00:00:00 2001 From: coolneng Date: Wed, 19 May 2021 20:43:14 +0200 Subject: [PATCH] Refactor fitness calculation using combinations --- src/main.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main.py b/src/main.py index 13eb1d5..30ce562 100755 --- a/src/main.py +++ b/src/main.py @@ -3,6 +3,7 @@ from greedy import greedy_algorithm from local_search import local_search from sys import argv from time import time +from itertools import combinations def execute_algorithm(choice, n, m, data): @@ -25,13 +26,14 @@ def get_row_distance(source, destination, data): def get_fitness(solutions, data): counter = 0 - for i in range(len(solutions) - 1): - for j in range(i + 1, len(solutions)): - counter += get_row_distance( - source=solutions["point"].iloc[i], - destination=solutions["point"].iloc[j], - data=data, - ) + comb = combinations(solutions.index, r=2) + for index in list(comb): + elements = solutions.loc[index, :] + counter += get_row_distance( + source=elements["point"].head(n=1).values[0], + destination=elements["point"].tail(n=1).values[0], + data=data, + ) return counter