-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
103 lines (88 loc) · 3.06 KB
/
main.cc
File metadata and controls
103 lines (88 loc) · 3.06 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
#include "reader.h"
#include "solver.h"
#include "ACO_mpi.hpp"
#include "ga_mpi.hpp"
#include <chrono>
int main(int argc, char *argv[])
{
std::string filename = (argc > 1) ? argv[1] : "tsp_graph/a280.tsp";
std::string type = (argc > 2) ? argv[2] : "aco";
std::string parallel = (argc > 3) ? argv[3] : "mpi";
TSPReader reader;
if (!reader.readFile(filename))
{
std::cerr << "Failed to read TSP file." << std::endl;
return 1;
}
std::vector<int> bestTour;
double bestDistance = 0.0, elapsedTime = 0.0;
std::cout << "Solving TSP...\n";
if (type == "aco")
{
auto start = std::chrono::high_resolution_clock::now();
if (parallel == "mpi")
{
int rank, size;
MPI_Init(nullptr, nullptr);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
std::cout << rank << " " << size << std::endl;
ACOMpi solver(reader.getPoints());
solver.solve();
bestTour = solver.getTour();
bestDistance = solver.getDistance();
MPI_Barrier(MPI_COMM_WORLD);
if (rank != 0) {
MPI_Finalize();
return 0;
} else {
MPI_Finalize();
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
elapsedTime = duration.count();
}
else if (type == "ga")
{
// Solve TSP
auto start = std::chrono::high_resolution_clock::now();
std::pair<std::vector<int>, double> result;
if (parallel == "mpi") {
int rank, size;
MPI_Init(nullptr, nullptr);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
ga_mpi gampi(reader.getPoints(), 100, 10000, 0.05, 0.8);
start = std::chrono::high_resolution_clock::now();
result = gampi.solve();
MPI_Barrier(MPI_COMM_WORLD);
if (rank != 0) {
MPI_Finalize();
return 0;
} else {
MPI_Finalize();
}
}
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
bestTour = result.first;
bestDistance = result.second;
elapsedTime = duration.count();
}
// write results to file
std::string res_fname = "res/" + filename + "_" + type + "_mpi.txt";
std::ofstream file;
file.open(res_fname);
file << "Testcase: " << filename << "\n";
file << "Time taken: " << elapsedTime << " milliseconds\n";
file << "Total distance: " << bestDistance << "\n";
file << "Best tour found:\n";
for (int city : bestTour)
{
file << city << "\n";
}
// print results to console
std::cout << "Distance: " << bestDistance << "\nSolution found in " << elapsedTime << " milliseconds\n";
return 0;
}