-
Notifications
You must be signed in to change notification settings - Fork 0
/
TSPState_test.py
305 lines (251 loc) · 11.8 KB
/
TSPState_test.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
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
import unittest
import random
from heapq import *
from TSPClasses import *
from TSPState import *
from Proj5GUI import *
from copy import deepcopy
def onlyContains(value, list):
contains = True
for item in list:
if item != value:
contains = False
return contains
def colOnlyContains(value, matrix, col):
contains = True
for row in matrix:
if row[col] != value:
contains = False
return contains
def contains(value, list):
contains = False
for item in list:
if item == value:
contains = True
return contains
def colContains(value, matrix, col):
contains = False
for row in matrix:
if row[col] == value:
contains = True
# for col_i in range(len(row)):
# if col_i == col:
return contains
class TestTSPState(unittest.TestCase):
def setUp(self):
app = QApplication(sys.argv)
proj5Gui = Proj5GUI("test")
SCALE = 1.0
proj5Gui.curSeed = QLineEdit("10")
proj5Gui.data_range = { 'x':[-1.5*SCALE,1.5*SCALE], \
'y':[-SCALE,SCALE] }
proj5Gui.size = QLineEdit("10")
self.testScenario = Scenario(proj5Gui.newPoints(), "Hard (Deterministic)", 20)
self.states = []
for i in range(10):
state = TSPState(np.zeros(random.randint(1, 20)), self.testScenario.getCities(), random.randint(1,20))
if i % 2 == 0:
self.states.append(deepcopy(state))
self.states.append(state)
self.testState = TSPState(self.testScenario.getCities(), self.testScenario.getCities(), 0)
self.testState.initMatrix()
print(self.testState.costMatrix)
self.oldMatrix = deepcopy(self.testState.costMatrix)
def test_init(self):
state = TSPState(self.testScenario.getCities(), self.testScenario.getCities(), 0)
self.assertEqual(state.path, self.testScenario.getCities())
self.assertEqual(state.cities, self.testScenario.getCities())
state.initMatrix()
row_index = 0
col_index = 0
for fromCity in self.testScenario.getCities():
col_index = 0
for toCity in self.testScenario.getCities():
self.assertEqual(state.costMatrix[row_index][col_index], fromCity.costTo(toCity))
col_index += 1
row_index += 1
def test_find_row_min(self):
min, minIndex = self.testState.findRowMin(2)
self.assertEqual(min, 246)
self.assertEqual(minIndex, 9)
def test_reduce_row(self):
oldRow = deepcopy(self.testState.costMatrix[2])
print(self.testState.costMatrix[2])
self.testState.reduceRow(2, 246, 9)
print(self.testState.costMatrix[2])
for col_index in range(len(self.testState.costMatrix[2])):
# if col_index != 9:
self.assertEqual(self.testState.costMatrix[2][col_index], oldRow[col_index] - 246)
# else:
# self.assertEqual(self.testState.costMatrix[2][col_index], oldRow[col_index])
def test_reduce_0_row(self):
oldRow = deepcopy(self.testState.costMatrix[0])
print(self.testState.costMatrix[0])
self.testState.reduceRow(0, 0, 4)
print(self.testState.costMatrix[0])
for col_index in range(len(self.testState.costMatrix[0])):
self.assertEqual(self.testState.costMatrix[0][col_index], oldRow[col_index])
def test_reduce_matrix_rows(self):
oldMatrix = deepcopy(self.testState.costMatrix)
rowMins = []
colMins = []
newBest = 0
for row in range(len(self.testState.costMatrix)):
min, minIndex = self.testState.findRowMin(row)
minVal = {'min': min,
'minIndex': minIndex}
rowMins.append(minVal)
newBest += min
self.testState.reduceMatrixRows()
for row_index in range(len(self.testState.costMatrix)):
for col_index in range(len(self.testState.costMatrix[row_index])):
# if col_index != rowMins[row_index]['minIndex']:
self.assertEqual(self.testState.costMatrix[row_index][col_index],
oldMatrix[row_index][col_index] - rowMins[row_index]['min'])
self.assertEqual(newBest, self.testState.bestCost)
def test_reduce_matrix_cols(self):
oldMatrix = deepcopy(self.testState.costMatrix)
colMins = []
newBest = 0
for col in range(len(self.testState.costMatrix[0])):
min, minIndex = self.testState.findColMin(col)
minVal = {'min': min,
'minIndex': minIndex}
colMins.append(minVal)
newBest += min
print(colMins)
self.testState.reduceMatrixCols()
for col_index in range(len(self.testState.costMatrix[0])):
for row_index in range(len(self.testState.costMatrix)):
# if row_index != colMins[col_index]['minIndex']:
self.assertEqual(self.testState.costMatrix[row_index][col_index],
oldMatrix[row_index][col_index] - colMins[col_index]['min'],
"\nnew value " + str(self.testState.costMatrix[row_index][col_index]) +
"\nold value " + str(oldMatrix[row_index][col_index]) +
"\nmin " + str(colMins[col_index]['min']) +
"\nmin_index " + str(colMins[col_index]['minIndex']) +
"\ncol_index " + str(col_index))
self.assertEqual(newBest, self.testState.bestCost)
def test_reduce_to_0(self):
min, minIndex = self.testState.findRowMin(2)
print("min is " + str(min) + " at index " + str(minIndex))
self.testState.reduceMatrixRows()
row_i = 0
for row in self.testState.costMatrix:
self.assertTrue(contains(0, row), "at index " + str(row_i) + " row is " + str(row) +
"\nold row is " + str(self.oldMatrix[row_i]))
row_i += 1
def test_col_0_min(self):
min, minIndex = self.testState.findColMin(0)
self.assertEqual(min, 593)
self.assertEqual(minIndex, 3)
pass
def test_reduce_col_0(self):
oldMatrix = deepcopy(self.testState.costMatrix)
self.testState.reduceCol(0, 593, 3)
colContains(0, self.testState.costMatrix, 3)
for row_index in range(len(self.testState.costMatrix)):
# if row_index is not 3:
self.assertEqual(self.testState.costMatrix[row_index][0] + 593,
oldMatrix[row_index][0])
def test_heap(self):
heap = []
for state in self.states:
heappush(heap, state)
for h in heap:
print(str(h.path) + " " + str(h.bestCost))
while len(heap) > 1:
popV = heappop(heap)
topV = heap[0]
if len(popV.path) == len(topV.path):
if popV.bestCost != topV.bestCost:
self.assertTrue(popV.bestCost < topV.bestCost)
else:
self.assertTrue(len(popV.path) > len(topV.path))
def test_inf_row(self):
oldMatrix = deepcopy(self.testState.costMatrix)
self.testState.infRow(3)
for val in self.testState.costMatrix[3]:
self.assertEqual(val, np.inf)
for row_i in range(len(self.testState.costMatrix)):
if row_i is not 3:
for col_i in range(len(self.testState.costMatrix[row_i])):
self.assertEqual(self.testState.costMatrix[row_i][col_i], oldMatrix[row_i][col_i])
self.assertEqual(self.testState.coveredRows[0], 3)
def test_inf_col(self):
oldMatrix = deepcopy(self.testState.costMatrix)
self.testState.infCol(3)
for row in self.testState.costMatrix:
self.assertEqual(row[3], np.inf)
for row_i in range(len(self.testState.costMatrix)):
for col_i in range(len(self.testState.costMatrix[row_i])):
if col_i is not 3:
self.assertEqual(self.testState.costMatrix[row_i][col_i], oldMatrix[row_i][col_i])
self.assertEqual(self.testState.coveredCols[0], 3)
def test_inf_pair(self):
self.testState.infPair(4, 2)
print(self.testState)
for row_i in range(len(self.testState.costMatrix)):
for col_i in range(len(self.testState.costMatrix[row_i])):
if row_i is 2 and col_i is 4:
self.assertEqual(self.testState.costMatrix[row_i][col_i], np.inf)
else:
self.assertEqual(self.testState.costMatrix[row_i][col_i], self.oldMatrix[row_i][col_i])
def test_cover_cities(self):
self.testState.coverCities(4, 5)
for row_i in range(len(self.testState.costMatrix)):
if row_i is not 4:
for col_i in range(len(self.testState.costMatrix[row_i])):
if col_i is not 5:
if row_i is 5 and col_i is 4:
self.assertEqual(self.testState.costMatrix[row_i][col_i], np.inf)
else:
self.assertEqual(self.testState.costMatrix[row_i][col_i], self.oldMatrix[row_i][col_i])
def test_cover_and_reduce(self):
city1 = 2
city2 = 7
self.testState.reduceMatrix()
for row in self.testState.costMatrix:
self.assertTrue(contains(0, row))
for col_i in range(len(self.testState.costMatrix[0])):
self.assertTrue(colContains(0, self.testState.costMatrix, col_i))
print(self.testState)
self.assertLess(self.testState.bestCost, np.inf)
self.testState.coverCities(city1,city2)
print(self.testState)
self.assertIn(city1, self.testState.coveredRows)
self.assertIn(city2, self.testState.coveredCols)
self.assertEqual(self.testState.costMatrix[city2][city1], np.inf)
self.testState.reduceMatrix()
for row_i in range(len(self.testState.costMatrix)):
if not contains(row_i, self.testState.coveredRows):
self.assertTrue(contains(0, self.testState.costMatrix[row_i]))
else:
self.assertTrue(onlyContains(np.inf, self.testState.costMatrix[row_i]))
for col_i in range(len(self.testState.costMatrix[0])):
if not contains(col_i, self.testState.coveredCols):
self.assertTrue(colContains(0, self.testState.costMatrix, col_i))
else:
self.assertTrue(colOnlyContains(np.inf, self.testState.costMatrix, col_i))
print(self.testState)
self.assertLess(self.testState.bestCost, np.inf)
# try it again
self.testState.coverCities(city1 + 1, city2 - 1)
self.assertIn(city1, self.testState.coveredRows)
self.assertIn(city1 + 1, self.testState.coveredRows)
self.assertIn(city2, self.testState.coveredCols)
self.assertIn(city2 - 1, self.testState.coveredCols)
self.assertEqual(self.testState.costMatrix[city2 - 1][city1 + 1], np.inf)
self.testState.reduceMatrix()
for row_i in range(len(self.testState.costMatrix)):
if not contains(row_i, self.testState.coveredRows):
self.assertTrue(contains(0, self.testState.costMatrix[row_i]))
else:
self.assertTrue(onlyContains(np.inf, self.testState.costMatrix[row_i]))
for col_i in range(len(self.testState.costMatrix[0])):
if not contains(col_i, self.testState.coveredCols):
self.assertTrue(colContains(0, self.testState.costMatrix, col_i))
else:
self.assertTrue(colOnlyContains(np.inf, self.testState.costMatrix, col_i))
print(self.testState)
self.assertLess(self.testState.bestCost, np.inf)