Fix fitness computation

This commit is contained in:
coolneng 2021-05-19 19:38:56 +02:00
parent dfdd142fdb
commit 9a4831e31e
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
1 changed files with 25 additions and 5 deletions

View File

@ -15,14 +15,33 @@ def execute_algorithm(choice, n, m, data):
exit(1) exit(1)
def show_results(solutions, time_delta): def get_row_distance(source, destination, data):
distance_sum = solutions["distance"].sum() row = data.query(
"""(source == @source and destination == @destination) or \
(source == @destination and destination == @source)"""
)
return row["distance"].values[0]
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,
)
return counter
def show_results(solutions, fitness, time_delta):
duplicates = solutions.duplicated().any() duplicates = solutions.duplicated().any()
print(solutions) print(solutions)
print("Total distance: " + str(distance_sum)) print(f"Total distance: {fitness}")
if not duplicates: if not duplicates:
print("No duplicates found") print("No duplicates found")
print("Execution time: " + str(time_delta)) print(f"Execution time: {time_delta}")
def usage(argv): def usage(argv):
@ -40,7 +59,8 @@ def main():
start_time = time() start_time = time()
solutions = execute_algorithm(choice=argv[2], n=n, m=m, data=data) solutions = execute_algorithm(choice=argv[2], n=n, m=m, data=data)
end_time = time() end_time = time()
show_results(solutions, time_delta=end_time - start_time) fitness = get_fitness(solutions, data)
show_results(solutions, fitness, time_delta=end_time - start_time)
if __name__ == "__main__": if __name__ == "__main__":