Repository navigation
Expand file tree
/
Copy pathTrustRank_directed_iter.cpp
More file actions
586 lines (433 loc) · 15.1 KB
/
Copy pathTrustRank_directed_iter.cpp
File metadata and controls
586 lines (433 loc) · 15.1 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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <set>
#include <map>
#include <list>
#include <vector>
#include <assert.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <pthread.h>
#include <time.h>
#define GCC_VERSION (__GNUC__ * 10000 \
+ __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
#if GCC_VERSION >= 40300
#include <tr1/unordered_map>
using namespace std::tr1;
#define hash_map unordered_map
#else
#include <unordered_map>
#endif
using namespace std;
/**
* Returns the peak (maximum so far) resident set size (physical
* memory use) measured in bytes, or zero if the value cannot be
* determined on this OS.
*/
// random generator function:
ptrdiff_t myrandom (ptrdiff_t i) { return rand()%i;}
// pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) = myrandom;
long randint(long min, long max){
return long(rand() / (RAND_MAX + 0.0) * (max - min) + min);
}
bool my_comparator ( const pair<double, int>& l, const pair<double, int>& r)
{
return l.first > r.first;
}
class Data{
public:
typedef unsigned long int vertex;
long N;
//adjacency list
//weighted graph
unordered_map<vertex,set<pair<vertex, double> > > network_in, network_out, network_map;
//1 : weighted, 0: unweighted
int weighted_graph;
//priors
//probability of being benign for each node
double *prior;
//network file
char *network_file;
char *post_file;
char *prior_file;
//during computation, post is treated as the normalized multiplication of incoming messages of each node.
//
double *post;
double *post_pre;
//to support different input format
char *train_set_file;
//parameters of PMR model when input is a set of labeled nodes, instead of priors
double theta_pos;
double theta_neg;
double theta_unl;
//this weight is used when the input is an unweighted graph
double weight;
//for sybilrank
double alpha;
double max_iter;
//-1: no boosting
//0 : only benign labels are given. Sample the same number of nodes as Sybils
//1 : only Sybils labels are givem. Sample the same number of nodes as benign labels
//2 : assume no labels are given. Sample num_benign as benign labels. Sample num_sybil nodes as Sybil labels.
int boosting_type;
int num_benign;
int num_sybil;
int boosting_iter;
int* ordering_array;
int sampled_sybil_nodes;
int num_threads;
double polar;
double homophily;
double estimated_polar;
string aggregator;
class TrustRank_arg{
public:
Data * data_pointer;
int current_thread;
TrustRank_arg(){}
};
Data(){
}
void add_edge(vertex node1, vertex node2, double w){
// add edge (node1, node2)
// no self loops
if(node1==node2){
return;
}
//add node2 to the adjacency list of node1
pair <vertex, double> nei (node1, w);
network_in[node2].insert(nei);
network_out[node1].insert(make_pair(node2, w));
}
/* Read in the social graph */
//the format for the social graph is
//each line corresponds to an edge, e.g, 3 2 0.8
//each edge in the graph appears twice, e.g.,
//3 2 0.8
//2 3 0.9
void read_network(){
ifstream in(network_file,ifstream::in);
assert(in);
string line;
vertex node1,node2,max_id=0;
double w;
//read edges
while(getline(in,line)!=NULL){
node1=(vertex)atol(strtok((char *)line.c_str()," \n\t\r"));
node2=(vertex)atol(strtok(NULL," \n\t\r"));
if (weighted_graph == 1) {
w=(double)atof(strtok(NULL," \n\t\r"));
}
else{
w = weight;
}
//cout << node1 << " " << node2 << " " << w << endl;
add_edge(node1, node2, w);
if(max_id < node1){
max_id = node1;
}
if(max_id < node2){
max_id = node2;
}
}
//number of nodes in the graph
// N = network_map.size();
N = max_id + 1;
// cout<<N<<" "<<network_map.size()<<endl;
//allocate space for final scores
post = (double*) malloc(sizeof(double)*(N));
post_pre = (double*) malloc(sizeof(double)*(N));
//allocate space for final scores
prior = (double*) malloc(sizeof(double)*(N));
in.close();
cout<<"read network done"<<endl;
}
void read_prior(){
//initialize priors as theta_unl
vertex node;
if (train_set_file == "" && boosting_type == -1){
for (node = 0; node < N; node++) {
prior[node] = 0.5;
}
}else{
for (node = 0; node < N; node++) {
prior[node] = theta_unl;
}
cout<<"initialize prior done"<<endl;
}
if (prior_file != "") {
ifstream in(prior_file,ifstream::in);
assert(in);
string line;
double score;
while(getline(in,line)!=NULL){
node=(vertex)atol(strtok((char *)line.c_str()," \n\t\r"));
score=(double)atof(strtok(NULL," \n\t\r"));
prior[node] = score;
}
in.close();
}
if (train_set_file != "" && boosting_type == -1){
ifstream in(train_set_file,ifstream::in);
assert(in);
string line;
//reading labeled benign nodes.
getline(in,line);
istringstream pos_train_str(line);
vertex sub;
while (pos_train_str){
pos_train_str >> sub;
prior[sub] = theta_pos;
//cout << sub << " " << prior[sub] << endl;
}
getline(in,line);
istringstream neg_train_str(line);
// while (neg_train_str){
// neg_train_str >> sub;
// prior[sub] = theta_neg;
// //cout << sub << " " << prior[sub] << endl;
// }
in.close();
}
cout<<"read prior done"<<endl;
}
void write_posterior(){
ofstream out(post_file, ofstream::out);
for (vertex i = 0; i < N; i++) {
out << i << " " << setprecision(10) << post[i] << endl;
}
out.close();
}
//output peak memory use and total runtime
// void write_memtime(long mem, double time){
//
// ofstream out(post_file, ofstream::out);
// out << "Memory\t" << mem << endl;
// out << "Time\t" << time << endl;
//
// out.close();
// cout<< "Memory\t" << mem << endl;
// cout<< "Time\t" << time << endl;
//
// }
static void * TrustRank_thread(void *arg_pointer){
Data * pointer = ((TrustRank_arg *)arg_pointer)->data_pointer;
int current_thread = ((TrustRank_arg *)arg_pointer)->current_thread;
int num_nodes = ceil(((float)pointer->N) / pointer->num_threads);
int start = current_thread * num_nodes;
int end = current_thread * num_nodes + num_nodes;
if (end > pointer->N) {
end = pointer->N;
}
//cout << "begins " << current_thread << " " << start << " " << end << endl;
vertex node;
double message;
set<pair<vertex, double> >::iterator nei_iter;
double value[2], sum;
//## Code below edited by Le Zhang on Feb 16, 2016 ##
//## [email protected]
for (vertex index = start; index < end; index++) {
node = pointer->ordering_array[index];
//update the the post for node
message = 0;
if(pointer->network_in.find(node) != pointer->network_in.end()){
for (nei_iter = pointer->network_in[node].begin(); nei_iter != pointer->network_in[node].end(); nei_iter++) {
double sum_wei = 0;
if(pointer->network_out.find((*nei_iter).first) != pointer->network_out.end()){
sum_wei = pointer->network_out[(*nei_iter).first].size();
}else{
cout<<"network out error at node "<<(*nei_iter).first<<endl;
}
//use edge weight but will be extremely slow
// if(pointer->network_out[(*nei_iter).first].size()!=0){
// for(set<pair<vertex, double> >::iterator wei_iter = pointer->network_out[(*nei_iter).first].begin(); wei_iter!=pointer->network_out[(*nei_iter).first].end(); wei_iter++){
// sum_wei += (*wei_iter).second;
// }
// }
if(sum_wei == 0){
message += 0;
}else{
message += pointer -> post_pre[(*nei_iter).first] * (*nei_iter).second / sum_wei;
}
// * (*nei_iter).second;
// cout<<message<<endl;
}
}
pointer->post[node] = (1 - pointer->alpha) * message + pointer->alpha * pointer->prior[node];
}
//cout << "end " << current_thread << " " << start << " " << end << endl;
}
void TrustRank(){
//use iterative method to propagate messages
//flooding method's performance is not as good as iterative method
ordering_array = (int*) malloc(sizeof(int) * N);
//initialize posts
memcpy(post, prior, sizeof(double) * N);
//random ordering
vector<vertex> ordering;
for (vertex i = 0; i < N; i++) {
ordering.push_back(i);
}
int iter = 1;
vector<vertex>::iterator iter_order;
TrustRank_arg * arg_pointer;
int current_thread;
pthread_t thread;
vector<pthread_t> threads;
vertex i = 0;
while (iter <= max_iter) {
threads.clear();
memcpy(post_pre, post, sizeof(double) * N);
random_shuffle(ordering.begin(), ordering.end(), p_myrandom);
for (iter_order = ordering.begin(), i = 0; iter_order != ordering.end(); iter_order++, i++){
ordering_array[i] = *iter_order;
}
for (current_thread = 0; current_thread < num_threads; current_thread++) {
arg_pointer = new TrustRank_arg();
arg_pointer->data_pointer = this;
arg_pointer->current_thread = current_thread;
pthread_create(&thread, NULL, TrustRank_thread, (void*)arg_pointer);
threads.push_back(thread);
}
for (i = 0; i < threads.size(); i++) {
pthread_join(threads[i], NULL);
//cout << i << endl;
}
//output result of this iteration
//post_file_iter = post_file + to_string(iter) + ".txt";
stringstream post_file_iter;
post_file_iter << post_file << iter << ".txt";
const char *addr = post_file_iter.str().c_str();
ofstream out(addr, ofstream::out);
for (vertex i = 0; i < N; i++) {
out << i << " " << setprecision(10) << post[i] << endl;
}
out.close();
iter += 1;
cout << "write graph at iter " << iter << "done." << endl;
}
// Normalization
// for(vertex v=0; v<N;v++){
// if(network_in[v].size() != 0){
// post[v] /= network_in[v].size();
// }
// }
}
void parse_par(int argc, char **argv){
//default setting
network_file = "";
boosting_type = -1;
boosting_iter = 5;
num_benign = 10;
num_sybil = 10;
max_iter = 10;
theta_pos = 1.0;
theta_neg = 0.1;
theta_unl = 0;
weight = 1.0;
alpha = 0.15;//for sybilrank
train_set_file = "";
post_file = "";
prior_file = "";
//by default, weighted graph
weighted_graph = 1;
sampled_sybil_nodes = 1;
num_threads = 1;
aggregator = "hea";
estimated_polar = -1;
int i = 1;
while (i < argc) {
if (strcmp(argv[i],"-graphfile") == 0){
network_file = argv[i + 1];
}
else if (strcmp(argv[i],"-trainfile") == 0){
train_set_file = argv[i + 1];
}
else if (strcmp(argv[i],"-priorfile") == 0){
prior_file = argv[i + 1];
}
else if (strcmp(argv[i], "-postfile") == 0){
post_file = argv[i + 1];
}
else if (strcmp(argv[i],"-bt") == 0){
boosting_type = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-bi") == 0){
boosting_iter = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-mIter") == 0){
max_iter = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-tp") == 0){
theta_pos = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-tn") == 0){
theta_neg = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-tu") == 0){
theta_unl = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-wei") == 0){
weight = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-wg") == 0){
weighted_graph = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-ss") == 0){
sampled_sybil_nodes = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-nt") == 0){
num_threads = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-nb") == 0){
num_benign = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-ns") == 0){
num_sybil = atoi(argv[i + 1]);
}
else if (strcmp(argv[i],"-agg") == 0){
aggregator = argv[i + 1];
}
else if (strcmp(argv[i],"-ep") == 0){
estimated_polar = atof(argv[i + 1]);
}
else if (strcmp(argv[i],"-alph") == 0){
alpha = atof(argv[i + 1]);
}
else{
cout << "undefined inputs: " << argv[i] <<endl;
exit(0);
}
i += 2;
}
}
};
int main (int argc, char **argv)
{
clock_t start, end;
start = clock() ;
srand ( time(NULL) );
Data data;
cout << 1 << endl;
data.parse_par(argc, argv);
cout << 2 << endl;
data.read_network();
cout << 3 << endl;
data.read_prior();
cout << 4 << endl;
data.TrustRank();
// data.write_posterior();
end = clock() ;
// cout<<endl<<"Total time taken: "<<(double)(end-start)/CLOCKS_PER_SEC*1000<<" ms"<<endl;
// size_t peakSize = getPeakRSS( );
// cout<<peakSize<<endl;
// data.write_memtime((long)peakSize, (double)(end-start)/CLOCKS_PER_SEC*1000);
return 0;
}