-
Notifications
You must be signed in to change notification settings - Fork 2
/
tests.py
177 lines (145 loc) · 4.19 KB
/
tests.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
from tsp import TSP
from pandas import DataFrame, set_option
import time
def createRandomCompleteGraph(n, symmetric=False):
"""
create a complete symmetric graph with
random edge weight
"""
from random import randint
v = [i for i in range(1, n+1)]
e = []
M = [[0 for v1 in v] for v2 in v]
for ind1, i in enumerate(v):
ind2 = ind1+1
for j in v[ind1+1:]:
if i != j:
w = randint(1, 100)
e.append((i,j,w))
e.append((j,i,w))
M[ind1][ind2] = w
if symmetric:
M[ind2][ind1] = w
else:
w = randint(1, 100)
M[ind2][ind1] = w
ind2 += 1
import numpy as np
M = np.array(M)
#print(M, "\n")
return v, e, M
def callAndTime(input_):
"""
Wrapper for calling and timing a function
args:
input: List
func at zeroth index and
rest are arguments
"""
func= input_[0]
args = input_[1:]
start = time.time()
ret = func(*args)
timetaken = time.time() - start
return ret, timetaken
def test2(n=500):
"""
Given number of nodes -
Test greedy tour using default starting node,
two optimal tour using greedy tour
three optimal tour using greedy tour
three optimal tour using two optimal tour
"""
v, e, M = createRandomCompleteGraph(n)
tsp = TSP(v, e)
print("Greedy tour")
(greedytour, greedytourlen), time = callAndTime((tsp.greedyTour,))
print(greedytour, greedytourlen, time)
print("\n2OPT")
(twoopttour, twooptlen), time = callAndTime((tsp.twoOPT, greedytour))
print(twoopttour, twooptlen, time)
print("\n3OPT Using greedytour")
(threeopttour, threeoptlen), time = callAndTime((tsp.threeOPT, greedytour))
print(threeopttour, threeoptlen, time)
print("\n3OPT Using 2OPT tour")
(threeopttour, threeoptlen), time = callAndTime((tsp.threeOPT, twoopttour))
print(threeopttour, threeoptlen, time)
def testResultDataFrame(rows, columns=None):
"""
Generic function to create pandas dataframe
on results of different test runs
with different starting node
"""
if columns == None:
columns_ = ["StartNode", "GreedyTour",
"TwoOPT", "ThreeOPT_Greedy",
"Time1", "ThreeOPT_TwoOPT", "Time2"]
else:
columns_ = columns
df = DataFrame(rows, columns=columns_)
set_option('display.max_columns', None)
print(df)
df.to_csv('sampleresult.csv', index=False)
print(f"GREEDY SOL AVG LENGTH = {df.GreedyTour.mean()}")
print(f"2 OPT SOL AVG LENGTH = {df.TwoOPT.mean()}")
print(f"3 OPT FROM GREEDY SOL AVG LENGTH = {df['ThreeOPT_Greedy'].mean()} time = {df.Time1.mean()}")
print(f"3 OPT FROM 2 OPT SOL AVG LENGTH = {df['ThreeOPT_TwoOPT'].mean()} time = {df.Time2.mean()}")
def test3(n=30, tsp_object=None):
"""
Test sequentially over all possible starting
node
"""
if isinstance(tsp_object, TSP):
tsp = tsp_object
else:
v, e, M = createRandomCompleteGraph(n)
tsp = TSP(v, e)
rows = []
for v_ in tsp.nodes:
greedytour, greedytourlen = tsp.greedyTour(v_)
twoopttour, twooptlen = tsp.twoOPT(greedytour)
(threeopttour1, threeoptlen1), time1 = callAndTime((tsp.threeOPT, greedytour))
(threeopttour2, threeoptlen2), time2 = callAndTime((tsp.threeOPT, twoopttour))
rows.append(
[v_, greedytourlen,
twooptlen,
threeoptlen1, time1,
threeoptlen2, time2]
)
testResultDataFrame(rows)
def test4(n=30, tsp_object=None):
"""
Test concurrently over all possible
starting node using multiprocessing module
"""
if isinstance(tsp_object, TSP):
tsp = tsp_object
else:
v, e, M = createRandomCompleteGraph(n)
tsp = TSP(v, e)
rows = []
from multiprocessing import Pool
from itertools import product
p = Pool(8)
greedysol = p.map(tsp.greedyTour, tsp.nodes)
twooptsol = p.map(tsp.twoOPT, [sol[0] for sol in greedysol])
threeoptsol1 = p.map(callAndTime, [(tsp.threeOPT, sol[0]) for sol in greedysol])
threeoptsol2 = p.map(callAndTime, [(tsp.threeOPT, sol[0]) for sol in twooptsol])
for ind in range(n):
rows.append(
[tsp.nodes[ind],
greedysol[ind][1],
twooptsol[ind][1],
threeoptsol1[ind][0][1], threeoptsol1[ind][1],
threeoptsol2[ind][0][1], threeoptsol2[ind][1]]
)
testResultDataFrame(rows)
def test6(n=50):
v, e, M = createRandomCompleteGraph(n)
tsp = TSP(v, e)
#ret, time = callAndTime((test3, n, tsp))
#print(ret, time)
ret, time = callAndTime((test4, n, tsp))
print(ret, time)
if __name__ == '__main__':
test2()