-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTSP_GA_Multithread.cpp
More file actions
257 lines (195 loc) · 6.68 KB
/
TSP_GA_Multithread.cpp
File metadata and controls
257 lines (195 loc) · 6.68 KB
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
#include <bits/stdc++.h>
using namespace std;
const int num_threads = 4;
vector< vector< vector<double> > > next_generation[num_threads];
static mutex mtx;
// Read data from input file. Each line is expected in the format "city_number x_coordinate y_coordinate"
vector< vector<double> > getData() {
vector< vector<double> > ans;
int name;
double x, y;
while (scanf("%d %lf %lf", &name, &x, &y) != EOF) {
vector<double> city(3);
city[0] = name;
city[1] = x;
city[2] = y;
ans.push_back(city);
}
return ans;
}
// print an individual path (all it's cities, in order)
void printIndividual(vector< vector<double> > A) {
for (int i = 0; i < (int)A.size(); i++) {
printf("%d ", (int)A[i][0]);
}
printf("\n");
}
// distance between 2 points in 2D plane (A[1] and A[2] are, respectively, the x and y coordinates)
double dist(vector<double> A, vector<double> B) {
return sqrt(pow((A[1] - B[1]), 2) + pow((A[2] - B[2]), 2));
}
// Calculate the length of an individual path
double pathLen(vector< vector<double> > A) {
double path = 0;
for (int i = 0; i < A.size()-1; i++) {
path += dist(A[i], A[i+1]);
}
return path;
}
// Comparison used to sort each population by the most adapted individuals (shortest paths)
struct compare {
inline bool operator() (vector< vector<double> > A, vector< vector<double> > B) {
double pathA = 0;
double pathB = 0;
for (int i = 0; i < A.size()-1; i++) {
pathA += dist(A[i], A[i+1]);
}
for (int i = 0; i < B.size()-1; i++) {
pathB += dist(B[i], B[i+1]);
}
return pathA < pathB;
}
};
// Sort the cities in random order to generate a random individual
vector< vector<double> > createRandomIndividual(vector< vector<double> > data) {
vector< vector<double> > ans;
ans.push_back(data[0]);
vector<int> index(data.size()-1);
for (int i = 1; i < data.size(); i++)
index[i-1] = i;
while (index.size() > 0) {
unsigned int seed = rand();
int j = rand_r(&seed) % index.size();
ans.push_back(data[index[j]]);
index.erase(index.begin() + j);
}
ans.push_back(data[0]);
return ans;
}
// Insert mutation in an individual (in a random position)
void mutate(vector< vector<double> >& A) {
if (A.size() == 0)
throw invalid_argument( "Mutate zero_leght individual" );
unsigned int seed = rand();
int erase = rand_r(&seed) % (A.size()-2) + 1;
vector<double> temp = A[erase];
A.erase(A.begin() + erase);
int insert;
do {
seed = rand();
insert = rand_r(&seed) % (A.size()-2) + 1;
} while (insert == erase);
A.insert(A.begin() + insert, temp);
}
// crossover between 2 parents producing 1 children
vector< vector<double> > crossover(vector< vector<double> > A, vector< vector<double> > B) {
vector< vector<double> > ans(A.size());
unsigned int seed = rand();
if (rand_r(&seed) % 2 < 1) swap(A, B);
set< vector<double> > contains;
set<int> ocupied;
ans[0] = A[0];
ans[A.size()-1] = A[A.size()-1];
contains.insert(A[A.size()-1]);
ocupied.insert(A.size()-1);
contains.insert(A[0]);
ocupied.insert(0);
seed = rand();
int start = rand_r(&seed) % A.size();
for (int i = 0; i <= A.size() / 2; i++) {
int index = (start + i) % A.size();
ans[index] = A[index];
contains.insert(A[index]);
ocupied.insert(index);
}
int j = 0;
for (int i = 0; i < A.size(); i++) {
if (ocupied.find(i) != ocupied.end())
continue;
while (j < B.size() && (contains.find(B[j]) != contains.end()))
j++;
if (j >= B.size()) break;
contains.insert(B[j]);
ocupied.insert(i);
ans[i] = B[j];
}
for (int i = 0; i < A.size(); i++)
if (ocupied.find(i) == ocupied.end())
ans[i] = A[i];
return ans;
}
// Mutual exclusive method to insert individuals in the critical region
void safe_insert(vector< vector<double> > individual, int next_gen_id, int population_size) {
lock_guard<mutex> lock(mtx);
if (next_generation[next_gen_id].size() >= population_size) return;
next_generation[next_gen_id].push_back(individual);
}
// Genetic Algorithm to solve the Traveling Salesman Problem
void TSP_GA(vector< vector<double> > data, int population_size, int best_individuals, int number_of_children, int number_of_migrations, int number_of_generations, double mutation_probability, int t_id) {
clock_t time = clock();
vector< vector<double> > ans;
// if (data.size() == 0) return;
// Create initial population
vector< vector< vector<double> > > population(population_size);
for (int i = 0; i < population_size; i++)
population[i] = createRandomIndividual(data);
// Termination criteria: number of generations
for (int generation = 0; generation <= number_of_generations; generation++) {
// Sort population by individuals fitness (shortest paths prevail)
sort(population.begin(), population.end(), compare());
ans = population[0];
// Migration: fitness based (best individuals migrate to other populations)
for (int i = 0, j = 0; i < number_of_migrations; i++, j++) {
if (j % num_threads == t_id) j++;
safe_insert(population[i], i % num_threads, population_size);
}
// Crossover between best individuals
for (int i = 0; i < best_individuals - 1; i++)
for (int j = 0; j < number_of_children; j++)
safe_insert(crossover(population[i], population[i+1]), t_id, population_size);
// complete the next generation with some random (lucky) individuals from previous generation
set<int> picked;
while (next_generation[t_id].size() < population_size) {
int lucky;
do {
unsigned int seed = rand();
lucky = rand_r(&seed) % population_size;
} while (picked.find(lucky) != picked.end());
picked.insert(lucky);
safe_insert(population[lucky], t_id, population_size);
}
// Mutation
for (int i = 0; i < next_generation[t_id].size(); i++) {
unsigned int seed = rand();
if (rand_r(&seed) % 100 < mutation_probability * 100)
mutate(next_generation[t_id][i]);
}
// update current generation
for (int i = 0; i < population_size; i++)
population[i] = next_generation[t_id][i];
next_generation[t_id].clear();
}
// print current population(thread) length, elapsed_time and path
time = clock() - time;
printf("%d | %lf | %lf | ", t_id, pathLen(ans), double(time)/CLOCKS_PER_SEC);
printIndividual(ans);
}
int main() {
vector< vector<double> > data = getData();
int population_size = 100;
int best_individuals = 20;
int number_of_children = 4;
int number_of_migrations = 4;
int number_of_generations = 30;
double mutation_probability = 0.1;
// Print the results
printf("\nThread_id | Lenght | Elapsed_time | Path\n");
thread t[num_threads];
for (int i = 0; i < num_threads; i++) {
t[i] = thread(TSP_GA, data, population_size, best_individuals, number_of_children, number_of_migrations, number_of_generations, mutation_probability, i);
}
for (int i = 0; i < num_threads; i++) {
t[i].join();
}
return 0;
}