-
Notifications
You must be signed in to change notification settings - Fork 0
/
TourTest.java
340 lines (294 loc) · 10.5 KB
/
TourTest.java
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
/**
* The test class TourTest.
*
* @author gcschmit
* @version 03jun2020
*/
public class TourTest
{
/**
* Default constructor for test class TourTest
*/
public TourTest()
{
}
/**
* Sets up the test fixture.
*
* Called before every test case method.
*/
@BeforeEach
public void setUp()
{
}
/**
* Tears down the test fixture.
*
* Called after every test case method.
*/
@AfterEach
public void tearDown()
{
}
// !!! how best to test that distance is updated propertly?
@Test
public void testDefaultConstructor()
{
Tour tour = new Tour();
int[] cityIndices = tour.getCityIndices();
assertEquals(BigTenData.getCities().length, cityIndices.length);
for(int i = 0; i < cityIndices.length; i++)
{
assertEquals(0, cityIndices[i]);
}
}
@Test
public void testCopyTour()
{
Tour tour1 = new Tour();
int[] cityIndices = tour1.getCityIndices();
int[] originalCityIndices = new int[cityIndices.length];
for(int i = 0; i < cityIndices.length; i++)
{
cityIndices[i] = i;
originalCityIndices[i] = cityIndices[i];
}
tour1.updateDistance();
double originalDistance = tour1.getDistance();
Tour tour2 = Tour.copyTour(tour1);
assertArrayEquals(tour1.getCityIndices(), tour2.getCityIndices(),
"the new tour has different city indices than the specified tour");
assertNotSame(tour1.getCityIndices(), tour2.getCityIndices(),
"the new tour refers to the same array as the specified tour; was the reference copied?");
assertEquals(tour1.getDistance(), tour2.getDistance());
assertArrayEquals(originalCityIndices, tour1.getCityIndices(),
"the copyTour method changed the specified Tour object");
assertEquals(originalDistance, tour1.getDistance(),
"the copyTour method changed the specified Tour object");
}
@Test
public void testSwapRandTwo()
{
int randCount = 0;
Tour tour = new Tour();
int[] cityIndices = tour.getCityIndices();
for(int i = 0; i < cityIndices.length; i++)
{
cityIndices[i] = i;
}
tour.updateDistance();
for(int i = 0; i < 1000; i++)
{
Tour prevTour = Tour.copyTour(tour);
int[] prevCityIndices = prevTour.getCityIndices();
tour.swapRandTwo();
assertEquals(0, cityIndices[0], "the element at index 0 should never be swapped");
int index1 = 0, index2 = 0;
for(int j = 0; j < cityIndices.length; j++)
{
if(cityIndices[j] != prevCityIndices[j])
{
if(index1 == 0)
{
index1 = j;
randCount++;
}
else if(index2 == 0)
{
index2 = j;
}
else
{
fail("more than 2 elements were swapped");
}
}
}
assertEquals(prevCityIndices[index1], cityIndices[index2]);
assertEquals(prevCityIndices[index2], cityIndices[index1]);
}
assertTrue(randCount >= 800, "elements should be swapped a majority of the time");
}
@Test
public void testPopulateWithCities()
{
double prevDistance = 0;
int dupTourCount = 0;
for(int i = 0; i < 1000; i++)
{
Tour tour = new Tour();
tour.populateWithCities();
int[] cityIndices = tour.getCityIndices();
for(int j = 0; j < BigTenData.getCities().length; j++)
{
assertEquals(1, Util.count(cityIndices, j, 0, BigTenData.getCities().length));
}
assertEquals(0, cityIndices[0], "the first element must always be 0");
assertNotEquals(0.0, tour.getDistance());
if(tour.getDistance() == prevDistance)
{
dupTourCount++;
}
prevDistance = tour.getDistance();
}
assertTrue(dupTourCount < 5, "consecutive random tours should be very rare");
}
@Test
public void testUpdateDistance()
{
Tour tour = new Tour();
int[] cityIndices = tour.getCityIndices();
for(int i = 0; i < cityIndices.length; i++)
{
cityIndices[i] = i;
}
tour.updateDistance();
double dist1 = tour.getDistance();
assertEquals(5105, dist1, 5);
tour.updateDistance();
double dist2 = tour.getDistance();
assertEquals(dist1, dist2, 1, "consecutive calls to updateDistance are not equal");
int[] cityIndices2 = { 0, 1, 3, 5, 7, 9, 11, 13, 2, 4, 6, 8, 10, 12 };
for(int i = 0; i < cityIndices.length; i++)
{
cityIndices[i] = cityIndices2[i];
}
tour.updateDistance();
dist1 = tour.getDistance();
assertEquals(6751, dist1, 5);
tour.updateDistance();
dist2 = tour.getDistance();
assertEquals(dist1, dist2, 1, "consecutive calls to updateDistance are not equal");
}
@Test
public void testMutate()
{
int mutateCount = 0;
Tour tour = new Tour();
tour.populateWithCities();
for(int i = 0; i < 1000; i++)
{
Tour prevTour = Tour.copyTour(tour);
tour.mutate();
if(! Arrays.equals(prevTour.getCityIndices(), tour.getCityIndices()))
{
mutateCount++;
double prevDistance = tour.getDistance();
tour.updateDistance();
assertEquals(prevDistance, tour.getDistance(), 0.01);
}
}
assertEquals(TravelingStudent.mutationProbability, mutateCount/1000.0, 0.05,
"the measured probability of mutation differs from the specified probability");
}
@Test
public void testCrossOver()
{
Tour parentA;
Tour parentB;
do
{
parentA = new Tour();
parentA.populateWithCities();
parentB = new Tour();
parentB.populateWithCities();
}
while(Arrays.equals(parentA.getCityIndices(), parentB.getCityIndices()));
int[][] testIndexPairs = new int[][] { {1, 1}, {11, 11}, {7, 7} };
for(int[] indexPair : testIndexPairs)
{
Tour child = parentA.crossOver(parentB, indexPair[0], indexPair[1]);
int[] cityIndices = child.getCityIndices();
for(int j = 0; j < BigTenData.getCities().length; j++)
{
assertEquals(1, Util.count(cityIndices, j, 0, BigTenData.getCities().length),
"value " + j + " missing in child");
}
assertEquals(0, cityIndices[0], "the first element must always be 0");
assertArrayEquals(cityIndices, parentA.getCityIndices(),
"child expected to match parentA");
assertNotEquals(0, child.getDistance(), "the child's distance must not be zero");
}
testIndexPairs = new int[][] { {1, 13} };
for(int[] indexPair : testIndexPairs)
{
Tour child = parentA.crossOver(parentB, indexPair[0], indexPair[1]);
int[] cityIndices = child.getCityIndices();
for(int j = 0; j < BigTenData.getCities().length; j++)
{
assertEquals(1, Util.count(cityIndices, j, 0, BigTenData.getCities().length),
"value " + j + " missing in child");
}
assertEquals(0, cityIndices[0], "the first element must always be 0");
assertArrayEquals(cityIndices, parentB.getCityIndices(),
"child expected to match parentB");
}
for(int x1 = 1; x1 < BigTenData.getCities().length - 2; x1++)
{
for(int x2 = x1; x2 < BigTenData.getCities().length; x2++)
{
Tour child = parentA.crossOver(parentB, x1, x2);
int[] cityIndices = child.getCityIndices();
for(int j = 0; j < BigTenData.getCities().length; j++)
{
assertEquals(1, Util.count(cityIndices, j, 0, BigTenData.getCities().length),
"value " + j + " missing in child");
}
assertEquals(0, cityIndices[0], "the first element must always be 0");
}
}
}
@Test
public void testGetCities()
{
Tour tour = new Tour();
int[] cityIndices = tour.getCityIndices();
for(int i = 0; i < cityIndices.length; i++)
{
cityIndices[i] = cityIndices.length - i - 1;
}
tour.updateDistance();
City[] expectedCities = BigTenData.getCities();
City[] actualCities = tour.getCities();
for(int i = 0; i < expectedCities.length; i++)
{
assertEquals(expectedCities[i], actualCities[expectedCities.length - i - 1],
"the city at index " + i + " should be " + expectedCities[i]);
}
int[] randomTour = { 0, 1, 10, 13, 6, 8, 2, 7, 4, 11, 9, 5, 3, 12 };
for(int i = 0; i < cityIndices.length; i++)
{
cityIndices[i] = randomTour[i];
}
tour.updateDistance();
actualCities = tour.getCities();
for(int i = 0; i < expectedCities.length; i++)
{
assertEquals(expectedCities[randomTour[i]], actualCities[i],
"the city at index " + i + " should be " + expectedCities[randomTour[i]]);
}
}
@Test
public void testCompareTo()
{
Tour[] tours = new Tour[1000];
for(int i = 0; i < tours.length; i++)
{
tours[i] = new Tour();
tours[i].populateWithCities();
}
Arrays.sort(tours);
double prevDistance = 0;
for(int i = 0; i < tours.length; i++)
{
assertTrue(tours[i].getDistance() >= prevDistance, "tours incorrectly compared");
prevDistance = tours[i].getDistance();
}
Tour equalTour = Tour.copyTour(tours[0]);
assertEquals(0, equalTour.compareTo(tours[0]), "compared tours should be equal");
}
}