-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimulated_annealing.py
More file actions
64 lines (38 loc) · 1.15 KB
/
Copy pathsimulated_annealing.py
File metadata and controls
64 lines (38 loc) · 1.15 KB
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
# This code is for fully educational purposes and it implements the simulated annealing AI algorithm
# for more information please visit: https://en.wikipedia.org/wiki/Simulated_annealing
import numpy
import time
import random
NEIGHBORHOOD_MAX = 40
NEIGHBORHOOD_MIN = 0
T_MAX = 100
T_MIN = 0.01
def f(x):
assert NEIGHBORHOOD_MIN <= x <= NEIGHBORHOOD_MAX
return numpy.sin(0.15 * x) + numpy.cos(x)
def acceptance(s, new_s, temperature):
return numpy.exp(-1 * float(new_s - s) / float(temperature))
def next_neighbor(sol, alpha, T, T_max):
move = alpha * NEIGHBORHOOD_MAX
if sol - move <= NEIGHBORHOOD_MIN:
return sol + move
elif sol + move >= NEIGHBORHOOD_MAX:
return sol - move
else:
return sol + move * random.uniform(-1, 1)
def SA(sol):
s = sol
T = T_MAX
alpha = 0.4
beta = 0.9
while T > T_MIN:
print 'S: %s | T: %s' % (str(s), str(T))
for _ in range(200):
new_s = next_neighbor(s, alpha, T, T_MAX)
if f(new_s) > f(s) or acceptance(s, new_s, T) > random.uniform(0, 1):
s = new_s
alpha *= beta
T *= beta
return s, f(s), f(12.6025895306)
if __name__ == '__main__':
print 'SOL: %s | RESULT: %s | TARGET: %s' % SA(15.0)