-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTSP_HC.py
68 lines (61 loc) · 1.55 KB
/
TSP_HC.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
import random
def neighbours(sol, j):
'''
l = [sol[0:j-1] + sol[j:i] + [sol[j-1]] + sol[i:] for i in range(j, N+1) if sol[0:j-1] + sol[j:i] + [sol[j-1]] + sol[i:] not in used]
used.extend(l)
print(sol)
'''
l = []
for i in range(2, N):
c = [] + sol
c[j], c[i] = c[i], c[j]
if c not in used:
l.append(c)
used.extend(l)
return l
def cost(sol):
if len(sol) == 0:
return 0
s = 0
for i in range(1,len(sol)):
s += V[sol[i]-1][sol[i-1]-1]
return s + V[sol[len(sol)-1]-1][sol[0]-1]
def tsp():
x = [i for i in range(1, N + 1)]
k = 0
j = 1
while j < N:
print(cost(x))
k += 1
n = neighbours(x, j)
if len(n) == 0:
j += 1
continue
min_n = min(n, key = lambda a : cost(a))
if cost(min_n) >= cost(x):
j += 1
continue
x = min_n
print(k)
return x
def solve():
global N
N = 0
global V
V = []
global used
used = []
with open("input.txt", "r") as f:
N = int(f.readline())
for i in range(N):
try:
V.append(list(map(float, f.readline().strip().split(','))))
except ValueError:
pass
solution = tsp()
with open("output.txt", "w") as f:
out = ",".join(map(str, solution))
f.write(str(len(solution)) + "\n")
f.write(out + "\n")
f.write(str(cost(solution)))
solve()