-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSA.py
216 lines (195 loc) · 6.13 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
from cmath import log
import matplotlib.pyplot as plt
import random
import numpy as np
import time
import math
#读取城市的x,y坐标
def load(txt):
f = open(txt)
map=[]
flag = 0
for line in f:
line = line.strip()
if line == "NODE_COORD_SECTION":
flag = 1
continue
if line == "EOF":
break
if flag:
a = line.split()
map.append((float(a[1]),float(a[2])))
return tuple(map)
#获取两个城市间的二维欧几里得距离
def getDist():
global map,size
dist = np.zeros((size,size))
for i in range(0,size):
for j in range(0,size):
dist[i][j] = ((map[i][0]-map[j][0])**2 + (map[i][1]-map[j][1])**2)**0.5
return dist
txt = "C:\\Users\\Cecilia\\Desktop\\TSP\\a280.txt"
map = load(txt)
size = len(map)
visited = {}
solutions = []
DIST = getDist()
count = 0
#根据路径获取该路径总代价
def getCost(path):
cost = 0
former = path[0]
for city in path:
cost += DIST[former][city]
former = city
cost += DIST[path[0]][path[-1]]
return cost
#反转一段区间,获取新邻域
def getNei_rev(path):
global size
min = 1000000000
for cnt in range(100):
i,j = sorted(random.sample(range(1,size-1),2))
path_ = path[:i] + path[i:j+1][::-1] + path[j+1:]
if path_ not in visited:
cost = getCost(path_)
visited.update({path_:cost})
else:
cost = visited[path_]
if cost < visited[path]:
min = cost
p = path_
break
if cost < min:
min = cost
p = path_
'''cost -= DIST[path[i]][path[i-1]] + DIST[path[j]][path[j+1]]
cost += DIST[path[i-1]][path[j]] + DIST[path[i]][path[j+1]]
if int(cost) == int(getCost(path_)):
break
else:
continue'''
return p,min
#交换两个城市,获取新邻域
def getNei_exc(path):
global size
min = 1000000000
for cnt in range(100):
i,j = sorted(random.sample(range(1,size-1),2))
path_ = path[:i] + path[j:j+1] + path[i+1:j] + path[i:i+1] + path[j+1:]
if path_ not in visited:
cost = getCost(path_)
visited.update({path_:cost})
else:
cost = visited[path_]
if cost < visited[path]:
min = cost
p = path_
break
if cost < min:
min = cost
p = path_
'''cost -= DIST[path[i]][path[i-1]] + DIST[path[j]][path[j-1]] + DIST[path[i]][path[i+1]] + DIST[path[j]][path[j+1]]
cost += DIST[path[i-1]][path[j]] + DIST[path[j]][path[i+1]] + DIST[path[j-1]][path[i]] + DIST[path[i]][path[j+1]]
if int(cost) == int(getCost(path_)):
break
else:
continue'''
return p,min
#随机挑选两个城市插入序列头部,获取新邻域
def getNei_ins(path):
global size
min = 1000000000
for cnt in range(100):
i,j = sorted(random.sample(range(1,size-1),2))
path_ = path[i:i+1] + path[j:j+1] + path[:i] + path[i+1:j] + path[j+1:]
if path_ not in visited:
cost = getCost(path_)
visited.update({path_:cost})
else:
cost = visited[path_]
if cost < visited[path]:
min = cost
p = path_
break
if cost < min:
min = cost
p = path_
'''cost -= DIST[path[i]][path[i-1]] + DIST[path[j]][path[j-1]] + DIST[path[i]][path[i+1]] + DIST[path[j]][path[j+1]] + DIST[path[0]][path[-1]]
cost += DIST[path[i]][path[j]] + DIST[path[j]][path[0]] + DIST[path[i-1]][path[i+1]] + DIST[path[j-1]][path[j+1]] + DIST[path[-1]][path[i]]
if int(cost) == int(getCost(path_)):
break
else:
continue'''
return p,min
#在Local Search中使用VND方法进行搜索
def VND(path):
path,min = getNei_rev(path)
l = 1
while l < 3:
if l == 0:
path_,cost = getNei_rev(path)
elif l == 1:
path_,cost = getNei_exc(path)
elif l == 2:
path_,cost = getNei_ins(path)
if cost < min:
path = path_
min = cost
l = 0
else:
l+=1
return path,min
#模拟退火算法
def SA(path,kmax,t0,t_end):
temp = path
min = solutions[0]
result = [temp,min] #记录迭代过的最优的解
global count
t = t0 #初始温度
while t > t_end:
for k in range(1,kmax):
path_nei,cost = VND(temp) #进行变邻域操作
#print(cost)
solutions.append(cost)
count+=1
#判断是否接受该解
if cost < min or random.random() < np.exp(-((cost-min)/t*k)):
temp = path_nei
min = cost
if cost < result[1]:
result = [path_nei,cost]
#t/=math.log10(1+k)
t/=k+1 #降温操作
return result[0],result[1]
def main():
global solutions,visited,size,map
kmax = 100
t0 = 500000
t_end = 0.00001
start = tuple([k for k in range(size)])
visited.update({start:getCost(start)})
solutions.append(visited[start])
time_start = time.time()
global count
count = 0
path_,cost = SA(start,kmax,t0,t_end)
path = path_[:] + path_[:1]
time_end = time.time()
print()
print('Algorithm SA iterated',count,'times!\n',sep=' ')
print('It cost ',time_end-time_start,'s',sep='') #此处单位为秒
print('You got the best solution:',cost,sep='\n')
print(path)
best = 2579
print("误差为:",(cost-best)/best)
x = np.array([map[i][0] for i in path])
y = np.array([map[i][1] for i in path])
i = np.arange(0,len(solutions))
solutions = np.array(solutions)
plt.subplot(121)
plt.plot(x,y)
plt.subplot(122)
plt.plot(i,solutions)
plt.show()
main()