forked from ERGO-Code/HiGHS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHighsSymmetry.cpp
More file actions
1921 lines (1612 loc) · 66 KB
/
HighsSymmetry.cpp
File metadata and controls
1921 lines (1612 loc) · 66 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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the HiGHS linear optimization suite */
/* */
/* Available as open-source under the MIT License */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file HighsSymmetry.cpp
* @brief Facilities for symmetry detection
* @author Leona Gottwald
*/
#include "presolve/HighsSymmetry.h"
#include <algorithm>
#include <numeric>
#include "../extern/pdqsort/pdqsort.h"
#include "mip/HighsCliqueTable.h"
#include "mip/HighsDomain.h"
#include "parallel/HighsParallel.h"
#include "util/HighsDisjointSets.h"
void HighsSymmetryDetection::removeFixPoints() {
Gend.resize(numVertices);
for (HighsInt i = 0; i < numVertices; ++i) {
Gend[i] =
std::partition(Gedge.begin() + Gstart[i], Gedge.begin() + Gstart[i + 1],
[&](const std::pair<HighsInt, HighsUInt>& edge) {
return cellSize(vertexToCell[edge.first]) > 1;
}) -
Gedge.begin();
assert(Gend[i] >= Gstart[i] && Gend[i] <= Gstart[i + 1]);
}
HighsInt unitCellIndex = numVertices;
currentPartition.erase(
std::remove_if(currentPartition.begin(), currentPartition.end(),
[&](HighsInt vertex) {
if (cellSize(vertexToCell[vertex]) == 1) {
--unitCellIndex;
vertexToCell[vertex] = unitCellIndex;
return true;
}
return false;
}),
currentPartition.end());
for (HighsInt i = 0; i < numVertices; ++i) {
if (Gend[i] == Gstart[i + 1]) continue;
for (HighsInt j = Gend[i]; j < Gstart[i + 1]; ++j)
Gedge[j].first = vertexToCell[Gedge[j].first];
}
if ((HighsInt)currentPartition.size() < numVertices) {
numVertices = currentPartition.size();
if (numVertices == 0) {
numActiveCols = 0;
return;
}
currentPartitionLinks.resize(numVertices);
cellInRefinementQueue.assign(numVertices, false);
assert(refinementQueue.empty());
refinementQueue.clear();
HighsInt cellStart = 0;
HighsInt cellNumber = 0;
for (HighsInt i = 0; i < numVertices; ++i) {
HighsInt vertex = currentPartition[i];
// if the cell number is different to the current cell number this is the
// start of a new cell
if (cellNumber != vertexToCell[vertex]) {
// remember the number of this cell to identify its end
cellNumber = vertexToCell[vertex];
// set the link of the cell start to point to its end
currentPartitionLinks[cellStart] = i;
// remember start of this cell
cellStart = i;
}
// correct the vertexToCell array to store the start index of the
// cell, not its number
updateCellMembership(i, cellStart, false);
}
// set the column partition link of the last started cell to point past the
// end
assert((int)currentPartitionLinks.size() > 0);
currentPartitionLinks[cellStart] = numVertices;
numActiveCols =
std::partition_point(currentPartition.begin(), currentPartition.end(),
[&](HighsInt v) { return v < numCol; }) -
currentPartition.begin();
} else
numActiveCols = numCol;
}
void HighsSymmetries::clear() {
permutationColumns.clear();
permutations.clear();
orbitPartition.clear();
orbitSize.clear();
columnPosition.clear();
linkCompressionStack.clear();
columnToOrbitope.clear();
orbitopes.clear();
numPerms = 0;
numGenerators = 0;
}
void HighsSymmetries::mergeOrbits(HighsInt v1, HighsInt v2) {
if (v1 == v2) return;
HighsInt orbit1 = getOrbit(v1);
HighsInt orbit2 = getOrbit(v2);
if (orbit1 == orbit2) return;
if (orbitSize[orbit2] < orbitSize[orbit1]) {
orbitPartition[orbit2] = orbit1;
orbitSize[orbit1] += orbitSize[orbit2];
} else {
orbitPartition[orbit1] = orbit2;
orbitSize[orbit2] += orbitSize[orbit1];
}
return;
}
HighsInt HighsSymmetries::getOrbit(HighsInt col) {
HighsInt i = columnPosition[col];
if (i == -1) return -1;
HighsInt orbit = orbitPartition[i];
if (orbit != orbitPartition[orbit]) {
do {
linkCompressionStack.push_back(i);
i = orbit;
orbit = orbitPartition[orbit];
} while (orbit != orbitPartition[orbit]);
do {
i = linkCompressionStack.back();
linkCompressionStack.pop_back();
orbitPartition[i] = orbit;
} while (!linkCompressionStack.empty());
}
return orbit;
}
HighsInt HighsSymmetries::propagateOrbitopes(HighsDomain& domain) const {
if (columnToOrbitope.size() == 0 || domain.getBranchDepth() == 0) return 0;
std::set<HighsInt> propagationOrbitopes;
const auto& domchgstack = domain.getDomainChangeStack();
for (HighsInt pos : domain.getBranchingPositions()) {
const HighsInt* orbitope = columnToOrbitope.find(domchgstack[pos].column);
if (orbitope) propagationOrbitopes.insert(*orbitope);
}
HighsInt numFixed = 0;
for (HighsInt i : propagationOrbitopes) {
numFixed += orbitopes[i].orbitalFixing(domain);
if (domain.infeasible()) break;
}
// if (numFixed)
// printf("orbital fixing for full orbitope found %d fixings\n", numFixed);
return numFixed;
}
std::shared_ptr<const StabilizerOrbits>
HighsSymmetries::computeStabilizerOrbits(const HighsDomain& localdom) {
const auto& domchgStack = localdom.getDomainChangeStack();
const auto& branchingPos = localdom.getBranchingPositions();
StabilizerOrbits stabilizerOrbits;
stabilizerOrbits.stabilizedCols.reserve(permutationColumns.size());
for (HighsInt i : branchingPos) {
HighsInt col = domchgStack[i].column;
if (columnPosition[col] == -1) continue;
assert(localdom.variableType(col) != HighsVarType::kContinuous);
// if we branch a variable upwards it is either binary and branched to one
// and needs to be stabilized or it is a general integer and needs to be
// stabilized regardless of branching direction.
// if we branch downwards we only need to stabilize on this
// branching column if it is a general integer
if (domchgStack[i].boundtype == HighsBoundType::kLower ||
!localdom.isGlobalBinary(col))
stabilizerOrbits.stabilizedCols.push_back(columnPosition[col]);
}
HighsInt permLength = permutationColumns.size();
orbitPartition.resize(permLength);
std::iota(orbitPartition.begin(), orbitPartition.end(), 0);
orbitSize.assign(permLength, 1);
for (HighsInt i = 0; i < numPerms; ++i) {
const HighsInt* perm = permutations.data() + i * permutationColumns.size();
bool permRespectsBranchings = true;
for (HighsInt i : stabilizerOrbits.stabilizedCols) {
if (permutationColumns[i] != perm[i]) {
permRespectsBranchings = false;
break;
}
}
if (!permRespectsBranchings) continue;
for (HighsInt j = 0; j < permLength; ++j) {
mergeOrbits(permutationColumns[j], perm[j]);
}
}
stabilizerOrbits.stabilizedCols.clear();
stabilizerOrbits.orbitCols.reserve(permLength);
for (HighsInt i = 0; i < permLength; ++i) {
if (localdom.variableType(permutationColumns[i]) ==
HighsVarType::kContinuous)
continue;
HighsInt orbit = getOrbit(permutationColumns[i]);
if (orbitSize[orbit] == 1)
stabilizerOrbits.stabilizedCols.push_back(permutationColumns[i]);
else if (localdom.isGlobalBinary(permutationColumns[i]))
stabilizerOrbits.orbitCols.push_back(permutationColumns[i]);
}
stabilizerOrbits.symmetries = this;
pdqsort(stabilizerOrbits.stabilizedCols.begin(),
stabilizerOrbits.stabilizedCols.end());
if (!stabilizerOrbits.orbitCols.empty()) {
pdqsort(stabilizerOrbits.orbitCols.begin(),
stabilizerOrbits.orbitCols.end(),
[&](HighsInt col1, HighsInt col2) {
return getOrbit(col1) < getOrbit(col2);
});
HighsInt numOrbitCols = stabilizerOrbits.orbitCols.size();
stabilizerOrbits.orbitStarts.reserve(numOrbitCols + 1);
stabilizerOrbits.orbitStarts.push_back(0);
for (HighsInt i = 1; i < numOrbitCols; ++i) {
if (getOrbit(stabilizerOrbits.orbitCols[i]) !=
getOrbit(stabilizerOrbits.orbitCols[i - 1]))
stabilizerOrbits.orbitStarts.push_back(i);
}
stabilizerOrbits.orbitStarts.push_back(numOrbitCols);
}
return std::make_shared<const StabilizerOrbits>(std::move(stabilizerOrbits));
}
HighsInt StabilizerOrbits::orbitalFixing(HighsDomain& domain) const {
HighsInt numFixed = symmetries->propagateOrbitopes(domain);
if (domain.infeasible() || orbitCols.empty()) return numFixed;
HighsInt numOrbits = orbitStarts.size() - 1;
for (HighsInt i = 0; i < numOrbits; ++i) {
HighsInt fixcol = -1;
for (HighsInt j = orbitStarts[i]; j < orbitStarts[i + 1]; ++j) {
if (domain.isFixed(orbitCols[j])) {
fixcol = orbitCols[j];
break;
}
}
if (fixcol != -1) {
HighsInt oldNumFixed = numFixed;
auto oldSize = domain.getDomainChangeStack().size();
if (domain.col_lower_[fixcol] == 1.0) {
for (HighsInt j = orbitStarts[i]; j < orbitStarts[i + 1]; ++j) {
if (domain.col_lower_[orbitCols[j]] == 1.0) continue;
++numFixed;
domain.changeBound(HighsBoundType::kLower, orbitCols[j], 1.0,
HighsDomain::Reason::unspecified());
if (domain.infeasible()) return numFixed;
}
} else {
for (HighsInt j = orbitStarts[i]; j < orbitStarts[i + 1]; ++j) {
if (domain.col_upper_[orbitCols[j]] == 0.0) continue;
++numFixed;
domain.changeBound(HighsBoundType::kUpper, orbitCols[j], 0.0,
HighsDomain::Reason::unspecified());
if (domain.infeasible()) return numFixed;
}
}
HighsInt newFixed = numFixed - oldNumFixed;
if (newFixed != 0) {
domain.propagate();
if (domain.infeasible()) return numFixed;
if ((HighsInt)(domain.getDomainChangeStack().size() - oldSize) >
newFixed)
i = -1;
}
}
}
return numFixed;
}
bool StabilizerOrbits::isStabilized(HighsInt col) const {
return symmetries->columnPosition[col] == -1 ||
std::binary_search(stabilizedCols.begin(), stabilizedCols.end(), col);
}
void HighsOrbitopeMatrix::determineOrbitopeType(HighsCliqueTable& cliquetable) {
for (HighsInt j = 0; j < rowLength; ++j) {
for (HighsInt i = 0; i < numRows; ++i) {
columnToRow.insert(entry(i, j), i);
}
}
rowIsSetPacking.assign(numRows, -1);
numSetPackingRows = 0;
for (HighsInt j = 1; j < rowLength; ++j) {
HighsInt* colj1 = &entry(0, j);
for (HighsInt j0 = 0; j0 < j; ++j0) {
HighsInt* colj0 = &entry(0, j0);
for (HighsInt i = 0; i < numRows; ++i) {
if (rowIsSetPacking[i] != -1) continue;
HighsInt xj0 = colj0[i];
HighsInt xj1 = colj1[i];
auto commonClique = cliquetable.findCommonClique({xj0, 1}, {xj1, 1});
if (commonClique.first == nullptr) {
rowIsSetPacking[i] = false;
continue;
}
HighsInt overlap = 0;
for (HighsInt k = 0; k < commonClique.second; ++k) {
if (commonClique.first[k].val == 0) continue;
HighsInt* cliqueColRow = columnToRow.find(commonClique.first[k].col);
if (cliqueColRow && *cliqueColRow == i) ++overlap;
}
if (overlap == rowLength) {
rowIsSetPacking[i] = true;
++numSetPackingRows;
if (numSetPackingRows == numRows) break;
}
}
if (numSetPackingRows == numRows) break;
}
if (numSetPackingRows == numRows) break;
}
// now for the rows that do not have a set packing structure check
// if we have such structure when all columns in the row are negated
for (HighsInt i = 0; i < numRows; ++i) {
if (!rowIsSetPacking[i]) rowIsSetPacking[i] = -1;
}
for (HighsInt j = 1; j < rowLength; ++j) {
HighsInt* colj1 = &entry(0, j);
for (HighsInt j0 = 0; j0 < j; ++j0) {
HighsInt* colj0 = &entry(0, j0);
for (HighsInt i = 0; i < numRows; ++i) {
if (rowIsSetPacking[i] != -1) continue;
HighsInt xj0 = colj0[i];
HighsInt xj1 = colj1[i];
// now look for cliques with value 0
auto commonClique = cliquetable.findCommonClique({xj0, 0}, {xj1, 0});
if (commonClique.first == nullptr) {
rowIsSetPacking[i] = false;
continue;
}
HighsInt overlap = 0;
for (HighsInt k = 0; k < commonClique.second; ++k) {
// skip clique variables with values of 1
if (commonClique.first[k].val == 1) continue;
HighsInt* cliqueColRow = columnToRow.find(commonClique.first[k].col);
if (cliqueColRow && *cliqueColRow == i) ++overlap;
}
if (overlap == rowLength) {
// mark with value 2, for negated set packing row with at most one
// zero
rowIsSetPacking[i] = 2;
++numSetPackingRows;
if (numSetPackingRows == numRows) break;
}
}
if (numSetPackingRows == numRows) break;
}
if (numSetPackingRows == numRows) break;
}
}
HighsInt HighsOrbitopeMatrix::getBranchingColumn(
const std::vector<double>& colLower, const std::vector<double>& colUpper,
HighsInt col) const {
const HighsInt* i = columnToRow.find(col);
if (i && rowIsSetPacking[*i]) {
for (HighsInt j = 0; j < rowLength; ++j) {
HighsInt branchCol = entry(*i, j);
if (branchCol == col) break;
if (colLower[branchCol] != colUpper[branchCol]) return branchCol;
}
}
return col;
}
HighsInt HighsOrbitopeMatrix::orbitalFixingForPackingOrbitope(
const std::vector<HighsInt>& rows, HighsDomain& domain) const {
HighsInt numDynamicRows = rows.size();
// printf("propagate packing orbitope\n");
std::vector<HighsInt> firstOneInRow(numDynamicRows, -1);
for (HighsInt j = 0; j < rowLength; ++j) {
for (HighsInt i = 0; i < numDynamicRows; ++i) {
if (firstOneInRow[i] != -1) continue;
HighsInt r = rows[i];
HighsInt colrj = entry(r, j);
if (rowIsSetPacking[r] == 1) {
if (domain.col_lower_[colrj] > 0.5) firstOneInRow[i] = j;
} else {
assert(rowIsSetPacking[r] == 2);
if (domain.col_upper_[colrj] < 0.5) firstOneInRow[i] = j;
}
}
}
// we start looping over the rows and keep the index j at the column that
// is the right most possible location for a 1 entry.
// For the first row this position is 0.
HighsInt j = 0;
HighsInt numFixed = 0;
for (HighsInt i = 0; i < numDynamicRows; ++i) {
// at this position we know that the last possible position
// of a 1-entry in this row is j. If we have a 1 behind j
// the face of the orbitope intersecting the set of initial fixings is empty
if (firstOneInRow[i] > j) {
domain.markInfeasible();
// printf("packing orbitope propagation found infeasibility\n");
return numFixed;
}
HighsInt col_ij = entry(rows[i], j);
bool negate_i = rowIsSetPacking[rows[i]] == 2;
bool notZeroFixed = negate_i ? domain.col_lower_[col_ij] < 0.5
: domain.col_upper_[col_ij] > 0.5;
// as long as the entry is fixed to zero
// the frontier stays at the same column
// if we encounter an entry that is not fixed to zero
// we need to proceed with the next column and found a frontier step
if (notZeroFixed) {
// found a frontier step. Now we first check for the current column
// if it can be fixed to 1.
// For this we check if we find an infeasibility in the case where this
// column was fixed to zero in which case the frontier would have stayed
// at j for the following rows.
HighsInt j0 = j;
for (HighsInt k = i + 1; k < numDynamicRows; ++k) {
if (firstOneInRow[k] > j0) {
if (negate_i)
domain.changeBound(HighsBoundType::kUpper, col_ij, 0.0,
HighsDomain::Reason::unspecified());
else
domain.changeBound(HighsBoundType::kLower, col_ij, 1.0,
HighsDomain::Reason::unspecified());
++numFixed;
if (domain.infeasible()) {
// printf("packing orbitope propagation found infeasibility\n");
return numFixed;
}
break;
}
bool negate_k = rowIsSetPacking[rows[k]] == 2;
bool notZeroFixed = negate_k
? domain.col_lower_[entry(rows[k], j0)] < 0.5
: domain.col_upper_[entry(rows[k], j0)] > 0.5;
if (notZeroFixed) {
++j0;
if (j0 == rowLength) break;
}
}
++j;
if (j == rowLength) break;
for (HighsInt k = 0; k <= i; ++k) {
// we should have checked this row at the frontier position
assert(firstOneInRow[k] < j);
HighsInt col_kj = entry(rows[k], j);
bool negate_k = rowIsSetPacking[rows[k]] == 2;
if (negate_k) {
if (domain.col_lower_[col_kj] > 0.5) continue;
domain.changeBound(HighsBoundType::kLower, col_kj, 1.0,
HighsDomain::Reason::unspecified());
} else {
if (domain.col_upper_[col_kj] < 0.5) continue;
domain.changeBound(HighsBoundType::kUpper, col_kj, 0.0,
HighsDomain::Reason::unspecified());
}
++numFixed;
if (domain.infeasible()) {
// this can happen due to deductions from earlier fixings
// otherwise it would have been caught by the infeasibility
// check within the next loop that goes over i
// printf("packing orbitope propagation found infeasibility\n");
return numFixed;
}
}
}
}
// check if there are more columns that can be completely fixed to zero
while (++j < rowLength) {
for (HighsInt k = 0; k < numDynamicRows; ++k) {
// we should have checked this row at the frontier position
assert(firstOneInRow[k] < j);
HighsInt col_kj = entry(rows[k], j);
bool negate_k = rowIsSetPacking[rows[k]] == 2;
if (negate_k) {
if (domain.col_lower_[col_kj] > 0.5) continue;
domain.changeBound(HighsBoundType::kLower, col_kj, 1.0,
HighsDomain::Reason::unspecified());
} else {
if (domain.col_upper_[col_kj] < 0.5) continue;
domain.changeBound(HighsBoundType::kUpper, col_kj, 0.0,
HighsDomain::Reason::unspecified());
}
// printf("new fixed\n");
++numFixed;
if (domain.infeasible()) {
// this can happen due to deductions from earlier fixings
// otherwise it would have been caught by the infeasibility
// check within the next loop that goes over i
// printf("packing orbitope propagation found infeasibility\n");
return numFixed;
}
}
}
if (!domain.infeasible() && numFixed) domain.propagate();
// if (numFixed)
// printf("orbital fixing for packing case fixed %d columns\n", numFixed);
return numFixed;
}
HighsInt HighsOrbitopeMatrix::orbitalFixingForFullOrbitope(
const std::vector<HighsInt>& rows, HighsDomain& domain) const {
HighsInt numDynamicRows = rows.size();
std::vector<int8_t> Mminimal(numDynamicRows * rowLength, -1);
for (HighsInt j = 0; j < rowLength; ++j) {
for (HighsInt i = 0; i < numDynamicRows; ++i) {
HighsInt r = rows[i];
HighsInt colij = matrix[r + j * numRows];
if (domain.col_lower_[colij] == 1.0)
Mminimal[i + j * numDynamicRows] = 1;
else if (domain.col_upper_[colij] == 0.0)
Mminimal[i + j * numDynamicRows] = 0;
}
}
std::vector<int8_t> Mmaximal = Mminimal;
int8_t* MminimaljLast = Mminimal.data() + numDynamicRows * (rowLength - 1);
int8_t* MmaximaljFirst = Mmaximal.data();
for (HighsInt k = 0; k < numDynamicRows; ++k) {
if (MminimaljLast[k] == -1) MminimaljLast[k] = 0;
if (MmaximaljFirst[k] == -1) MmaximaljFirst[k] = 1;
}
auto i_fixed = [&](const int8_t* colj0, const int8_t* colj1) {
for (HighsInt i = 0; i < numDynamicRows; ++i) {
if (colj0[i] != -1 && colj1[i] != -1 && colj0[i] != colj1[i]) return i;
}
return numDynamicRows;
};
auto i_discr = [&](const int8_t* colj0, const int8_t* colj1, HighsInt i_f) {
for (HighsInt i = i_f; i >= 0; --i) {
if (colj0[i] != 0 && colj1[i] != 1) return i;
}
return HighsInt{-1};
};
for (HighsInt j = rowLength - 2; j >= 0; --j) {
int8_t* colj0 = Mminimal.data() + j * numDynamicRows;
int8_t* colj1 = colj0 + numDynamicRows;
HighsInt i_f = i_fixed(colj0, colj1);
if (i_f == numDynamicRows) {
for (HighsInt k = 0; k < numDynamicRows; ++k) {
int8_t isFree = (colj0[k] == -1);
colj0[k] += (isFree & colj1[k]) + isFree;
}
} else {
HighsInt i_d = i_discr(colj0, colj1, i_f);
if (i_d == -1) {
domain.markInfeasible();
return 0;
}
for (HighsInt k = 0; k < i_d; ++k) {
int8_t isFree = (colj0[k] == -1);
colj0[k] += (isFree & colj1[k]) + isFree;
}
colj0[i_d] = 1;
for (HighsInt k = i_d + 1; k < numDynamicRows; ++k)
colj0[k] += (colj0[k] == -1);
}
}
for (HighsInt j = 1; j < rowLength; ++j) {
int8_t* colj0 = Mmaximal.data() + j * numDynamicRows;
int8_t* colj1 = colj0 - numDynamicRows;
HighsInt i_f = i_fixed(colj1, colj0);
if (i_f == numDynamicRows) {
for (HighsInt k = 0; k < numDynamicRows; ++k) {
int8_t isFree = (colj0[k] == -1);
colj0[k] += (isFree & colj1[k]) + isFree;
}
} else {
HighsInt i_d = i_discr(colj1, colj0, i_f);
if (i_d == -1) {
domain.markInfeasible();
return 0;
}
for (HighsInt k = 0; k < i_d; ++k) {
int8_t isFree = (colj0[k] == -1);
colj0[k] += (isFree & colj1[k]) + isFree;
}
colj0[i_d] = 0;
for (HighsInt k = i_d + 1; k < numDynamicRows; ++k)
colj0[k] += 2 * (colj0[k] == -1);
}
}
HighsInt numFixed = 0;
for (HighsInt j = 0; j < rowLength; ++j) {
const int8_t* colMaximal = Mmaximal.data() + j * numDynamicRows;
const int8_t* colMinimal = Mminimal.data() + j * numDynamicRows;
for (HighsInt i = 0; i < numDynamicRows; ++i) {
if (colMinimal[i] != colMaximal[i]) {
assert(colMinimal[i] < colMaximal[i]);
break;
}
HighsInt r = rows[i];
HighsInt colrj = matrix[r + j * numRows];
if (domain.isFixed(colrj)) continue;
++numFixed;
if (colMinimal[i] == 1)
domain.changeBound(HighsBoundType::kLower, colrj, 1.0,
HighsDomain::Reason::unspecified());
else
domain.changeBound(HighsBoundType::kUpper, colrj, 0.0,
HighsDomain::Reason::unspecified());
if (domain.infeasible()) break;
}
if (domain.infeasible()) break;
}
if (!domain.infeasible()) domain.propagate();
return numFixed;
}
HighsInt HighsOrbitopeMatrix::orbitalFixing(HighsDomain& domain) const {
std::vector<HighsInt> rows;
std::vector<uint8_t> rowUsed(numRows);
rows.reserve(numRows);
const auto& branchpos = domain.getBranchingPositions();
const auto& domchgstack = domain.getDomainChangeStack();
bool isPacking = true;
for (HighsInt pos : branchpos) {
const HighsInt* i = columnToRow.find(domchgstack[pos].column);
if (i && !rowUsed[*i]) {
rowUsed[*i] = true;
isPacking = isPacking && rowIsSetPacking[*i] != 0;
rows.push_back(*i);
}
}
if (rows.empty()) return 0;
if (isPacking) return orbitalFixingForPackingOrbitope(rows, domain);
return orbitalFixingForFullOrbitope(rows, domain);
}
void HighsSymmetryDetection::initializeGroundSet() {
vertexGroundSet = currentPartition;
pdqsort(vertexGroundSet.begin(), vertexGroundSet.end());
vertexPosition.resize(vertexToCell.size(), -1);
for (HighsInt i = 0; i < numVertices; ++i)
vertexPosition[vertexGroundSet[i]] = i;
orbitPartition.resize(numVertices);
std::iota(orbitPartition.begin(), orbitPartition.end(), 0);
orbitSize.assign(numVertices, 1);
automorphisms.resize(numVertices * 64);
numAutomorphisms = 0;
currNodeCertificate.reserve(numVertices);
}
bool HighsSymmetryDetection::mergeOrbits(HighsInt v1, HighsInt v2) {
if (v1 == v2) return false;
HighsInt orbit1 = getOrbit(v1);
HighsInt orbit2 = getOrbit(v2);
if (orbit1 == orbit2) return false;
if (orbit1 < orbit2) {
orbitPartition[orbit2] = orbit1;
orbitSize[orbit1] += orbitSize[orbit2];
} else {
orbitPartition[orbit1] = orbit2;
orbitSize[orbit2] += orbitSize[orbit1];
}
return true;
}
HighsInt HighsSymmetryDetection::getOrbit(HighsInt vertex) {
HighsInt i = vertexPosition[vertex];
HighsInt orbit = orbitPartition[i];
if (orbit != orbitPartition[orbit]) {
do {
linkCompressionStack.push_back(i);
i = orbit;
orbit = orbitPartition[orbit];
} while (orbit != orbitPartition[orbit]);
do {
i = linkCompressionStack.back();
linkCompressionStack.pop_back();
orbitPartition[i] = orbit;
} while (!linkCompressionStack.empty());
}
return orbit;
}
void HighsSymmetryDetection::initializeHashValues() {
for (HighsInt i = 0; i != numVertices; ++i) {
HighsInt cell = vertexToCell[i];
for (HighsInt j = Gstart[i]; j != Gend[i]; ++j) {
HighsHashHelpers::sparse_combine32(vertexHash[Gedge[j].first], cell,
Gedge[j].second);
}
markCellForRefinement(cell);
}
}
bool HighsSymmetryDetection::updateCellMembership(HighsInt i, HighsInt cell,
bool markForRefinement) {
HighsInt vertex = currentPartition[i];
if (vertexToCell[vertex] != cell) {
// set new cell id
vertexToCell[vertex] = cell;
if (i != cell) currentPartitionLinks[i] = cell;
// update hashes of affected rows
if (markForRefinement) {
for (HighsInt j = Gstart[vertex]; j != Gend[vertex]; ++j) {
HighsInt edgeDestinationCell = vertexToCell[Gedge[j].first];
if (cellSize(edgeDestinationCell) == 1) continue;
HighsHashHelpers::sparse_combine32(vertexHash[Gedge[j].first], cell,
Gedge[j].second);
markCellForRefinement(edgeDestinationCell);
}
}
return true;
}
return false;
}
bool HighsSymmetryDetection::splitCell(HighsInt cell, HighsInt splitPoint) {
u32 hSplit = getVertexHash(currentPartition[splitPoint]);
u32 hCell = getVertexHash(currentPartition[cell]);
u32 certificateVal =
(HighsHashHelpers::pair_hash<0>(hSplit, hCell) +
HighsHashHelpers::pair_hash<1>(
cell, currentPartitionLinks[cell] - splitPoint) +
HighsHashHelpers::pair_hash<2>(splitPoint, splitPoint - cell)) >>
32;
// employ prefix pruning scheme as in bliss
if (!firstLeaveCertificate.empty()) {
firstLeavePrefixLen +=
(firstLeavePrefixLen == (HighsInt)currNodeCertificate.size()) *
(certificateVal == firstLeaveCertificate[currNodeCertificate.size()]);
bestLeavePrefixLen +=
(bestLeavePrefixLen == (HighsInt)currNodeCertificate.size()) *
(certificateVal == bestLeaveCertificate[currNodeCertificate.size()]);
// if the node certificate is not a prefix of the first leave's certificate
// and it comes lexicographically after the certificate value of the
// lexicographically smallest leave certificate we prune the node
if (firstLeavePrefixLen <= (HighsInt)currNodeCertificate.size() &&
bestLeavePrefixLen <= (HighsInt)currNodeCertificate.size()) {
u32 diffVal = bestLeavePrefixLen == (HighsInt)currNodeCertificate.size()
? certificateVal
: currNodeCertificate[bestLeavePrefixLen];
if (diffVal > bestLeaveCertificate[bestLeavePrefixLen]) return false;
}
}
currentPartitionLinks[splitPoint] = currentPartitionLinks[cell];
currentPartitionLinks[cell] = splitPoint;
cellCreationStack.push_back(splitPoint);
currNodeCertificate.push_back(certificateVal);
return true;
}
void HighsSymmetryDetection::markCellForRefinement(HighsInt cell) {
if (cellSize(cell) == 1 || cellInRefinementQueue[cell]) return;
cellInRefinementQueue[cell] = true;
refinementQueue.push_back(cell);
std::push_heap(refinementQueue.begin(), refinementQueue.end(),
std::greater<HighsInt>());
}
HighsSymmetryDetection::u32 HighsSymmetryDetection::getVertexHash(HighsInt v) {
const u32* h = vertexHash.find(v);
if (h) return *h;
return 0;
}
bool HighsSymmetryDetection::partitionRefinement() {
while (!refinementQueue.empty()) {
std::pop_heap(refinementQueue.begin(), refinementQueue.end(),
std::greater<HighsInt>());
HighsInt cellStart = refinementQueue.back();
HighsInt firstCellStart = cellStart;
refinementQueue.pop_back();
cellInRefinementQueue[cellStart] = false;
if (cellSize(cellStart) == 1) continue;
HighsInt cellEnd = currentPartitionLinks[cellStart];
assert(cellEnd >= cellStart);
// first check which vertices do have updated hash values and put them to
// the end of the partition
HighsInt refineStart =
std::partition(
currentPartition.begin() + cellStart,
currentPartition.begin() + cellEnd,
[&](HighsInt v) { return vertexHash.find(v) == nullptr; }) -
currentPartition.begin();
// if there are none there is nothing to refine
if (refineStart == cellEnd) continue;
// sort the vertices that have updated hash values by their hash values
pdqsort(currentPartition.begin() + refineStart,
currentPartition.begin() + cellEnd, [&](HighsInt v1, HighsInt v2) {
return vertexHash[v1] < vertexHash[v2];
});
// if not all vertices have updated hash values directly create the first
// new cell at the start of the range that we want to refine
if (refineStart != cellStart) {
assert(refineStart != cellStart);
assert(refineStart != cellEnd);
if (!splitCell(cellStart, refineStart)) {
// node can be pruned, make sure hash values are cleared and queue is
// empty
for (HighsInt c : refinementQueue) cellInRefinementQueue[c] = false;
refinementQueue.clear();
vertexHash.clear();
return false;
}
cellStart = refineStart;
updateCellMembership(cellStart, cellStart);
}
// now update the remaining vertices
bool prune = false;
HighsInt i;
assert(vertexHash.find(currentPartition[cellStart]) != nullptr);
// store value of first hash
u64 lastHash = vertexHash[currentPartition[cellStart]];
for (i = cellStart + 1; i < cellEnd; ++i) {
HighsInt vertex = currentPartition[i];
// get this vertex hash value
u64 hash = vertexHash[vertex];
if (hash != lastHash) {
// hash values do not match -> start of new cell
if (!splitCell(cellStart, i)) {
// refinement process yielded bad prefix of certificate
// -> node can be pruned
prune = true;
break;
}
cellStart = i;
// remember hash value of this new cell under lastHash
lastHash = hash;
}
// update membership of vertex to new cell
updateCellMembership(i, cellStart);
}
if (prune) {
// node can be pruned, make sure hash values are cleared and queue is
// empty
for (HighsInt c : refinementQueue) cellInRefinementQueue[c] = false;
refinementQueue.clear();
vertexHash.clear();
currentPartitionLinks[firstCellStart] = cellEnd;
// undo possibly incomplete changes done to the cells
for (--i; i >= refineStart; --i)
updateCellMembership(i, firstCellStart, false);
return false;
}
assert(currentPartitionLinks[cellStart] == cellEnd);
}
vertexHash.clear();
return true;
}
HighsInt HighsSymmetryDetection::selectTargetCell() const {
HighsInt i = 0;
if (nodeStack.size() > 1) i = nodeStack[nodeStack.size() - 2].targetCell;
while (i < numVertices) {
if (cellSize(i) > 1) return i;
++i;
}
return -1;
}
bool HighsSymmetryDetection::checkStoredAutomorphism(HighsInt vertex) const {