-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathEO_functions_unipartite.cpp
1085 lines (945 loc) · 36.4 KB
/
EO_functions_unipartite.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
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
/*
* IBN_bi.cpp
*
* Created on: 12 feb. 2018
* Author: mariapalazzi
*/
////mex -largeArrayDims IBN_bi.cpp
//#include <//mex.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstdlib>
#include <ctime>
#include <istream>
#include <string>
#include <sstream>
#include <fstream>
#include <cmath>
#include <random>
#include <valarray>
#include <cfloat>
#include <cstdio>
#include <numeric>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/functional.h>
#include <pybind11/stl_bind.h>
//#include "EO_aux_functions_opt1.hpp"
#define MIN(X, Y) (((X) < (Y)) ? (X) : (Y))
#define MAX(X, Y) (((X) < (Y)) ? (Y) : (X))
using namespace std;
//void log_file( const string &text )
//{
// if (false) {
// ofstream log_file("log_file.log", ios_base::out | ios_base::app );
// log_file << text << endl;
// }
//}
//char outputString[500];
//char * log_prefix_global;
vector<int> myargsort(vector<double> x){
int la = x.size();
vector<double> c = x;
vector<int> y (la);
int i=0,ii,ti;
for(i=0;i<la;i++)
y[i]=i;
i=0;
double td;
while (i<(la-1)){
ii=i+1;
while (ii<(la)){
if (c[ii]<c[i]){
td = c[i];
c[i] = c[ii];
c[ii] = td;
ti = y[i];
y[i] = y[ii];
y[ii] = ti;
}
ii++;
}
i++;
}
return y;
}
vector<int> indexes_sorted_vector(vector<double> & x){
std::vector<int> vs(x.size());
std::iota(vs.begin(), vs.end(), 0);
auto comparator = [&x](int a, int b){ return x[a] < x[b]; };
std::sort(vs.begin(), vs.end(), comparator);
//for (int i=0; i<5; i++){
// cout<< vs[i] <<endl;
//}
return vs;
}
void shuffle_partitions(vector<int> & labels){
int half_size = round(labels.size()/2);
for (int i=0; i<labels.size();i++){
if (i < half_size){
labels[i]=1;
}else{
labels[i]=2;
}
}
/* Shuffling the labels*/
random_device rd; //non fixed seed
mt19937 gen(rd());
shuffle ( labels.begin(), labels.end(), gen);
//log_file("\tleaving bipartition function");
}
/* Function to perform the bipartition of a given array.
It receives an NxM matrix and two empty vectors where the label nodes will be storage*/
void bipartition(
vector<vector<int> > & input_matrix,
vector<int> & label_col,
int nextBlockId){
if(label_col.size()>2){
int rnd_label = 0;
int num_label1 = 0;
int num_label2 = 0;
for (int i=0 ; i<label_col.size() ; i++){
rnd_label = rand() % 2;
label_col[i] = rand() % 2 + nextBlockId;
if(rnd_label == 1){
num_label1 ++;
}else{
num_label2 ++;
}
}
if(num_label1 == 0 || num_label2 == 0){
shuffle_partitions(label_col);
}
}else{
label_col[0] = nextBlockId;
label_col[1] = nextBlockId + 1;
}
// int N_rows=input_matrix.size();
// int N_cols=input_matrix[0].size();
// vector<int> label_row2 (N_rows,0);
// vector<int> label_col2 (N_cols,0);
// /* Splitting in two and assigning the labels*/
// for (unsigned int i=0; i<(N_rows);i++){
// if (i<(N_rows/2))
// label_row2[i]=1;
// else
// label_row2[i]=2;
// }
// for (unsigned int i=0; i<(N_cols);i++){
// if (i<N_cols/2)
// label_col2[i]=1;
// else
// label_col2[i]=2;
// }
// /* Shuffling the labels*/
// random_device rd; //non fixed seed
// mt19937 gen(rd());
// shuffle ( label_col2.begin(), label_col2.end(), gen);
// shuffle ( label_row2.begin(), label_row2.end(), gen);
//
// label_row=label_row2;
// label_col=label_col2;
}
void lambdas_inblock(
vector<vector<int> > & input_matrix,
vector<int> & k_cols,
vector<int> label_cols,
vector<double>& lambda_cols,
int max_number_blocks){
int N_cols=input_matrix[0].size();
int N_rows=input_matrix.size();
lambda_cols= vector<double>(N_cols,0);
// vector<int>::iterator it;
//vector<int> k_cols(N_cols,0);
//vector<int> k_rows(N_rows,0);
// getting current number of blocks
//int max_number_blocks_cols=*max_element(label_cols.begin(), label_cols.end());
//int max_number_blocks_rows=*max_element(label_rows.begin(), label_rows.end());
//int max_number_blocks = MAX(max_number_blocks_cols,max_number_blocks_rows);
vector<vector<int> > list_of_blocks_cols(max_number_blocks+1,vector<int>());
//sprintf(outputString,"fprintf('inside current_block->first_step->lambdas_inblock_rows->start (%lu,%lu), max_blocks %i\\n');",
// label_rows.size(),
// label_cols.size(),
// max_number_blocks);
//mexEvalString(outputString);
//creating a vector that separate the nodes according to the block the belong to columns
// for (int i=0; i<= max_number_blocks; i++){
// vector<int> appending_cols_nodes;
// for (int j = 0; j < label_cols.size(); ++j){
// if (label_cols[j]==i){
// appending_cols_nodes.push_back(j);
// }
// }
// list_of_blocks_cols.push_back(appending_cols_nodes);
// }
for (int j = 0; j < label_cols.size(); ++j){
list_of_blocks_cols[label_cols[j]].push_back(j);
}
//sprintf(outputString,
// "fprintf('inside current_block->first_step->lambdas_inblock_rows->list_of_blocks_cols size %lu\\n');",
// label_cols.size());
//mexEvalString(outputString);
//rows
// for (unsigned int i=0; i<= max_number_blocks; i++){
// vector<int> appending_rows_nodes;
// for (unsigned int j = 0; j < label_rows.size(); ++j){
// if (label_rows[j]==i){
// appending_rows_nodes.push_back(j);
// }
// }
// list_of_blocks_rows.push_back(appending_rows_nodes);
// }
//sprintf(outputString,
// "fprintf('inside current_block->first_step->lambdas_inblock_rows->list_of_blocks_rows size %lu\\n');",
// label_rows.size());
//mexEvalString(outputString);
//getting the k degrees <--- this is now an input parameter, this does not change in all the iterations
// for (unsigned int i = 0; i < label_rows.size(); ++i) {
// for (unsigned int j = 0; j < label_cols.size(); ++j) {
// k_rows[i]+=input_matrix[i][j];
// k_cols[j]+=input_matrix[i][j];
// }
// }
//computing the column nodes contribution (pair overlap)
for (int l=0; l<list_of_blocks_cols.size(); l++){
int size_blocks_cols=(double)list_of_blocks_cols[l].size();
for (int i=0; i<size_blocks_cols; i++){
int ii = list_of_blocks_cols[l][i];
for (int j=0; j<size_blocks_cols; j++){
int jj =list_of_blocks_cols[l][j];
double PO_col_i=0;
double normalization = ((double)(k_cols[jj]))*((double)(size_blocks_cols-1.0));//double normalization = (2/(N_rows+N_cols))*(1/((double)(k_cols[jj])*(double)(size_blocks_cols-1.0)));
if ( (k_cols[ii]>=k_cols[jj])&(k_cols[jj]>0) & (normalization!=0.0)&(ii!=jj)){
for (int k=0; k<list_of_blocks_cols[l].size(); k++){
int kk = list_of_blocks_cols[l][k];
if ((input_matrix[kk][ii]*input_matrix[kk][jj])==1){
PO_col_i++;
}
}
if (k_cols[ii]==k_cols[jj]){
double null_model = ((double)(k_cols[ii]*k_cols[jj]))/((double)(N_rows));
PO_col_i=(PO_col_i-null_model)/(2.0*normalization);
lambda_cols[ii]+=PO_col_i;
} else{
double null_model = ((double)(k_cols[ii]*k_cols[jj]))/((double)(N_rows));
PO_col_i=(PO_col_i-null_model)/normalization;
lambda_cols[ii]+=PO_col_i;
}
}
}
}
}
//sprintf(outputString,"fprintf('inside current_block->first_step->lambdas_inblock_cols \\n');");
//mexEvalString(outputString);
//sprintf(outputString,"fprintf('inside current_block->first_step->lambdas_inblock_rows \\n');");
//mexEvalString(outputString);
}
void lambdas_modularity(
vector<vector<int> > & input_matrix,
vector<int> & k_cols,
vector<int> label_cols,
vector<double>& lambda_cols,
int max_number_blocks){
int N_cols=input_matrix[0].size();
vector<int>::iterator it;
vector<int> blocks_cols(label_cols);
lambda_cols=vector<double>(N_cols,0);
vector<double> kappa_cols(N_cols,0);
//vector<int> k_rows(N_rows,0);
//vector<int> k_cols(N_cols,0);
//obtaining current number of blocks
//cols
sort(blocks_cols.begin(), blocks_cols.end());
it=unique(blocks_cols.begin(), blocks_cols.end());
blocks_cols.resize(distance(blocks_cols.begin(),it) );
//vector<double> links_blocks_cols(*max_element(blocks_cols.begin(), blocks_cols.end()),0);
vector<double> links_blocks_cols(max_number_blocks,0);
double total_links=0.;
//getting the total links
for (unsigned int i = 0; i < label_cols.size(); ++i) {
for (unsigned int j = 0; j < label_cols.size(); ++j) {
total_links+=input_matrix[i][j];
}
}
// total_links = accumulate(k_cols.begin(), k_cols.end(), 0.0);
// getting the kappa's
for (unsigned int i = 0; i < label_cols.size(); ++i) {
for (unsigned int j = 0; j < label_cols.size(); ++j) {
if ((input_matrix[i][j]==1) & (label_cols[i]==label_cols[j])){
kappa_cols[j]+=1./(double)k_cols[j];
}
}
}
// getting the a_r(i)'s
for (unsigned int i = 0; i < label_cols.size(); ++i) {
links_blocks_cols[label_cols[i]-1]+=k_cols[i]/total_links;
}
//getting the lambda fitness
for (unsigned int i = 0; i < label_cols.size(); ++i) {
lambda_cols[i]=kappa_cols[i] - links_blocks_cols[label_cols[i]-1];
}
}
double calculate_inblock_nestedness(
vector<vector<int> > & input_matrix,
vector<double> lambda_cols){
double I;
int N_cols=input_matrix[0].size();
double i_col=0;
//for (unsigned int i=0; i<N_cols; i++){
// i_col+=lambda_cols[i];
//}
i_col = accumulate(lambda_cols.begin(), lambda_cols.end(), 0.0);
//for (unsigned int j=0; j<N_rows; j++){
// i_row+=lambda_rows[j];
//}
I=(2.0/((double)N_cols))*(i_col);
//log_file("\tleaving calculate_inblock_nestedness function");
//log_file("\t-- I: "+ to_string(I));
return I;
}
double calculate_modularity(
vector<vector<int> > & input_matrix,
vector<int> & k_cols,
vector<double> lambda_cols){
double Q;
double q_cols=0;
int N_cols=input_matrix[0].size();
//vector<int> k_rows(N_rows,0);
//vector<int> k_cols(N_cols,0);
double total_links=0;
for (unsigned int i = 0; i < N_cols ; ++i) {
for ( int j = 0; j < N_cols; ++j) {
total_links+=input_matrix[i][j];
}
}
// total_links = accumulate(k_rows.begin(), k_rows.end(), 0.0);
for (int i=0; i<N_cols; i++){
q_cols+=k_cols[i]*lambda_cols[i];
}
Q=(q_cols)/(total_links);
// cout<<Q<<endl;
//log_file("\tleaving calculate_modularity function");
return Q;
}
void lambda_i(
vector<vector<int> > & input_matrix,
vector<int> & k_cols,
vector<int> label_cols,
vector<double> &lambda_cols,
int max_number_blocks,
bool ibn){
if (ibn==true){
/* Function that calculate the fitness contributions of nodes (lambda_i) for the in-block nestedness*/
lambdas_inblock(
input_matrix,
k_cols,
label_cols,
lambda_cols,
max_number_blocks);
}else {
/* Function that calculate the fitness contributions of nodes (lambda_i) for the modularity*/
lambdas_modularity(
input_matrix,
k_cols,
label_cols,
lambda_cols,
max_number_blocks);
}
//log_file("\tleaving lambda_i function");
}
vector<double> call_lambda_i(
vector<vector<int> > & input_matrix,
vector<int> & k_cols,
vector<int> label_cols,
int max_number_blocks,
bool ibn){
vector<double> out;
vector<double> lambda_cols(label_cols.size(),0);
lambda_i(
input_matrix,
k_cols,
label_cols,
lambda_cols,
max_number_blocks,
ibn);
out=lambda_cols;
return out;
}
double calculate_Fitness(
vector<vector<int> > & input_matrix,
vector<int> & k_cols,
vector<double> lambda_cols,
bool ibn){
//log_file("\tentering calculate_Fitness function");
/* Calculate the EO metric of the whole network */
double metric;
if (ibn==true) {
// in block nestedness
metric=calculate_inblock_nestedness(input_matrix, lambda_cols);
}else{ // modularity
metric=calculate_modularity(
input_matrix,
k_cols,
lambda_cols);
}
// double (* calculate_genericFitness)(vector<vector<int> > &,vector<int> &,vector<int> &,vector<double>,vector<double>);
//
// metric=calculate_genericFitness(
// input_matrix,
// k_cols,
// k_rows,
// lambda_cols,
// lambda_rows);
//log_file("\t++ Metric: "+ to_string(metric));
//log_file("\tleaving calculate_Fitness function");
return metric;
}
/*Obtain the node that is going to be moved from its block, based on
probability distribution. tau-EO like in Duch et al 2005.*/
int low_fitness_node(
vector<double> lambda_cols) {
int low_node;
int N=lambda_cols.size();
double tau = 1.+1./log(N); //tau-exponent
vector<double> probabilities(N,0);
double p_norm=0;
vector<int> lambda_sorted(N);
// generate distribution of probabilities
for (int i=0; i<N ; i++){
probabilities[i]= pow(i+1,-tau);
p_norm+=probabilities[i];
}
for (int j=0; j<N; j++){
probabilities[j]=probabilities[j]/p_norm;
}
discrete_distribution<int> distribution(probabilities.begin(), probabilities.end());
random_device rd;
mt19937 gen(rd());
// sorting the lambda_stacked vector
lambda_sorted = indexes_sorted_vector(lambda_cols);
low_node=lambda_sorted[distribution(gen)];
//log_file("\tleaving low_fitness_node function");
return low_node;
}
void first_step(
vector<vector<int> > &input_matrix,
vector<int> & k_cols,
vector<int> label_cols,
int Malpha,
vector<int> &label_cols_new,
int blockId1,
int blockId2,
int max_number_blocks,
bool ibn){
/*Given a matrix arbitrarily partitioned in two, move the nodes with "lowest" fitness
until optimal Ieo is reached. The stopping criteria for the optimal Ieo is given by
Malpha.*/
//log_file("****entering first_step function");
vector<double> lambda_cols(label_cols.size(),0);
vector<double> lambda_cols_copy(label_cols.size(),0);
vector<int> label_temporal_cols;
double Io=-1.0;
double In=-1.0;
int alpha=0;
int max_alpha=0;
int low_node=0;
//moving the nodes of a current partition from one block to the other
// int c=0;
//if(Malpha > 50){
// mexErrMsgTxt("Malpha error inside recursive_step\n");
//}
//compute the first lambda_i
label_temporal_cols = label_cols;
//sprintf(outputString,"fprintf('inside current_block->first_step->alpha %i, Malpha %i, max_blocks %i\\n');",
// alpha,Malpha,max_number_blocks);
//mexEvalString(outputString);
lambda_i(
input_matrix,
k_cols,
label_temporal_cols,
lambda_cols,
max_number_blocks,
ibn);
while (alpha<Malpha){
label_temporal_cols = label_cols;
lambda_cols_copy = lambda_cols;
//sprintf(outputString,"fprintf('inside current_block->first_step->alpha %i, Malpha %i \\n');",alpha,Malpha);
//mexEvalString(outputString);
//sprintf(outputString,"fprintf('inside current_block->first_step->lambda_i \\n');");
//mexEvalString(outputString);
low_node=low_fitness_node(lambda_cols);
//sprintf(outputString,"fprintf('inside current_block->first_step->low_fitness_node \\n');");
//mexEvalString(outputString);
//TODO: do this update of lambda_i's for IBN in a nicer way
// changing the node label
// test instead of the if, replace the operation by using booleans
//consider low_node ranges from 0 to (N+M)-1
if (label_temporal_cols[low_node]==blockId1){
label_temporal_cols[low_node]=blockId2;
}else {
label_temporal_cols[low_node]=blockId1;
}
lambda_i(
input_matrix,
k_cols,
label_temporal_cols,
lambda_cols,
max_number_blocks,
ibn);
//sprintf(outputString,"fprintf('inside current_block->first_step->lambda_i_2 \\n');");
//mexEvalString(outputString);
In = calculate_Fitness(
input_matrix,
k_cols,
lambda_cols,
ibn);
//sprintf(outputString,"fprintf('inside current_block->first_step->calculate_fitness \\n');");
//mexEvalString(outputString);
int suma_col_1=0,suma_col_2=0;
for(int i=0; i<label_temporal_cols.size();i++){
if (label_temporal_cols[i]==blockId1)
suma_col_1++;
else
suma_col_2++;
}
//sprintf(outputString,"fprintf('inside current_block->first_step->suma_row_1 (%i,%i,%i,%i) \\n');",
// suma_col_1,
// suma_col_2,
// suma_row_1,
// suma_row_2);
//mexEvalString(outputString);
// if an increase is found, save the changes
//if ((In>Io) & (suma_col_1>1) & (suma_col_2>1)& (suma_row_1>1) & (suma_row_2>1)){
if (In>Io){
Io=In;
label_cols = label_temporal_cols;
alpha=0;
}else{
lambda_cols = lambda_cols_copy; //this and the load of the copy of label cols has the same
//objective, just thath the variable names operate differnt, the labels_temporal_cols logic
//shold be improved
alpha++;
}
if(max_alpha<alpha){
max_alpha = alpha;
// sprintf(outputString,"fprintf('%s: max_alpha %i of Malpha %i \\n');",log_prefix_global,max_alpha,Malpha);
// mexEvalString(outputString);
}
}
label_cols_new = label_cols;
//log_file("**** ** New value of In: " + to_string(In));
}
void update_partitions_labels(
vector<int> &total_in_label_cols,
vector<int> label_partition_cols,
int current_block){
//log_file("\tentering update_partitions_labels function");
int j=0;
//updating the label vector with the new partitions
for (unsigned int i=0; i<total_in_label_cols.size(); i++){
if (total_in_label_cols[i]==current_block){
total_in_label_cols[i]=label_partition_cols[j];
j++;
}
}
//log_file("\tleaving update_partitions_labels function");
}
//typedef struct partition_component {
// vector<int> row_ids;
// vector<int> col_ids;
// int pos_partition_rows;
// int pos_partition_cols;
//} partition_component;
vector<int> recursive_step(
vector<vector<int> > & adjacency_matrix,
vector<int> & k_cols,
double alpha_scale_factor,
int repetitions,
bool ibn){
//log_file("\tentering recursive_step function");
vector<vector<int> > out;
int N_cols=adjacency_matrix[0].size();
// int Malpha=int((N_cols+N_rows)*alpha_scale_factor);
vector<int> col_labels_out(N_cols,1);
vector<int > labels_temp_col(N_cols,1);
vector<double > lambda_cols(N_cols,0);
//printf("Begin recursive_step function and calling other functions %i repetitions\n",repetitions);
////sprintf(outputString,"fprintf('Begin recursive_step function and calling other functions %i repetitions\\n');",repetitions);
////mexEvalString(outputString);
//if(Malpha > 50){
// //mexErrMsgTxt("Malpha error recursive_step\n");
//}
//std::deque<partition_component> partitions_to_explore();
//vector<vector<int>>current_parition;
double If=-DBL_MAX;
double I;
int current_block=1;
for (int i=0; i<repetitions; i++){
////sprintf(outputString,"fprintf('Begin recursive_step function and calling other functions %i repetitions\\n');",i);
////mexEvalString(outputString);
//sprintf(outputString,"fprintf('%s: Initializing 1st block\\n');", log_prefix_global);
// //mexEvalString(outputString);
double In=-DBL_MAX;
vector<int> labels_final_col(N_cols,1);
int max_number_blocks_cols=*max_element(labels_final_col.begin(), labels_final_col.end());
int max_number_blocks = MAX(max_number_blocks_cols,max_number_blocks_cols) + 1;
lambda_i(adjacency_matrix,
k_cols,
labels_final_col,
lambda_cols,
max_number_blocks,
ibn);
I = calculate_Fitness(
adjacency_matrix,
k_cols,
lambda_cols,
ibn);
// col_labels_out = labels_final_col;
// row_labels_out = labels_final_row;
//while (current_block <= *max_element(labels_final_col.begin(), labels_final_col.end())){
while (current_block <= max_number_blocks){
//Building the new matrix from one of the two partitions
//int number_of_blocks=*max_element(labels_final_col.begin(), labels_final_col.end());
int number_of_blocks=max_number_blocks;
vector<int> indices_col;
for (int i=0; i<labels_final_col.size(); i++){
if (labels_final_col[i]==current_block){
indices_col.push_back(i);
}
}
if( indices_col.size()>1){
//keep a copy of old labels
vector<int> old_labels_final_col = labels_final_col;
vector<vector<int> > sub_matrix; //new matrix
vector<int> sub_matrix_k_cols(indices_col.size(),0);
for (int j=0; j<indices_col.size(); j++){
vector<int> aux;
for (int k=0; k<indices_col.size(); k++){
aux.push_back(adjacency_matrix[indices_col[j]][indices_col[k]]);
}
sub_matrix.push_back (aux);
}
//calculate degress
for (int i_cols = 0; i_cols < indices_col.size(); i_cols++) {
for (int j_cols = 0; j_cols < indices_col.size(); j_cols ++) {
sub_matrix_k_cols[j_cols]+=sub_matrix[i_cols][j_cols];
}
}
//applying the first step function to the new matrix
int n_col=int(sub_matrix[0].size());
int malpha2=int((n_col)*alpha_scale_factor);
int newBlockId = number_of_blocks + 1;
vector<int> labels_cols(indices_col.size(),0);
bipartition(sub_matrix,labels_cols,newBlockId);
max_number_blocks = newBlockId + 1 + 1;
////sprintf(outputString,"fprintf('inside current_block->bipartition\\n');");
////mexEvalString(outputString);
//if(malpha2 > 50){
// //mexErrMsgTxt("Malpha error current_block->recursive_step\n");
//}
first_step(
sub_matrix,
sub_matrix_k_cols,
labels_cols,
malpha2,
labels_cols,
newBlockId,
newBlockId + 1,
max_number_blocks,
ibn);
////sprintf(outputString,"fprintf('inside current_block->first_step\\n');");
////mexEvalString(outputString);
update_partitions_labels(
labels_final_col,
labels_cols,
current_block);
////sprintf(outputString,"fprintf('inside current_block->update_partitions_labels\\n');");
////mexEvalString(outputString);
lambda_i(adjacency_matrix,
k_cols,
labels_final_col,
lambda_cols,
max_number_blocks,
ibn);
////sprintf(outputString,"fprintf('inside current_block->lambda_i\\n');");
////mexEvalString(outputString);
In = calculate_Fitness(
adjacency_matrix,
k_cols,
lambda_cols,ibn);
//sprintf(outputString,"fprintf('%s: New fitness = %f, max fitness %f\\n');",log_prefix_global,(double)In,(double)I);
//mexEvalString(outputString);
////log_file("New value of In " + to_string(In) );
if (In>I){
I=In;
//labels_temp_col = vector<int>(labels_final_col);
//labels_temp_row = vector<int>(labels_final_row);
//sprintf(outputString,"fprintf('%s: Bipartition accepted, new fitness improved = %f\\n');",log_prefix_global,(double)I);
//mexEvalString(outputString);
}else{
labels_final_col = old_labels_final_col;
// //sprintf(outputString,"fprintf('%s: Rejecting bi-partition\\n');",log_prefix_global);
// //mexEvalString(outputString);
}
}
current_block++;
//recompute the maximum number of blocks
max_number_blocks_cols=*max_element(labels_final_col.begin(), labels_final_col.end());
max_number_blocks = MAX(max_number_blocks_cols,max_number_blocks_cols) + 1;
}
if (I > If){
If = I;
col_labels_out = labels_final_col;
//printf("new Io = %f\n",Io);
}
}
int min_block;
int min_block_col=*min_element(col_labels_out.begin(), col_labels_out.end());
min_block = min_block_col;
min_block=(min_block-1);
for (int k=0; k<col_labels_out.size();k++){
col_labels_out[k]=(col_labels_out[k]-min_block);
}
// int max_number_blocks_cols=*max_element(col_labels_out.begin(), col_labels_out.end());
// int max_number_blocks_rows=*max_element(row_labels_out.begin(), row_labels_out.end());
// int max_number_blocks = MAX(max_number_blocks_cols,max_number_blocks_rows) + 1;
// lambda_i(adjacency_matrix,
// k_cols,
// k_rows,
// col_labels_out,
// row_labels_out,
// lambda_cols,
// lambda_rows,
// max_number_blocks,
// ibn);
// double In = calculate_Fitness(
// adjacency_matrix,
// k_cols,
// k_rows,
// lambda_cols,
// lambda_rows,ibn);
// //sprintf(outputString,"fprintf('%s: final Q value = %f, max_number_blocks = %i\\n');",
// log_prefix_global,(double)In,max_number_blocks);
// //mexEvalString(outputString);
//log_file("leaving recursive_step function");
return col_labels_out;
}
//void load_matrix_sparseMatlab(const mxArray* is,vector< vector<int> > & matrix){
//
// double *Gpr = mxGetPr(is);
// size_t *Gir = mxGetIr(is);
// size_t *Gjc = mxGetJc(is);
//
// size_t M = mxGetM(is);
// size_t N = mxGetN(is);
//
// //sprintf(outputString,"fprintf('%s: Reading matrix of size (%i,%i)\\n');",log_prefix_global,(int)M,(int)N);
// //mexEvalString(outputString);
//
// size_t sIndEdges;
// size_t eIndEdges;
//
// // clear first
// matrix.clear();
// matrix.resize(N,vector<int>(M,0));
//
// int w;
// for(int v=0; v<N; v++){ //<--- should this increment on 2
// ////sprintf(outputString,"fprintf('Row %i\\n');",(int)v);
// ////mexEvalString(outputString);
//
// //matrix.push_back(vector<int>(M,0));
//
// sIndEdges = Gjc[v];
// eIndEdges = Gjc[v+1];
// for( int nInd = sIndEdges ; nInd<eIndEdges ; nInd++ ){
// w = Gir[nInd];
// matrix[v][int(w)] = 1;
// //printf("row %i, col %i\n",v,w);
// }
// }
// //sprintf(outputString,"fprintf('%s: end read matrix\\n');",log_prefix_global);
// //mexEvalString(outputString);
//}
void load_matrix(istream* is,vector< vector<int> >* matrix,\
const string& delim = " \t"){
string line;
string strnum;
// clear first
matrix->clear();
// parse line by line
while (getline(*is, line))
{
matrix->push_back(vector<int>());
for (string::const_iterator i = line.begin(); i != line.end(); ++ i)
{
// If i is not a delim, then append it to strnum
if (delim.find(*i) == string::npos)
{
strnum += *i;
if (i + 1 != line.end()) // If it's the last char, do not continue
continue;
}
// if strnum is still empty, it means the previous char is also a
// delim (several delims appear together). Ignore this char.
if (strnum.empty())
continue;
// If we reach here, we got a number. Convert it to double.
int number;
istringstream(strnum) >> number;
matrix->back().push_back(number);
strnum.clear();
}
}
// log_file("File read: pass");
}
//data_out fitness_of_partition(const mxArray *is, vector<int> partition_rows, vector<int> partition_cols, int ibn,char * log_prefix){
//
// data_out var;
// vector<vector<int> > M;
// //ifstream is(Filename);
// //load_matrix(&is, &M);
// log_prefix_global = log_prefix;
// load_matrix_sparseMatlab(is, M);
//
// //network degress by rows and columns
// int N_cols=M[0].size();
// int N_rows=M.size();
// vector<int> k_rows(N_rows,0);
// vector<int> k_cols(N_cols,0);
// for (int i = 0; i < N_rows; ++i) {
// for (int j = 0; j < N_cols; ++j) {
// k_rows[i]+=M[i][j];
// k_cols[j]+=M[i][j];
// }
// }
//
// vector<vector<int> > partitions;
// partitions.push_back(partition_rows);
// partitions.push_back(partition_cols);
//
// int max_number_blocks_cols=*max_element(partitions[1].begin(), partitions[1].end());
// int max_number_blocks_rows=*max_element(partitions[0].begin(), partitions[0].end());
// int max_number_blocks = MAX(max_number_blocks_cols,max_number_blocks_rows) + 1;
//
// vector<vector<double> > lambdas = call_lambda_i(
// M,
// k_cols,
// k_rows,
// partitions[1],
// partitions[0],
// max_number_blocks,
// ibn);
//
// double Q=calculate_Fitness(
// M,
// k_cols,
// k_rows,
// lambdas[0],
// lambdas[1],
// ibn);
//
// var.QI_eo= Q;
// var.partitions_result=partitions;
// return var;
//}
//data_out extremal_optimization(const mxArray *is, double alpha_parameter, int repetitions, bool ibn,char * log_prefix){
// data_out var;
// vector<vector<int> > M;
// //ifstream is(Filename);
// //load_matrix(&is, &M);
// log_prefix_global = log_prefix;
// load_matrix_sparseMatlab(is, M);
//
// //network degress by rows and columns
// int N_cols=M[0].size();
// int N_rows=M.size();
// vector<int> k_rows(N_rows,0);
// vector<int> k_cols(N_cols,0);
// for (int i = 0; i < N_rows; ++i) {
// for (int j = 0; j < N_cols; ++j) {
// k_rows[i]+=M[i][j];
// k_cols[j]+=M[i][j];