-
Notifications
You must be signed in to change notification settings - Fork 0
/
SA_ConstSpeed.py
64 lines (53 loc) · 1.78 KB
/
SA_ConstSpeed.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
'''
SA_ConstSpeed
'''
import numpy as np
import SA_base
class SA_ConstSpeed(SA_base.SA_base):
INIT_TEMP_MULTIPLIER = 2.0
TINY_COOL = 0.999
FROZEN = 10
def __init__(self, problemInst, movesPerTemp=500, cool=0.9, alpha=10):
SA_base.SA_base.__init__(self, problemInst, 0.0, movesPerTemp, cool)
self.alpha = alpha
def initTemp(self):
moveDelta = []
for j in xrange(0, self.movesPerTemp):
delta = self.inst.genMove()
moveDelta.append(delta)
return self.INIT_TEMP_MULTIPLIER * np.std(moveDelta)
def newTemp(self, temp, var):
self.resetStats()
if var == 0.0:
return temp * self.cool
dif = self.alpha * temp * temp / var
if temp - dif < temp * self.cool:
return temp * self.cool
return temp - dif
def optimize(self):
moves = 0
costList = []
temp = self.initTemp()
frozenCount = 0
lastCost = 0
while frozenCount < self.FROZEN and moves < self.MAXMOVES:
for j in xrange(0, self.movesPerTemp):
moves += 1
delta = self.inst.genMove()
if self.metropolis(delta, temp):
self.commitMove(delta)
var = self.costVar()
if lastCost == self.cost:
frozenCount += 1
else:
frozenCount = 0
temp = self.newTemp(temp, var)
costList.append(moves)
costList.append(self.cost)
lastCost = self.cost
return costList
if __name__ == '__main__':
import TravSales
inst = TravSales.TravSales()
base = SA_ConstSpeed(inst, 1000, 0.9, 10)
print base.optimize()