-
Notifications
You must be signed in to change notification settings - Fork 2
/
Tour.cs
432 lines (394 loc) · 18 KB
/
Tour.cs
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//////////////////////////////////////////////////////////////////////////////////////////////////
// File Name: Tour.cs
// Permission to use, copy, modify, and distribute this Program and its documentation,
// if any, for any purpose and without fee is hereby granted, provided that:
// (i) you not charge any fee for the Program, and the Program not be incorporated
// by you in any software or code for which compensation is expected or received;
// (ii) the copyright notice listed above appears in all copies;
// (iii) both the copyright notice and this Agreement appear in all supporting documentation; and
// (iv) the name of Michael LaLena or lalena.com not be used in advertising or publicity
// pertaining to distribution of the Program without specific, written prior permission.
///////////////////////////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using System.Text;
namespace Tsp
{
/// <summary>
/// This class represents one instance of a tour through all the cities.
/// </summary>
public class Tour : List<Link>
{
/// <summary>
/// Constructor that takes a default capacity.
/// </summary>
/// <param name="capacity">Initial size of the tour. Should be the number of cities in the tour.</param>
public Tour(int capacity)
: base(capacity)
{
resetTour(capacity);
}
/// <summary>
/// Private copy of this fitness of this tour.
/// </summary>
private double fitness;
/// <summary>
/// The fitness (total tour length) of this tour.
/// </summary>
public double Fitness
{
set
{
fitness = value;
}
get
{
return fitness;
}
}
/// <summary>
/// Creates the tour with the correct number of cities and creates initial connections of all -1.
/// </summary>
/// <param name="numberOfCities"></param>
private void resetTour(int numberOfCities)
{
this.Clear();
Link link;
for (int i = 0; i < numberOfCities; i++)
{
link = new Link();
link.Connection1 = -1;
link.Connection2 = -1;
this.Add(link);
}
}
/// <summary>
/// Determine the fitness (total length) of an individual tour.
/// </summary>
/// <param name="cities">The cities in this tour. Used to get the distance between each city.</param>
public void DetermineFitness(Cities cities)
{
Fitness = 0;
int lastCity = 0;
int nextCity = this[0].Connection1;
foreach (Link link in this)
{
Fitness += cities[lastCity].Distances[nextCity];
// figure out if the next city in the list is [0] or [1]
if (lastCity != this[nextCity].Connection1)
{
lastCity = nextCity;
nextCity = this[nextCity].Connection1;
}
else
{
lastCity = nextCity;
nextCity = this[nextCity].Connection2;
}
}
}
/// <summary>
/// Creates a link between 2 cities in a tour, and then updates the city usage.
/// </summary>
/// <param name="tour">The incomplete child tour.</param>
/// <param name="cityUsage">Number of times each city has been used in this tour. Is updated when cities are joined.</param>
/// <param name="city1">The first city in the link.</param>
/// <param name="city2">The second city in the link.</param>
private static void joinCities(Tour tour, int[] cityUsage, int city1, int city2)
{
// Determine if the [0] or [1] link is available in the tour to make this link.
if (tour[city1].Connection1 == -1)
{
tour[city1].Connection1 = city2;
}
else
{
tour[city1].Connection2 = city2;
}
if (tour[city2].Connection1 == -1)
{
tour[city2].Connection1 = city1;
}
else
{
tour[city2].Connection2 = city1;
}
cityUsage[city1]++;
cityUsage[city2]++;
}
/// <summary>
/// Find a link from a given city in the parent tour that can be placed in the child tour.
/// If both links in the parent aren't valid links for the child tour, return -1.
/// </summary>
/// <param name="parent">The parent tour to get the link from.</param>
/// <param name="child">The child tour that the link will be placed in.</param>
/// <param name="cityList">The list of cities in this tour.</param>
/// <param name="cityUsage">Number of times each city has been used in the child.</param>
/// <param name="city">City that we want to link from.</param>
/// <returns>The city to link to in the child tour, or -1 if none are valid.</returns>
private static int findNextCity(Tour parent, Tour child, Cities cityList, int[] cityUsage, int city)
{
if (testConnectionValid(child, cityList, cityUsage, city, parent[city].Connection1))
{
return parent[city].Connection1;
}
else if (testConnectionValid(child, cityList, cityUsage, city, parent[city].Connection2))
{
return parent[city].Connection2;
}
return -1;
}
/// <summary>
/// Determine if it is OK to connect 2 cities given the existing connections in a child tour.
/// If the two cities can be connected already (witout doing a full tour) then it is an invalid link.
/// </summary>
/// <param name="tour">The incomplete child tour.</param>
/// <param name="cityList">The list of cities in this tour.</param>
/// <param name="cityUsage">Array that contains the number of times each city has been linked.</param>
/// <param name="city1">The first city in the link.</param>
/// <param name="city2">The second city in the link.</param>
/// <returns>True if the connection can be made.</returns>
private static bool testConnectionValid(Tour tour, Cities cityList, int[] cityUsage, int city1, int city2)
{
// Quick check to see if cities already connected or if either already has 2 links
if ((city1 == city2) || (cityUsage[city1] == 2) || (cityUsage[city2] == 2))
{
return false;
}
// A quick check to save CPU. If haven't been to either city, connection must be valid.
if ((cityUsage[city1] == 0) || (cityUsage[city2] == 0))
{
return true;
}
// Have to see if the cities are connected by going in each direction.
for (int direction = 0; direction < 2; direction++)
{
int lastCity = city1;
int currentCity;
if (direction == 0)
{
currentCity = tour[city1].Connection1; // on first pass, use the first connection
}
else
{
currentCity = tour[city1].Connection2; // on second pass, use the other connection
}
int tourLength = 0;
while ((currentCity != -1) && (currentCity != city2) && (tourLength < cityList.Count - 2))
{
tourLength++;
// figure out if the next city in the list is [0] or [1]
if (lastCity != tour[currentCity].Connection1)
{
lastCity = currentCity;
currentCity = tour[currentCity].Connection1;
}
else
{
lastCity = currentCity;
currentCity = tour[currentCity].Connection2;
}
}
// if cities are connected, but it goes through every city in the list, then OK to join.
if (tourLength >= cityList.Count - 2)
{
return true;
}
// if the cities are connected without going through all the cities, it is NOT OK to join.
if (currentCity == city2)
{
return false;
}
}
// if cities weren't connected going in either direction, we are OK to join them
return true;
}
/// <summary>
/// Perform the crossover operation on 2 parent tours to create a new child tour.
/// This function should be called twice to make the 2 children.
/// In the second call, the parent parameters should be swapped.
/// </summary>
/// <param name="parent1">The first parent tour.</param>
/// <param name="parent2">The second parent tour.</param>
/// <param name="cityList">The list of cities in this tour.</param>
/// <param name="rand">Random number generator. We pass around the same random number generator, so that results between runs are consistent.</param>
/// <returns>The child tour.</returns>
public static Tour Crossover(Tour parent1, Tour parent2, Cities cityList, Random rand)
{
Tour child = new Tour(cityList.Count); // the new tour we are making
int[] cityUsage = new int[cityList.Count]; // how many links 0-2 that connect to this city
int city; // for loop variable
int nextCity; // the other city in this link
for (city = 0; city < cityList.Count; city++)
{
cityUsage[city] = 0;
}
// Take all links that both parents agree on and put them in the child
for (city = 0; city < cityList.Count; city++)
{
if (cityUsage[city] < 2)
{
if (parent1[city].Connection1 == parent2[city].Connection1)
{
nextCity = parent1[city].Connection1;
if (testConnectionValid(child, cityList, cityUsage, city, nextCity))
{
joinCities(child, cityUsage, city, nextCity);
}
}
if (parent1[city].Connection2 == parent2[city].Connection2)
{
nextCity = parent1[city].Connection2;
if (testConnectionValid(child, cityList, cityUsage, city, nextCity))
{
joinCities(child, cityUsage, city, nextCity);
}
}
if (parent1[city].Connection1 == parent2[city].Connection2)
{
nextCity = parent1[city].Connection1;
if (testConnectionValid(child, cityList, cityUsage, city, nextCity))
{
joinCities(child, cityUsage, city, nextCity);
}
}
if (parent1[city].Connection2 == parent2[city].Connection1)
{
nextCity = parent1[city].Connection2;
if (testConnectionValid(child, cityList, cityUsage, city, nextCity))
{
joinCities(child, cityUsage, city, nextCity);
}
}
}
}
// The parents don't agree on whats left, so we will alternate between using
// links from parent 1 and then parent 2.
for (city = 0; city < cityList.Count; city++)
{
if (cityUsage[city] < 2)
{
if (city % 2 == 1) // we prefer to use parent 1 on odd cities
{
nextCity = findNextCity(parent1, child, cityList, cityUsage, city);
if (nextCity == -1) // but if thats not possible we still go with parent 2
{
nextCity = findNextCity(parent2, child, cityList, cityUsage, city); ;
}
}
else // use parent 2 instead
{
nextCity = findNextCity(parent2, child, cityList, cityUsage, city);
if (nextCity == -1)
{
nextCity = findNextCity(parent1, child, cityList, cityUsage, city);
}
}
if (nextCity != -1)
{
joinCities(child, cityUsage, city, nextCity);
// not done yet. must have been 0 in above case.
if (cityUsage[city] == 1)
{
if (city % 2 != 1) // use parent 1 on even cities
{
nextCity = findNextCity(parent1, child, cityList, cityUsage, city);
if (nextCity == -1) // use parent 2 instead
{
nextCity = findNextCity(parent2, child, cityList, cityUsage, city);
}
}
else // use parent 2
{
nextCity = findNextCity(parent2, child, cityList, cityUsage, city);
if (nextCity == -1)
{
nextCity = findNextCity(parent1, child, cityList, cityUsage, city);
}
}
if (nextCity != -1)
{
joinCities(child, cityUsage, city, nextCity);
}
}
}
}
}
// Remaining links must be completely random.
// Parent's links would cause multiple disconnected loops.
for (city = 0; city < cityList.Count; city++)
{
while (cityUsage[city] < 2)
{
do
{
nextCity = rand.Next(cityList.Count); // pick a random city, until we find one we can link to
} while (!testConnectionValid(child, cityList, cityUsage, city, nextCity));
joinCities(child, cityUsage, city, nextCity);
}
}
return child;
}
/// <summary>
/// Randomly change one of the links in this tour.
/// </summary>
/// <param name="rand">Random number generator. We pass around the same random number generator, so that results between runs are consistent.</param>
public void Mutate(Random rand)
{
int cityNumber = rand.Next(this.Count);
Link link = this[cityNumber];
int tmpCityNumber;
// Find which 2 cities connect to cityNumber, and then connect them directly
if (this[link.Connection1].Connection1 == cityNumber) // Conn 1 on Conn 1 link points back to us.
{
if (this[link.Connection2].Connection1 == cityNumber)// Conn 1 on Conn 2 link points back to us.
{
tmpCityNumber = link.Connection2;
this[link.Connection2].Connection1 =link.Connection1;
this[link.Connection1].Connection1 = tmpCityNumber;
}
else // Conn 2 on Conn 2 link points back to us.
{
tmpCityNumber = link.Connection2;
this[link.Connection2].Connection2 = link.Connection1;
this[link.Connection1].Connection1 = tmpCityNumber;
}
}
else // Conn 2 on Conn 1 link points back to us.
{
if (this[link.Connection2].Connection1 == cityNumber)// Conn 1 on Conn 2 link points back to us.
{
tmpCityNumber = link.Connection2;
this[link.Connection2].Connection1 = link.Connection1;
this[link.Connection1].Connection2 = tmpCityNumber;
}
else // Conn 2 on Conn 2 link points back to us.
{
tmpCityNumber = link.Connection2;
this[link.Connection2].Connection2 = link.Connection1;
this[link.Connection1].Connection2 = tmpCityNumber;
}
}
int replaceCityNumber = -1;
do
{
replaceCityNumber = rand.Next(this.Count);
}
while (replaceCityNumber == cityNumber);
Link replaceLink = this[replaceCityNumber];
// Now we have to reinsert that city back into the tour at a random location
tmpCityNumber = replaceLink.Connection2;
link.Connection2 = replaceLink.Connection2;
link.Connection1 = replaceCityNumber;
replaceLink.Connection2 = cityNumber;
if (this[tmpCityNumber].Connection1 == replaceCityNumber)
{
this[tmpCityNumber].Connection1 = cityNumber;
}
else
{
this[tmpCityNumber].Connection2 = cityNumber;
}
}
}
}