-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathModule.cpp
More file actions
3218 lines (2459 loc) · 106 KB
/
Copy pathModule.cpp
File metadata and controls
3218 lines (2459 loc) · 106 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
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Author: Seung-Hee Bae (shbae@cs.washington.edu)
* Date: Mar. 2014
* Copyright (C) since 2013, Seung-Hee Bae, Bill Howe, Database Group at the University of Washington
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <omp.h>
#include <cstdlib>
#if defined __GNUCC__ || defined __APPLE__
#include <ext/hash_map>
#else
#include <hash_map>
#endif
#include <ctime>
#include <sys/time.h>
#include "Module.h"
#include "Node.h"
#include "timing.h"
//typedef __gnu_cxx::hash_map<int, double> flowmap;
typedef map<int, double> flowmap;
typedef map<int, pair<double, double> > modInfo; // <modID, <exitPr, sumPr> >
void findAssignedPart(int* start, int* end, int numNodes, int numTh, int myID);
const double InitLength = 10000.0;
using namespace std;
struct MoveSummary {
double diffCodeLen; // delta(L(M))
int newModule;
double sumPr1, sumPr2; // updated sumPr1 and sumPr2. 1 --> oldM, 2 --> newM.
double exitPr1, exitPr2; // updated exitPr1 and exitPr2.
double newSumExitPr; // SUM(q_i) = this.sumAllExitPr + q_1_new + q_2_new - q_1_old - q_2_old.
};
// Constructors of Module class.
Module::Module()
:index(0),
exitPr(0.0),
stayPr(0.0),
sumPr(0.0),
sumTPWeight(0.0),
sumDangling(0.0),
numMembers(1)
{}
Module::Module(int idx, double exitPr, double sumPr)
:index(idx),
exitPr(exitPr),
stayPr(exitPr + sumPr),
sumPr(sumPr),
sumTPWeight(0.0),
sumDangling(0.0),
numMembers(1)
{}
//Module::Module(int idx, int nNode, Node nd)
Module::Module(int idx, Node * nd)
:index(idx),
exitPr(0.0),
stayPr(0.0),
sumPr(nd->Size()),
sumTPWeight(nd->TeleportWeight()),
sumDangling(0.0),
numMembers(1)
{
double sumExitFlow = 0.0;
int nOutLinks = nd->outLinks.size();
for (int i = 0; i < nOutLinks; i++)
sumExitFlow += nd->outLinks[i].second; // after call initiate(), w_ab is updated as p_a * w_ab.
if (nd->IsSuper()) { // If SuperNode, we already calculated exitPr in the corresponding module.
exitPr = nd->ExitPr();
sumDangling = nd->DanglingSize();
}
// exitPr = tau * (1 - sum(tau_a))*sum(p_a) + (1-tau) * sumExitFlow.
else if (!nd->IsDangling()) {
exitPr = Network::alpha * (1.0 - nd->TeleportWeight()) * sumPr + Network::beta * sumExitFlow;
nd->setExitPr(exitPr);
}
else {
exitPr = (1.0 - nd->TeleportWeight()) * sumPr;
nd->setExitPr(exitPr);
sumDangling = nd->Size();
}
stayPr = exitPr + sumPr; // exitPr + sum (P_a).
members.push_back(nd);
}
SubModule::SubModule()
: modIdx(0),
numMembers(0),
sumPr(0.0),
sumTPWeight(0.0),
sumDangling(0.0)
{}
/**
* Module mod : module of newNetwork (network based on a module in original network.)
*/
SubModule::SubModule(Module& mod, map<int, int>& origNodeID, int modIndex) {
numMembers = mod.NumMembers();
sumPr = mod.SumPr();
sumTPWeight = mod.SumTPWeight();
sumDangling = mod.SumDangling();
modIdx = modIndex;
for (vector<Node*>::iterator it = mod.members.begin(); it != mod.members.end(); it++) {
members.push_back(origNodeID[(*it)->ID()]);
}
// numMembers should be equal to members.size() ...
if (numMembers != members.size())
cout << "SOMETHING WRONG!!! -- numMembers != members.size() in SubModule()..." << endl;
}
SubModule::SubModule(Module& mod) {
modIdx = mod.Index();
numMembers = 1;
sumPr = mod.SumPr();
sumTPWeight = mod.SumTPWeight();
sumDangling = mod.SumDangling();
members.push_back(mod.members[0]->ID());
}
// Constructors of Network class.
Network::Network()
:level(0),
codeLength(InitLength),
nNode(0),
nEdge(0),
nEmptyMod(0),
nModule(0),
totNodeWeights(0.0),
nDanglings(0),
allNodes_log_allNodes(0.0),
sumAllExitPr(0.0)
{}
Network::Network(int numNode, int numEdge, int level)
:level(level),
codeLength(InitLength),
nNode(numNode),
nEdge(numEdge),
nEmptyMod(0),
nModule(numNode),
totNodeWeights(0.0),
nDanglings(0),
allNodes_log_allNodes(0.0),
sumAllExitPr(0.0)
{
emptyModules.reserve(numNode);
}
Network::Network(int numNode, int numEdge, int level, double codeLen)
:level(level),
codeLength(codeLen),
nNode(numNode),
nEdge(numEdge),
nEmptyMod(0),
nModule(numNode),
totNodeWeights(0.0),
nDanglings(0),
allNodes_log_allNodes(0.0),
sumAllExitPr(0.0)
{
emptyModules.reserve(numNode);
}
/*
* Other Member functions of Network class.
*/
void Network::findDanglingNodes() {
nDanglings = 0; // reset the number of dangling nodes.
for (int i = 0; i < nNode; i++) {
if (nodes[i].outLinks.empty()) {
danglings.push_back(i);
nDanglings++;
nodes[i].setIsDangling(true);
}
}
}
//void Network::initiate() {
void Network::initiate(int numTh) {
int nDangNodes = 0;
struct timeval startT, endT;
omp_set_num_threads(numTh);
gettimeofday(&startT, NULL);
#pragma omp parallel reduction (+:nDangNodes)
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++) {
if (nodes[i].outLinks.empty()) {
#pragma omp critical (updateDang)
{
danglings.push_back(i);
}
nDangNodes++;
nodes[i].setIsDangling(true);
}
else { // normal nodes. --> Normalize edge weights.
int nOutLinks = nodes[i].outLinks.size();
//double sum = nodes[i].selfLink; // don't support selfLink yet.
double sum = 0.0;
for (int j = 0; j < nOutLinks; j++)
sum += nodes[i].outLinks[j].second;
//nodes[i].selfLink /= sum;
for (int j = 0; j < nOutLinks; j++)
nodes[i].outLinks[j].second /= sum;
}
}
}
gettimeofday(&endT, NULL);
nDanglings = nDangNodes;
cout << "Level " << level << ": the number of dangling nodes = " << nDanglings << endl;
cout << "Time for finding dangling nodes : " << elapsedTimeInSec(startT, endT) << " (sec)" << endl;
gettimeofday(&startT, NULL);
calculateSteadyState(numTh);
gettimeofday(&endT, NULL);
cout << "Time for calculating steady state of nodes (eigenvector): " << elapsedTimeInSec(startT, endT) << " (sec)" << endl;
gettimeofday(&startT, NULL);
// Update edges to represent flow based on calculated steady state (aka size).
#pragma omp parallel
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++) {
if (!nodes[i].IsDangling()) {
int nOutLinks = nodes[i].outLinks.size();
for (int j = 0; j < nOutLinks; j++) {
nodes[i].outLinks[j].second = nodes[i].Size() * nodes[i].outLinks[j].second;
}
}
else {
nodes[i].setDanglingSize(nodes[i].Size());
}
}
}
gettimeofday(&endT, NULL);
cout << "Time for updating edge weights based on flow information: " << elapsedTimeInSec(startT, endT) << " (sec)" << endl;
// Calculate SUM of p_log_p over all nodes.
double allNds_log_allNds = 0.0;
#pragma omp parallel reduction (+:allNds_log_allNds)
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++)
allNds_log_allNds += pLogP(nodes[i].Size());
}
allNodes_log_allNodes = allNds_log_allNds;
/////////////////////////////
// Make modules from nodes //
/////////////////////////////
#pragma omp parallel
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++) {
modules[i] = Module(i, &nodes[i]); // Assign each Node to a corresponding Module object.
// Initially, each node is in its own module.
nodes[i].setModIdx(i);
}
}
nModule = nNode;
gettimeofday(&startT, NULL);
calibrate(numTh);
gettimeofday(&endT, NULL);
cout << "Time for calculating of initial code length: " << elapsedTimeInSec(startT, endT) << " (sec)" << endl;
}
// calculating steady state of nodes by Power Iteration Method.
// same as Greedy::eigenvector() in Infomap implementation.
// Modify for the design of this implementation.
void Network::calculateSteadyState(int numTh) {
// initial probability distribution = 1 / N.
vector<double> size_tmp = vector<double>(nNode, 1.0/nNode);
int iter = 0;
double danglingSize = 0.0;
double sqdiff = 1.0;
double sum = 0.0;
// Generate addedSize array per each thread, so that we don't need to use lock for each nodeSize addition.
double** addedSize = new double*[numTh];
for (int i = 0; i < numTh; i++)
addedSize[i] = new double[nNode];
do {
// calculate sum of the size of dangling nodes.
danglingSize = 0.0;
#pragma omp parallel reduction (+:danglingSize)
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nDanglings, nTh, myID);
for (int i = start; i < end; i++)
danglingSize += size_tmp[danglings[i]];
}
// flow via teleportation.
#pragma omp parallel
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++)
nodes[i].setSize( (alpha + beta*danglingSize) * nodes[i].TeleportWeight());
}
int realNumTh = 0;
// flow from network steps via following edges.
#pragma omp parallel
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
#pragma omp master
{
realNumTh = nTh;
}
double *myAddedSize = addedSize[myID];
for (int i = 0; i < nNode; i++)
myAddedSize[i] = 0.0; // initialize for the temporary addedSize array.
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++) {
int nOutLinks = nodes[i].outLinks.size();
for (int j = 0; j < nOutLinks; j++) {
myAddedSize[nodes[i].outLinks[j].first] += beta * nodes[i].outLinks[j].second * size_tmp[i];
}
}
}
//set nodes[i].size by added addedSize[] values.
#pragma omp parallel
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++) {
for (int j = 0; j < realNumTh; j++)
nodes[i].addSize(addedSize[j][i]);
}
}
// Normalize of node size.
sum = 0.0;
#pragma omp parallel reduction (+:sum)
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++)
sum += nodes[i].Size();
}
sqdiff = 0.0;
#pragma omp parallel reduction (+:sqdiff)
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nNode, nTh, myID);
for (int i = start; i < end; i++) {
nodes[i].setSize(nodes[i].Size()/sum);
sqdiff += fabs(nodes[i].Size() - size_tmp[i]);
size_tmp[i] = nodes[i].Size();
}
}
iter++;
} while ((iter < 200) && (sqdiff > 1.0e-15 || iter < 50));
// deallocate 2D array.
for (int i = 0; i < numTh; i++)
delete [] addedSize[i];
delete [] addedSize;
cout << "Calculating flow done in " << iter << " iterations!" << endl;
}
// This function calculate current codeLength.
// This implementation is modified version of infomap implementation.
void Network::calibrate(int numTh) {
//This is the calculation of Equation (4) in the paper.
double sum_exit_log_exit = 0.0;
double sum_stay_log_stay = 0.0;
double sumExit = 0.0;
omp_set_num_threads(numTh);
#pragma omp parallel reduction (+:sum_exit_log_exit, sum_stay_log_stay, sumExit)
{
int myID = omp_get_thread_num();
int nTh = omp_get_num_threads();
int start, end;
findAssignedPart(&start, &end, nModule, nTh, myID);
for (unsigned int i = start; i < end; i++) {
sum_exit_log_exit += pLogP(modules[i].ExitPr());
sum_stay_log_stay += pLogP(modules[i].StayPr());
sumExit += modules[i].ExitPr();
}
}
sumAllExitPr = sumExit;
double sumExit_log_sumExit = pLogP(sumExit);
codeLength = sumExit_log_sumExit - 2.0 * sum_exit_log_exit + sum_stay_log_stay - allNodes_log_allNodes;
}
/*
* This function implements the 1) procedure of stochastic_greedy_partition(Network &network) function in SeqInfomap.cpp.
*
* 1) in random sequential order, each node is moved to its neighbor module that results in the largest gain of the map eq.
* If no move results in a gain of the map equation, the node stays in its original module.
*
* return: the number of moves.
*/
int Network::move() {
// Generate random sequential order of nodes.
vector<int> randomOrder(nNode);
for (int i = 0; i < nNode; i++)
randomOrder[i] = i;
for (int i = 0; i < nNode; i++) {
int target = R->randInt(nNode - 1);
// swap numbers between i and target.
int tmp = randomOrder[i];
randomOrder[i] = randomOrder[target];
randomOrder[target] = tmp;
}
int numMoved = 0; // the counter for the number of movements.
// Move each node to one of its neighbor modules in random sequential order.
for (int i = 0; i < nNode; i++) {
Node& nd = nodes[randomOrder[i]]; // look at i_th Node of the random sequential order.
int oldMod = nd.ModIdx();
int nModLinks = 0; // The number of links to/from between the current node and other modules.
flowmap outFlowToMod; // <modID, flow> for outFlow...
flowmap inFlowFromMod; // <modID, flow> for inFlow...
// count other modules that are connected to the current node.
// During this counting, aggregate outFlowNotToOldMod and inFlowFromOldMod values.
for (link_iterator linkIt = nd.outLinks.begin(); linkIt != nd.outLinks.end(); linkIt++) {
int newMod = nodes[linkIt->first].ModIdx();
if (outFlowToMod.count(newMod) > 0) {
outFlowToMod[newMod] += beta * linkIt->second;
}
else {
outFlowToMod[newMod] = beta * linkIt->second; // initialization of the outFlow of the current newMod.
inFlowFromMod[newMod] = 0.0;
nModLinks++;
}
}
for (link_iterator linkIt = nd.inLinks.begin(); linkIt != nd.inLinks.end(); linkIt++) {
int newMod = nodes[linkIt->first].ModIdx();
if (inFlowFromMod.count(newMod) > 0) {
inFlowFromMod[newMod] += beta * linkIt->second;
}
else {
outFlowToMod[newMod] = 0.0;
inFlowFromMod[newMod] = beta * linkIt->second;
nModLinks++;
}
}
if (nModLinks != outFlowToMod.size())
cout << "ALERT: nModLinks != outFlowToMod.size() in Network::move()." << endl;
// copy node specific values for easy use and efficiency.
double ndSize = nd.Size(); // p_nd.
double ndTPWeight = nd.TeleportWeight(); // tau_nd.
double ndDanglingSize = nd.DanglingSize();
double oldExitPr1 = modules[oldMod].ExitPr();
double oldSumPr1 = modules[oldMod].SumPr();
double oldSumDangling1 = modules[oldMod].SumDangling();
double oldModTPWeight = modules[oldMod].SumTPWeight();
double additionalTeleportOutFlow = (alpha * ndSize + beta * ndDanglingSize) * (oldModTPWeight - ndTPWeight);
double additionalTeleportInFlow = (alpha * (oldSumPr1 - ndSize) + beta * (oldSumDangling1 - ndDanglingSize)) * ndTPWeight;
// For teleportation and danling nodes.
for (flowmap::iterator it = outFlowToMod.begin(); it != outFlowToMod.end(); it++) {
int newMod = it->first;
if (newMod == oldMod) {
outFlowToMod[newMod] += additionalTeleportOutFlow;
inFlowFromMod[newMod] += additionalTeleportInFlow;
}
else {
outFlowToMod[newMod] += (alpha * ndSize + beta * ndDanglingSize) * modules[newMod].SumTPWeight();
inFlowFromMod[newMod] += (alpha * modules[newMod].SumPr() + beta * modules[newMod].SumDangling()) * ndTPWeight;
}
}
// Calculate flow to/from own module (default value if no links to own module).
double outFlowToOldMod = additionalTeleportOutFlow;
double inFlowFromOldMod = additionalTeleportInFlow;
if (outFlowToMod.count(oldMod) > 0) {
outFlowToOldMod = outFlowToMod[oldMod];
inFlowFromOldMod = inFlowFromMod[oldMod];
}
//////////////////// THE OPTION TO MOVE TO EMPTY MODULE ////////////////
if (modules[oldMod].members.size() > 1 && emptyModules.size() > 0) {
int emptyTarget = emptyModules.back();
outFlowToMod[emptyTarget] = 0.0;
inFlowFromMod[emptyTarget] = 0.0;
nModLinks++;
}
//vector<MoveSummary> moveResults(nModLinks);
MoveSummary currentResult;
MoveSummary bestResult;
double newExitPr1 = oldExitPr1 - nd.ExitPr() + outFlowToOldMod + inFlowFromOldMod;
bestResult.diffCodeLen = 0.0; // This is the default value, if we can't find diffCodeLen < 0, then don't move the node.
for (flowmap::iterator it = outFlowToMod.begin(); it != outFlowToMod.end(); it++) {
int newMod = it->first;
double outFlowToNewMod = it->second;
double inFlowFromNewMod = inFlowFromMod[newMod];
if (newMod != oldMod) {
// copy module specific values...
double oldExitPr2 = modules[newMod].ExitPr();
double oldSumPr2 = modules[newMod].SumPr();
// Calculate status of current investigated movement of the node nd.
currentResult.newModule = newMod;
currentResult.sumPr1 = oldSumPr1 - ndSize; // This should be 0.0, because oldModule will be empty module.
currentResult.sumPr2 = oldSumPr2 + ndSize;
currentResult.exitPr1 = newExitPr1;
currentResult.exitPr2 = oldExitPr2 + nd.ExitPr() - outFlowToNewMod - inFlowFromNewMod;
currentResult.newSumExitPr = sumAllExitPr + newExitPr1 + currentResult.exitPr2 - oldExitPr1 - oldExitPr2;
// Calculate delta_L(M) = L(M)_new - L(M)_old
double delta_allExit_log_allExit = pLogP(currentResult.newSumExitPr) - pLogP(sumAllExitPr);
double delta_exit_log_exit = pLogP(currentResult.exitPr1) + pLogP(currentResult.exitPr2) - pLogP(oldExitPr1) - pLogP(oldExitPr2);
double delta_stay_log_stay = pLogP(currentResult.exitPr1 + currentResult.sumPr1) + pLogP(currentResult.exitPr2 + currentResult.sumPr2) \
- pLogP(oldExitPr1 + oldSumPr1) - pLogP(oldExitPr2 + oldSumPr2);
// delta_L(M) = delta_allExit - 2.0 * delta_exit_log_exit + delta_stay_log_stay.
currentResult.diffCodeLen = delta_allExit_log_allExit - 2.0 * delta_exit_log_exit + delta_stay_log_stay;
if (currentResult.diffCodeLen < bestResult.diffCodeLen) {
// we need to update bestResult with currentResult.
bestResult.diffCodeLen = currentResult.diffCodeLen;
bestResult.newModule = currentResult.newModule;
bestResult.sumPr1 = currentResult.sumPr1;
bestResult.sumPr2 = currentResult.sumPr2;
bestResult.exitPr1 = currentResult.exitPr1;
bestResult.exitPr2 = currentResult.exitPr2;
bestResult.newSumExitPr = currentResult.newSumExitPr;
}
}
}
// Make best possible move for the current node nd.
if (bestResult.diffCodeLen < 0.0) {
// update related to newMod...
int newMod = bestResult.newModule;
if (modules[newMod].NumMembers() == 0) {
newMod = emptyModules.back();
emptyModules.pop_back();
nEmptyMod--;
nModule++;
}
nd.setModIdx(newMod);
modules[newMod].increaseNumMembers();
modules[newMod].setExitPr(bestResult.exitPr2);
modules[newMod].setSumPr(bestResult.sumPr2);
modules[newMod].setStayPr(bestResult.exitPr2 + bestResult.sumPr2);
modules[newMod].addSumTPWeight(ndTPWeight);
if (nd.IsDangling()) {
modules[newMod].addSumDangling(ndSize);
modules[oldMod].minusSumDangling(ndSize);
}
// update related to the oldMod...
modules[oldMod].decreaseNumMembers();
modules[oldMod].setExitPr(bestResult.exitPr1);
modules[oldMod].setSumPr(bestResult.sumPr1);
modules[oldMod].setStayPr(bestResult.exitPr1 + bestResult.sumPr1);
modules[oldMod].minusSumTPWeight(ndTPWeight);
if (modules[oldMod].NumMembers() == 0) {
nEmptyMod++;
nModule--;
emptyModules.push_back(oldMod);
}
sumAllExitPr = bestResult.newSumExitPr;
codeLength += bestResult.diffCodeLen;
numMoved++;
}
}
if (nodes.size() != nModule + nEmptyMod) {
cout << "Something wrong!! nodes.size() != nModule + nEmptyMod." << endl;
}
return numMoved;
}
/*
* This function implements a prioritized version of move() above.
*
* return: the number of movements.
*/
int Network::prioritize_move(double vThresh) {
int nActive = activeNodes.size();
int nNextActive = 0; // This is a counter for the number of active nodes on next iteration.
// Generate random sequential order of active nodes.
vector<int> randomOrder(nActive);
for (int i = 0; i < nActive; i++)
randomOrder[i] = activeNodes[i];
for (int i = 0; i < nActive; i++) {
int target = R->randInt(nActive - 1);
// swap numbers between i and target.
int tmp = randomOrder[i];
randomOrder[i] = randomOrder[target];
randomOrder[target] = tmp;
}
int numMoved = 0;
// Move each node to one of its neighbor modules in random sequential order.
for (int i = 0; i < nActive; i++) {
Node& nd = nodes[randomOrder[i]]; // look at i_th Node of the random sequential order.
int oldMod = nd.ModIdx();
int nModLinks = 0; // The number of links to/from between the current node and other modules.
flowmap outFlowToMod; // <modID, flow> for outFlow...
flowmap inFlowFromMod; // <modID, flow> for inFlow...
// count other modules that are connected to the current node.
// During this counting, aggregate outFlowNotToOldMod and inFlowFromOldMod values.
for (link_iterator linkIt = nd.outLinks.begin(); linkIt != nd.outLinks.end(); linkIt++) {
int newMod = nodes[linkIt->first].ModIdx();
if (outFlowToMod.count(newMod) > 0) {
outFlowToMod[newMod] += beta * linkIt->second;
}
else {
outFlowToMod[newMod] = beta * linkIt->second; // initialization of the outFlow of the current newMod.
inFlowFromMod[newMod] = 0.0;
nModLinks++;
}
}
for (link_iterator linkIt = nd.inLinks.begin(); linkIt != nd.inLinks.end(); linkIt++) {
int newMod = nodes[linkIt->first].ModIdx();
if (inFlowFromMod.count(newMod) > 0) {
inFlowFromMod[newMod] += beta * linkIt->second;
}
else {
outFlowToMod[newMod] = 0.0;
inFlowFromMod[newMod] = beta * linkIt->second;
nModLinks++;
}
}
if (nModLinks != outFlowToMod.size())
cout << "ALERT: nModLinks != outFlowToMod.size() in Network::move()." << endl;
// copy node specific values for easy use and efficiency.
double ndSize = nd.Size(); // p_nd.
double ndTPWeight = nd.TeleportWeight(); // tau_nd.
double ndDanglingSize = nd.DanglingSize();
double oldExitPr1 = modules[oldMod].ExitPr();
double oldSumPr1 = modules[oldMod].SumPr();
double oldSumDangling1 = modules[oldMod].SumDangling();
double oldModTPWeight = modules[oldMod].SumTPWeight();
double additionalTeleportOutFlow = (alpha * ndSize + beta * ndDanglingSize) * (oldModTPWeight - ndTPWeight);
double additionalTeleportInFlow = (alpha * (oldSumPr1 - ndSize) + beta * (oldSumDangling1 - ndDanglingSize)) * ndTPWeight;
// For teleportation and danling nodes.
for (flowmap::iterator it = outFlowToMod.begin(); it != outFlowToMod.end(); it++) {
int newMod = it->first;
if (newMod == oldMod) {
outFlowToMod[newMod] += additionalTeleportOutFlow;
inFlowFromMod[newMod] += additionalTeleportInFlow;
}
else {
outFlowToMod[newMod] += (alpha * ndSize + beta * ndDanglingSize) * modules[newMod].SumTPWeight();
inFlowFromMod[newMod] += (alpha * modules[newMod].SumPr() + beta * modules[newMod].SumDangling()) * ndTPWeight;
}
}
// Calculate flow to/from own module (default value if no links to own module).
double outFlowToOldMod = additionalTeleportOutFlow;
double inFlowFromOldMod = additionalTeleportInFlow;
if (outFlowToMod.count(oldMod) > 0) {
outFlowToOldMod = outFlowToMod[oldMod];
inFlowFromOldMod = inFlowFromMod[oldMod];
}
//////////////////// THE OPTION TO MOVE TO EMPTY MODULE ////////////////
if (modules[oldMod].members.size() > 1 && emptyModules.size() > 0) {
int emptyTarget = emptyModules.back();
outFlowToMod[emptyTarget] = 0.0;
inFlowFromMod[emptyTarget] = 0.0;
nModLinks++;
}
MoveSummary currentResult;
MoveSummary bestResult;
double newExitPr1 = oldExitPr1 - nd.ExitPr() + outFlowToOldMod + inFlowFromOldMod;
bestResult.diffCodeLen = 0.0; // This is the default value, if we can't find diffCodeLen < 0, then don't move the node.
for (flowmap::iterator it = outFlowToMod.begin(); it != outFlowToMod.end(); it++) {
int newMod = it->first;
double outFlowToNewMod = it->second;
double inFlowFromNewMod = inFlowFromMod[newMod];
if (newMod != oldMod) {
// copy module specific values...
double oldExitPr2 = modules[newMod].ExitPr();
double oldSumPr2 = modules[newMod].SumPr();
// Calculate status of current investigated movement of the node nd.
currentResult.newModule = newMod;
currentResult.sumPr1 = oldSumPr1 - ndSize; // This should be 0.0, because oldModule will be empty module.
currentResult.sumPr2 = oldSumPr2 + ndSize;
currentResult.exitPr1 = newExitPr1;
currentResult.exitPr2 = oldExitPr2 + nd.ExitPr() - outFlowToNewMod - inFlowFromNewMod;
currentResult.newSumExitPr = sumAllExitPr + newExitPr1 + currentResult.exitPr2 - oldExitPr1 - oldExitPr2;
// Calculate delta_L(M) = L(M)_new - L(M)_old
double delta_allExit_log_allExit = pLogP(currentResult.newSumExitPr) - pLogP(sumAllExitPr);
double delta_exit_log_exit = pLogP(currentResult.exitPr1) + pLogP(currentResult.exitPr2) - pLogP(oldExitPr1) - pLogP(oldExitPr2);
double delta_stay_log_stay = pLogP(currentResult.exitPr1 + currentResult.sumPr1) + pLogP(currentResult.exitPr2 + currentResult.sumPr2) \
- pLogP(oldExitPr1 + oldSumPr1) - pLogP(oldExitPr2 + oldSumPr2);
// delta_L(M) = delta_allExit - 2.0 * delta_exit_log_exit + delta_stay_log_stay.
currentResult.diffCodeLen = delta_allExit_log_allExit - 2.0 * delta_exit_log_exit + delta_stay_log_stay;
if (currentResult.diffCodeLen < bestResult.diffCodeLen) {
// we need to update bestResult with currentResult.
bestResult.diffCodeLen = currentResult.diffCodeLen;
bestResult.newModule = currentResult.newModule;
bestResult.sumPr1 = currentResult.sumPr1;
bestResult.sumPr2 = currentResult.sumPr2;
bestResult.exitPr1 = currentResult.exitPr1;
bestResult.exitPr2 = currentResult.exitPr2;
bestResult.newSumExitPr = currentResult.newSumExitPr;
}
}
}
// Make best possible move for the current node nd.
//if (bestResult.diffCodeLen < 0.0) {
if (bestResult.diffCodeLen < vThresh) {
// update related to newMod...
int newMod = bestResult.newModule;
if (modules[newMod].NumMembers() == 0) {
newMod = emptyModules.back();
emptyModules.pop_back();
nEmptyMod--;
nModule++;
}
nd.setModIdx(newMod);
modules[newMod].increaseNumMembers();
modules[newMod].setExitPr(bestResult.exitPr2);
modules[newMod].setSumPr(bestResult.sumPr2);
modules[newMod].setStayPr(bestResult.exitPr2 + bestResult.sumPr2);
modules[newMod].addSumTPWeight(ndTPWeight);
if (nd.IsDangling()) {
modules[newMod].addSumDangling(ndSize);
modules[oldMod].minusSumDangling(ndSize);
}
// update related to the oldMod...
modules[oldMod].decreaseNumMembers();
modules[oldMod].setExitPr(bestResult.exitPr1);
modules[oldMod].setSumPr(bestResult.sumPr1);
modules[oldMod].setStayPr(bestResult.exitPr1 + bestResult.sumPr1);
modules[oldMod].minusSumTPWeight(ndTPWeight);
if (modules[oldMod].NumMembers() == 0) {
nEmptyMod++;
nModule--;
emptyModules.push_back(oldMod);
}
sumAllExitPr = bestResult.newSumExitPr;
codeLength += bestResult.diffCodeLen;
numMoved++;
// update activeNodes and isActives vectors.
// We have to add the following nodes in activeNodes: neighbors, members in oldMod & newMod.
for (link_iterator linkIt = nd.outLinks.begin(); linkIt != nd.outLinks.end(); linkIt++)
isActives[linkIt->first] = 1; // set as an active nodes.
for (link_iterator linkIt = nd.inLinks.begin(); linkIt != nd.inLinks.end(); linkIt++)
isActives[linkIt->first] = 1; // set as an active nodes.
}
}
vector<int>().swap(activeNodes);
for (int i = 0; i < isActives.size(); i++) {
if (isActives[i] == 1) {
activeNodes.push_back(i);
isActives[i] = 0; // reset the flag of isActives[i].
}
}
if (nodes.size() != nModule + nEmptyMod) {
cout << "Something wrong!! nodes.size() != nModule + nEmptyMod." << endl;
}
return numMoved;
}
/*
* + This is a parallel implementation of Network::move() function via OpenMP.
* We will loose the criteria for simple implementation and it may help to avoid local optima problem.
*/
int Network::parallelMove(int numTh, double& tSequential) {
struct timeval tStart, tEnd;
gettimeofday(&tStart, NULL);
// Generate random sequential order of nodes.
vector<int> randomOrder(nNode);
for (int i = 0; i < nNode; i++)
randomOrder[i] = i;
for (int i = 0; i < nNode; i++) {
int target = R->randInt(nNode - 1);
// swap numbers between i and target.
int tmp = randomOrder[i];
randomOrder[i] = randomOrder[target];
randomOrder[target] = tmp;
}
int numMoved = 0;
omp_set_num_threads(numTh);
const int emptyTarget = nNode + 1; // This will be an indicator of moving to emptyModule.
gettimeofday(&tEnd, NULL);
tSequential += elapsedTimeInSec(tStart, tEnd);
#pragma omp parallel for
// Move each node to one of its neighbor modules in random sequential order.
for (int i = 0; i < nNode; i++) {
Node& nd = nodes[randomOrder[i]]; // look at i_th Node of the random sequential order.
int oldMod = nd.ModIdx();
int nModLinks = 0; // The number of links to/from between the current node and other modules.
flowmap outFlowToMod; // <modID, flow> for outFlow...