updating libs simulated_annealing + tests

This commit is contained in:
Louis 2023-06-18 11:37:12 +02:00
parent e1ae299313
commit c903686d07
2 changed files with 16 additions and 18 deletions

View File

@ -1,17 +1,16 @@
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from libs.simulated_annealing import SimulatedAnnealing, distance, total_distance from libs.simulated_annealing import SimulatedAnnealing, 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]] 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 = 7542 optimal = 33523
temperature = 10000 temperatures = [100000, 1000000, 10000000, 100000000]
cooling_rate = 0.999 cooling_rate = 0.99999
temperature_ok = 0.01 temperature_ok = 0.000001
max_times = [1, 2, 5]
iterations = 2 iterations = 2
best_distances = [] best_distances = []
times = [] temps = []
colors = [ colors = [
'#1f77b4', # Bleu moyen '#1f77b4', # Bleu moyen
'#ff7f0e', # Orange '#ff7f0e', # Orange
@ -35,13 +34,13 @@ colors = [
'#999999', # Gris moyen '#999999', # Gris moyen
] ]
for max_time in max_times: for temperature in temperatures:
for iteration in range(iterations): for iteration in range(iterations):
simulated_annealing = SimulatedAnnealing(cities, temperature=10000, cooling_rate=0.999, temperature_ok=0.01) simulated_annealing = SimulatedAnnealing(cities, temperature=temperature, cooling_rate=0.999, temperature_ok=0.01)
print("Running iteration number {}/{} ({} sec)".format(iteration + 1, iterations, max_time)) print("Running iteration number {}/{} ({} temperature)".format(iteration + 1, iterations, temperature))
best_distance, best_route = SimulatedAnnealing.run() best_route = simulated_annealing.run()
best_distances.append([best_distance, colors[max_times.index(max_time) % len(colors)]]) best_distances.append([total_distance(best_route), colors[temperatures.index(temperature) % len(colors)]])
times.append(max_time) temps.append(temperature)
title = "" title = ""
title += "Best distance per iterations\n" title += "Best distance per iterations\n"
@ -68,7 +67,7 @@ bars = plt.bar(range(len(values)), values, color=colors)
for i, bar in enumerate(bars): for i, bar in enumerate(bars):
yval = bar.get_height() yval = bar.get_height()
plt.text(bar.get_x() + bar.get_width()/2, yval + 0.05, plt.text(bar.get_x() + bar.get_width()/2, yval + 0.05,
"dist: {}\ntime: {}s".format(int(yval), times[i]), "dist: {}\ntime: {}s".format(int(yval), temps[i]),
rotation=75, ha='center', va='bottom') rotation=75, ha='center', va='bottom')
plt.xticks(range(len(values)), [str(i+1) for i in range(len(values))]) plt.xticks(range(len(values)), [str(i+1) for i in range(len(values))])

View File

@ -7,12 +7,11 @@ def total_distance(cities):
return sum([distance(cities[i - 1], cities[i]) for i in range(len(cities))]) return sum([distance(cities[i - 1], cities[i]) for i in range(len(cities))])
class SimulatedAnnealing: class SimulatedAnnealing:
def __init__(self, cities, temperature=10000, cooling_rate=0.9999, temperature_ok=0.001, cluster_index=0): def __init__(self, cities, temperature=10000, cooling_rate=0.9999, temperature_ok=0.001):
self.cities = cities self.cities = cities
self.temperature = temperature self.temperature = temperature
self.cooling_rate = cooling_rate self.cooling_rate = cooling_rate
self.temperature_ok = temperature_ok self.temperature_ok = temperature_ok
self.cluster_index = cluster_index
def run(self): def run(self):
interration = 0 interration = 0
@ -37,5 +36,5 @@ class SimulatedAnnealing:
interration += 1 interration += 1
# Print every 1000 iterations # Print every 1000 iterations
if interration % 1000 == 0: if interration % 1000 == 0:
print("Cluster", self.cluster_index, ": iteration", interration, "with current total distance", total_distance(current_solution)) print("Iteration", interration, "with distance", total_distance(current_solution))
return best_solution return best_solution