-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathVNS.py
214 lines (192 loc) · 5.43 KB
/
VNS.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
import matplotlib.pyplot as plt
import random
import numpy as np
import time
#读取城市的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 shaking(path):
global size
ini = visited[path]
cnt = 0
while True:
pos1,pos2,pos3 = sorted(random.sample(range(0,size),3))
path_ = path[pos1:pos2] + path[:pos1] + path[pos3:] + path[pos2:pos3]
if path_ not in visited:
cost = getCost(path_)
visited.update({path_:cost})
else:
cost = visited[path_]
cnt+=1
if ini >= cost:
break
elif cnt > 100:
path_ = path
cost = ini
break
return path_
#反转一段区间,获取新邻域
def getNei_rev(path):
global size
min = visited[path]
cnt = 0
while True:
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_]
cnt+=1
if cost < min:
min = cost
break
elif cnt > 1000:
path_ = path
break
return path_,min
#交换两个城市,获取新邻域
def getNei_exc(path):
global size
min = visited[path]
cnt = 0
while True:
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_]
cnt+=1
if cost < min:
min = cost
break
elif cnt > 1000:
path_ = path
break
return path_,min
#随机挑选两个城市插入序列头部,获取新邻域
def getNei_ins(path):
global size
min = visited[path]
cnt = 0
while True:
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_]
cnt+=1
if cost < min:
min = cost
break
elif cnt > 1000:
path_ = path
break
return path_,min
#在Local Search中使用VND方法进行搜索
def VND(path):
l = 0
min = visited[path]
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 VNS(path,kmax):
k = 0
temp = path
min = solutions[0]
global count
while k < kmax:
#扰动后进行变邻域操作
path_nei,cost = VND(shaking(temp))
print(cost)
solutions.append(cost)
count+=1
if cost < min:
temp = path_nei #记录迭代过的最优的解
min = cost
k = 0
else:
k+=1
return temp,min
def main():
time_start = time.time()
global solutions,visited,size,map
kmax = 1000
start = tuple([k for k in range(size)])
visited.update({start:getCost(start)})
solutions.append(visited[start])
path_,cost = VNS(start,kmax)
path = path_[:] + path_[:1]
time_end = time.time()
print()
print('Algorithm VNS 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 = int(input("The best solution should be: "))
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()