-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtsp.py
46 lines (38 loc) · 1.15 KB
/
tsp.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
from random import random
from simulated_annealing import SimAnneal
import numpy as np
import time
def tsp_read(nodes):
infile = open(nodes, 'r')
content = infile.readline().strip().split()
print("File Name: ", content[2])
while content[0] != 'NODE_COORD_SECTION':
if(content[0] == 'DIMENSION'):
dimension = content[2]
content = infile.readline().strip().split()
nodelist = []
placelist = []
print('Dimension', dimension)
N = int(dimension)
for i in range(0, N):
x, y, z = infile.readline().strip().split()[:]
nodelist.append([float(y), float(z)])
placelist.append(x)
# Close input file
infile.close()
return nodelist, placelist
def main():
# generate_random_coords(100)
nodes, place = tsp_read("Data/rajasthan.tsp")
coords = np.array(nodes)
n = len(coords)
start = time.time_ns()
sa = SimAnneal(coords, place, stopping_iter=n*10000000)
end = time.time_ns()
print('Execution Time', end-start)
sa.simulated_annealing()
sa.display_optimal_path()
sa.animateSolutions()
sa.plot_learning()
if __name__ == "__main__":
main()