-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtsp_ls_naive.codon
357 lines (317 loc) · 12.4 KB
/
tsp_ls_naive.codon
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/opt/homebrew/bin/python3
# -*- coding: utf-8 -*-
# --------------------------------------------------------------------
# Local search for TSP
#
# Author: Shunji Umetani <umetani@ist.osaka-u.ac.jp>
# Date: 2023/3/22
# --------------------------------------------------------------------
# import modules -----------------------------------------------------
import sys
import time
import math
import random
from typing import List
from typing import Tuple
# constant -----------------------------------------------------------
OR_OPT_SIZE = 3 # maximum size of subpath (or_opt_search)
# --------------------------------------------------------------------
# TSP data
# --------------------------------------------------------------------
class Tsp:
name: str
num_node: int
coord: List[Tuple[float, float]]
# constructor ----------------------------------------------------
def __init__(self):
self.name = '' # name of TSP instance
self.num_node = 0 # number of nodes
self.coord = [] # coordinate list of nodes
# read TSP data --------------------------------------------------
def read(self, args):
# open file
if len(args) != 2:
print('Invalid arguments!')
sys.exit(1)
input_file = open(args[1], 'r')
raw_data: List[str] = input_file.readlines()
input_file.close()
# read data
data: List[List[str]] = [[] for _ in range(len(raw_data))]
start_coord: int = -1
for i in range(len(raw_data)):
data[i] = (raw_data[i].rstrip()).split()
data[i] = list(filter(lambda str:str != ':', data[i])) # remove colon
if len(data[i]) > 0:
data[i][0] = data[i][0].rstrip(':') # remove colon
match data[i][0]:
case 'NAME':
self.name = data[i][1]
case 'TYPE':
if data[i][1] != 'TSP':
print('Problem type is not TSP!')
sys.exit(1)
case 'DIMENSION':
self.num_node = int(data[i][1])
case 'EDGE_WEIGHT_TYPE': # accept only EUC_2D
if data[i][1] != 'EUC_2D':
print('Edge weight type is not EUC_2D!')
sys.exit(1)
case 'NODE_COORD_SECTION':
start_coord = i
# coord section
self.coord = [(0.0, 0.0)] * self.num_node
i = start_coord+1
for k in range(self.num_node):
self.coord[k] = (float(data[i][1]), float(data[i][2]))
i += 1
# write TSP data -------------------------------------------------
def write(self):
print('\n[TSP data]')
print(f'name:\t{self.name}')
print(f'#node:\t{self.num_node}')
print(f'coord:\t{self.coord}')
# calculate distance (rounded Euclidean distance in 2D) ----------
def dist(self, v1: int, v2: int) -> int:
xd = self.coord[v1][0] - self.coord[v2][0]
yd = self.coord[v1][1] - self.coord[v2][1]
return int(math.sqrt(xd * xd + yd * yd)+0.5)
# --------------------------------------------------------------------
# working data
# --------------------------------------------------------------------
class Work:
tour: List[int] # tour of salesman
obj: int # objective value
# constructor ----------------------------------------------------
def __init__(self, tsp):
self.tour = [i for i in range(tsp.num_node)]
self.obj = 0
# calculate tour length ------------------------------------------
def tour_len(self, tsp) -> int:
length: int = 0
for i in range(len(self.tour)):
length += tsp.dist(self.tour[i], self.tour[(i+1) % len(self.tour)])
return length
# write working data ---------------------------------------------
def write(self, tsp):
print('\n[Tour data]')
print(f'length:\t{self.tour_len(tsp)}')
# function -----------------------------------------------------------
# --------------------------------------------------------------------
# nearest neighbor algorithm
# tsp[in] TSP data
# work[in,out] working data
# --------------------------------------------------------------------
def nearest_neighbor(tsp, work):
print('\n[nearest neighbor]')
# nearest neighbor
for i in range(1,tsp.num_node):
# find nearest unvisited node
min_dist: int = sys.maxsize
arg_min_dist: int = -1
for j in range(i, tsp.num_node):
dist: int = tsp.dist(work.tour[i-1], work.tour[j])
if dist < min_dist:
min_dist = dist
arg_min_dist = j
# set nearest unvisited node
work.tour[i], work.tour[arg_min_dist] = work.tour[arg_min_dist], work.tour[i]
# calculate tour length
work.obj = work.tour_len(tsp)
print(f'length:\t{work.obj}')
# --------------------------------------------------------------------
# 2-opt search
# tsp[in] TSP data
# work[in,out] working data
# -> [True] improved
# --------------------------------------------------------------------
def two_opt_search(tsp, work) -> bool:
# incremental evaluation
def inc_eval(tsp, work, i: int, j: int) -> int:
u, next_u = work.tour[i], work.tour[(i+1) % len(work.tour)]
v, next_v = work.tour[j], work.tour[(j+1) % len(work.tour)]
cur = tsp.dist(u, next_u) + tsp.dist(v, next_v)
new = tsp.dist(u, v) + tsp.dist(next_u, next_v)
return new - cur
# change tour
def change_tour(tsp, work, i: int, j: int):
# reverse subpath [i+1,...,j]
work.tour[i+1:j+1] = work.tour[j:i:-1]
# update tour length
work.obj = work.tour_len(tsp)
# 2-opt search (main)
improved: bool = False
restart: bool = True
while restart:
restart = False
nbhd = ((i,j)
for i in range(len(work.tour))
for j in range(i+2, len(work.tour)))
for i,j in nbhd:
delta = inc_eval(tsp, work, i, j)
if delta < 0:
change_tour(tsp, work, i, j)
improved = True
restart = True
break
return improved
# --------------------------------------------------------------------
# Or-opt search
# tsp[in] TSP data
# work[in,out] working data
# size[in] length of subpath
# -> [True] improved
# --------------------------------------------------------------------
def or_opt_search(tsp, work, size: int = OR_OPT_SIZE) -> bool:
# incremental evaluation
def inc_eval(tsp, work, s: int, i: int, j: int) -> Tuple[int, str]:
head_p, tail_p = work.tour[i], work.tour[(i+s-1) % len(work.tour)]
prev_p, next_p = work.tour[(i-1) % len(work.tour)], work.tour[(i+s) % len(work.tour)]
v, next_v = work.tour[j % len(work.tour)], work.tour[(j+1) % len(work.tour)]
cur = tsp.dist(prev_p, head_p) + tsp.dist(tail_p, next_p) + tsp.dist(v, next_v)
new_fwd = tsp.dist(prev_p, next_p) + tsp.dist(v, head_p) + tsp.dist(tail_p, next_v)
new_bak = tsp.dist(prev_p, next_p) + tsp.dist(v, tail_p) + tsp.dist(head_p, next_v)
if new_fwd <= new_bak:
return (new_fwd - cur, 'fwd')
else:
return (new_bak - cur, 'bak')
# change tour
def change_tour(tsp, work, s: int, i: int, j: int, oper: str):
# get subpath [i,...,i+s-1]
subpath: List[int] = []
for h in range(s):
subpath.append(work.tour[(i+h) % len(work.tour)])
if oper == 'bak':
subpath.reverse()
# move subpath [i,...,i+s-1] to j+1
for h in range(i+s,j+1):
work.tour[(h-s) % len(work.tour)] = work.tour[h % len(work.tour)]
for h in range(s):
work.tour[(j+1-s+h) % len(work.tour)] = subpath[h]
# update tour length
work.obj = work.tour_len(tsp)
# Or-opt search (main)
improved: bool = False
restart: bool = True
while restart:
restart = False
nbhd = ((s,i,j)
for s in range(1,size+1)
for i in range(len(work.tour))
for j in range(i+s,i+len(work.tour)-1))
for s,i,j in nbhd:
delta, oper = inc_eval(tsp, work, s, i, j)
if delta < 0:
change_tour(tsp, work, s, i, j, oper)
improved = True
restart = True
break
return improved
# --------------------------------------------------------------------
# 3-opt search
# tsp[in] TSP data
# work[in,out] working data
# -> [True] improved
# --------------------------------------------------------------------
def three_opt_search(tsp, work) -> bool:
# incremental evaluation
def inc_eval(tsp, work, i: int, j: int, k: int) -> Tuple[int, str]:
best: int = sys.maxsize
arg_best: str = ''
u, next_u = work.tour[i], work.tour[(i+1) % len(work.tour)]
v, next_v = work.tour[j], work.tour[(j+1) % len(work.tour)]
w, next_w = work.tour[k], work.tour[(k+1) % len(work.tour)]
cur = tsp.dist(u, next_u) + tsp.dist(v, next_v) + tsp.dist(w, next_w)
# type1
new = tsp.dist(u, next_v) + tsp.dist(v, next_w) + tsp.dist(w, next_u)
if new - cur < best:
best, arg_best = new - cur, 'type1'
# type2
new = tsp.dist(u, w) + tsp.dist(next_v, next_u) + tsp.dist(v, next_w)
if new - cur < best:
best, arg_best = new - cur, 'type2'
# type3
new = tsp.dist(u, next_v) + tsp.dist(w, v) + tsp.dist(next_u, next_w)
if new - cur < best:
best, arg_best = new - cur, 'type3'
# type4
new = tsp.dist(v, u) + tsp.dist(next_w, next_v) + tsp.dist(w, next_u)
if new - cur < best:
best, arg_best = new - cur, 'type4'
return (best, arg_best)
# change tour
def change_tour(tsp, work, i: int, j: int, k:int, oper :str):
match oper:
case 'type1':
work.tour[i+1:k+1] = work.tour[j+1:k+1] + work.tour[i+1:j+1]
case 'type2':
work.tour[i+1:k+1] = work.tour[k:j:-1] + work.tour[i+1:j+1]
case 'type3':
work.tour[i+1:k+1] = work.tour[j+1:k+1] + work.tour[j:i:-1]
case 'type4':
work.tour[i+1:k+1] = work.tour[j:i:-1] + work.tour[k:j:-1]
# update tour length
work.obj = work.tour_len(tsp)
# 3-opt search (main)
improved = False
restart = True
while restart:
restart = False
nbhd = ((i,j,k)
for i in range(len(work.tour))
for j in range(i+2,len(work.tour))
for k in range(j+2,len(work.tour)))
for i,j,k in nbhd:
delta, oper = inc_eval(tsp, work, i, j, k)
if delta < 0:
change_tour(tsp, work, i, j, k, oper)
improved = True
restart = True
break
return improved
# --------------------------------------------------------------------
# local search algorithm
# tsp[in] TSP data
# work[in,out] working data
# --------------------------------------------------------------------
def local_search(tsp, work):
print('\n[local search]')
# local search
while True:
# 2-opt search
two_opt_search(tsp, work)
# Or-opt search
if or_opt_search(tsp, work):
continue
# 3-opt search
if three_opt_search(tsp, work):
continue
break
# calculate tour length
work.obj = work.tour_len(tsp)
print(f'length:\t{work.obj}')
# --------------------------------------------------------------------
# main
# --------------------------------------------------------------------
def main(args=sys.argv):
# set starting time
start_time = time.time()
# read instance
tsp = Tsp()
tsp.read(args)
tsp.write()
# solve TSP
work = Work(tsp)
nearest_neighbor(tsp, work)
local_search(tsp, work)
work.write(tsp)
# set completion time
end_time = time.time()
# display computation time
print(f'\nTotal time:\t{end_time - start_time:.3f} sec')
# main ---------------------------------------------------------------
if __name__ == "__main__":
main()
# --------------------------------------------------------------------
# end of file
# --------------------------------------------------------------------