-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
316 lines (184 loc) · 5.13 KB
/
main.cpp
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
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>
const int NUM_CITIES = 5;
const int NUM_ITERATIONS = 100;
const float MUTATION_RATE = 0.1;
struct Solution
{
std::vector < int >path;
int fitness;
};
// Calculate the fitness of a solution (the total distance of the path)
int
calculateFitness (const Solution & s,
const std::vector < std::vector < int >>&distances)
{
int fitness = 0;
for (int i = 0; i < NUM_CITIES - 1; i++)
{
fitness += distances[s.path[i]][s.path[i + 1]];
}
fitness += distances[s.path[NUM_CITIES - 1]][s.path[0]];
return fitness;
}
// Generate a random initial solution
Solution generateInitialSolution (std::mt19937 & rng)
{
Solution s;
std::vector < int >cities (NUM_CITIES);
std::iota (cities.begin (), cities.end (), 0);
std::shuffle (cities.begin (), cities.end (), rng);
s.path = cities;
return s;
}
// Select two solutions from a population
void
selectSolutions (const std::vector < Solution > &population, Solution & s1,
Solution & s2)
{
std::uniform_int_distribution < int >dist (0, NUM_CITIES - 1);
int i1 = dist (rng);
int i2 = dist (rng);
while (i2 == i1)
{
i2 = dist (rng);
}
s1 = population[i1];
s2 = population[i2];
}
// Generate two offspring from two solutions using improved greedy crossover
void
improvedGreedyCrossover (const Solution & s1, const Solution & s2,
Solution & o1, Solution & o2,
const std::vector < std::vector < int >>&distances)
{
// Copy the paths from the parents
o1.path = s1.path;
o2.path = s2.path;
// Choose two random cities
std::uniform_int_distribution < int >dist (0, NUM_CITIES - 1);
int c1 = dist (rng);
int c2 = dist (rng);
while (c2 == c1)
{
c2 = dist (rng);
}
// Swap the cities in the offspring paths
std::swap (o1.path[c1], o1.path[c2]);
std::swap (o2.path[c1], o2.path[c2]);
// Evaluate the fitness of the offspring
o1.fitness = calculateFitness (o1, distances);
o2.fitness = calculateFitness (o2, distances);
// Use improved greedy crossover to improve the offspring solutions
for (int i = 0; i < NUM_CITIES; i++)
{
if (o1.path[i] == s1.path[c1])
{
int j = (i + 1) % NUM_CITIES;
while (o1.path[j] != s2.path[c2])
{
int k = (j + 1) % NUM_CITIES;
if (distances[o1.path[i]][o1.path[j]] +
distances[o1.path[j]][o1.path[k]] -
distances[o1.path[i]][o1.path[k]] < 0)
{
std::swap (o1.path[j], o1.path[k]);
j = k;
}
else
{
break;
}
}
}
if (o2.path[i] == s2.path[c2])
{
int j = (i + 1) % NUM_CITIES;
while (o2.path[j] != s1.path[c1])
{
int k = (j + 1) % NUM_CITIES;
if (distances[o2.path[i]][o2.path[j]] +
distances[o2.path[j]][o2.path[k]] -
distances[o2.path[i]][o2.path[k]] < 0)
{
std::swap (o2.path[j], o2.path[k]);
j = k;
}
else
{
break;
}
}
}
}
// Recalculate the fitness of the offspring
o1.fitness = calculateFitness (o1, distances);
o2.fitness = calculateFitness (o2, distances);
}
// Mutate a solution by swapping two random cities
void
mutate (Solution & s)
{
std::uniform_int_distribution < int >dist (0, NUM_CITIES - 1);
int c1 = dist (rng);
int c2 = dist (rng);
while (c2 == c1)
{
c2 = dist (rng);
}
std::swap (s.path[c1], s.path[c2]);
}
int
main ()
{
// Seed the random number generator
std::mt19937 rng (std::random_device
{
} ());
// Initialize the population
std::vector < Solution > population (NUM_CITIES);
for (int i = 0; i < NUM_CITIES; i++)
{
population[i] = generateInitialSolution (rng);
population[i].fitness = calculateFitness (population[i], distances);
}
// Run the IGC algorithm
for (int iteration = 0; iteration < NUM_ITERATIONS; iteration++)
{
Solution s1, s2, o1, o2;
selectSolutions (population, s1, s2);
improvedGreedyCrossover (s1, s2, o1, o2, distances);
if (o1.fitness < s1.fitness)
{
mutate (o1);
}
if (o2.fitness < s2.fitness)
{
mutate (o2);
}
// Replace the worst solutions in the population with the offspring
std::sort (population.begin (), population.end (),
[](const Solution & a, const Solution & b)
{
return a.fitness < b.fitness;
}
);
population[NUM_CITIES - 1] = o1;
population[NUM_CITIES - 2] = o2;
}
// Print the best solution
Solution best = population[0];
for (int i = 1; i < NUM_CITIES; i++)
{
if (population[i].fitness < best.fitness)
{
best = population[i];
}
}
std::
cout << "Best solution found after " << NUM_ITERATIONS << " iterations: "
<< best.fitness << std::endl;
return 0;
}