-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindAlmost.cpp
1314 lines (1227 loc) · 41.7 KB
/
findAlmost.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
#include "parFindAlmost.hpp"
#include <unistd.h>
/* being driver */
int main(int argc, char** argv) {
std::srand( unsigned (std::time(0) + getpid()) );
return driver<findAlmost,parFindAlmost>(argc, argv);
}
/* end driver */
/* begin findAlmost methods */
branchSub* findAlmost::blankSub() {
findAlmostSub* newSP = new findAlmostSub;
newSP->setGlobalInfo(this);
return newSP;
}
/* end findAlmost methods */
/* begin findAlmostSub methods */
void findAlmostSub::boundComputation(double* controlParam) {
DEBUGPR(700, ucout << "findAlmostSub::boundComputation(double*) called for " << this
<< " at address " << (void*)this << std::endl);
// if the flag testBranchingStrength is set,
// this only works at the root node, and then terminates
// by killing the root node
// TODO: extend to work at other nodes?
if(global()->testBranchingStrength) {
// do initial refinement pass
refineByDegreeDiff(g,perg,B);
{
int lab[n];
int ptn[n];
perg.components(lab,ptn);
g.callNauty(lab,ptn);
}
// do root node refinement
int * edge_use = new int [LTIdx(n,0)](); //zero-initize new array of ints
refineUntilNone(g, g_fixed, perg, B, edge_use);
findBranchEdge(edge_use);
delete [] edge_use;
std::vector<int> refinedByFixedBranch;
// need an escape in case there's no edge to branch on because the refinement was perfect
if(disjunctEdges.num() == 0) {
testBranchingWriteToGlobal(0, refinedByFixedBranch);
setState(dead);
return;
}
// collect strong branching statistics for left (fixing) child
// first for the branch we would have taken based on the branching rule
// (assuming we just branch on one edge at a time..)
int refinedDefaultBranch = simulateLeftBranch( disjunctEdges.get(0) );
// now collect for all other edges in g
refinedByFixedBranch.reserve(g.numEdges());
DenseGraph::Row gi;
// iterate over all edges
for (int i = 0; i < n; i++){
gi = g.row(i);
for (int j = i; (j = gi.nextVertex(j)) >= 0; ){ //check upper triangle only
Edge sim_branch_edge = Edge(i,j);
refinedByFixedBranch.push_back(simulateLeftBranch(sim_branch_edge));
}
}
testBranchingWriteToGlobal(refinedDefaultBranch, refinedByFixedBranch);
setState(dead);
return;
}
if(boundCompFirstPass) { //called after node is first deleted
if(delNode) {
refineByDegreeDiff(g,perg,B);
{
int lab[n];
int ptn[n];
perg.components(lab,ptn);
g.callNauty(lab,ptn);
}
//get new lowerBound
lowerBound = independentSetHeuristic(perg);
DEBUGPR(500, ucout << "Right branch, after refineByDegreeDiff lowerBound = " << lowerBound << std::endl);
bound = lowerBound;
DEBUGPR(300, ucout << "Right branch, nauty called. Edge deletions here: " << delEdges << std::endl
<< "Orbits here: " << g.orbits << std::endl);
solution * sol = new findAlmostSol(global(),delEdges,g.orbits,g.numOrbits(),g.groupSize());
global()->foundSolution(sol);
if( B <= 0 || g_fixed.numEdges() >= g.numEdges() || lowerBound >= g.numOrbits()) {
DEBUGPR(300, if(B<=0) ucout << "Pruning here because B<=0\n");
DEBUGPR(300, if(g_fixed.numEdges() >= g.numEdges()) ucout << "Pruning here because we fixed all the edges\n");
DEBUGPR(300, if(lowerBound >= g.numOrbits()) ucout << "Pruning here because lowerBound >= g.numOrbits()\n");
DEBUGPR(400, ucout << "g_fixed here: " << g_fixed << std::endl);
setState(dead); //fathom node
return;
}
} else { //delNode is false, so this was a fixed node.
//it's possible we've fixed all the edges, in which case we want to prune
if (g_fixed.numEdges() >= g.numEdges()) {
DEBUGPR(300, ucout << "Pruning because g_fixed.numEdges() >= g.numEdges()\n");
DEBUGPR(400, ucout << "g_fixed here: " << g_fixed << std::endl);
setState(dead);
return;
}
//this is very cheap, so we might as well do it here
refineByDegreeDiffFixedEdges(g, g_fixed, perg);
//get new lowerBound
lowerBound = independentSetHeuristic(perg);
DEBUGPR(500, ucout << "Left branch, after refineByDegreeDiffFixedEdges lowerBound = " << lowerBound << std::endl);
bound = lowerBound;
if ( lowerBound >= g.numOrbits() /* || fixBranch->lowerBound >= bGlobal()->incumbentValue*/ ){ //PEBBL should do the latter
DEBUGPR(300, ucout << "Pruning because comp>=g.numOrbits().\n");
DEBUGPR(400, ucout << "g_fixed here: " << g_fixed << std::endl);
setState(dead);
return;
}
}
boundCompFirstPass = false;
return; //this will force PEBBL to update these bounds before doing a matching pass
}
int * edge_use = new int [LTIdx(n,0)](); //zero-initize new array of ints
int perg_edges_bf = perg.numEdges();
// new virtual function
runRefineByMatching(g, g_fixed, perg, B, edge_use);
lowerBound = independentSetHeuristic(perg);
DEBUGPR(500, ucout << "After refineByMatching lowerBound = " << lowerBound << std::endl);
bound = lowerBound;
if(lowerBound >= g.numOrbits()){
delete [] edge_use;
DEBUGPR(300, if(lowerBound >= g.numOrbits()) ucout << "Pruning here because lowerBound >= g.numOrbits()\n");
DEBUGPR(400, ucout << "g_fixed here: " << g_fixed << std::endl);
setState(dead);
return;
}
if(perg_edges_bf == perg.numEdges()){
if (global()->localBranching) {
DenseGraph perg_copy = perg;
DenseGraph g_fixed_copy = g_fixed;
std::fill(edge_use, edge_use+LTIdx(n,0), 0);
int B_heur = 1;
//refineByDegreeDiff(g, perg_copy, B_heur);
// new virtual function
runRefineByMatching(g, g_fixed_copy, perg_copy, B_heur, edge_use, true);
}
findBranchEdge(edge_use);
delete [] edge_use;
// new virtual function
makeBranchingDecisionsAgree();
if (disjunctEdges.num() == 0) {
setState(dead);
} else {
setState(bounded);
}
return;
}
delete [] edge_use;
}
// returns the number refined on simulated fixing disjuction
int findAlmostSub::simulateLeftBranch(const Edge &branch_edge) {
// copy perg, g_fixed
DenseGraph perg_branch = perg;
DenseGraph g_fixed_branch = g_fixed;
// add edge to g_fixed
g_fixed_branch.addEdge(branch_edge.u, branch_edge.v);
int perg_edges_start = perg_branch.numEdges();
// may as well do this first
refineByDegreeDiffFixedEdges(g, g_fixed_branch, perg_branch);
int * edge_use = new int [LTIdx(n,0)]();
refineUntilNone(g, g_fixed_branch, perg_branch, B, edge_use);
delete [] edge_use;
return (perg_edges_start - perg_branch.numEdges());
}
void findAlmostSub::refineUntilNone(const DenseGraph &g, const DenseGraph &g_fixed, DenseGraph &perg, int B, int* edge_use) {
for(;;) {
std::fill(edge_use, edge_use+LTIdx(n,0), 0);
int perg_edges_bf = perg.numEdges();
// new virtual function
runRefineByMatching(g, g_fixed, perg, B, edge_use);
if(perg_edges_bf == perg.numEdges()) return;
}
}
void findAlmostSub::findBranchEdge(int * edge_use) {
DEBUGPR(700, ucout << "findAlmostSub::splitComputation() called for " << this
<< " at address " << (void*)this << std::endl);
int i,j,k,max,best_i,best_j;
DenseGraph::Row gi;
if (global()->trackEdges && (prev_second_choice.u != -1)) {
int place = 0;
int val = edge_use[LTIdx(prev_second_choice.v, prev_second_choice.u)];
for (i = 0, gi = g.row(i) ; i < n; ++i, gi.nextRow()){
for (j = i; (j = gi.nextVertex(j)) >= 0; ){
if ( edge_use[LTIdx(j,i)] > val ) place++;
}
}
global()->rankInChild.push_back(place);
}
if(!(global()->randomBranching)){
for (k = 0; k < global()->DisjunctNum()+int(global()->trackEdges) ; ++k) {
max=0; best_i = -1; best_j = -1;
for (i = 0, gi = g.row(i) ; i < n; ++i, gi.nextRow()){
for (j = i; (j = gi.nextVertex(j)) >= 0; ){
DEBUGPR(1100, /*if(uMPI::rank == uMPI::ioProc)*/ ucout << "edge_use(" << i << ',' << j << ") = "<< edge_use[LTIdx(j,i)] << std::endl);
if (max < edge_use[LTIdx(j,i)]){
max = edge_use[LTIdx(j,i)];
best_i = i;
best_j = j;
}
}
}
//if we didn't find a better edge on this pass
if (max == 0) {
break;
} else {
//zero this out so we don't see him again
if ( k < global()->DisjunctNum() ) {
edge_use[LTIdx(best_j, best_i)] = 0;
disjunctEdges.add(best_i, best_j);
} else {
prev_second_choice.u = best_i; prev_second_choice.v = best_j;
}
}
}
if (disjunctEdges.num() > 0) { //a max was found, and one end point isn't isolated in perg
//(if it was, we wouldn't have set up a matching problem for it in the
// most recent pass. Also, fixed edges won't be deleted because they're
// not used in any solution <= 2B.
DEBUGPR(300, /*if (uMPI::rank == uMPI::ioProc)*/ ucout << "EDGE_FOUND, branching on edge(s) " << disjunctEdges << std::endl
<< "Deleted edges here: " << delEdges << std::endl);
DEBUGPR(300, /*if (uMPI::rank == uMPI::ioProc)*/ ucout << "Fixed edges here: " << g_fixed << std::endl);
return;
} else { //if we got here, that means no edge had to be deleted in order
// for the matching to work...this probably means the graph has a decent
// amount of symmetry, so let's try to branch on something that will break
// it in order to fathom quickly further down (for now keep implementation
// the same as code on server)
DEBUGPR(1, ucout << "Warning, no edge had to be deleted in order to make the matching work" << std::endl);
for (i=0, gi = g.row(i); i<n; ++i, gi.nextRow()) {
for (j=i; (j = gi.nextVertex(j)) >= 0; ) {
//don't delete fixed edges
if (g_fixed.isEdge(i,j)) {continue;}
//no point in deleting if this is true
if (perg.isIsolated(i) && perg.isIsolated(j)) {continue;}
// keep adding
disjunctEdges.add(i,j);
// until this is full
if (disjunctEdges.left() == 0) {
DEBUGPR(300,/* if (uMPI::rank == uMPI::ioProc)*/ ucout << "EDGE_FOUND, branching on edge(s) " << disjunctEdges << std::endl
<< "Deleted edges here: " << delEdges << std::endl);
DEBUGPR(300,/* if (uMPI::rank == uMPI::ioProc)*/ ucout << "Fixed edges here: " << g_fixed << std::endl);
return;
}
}
}
if (disjunctEdges.num() > 0) {
DEBUGPR(300,/* if (uMPI::rank == uMPI::ioProc)*/ ucout << "EDGE_FOUND, branching on edge(s) " << disjunctEdges << std::endl
<< "Deleted edges here: " << delEdges << std::endl);
DEBUGPR(300,/* if (uMPI::rank == uMPI::ioProc)*/ ucout << "Fixed edges here: " << g_fixed << std::endl);
return;
}
}
} else { //implement random branching
std::vector<int> randomIterator (n); //make a vector for iterator
std::vector<int> jRandomIterator; //make vector for neighbors
for(i=0; i<n; i++) randomIterator[i] = i;
std::random_shuffle( randomIterator.begin(), randomIterator.end() );
for (i=0; i<n; i++) {
jRandomIterator.clear(); //clear out randomIterator[i]'s neighbors
gi = g.row(randomIterator[i]);
for (j=0; (j = gi.nextVertex(j)) >= 0;) {
jRandomIterator.push_back(j); //load neighbor vector
}
std::random_shuffle( jRandomIterator.begin(), jRandomIterator.end() ); //shuffle neighbor vector
for (std::vector<int>::iterator it=jRandomIterator.begin(); it!=jRandomIterator.end(); ++it) {
//don't delete fixed edges
if (g_fixed.isEdge(randomIterator[i],*it)) {continue;}
//no point in deleting if this is true
if (perg.isIsolated(randomIterator[i]) && perg.isIsolated(*it)) {continue;}
disjunctEdges.add(randomIterator[i], *it);
if (disjunctEdges.left() == 0) {
DEBUGPR(300,/* if (uMPI::rank == uMPI::ioProc)*/ ucout << "EDGE_FOUND, branching on edges "
<< disjunctEdges << std::endl
<< "Deleted edges here: " << delEdges << std::endl);
DEBUGPR(300,/* if (uMPI::rank == uMPI::ioProc)*/ ucout << "Fixed edges here: " << g_fixed << std::endl);
return;
}
}
}
if (disjunctEdges.num() > 0) return;
}
// if we got here either all the edges are fixed or isolated in perg, so this
// node can die
DEBUGPR(400, ucout << "We ran out of edges to fix - deleted edges here:" << delEdges << std::endl);
DEBUGPR(400, ucout << "g_fixed here: " << g_fixed << std::endl);
return;
}
void findAlmostSub::findAlmostSubAsChildOf(findAlmostSub* parent, int whichChild){
//to keep left dive first, i==disjunctEdges.num() ~ fixed child, i<disjunctEdges.num() ~ deleted child
branchSubAsChildOf(parent);
bound = lowerBound = parent->lowerBound;
globalPtr = parent->global();
n = parent->n;
B = parent->B;
g = parent->g;
delEdges = parent->delEdges;
g_fixed = parent->g_fixed;
perg = parent->perg;
disjunctEdges = EdgeList(global()->DisjunctNum());
boundCompFirstPass = true;
if (global()->trackEdges) prev_second_choice = parent->prev_second_choice;
//**************************begin del edge disjunction*******************//
if (whichChild < parent->disjunctEdges.num()) {
delNode = true;
B--; //decrement B
g.delEdge(parent->disjunctEdges.getu(whichChild), parent->disjunctEdges.getv(whichChild));//delete edge from graph
DEBUGPR(300, /*if (uMPI::rank == uMPI::ioProc)*/ ucout << "Deleting edge " << parent->disjunctEdges.get(whichChild) << std::endl);
delEdges.add(parent->disjunctEdges.get(whichChild)); //add to list of deleted edges
}
//*************************end del edge disjunction********************//
//********************Edge fixing child**********************//
if( whichChild == parent->disjunctEdges.num() ){
delNode = false;
for (int k = 0; k < parent->disjunctEdges.num(); ++k)
g_fixed.addEdge(parent->disjunctEdges.getu(k),parent->disjunctEdges.getv(k)); //add to graph of fixings
DEBUGPR(300, /*if (uMPI::rank == uMPI::ioProc)*/ ucout << "Fixing edge(s)" << parent->disjunctEdges << std::endl);
}
//***************************end Fix edge disjunction*********************//
}
/* end findAlmostSub methods */
/* begin parFindAlmost methods */
void parFindAlmostSub::runRefineByMatching( const DenseGraph &g, const DenseGraph &g_fixed, DenseGraph &perg, int B, int* edge_use, bool always_collect_edge_use ) {
if (rampingUp()) {
parRefineByMatching(g, g_fixed, perg, B, edge_use, always_collect_edge_use);
//DEBUGPR(200,
// if (uMPI::rank == uMPI::ioProc) ucout << "During syncronous ramp-up, we refined " << perg_edges_bf - perg.numEdges() << std::endl);
} else {
refineByMatching(g, g_fixed, perg, B, edge_use, always_collect_edge_use);
}
}
void parFindAlmostSub::makeBranchingDecisionsAgree() {
if (global()->randomBranching && rampingUp()) {
// make the disjunctions all the same
// we'll use ioProc's rank
auto packed_edges = utilib::PackBuffer(disjunctEdges.packSize());
disjunctEdges.write(packed_edges);
uMPI::broadcast((char*)packed_edges.buf(),packed_edges.size(),MPI::CHAR,uMPI::ioProc);
auto unpacked_edges = utilib::UnPackBuffer(packed_edges.buf(), packed_edges.size());
disjunctEdges.read(unpacked_edges);
}
}
void parFindAlmostSub::pack(utilib::PackBuffer& outBuffer) {
// We can do better...we don't need to pack g, instead let's get it from global()->gRef() and
// then delete the appropriate edges when we unpack...but we need to pack g's orbits
outBuffer << delEdges << g_fixed << perg << delNode << boundCompFirstPass
<< lowerBound << n << B << disjunctEdges;
for(int i=0; i<n; i++){
outBuffer << g.orbits[i];
}
outBuffer << g.stats.numorbits;
}
void parFindAlmostSub::unpack(utilib::UnPackBuffer& inBuffer) {
inBuffer >> delEdges >> g_fixed >> perg >> delNode >> boundCompFirstPass
>> lowerBound >> n >> B >> disjunctEdges;
g = global()->gRef(); //get g from global branching object
//fix so it's actually the current node
for(int i=0; i<delEdges.num(); i++){
g.delEdge(delEdges.getu(i),delEdges.getv(i));
}
for(int i=0; i<n; i++){
inBuffer >> g.orbits[i];
}
inBuffer >> g.stats.numorbits;
}
int parFindAlmost::spPackSize(){
// g.orbits + g.stats.numorbits + delEdges + g_fixed + perg
return ( ((g.numVertices()+1) + 2*budget + 2)*sizeof(int) + 2*g.DenseGraph::packSize()
+ 3*sizeof(bool) + 5*sizeof(int) );
}
void parFindAlmost::pack(utilib::PackBuffer& outBuffer) {
outBuffer << g << budget;
}
void parFindAlmost::unpack(utilib::UnPackBuffer& inBuffer) {
inBuffer >> g >> budget;
}
parallelBranchSub* parFindAlmost::blankParallelSub() {
parFindAlmostSub* newSP = new parFindAlmostSub;
DEBUGPR(700, ucout << "new parFindAlmostSub from parFindAlmost::blankParallelSub(), address: " <<(void*) newSP << std::endl);
newSP->setGlobalInfo(this);
return newSP;
}
/* end parFindAlmost methodes */
/* being findAlmostSol methods */
void findAlmostSol::packContents(utilib::PackBuffer& outBuf){
outBuf << delEdges << n;
for(int i=0; i<n; i++) {
outBuf << orbits[i];
}
outBuf << group_size;
}
void findAlmostSol::unpackContents(utilib::UnPackBuffer& inBuf) {
inBuf >> delEdges >> n;
orbits = new int[n];
for(int i=0; i<n; i++) {
inBuf >> orbits[i];
}
inBuf >> group_size;
}
int findAlmostSol::maxContentsBufSize() {
return ( delEdges.packSize() + (n+1)*sizeof(int) + sizeof(double) );
}
/* writes the solution to the given ostream object */
void findAlmostSol::printContents(std::ostream& os){
os << "Edges deleted: " << delEdges << std::endl;
os << "Nontrival orbits:";
int * thisOrbit = new int[n];
int count;
for( int i=0; i < n ; i++){
if (orbits[i] == i) { //this is the first vertex in this orbit
count = 0;
thisOrbit[count] = i;
count++;
for ( int j=i+1; j < n; j++) { //find the others
if (orbits[j] == i){
thisOrbit[count] = j;
count++;
}
}
if(count > 1) { //if there's more than a singleton we'll put to os
for( int j=0; j<count; j++) os << " " << (thisOrbit[j]+1);
//put number of orbits at end
os << " " << "(" << count << ");";
}
}
}
os << '\n';
os << "Group size: " << group_size << std::endl;
delete [] thisOrbit;
}
/* end findAlmostSol methods */
//************* begin refinement algorithms ******************//
// This function is from libhungarian by Cyrill Stachniss, with slight modifications for speed when repeatedly calling //
// libhungarian is available at: http://www2.informatik.uni-freiburg.de/~stachnis/misc.html
int hungarian_solve(hungarian_problem_t* p)
{
int i, j, m, n, k, l, s, t, q, unmatched, cost;
cost=0;
m =p->num_rows;
n =p->num_cols;
// put these on the stack; they're small
int col_mate[m];
int unchosen_row[m];
int row_dec[m];
int slack_row[m];
int row_mate[n];
int parent_row[n];
int col_inc[n];
int slack[n];
/* //this should be done already
for (i=0;i<p->num_rows;i++) {
col_mate[i]=0;
unchosen_row[i]=0;
row_dec[i]=0;
slack_row[i]=0;
}
for (j=0;j<p->num_cols;j++) {
row_mate[j]=0;
parent_row[j] = 0;
col_inc[j]=0;
slack[j]=0;
}
for (i=0;i<p->num_rows;++i)
for (j=0;j<p->num_cols;++j)
p->assignment[i][j]=HUNGARIAN_NOT_ASSIGNED; */
// Begin subtract column minima in order to start with lots of zeroes 12
for (l=0;l<n;l++)
{
s=p->cost[0][l];
for (k=1;k<m;k++)
if (p->cost[k][l]<s)
s=p->cost[k][l];
cost+=s;
if (s!=0)
for (k=0;k<m;k++)
p->cost[k][l]-=s;
}
// End subtract column minima in order to start with lots of zeroes 12
// Begin initial state 16
t=0;
// replace this with calls to fill
for (l=0;l<n;l++) row_mate[l] = -1;
for (l=0;l<n;l++) parent_row[l] = -1;
for (l=0;l<n;l++) col_inc[l] = 0;
for (l=0;l<n;l++) slack[l] = INF;
for (k=0;k<m;k++) slack_row[k] = 0;
for (k=0;k<m;k++) unchosen_row[k] = 0;
for (k=0;k<m;k++)
{
slack_row[k] = 0;
s=p->cost[k][0];
for (l=1;l<n;l++)
if (p->cost[k][l]<s)
s=p->cost[k][l];
row_dec[k]=s;
for (l=0;l<n;l++)
if (s==p->cost[k][l] && row_mate[l]<0)
{
col_mate[k]=l;
row_mate[l]=k;
goto row_done;
}
col_mate[k]= -1;
unchosen_row[t++]=k;
row_done:
;
}
// End initial state 16
// Begin Hungarian algorithm 18
if (t==0)
goto done;
unmatched=t;
while (1)
{
q=0;
while (1)
{
while (q<t)
{
// Begin explore node q of the forest 19
{
k=unchosen_row[q];
s=row_dec[k];
for (l=0;l<n;l++)
if (slack[l])
{
int del;
del=p->cost[k][l]-s+col_inc[l];
if (del<slack[l])
{
if (del==0)
{
if (row_mate[l]<0)
goto breakthru;
slack[l]=0;
parent_row[l]=k;
unchosen_row[t++]=row_mate[l];
}
else
{
slack[l]=del;
slack_row[l]=k;
}
}
}
}
// End explore node q of the forest 19
q++;
}
// Begin introduce a new zero into the matrix 21
s=INF;
for (l=0;l<n;l++)
if (slack[l] && slack[l]<s)
s=slack[l];
for (q=0;q<t;q++)
row_dec[unchosen_row[q]]+=s;
for (l=0;l<n;l++)
if (slack[l])
{
slack[l]-=s;
if (slack[l]==0)
{
// Begin look at a new zero 22
k=slack_row[l];
if (row_mate[l]<0)
{
for (j=l+1;j<n;j++)
if (slack[j]==0)
col_inc[j]+=s;
goto breakthru;
}
else
{
parent_row[l]=k;
unchosen_row[t++]=row_mate[l];
}
// End look at a new zero 22
}
}
else
col_inc[l]+=s;
// End introduce a new zero into the matrix 21
}
breakthru:
// Begin update the matching 20
while (1)
{
j=col_mate[k];
col_mate[k]=l;
row_mate[l]=k;
if (j<0)
break;
k=parent_row[j];
l=j;
}
// End update the matching 20
if (--unmatched==0)
goto done;
// Begin get ready for another stage 17
t=0;
for (l=0;l<n;l++)
{
parent_row[l]= -1;
slack[l]=INF;
}
for (k=0;k<m;k++)
if (col_mate[k]<0)
{
unchosen_row[t++]=k;
}
// End get ready for another stage 17
}
done:
// Begin doublecheck the solution 23
/* don't bother please
for (k=0;k<m;k++)
for (l=0;l<n;l++)
if (p->cost[k][l]<row_dec[k]-col_inc[l])
exit(0);
for (k=0;k<m;k++)
{
l=col_mate[k];
if (l<0 || p->cost[k][l]!=row_dec[k]-col_inc[l])
exit(0);
}
k=0;
for (l=0;l<n;l++)
if (col_inc[l])
k++;
if (k>m)
exit(0);
// End doublecheck the solution 23
*/
// End Hungarian algorithm 18
for (i=0;i<m;++i)
{
p->assignment[i][col_mate[i]]=HUNGARIAN_ASSIGNED;
/*TRACE("%d - %d\n", i, col_mate[i]);*/
}
for (k=0;k<m;++k)
{
for (l=0;l<n;++l)
{
/*TRACE("%d ",p->cost[k][l]-row_dec[k]+col_inc[l]);*/
p->cost[k][l]=p->cost[k][l]-row_dec[k]+col_inc[l];
}
/*TRACE("\n");*/
}
for (i=0;i<m;i++)
cost+=row_dec[i];
for (i=0;i<n;i++)
cost-=col_inc[i];
/* // don't need these for stack-alloc'd arrays
free(slack);
free(col_inc);
free(parent_row);
free(row_mate);
free(slack_row);
free(row_dec);
free(unchosen_row);
free(col_mate);
*/
return cost;
}
void refineByMatching(const DenseGraph &g, const DenseGraph &g_fixed, DenseGraph &perg, int B, int* edge_use, bool always_collect_edge_use){
int i, inf = 4*B, n = g.numVertices();
//begin block for parallel processing
{
int j,k,l,deg_gi,deg_gj,size,u,v;
bool neighbors;
bool collect_edge_use = true;
DenseGraph::Row pergi;
int biSize = n+B;
int* Ni;
int* Nj;
int** biGraph;
int** assignment;
hungarian_problem_t p;
int cost;
biGraph = (int**)malloc(biSize*sizeof(int*));
assignment = (int**)malloc(biSize*sizeof(int*));
Ni = (int*)malloc(n*sizeof(int));
Nj = (int*)malloc(n*sizeof(int));
for (j = 0; j < biSize; ++j){
biGraph[j] = (int*)malloc(biSize*sizeof(int));
assignment[j] = (int*)malloc(biSize*sizeof(int));
}
for (i = 0; i < n; i++){
pergi = perg.row(i);
for (j = i; (j = pergi.nextVertex(j)) >= 0; ){ //check upper triangle only
deg_gi = g.deg(i); deg_gj = g.deg(j);
//set up and solve matching problem
neighbors = false;
size = (deg_gi > deg_gj) ? deg_gi : deg_gj ; //return greater of deg_gi and deg_gj
//printf("B = %d", B);
size += B;
neighbors = g.isEdge(i,j);
if (neighbors) size--; //if i and j are neighbors, we won't add them to the biGraph
buildCostMatrix(i, j, g, g_fixed, perg, size, inf, neighbors, biGraph, Ni, Nj);
for(k = 0; k < size; k++){
memset(assignment[k], 0, size*sizeof(int));
} // zero out assignment matrix
//let's do the initializtion ourselves
p.num_rows = size; p.num_cols = size;
p.cost = biGraph; p.assignment = assignment;
cost = hungarian_solve(&p);
// This information needs to be shared between the treads somehow...
if (cost > 2*B){ //we can kill i,j
//printf("Cost = %d, killing this permutation\n", cost);
//printf("Kill permutation (%d, %d) in refineByMatching\n", i, j);
perg.delEdge(i,j);
//if we delete an edge from perg we won't be using the edge_use data,
//so we can save time by not writing it
if (!always_collect_edge_use) collect_edge_use = false;
} else if(collect_edge_use) {
if (neighbors) {
for (k = 0; k < deg_gi-1; ++k){
for (l = deg_gj-1; l < size; ++l){
if (assignment[k][l] == 1){
u = Ni[k];
//printf("edge %d, %d got deleted\n", u, i);
if (i>u) {
edge_use[LTIdx(i,u)]++;
} else { // u>i
edge_use[LTIdx(u,i)]++;
}
break; //if we come across 1 in the row, we shouldn't find others
}
}
}
for ( ; k < size; ++k){
for (l = 0; l < deg_gj-1; ++l){
if (assignment[k][l] == 1){
v = Nj[l];
//printf("edge %d, %d got deleted\n", v, j);
if (j>v) {
edge_use[LTIdx(j,v)]++;
} else { //v>j
edge_use[LTIdx(v,j)]++;
}
break; //sim
}
}
}
} else {
for (k = 0; k < deg_gi; ++k){
for (l = deg_gj; l < size; ++l){
if (assignment[k][l] == 1){
u = Ni[k];
//printf("edge %d, %d got deleted\n", u, i);
if (i>u) {
edge_use[LTIdx(i,u)]++;
} else { // u>i
edge_use[LTIdx(u,i)]++;
}
break;
}
}
}
for ( ; k < size; ++k){
for (l = 0; l < deg_gj; ++l){
if (assignment[k][l] == 1){
v = Nj[l];
//printf("edge %d, %d got deleted\n", v, j);
if (j>v) {
edge_use[LTIdx(j,v)]++;
} else { //v>j
edge_use[LTIdx(v,j)]++;
}
break;
}
}
}
}
//printf("\n");
} //end info needing to be shared
}
}
for (j = 0; j < biSize; ++j){
free(biGraph[j]);
free(assignment[j]);
}
free(Ni); free(Nj);
free(biGraph);
free(assignment);
} //end parallel scope
}
/* TODO: fix logic to handle self-loops in g */
void parRefineByMatching(const DenseGraph &g, const DenseGraph &g_fixed, DenseGraph &perg, int B, int* edge_use, bool always_collect_edge_use ){
int i, inf = 4*B, n = g.numVertices();
//begin block for parallel processing
{
int j,k,l,deg_gi,deg_gj,size,u,v;
bool neighbors;
bool collect_edge_use = true;
DenseGraph::Row pergi;
int biSize = n+B;
int* Ni;
int* Nj;
int** biGraph;
int** assignment;
hungarian_problem_t p;
int cost;
biGraph = (int**)malloc(biSize*sizeof(int*));
assignment = (int**)malloc(biSize*sizeof(int*));
Ni = (int*)malloc(n*sizeof(int));
Nj = (int*)malloc(n*sizeof(int));
for (j = 0; j < biSize; ++j){
biGraph[j] = (int*)malloc(biSize*sizeof(int));
assignment[j] = (int*)malloc(biSize*sizeof(int));
}
//begin figuring out which edges in perg we'll process
int num_iterations = perg.numEdges(); //- n; //we'll only iterate over perg's
int iteration = 0;
bool * perg_del_edges = new bool[num_iterations](); //itilize to 0
int iters_per_proc = num_iterations / (uMPI::size);
int iters_remaining = num_iterations%(uMPI::size);
int my_start_iter = uMPI::rank*iters_per_proc + ((uMPI::rank < iters_remaining) ? (uMPI::rank) : iters_remaining); //return the smaller of rank and iters remaining
int my_end_iter = (uMPI::rank+1)*iters_per_proc + (((uMPI::rank+1) < iters_remaining) ? (uMPI::rank+1) : iters_remaining);
//end
for (i = 0; i < n; i++){
pergi = perg.row(i);
for (j = i; (j = pergi.nextVertex(j)) >= 0; iteration++){ //check upper triangle only
if( iteration >= my_start_iter && iteration < my_end_iter) {
// printf("\n(%d, %d) is an edge in the possible permutations graph\n", i, j);
deg_gi = g.deg(i); deg_gj = g.deg(j);
//set up and solve matching problem
neighbors = false;
size = (deg_gi > deg_gj) ? deg_gi : deg_gj ; //return greater of deg_gi and deg_gj
//printf("B = %d", B);
size += B;
neighbors = g.isEdge(i,j);
if (neighbors) size--; //if i and j are neighbors, we won't add them to the biGraph
buildCostMatrix(i, j, g, g_fixed, perg, size, inf, neighbors, biGraph, Ni, Nj);
for(k = 0; k < size; k++){
memset(assignment[k], 0, size*sizeof(int));
} // zero out assignment matrix
//let's do the initializtion ourselves
p.num_rows = size; p.num_cols = size;
p.cost = biGraph; p.assignment = assignment;
cost = hungarian_solve(&p);
// This information needs to be shared between the treads somehow...
if (cost > 2*B){ //we can kill i,j
//printf("Cost = %d, killing this permutation\n", cost);
//if (uMPI::rank == uMPI::ioProc) ucout << "Planning to kill permutation (" << i << "," << j << ") in refineByMatching, counter = " << counter << std::endl;
//perg.delEdge(i,j);
perg_del_edges[iteration] = true;
//if we delete an edge from perg we won't be using the edge_use data,
//so we can save time by not writing it
if (!always_collect_edge_use) collect_edge_use = false;
} else if(collect_edge_use) {
if (neighbors) {
for (k = 0; k < deg_gi-1; ++k){
for (l = deg_gj-1; l < size; ++l){
if (assignment[k][l] == 1){
u = Ni[k];
//printf("edge %d, %d got deleted\n", u, i);
if (i>u) {
edge_use[LTIdx(i,u)]++;
} else { // u>i
edge_use[LTIdx(u,i)]++;
}
break; //if we come across 1 in the row, we shouldn't find others
}
}
}
for ( ; k < size; ++k){
for (l = 0; l < deg_gj-1; ++l){
if (assignment[k][l] == 1){
v = Nj[l];
//printf("edge %d, %d got deleted\n", v, j);
if (j>v) {
edge_use[LTIdx(j,v)]++;
} else { //v>j
edge_use[LTIdx(v,j)]++;
}
break; //sim
}
}
}
} else {
for (k = 0; k < deg_gi; ++k){
for (l = deg_gj; l < size; ++l){
if (assignment[k][l] == 1){
u = Ni[k];
//printf("edge %d, %d got deleted\n", u, i);
if (i>u) {
edge_use[LTIdx(i,u)]++;
} else { // u>i
edge_use[LTIdx(u,i)]++;
}
break;
}
}
}
for ( ; k < size; ++k){
for (l = 0; l < deg_gj; ++l){
if (assignment[k][l] == 1){
v = Nj[l];
//printf("edge %d, %d got deleted\n", v, j);
if (j>v) {
edge_use[LTIdx(j,v)]++;
} else { //v>j
edge_use[LTIdx(v,j)]++;
}
break;
}
}
}
}
//printf("\n");
} //end info needing to be shared
}
}
}
//if (num_iterations != iteration) ucout << "You're a dumbass, interation = " << iteration << " and num_iterations = " << num_iterations << std::endl;
//collect info and act on it
//set collect_edge_use ... if it's false on some thread it's false
//everywhere, and we'll need to gather up the deleted edges
uMPI::reduceCast(MPI_IN_PLACE,&collect_edge_use,1,MPI::BOOL,MPI_LAND);
if(collect_edge_use){ //in this case, no edges were deleted, so we'll probably be branching
uMPI::reduceCast(MPI_IN_PLACE,edge_use,LTIdx(n,0),MPI::INT,MPI_SUM);
} else { //in this case some edges were deleted, so we need to update perg.
uMPI::reduceCast(MPI_IN_PLACE,perg_del_edges,num_iterations,MPI::BOOL,MPI_LOR);
iteration = 0;
for (i = 0; i < n; i++){
pergi = perg.row(i);
for (j = i; (j = pergi.nextVertex(j)) >= 0; iteration++){ //check upper triangle only
if(perg_del_edges[iteration]){
perg.delEdge(i,j);