-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
217 lines (201 loc) · 5.84 KB
/
main.cpp
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
#include <fstream>
#include <iostream>
#include <omp.h>
#include <stdlib.h>
#include <string>
#include <sys/resource.h>
#include <sys/time.h>
#include <time.h>
#include <tuple>
#include "Constants.h"
#include "Degree.h"
#include "Deterministic.h"
#include "Greedy.h"
#include "Network.h"
#include "Randomized.h"
#include "SampleRandomized.h"
#include "SingleGreedy.h"
#include "StreamingGreedy.h"
//#include <time.h>
#include <chrono>
#include <iomanip> // setprecision
#if defined(_WIN32)
#include <direct.h>
#else
#include <sys/stat.h>
#include <sys/types.h>
#endif
using namespace std;
#pragma warning(disable : 4996)
vector<uint> B_vals = {15, 20, 25, 30, 35, 40, 45, 50};
vector<uint> M_vals = {3};
vector<double> Delta_vals = {1};
Network *g;
void print_help() {
cout << "Options: " << endl;
cout << "-f <data filename>" << endl
<< "-V <size of V> # default: 50" << endl
<< "-t <type of experiment, 0: influence maximization, 1: sensor "
"placement> # default: 0"
<< endl
<< "-k <value of k> # default: 3" << endl
<< "-B <value of B> # default: 10" << endl
<< "-M <value of M> # default: 3" << endl
<< "-e <value of epsilon> # default: 0.5 for IM and 0 for sensor "
"placement"
<< endl
<< "-n <value of eta - denoise step for RStream> # default: 2" << endl
<< "-g <value of gamma> # default: 1.0" << endl
<< "-a <algorithm, 1: DStream, 2: RStream, 3: SGr>, 4: SampleRstream # "
"default: 1, please use SSA source code for testing IM algorithm"
<< endl
<< "-p <number of threads to running algorithms> # default: 4" << endl;
}
pair<string, int> parseArgs(int argc, char **argv) {
string filename = "error";
int no_nodes = 50;
int i = 1;
while (i < argc - 1) {
string arg = argv[i];
if (arg == "-f") {
filename = argv[i + 1];
} else if (arg == "-g" || arg == "-e") {
string s_val = argv[i + 1];
std::string::size_type sz;
Constants::EPS = std::stod(s_val, &sz);
} else {
string s_val = argv[i + 1];
std::string::size_type sz;
int val = std::stoi(s_val, &sz);
if (arg == "-t") {
Constants::DATA = val == 0 ? Social : Sensor;
if (Constants::DATA == Sensor) {
Constants::NO_DENOISE_STEPS = 1;
}
} else if (arg == "-V") {
no_nodes = val;
} else if (arg == "-k") {
Constants::K = val;
} else if (arg == "-r") {
Constants::RHO = val;
} else if (arg == "-b") {
Constants::BETA = val;
} else if (arg == "-B") {
Constants::BUDGET = val;
} else if (arg == "-n") {
Constants::NO_DENOISE_STEPS = val;
if (Constants::DATA == Sensor) {
Constants::NO_DENOISE_STEPS = 1;
}
} else if (arg == "-a") {
switch (val) {
case 0:
Constants::ALGORITHM = aGreedy;
break;
case 1:
Constants::ALGORITHM = Dstream;
break;
case 2:
Constants::ALGORITHM = Rstream;
break;
case 3:
Constants::ALGORITHM = SGr;
break;
case 4:
Constants::ALGORITHM = SampleRstream;
break;
default:
Constants::ALGORITHM = Dstream;
break;
}
} else if (arg == "-p") {
Constants::NUM_THREAD = val;
}
}
i += 2;
}
return pair<string, int>(filename, no_nodes);
}
void run_command(string filename, int no_nodes) {
Network *g = new Network();
bool r = false;
if (Constants::DATA == Social) {
r = g->read_network_from_file(no_nodes, filename, false);
} else
r = g->read_sensor_data(no_nodes, filename);
if (!r) {
cout << "Wrong file, format or arguments" << endl;
print_help();
} else {
double sol = 0.0;
int no_queries = 0;
omp_set_num_threads(Constants::NUM_THREAD);
switch (Constants::ALGORITHM) {
case aGreedy: {
Greedy *gr = new Greedy(g);
sol = gr->get_solution();
no_queries = gr->get_no_queries();
delete gr;
break;
}
case Rstream: {
Constants::NO_DENOISE_STEPS = 1;
Randomized *ra = new Randomized(g);
sol = ra->get_solution(false);
no_queries = ra->get_no_queries();
delete ra;
break;
}
case SGr: {
StreamingGreedy *s_gr = new StreamingGreedy(g);
sol = s_gr->get_solution();
no_queries = s_gr->get_no_queries();
delete s_gr;
break;
}
case SampleRstream: {
SampleRandomized *srs = new SampleRandomized(g);
sol = srs->get_solution();
no_queries = srs->get_no_queries();
delete srs;
break;
}
default: {
Constants::NO_DENOISE_STEPS = 1;
Deterministic *de = new Deterministic(g);
sol = de->get_solution();
no_queries = de->get_no_queries();
delete de;
break;
}
}
if (Constants::DATA == Sensor)
sol = sol / 100;
std::cout << Constants::ALGORITHM << "," << Constants::EPS << ","
<< Constants::BUDGET << "," << sol << "," << no_queries << ",";
}
delete g;
}
void print_performances(struct rusage *performance_start,
struct rusage *performance_end) {
cout << (performance_end->ru_utime.tv_sec -
performance_start->ru_utime.tv_sec) +
1e-6 * (performance_end->ru_utime.tv_usec -
performance_start->ru_utime.tv_usec)
<< endl;
}
int main(int argc, char *argv[]) {
srand(time(NULL));
pair<string, int> r = parseArgs(argc, argv);
struct rusage performance_start, performance_end;
getrusage(RUSAGE_SELF, &performance_start);
if (r.first == "error") {
std::cout << "Wrong file, format or arguments" << endl;
print_help();
} else {
run_command(r.first, r.second);
}
getrusage(RUSAGE_SELF, &performance_end);
print_performances(&performance_start, &performance_end);
return 0;
}