-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
455 lines (389 loc) · 15 KB
/
main.cpp
File metadata and controls
455 lines (389 loc) · 15 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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#include <mpi.h> /* requirement for MPI */
#include <iostream>
#include <string>
#include <fstream>
#include <random>
#include <algorithm>
#include <cmath>
#include <vector>
#include <sstream>
#include <chrono>
#include "sequential/travelling_salesman_problem.hpp"
#include "island/island.hpp"
#include <cassert>
using namespace std;
// Global variables (parameters)
// --log_freq
int log_freq = 100;
// --epochs
int nr_epochs = 1000;
// --population
int nr_individuals = 128;
// island or naive or sequential
// 2 1 0
int mode = 0;
string data_dir = "data";
// --data
string data_file = "a280.csv";
// --log_dir
string log_dir = "logs/";
// --elite_size
int elite_size = 65;
// --mutation
int mutation = 10;
// --migration_period
int migration_period = 200;
// --migration_amount
int migration_amount = 5;
// --migration_topology {isolated, ring, fully_connected}
Island::MigrationTopology migration_topology = Island::MigrationTopology::FULLY_CONNECTED;
// --selection_policy {pure_random, truncation, fitness_proportionate_selection, stochastic_universal_sampling, tournament_selection}
Island::SelectionPolicy selection_policy = Island::SelectionPolicy::TRUNCATION;
// --replacement_policy {pure_random, truncation, dejong_crowding}
Island::ReplacementPolicy replacement_policy = Island::ReplacementPolicy::TRUNCATION;
// --underlying_communication {blocking, non_blocking, rma}
Island::UnderlyingCommunication communication = Island::UnderlyingCommunication::BLOCKING;
// --verbose
int verbose = 0;
// typedefs
typedef vector<double> vec_d;
// constants
const int DATA_TAG = 0x011;
const int THREADS_PER_ISLAND = 2;
inline bool file_exists(const std::string& name) {
ifstream f(name.c_str());
return f.good();
}
void parse_args(int argc, char** argv, bool verbose_args=false) {
if (verbose_args) {
cout << "Found " << argc - 1 << " arguments." << endl;
}
for (int i = 1; i < argc; ++i) {
// Single arguments
if (argv[i] == (string) "sequential") {
mode = 0;
if (verbose_args) {
cout << "Mode " << argv[i] << endl;
}
} else if (argv[i] == (string) "naive") {
mode = 1;
if (verbose_args) {
cout << "Mode " << argv[i] << endl;
}
} else if (argv[i] == (string) "island") {
mode = 2;
if (verbose_args) {
cout << "Mode " << argv[i] << endl;
}
}
// Dual arguments
else if (argv[i] == (string) "--epochs") {
assert(i + 1 < argc);
try {
nr_epochs = stoi(argv[i+1]);
} catch (const std::invalid_argument &e) {
cerr << "Invalid integer for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Number of epochs:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--data") {
assert(i + 1 < argc);
if (file_exists(data_dir + "/" + argv[i+1])) {
data_file = argv[i+1];
} else {
cerr << "Invalid data file" << endl;
exit(1);
}
if (verbose_args) {
cout << "Problem name:\t\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--population") {
assert(i + 1 < argc);
try {
nr_individuals = stoi(argv[i+1]);
} catch (const std::invalid_argument &e) {
cerr << "Invalid integer for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Number of individuals:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--log_dir") {
assert(i + 1 < argc);
log_dir = argv[i + 1];
if (verbose_args) {
cout << "Logging location:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--migration_period") {
assert(i + 1 < argc);
try {
migration_period = stoi(argv[i+1]);
} catch (const std::invalid_argument &e) {
cerr << "Invalid integer for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Migration Period:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--migration_amount") {
assert(i + 1 < argc);
try {
migration_amount = stoi(argv[i+1]);
} catch (const std::invalid_argument &e) {
cerr << "Invalid integer for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Migration Amount:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--elite_size") {
assert(i + 1 < argc);
try {
elite_size = stoi(argv[i+1]);
} catch (const std::invalid_argument &e) {
cerr << "Invalid integer for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Elite size:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--mutation") {
assert(i + 1 < argc);
try {
mutation = stoi(argv[i+1]);
} catch (const std::invalid_argument &e) {
cerr << "Invalid integer for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Mutation:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--verbose") {
assert(i + 1 < argc);
try {
verbose_args = stoi(argv[i + 1]);
verbose = stoi(argv[i + 1]);
} catch (const std::invalid_argument &e) {
cerr << "Invalid integer for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Verbose:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--log_freq") {
assert(i + 1 < argc);
try {
log_freq = stoi(argv[i + 1]);
} catch (const std::invalid_argument &e) {
cerr << "Invalid integer for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Log Frequency:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--migration_topology") {
assert(i + 1 < argc);
if (argv[i+1] == (string) "isolated") {
migration_topology = Island::MigrationTopology::ISOLATED;
} else if (argv[i+1] == (string) "ring") {
migration_topology = Island::MigrationTopology::RING;
} else if (argv[i+1] == (string) "fully_connected") {
migration_topology = Island::MigrationTopology::FULLY_CONNECTED;
} else {
cerr << "Invalid choice (" << argv[i+1] << ") for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Migration Topology:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--selection_policy") {
assert(i + 1 < argc);
if (argv[i+1] == (string) "pure_random") {
selection_policy = Island::SelectionPolicy::PURE_RANDOM;
} else if (argv[i+1] == (string) "truncation") {
selection_policy = Island::SelectionPolicy::TRUNCATION;
} else if (argv[i+1] == (string) "fitness_proportionate_selection") {
selection_policy = Island::SelectionPolicy::FITNESS_PROPORTIONATE_SELECTION;
} else if (argv[i+1] == (string) "stochastic_universal_sampling") {
selection_policy = Island::SelectionPolicy::STOCHASTIC_UNIVERSAL_SAMPLING;
} else if (argv[i+1] == (string) "tournament_selection") {
selection_policy = Island::SelectionPolicy::TOURNAMENT_SELECTION;
} else {
cerr << "Invalid choice (" << argv[i+1] << ") for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Selection Policy:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--replacement_policy") {
assert(i + 1 < argc);
if (argv[i+1] == (string) "pure_random") {
replacement_policy = Island::ReplacementPolicy::PURE_RANDOM;
} else if (argv[i+1] == (string) "truncation") {
replacement_policy = Island::ReplacementPolicy::TRUNCATION;
} else if (argv[i+1] == (string) "dejong_crowding") {
replacement_policy = Island::ReplacementPolicy::DEJONG_CROWDING;
} else {
cerr << "Invalid choice (" << argv[i+1] << ") for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Replacement Policy:\t" << argv[i+1] << endl;
}
} else if (argv[i] == (string) "--underlying_communication") {
assert(i + 1 < argc);
if (argv[i+1] == (string) "blocking") {
communication = Island::UnderlyingCommunication::BLOCKING;
} else if (argv[i+1] == (string) "non_blocking") {
communication = Island::UnderlyingCommunication::NON_BLOCKING;
} else if (argv[i+1] == (string) "rma") {
communication = Island::UnderlyingCommunication::RMA;
} else {
cerr << "Invalid choice (" << argv[i+1] << ") for " << argv[i] << endl;
exit(1);
}
if (verbose_args) {
cout << "Communication:\t" << argv[i+1] << endl;
}
}
}
}
double computeStdDev(vec_d data) {
double mean = accumulate(data.begin(), data.end(), 0.0) / data.size();
double sum_squares = 0.0;
for(auto it = data.begin(); it != data.end(); it++) {
sum_squares += (*it - mean) * (*it - mean);
}
sum_squares = sum_squares / data.size();
return sqrt(sum_squares);
}
double computeMean(vec_d data) {
double sum = accumulate(data.begin(), data.end(), 0.0);
return sum / data.size();
}
void read_input(int &num_cities, float*& cities_matrix) {
// READ INPUT
ifstream input(data_dir + "/" + data_file);
// Read number of cities
string dim;
getline(input, dim);
num_cities = stoi(dim);
cities_matrix = new float[num_cities * num_cities];
// Read values
for (int i = 0; i < num_cities; ++i) {
string line;
getline(input, line);
if(!input.good()){
break;
}
stringstream iss(line);
for (int j = 0; j < num_cities; ++j) {
string val;
getline(iss, val, ';');
if(!iss.good()){
break;
}
stringstream converter(val);
converter >> cities_matrix[i + num_cities * j];
}
}
input.close();
}
// Data file
// --data att48.csv
// Number of individuals (per island if island model)
// --population 100
// Logging direction
// --log_dir folder_name_in_logs
//
// Run sequential:
// sequential --epochs 1000
//
// Run island
// island
int main(int argc, char** argv) {
// Parse arguments and save in global variables
parse_args(argc, argv, verbose);
// Read problem
int number_cities = -1;
float *node_edge_mat;
read_input(number_cities, node_edge_mat);
assert(number_cities != -1);
// Create problem
TravellingSalesmanProblem problem(number_cities, node_edge_mat, nr_individuals, elite_size, mutation, verbose,
log_freq);
// Sequential version WITHOUT MPI
if (mode == 0) {
// Start logging
problem.set_logger(new Logger(log_dir, 0));
auto start = chrono::high_resolution_clock::now();
double final_distance = problem.solve(nr_epochs, 0);
if (verbose > 0) {
cout << "Final distance is " << final_distance << endl;
}
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
if (verbose > 0) {
cout << duration.count() << " ms total runtime" << endl;
}
// Version WTIH MPI
} else {
MPI_Init(&argc, &argv); /* requirement for MPI */
int rank;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
// Start logging
problem.set_logger(new Logger(log_dir, rank));
// NAIVE PARALLEL MODEL
if (mode == 1) {
auto start = chrono::high_resolution_clock::now();
double final_distance = problem.solve(nr_epochs, rank);
if (verbose > 0) {
cout << "Final distance is " << final_distance << " (rank " << rank << ")" << endl;
}
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
if (verbose > 0) {
cout << duration.count() << " ms total runtime (rank " << rank << ")" << endl;
}
if (rank != 0) {
// send result to rank 0
MPI_Send(&final_distance, 1, MPI_DOUBLE, 0, DATA_TAG, MPI_COMM_WORLD);
} else {
int numProcesses;
MPI_Comm_size(MPI_COMM_WORLD, &numProcesses);
double buff;
vec_d all_dists;
all_dists.push_back(final_distance);
// receive results from all other ranks
for (int i = 0; i < numProcesses - 1; i++) {
MPI_Recv(&buff, 1, MPI_DOUBLE, MPI_ANY_SOURCE, DATA_TAG, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
all_dists.push_back(buff);
final_distance = min(buff, final_distance);
}
double mean = computeMean(all_dists);
double stddev = computeStdDev(all_dists);
if (verbose > 0) {
cout << "Best final distance overall is " << final_distance << endl;
cout << "(mean is " << mean << ", std dev is " << stddev << ")" << endl;
}
}
// ISLAND MODEL
} else if (mode == 2) {
Island island(problem, migration_topology, migration_amount,
migration_period, // immigrants RECEIVED per migration, migration period
selection_policy,
replacement_policy,
communication);
double bestDistance = island.solve(nr_epochs); // number of evolution steps
if (verbose > 0) {
cout << bestDistance << endl;
}
}
MPI_Finalize(); /* requirement for MPI */
}
// Delete cities matrix
delete (node_edge_mat);
return 0;
}