root / custompackages / graph-parser / src / simulation.cpp @ 9fc1fa64
History | View | Annotate | Download (5.56 KB)
1 |
#include <cstdlib> |
---|---|
2 |
#include <ctime> |
3 |
#include <iostream> |
4 |
#include <fstream> |
5 |
#include <dirent.h> |
6 |
#include <vector> |
7 |
#include "bi_connected_components.h" |
8 |
#include "centrality.h" |
9 |
#include "graph_manager.h" |
10 |
#include "parser.h" |
11 |
using namespace std; |
12 |
|
13 |
void get_all_files_in_directory(string dir_path, vector<string>& files, string with_extension) { |
14 |
DIR* dir; |
15 |
dirent* pdir; |
16 |
|
17 |
dir = opendir(dir_path.c_str()); |
18 |
|
19 |
while (pdir = readdir(dir)) {
|
20 |
string filename = pdir->d_name;
|
21 |
string::size_type idx;
|
22 |
idx = filename.find('.');
|
23 |
string extension = ""; |
24 |
if (idx != string::npos) { |
25 |
extension = filename.substr(idx + 1);
|
26 |
} |
27 |
|
28 |
if (extension.compare(with_extension) == 0) { |
29 |
files.push_back(filename); |
30 |
cout << filename << endl; |
31 |
} |
32 |
} |
33 |
} |
34 |
|
35 |
void calculate_brandes_bc(const GraphManager& gm, bool targets_inclusion) { |
36 |
CentralityVec v_centrality_vec = CentralityVec(boost::num_vertices(gm.g_)); |
37 |
CentralityPMap v_centrality_pmap = CentralityPMap(v_centrality_vec.begin(), gm.v_index_pmap());; |
38 |
|
39 |
if (gm.weighted_graph()) { // calculate BC for weighted graph |
40 |
cout << "======= BCC - BC for weighted graph ======\n";
|
41 |
|
42 |
typedef map<Edge, double> EdgeWeightStdMap; |
43 |
typedef boost::associative_property_map<EdgeIndexStdMap> EdgeWeightPMap;
|
44 |
EdgeIndexStdMap edge_weight_std_map; |
45 |
EdgeWeightPMap edge_weight_pmap = EdgeWeightPMap(edge_weight_std_map); |
46 |
|
47 |
|
48 |
BGL_FORALL_EDGES(edge, gm.g_, Graph) { |
49 |
edge_weight_std_map[edge] = gm.g_[edge].cost; |
50 |
} |
51 |
boost::brandes_betweenness_centrality_targets_inclusion(gm.g_, |
52 |
targets_inclusion, |
53 |
boost::centrality_map( |
54 |
v_centrality_pmap).vertex_index_map( |
55 |
gm.v_index_pmap()).weight_map( |
56 |
edge_weight_pmap) |
57 |
); |
58 |
} |
59 |
else { // for unweighted graph |
60 |
boost::brandes_betweenness_centrality_targets_inclusion(gm.g_, |
61 |
targets_inclusion, |
62 |
boost::centrality_map( |
63 |
v_centrality_pmap).vertex_index_map( |
64 |
gm.v_index_pmap()) |
65 |
); |
66 |
} |
67 |
|
68 |
boost::relative_betweenness_centrality(gm.g_, v_centrality_pmap); |
69 |
} |
70 |
|
71 |
void run_simulation_for_a_graph(string input_path, double& elapsed_secs_bc, double& elapsed_secs_hbc, int number_of_experiments, bool is_weighted_graph) { |
72 |
/* Returns the running time for Brandes BC and HBC
|
73 |
*/
|
74 |
clock_t begin, end; |
75 |
|
76 |
// Disable the console output: http://stackoverflow.com/questions/30184998/how-to-disable-cout-output-in-the-runtime
|
77 |
streambuf* orig_buf = cout.rdbuf(); // get underlying buffer
|
78 |
cout.rdbuf(0); // set null |
79 |
|
80 |
GraphManager gm(is_weighted_graph); |
81 |
cout << input_path << endl; |
82 |
readEdgeFileGraphManager(input_path, gm); |
83 |
|
84 |
// For Brandes BC
|
85 |
begin = clock(); |
86 |
bool targets_inclusion = true; |
87 |
calculate_brandes_bc(gm, targets_inclusion); |
88 |
end = clock(); |
89 |
elapsed_secs_bc = double(end - begin) / CLOCKS_PER_SEC;
|
90 |
|
91 |
// For HBC
|
92 |
begin = clock(); |
93 |
BiConnectedComponents bcc = BiConnectedComponents(gm); |
94 |
bcc.run(); |
95 |
end = clock(); |
96 |
elapsed_secs_hbc = double(end - begin) / CLOCKS_PER_SEC;
|
97 |
|
98 |
// Restore the console output
|
99 |
cout.rdbuf(orig_buf); |
100 |
|
101 |
// time_log = make_tuple(elapsed_secs_bc, elapsed_secs_hbc);
|
102 |
} |
103 |
|
104 |
void write_simulation_result(string out_file_path, string filename, int run_index, double bc_time, double hbc_time) { |
105 |
ofstream out_file(out_file_path.c_str(), std::ofstream::out | std::ofstream::app); |
106 |
|
107 |
// Extract information from file name
|
108 |
string graph_type = filename.substr(0, 2); |
109 |
|
110 |
string::size_type sep_pos = filename.find('_'); |
111 |
string number_of_nodes = filename.substr(2, sep_pos - 2); |
112 |
|
113 |
string separator = "\t"; |
114 |
if (out_file.is_open()) {
|
115 |
out_file << filename << separator; |
116 |
out_file << graph_type << separator; |
117 |
out_file << number_of_nodes << separator; |
118 |
out_file << run_index << separator; |
119 |
out_file << bc_time << separator; |
120 |
out_file << hbc_time << separator << endl; |
121 |
} |
122 |
out_file.close(); |
123 |
} |
124 |
|
125 |
int main(int argc, char * argv[]) { |
126 |
/*
|
127 |
Input: the folder with all the *.edges graph file
|
128 |
|
129 |
For each graph, run the simulation <num_of_times> times
|
130 |
Write the output into one csv file
|
131 |
|
132 |
filename | graph type | number of nodes | id of the run | Brandes | HBC
|
133 |
*/
|
134 |
|
135 |
// Handle command-line arguments
|
136 |
if (argc < 2) { |
137 |
cout << "Provide the input directory\n";
|
138 |
exit(1);
|
139 |
} |
140 |
|
141 |
string input_dir = string(argv[1]); |
142 |
|
143 |
int number_of_experiments = 10; // default |
144 |
if (argc >= 3) { |
145 |
number_of_experiments = atoi(argv[2]);
|
146 |
} |
147 |
|
148 |
string output_file_path = "../output/simulation.out"; |
149 |
if (argc >= 4) { |
150 |
output_file_path = string(argv[3]); |
151 |
} |
152 |
|
153 |
bool is_weighted_graph = true; |
154 |
if (argc >= 5) { |
155 |
if (string(argv[4]).compare("false") == 0) { |
156 |
is_weighted_graph = false;
|
157 |
} |
158 |
} |
159 |
|
160 |
// Find all the files
|
161 |
vector<string> files;
|
162 |
get_all_files_in_directory(input_dir, files, "edges");
|
163 |
|
164 |
for (string file : files) { |
165 |
cout << "Running for file " << file << endl;
|
166 |
for (int run_index = 0; run_index < number_of_experiments; ++run_index) { |
167 |
double bc_time;
|
168 |
double hbc_time;
|
169 |
string input_path = input_dir + "/" + file; |
170 |
run_simulation_for_a_graph(input_path, bc_time, hbc_time, number_of_experiments, is_weighted_graph); |
171 |
cout << " " << run_index << ": " << bc_time << " | " << hbc_time << endl; |
172 |
write_simulation_result(output_file_path, file, run_index, bc_time, hbc_time); |
173 |
} |
174 |
} |
175 |
} |
176 |
|
177 |
|