diff --git a/.assets/images/att48/stats_average_2_att48_aco_0.01_to_5_secs_ant_10_alpha_1_beta_1_to_4.png b/.assets/images/att48/stats_average_2_att48_aco_0.01_to_5_secs_ant_10_alpha_1_beta_1_to_4.png new file mode 100644 index 0000000..49ee24e Binary files /dev/null and b/.assets/images/att48/stats_average_2_att48_aco_0.01_to_5_secs_ant_10_alpha_1_beta_1_to_4.png differ diff --git a/.assets/images/att48/stats_average_5_att48_aco_0.01_to_20_secs_ant_10_alpha_1_beta_4.png b/.assets/images/att48/stats_average_5_att48_aco_0.01_to_20_secs_ant_10_alpha_1_beta_4.png new file mode 100644 index 0000000..a0e69a8 Binary files /dev/null and b/.assets/images/att48/stats_average_5_att48_aco_0.01_to_20_secs_ant_10_alpha_1_beta_4.png differ diff --git a/tests/04_cluster_ant_colony_no_animation.py b/tests/04_cluster_ant_colony_no_animation.py index 7b62d0e..a279594 100644 --- a/tests/04_cluster_ant_colony_no_animation.py +++ b/tests/04_cluster_ant_colony_no_animation.py @@ -11,13 +11,15 @@ def generate_cities(nb, max_coords=1000): nb_ville = 2000 max_coords = 1000 nb_truck = 1 -max_time = 2 +max_time = 5 nb_ants = 10 max_time_per_cluster = max_time / nb_truck start_time_generate = time.time() -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]] +cities = [[37.4393516691, 541.2090699418], [612.1759508571, 494.3166877396], [38.1312338227, 353.1484581781], [53.4418081065, 131.484901365], [143.0606355347, 631.7200953923], [689.9451267256, 468.5354998742], [112.7478815786, 529.417757826], [141.4875865042, 504.818485571], [661.0513901702, 445.9375182115], [98.7899036592, 384.5926031158], [697.3881696597, 180.3962284275], [536.4894189738, 287.2279085051], [192.4067320507, 20.439405931], [282.7865258765, 229.8001556189], [240.8251726391, 281.51414372], [246.9281323057, 322.461332116], [649.7313216456, 62.3331575282], [352.96585626, 666.7873101942], [633.392367658, 534.9398453712], [488.311799404, 437.4869439948], [141.4039286509, 228.4325551488], [17.3632612602, 240.2407068508], [397.5586451389, 231.3591208928], [565.7853781464, 282.3858748974], [475.8975387047, 468.5392706317], [322.4224566559, 550.3165478233], [397.5586634023, 74.7588387765], [672.8618339396, 432.882640963], [571.2189680147, 530.261699153], [104.6531165914, 482.8224768783], [356.7098388794, 67.6477131712], [400.4070255527, 253.6794479997], [282.3036243109, 426.8380500923], [58.7766988363, 507.1712386832], [189.75062244, 460.3815233617], [659.9124120147, 226.6284156239], [639.0307636033, 467.2302300719], [415.0258357432, 233.3045376118], [547.2662016307, 161.6589278401], [616.6547902644, 339.3409309407], [494.8592427417, 148.1217856389], [629.9980812186, 433.4548164038], [471.101431241, 314.2219307579], [138.2440514421, 137.1679919735], [91.5847556724, 110.0203007516], [390.6972811808, 423.9774318385], [565.1617825137, 429.1598152874], [54.5248980387, 438.5515408431], [334.350832971, 153.796923804], [531.0291024509, 612.3874827889], [475.7345905802, 385.7844618897], [228.8325218994, 410.4461939615], [578.3805347586, 321.3303494537], [358.9170574485, 404.4670352898], [486.4648554867, 593.0429937016], [343.169370767, 509.3123571315], [530.3626972076, 137.6881275684], [498.8065475299, 576.2102674608], [224.31827155, 312.4677490415], [595.836073259, 81.8130051356], [661.5588724308, 217.0456944477], [43.6892045516, 305.4722789165], [79.465345253, 445.9641737689], [210.4163247004, 130.7151137038], [432.2642292251, 629.4092661116], [623.2487161301, 69.189285084], [436.5194739944, 282.935645607], [59.4163265482, 40.1280234442], [630.9230074073, 230.342988813], [579.3265539688, 601.0359410602], [117.862450748, 112.9796833705], [297.7912565664, 166.3131886803], [22.7642703744, 455.5340094037], [259.7095810385, 10.6199925885], [342.3579873647, 599.3880182608], [10.0260950143, +488.9310558282], [315.2926064118, 273.2275475579], [220.7044919297, 270.0819745721], [192.1186059948, 314.1839922798], [271.5042718992, 225.2921989972], [530.7320005441, 504.0670155337], [42.5331441666, 656.3645162886], [396.1274792588, 539.4648066027], [118.6631474021, 508.7129103929], [395.6913876595, 699.5376048429], [559.0157105844, 560.8866941411], [22.6471035906, 526.2470392816], [135.6377085256, 325.8409901555], [141.4507014379, 485.2477927763], [396.7741299332, 460.7557115283], [87.7494562765, 19.6170129082], [350.4245639661, 420.6531186835], [216.7010817133, 466.4816410995], [130.9237737024, 351.1491733079], [72.6329856671, 645.7852219213], [144.6002949996, 457.4224283926], [212.3725077442, 594.9216893413], [49.9186786455, 541.4350825349], [656.6943525585, 558.1109593509], [176.5941623792, 648.5239953299], [500.3825200226, 198.7428378322], [634.317867842, 612.8291643194], [59.7537372726, 551.6321886765], [15.2145765106, 143.0441928532], [283.0054378872, 376.4439530184], [146.5389000907, 39.4231794338], [101.8685605377, 635.098685018], [588.1968537448, 580.5946976921], [457.2628632528, 350.0164047376], [537.4663680494, 472.5842276692], [269.3669098585, 367.4763636538], [239.9045383695, 102.629765339], [88.4677500396, 384.0507209275], [658.9133693395, 583.9575181023], [97.7359146347, 157.4558657632], [506.6191384007, 233.0022156094], [500.2566898239, 64.9136393489], [594.4048565021, 275.874186899], [66.230814661, 24.1317387604], [598.4162993909, 414.5557574275], [172.308833083, 344.3963466366], [299.48128518, 251.829512132], [303.8379894831, 21.052606379], [197.896926984, 512.388896098], [56.0199567669, 243.0663818382], [255.5566183121, 448.8651882442], [608.4256112402, 222.5421309272], [70.2722703273, 77.9227026433], [398.2298999899, 119.557657386], [635.4970237093, 133.3225902609], [378.3484559418, 272.2907677147], [484.8029663388, 677.0730379436], [278.8710882619, 299.9308770828], [381.6537300653, 360.3337602785], [557.6070707573, 595.3185092281], [249.0589749342, 76.6595112599], [562.9048787838, 670.0382113114], [398.550436558, 392.6493259144], [590.893972056, 370.7414913742], [558.2008003726, 0.4198814512], [461.4114714423, 530.5254969413], [354.7242881504, 685.40453619], [193.6611295657, +669.7432521028], [352.3140807211, 140.3273323662], [308.434570974, 115.2054269847], [299.588137008, 530.588961902], [334.2748764383, 152.1494569394], [690.9658585947, 134.5793307203], [48.0798124069, 270.968067372], [91.6467647724, 166.3541158474]] #cities[0] = [max_coords/2, max_coords/2] stop_time_generate = time.time() @@ -84,8 +86,8 @@ for i, (route, color) in enumerate(best_routes): y = [city[1] for city in route] x.append(x[0]) y.append(y[0]) - plt.plot(x, y, color=color, marker='x', linestyle='-', label=f"Cluster {i}") + plt.plot(x, y, color="blue", marker='o', linestyle='-', label=f"Cluster {i}") # add title with nb_ville, nb_truck and max_time - plt.title(f"nb_ville = {nb_ville}, nb_truck = {nb_truck}, max_time = {max_time}") + plt.title(f"nb_ville = {len(cities)}, nb_truck = {nb_truck}, max_time = {max_time}") plt.show() diff --git a/tests/04_cluster_ant_colony_no_animation_no_random_alpha_variation.py b/tests/04_cluster_ant_colony_no_animation_no_random_alpha_variation.py new file mode 100644 index 0000000..ed8c81e --- /dev/null +++ b/tests/04_cluster_ant_colony_no_animation_no_random_alpha_variation.py @@ -0,0 +1,79 @@ +import matplotlib.pyplot as plt +import numpy as np +from libs.clustering import split_tour_across_clusters +from libs.aco import AntColony, total_distance + +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 + +max_times = [0.01, 0.1, 0.2, 0.5] +n_ants = 10 +alphas = [1, 4] +beta = 4 +evaporation = 0.5 +intensification = 2 +n_runs = 5 # Nombre de fois où chaque configuration sera exécutée pour obtenir une moyenne + +average_best_route_per_max_time_per_alpha = [] + +for alpha in alphas: + average_best_route_per_max_time = [] + for max_time in max_times: + + total_best_route_length = 0 + + for _ in range(n_runs): + print("Run number: ", _, "for max_time: ", max_time) + clusters = split_tour_across_clusters(cities, 1) + + total_distance_for_run = 0 + + for i, cluster_indices in enumerate(clusters.values()): + cluster_cities = [cities[index] for index in cluster_indices] + + ant_colony = AntColony(cluster_cities, n_ants=n_ants, max_time=max_time, alpha=alpha, beta=beta, evaporation=evaporation, intensification=intensification) + best_route = ant_colony.run() + total_distance_for_run += total_distance(best_route) + + total_best_route_length += total_distance_for_run + + average_best_route_length = total_best_route_length / n_runs + average_best_route_per_max_time.append(average_best_route_length) + + average_best_route_per_max_time_per_alpha.append(average_best_route_per_max_time) + +colors = ['b', 'g'] + +total_bars_per_group = len(alphas) +total_groups = len(max_times) + +unit_space_per_group = 1 +bars_space_per_group = total_bars_per_group / (total_bars_per_group + 1) +bar_width = bars_space_per_group / total_bars_per_group + +bar_positions = np.arange(total_groups) + +plt.figure(figsize=(10, 7)) + +for i, average_best_route_per_max_time in enumerate(average_best_route_per_max_time_per_alpha): + plt.bar(bar_positions + i * bar_width, average_best_route_per_max_time, width=bar_width, color=colors[i]) + +plt.axhline(y=optimal, color='r') + +plt.xticks(bar_positions + bar_width / 2, max_times) # Set the x-axis labels to be the max_time values +plt.xlabel('Max time') +plt.ylabel('Average best route length') + +title = "" +title += "Average best route length ({} iterations) for different max times\n".format(n_runs) +title += "Nb cities: " + str(len(cities)) + " / " +title += "Ants: " + str(n_ants) + " / " +title += "Alpha: " + str(alphas) + " / " +title += "Beta: " + str(beta) + " / " +title += "Evaporation: " + str(evaporation) + " / " +title += "Intensification: " + str(intensification) +plt.title(title) + +plt.show() \ No newline at end of file diff --git a/tests/04_cluster_ant_colony_no_animation_no_random_beta_variation.py b/tests/04_cluster_ant_colony_no_animation_no_random_beta_variation.py new file mode 100644 index 0000000..d93db20 --- /dev/null +++ b/tests/04_cluster_ant_colony_no_animation_no_random_beta_variation.py @@ -0,0 +1,86 @@ +import matplotlib.pyplot as plt +from libs.clustering import split_tour_across_clusters +from libs.aco import AntColony, total_distance +import numpy as np + +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 + +max_times = [0.1, 0.2, 0.5, 1, 2] +n_ants = 10 +alpha = 1 +betas = [2, 4, 6, 8] # Pour contenir deux valeurs de beta +evaporation = 0.5 +intensification = 2 +n_runs = 2 # Nombre de fois où chaque configuration sera exécutée pour obtenir une moyenne + +average_best_route_per_max_time_per_beta = [] + +for beta in betas: + average_best_route_per_max_time = [] + + for max_time in max_times: + + total_best_route_length = 0 + + for _ in range(n_runs): + print("Run number: ", _, "for max_time: ", max_time, " and beta: ", beta) + clusters = split_tour_across_clusters(cities, 1) + + total_distance_for_run = 0 + + for i, cluster_indices in enumerate(clusters.values()): + cluster_cities = [cities[index] for index in cluster_indices] + + ant_colony = AntColony(cluster_cities, n_ants=n_ants, max_time=max_time, alpha=alpha, beta=beta, evaporation=evaporation, intensification=intensification) + best_route = ant_colony.run() + total_distance_for_run += total_distance(best_route) + + total_best_route_length += total_distance_for_run + + average_best_route_length = total_best_route_length / n_runs + average_best_route_per_max_time.append(average_best_route_length) + + average_best_route_per_max_time_per_beta.append(average_best_route_per_max_time) + +# Maintenant, nous avons toutes les valeurs moyennes, créons un histogramme +colors = ['b', 'g', 'r', 'c', 'm', 'y', 'k'] + +total_bars_per_group = len(betas) +total_groups = len(max_times) + +# Unité d'espace pour chaque groupe de barres (incluant l'espace entre les groupes) +unit_space_per_group = 1 + +# Espace qui serait occupé par les barres dans chaque groupe +bars_space_per_group = total_bars_per_group / (total_bars_per_group + 1) + +# La largeur de chaque barre serait +bar_width = bars_space_per_group / total_bars_per_group +index = np.arange(len(max_times)) + +plt.figure() + +for i, average_best_route_per_max_time in enumerate(average_best_route_per_max_time_per_beta): + plt.bar(index + i * bar_width, average_best_route_per_max_time, bar_width, color=colors[i], label='Beta = '+str(betas[i])) + for j, v in enumerate(average_best_route_per_max_time): + plt.text(j + i * bar_width, v + 0.01 + (i*0.2), int(v), va='bottom', ha='center') + +plt.axhline(y=optimal, color='r') +plt.xlabel('Max time') +plt.ylabel('Average best route length') + +title = "" +title += "Comparaison of betas with average best route length ({} iterations) for different max times and each betas\n".format(n_runs) +title += "Nb cities: " + str(len(cities)) + " / " +title += "Ants: " + str(n_ants) + " / " +title += "Alpha: " + str(alpha) + " / " +title += "Evaporation: " + str(evaporation) + " / " +title += "Intensification: " + str(intensification) +plt.title(title) + +plt.xticks(index + bar_width / 2, max_times) +plt.legend() + +plt.tight_layout() +plt.show() \ No newline at end of file diff --git a/tests/04_cluster_ant_colony_no_animation_no_random_single_value.py b/tests/04_cluster_ant_colony_no_animation_no_random_single_value.py new file mode 100644 index 0000000..1e18e06 --- /dev/null +++ b/tests/04_cluster_ant_colony_no_animation_no_random_single_value.py @@ -0,0 +1,91 @@ +import matplotlib.pyplot as plt +from libs.clustering import split_tour_across_clusters +from libs.aco import AntColony, total_distance + +cities = [[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 = 538 + +max_times = [0.01, 0.1, 0.2, 0.5, 1, 2, 5, 10, 20] +n_ants = 10 +alpha = 1 +beta = 4 +evaporation = 0.5 +intensification = 2 +n_runs = 5 # Nombre de fois où chaque configuration sera exécutée pour obtenir une moyenne + + +average_best_route_per_max_time = [] + +for max_time in max_times: + + total_best_route_length = 0 + + for _ in range(n_runs): + print("Run number: ", _, "for max_time: ", max_time) + clusters = split_tour_across_clusters(cities, 1) + + total_distance_for_run = 0 + + for i, cluster_indices in enumerate(clusters.values()): + cluster_cities = [cities[index] for index in cluster_indices] + + ant_colony = AntColony(cluster_cities, n_ants=n_ants, max_time=max_time, alpha=alpha, beta=beta, evaporation=evaporation, intensification=intensification) + best_route = ant_colony.run() + total_distance_for_run += total_distance(best_route) + + total_best_route_length += total_distance_for_run + + average_best_route_length = total_best_route_length / n_runs + average_best_route_per_max_time.append(average_best_route_length) + +# Maintenant, nous avons toutes les valeurs moyennes, créons un histogramme +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 +] + +plt.figure() +bar_width = 0.8 +bar_positions = range(len(max_times)) # Crée une liste d'indices pour chaque barre +plt.bar(bar_positions, average_best_route_per_max_time, width=bar_width, color=colors[:len(max_times)]) +plt.axhline(y=optimal, color='r') + +# Ajouter des valeurs au-dessus des barres +for i, v in enumerate(average_best_route_per_max_time): + plt.text(i - 0.15, v + 0.01, round(v, 2)) + +plt.xticks(bar_positions, max_times) # Fixe les labels sur l'axe des x aux valeurs de max_time +plt.xlabel('Max time') +plt.ylabel('Average best route length') + +title = "" +title += "Average best route length ({} iterations) for different max times\n".format(n_runs) +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.show() \ No newline at end of file diff --git a/tests/data_sample/ALL_tsp/ch150.opt.tour b/tests/data_sample/ALL_tsp/ch150.opt.tour new file mode 100644 index 0000000..6499874 --- /dev/null +++ b/tests/data_sample/ALL_tsp/ch150.opt.tour @@ -0,0 +1,157 @@ +NAME : ch150.opt.tour +COMMENT : Length 6528 +TYPE : TOUR +DIMENSION : 150 +TOUR_SECTION +1 +98 +103 +82 +95 +107 +5 +100 +143 +97 +146 +26 +75 +18 +142 +85 +65 +132 +137 +50 +55 +58 +141 +83 +56 +90 +46 +92 +54 +138 +134 +131 +32 +23 +38 +67 +43 +109 +51 +20 +25 +110 +81 +29 +86 +135 +70 +108 +102 +114 +99 +19 +2 +37 +6 +28 +9 +42 +120 +47 +139 +40 +53 +118 +24 +12 +116 +101 +41 +57 +39 +127 +69 +36 +61 +11 +148 +130 +17 +66 +60 +140 +117 +129 +27 +31 +123 +74 +13 +106 +91 +119 +68 +128 +45 +71 +44 +64 +112 +136 +145 +144 +49 +147 +72 +80 +14 +122 +77 +133 +15 +78 +21 +150 +115 +4 +104 +22 +125 +149 +62 +3 +113 +10 +94 +88 +121 +79 +59 +16 +111 +105 +33 +126 +52 +93 +124 +35 +96 +89 +8 +7 +84 +30 +63 +48 +73 +76 +34 +87 +-1 +EOF diff --git a/tests/data_sample/ALL_tsp/eil76.opt.tour b/tests/data_sample/ALL_tsp/eil76.opt.tour new file mode 100644 index 0000000..6fd2cba --- /dev/null +++ b/tests/data_sample/ALL_tsp/eil76.opt.tour @@ -0,0 +1,83 @@ +NAME : eil76.opt.tour +COMMENT : Optimum tour for eil76.tsp (538) +TYPE : TOUR +DIMENSION : 76 +TOUR_SECTION +1 +33 +63 +16 +3 +44 +32 +9 +39 +72 +58 +10 +31 +55 +25 +50 +18 +24 +49 +23 +56 +41 +43 +42 +64 +22 +61 +21 +47 +36 +69 +71 +60 +70 +20 +37 +5 +15 +57 +13 +54 +19 +14 +59 +66 +65 +38 +11 +53 +7 +35 +8 +46 +34 +52 +27 +45 +29 +48 +30 +4 +75 +76 +67 +26 +12 +40 +17 +51 +6 +68 +2 +74 +28 +62 +73 +-1 +EOF