-
Notifications
You must be signed in to change notification settings - Fork 5
/
SA.py
47 lines (41 loc) · 1.4 KB
/
SA.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
import random
import math
import InitMutFit as imf
def SimulatedAnnealing(steps, placeholder, distMat, seed):
# hardcoded
STARTTEMP = 5
ENDTEMP = .001
random.seed(seed)
stops = distMat.shape[0]
# Randomly generate our starting point
bestSol = imf.InitializeSol(stops)
bestFit = imf.Fitness(bestSol, distMat)
currSol = bestSol
currFit = bestFit
fitHistory = []
# uniform cooling schedule based on number of steps
# There are lots of other strategies for cooling the chain,
# but this is nice and simple
deltat = (STARTTEMP - ENDTEMP) / steps
temp = STARTTEMP
for i in range(steps):
# try several steps
trialSol = imf.Mutate(bestSol)
trialFit = imf.Fitness(trialSol, distMat)
# if trialFit is better (less than) currFit, it will always
# be accepted. Otherwise, accept with probability related
# to the temperature.
probAccept = math.exp((currFit - trialFit) / temp)
if random.random() <= probAccept:
currFit = trialFit
currSol = trialSol
# update best solution if appropriate
if currFit < bestFit:
bestFit = currFit
bestSol = currSol
if i % 1000 == 0:
print(bestFit)
fitHistory.append(bestFit)
# update temperature
temp = temp - deltat
return bestSol, fitHistory