updating notebook
This commit is contained in:
parent
21cc9d19dd
commit
eadc1bdd10
File diff suppressed because one or more lines are too long
BIN
all_clusters.png
BIN
all_clusters.png
Binary file not shown.
|
Before Width: | Height: | Size: 13 KiB |
BIN
cluster_0.png
BIN
cluster_0.png
Binary file not shown.
|
Before Width: | Height: | Size: 33 KiB |
BIN
cluster_1.png
BIN
cluster_1.png
Binary file not shown.
|
Before Width: | Height: | Size: 28 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 27 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 23 KiB |
@ -30,8 +30,6 @@ print("\n---- TIME ----")
|
|||||||
print("generate cities time: ", stop_time_generate - start_time_generate)
|
print("generate cities time: ", stop_time_generate - start_time_generate)
|
||||||
print("split cities time: ", stop_time_split - start_time_split)
|
print("split cities time: ", stop_time_split - start_time_split)
|
||||||
|
|
||||||
# create new figure for annealing paths
|
|
||||||
plt.figure()
|
|
||||||
colors = [
|
colors = [
|
||||||
'#1f77b4', # Bleu moyen
|
'#1f77b4', # Bleu moyen
|
||||||
'#ff7f0e', # Orange
|
'#ff7f0e', # Orange
|
||||||
|
|||||||
43
tests/libs/simulated_annealing_stats.py
Normal file
43
tests/libs/simulated_annealing_stats.py
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
import math, random
|
||||||
|
|
||||||
|
def distance(city1, city2):
|
||||||
|
return math.sqrt((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2)
|
||||||
|
|
||||||
|
def total_distance(cities):
|
||||||
|
return sum([distance(cities[i - 1], cities[i]) for i in range(len(cities))])
|
||||||
|
|
||||||
|
class SimulatedAnnealing:
|
||||||
|
def __init__(self, cities, temperature=10000, cooling_rate=0.9999, temperature_ok=0.001):
|
||||||
|
self.cities = cities
|
||||||
|
self.temperature = temperature
|
||||||
|
self.cooling_rate = cooling_rate
|
||||||
|
self.temperature_ok = temperature_ok
|
||||||
|
self.distances = []
|
||||||
|
self.temperatures = []
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
interration = 0
|
||||||
|
current_solution = self.cities.copy()
|
||||||
|
best_solution = self.cities.copy()
|
||||||
|
while self.temperature > self.temperature_ok:
|
||||||
|
new_solution = current_solution.copy()
|
||||||
|
# Swap two cities in the route
|
||||||
|
i = random.randint(0, len(new_solution) - 1)
|
||||||
|
j = random.randint(0, len(new_solution) - 1)
|
||||||
|
new_solution[i], new_solution[j] = new_solution[j], new_solution[i]
|
||||||
|
# Calculate the acceptance probability
|
||||||
|
current_energy = total_distance(current_solution)
|
||||||
|
new_energy = total_distance(new_solution)
|
||||||
|
delta = new_energy - current_energy
|
||||||
|
|
||||||
|
if delta < 0 or random.random() < math.exp(-delta / self.temperature):
|
||||||
|
current_solution = new_solution
|
||||||
|
if total_distance(current_solution) < total_distance(best_solution):
|
||||||
|
best_solution = current_solution
|
||||||
|
|
||||||
|
if interration % 10 == 0:
|
||||||
|
self.distances.append(total_distance(current_solution))
|
||||||
|
# Cool down
|
||||||
|
self.temperature *= self.cooling_rate
|
||||||
|
interration += 1
|
||||||
|
return best_solution, self.distances
|
||||||
Loading…
x
Reference in New Issue
Block a user