-
Notifications
You must be signed in to change notification settings - Fork 29
/
simulated_annealing.py
113 lines (93 loc) · 3.96 KB
/
simulated_annealing.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import math
import random
import matplotlib.pyplot as plt
import tsp_utils
import animated_visualizer
class SimulatedAnnealing:
def __init__(self, coords, temp, alpha, stopping_temp, stopping_iter):
''' animate the solution over time
Parameters
----------
coords: array_like
list of coordinates
temp: float
initial temperature
alpha: float
rate at which temp decreases
stopping_temp: float
temerature at which annealing process terminates
stopping_iter: int
interation at which annealing process terminates
'''
self.coords = coords
self.sample_size = len(coords)
self.temp = temp
self.alpha = alpha
self.stopping_temp = stopping_temp
self.stopping_iter = stopping_iter
self.iteration = 1
self.dist_matrix = tsp_utils.vectorToDistMatrix(coords)
self.curr_solution = tsp_utils.nearestNeighbourSolution(self.dist_matrix)
self.best_solution = self.curr_solution
self.solution_history = [self.curr_solution]
self.curr_weight = self.weight(self.curr_solution)
self.initial_weight = self.curr_weight
self.min_weight = self.curr_weight
self.weight_list = [self.curr_weight]
print('Intial weight: ', self.curr_weight)
def weight(self, sol):
'''
Calcuate weight
'''
return sum([self.dist_matrix[i, j] for i, j in zip(sol, sol[1:] + [sol[0]])])
def acceptance_probability(self, candidate_weight):
'''
Acceptance probability as described in:
https://stackoverflow.com/questions/19757551/basics-of-simulated-annealing-in-python
'''
return math.exp(-abs(candidate_weight - self.curr_weight) / self.temp)
def accept(self, candidate):
'''
Accept with probability 1 if candidate solution is better than
current solution, else accept with probability equal to the
acceptance_probability()
'''
candidate_weight = self.weight(candidate)
if candidate_weight < self.curr_weight:
self.curr_weight = candidate_weight
self.curr_solution = candidate
if candidate_weight < self.min_weight:
self.min_weight = candidate_weight
self.best_solution = candidate
else:
if random.random() < self.acceptance_probability(candidate_weight):
self.curr_weight = candidate_weight
self.curr_solution = candidate
def anneal(self):
'''
Annealing process with 2-opt
described here: https://en.wikipedia.org/wiki/2-opt
'''
while self.temp >= self.stopping_temp and self.iteration < self.stopping_iter:
candidate = list(self.curr_solution)
l = random.randint(2, self.sample_size - 1)
i = random.randint(0, self.sample_size - l)
candidate[i: (i + l)] = reversed(candidate[i: (i + l)])
self.accept(candidate)
self.temp *= self.alpha
self.iteration += 1
self.weight_list.append(self.curr_weight)
self.solution_history.append(self.curr_solution)
print('Minimum weight: ', self.min_weight)
print('Improvement: ',
round((self.initial_weight - self.min_weight) / (self.initial_weight), 4) * 100, '%')
def animateSolutions(self):
animated_visualizer.animateTSP(self.solution_history, self.coords)
def plotLearning(self):
plt.plot([i for i in range(len(self.weight_list))], self.weight_list)
line_init = plt.axhline(y=self.initial_weight, color='r', linestyle='--')
line_min = plt.axhline(y=self.min_weight, color='g', linestyle='--')
plt.legend([line_init, line_min], ['Initial weight', 'Optimized weight'])
plt.ylabel('Weight')
plt.xlabel('Iteration')
plt.show()