import numpy as np from matplotlib import pyplot as plt from libs.aco import AntColony, total_distance name_1 = "att48" cities_1 = [[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_1 = 33523 name_2 = "berlin52" cities_2 = [[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_2 = 7542 name_3 = "eil76" cities_3 = [[22, 22], [36, 26], [21, 45], [45, 35], [55, 20], [33, 34], [50, 50], [55, 45], [26, 59], [40, 66], [55, 65], [35, 51], [62, 35], [62, 57], [62, 24], [21, 36], [33, 44], [9, 56], [62, 48], [66, 14], [44, 13], [26, 13], [11, 28], [7, 43], [17, 64], [41, 46], [55, 34], [35, 16], [52, 26], [43, 26], [31, 76], [22, 53], [26, 29], [50, 40], [55, 50], [54, 10], [60, 15], [47, 66], [30, 60], [30, 50], [12, 17], [15, 14], [16, 19], [21, 48], [50, 30], [51, 42], [50, 15], [48, 21], [12, 38], [15, 56], [29, 39], [54, 38], [55, 57], [67, 41], [10, 70], [6, 25], [65, 27], [40, 60], [70, 64], [64, 4], [36, 6], [30, 20], [20, 30], [15, 5], [50, 70], [57, 72], [45, 42], [38, 33], [50, 4], [66, 8], [59, 5], [35, 60], [27, 24], [40, 20], [40, 37], [40, 40]] optimal_3 = 538 all_names = [name_1, name_2, name_3] all_cities = [cities_1, cities_2, cities_3] all_optimals = [optimal_1, optimal_2, optimal_3] n_ants = 10 alpha = 1 beta = 4 evaporation = 0.5 intensification = 2 max_times = [1, 2, 3] iterations = 1 bar_width = 0.15 opacity = 0.8 x = np.arange(len(max_times)) fig, ax = plt.subplots() # Preparing the colormap 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 ] best_distances = [] for cities in all_cities: 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)]]) ax.set_xlabel('Max Time') ax.set_ylabel('Best Distance') ax.set_title('Best distances per max time for each alpha and beta') ax.set_xticks(x + bar_width / 3) ax.set_xticklabels(max_times) ax.legend() plt.axhline(y=100, color='r') plt.show()