from matplotlib import pyplot as plt from libs.aco import AntColony, total_distance name = "att48" cities = [[6734, 1453], [2233, 10], [5530, 1424], [401, 841], [3082, 1644], [7608, 4458], [7573, 3716], [7265, 1268], [6898, 1885], [1112, 2049], [5468, 2606], [5989, 2873], [4706, 2674], [4612, 2035], [6347, 2683], [6107, 669], [7611, 5184], [7462, 3590], [7732, 4723], [5900, 3561], [4483, 3369], [6101, 1110], [5199, 2182], [1633, 2809], [4307, 2322], [675, 1006], [7555, 4819], [7541, 3981], [3177, 756], [7352, 4506], [7545, 2801], [3245, 3305], [6426, 3173], [4608, 1198], [23, 2216], [7248, 3779], [7762, 4595], [7392, 2244], [3484, 2829], [6271, 2135], [4985, 140], [1916, 1569], [7280, 4899], [7509, 3239], [10, 2676], [6807, 2993], [5185, 3258], [3023, 1942]] optimal = 33523 n_ants = 10 alpha = 1 beta = 2 evaporation = 0.5 intensification = 2 max_times = [0.1, 0.5, 1] iterations = 2 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, dist in enumerate(best_distances): plt.scatter(times[i], dist[0], color=dist[1]) plt.text(times[i], dist[0], "dist: {}\ntime: {}s".format(int(dist[0]), times[i]), rotation=75, va='bottom') plt.title(title) plt.xlabel('Max Time (seconds)') plt.ylabel('Distance') plt.axhline(y=optimal, color='r') max_dist = max([x[0] for x in best_distances]) # Extractions des valeurs plt.ylim(0, max_dist+max_dist*0.2) plt.show()