From 652ec7628f34f1a01a2fe1d39f7fd495c8b4d642 Mon Sep 17 00:00:00 2001 From: Louis Date: Tue, 20 Jun 2023 09:27:12 +0200 Subject: [PATCH] stats for simulated annealing --- ...aling_no_random_temperatures_variations.py | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 tests/102_analyse_simulated_annealing_no_random_temperatures_variations.py diff --git a/tests/102_analyse_simulated_annealing_no_random_temperatures_variations.py b/tests/102_analyse_simulated_annealing_no_random_temperatures_variations.py new file mode 100644 index 0000000..ab1bdd2 --- /dev/null +++ b/tests/102_analyse_simulated_annealing_no_random_temperatures_variations.py @@ -0,0 +1,118 @@ +import matplotlib.pyplot as plt +from libs.clustering import split_tour_across_clusters +from libs.simulated_annealing import SimulatedAnnealing, 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 + +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 +] + +nb_ville = 100 +max_coords = 1000 +nb_truck = 1 +temperatures = [1000, 10000, 100000,] +cooling_rate = 0.999 +temperature_ok = 0.001 +iterations = 1 + +clusters = split_tour_across_clusters(cities, nb_truck) + +for cluster in clusters.values(): + print(len(cluster)) + +# create new figure for annealing paths +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_routes = [] + +average_distances = [] + +# Pour chaque température +for temperature in temperatures: + + # Créez une liste pour stocker les distances pour chaque itération + distances = [] + + # Effectuez un certain nombre d'itérations + for _ in range(iterations): + + best_routes = [] + + for i, cluster_indices in enumerate(clusters.values()): + # Récupération des coordonnées de la ville + cluster_cities = [cities[index] for index in cluster_indices] + + # Appel de la fonction simulated_annealing + simulated_annealing = SimulatedAnnealing(cluster_cities, temperature=temperature, cooling_rate=cooling_rate, temperature_ok=temperature_ok) + best_route = simulated_annealing.run() + best_routes.append((best_route, colors[i % len(colors)])) + + # Calculez la distance totale pour toutes les routes obtenues lors de cette itération + total_distance_for_iteration = sum([total_distance(route) for route, color in best_routes]) + + # Ajoutez cette distance à la liste des distances + distances.append(total_distance_for_iteration) + + # Calculez la moyenne des distances et ajoutez-la à la liste des moyennes des distances + average_distances.append(np.mean(distances)) + +# Créez un nouvel histogramme pour afficher les moyennes des distances +plt.figure() + +# Affichez un bar pour chaque température, avec la couleur correspondante et la moyenne des distances comme hauteur +for i in range(len(temperatures)): + plt.bar(str(temperatures[i]), average_distances[i], color=colors[i % len(colors)]) + +# Ajoutez des étiquettes à chaque barre avec la moyenne des distances +for i in range(len(temperatures)): + plt.text(i, average_distances[i], round(average_distances[i], 2), ha = 'center') + +# Définir les étiquettes des axes +plt.xlabel('Température initiale') +plt.ylabel('Moyenne des distances sur {} itérations'.format(iterations)) + +# Afficher l'histogramme +plt.show() \ No newline at end of file