Fix fitness computation
This commit is contained in:
parent
dfdd142fdb
commit
9a4831e31e
30
src/main.py
30
src/main.py
|
@ -15,14 +15,33 @@ def execute_algorithm(choice, n, m, data):
|
|||
exit(1)
|
||||
|
||||
|
||||
def show_results(solutions, time_delta):
|
||||
distance_sum = solutions["distance"].sum()
|
||||
def get_row_distance(source, destination, data):
|
||||
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()
|
||||
print(solutions)
|
||||
print("Total distance: " + str(distance_sum))
|
||||
print(f"Total distance: {fitness}")
|
||||
if not duplicates:
|
||||
print("No duplicates found")
|
||||
print("Execution time: " + str(time_delta))
|
||||
print(f"Execution time: {time_delta}")
|
||||
|
||||
|
||||
def usage(argv):
|
||||
|
@ -40,7 +59,8 @@ def main():
|
|||
start_time = time()
|
||||
solutions = execute_algorithm(choice=argv[2], n=n, m=m, data=data)
|
||||
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__":
|
||||
|
|
Loading…
Reference in New Issue