-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathHighsCliqueTable.cpp
More file actions
2238 lines (1873 loc) · 76.2 KB
/
HighsCliqueTable.cpp
File metadata and controls
2238 lines (1873 loc) · 76.2 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 */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "mip/HighsCliqueTable.h"
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <numeric>
#include "../extern/pdqsort/pdqsort.h"
#include "mip/HighsCutPool.h"
#include "mip/HighsDomain.h"
#include "mip/HighsMipSolver.h"
#include "mip/HighsMipSolverData.h"
#include "parallel/HighsCombinable.h"
#include "parallel/HighsParallel.h"
#include "presolve/HighsPostsolveStack.h"
#include "util/HighsSplay.h"
#define ADD_ZERO_WEIGHT_VARS
static std::pair<HighsCliqueTable::CliqueVar, HighsCliqueTable::CliqueVar>
sortedEdge(HighsCliqueTable::CliqueVar v1, HighsCliqueTable::CliqueVar v2) {
if (v1.col > v2.col) return std::make_pair(v2, v1);
return std::make_pair(v1, v2);
}
void HighsCliqueTable::unlink(HighsInt pos, HighsInt cliqueid) {
assert(pos >= 0);
--numcliquesvar[cliqueentries[pos].index()];
HighsInt cliquelen = cliques[cliqueid].end - cliques[cliqueid].start;
if (cliquelen == 2)
invertedHashListSizeTwo[cliqueentries[pos].index()].erase(cliqueid);
else
invertedHashList[cliqueentries[pos].index()].erase(cliqueid);
}
void HighsCliqueTable::link(HighsInt pos, HighsInt cliqueid) {
assert(pos >= 0);
assert(!colDeleted[cliqueentries[pos].col]);
++numcliquesvar[cliqueentries[pos].index()];
HighsInt cliquelen = cliques[cliqueid].end - cliques[cliqueid].start;
if (cliquelen == 2)
invertedHashListSizeTwo[cliqueentries[pos].index()].insert(cliqueid);
else
invertedHashList[cliqueentries[pos].index()].insert(cliqueid, pos);
}
HighsInt HighsCliqueTable::findCommonCliqueId(int64_t& numQueries, CliqueVar v1,
CliqueVar v2) const {
++numQueries;
if (!invertedHashListSizeTwo[v1.index()].empty() &&
!invertedHashListSizeTwo[v2.index()].empty()) {
const HighsInt* sizeTwoCliqueId = sizeTwoCliques.find(sortedEdge(v1, v2));
if (sizeTwoCliqueId != nullptr) return *sizeTwoCliqueId;
}
const HighsHashTree<HighsInt, HighsInt>& h1 = invertedHashList[v1.index()];
const HighsHashTree<HighsInt, HighsInt>& h2 = invertedHashList[v2.index()];
const HighsHashTableEntry<HighsInt, HighsInt>* commonClique =
h1.find_common(h2);
if (commonClique) return commonClique->key();
return -1;
}
void HighsCliqueTable::resolveSubstitution(CliqueVar& v) const {
while (colsubstituted[v.col]) {
Substitution subst = substitutions[colsubstituted[v.col] - 1];
v = v.val == 1 ? subst.replace : subst.replace.complement();
}
}
void HighsCliqueTable::resolveSubstitution(HighsInt& col, double& val,
double& offset) const {
while (colsubstituted[col]) {
Substitution subst = substitutions[colsubstituted[col] - 1];
if (subst.replace.val == 0) {
offset += val;
val = -val;
}
col = subst.replace.col;
}
}
HighsInt HighsCliqueTable::runCliqueSubsumption(
const HighsDomain& globaldom, std::vector<CliqueVar>& clique) {
// remove deleted cols
clique.erase(
std::remove_if(clique.begin(), clique.end(),
[&](CliqueVar clqvar) { return colDeleted[clqvar.col]; }),
clique.end());
// return if clique does not have enough elements
if (clique.size() <= 2) return 0;
// callback for removing cliques
HighsInt nremoved = 0;
auto removeCliques = [&](HighsInt cliqueid) {
++nremoved;
cliques[cliqueid].origin = kHighsIInf;
removeClique(cliqueid);
};
// check clique
bool redundant;
HighsInt dominatingOrigin;
cliqueSubsumption(clique, redundant, dominatingOrigin, removeCliques);
if (redundant) clique.clear();
if (!infeasvertexstack.empty()) {
clique.erase(
std::remove_if(clique.begin(), clique.end(),
[&](CliqueVar v) { return globaldom.isFixed(v.col); }),
clique.end());
}
return nremoved;
}
void HighsCliqueTable::cliqueSubsumption(
const std::vector<CliqueVar>& clique, bool& redundant,
HighsInt& dominatingOrigin,
std::function<void(HighsInt)> removeCliqueCallback) {
// collect indices of cliques that contain variables from the
// provided vector
collectCliques(clique);
// initialise
redundant = false;
dominatingOrigin = kHighsIInf;
for (HighsInt cliqueid : cliquehitinds) {
// remember hits and zero out vector
HighsInt hits = cliquehits[cliqueid];
cliquehits[cliqueid] = 0;
if (hits == static_cast<HighsInt>(clique.size())) {
// clique is redundant
redundant = true;
if (cliques[cliqueid].origin != kHighsIInf &&
cliques[cliqueid].origin != -1)
dominatingOrigin = cliques[cliqueid].origin;
} else if (cliques[cliqueid].numActive() == hits) {
// clique is subset of another clique
if (cliques[cliqueid].equality) {
// subset of clique is an equality clique (stored in the clique table),
// thus the remaining variables can be fixed
bool sizeTwo = cliques[cliqueid].end - cliques[cliqueid].start == 2;
for (CliqueVar v : clique) {
bool vHasClq =
sizeTwo ? invertedHashListSizeTwo[v.index()].contains(cliqueid)
: invertedHashList[v.index()].contains(cliqueid);
if (!vHasClq) infeasvertexstack.push_back(v);
}
} else {
// clique from clique table contains a subset of the variables, thus it
// can be removed
removeCliqueCallback(cliqueid);
}
}
}
// clear vector of cliques indices
cliquehitinds.clear();
}
void HighsCliqueTable::collectCliques(const std::vector<CliqueVar>& clique) {
// resize vector
if (cliquehits.size() < cliques.size()) cliquehits.resize(cliques.size());
// collect indices of cliques that contain variables from the
// provided vector
for (CliqueVar v : clique) {
invertedHashList[v.index()].for_each([&](HighsInt cliqueid) {
if (cliquehits[cliqueid] == 0) cliquehitinds.push_back(cliqueid);
++cliquehits[cliqueid];
});
invertedHashListSizeTwo[v.index()].for_each([&](HighsInt cliqueid) {
if (cliquehits[cliqueid] == 0) cliquehitinds.push_back(cliqueid);
++cliquehits[cliqueid];
});
}
}
void HighsCliqueTable::bronKerboschRecurse(BronKerboschData& data,
HighsInt Plen, const CliqueVar* X,
HighsInt Xlen) const {
double w = data.wR;
for (HighsInt i = 0; i != Plen; ++i) w += data.P[i].weight(data.sol);
if (w < data.minW - data.feastol) return;
if (Plen == 0 && Xlen == 0) {
std::vector<CliqueVar> clique = data.R;
if (data.minW < w - data.feastol) {
data.maxcliques -= data.cliques.size();
data.cliques.clear();
data.minW = w;
}
data.cliques.emplace_back(std::move(clique));
// do not further search for cliques that are violated less than this
// current clique
return;
}
++data.ncalls;
if (data.stop()) return;
double pivweight = -1.0;
CliqueVar pivot{0, 0};
for (HighsInt i = 0; i != Xlen; ++i) {
if (X[i].weight(data.sol) > pivweight) {
pivweight = X[i].weight(data.sol);
pivot = X[i];
if (pivweight >= 1.0 - data.feastol) break;
}
}
if (pivweight < 1.0 - data.feastol) {
for (HighsInt i = 0; i != Plen; ++i) {
if (data.P[i].weight(data.sol) > pivweight) {
pivweight = data.P[i].weight(data.sol);
pivot = data.P[i];
if (pivweight >= 1.0 - data.feastol) break;
}
}
}
std::vector<CliqueVar> PminusNu;
PminusNu.reserve(Plen);
queryNeighbourhood(data.neighbourhoodInds, data.numNeighbourhoodQueries,
pivot, data.P.data(), Plen);
data.neighbourhoodInds.push_back(Plen);
HighsInt k = 0;
for (HighsInt i : data.neighbourhoodInds) {
while (k < i) PminusNu.push_back(data.P[k++]);
++k;
}
pdqsort(PminusNu.begin(), PminusNu.end(), [&](CliqueVar a, CliqueVar b) {
return std::make_pair(a.weight(data.sol), a.index()) >
std::make_pair(b.weight(data.sol), b.index());
});
std::vector<CliqueVar> localX;
localX.insert(localX.end(), X, X + Xlen);
for (CliqueVar v : PminusNu) {
HighsInt newPlen = partitionNeighbourhood(data.neighbourhoodInds,
data.numNeighbourhoodQueries, v,
data.P.data(), Plen);
HighsInt newXlen = partitionNeighbourhood(data.neighbourhoodInds,
data.numNeighbourhoodQueries, v,
localX.data(), localX.size());
// add v to R, update the weight, and do the recursive call
data.R.push_back(v);
double wv = v.weight(data.sol);
data.wR += wv;
bronKerboschRecurse(data, newPlen, localX.data(), newXlen);
if (data.stop()) return;
// remove v from R restore the weight and continue the loop in this call
data.R.pop_back();
data.wR -= wv;
w -= wv;
if (w < data.minW) return;
// find the position of v in the vertices removed from P for the recursive
// call
// and also remove it from the set P for this call
HighsInt vpos = -1;
for (HighsInt i = newPlen; i != Plen; ++i) {
if (data.P[i] == v) {
vpos = i;
break;
}
}
// do the removal by first swapping it to the end of P and reduce the size
// of P accordingly
assert(vpos != -1);
--Plen;
std::swap(data.P[vpos], data.P[Plen]);
localX.push_back(v);
}
}
#if 0
static void printRow(const HighsDomain& domain, const HighsInt* inds,
const double* vals, HighsInt len, double lhs, double rhs) {
printf("%g <= ", lhs);
for (HighsInt i = 0; i != len; ++i) {
char sign = vals[i] > 0 ? '+' : '-';
char var = domain.isBinary(inds[i]) ? 'x' : 'y';
printf("%c%g %c%" HIGHSINT_FORMAT " ", sign, std::abs(vals[i]), var, inds[i]);
}
printf("<= %g\n", rhs);
}
static void printClique(
const std::vector<HighsCliqueTable::CliqueVar>& clique) {
bool first = true;
for (HighsCliqueTable::CliqueVar v : clique) {
if (!first) printf("+ ");
char complemented = v.val == 0 ? '~' : ' ';
printf("%cx%" HIGHSINT_FORMAT " ", complemented, v.col);
first = false;
}
printf("<= 1\n");
}
#endif
void HighsCliqueTable::doAddClique(const CliqueVar* cliquevars,
HighsInt numcliquevars, bool equality,
HighsInt origin) {
HighsInt cliqueid;
if (freeslots.empty()) {
cliqueid = cliques.size();
cliques.emplace_back();
} else {
cliqueid = freeslots.back();
freeslots.pop_back();
}
cliques[cliqueid].equality = equality;
cliques[cliqueid].origin = origin;
decltype(freespaces)::iterator it;
HighsInt maxEnd;
if (freespaces.empty() ||
(it = freespaces.lower_bound(
std::make_pair(numcliquevars, HighsInt{-1}))) == freespaces.end()) {
cliques[cliqueid].start = cliqueentries.size();
cliques[cliqueid].end = cliques[cliqueid].start + numcliquevars;
maxEnd = cliques[cliqueid].end;
cliqueentries.resize(cliques[cliqueid].end);
} else {
auto freespace = *it;
freespaces.erase(it);
cliques[cliqueid].start = freespace.second;
cliques[cliqueid].end = cliques[cliqueid].start + numcliquevars;
maxEnd = cliques[cliqueid].start + freespace.first;
}
cliques[cliqueid].numZeroFixed = 0;
bool fixtozero = false;
HighsInt k = cliques[cliqueid].start;
for (HighsInt i = 0; i != numcliquevars; ++i) {
CliqueVar v = cliquevars[i];
resolveSubstitution(v);
if (fixtozero) {
infeasvertexstack.push_back(v);
continue;
}
// due to substitutions the variable may occur together with its complement
// in this clique and we can fix all other variables in the clique to zero:
// x + ~x + ... <= 1
// <=> x + 1 - x + ... <= 1
// <=> ... <= 0
bool clqHasVCompl =
numcliquevars == 2
? invertedHashListSizeTwo[v.complement().index()].contains(cliqueid)
: invertedHashList[v.complement().index()].contains(cliqueid);
if (clqHasVCompl) {
fixtozero = true;
for (HighsInt j = cliques[cliqueid].start; j != k; ++j) {
if (cliqueentries[j].col != v.col)
infeasvertexstack.push_back(cliqueentries[j]);
unlink(j, cliqueid);
}
k = cliques[cliqueid].start;
continue;
}
// due to substitutions the variable may occur twice in this clique and
// we can fix it to zero: x + x + ... <= 1 <=> 2x <= 1 <=> x <= 0.5 <=>
// x = 0
bool inserted;
if (numcliquevars == 2)
inserted = invertedHashListSizeTwo[v.index()].insert(cliqueid);
else
inserted = invertedHashList[v.index()].insert(cliqueid, k);
if (!inserted) {
infeasvertexstack.push_back(v);
continue;
}
cliqueentries[k] = v;
++numcliquesvar[v.index()];
++k;
}
if (maxEnd > k) {
if (int(cliqueentries.size()) == maxEnd)
cliqueentries.resize(k);
else
freespaces.emplace(maxEnd - k, k);
if (cliques[cliqueid].end > k) {
switch (k - cliques[cliqueid].start) {
case 0:
// clique empty, so just mark it as deleted
cliques[cliqueid].start = -1;
cliques[cliqueid].end = -1;
freeslots.push_back(cliqueid);
return;
case 1:
// size 1 clique is redundant, so unlink the single linked entry
// and mark it as deleted
unlink(cliques[cliqueid].start, cliqueid);
cliques[cliqueid].start = -1;
cliques[cliqueid].end = -1;
freeslots.push_back(cliqueid);
return;
case 2:
// due to substitutions the clique became smaller and is now of size
// two as a result we need to link it to the size two cliqueset
// instead of the normal cliqueset
assert(cliqueid >= 0 &&
cliqueid < static_cast<HighsInt>(cliques.size()));
assert(cliques[cliqueid].start >= 0 &&
cliques[cliqueid].start <
static_cast<HighsInt>(cliqueentries.size()));
unlink(cliques[cliqueid].start, cliqueid);
unlink(cliques[cliqueid].start + 1, cliqueid);
cliques[cliqueid].end = k;
link(cliques[cliqueid].start, cliqueid);
link(cliques[cliqueid].start + 1, cliqueid);
break;
default:
cliques[cliqueid].end = k;
}
}
}
HighsInt cliqueLen = cliques[cliqueid].end - cliques[cliqueid].start;
numEntries += cliqueLen;
if (cliqueLen == 2)
sizeTwoCliques.insert(
sortedEdge(cliqueentries[cliques[cliqueid].start],
cliqueentries[cliques[cliqueid].start + 1]),
cliqueid);
}
struct ThreadNeighbourhoodQueryData {
int64_t numQueries;
std::vector<HighsInt> neighbourhoodInds;
};
void HighsCliqueTable::queryNeighbourhood(
std::vector<HighsInt>& neighbourhoodInds, int64_t& numQueries, CliqueVar v,
CliqueVar* q, HighsInt N) const {
neighbourhoodInds.clear();
if (numCliques(v) == 0) return;
if (numEntries - sizeTwoCliques.size() * 2 < minEntriesForParallelism) {
for (HighsInt i = 0; i < N; ++i) {
if (haveCommonClique(numQueries, v, q[i])) neighbourhoodInds.push_back(i);
}
} else {
auto neighbourhoodData =
makeHighsCombinable<ThreadNeighbourhoodQueryData>([N]() {
ThreadNeighbourhoodQueryData d;
d.neighbourhoodInds.reserve(N);
d.numQueries = 0;
return d;
});
highs::parallel::for_each(
0, N,
[this, &neighbourhoodData, v, q](HighsInt start, HighsInt end) {
ThreadNeighbourhoodQueryData& d = neighbourhoodData.local();
for (HighsInt i = start; i < end; ++i) {
if (haveCommonClique(d.numQueries, v, q[i]))
d.neighbourhoodInds.push_back(i);
}
},
10);
neighbourhoodData.combine_each([&](ThreadNeighbourhoodQueryData& d) {
neighbourhoodInds.insert(neighbourhoodInds.end(),
d.neighbourhoodInds.begin(),
d.neighbourhoodInds.end());
numQueries += d.numQueries;
});
pdqsort(neighbourhoodInds.begin(), neighbourhoodInds.end());
}
}
HighsInt HighsCliqueTable::partitionNeighbourhood(
std::vector<HighsInt>& neighbourhoodInds, int64_t& numQueries, CliqueVar v,
CliqueVar* q, HighsInt N) const {
queryNeighbourhood(neighbourhoodInds, numQueries, v, q, N);
for (size_t i = 0; i < neighbourhoodInds.size(); ++i)
std::swap(q[i], q[neighbourhoodInds[i]]);
return static_cast<HighsInt>(neighbourhoodInds.size());
}
HighsInt HighsCliqueTable::shrinkToNeighbourhood(
std::vector<HighsInt>& neighbourhoodInds, int64_t& numQueries, CliqueVar v,
CliqueVar* q, HighsInt N) {
queryNeighbourhood(neighbourhoodInds, numQueries, v, q, N);
for (size_t i = 0; i < neighbourhoodInds.size(); ++i)
q[i] = q[neighbourhoodInds[i]];
return static_cast<HighsInt>(neighbourhoodInds.size());
}
bool HighsCliqueTable::fixCol(HighsDomain& globaldom, CliqueVar v,
bool doProcessInfeasibleVertices) {
bool wasfixed = globaldom.isFixed(v.col);
globaldom.fixCol(v.col, static_cast<double>(1 - v.val));
if (globaldom.infeasible()) return false;
if (!wasfixed) {
++nfixings;
infeasvertexstack.push_back(v);
if (doProcessInfeasibleVertices) processInfeasibleVertices(globaldom);
}
return true;
}
bool HighsCliqueTable::processNewEdge(HighsDomain& globaldom, CliqueVar v1,
CliqueVar v2) {
if (v1.col == v2.col) {
if (v1.val == v2.val) {
fixCol(globaldom, v1, true);
return false;
}
return true;
}
// invertedEdgeCache.erase(sortedEdge(v1, v2));
if (haveCommonClique(v1.complement(), v2)) {
fixCol(globaldom, v2, true);
return false;
} else if (haveCommonClique(v2.complement(), v1)) {
fixCol(globaldom, v1, true);
return false;
} else {
// return if complements are not in a clique
if (!foundCover(globaldom, v1.complement(), v2.complement())) return false;
if (globaldom.infeasible()) return true;
foundCover(globaldom, v1, v2);
if (globaldom.isFixed(v1.col) || globaldom.isFixed(v2.col) ||
globaldom.infeasible())
return true;
Substitution substitution;
if (v2.col < v1.col) {
if (v1.val == 1) v2 = v2.complement();
substitution.substcol = v1.col;
substitution.replace = v2;
} else {
if (v2.val == 1) v1 = v1.complement();
substitution.substcol = v2.col;
substitution.replace = v1;
}
substitutions.push_back(substitution);
colsubstituted[substitution.substcol] = substitutions.size();
auto replace = [&](CliqueVar substitutedVar, CliqueVar replacementVar) {
HighsHashTree<HighsInt, HighsInt>& substList =
invertedHashList[substitutedVar.index()];
HighsHashTree<HighsInt, HighsInt>& replaceList =
invertedHashList[replacementVar.index()];
numcliquesvar[replacementVar.index()] +=
numcliquesvar[substitutedVar.index()];
numcliquesvar[substitutedVar.index()] = 0;
substList.for_each([&](HighsInt cliqueid, HighsInt location) {
replaceList.insert(cliqueid, location);
cliqueentries[location] = replacementVar;
});
substList.clear();
HighsHashTree<HighsInt>& substListSizeTwo =
invertedHashListSizeTwo[substitutedVar.index()];
HighsHashTree<HighsInt>& replaceListSizeTwo =
invertedHashListSizeTwo[replacementVar.index()];
substListSizeTwo.for_each([&](HighsInt cliqueid) {
HighsInt pos = cliques[cliqueid].start;
HighsInt otherPos = pos + 1;
if (cliqueentries[otherPos] == substitutedVar) std::swap(pos, otherPos);
replaceListSizeTwo.insert(cliqueid);
cliqueentries[pos] = replacementVar;
sizeTwoCliques.erase(
sortedEdge(substitutedVar, cliqueentries[otherPos]));
sizeTwoCliques.insert(
sortedEdge(replacementVar, cliqueentries[otherPos]), cliqueid);
});
substListSizeTwo.clear();
};
replace(CliqueVar(substitution.substcol, 1), substitution.replace);
replace(CliqueVar(substitution.substcol, 0),
substitution.replace.complement());
return true;
}
}
void HighsCliqueTable::addClique(const HighsMipSolver& mipsolver,
CliqueVar* cliquevars, HighsInt numcliquevars,
bool equality, HighsInt origin) {
HighsDomain& globaldom = mipsolver.mipdata_->getDomain();
mipsolver.mipdata_->debugSolution.checkClique(cliquevars, numcliquevars);
const HighsInt maxNumCliqueVars = 100;
// lambda for complementing clique
auto complementClique = [&]() {
for (HighsInt i = 0; i != numcliquevars; ++i) {
cliquevars[i] = cliquevars[i].complement();
}
};
// lambda for analysing the clique to see if all variables can be fixed
auto fixAllVarsInClique = [&](bool& hasNewEdge) {
for (HighsInt i = 0; i != numcliquevars; ++i) {
if (!globaldom.isFixed(cliquevars[i].col) ||
cliquevars[i].val != globaldom.col_lower_[cliquevars[i].col])
continue;
// column is fixed to 1, every other entry can be fixed to zero
for (HighsInt k = 0; k != numcliquevars; ++k) {
if (k == i) continue;
if (!fixCol(globaldom, cliquevars[k])) return false;
}
processInfeasibleVertices(globaldom);
return true;
}
if (numcliquevars > maxNumCliqueVars) return false;
// todo, sort new clique to allow log n lookup of membership in size by
// binary search
for (HighsInt i = 0; i < numcliquevars - 1; ++i) {
if (globaldom.isFixed(cliquevars[i].col)) continue;
if (numCliques(cliquevars[i]) == 0 &&
numCliques(cliquevars[i].complement()) == 0) {
hasNewEdge = true;
continue;
}
for (HighsInt j = i + 1; j < numcliquevars; ++j) {
if (globaldom.isFixed(cliquevars[j].col)) continue;
if (haveCommonClique(cliquevars[i], cliquevars[j])) continue;
// todo: Instead of haveCommonClique use findCommonClique. If the
// common clique is smaller than this clique check if it is a subset
// of this clique. If it is a subset remove the clique and iterate the
// process until either a common clique that is not a subset of this
// one is found, or no common clique exists anymore in which case we
// proceed with the code below and set hasNewEdge to true
hasNewEdge = true;
bool iscover = processNewEdge(globaldom, cliquevars[i], cliquevars[j]);
if (globaldom.infeasible()) return false;
if (!mipsolver.mipdata_->nodequeue.empty()) {
const auto& v1Nodes =
cliquevars[i].val == 1
? mipsolver.mipdata_->nodequeue.getUpNodes(cliquevars[i].col)
: mipsolver.mipdata_->nodequeue.getDownNodes(
cliquevars[i].col);
const auto& v2Nodes =
cliquevars[j].val == 1
? mipsolver.mipdata_->nodequeue.getUpNodes(cliquevars[j].col)
: mipsolver.mipdata_->nodequeue.getDownNodes(
cliquevars[j].col);
if (!v1Nodes.empty() && !v2Nodes.empty()) {
// general integer variables can become binary during the search
// and the cliques might be discovered. That means we need to take
// care here, since the set of nodes branched upwards or downwards
// are not necessarily containing domain changes setting the
// variables to the corresponding clique value but could be
// redundant bound changes setting the upper bound to u >= 1 or
// the lower bound to l <= 0.
// itV1 will point to the first node where v1 is fixed to val and
// endV1 to the end of the range of such nodes. Same for
// itV2/endV2 with v2.
auto itV1 = v1Nodes.lower_bound(std::make_pair(
static_cast<double>(cliquevars[i].val), kHighsIInf));
auto endV1 = v1Nodes.upper_bound(std::make_pair(
static_cast<double>(cliquevars[i].val), kHighsIInf));
auto itV2 = v2Nodes.lower_bound(std::make_pair(
static_cast<double>(cliquevars[j].val), kHighsIInf));
auto endV2 = v2Nodes.upper_bound(std::make_pair(
static_cast<double>(cliquevars[j].val), kHighsIInf));
if (itV1 != endV1 && itV2 != endV2 &&
(itV1->second <= std::prev(endV2)->second ||
itV2->second <= std::prev(endV1)->second)) {
// node ranges overlap, check for nodes that can be pruned
while (itV1 != endV1 && itV2 != endV2) {
if (itV1->second < itV2->second) {
++itV1;
} else if (itV2->second < itV1->second) {
++itV2;
} else {
// if (!mipsolver.submip)
// printf("node %d can be pruned\n", itV2->second);
HighsInt prunedNode = itV2->second;
++itV1;
++itV2;
mipsolver.mipdata_->pruned_treeweight +=
mipsolver.mipdata_->nodequeue.pruneNode(prunedNode);
}
}
}
}
}
if (iscover) {
for (HighsInt k = 0; k != numcliquevars; ++k) {
if (k == i || k == j) continue;
if (!fixCol(globaldom, cliquevars[k])) return false;
}
processInfeasibleVertices(globaldom);
return true;
}
}
}
return false;
};
// lambda for checking the (complemented) clique
auto checkClique = [&](bool& hasNewEdge, bool complement) {
if (complement) complementClique();
bool done = fixAllVarsInClique(hasNewEdge);
if (complement) complementClique();
if (done) return true;
return false;
};
// resolve substitutions
for (HighsInt i = 0; i != numcliquevars; ++i) {
resolveSubstitution(cliquevars[i]);
}
// initialise flag
bool hasNewEdge = false;
// check if all variables can be fixed or infeasibility is detected
if (checkClique(hasNewEdge, false)) return;
if (globaldom.infeasible()) return;
if (numcliquevars == 2 && equality) {
// try complemented clique
if (checkClique(hasNewEdge, true)) return;
if (globaldom.infeasible()) return;
}
if (!hasNewEdge && origin == kHighsIInf) return;
CliqueVar* unfixedend =
std::remove_if(cliquevars, cliquevars + numcliquevars,
[&](CliqueVar v) { return globaldom.isFixed(v.col); });
numcliquevars = unfixedend - cliquevars;
if (numcliquevars < 2) return;
doAddClique(cliquevars, numcliquevars, equality, origin);
processInfeasibleVertices(globaldom);
}
void HighsCliqueTable::removeClique(HighsInt cliqueid) {
if (cliques[cliqueid].origin != kHighsIInf && cliques[cliqueid].origin != -1)
deletedrows.push_back(cliques[cliqueid].origin);
HighsInt start = cliques[cliqueid].start;
assert(start != -1);
HighsInt end = cliques[cliqueid].end;
HighsInt len = end - start;
if (len == 2) {
sizeTwoCliques.erase(
sortedEdge(cliqueentries[start], cliqueentries[start + 1]));
}
for (HighsInt i = start; i != end; ++i) {
unlink(i, cliqueid);
}
freeslots.push_back(cliqueid);
freespaces.emplace(len, start);
cliques[cliqueid].start = -1;
cliques[cliqueid].end = -1;
numEntries -= len;
}
void HighsCliqueTable::extractCliques(
const HighsMipSolver& mipsolver, std::vector<HighsInt>& inds,
std::vector<double>& vals, std::vector<int8_t>& complementation, double rhs,
HighsInt nbin, std::vector<HighsInt>& perm, std::vector<CliqueVar>& clique,
double feastol) {
HighsImplications& implics = mipsolver.mipdata_->implications;
HighsDomain& globaldom = mipsolver.mipdata_->getDomain();
perm.resize(inds.size());
std::iota(perm.begin(), perm.end(), 0);
auto binaryend = std::partition(perm.begin(), perm.end(), [&](HighsInt pos) {
return globaldom.isBinary(inds[pos]);
});
nbin = binaryend - perm.begin();
HighsInt ntotal = static_cast<HighsInt>(perm.size());
// if not all variables are binary, we extract variable upper and lower bounds
// constraints on the non-binary variable for each binary variable in the
// constraint
if (nbin < ntotal) {
for (HighsInt i = 0; i != nbin; ++i) {
HighsInt bincol = inds[perm[i]];
HighsCDouble impliedub = static_cast<HighsCDouble>(rhs) - vals[perm[i]];
if (implics.tooManyVarBounds()) break;
for (HighsInt j = nbin; j != ntotal; ++j) {
HighsInt col = inds[perm[j]];
if (globaldom.isFixed(col)) continue;
HighsCDouble colub =
static_cast<HighsCDouble>(globaldom.col_upper_[col]) -
globaldom.col_lower_[col];
HighsCDouble implcolub = impliedub / vals[perm[j]];
if (mipsolver.isColIntegral(col))
implcolub = std::floor(static_cast<double>(implcolub) +
mipsolver.mipdata_->feastol);
if (implcolub < colub - feastol) {
HighsCDouble coef;
HighsCDouble constant;
if (complementation[perm[i]] == -1) {
coef = colub - implcolub;
constant = implcolub;
} else {
coef = implcolub - colub;
constant = colub;
}
if (complementation[perm[j]] == -1) {
constant -= globaldom.col_upper_[col];
implics.addVLB(col, bincol, -static_cast<double>(coef),
-static_cast<double>(constant));
} else {
constant += globaldom.col_lower_[col];
implics.addVUB(col, bincol, static_cast<double>(coef),
static_cast<double>(constant));
}
}
}
}
}
// only one binary means we do have no cliques
if (nbin <= 1) return;
pdqsort(perm.begin(), binaryend, [&](HighsInt p1, HighsInt p2) {
return std::make_pair(vals[p1], p1) > std::make_pair(vals[p2], p2);
});
// check if any cliques exists
if (vals[perm[0]] + vals[perm[1]] <= rhs + feastol) return;
// check if this is a set packing constraint (or easily transformable
// into one)
if (std::abs(vals[0] - vals[perm[nbin - 1]]) <= feastol &&
rhs < 2 * vals[perm[nbin - 1]] - feastol) {
// the coefficients on the binary variables are all equal and the
// right hand side is strictly below two times the coefficient value.
// Therefore the constraint can be transformed into a set packing
// constraint by relaxing out all non-binary variables (if any),
// dividing by the coefficient value of the binary variables, and then
// possibly rounding down the right hand side
clique.clear();
for (auto j = 0; j != nbin; ++j) {
HighsInt pos = perm[j];
if (complementation[pos] == -1)
clique.emplace_back(inds[pos], 0);
else
clique.emplace_back(inds[pos], 1);
}
addClique(mipsolver, clique.data(), nbin);
if (globaldom.infeasible()) return;
// printf("extracted this clique:\n");
// printClique(clique);
return;
}
for (HighsInt k = nbin - 1; k != 0; --k) {
double mincliqueval = rhs - vals[perm[k]] + feastol;
auto cliqueend = std::partition_point(
perm.begin(), perm.begin() + k,
[&](HighsInt p) { return vals[p] > mincliqueval; });
// no clique for this variable
if (cliqueend == perm.begin()) continue;
clique.clear();
for (auto j = perm.begin(); j != cliqueend; ++j) {
HighsInt pos = *j;
if (complementation[pos] == -1)
clique.emplace_back(inds[pos], 0);
else
clique.emplace_back(inds[pos], 1);
}
if (complementation[perm[k]] == -1)
clique.emplace_back(inds[perm[k]], 0);
else
clique.emplace_back(inds[perm[k]], 1);
// printf("extracted this clique:\n");
// printClique(clique);
if (clique.size() >= 2) {
// if (clique.size() > 2) runCliqueSubsumption(globaldom, clique);
// runCliqueMerging(globaldom, clique);
// if (clique.size() >= 2) {
addClique(mipsolver, clique.data(), static_cast<HighsInt>(clique.size()));
if (globaldom.infeasible()) return;
//}
}
// further cliques are just subsets of this clique
if (cliqueend == perm.begin() + k) return;
}
}
void HighsCliqueTable::cliquePartition(std::vector<CliqueVar>& clqVars,
std::vector<HighsInt>& partitionStart) {
randgen.shuffle(clqVars.data(), clqVars.size());
std::vector<HighsInt> neighbourhoodInds;
neighbourhoodInds.reserve(clqVars.size());
HighsInt numClqVars = clqVars.size();
partitionStart.clear();
partitionStart.reserve(clqVars.size());
HighsInt extensionEnd = numClqVars;
partitionStart.push_back(0);
for (HighsInt i = 0; i < numClqVars; ++i) {
if (i == extensionEnd) {
partitionStart.push_back(i);
extensionEnd = numClqVars;
}
CliqueVar v = clqVars[i];
HighsInt extensionStart = i + 1;
extensionEnd =
partitionNeighbourhood(neighbourhoodInds, numNeighbourhoodQueries, v,
clqVars.data() + extensionStart,
extensionEnd - extensionStart) +