-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetSuccessors.py
78 lines (64 loc) · 2.98 KB
/
getSuccessors.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
import math
def getSuccessors(solution, temperature):
maxDist = 10*len(solution.keys())
dist = int(math.ceil((temperature*maxDist)/(10)))
if dist < 1: dist = 1
successors = []
for key in solution.keys():
for i in range(dist):
sOne = dict(solution)
sTwo = dict(solution)
sThree = dict(solution)
sFour = dict(solution)
sFive = dict(solution)
sSix = dict(solution)
sSeven = dict(solution)
sEight = dict(solution)
newSuccessors = []
if (solution[key][0] + i) < maxDist:
sOne[key] = ((solution[key][0] + i), (solution[key][1]))
newSuccessors.append(sOne)
if (solution[key][1] + i) < maxDist:
sFive[key] = ((solution[key][0] + i), (solution[key][1] + i))
newSuccessors.append(sFive)
if (solution[key][1] - i) > 0:
sEight[key] = ((solution[key][0] + i), (solution[key][1] - i))
newSuccessors.append(sEight)
if (solution[key][0] - i) > 0:
sTwo[key] = ((solution[key][0] - i), (solution[key][1]))
newSuccessors.append(sTwo)
if (solution[key][1] + i) < maxDist:
sSix[key] = ((solution[key][0] - i), (solution[key][1] + i))
newSuccessors.append(sSix)
if (solution[key][1] - i) > 0:
sSeven[key] = ((solution[key][0] - i), (solution[key][1] - i))
newSuccessors.append(sSeven)
if (solution[key][1] + i) < maxDist:
sThree[key] = ((solution[key][0]), (solution[key][1] + i))
newSuccessors.append(sThree)
if (solution[key][1] - i) > 0:
sFour[key] = ((solution[key][0]), (solution[key][1] - i))
newSuccessors.append(sFour)
successors.extend(newSuccessors)
return successors
def getSuccessorsHill(solution, dist=1):
successors = []
for key in solution.keys():
sOne = dict(solution)
sTwo = dict(solution)
sThree = dict(solution)
sFour = dict(solution)
sFive = dict(solution)
sSix = dict(solution)
sSeven = dict(solution)
sEight = dict(solution)
sOne[key] = ((solution[key][0] + dist), (solution[key][1]))
sTwo[key] = ((solution[key][0] - dist), (solution[key][1]))
sThree[key] = ((solution[key][0]), (solution[key][1] + dist))
sFour[key] = ((solution[key][0]), (solution[key][1] - dist))
sFive[key] = ((solution[key][0] + dist), (solution[key][1] + dist))
sSix[key] = ((solution[key][0] - dist), (solution[key][1] + dist))
sSeven[key] = ((solution[key][0] - dist), (solution[key][1] - dist))
sEight[key] = ((solution[key][0] + dist), (solution[key][1] - dist))
successors.extend([sOne, sTwo, sThree, sFour, sFive, sSix, sSeven, sEight])
return successors