82 lines
2.9 KiB
Python
82 lines
2.9 KiB
Python
from matplotlib import pyplot as plt
|
|
from libs.aco import AntColony, total_distance
|
|
|
|
name = "berlin52"
|
|
cities = [[565, 575], [25, 185], [345, 750], [945, 685], [845, 655], [880, 660], [25, 230], [525, 1000], [580, 1175], [650, 1130], [1605, 620], [1220, 580], [1465, 200], [1530, 5], [845, 680], [725, 370], [145, 665], [415, 635], [510, 875], [560, 365], [300, 465], [520, 585], [480, 415],
|
|
[835, 625], [975, 580], [1215, 245], [1320, 315], [1250, 400], [660, 180], [410, 250], [420, 555], [575, 665], [1150, 1160], [700, 580], [685, 595], [685, 610], [770, 610], [795, 645], [720, 635], [760, 650], [475, 960], [95, 260], [875, 920], [700, 500], [555, 815], [830, 485],
|
|
[1170, 65], [830, 610], [605, 625], [595, 360], [1340, 725], [1740, 245]]
|
|
optimal = 7542
|
|
|
|
n_ants = 10
|
|
alpha = 1
|
|
beta = 2
|
|
evaporation = 0.5
|
|
intensification = 2
|
|
max_times = [0.1, 0.5, 1, 2, 5, 10]
|
|
iterations = 1
|
|
|
|
best_distances = []
|
|
times = []
|
|
colors = [
|
|
'#1f77b4', # Bleu moyen
|
|
'#ff7f0e', # Orange
|
|
'#2ca02c', # Vert
|
|
'#d62728', # Rouge
|
|
'#9467bd', # Violet
|
|
'#8c564b', # Marron
|
|
'#e377c2', # Rose
|
|
'#7f7f7f', # Gris
|
|
'#bcbd22', # Vert olive
|
|
'#17becf', # Turquoise
|
|
'#1b9e77', # Vert Teal
|
|
'#d95f02', # Orange foncé
|
|
'#7570b3', # Violet moyen
|
|
'#e7298a', # Fuchsia
|
|
'#66a61e', # Vert pomme
|
|
'#e6ab02', # Jaune or
|
|
'#a6761d', # Bronze
|
|
'#666666', # Gris foncé
|
|
'#f781bf', # Rose clair
|
|
'#999999', # Gris moyen
|
|
]
|
|
|
|
for max_time in max_times:
|
|
for iteration in range(iterations):
|
|
ant_colony = AntColony(cities, n_ants, alpha, beta, evaporation, intensification, max_time)
|
|
print("Running iteration number {}/{} ({} sec)".format(iteration + 1, iterations, max_time))
|
|
best_route = ant_colony.run()
|
|
best_distances.append([total_distance(best_route), colors[max_times.index(max_time) % len(colors)]])
|
|
times.append(max_time)
|
|
|
|
title = ""
|
|
title += "Best distance per iterations ({})\n".format(name)
|
|
title += "Nb cities: " + str(len(cities)) + " / "
|
|
title += "Ants: " + str(n_ants) + " / "
|
|
title += "Alpha: " + str(alpha) + " / "
|
|
title += "Beta: " + str(beta) + " / "
|
|
title += "Evaporation: " + str(evaporation) + " / "
|
|
title += "Intensification: " + str(intensification)
|
|
plt.title(title)
|
|
plt.xlabel('Iteration')
|
|
plt.ylabel('Distance')
|
|
plt.axhline(y=optimal, color='r')
|
|
|
|
distances = [x[0] for x in best_distances] # Extractions des valeurs
|
|
|
|
max_dist = max(distances)
|
|
plt.ylim(0, max_dist+max_dist*0.2)
|
|
|
|
values = [item[0] for item in best_distances]
|
|
colors = [item[1] for item in best_distances]
|
|
|
|
bars = plt.bar(range(len(values)), values, color=colors)
|
|
|
|
for i, bar in enumerate(bars):
|
|
yval = bar.get_height()
|
|
plt.text(bar.get_x() + bar.get_width()/2, yval + 0.05,
|
|
"dist: {}\ntime: {}s".format(int(yval), times[i]),
|
|
rotation=75, ha='center', va='bottom')
|
|
|
|
plt.xticks(range(len(values)), [str(i+1) for i in range(len(values))])
|
|
|
|
plt.show() |