-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTour.cs
executable file
·331 lines (324 loc) · 9.31 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
using System;
using System.Collections.Generic;
namespace GeneticAlgorithm4TSP
{
class Tour
{
private static Random r = new Random();
public double fitness { get; set; }
public double probability { get; set; }
public double distance { get; set; }
public int[] tour { get; set; }
public Tour()
{
tour = new int[GA.numCities];
for (int i = 0; i < tour.Length; i++)
{
tour[i] = -1;
}
}
public void generateIndividual()
{
fitness = 0;
probability = 0;
distance = 0;
for (int i = 0; i < Map.Cities.Length; i++)
{
tour[i] = i;
}
int n = Map.Cities.Length;
while (n > 1)
{
n--;
int k = r.Next(n + 1);
int v = tour[k];
tour[k] = tour[n];
tour[n] = v;
}
}
public void SwapMutate()
{
int i = r.Next(0, tour.Length);
int j = r.Next(0, tour.Length);
int v = tour[i];
tour[i] = tour[j];
tour[j] = v;
fitness = 0;
probability = 0;
distance = 0;
}
public void InversionMutate()
{
int i, j;
do
{
i = r.Next(0, tour.Length);
j = r.Next(0, tour.Length);
} while (i == j);
if (i > j)
{
int temp = i;
i = j;
j = temp;
}
for (int k = i; k < j; k++, j--)
{
int temp = tour[i];
tour[i] = tour[j];
tour[j] = temp;
}
fitness = 0;
probability = 0;
distance = 0;
}
public void ScrambleMutate()
{
int i = r.Next(0, tour.Length);
int j = r.Next(0, tour.Length);
do
{
i = r.Next(0, tour.Length);
j = r.Next(0, tour.Length);
} while (i == j);
if (i > j)
{
int temp = i;
i = j;
j = temp;
}
int n = j - i + 1;
while (n > 1)
{
n--;
int k = r.Next(n + 1);
int v = tour[k + i];
tour[k + i] = tour[n + i];
tour[n + i] = v;
}
fitness = 0;
probability = 0;
distance = 0;
}
public bool hasCity(int key)
{
for (int i = 0; i < tour.Length; i++)
{
if (tour[i] == key)
{
return true;
}
}
return false;
}
public double getFitness()
{
if (GA.reverse)
{
return (double)getDistance();
}
if (fitness == 0)
{
fitness = 1 / (double)getDistance();
}
return fitness;
}
public double getDistance()
{
if (distance == 0)
{
double tourDistance = 0;
for (int i = 0; i < Map.Cities.Length; i++)
{
City fromCity = Map.Cities[tour[i]];
City destinationCity;
if (i + 1 < Map.Cities.Length)
{
destinationCity = Map.Cities[tour[i + 1]];
}
else
{
destinationCity = Map.Cities[tour[0]];
}
tourDistance += fromCity.distance(destinationCity);
}
distance = tourDistance;
}
return distance;
}
public Tour PMX_Crossover(Tour parent2)
{
Tour child = new Tour();
int r1, r2;
do
{
r1 = r.Next(GA.numCities);
r2 = r.Next(GA.numCities);
} while (r1 == r2);
if (r1 > r2)
{
int t = r1;
r1 = r2;
r2 = t;
}
for (int i = r1; i <= r2; i++)
{
child.tour[i] = this.tour[i];
}
for (int i = r1; i <= r2; i++)
{
bool found = false;
for (int j = 0; j < GA.numCities && !found; j++)
{
found = child.tour[j] == parent2.tour[i];
}
if (!found)
{
bool replaced = false;
int value = this.tour[i];
while (!replaced)
{
int k = 0;
while (parent2.tour[k] != value)
{
k++;
}
if (k <= r2 && k >= r1)
{
value = this.tour[k];
}
else
{
child.tour[k] = parent2.tour[i];
replaced = true;
}
}
}
}
for (int i = 0; i < GA.numCities; i++)
{
bool found = false;
for (int j = 0; j < GA.numCities && !found; j++)
{
found = child.tour[j] == parent2.tour[i];
}
if (!found)
{
child.tour[i] = parent2.tour[i];
}
}
return child;
}
public Tour CYCLE_Crossover(Tour parent2)
{
Tour child = new Tour();
List<List<int>> all = new List<List<int>>();
for (int i = 0; i < GA.numCities; i++)
{
if (!Has(all, i))
{
List<int> cycle = new List<int>();
bool cyclefound = false;
int Sindex = i;
while (!cyclefound)
{
bool found = false;
int j = -1;
while (!found)
{
found = this.tour[++j] == parent2.tour[Sindex];
}
if (j == i)
{
cycle.Add(j);
cyclefound = true;
}
else
{
cycle.Add(j);
Sindex = j;
}
}
all.Add(cycle);
}
}
bool flag = false;
foreach (List<int> i in all)
{
if (!flag)
{
foreach (int j in i)
{
child.tour[j] = this.tour[j];
}
flag = true;
}
else
{
foreach (int j in i)
{
child.tour[j] = this.tour[j];
}
flag = false;
}
}
return child;
}
public Tour ORDER1_Crossover(Tour parent2)
{
Tour child = new Tour();
int startPos = r.Next(this.tour.Length);
int endPos = r.Next(this.tour.Length);
for (int i = 0; i < child.tour.Length; i++)
{
if (startPos < endPos && i > startPos && i < endPos)
{
child.tour[i] = this.tour[i];
}
else if (startPos > endPos)
{
if (!(i < startPos && i > endPos))
{
child.tour[i] = this.tour[i];
}
}
}
for (int i = 0; i < parent2.tour.Length; i++)
{
if (!child.hasCity(parent2.tour[i]))
{
bool flag = false;
for (int ii = 0; ii < child.tour.Length && !flag; ii++)
{
if (child.tour[ii] == -1)
{
child.tour[ii] = parent2.tour[i];
flag = true;
}
}
}
}
return child;
}
private bool Has(List<List<int>> all, int a)
{
foreach (List<int> i in all)
{
if (i.Contains(a))
{
return true;
}
}
return false;
}
override
public string ToString()
{
string s = "";
for (int i = 0; i < tour.Length; i++)
{
s += tour[i] + "|";
}
return s;
}
}
}