Repository navigation
Expand file tree
/
Copy pathsimulated_annealing.py
More file actions
59 lines (38 loc) · 2.21 KB
/
Copy pathsimulated_annealing.py
File metadata and controls
59 lines (38 loc) · 2.21 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
import nbr_hood as nbr
import random
import pandas as pd
import numpy as np
from math import exp
#create class for hill climb algorithms
class simulatedAnnealing(nbr.nbrHood):
def __init__(self,df,numGroups,nbrhoodsize):
super().__init__(df,numGroups)
self.nbrhoodsize = nbrhoodsize
def runSimulatedAnnealing(self,coolingSchedule,tempIters,aggMethod,distMetric,randStart='Y',startMetric=1):
#random.seed(seed)
obs = 0
#create a starting solution
startSolution = self.startSol(randStart=randStart,startMetric=startMetric)
currentSolution = startSolution
currentDist = self.totalDist(self.groupMetrics(startSolution,aggMethod),distMetric)
for temperature in coolingSchedule:
print('temparature is %s' % temperature)
for iters in range(0,tempIters):
temp_nbr = self.createNbrhood(currentSolution,2)
obs = obs + 1
#calculate difference between current nbr and the best nbr
delta = self.totalDist(self.groupMetrics(temp_nbr[0],aggMethod),distMetric) - self.totalDist(self.groupMetrics(currentSolution,aggMethod),distMetric)
#calculate acceptance probability
if delta < 0:
acceptProb = 1.1
else:
acceptProb = exp(-delta/temperature)
#decide if it will be accepted
if acceptProb > random.uniform(0,1):
currentSolution = temp_nbr[0]
currentDist = self.totalDist(self.groupMetrics(currentSolution,aggMethod),distMetric)
print(currentDist)
obs_statement = ("%s Solutions Examined" % obs)
print("done")
#return solution metric and solution
return [self.groupMetrics(currentSolution,aggMethod),currentSolution,currentDist,obs_statement]