forked from sgkruk/Apress-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_tsp.py
67 lines (66 loc) · 2.09 KB
/
test_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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import print_function
from tsp import gen_data, solve_model, solve_model_eliminate, solve_model_p, solve_model_star
def main():
import sys
import random
import tableutils
n=10
if len(sys.argv)<=1:
print('Usage is main [data|display|run|path|star] [seed]')
return
elif len(sys.argv)>2:
random.seed(int(sys.argv[2]))
C,Points=gen_data(n)
header = ['P (x y)']+['P'+str(i) for i in range(n)]
left = ['P'+str(i)+' '+str(Points[i][0])+' '+str(Points[i][1])+' ' for i in range(n)]
if sys.argv[1]=='data':
tableutils.printmat(tableutils.wrapmat(C,left,header),False,1)
elif sys.argv[1]=='display':
subtours=[]
tours = []
count = 0
display = []
while count < 10 and len(tours) != 1:
rc,Value,tours=solve_model_eliminate(C,subtours)
subtours.extend(tours)
display.append(['{0}-({1})'.format(count,int(Value))]+[tour for tour in tours])
count += 1
display.insert(0,['Iter (value)','Tour(s)'])
for row in display:
l=''
for i in range(1,len(row)):
l=l+' '+str(row[i])
if i < len(row)-1:
l=l+';'
print('{0}, "{1}"'.format(row[0],l))
elif sys.argv[1]=='run':
rc,Value,tours=solve_model(C)
T=[tours]
Cost=[0]
for i in range(n):
Cost.append(C[tours[i]][tours[(i+1)%len(tours)]])
T.append(Cost)
tableutils.printmat(T,True)
elif sys.argv[1]=='path':
rc,Value,path=solve_model_p(C)
T=[['Nodes']+path]
Cost=['Distance',0]
Tcost=['Cumulative',0]
for i in range(n-1):
Cost.append(C[path[i]][path[(i+1)]])
Tcost.append(Tcost[-1]+Cost[-1])
#Cost.append('Total:'+str(Value))
T.append(Cost)
T.append(Tcost)
tableutils.printmat(T,True)
elif sys.argv[1]=='star':
rc,Value,Tour=solve_model_star(C)
Tour.append(Tour[0])
T=[Tour]
Cost=['Total dist '+str(int(Value)),0]
for i in range(len(Tour)-1):
Cost.append(C[Tour[i]][Tour[(i+1)%len(Tour)]])
Tour.insert(0,'NB '+str(len(Tour)))
T.append(Cost)
tableutils.printmat(T,True)
main()