-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeneticTspSolver.cpp
295 lines (279 loc) · 7.92 KB
/
GeneticTspSolver.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
/*
* GeneticTspSolver.cpp
*WRITTEN BY MOHAMMAD ASADOLAHI
*https://github.com/mohammadAsadolahi
*/
#include "Graph.cpp"
class Gene
{
public:
vector<int >route;
int cost;
Gene (vector<int >route ,int cost)
{
this->route = route;
this->cost = cost;
}
bool operator== (Gene const &gene)
{
return this->route == gene.route;
}
bool operator< (const Gene &other) const
{
return cost < other.cost;
}
};
class GeneticTspSolver
{
public:
Graph *graph; // the graph
vector<Gene >population;
vector<Gene >newGeneration;
int populationSize; // size of population
int generations; // amount of generations
int mutationRate; // mutation rate
// public:
GeneticTspSolver (Graph *graph ,int populationSize ,int generations ,
int mutationRate)
{
this->populationSize = populationSize;
this->generations = generations;
this->mutationRate = mutationRate;
this->graph = graph;
initialPopulation( );
}
int getCost (vector<int >&path)
{
int cost = 0;
for (int index = 1 ;index < graph->vertexCount ;index++)
{
int node = path[(index - 1)];
int nextNode = path[index];
if (graph->edges.find(make_pair(node ,nextNode))
== graph->edges.end( ))
{
return -1;
}
cost +=
(graph->edges.find(make_pair(node ,nextNode)))->second;
}
return cost;
}
bool isPathAlreadyExists (Gene path)
{
for (auto i = population.begin( ) ;i != population.end( ) ;i++)
{
if (path == (*i))
return true;
}
return false;
}
void initialPopulation ( )
{
srand((unsigned) time(0));
do
{
vector<int >route;
for (;route.size( ) < (graph->vertexCount) ;)
{
int random =
(rand( ) % this->graph->vertexCount);
vector<int >::iterator it = std::find(
route.begin( ) ,route.end( ) ,random);
if (it == route.end( ))
route.push_back(random);
}
int cost = getCost(route);
Gene newGene(route ,cost);
if ((!(isPathAlreadyExists(newGene))) && (cost != -1))
population.push_back(newGene);
} while (population.size( ) <= populationSize);
}
void sortPopulation ( )
{
std::sort(population.begin( ) ,population.end( ) ,
[ ] (const Gene &g1 ,const Gene &g2)
{
return g1.cost < g2.cost;
});
}
void printPopulation ( )
{
for (auto i = population.begin( ) ;i != population.end( ) ;i++)
{
for (auto pathIndex = i->route.begin( ) ;
pathIndex != i->route.end( ) ;pathIndex++)
{
cout << (*pathIndex) << " -> ";
}
cout << " with cost of: " << i->cost << endl;
}
}
void printGene (int index)
{
for (auto pathIndex = population[index].route.begin( ) ;
pathIndex != population[index].route.end( ) ;
pathIndex++)
{
cout << (*pathIndex) << " -> ";
}
cout << " with cost of: " << population[index].cost << endl;
}
void printGene (Gene gene)
{
for (auto pathIndex = gene.route.begin( ) ;
pathIndex != gene.route.end( ) ;pathIndex++)
{
cout << (*pathIndex) << " -> ";
}
cout << " with cost of: " << gene.cost << endl;
}
void crossOver (Gene &parent1 ,Gene &parent2 ,int crossOverIndex)
{
Gene tempChild = parent1;
for (int index = 0 ;index < crossOverIndex ;index++)
{
parent1.route[index] = parent2.route[index];
}
for (int index = 0 ;crossOverIndex < parent1.route.size( ) ;
crossOverIndex++ ,index++)
{
parent2.route[index] = tempChild.route[index];
}
int stats[parent1.route.size( )];// create an array to keep the statistics of routes
// trying to repair the first gene after crossover
for (int index = 0 ;index < parent1.route.size( ) ;index++)
{
auto count3 = count(parent1.route.begin( ) ,
parent1.route.end( ) ,index);
stats[index] = count3;
}
for (int index = 0 ;index < parent1.route.size( ) ;index++)
{
for (;stats[index] > 1 ;)
{
for (int i = 0 ;i < parent1.route.size( ) ;i++)
if (stats[i] == 0)
{
auto count3 = find(
parent1.route.begin( ) ,
parent1.route.end( ) ,
index);
(*count3) = i;
--stats[index];
stats[i] = 1;
break;
}
}
}
parent1.cost = getCost(parent1.route);
// then we repair second gene
for (int index = 0 ;index < parent2.route.size( ) ;index++)
{
auto count3 = count(parent2.route.begin( ) ,
parent2.route.end( ) ,index);
stats[index] = count3;
}
for (int index = 0 ;index < parent2.route.size( ) ;index++)
{
for (;stats[index] > 1 ;)
{
for (int i = 0 ;i < parent2.route.size( ) ;i++)
if (stats[i] == 0)
{
auto count3 = find(
parent2.route.begin( ) ,
parent2.route.end( ) ,
index);
(*count3) = i;
--stats[index];
stats[i] = 1;
break;
}
}
}
parent2.cost = getCost(parent2.route);
}
void applyMutation (int percent)
{
for (int i = 0 ;i < ((mutationRate * populationSize) / 100) ;
i++)
{
int mutationIndex = (rand( ) % populationSize);
do
{
mutationIndex = (rand( ) % populationSize);
} while (mutationIndex
<= (10 * populationSize / 100));
do
{
int position1;
int position2;
do
{
position1 = (rand( )
% graph->vertexCount);
position2 = (rand( )
% graph->vertexCount);
} while (position1 == position2);
int tempNumber =
population[mutationIndex].route[position1];
population[mutationIndex].route[position1] =
population[mutationIndex].route[position2];
population[mutationIndex].route[position2] =
tempNumber;
population[mutationIndex].cost = getCost(
population[mutationIndex].route);
} while (population[mutationIndex].cost == -1);
}
}
void applyMutation (Gene &gene)
{
do
{
int position1;
int position2;
do
{
position1 = (rand( ) % graph->vertexCount);
position2 = (rand( ) % graph->vertexCount);
} while (position1 == position2);
int tempNumber = gene.route[position1];
gene.route[position1] = gene.route[position2];
gene.route[position2] = tempNumber;
gene.cost = getCost(gene.route);
} while (gene.cost == -1);
}
void solve ( )
{
for (int generationIndex = 1 ;generationIndex <= generations ;
generationIndex++)
{
printPopulation( );
cout
<< "*******************************************************************\n";
sort(population.begin( ) ,population.end( ));
cout << "generation: " << generationIndex
<< " best of generation: ";
printGene(population[0]);
cout
<< "----------------------------------------------------\n";
for (int index = (10 * populationSize / 100) ;
index < populationSize ;)
{
crossOver(population[index] ,
population[(index + 1)] ,
population[index].route.size( ) / 2);
if (population[index].cost == -1)
applyMutation(population[index]);
if (population[index + 1].cost == -1)
applyMutation(population[index + 1]);
index = index + 2;
}
applyMutation(mutationRate);
}
cout << "\n the best solution is:";
printGene(population[0]);
}
};