-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
94 lines (71 loc) · 2.77 KB
/
Copy pathmain.cpp
File metadata and controls
94 lines (71 loc) · 2.77 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
#include <CL/cl.h>
#include <iostream>
#include <functional>
#include <numeric>
#include <chrono>
#include "graph.h"
#include <vector>
#include <map>
#include <iomanip>
#include <string>
#include "path_finder.h"
int main(int argc, const char* argv[]) {
if (argc != 3) {
std::cerr << "Should be only 2 arguments [file, num_test]";
return 1;
}
Graph graph;
if (graph.open(argv[1])) {
return 1;
}
int tests_amount;
try {
tests_amount = std::stoi(argv[2]);
}
catch (std::exception& e) {
std::cerr << "Second argument should be integer";
return 1;
}
std::vector<path_finder_algo> algos = { fordBellman, dijkstra , fordBellmanOpenCL };
std::map<std::string, std::vector<double>> working_times;
std::vector<std::vector<double>> results;
std::mt19937 gen;
std::cout << "Running " << tests_amount << " tests on graph with " << graph.vertices_amount << " vertices and " << graph.edges.size() << " edges" << std::endl << std::endl;
for (int test = 0; test < tests_amount; test++) {
std::cout << "#Test num " << test + 1 << std::endl;
int start = gen() % graph.vertices_amount;
std::cout << "Begin vertex is " << start << std::endl << std::endl;
for (auto& algo : algos) {
std::vector<double> res;
try {
res = algo(graph, start, working_times);
}
catch (std::exception& e) {
continue;
}
results.push_back(res);
std::cout << std::endl;
}
for (int i = 1; i < algos.size(); i++) {
for (int j = 0; j < results[0].size(); j++) {
if (std::abs(results[0][j] - results[i][j]) > 1e-6) {
std::cerr << "Wrong algo work with index " << i << std::endl;
std::cout << "Distance to vertex " << j << " should be " << results[0][j] << " ,but was " << results[i][j] << std::endl;
return 1;
}
}
}
}
std::map<std::string, double> average_times;
for (auto& i : working_times) {
double average_time = std::accumulate(i.second.begin(), i.second.end(), 0.0) / i.second.size();
average_times[i.first] = average_time;
}
double max_time = average_times["Dijkstra(CPU)"];
std::cout <<"\nAverage times" << std::endl <<
std::left << std::setw(40) << "Name" << std::setw(16) << "avg.time" << "percent" << std::endl;
std::cout << std::fixed;
for (auto& i : average_times) {
std::cout << std::left << std::setw(33) << std::setprecision(2) << i.first << "\t" << std::setw(10) << i.second << "\t" << std::setprecision(0) << i.second / max_time * 100.0 << "%" << std::endl;
}
}