-
Notifications
You must be signed in to change notification settings - Fork 14
/
CglGomory.cpp
2076 lines (2042 loc) · 57.8 KB
/
CglGomory.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
// Copyright (C) 2002, International Business Machines
// Corporation and others. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <cfloat>
#include <cassert>
#include <iostream>
//#define CGL_DEBUG 1
#ifdef NDEBUG
//#undef NDEBUG
#endif
#include "CoinPragma.hpp"
#include "CoinHelperFunctions.hpp"
#include "CoinPackedVector.hpp"
#include "CoinPackedMatrix.hpp"
#include "CoinIndexedVector.hpp"
#include "OsiRowCutDebugger.hpp"
#ifndef USE_CGL_RATIONAL
#define USE_CGL_RATIONAL 0
#endif
// -1 no rational, 0 as before, 1 use code from CglGmi
#if USE_CGL_RATIONAL>0
#if USE_CGL_RATIONAL<=10
#error "If USE_CGL_RATIONAL>0 must be at least 10 (maybe not more than 1000)"
#endif
#include "CoinRational.hpp"
#endif
#define CGL_HAS_CLP_GOMORY
#ifdef CGL_HAS_CLP_GOMORY
#include "OsiClpSolverInterface.hpp"
#endif
#include "CoinFactorization.hpp"
#undef CLP_OSL
#if COINUTILS_BIGINDEX_IS_INT
#define CLP_OSL 1
#if CLP_OSL!=1&&CLP_OSL!=3
#undef CLP_OSL
#else
#include "CoinOslFactorization.hpp"
#endif
#endif
#include "CoinWarmStartBasis.hpp"
#include "CglGomory.hpp"
#include "CoinFinite.hpp"
#ifdef CGL_DEBUG_GOMORY
int gomory_try=CGL_DEBUG_GOMORY;
#endif
//-------------------------------------------------------------------
// Generate Gomory cuts
//-------------------------------------------------------------------
void CglGomory::generateCuts(const OsiSolverInterface & si, OsiCuts & cs,
const CglTreeInfo info)
{
#ifdef CGL_DEBUG_GOMORY
gomory_try++;
#endif
// Get basic problem information
int numberColumns=si.getNumCols();
// get integer variables and basis
char * intVar = new char[numberColumns];
int i;
CoinWarmStart * warmstart = si.getWarmStart();
CoinWarmStartBasis* warm =
dynamic_cast<CoinWarmStartBasis*>(warmstart);
const double * colUpper = si.getColUpper();
const double * colLower = si.getColLower();
//#define CLP_INVESTIGATE2
#ifndef CLP_INVESTIGATE2
if ((info.options&16)!=0)
#endif
printf("%d %d %d\n",info.inTree,info.options,info.pass);
for (i=0;i<numberColumns;i++) {
if (si.isInteger(i)) {
if (colUpper[i]>colLower[i]+0.5) {
if (fabs(colUpper[i]-1.0)<1.0e-12&&fabs(colLower[i])<1.0e-12) {
intVar[i]=1; //0-1
} else if (colLower[i]>=0.0) {
intVar[i] = 2; // other
} else {
// negative bounds - I am not sure works
intVar[i] = 3;
}
} else {
intVar[i] = 4;
}
} else {
intVar[i]=0;
}
}
const OsiSolverInterface * useSolver=&si;
#ifdef CGL_HAS_CLP_GOMORY
double * objective = NULL;
OsiClpSolverInterface * clpSolver = dynamic_cast<OsiClpSolverInterface *>(originalSolver_);
int numberOriginalRows = -1;
if (clpSolver) {
useSolver = originalSolver_;
assert (gomoryType_);
// check simplex is plausible
if (!clpSolver->getNumRows()||numberColumns!=clpSolver->getNumCols()) {
delete originalSolver_;
originalSolver_=si.clone();
clpSolver = dynamic_cast<OsiClpSolverInterface *>(originalSolver_);
assert (clpSolver);
useSolver = originalSolver_;
}
ClpSimplex * simplex = clpSolver->getModelPtr();
numberOriginalRows = simplex->numberRows();
int numberRows = si.getNumRows();
assert (numberOriginalRows<=numberRows);
// only do if different (unless type 2x)
int gomoryType = gomoryType_%10;
int whenToDo = gomoryType_/10;
if (whenToDo==2 ||(numberRows>numberOriginalRows && whenToDo==1
&& (info.options&512)==0) ||
((info.options&1024)!=0 && (info.options&512)==0
&& numberTimesStalled_<3)) {
// bounds
memcpy(simplex->columnLower(),colLower,numberColumns*sizeof(double));
memcpy(simplex->columnUpper(),colUpper,numberColumns*sizeof(double));
double * obj = simplex->objective();
objective = CoinCopyOfArray(obj,numberColumns);
const double * pi = si.getRowPrice();
const CoinPackedMatrix * rowCopy = si.getMatrixByRow();
const int * column = rowCopy->getIndices();
const CoinBigIndex * rowStart = rowCopy->getVectorStarts();
const int * rowLength = rowCopy->getVectorLengths();
const double * rowElements = rowCopy->getElements();
const double * rowLower = si.getRowLower();
const double * rowUpper = si.getRowUpper();
int numberCopy;
int numberAdd;
double * rowLower2 = NULL;
double * rowUpper2 = NULL;
int * column2 = NULL;
CoinBigIndex * rowStart2 = NULL;
double * rowElements2 = NULL;
char * copy = new char [numberRows-numberOriginalRows];
memset(copy,0,numberRows-numberOriginalRows);
if (gomoryType==2) {
numberCopy=0;
numberAdd=0;
for (int iRow=numberOriginalRows;iRow<numberRows;iRow++) {
bool simple = true;
for (CoinBigIndex k=rowStart[iRow];
k<rowStart[iRow]+rowLength[iRow];k++) {
double value = rowElements[k];
if (value!=floor(value+0.5)) {
simple=false;
break;
}
}
if (simple) {
numberCopy++;
numberAdd+=rowLength[iRow];
copy[iRow-numberOriginalRows]=1;
}
}
if (numberCopy) {
//printf("Using %d rows out of %d\n",numberCopy,numberRows-numberOriginalRows);
rowLower2 = new double [numberCopy];
rowUpper2 = new double [numberCopy];
rowStart2 = new CoinBigIndex [numberCopy+1];
rowStart2[0]=0;
column2 = new int [numberAdd];
rowElements2 = new double [numberAdd];
}
}
numberCopy=0;
numberAdd=0;
const double * rowSolution = si.getRowActivity();
double offset=0.0;
for (int iRow=numberOriginalRows;iRow<numberRows;iRow++) {
if (!copy[iRow-numberOriginalRows]) {
double value = pi[iRow];
offset += rowSolution[iRow]*value;
for (CoinBigIndex k=rowStart[iRow];
k<rowStart[iRow]+rowLength[iRow];k++) {
int iColumn=column[k];
obj[iColumn] -= value*rowElements[k];
}
} else {
rowLower2[numberCopy]=rowLower[iRow];
rowUpper2[numberCopy]=rowUpper[iRow];
for (CoinBigIndex k=rowStart[iRow];
k<rowStart[iRow]+rowLength[iRow];k++) {
column2[numberAdd]=column[k];
rowElements2[numberAdd++]=rowElements[k];
}
numberCopy++;
rowStart2[numberCopy]=numberAdd;
}
}
#if 0
CoinThreadRandom randomNumberGenerator;
const double * solution = si.getColSolution();
for (int i=0;i<numberColumns;i++) {
if (intVar[i]==1) {
double randomNumber = randomNumberGenerator.randomDouble();
//randomNumber = 0.001*floor(randomNumber*1000.0);
if (solution[i]>0.5)
obj[i] -= randomNumber*0.001*fabs(obj[i]);
else
obj[i] += randomNumber*0.001*fabs(obj[i]);
}
}
#endif
if (numberCopy) {
clpSolver->addRows(numberCopy,
rowStart2,column2,rowElements2,
rowLower2,rowUpper2);
delete [] rowLower2 ;
delete [] rowUpper2 ;
delete [] column2 ;
delete [] rowStart2 ;
delete [] rowElements2 ;
}
delete [] copy;
memcpy(simplex->primalColumnSolution(),si.getColSolution(),
numberColumns*sizeof(double));
warm->resize(numberOriginalRows,numberColumns);
clpSolver->setBasis(*warm);
delete warm;
simplex->setDualObjectiveLimit(COIN_DBL_MAX);
simplex->setLogLevel(0);
simplex->primal(1);
// check basis
int numberTotal=simplex->numberRows()+simplex->numberColumns();
int superbasic=0;
for (int i=0;i<numberTotal;i++) {
if (simplex->getStatus(i)==ClpSimplex::superBasic)
superbasic++;
}
if (superbasic) {
//printf("%d superbasic!\n",superbasic);
simplex->dual();
superbasic=0;
for (int i=0;i<numberTotal;i++) {
if (simplex->getStatus(i)==ClpSimplex::superBasic)
superbasic++;
}
assert (!superbasic);
}
//printf("Trying - %d its status %d objs %g %g - with offset %g\n",
// simplex->numberIterations(),simplex->status(),
// simplex->objectiveValue(),si.getObjValue(),simplex->objectiveValue()+offset);
//simplex->setLogLevel(0);
warm=simplex->getBasis();
warmstart=warm;
if (simplex->status()) {
//printf("BAD status %d\n",simplex->status());
//clpSolver->writeMps("clp");
//si.writeMps("si");
delete [] objective;
objective=NULL;
useSolver=&si;
}
} else {
// don't do
delete warmstart;
warmstart=NULL;
if ((info.options&1024)==0)
numberTimesStalled_=0;
}
}
#endif
#ifdef CGL_DEBUG
const OsiRowCutDebugger * debugger = si.getRowCutDebugger();
if (debugger&&!debugger->onOptimalPath(si))
debugger = NULL;
#else
const OsiRowCutDebugger * debugger = NULL;
#endif
int numberRowCutsBefore = cs.sizeRowCuts();
if (warmstart)
generateCuts(debugger, cs, *useSolver->getMatrixByCol(),
*useSolver->getMatrixByRow(),
useSolver->getColSolution(),
useSolver->getColLower(), useSolver->getColUpper(),
useSolver->getRowLower(), useSolver->getRowUpper(),
intVar,warm,info);
#ifdef CGL_HAS_CLP_GOMORY
if (objective) {
ClpSimplex * simplex = clpSolver->getModelPtr();
memcpy(simplex->objective(),objective,numberColumns*sizeof(double));
delete [] objective;
// take out locally useless cuts
const double * solution = si.getColSolution();
double primalTolerance = 1.0e-7;
int numberRowCutsAfter = cs.sizeRowCuts();
for (int k = numberRowCutsAfter - 1; k >= numberRowCutsBefore; k--) {
const OsiRowCut * thisCut = cs.rowCutPtr(k) ;
double sum = 0.0;
int n = thisCut->row().getNumElements();
const int * column = thisCut->row().getIndices();
const double * element = thisCut->row().getElements();
assert (n);
for (int i = 0; i < n; i++) {
double value = element[i];
sum += value * solution[column[i]];
}
if (sum > thisCut->ub() + primalTolerance) {
sum = sum - thisCut->ub();
} else if (sum < thisCut->lb() - primalTolerance) {
sum = thisCut->lb() - sum;
} else {
sum = 0.0;
}
if (!sum) {
// take out
cs.eraseRowCut(k);
}
}
#ifdef CLP_INVESTIGATE2
printf("OR %p pass %d inTree %c - %d cuts (but %d deleted)\n",
originalSolver_,info.pass,info.inTree?'Y':'N',
numberRowCutsAfter-numberRowCutsBefore,
numberRowCutsAfter-cs.sizeRowCuts());
#endif
}
#endif
delete warmstart;
delete [] intVar;
if ((!info.inTree&&((info.options&4)==4||((info.options&8)&&!info.pass)))
||(info.options&16)!=0) {
int limit = maximumLengthOfCutInTree();
int numberRowCutsAfter = cs.sizeRowCuts();
for (int i=numberRowCutsBefore;i<numberRowCutsAfter;i++) {
int length = cs.rowCutPtr(i)->row().getNumElements();
if (length<=limit)
cs.rowCutPtr(i)->setGloballyValid();
}
}
if ((gomoryType_%10)==2) {
// back to original
assert(clpSolver);
int numberRows = clpSolver->getNumRows();
if (numberRows>numberOriginalRows) {
int numberDelete = numberRows-numberOriginalRows;
int * delRow = new int [numberDelete];
for (int i=0;i<numberDelete;i++)
delRow[i]=i+numberOriginalRows;
clpSolver->deleteRows(numberDelete,delRow);
delete [] delRow;
}
}
}
// Returns value - floor but allowing for small errors
inline double above_integer(double value) {
double value2=floor(value);
double value3=floor(value+0.5);
if (fabs(value3-value)<1.0e-9*(fabs(value3)+1.0))
return 0.0;
return value-value2;
}
#if USE_CGL_RATIONAL <= 0
//-------------------------------------------------------------------
// Returns the greatest common denominator of two
// positive integers, a and b, found using Euclid's algorithm
//-------------------------------------------------------------------
static long long int gcd(long long int a, long long int b)
{
long long int remainder = -1;
#if CGL_DEBUG>1
printf("gcd of %ld and %ld\n",a,b);
int nLoop=0;
#endif
// make sure a<=b (will always remain so)
if(a > b) {
// Swap a and b
long long int temp = a;
a = b;
b = temp;
}
// if zero then gcd is nonzero (zero may occur in rhs of packed)
if (!a) {
if (b) {
return b;
} else {
printf("**** gcd given two zeros!!\n");
abort();
}
}
while (remainder) {
#if CGL_DEBUG>1
nLoop++;
if (nLoop>50) {
abort();
return -1;
}
#endif
remainder = b % a;
b = a;
a = remainder;
}
#if CGL_DEBUG>1
printf("=> %d\n",b);
#endif
return b;
}
#endif
#define GOMORY_LONG
#ifdef GOMORY_LONG
#define SMALL_VALUE1 1.0e-14
#define GOMORY_INT long long int
#else
#define SMALL_VALUE1 1.0e-10
#define GOMORY_INT int
#endif
#if USE_CGL_RATIONAL>0
static long computeGcd(long a, long b) {
// This is the standard Euclidean algorithm for gcd
long remainder = 1;
// Make sure a<=b (will always remain so)
if (a > b) {
// Swap a and b
long temp = a;
a = b;
b = temp;
}
// If zero then gcd is nonzero
if (!a) {
if (b) {
return b;
}
else {
printf("### WARNING: CglGMI::computeGcd() given two zeroes!\n");
exit(1);
}
}
while (remainder) {
remainder = b % a;
b = a;
a = remainder;
}
return b;
} /* computeGcd */
static bool scaleCutIntegral(double* cutElem, int* cutIndex, int cutNz,
double& cutRhs, double maxdelta) {
long gcd, lcm;
double maxscale = 1000;
long maxdnom = USE_CGL_RATIONAL;
//long numerator = 0, denominator = 0;
// Initialize gcd and lcm
CoinRational r = CoinRational(cutRhs, maxdelta, maxdnom);
if (r.getNumerator() != 0){
gcd = labs(r.getNumerator());
lcm = r.getDenominator();
}
else{
#if defined GMI_TRACE_CLEAN
printf("Cannot compute rational number, scaling procedure aborted\n");
#endif
return false;
}
for (int i = 0; i < cutNz; ++i) {
CoinRational r = CoinRational(cutElem[i], maxdelta, maxdnom);
if (r.getNumerator() != 0){
gcd = computeGcd(gcd, r.getNumerator());
lcm *= r.getDenominator()/(computeGcd(lcm,r.getDenominator()));
}
else{
#if defined GMI_TRACE_CLEAN
printf("Cannot compute rational number, scaling procedure aborted\n");
#endif
return false;
}
}
double scale = ((double)lcm)/((double)gcd);
if (fabs(scale) > maxscale) {
#if defined GMI_TRACE_CLEAN
printf("Scaling factor too large, scaling procedure aborted\n");
#endif
return false;
}
scale = fabs(scale);
// Looks like we have a good scaling factor; scale and return;
for (int i = 0; i < cutNz; ++i) {
double value = cutElem[i]*scale;
cutElem[i] = floor(value+0.5);
assert (fabs(cutElem[i]-value)<1.0e-9);
}
{
double value = cutRhs*scale;
cutRhs = floor(value+0.5);
assert (fabs(cutRhs-value)<1.0e-9);
}
return true;
} /* scaleCutIntegral */
#elif USE_CGL_RATIONAL == 0
//-------------------------------------------------------------------
// Returns the nearest rational with denominator < maxDenominator
//-------------------------------------------------------------------
typedef struct {
int numerator;
int denominator;
} Rational;
typedef struct {
GOMORY_INT numerator;
GOMORY_INT denominator;
} RationalLong;
inline Rational nearestRational(double value, int maxDenominator)
{
RationalLong tryThis;
RationalLong tryA;
RationalLong tryB;
double integerPart;
#if CGL_DEBUG>1
printf("Rational of %g is ",value);
#endif
int nLoop=0;
tryA.numerator=0;
tryA.denominator=1;
tryB.numerator=1;
tryB.denominator=0;
Rational returnRational;
if (fabs(value)<SMALL_VALUE1) {
returnRational.numerator=static_cast<int>(tryA.numerator);
returnRational.denominator=static_cast<int>(tryA.denominator);
return returnRational;
}
integerPart = floor(value);
value -= integerPart;
tryThis.numerator = tryB.numerator* static_cast<GOMORY_INT> (integerPart) + tryA.numerator;
tryThis.denominator = tryB.denominator* static_cast<GOMORY_INT> (integerPart) + tryA.denominator;
tryA = tryB;
tryB = tryThis;
while (value>SMALL_VALUE1 && tryB.denominator <=maxDenominator) {
nLoop++;
if (nLoop>50) {
Rational bad;
bad.numerator=-1;
bad.denominator=-1;
#if CGL_DEBUG>1
printf(" *** bad rational\n");
#endif
return bad;
}
value = 1.0/value;
integerPart = floor(value+SMALL_VALUE1);
value -= integerPart;
tryThis.numerator = tryB.numerator* static_cast<GOMORY_INT> (integerPart) + tryA.numerator;
tryThis.denominator = tryB.denominator* static_cast<GOMORY_INT>(integerPart) + tryA.denominator;
tryA = tryB;
tryB = tryThis;
}
if (tryB.denominator <= maxDenominator) {
#if CGL_DEBUG>1
printf("%lld/%lld\n",tryB.numerator,tryB.denominator);
#endif
if (tryB.denominator<COIN_INT_MAX) {
returnRational.numerator=static_cast<int>(tryB.numerator);
returnRational.denominator=static_cast<int>(tryB.denominator);
} else {
returnRational.numerator=-1;
returnRational.denominator=-1;
#if CGL_DEBUG>1
printf(" *** bad rational\n");
#endif
}
} else {
#if CGL_DEBUG>1
printf("%lld/%lld\n",tryA.numerator,tryA.denominator);
#endif
if (tryA.denominator<COIN_INT_MAX) {
returnRational.numerator=static_cast<int>(tryA.numerator);
returnRational.denominator=static_cast<int>(tryA.denominator);
} else {
returnRational.numerator=-1;
returnRational.denominator=-1;
#if CGL_DEBUG>1
printf(" *** bad rational\n");
#endif
}
}
return returnRational;
}
#endif
// Does actual work - returns number of cuts
int
CglGomory::generateCuts(
#ifdef CGL_DEBUG
const OsiRowCutDebugger * debugger,
#else
const OsiRowCutDebugger * ,
#endif
OsiCuts & cs,
const CoinPackedMatrix & columnCopy,
const CoinPackedMatrix & rowCopy,
const double * colsol,
const double * colLower, const double * colUpper,
const double * rowLower, const double * rowUpper,
const char * intVar,
const CoinWarmStartBasis* warm,
const CglTreeInfo info)
{
int infoOptions=info.options;
bool globalCuts = (infoOptions&16)!=0;
double testFixed = (!globalCuts) ? 1.0e-8 : -1.0;
// get what to look at
double away = info.inTree ? away_ : CoinMin(away_,awayAtRoot_);
int numberRows=columnCopy.getNumRows();
int numberColumns=columnCopy.getNumCols();
CoinBigIndex numberElements=columnCopy.getNumElements();
// Allow bigger length on initial matrix (if special setting)
//if (limit==512&&!info.inTree&&!info.pass)
//limit=1024;
// Start of code to create a factorization from warm start (A) ====
// check factorization is okay
CoinFactorization factorization;
#ifdef CLP_OSL
CoinOslFactorization * factorization2=NULL;
if (alternateFactorization_) {
factorization2 = new CoinOslFactorization();
}
#endif
// We can either set increasing rows so ...IsBasic gives pivot row
// or we can just increment iBasic one by one
// for now let ...iBasic give pivot row
int status=-100;
// probably could use pivotVariables from OsiSimplexModel
int * rowIsBasic = new int[numberRows];
int * columnIsBasic = new int[numberColumns];
int i;
int numberBasic=0;
for (i=0;i<numberRows;i++) {
if (warm->getArtifStatus(i) == CoinWarmStartBasis::basic) {
rowIsBasic[i]=1;
numberBasic++;
} else {
rowIsBasic[i]=-1;
}
}
for (i=0;i<numberColumns;i++) {
if (warm->getStructStatus(i) == CoinWarmStartBasis::basic) {
columnIsBasic[i]=1;
numberBasic++;
} else {
columnIsBasic[i]=-1;
}
}
//returns 0 -okay, -1 singular, -2 too many in basis, -99 memory */
while (status<-98) {
#ifdef CLP_OSL
if (!alternateFactorization_) {
#endif
status=factorization.factorize(columnCopy,
rowIsBasic, columnIsBasic);
if (status==-99) factorization.areaFactor(factorization.areaFactor() * 2.0);
#ifdef CLP_OSL
} else {
double areaFactor=1.0;
status=factorization2->factorize(columnCopy,
rowIsBasic, columnIsBasic,areaFactor);
if (status==-99) areaFactor *= 2.0;
}
#endif
}
if (status) {
#ifdef COIN_DEVELOP
std::cout<<"Bad factorization of basis - status "<<status<<std::endl;
#endif
delete [] rowIsBasic;
delete [] columnIsBasic;
return -1;
}
// End of creation of factorization (A) ====
#ifdef CLP_OSL
double relaxation = !alternateFactorization_ ? factorization.conditionNumber() :
factorization2->conditionNumber();
#else
double relaxation = factorization.conditionNumber();
#endif
// if very small be a bit more careful
if (relaxation<1.0e-10)
relaxation=1.0/sqrt(relaxation);
#ifdef COIN_DEVELOP_z
if (relaxation>1.0e49)
printf("condition %g\n",relaxation);
#endif
//printf("condition %g %g\n",relaxation,conditionNumberMultiplier_);
relaxation *= conditionNumberMultiplier_;
double bounds[2]={-COIN_DBL_MAX,0.0};
int iColumn,iRow;
const int * column = rowCopy.getIndices();
const CoinBigIndex * rowStart = rowCopy.getVectorStarts();
const int * rowLength = rowCopy.getVectorLengths();
const double * rowElements = rowCopy.getElements();
const int * row = columnCopy.getIndices();
const CoinBigIndex * columnStart = columnCopy.getVectorStarts();
const int * columnLength = columnCopy.getVectorLengths();
const double * columnElements = columnCopy.getElements();
// we need to do book-keeping for variables at ub
double tolerance = 1.0e-7;
bool * swap= new bool [numberColumns];
for (iColumn=0;iColumn<numberColumns;iColumn++) {
if (columnIsBasic[iColumn]<0&&
colUpper[iColumn]-colsol[iColumn]<=tolerance) {
swap[iColumn]=true;
} else {
swap[iColumn]=false;
}
}
// get row activities (could use solver but lets do here )
double * rowActivity = new double [numberRows];
CoinFillN(rowActivity,numberRows,0.0);
for (iColumn=0;iColumn<numberColumns;iColumn++) {
double value = colsol[iColumn];
CoinBigIndex k;
for (k=columnStart[iColumn];k<columnStart[iColumn]+columnLength[iColumn];k++) {
iRow = row[k];
rowActivity[iRow] += columnElements[k]*value;
}
}
/* we need to mark rows:
0) equality
1) slack at lb (activity at ub)
2) slack at ub (activity at lb)
and 4 bit is set if slack must be integer
*/
int * rowType = new int[numberRows];
for (iRow=0;iRow<numberRows;iRow++) {
if (rowIsBasic[iRow]<0&&rowUpper[iRow]>rowLower[iRow]+1.0e-7) {
int type=0;
double rhs=0.0;
if (rowActivity[iRow]>=rowUpper[iRow]-1.0e-7) {
type=1;
rhs=rowUpper[iRow];
} else if (rowActivity[iRow]<=rowLower[iRow]+1.0e-7) {
type=2;
rhs=rowLower[iRow];
} else {
// probably large rhs
if (rowActivity[iRow]-rowLower[iRow]<
rowUpper[iRow]-rowActivity[iRow])
rowType[iRow]=2;
else
rowType[iRow]=1;
#ifdef CGL_DEBUG
assert (CoinMin(rowUpper[iRow]-rowActivity[iRow],
rowActivity[iRow]-rowUpper[iRow])<1.0e-5);
//abort();
continue;
#else
continue;
#endif
}
if (above_integer(rhs)<1.0e-10) {
// could be integer slack
bool allInteger=true;
CoinBigIndex k;
for (k=rowStart[iRow];
k<rowStart[iRow]+rowLength[iRow];k++) {
int iColumn=column[k];
if (!intVar[iColumn]||above_integer(rowElements[k])>1.0e-10) {
// not integer slacks
allInteger=false;
break;
}
}
if (allInteger) {
type |= 4;
}
}
rowType[iRow]=type;
} else {
// row is equality or basic
rowType[iRow]=0;
}
}
// Start of code to create work arrays for factorization (B) ====
// two vectors for updating (one is work)
CoinIndexedVector work;
CoinIndexedVector array;
// make sure large enough
work.reserve(numberRows);
array.reserve(numberRows);
int * arrayRows = array.getIndices();
double * arrayElements = array.denseVector();
// End of code to create work arrays (B) ====
int numberAdded=0;
// we also need somewhere to accumulate cut
CoinIndexedVector cutVector;
cutVector.reserve(numberColumns+1);
int * cutIndex = cutVector.getIndices();
double * cutElement = cutVector.denseVector();
// and for packed form (as not necessarily in order)
// also space for sort
bool doSorted = (infoOptions&256)!=0;
int lengthArray = static_cast<int>(numberColumns+1+((numberColumns+1)*sizeof(int))/sizeof(double));
if (doSorted)
lengthArray+=numberColumns;
double * packed = new double[lengthArray];
double * sort = packed+numberColumns+1;
int * which = reinterpret_cast<int *>(doSorted ? (sort+numberColumns): (sort));
double tolerance1=1.0e-6;
double tolerance2=0.9;
#if USE_CGL_RATIONAL <= 0
double tolerance3=1.0e-10;
#endif
double tolerance6=1.0e-6;
double tolerance9=1.0e-4;
#define MORE_GOMORY_CUTS 1
#ifdef CLP_INVESTIGATE2
int saveLimit = info.inTree ? 50 : 1000;
#else
#if MORE_GOMORY_CUTS==2||MORE_GOMORY_CUTS==3
int saveLimit;
#endif
#endif
// get limit on length of cut
int limit = 0;
if (!limit_)
dynamicLimitInTree_ = CoinMax(50,numberColumns/40);
if (!info.inTree) {
limit = limitAtRoot_;
if (!info.pass) {
tolerance1=1.0;
tolerance2=1.0e-2;
#if USE_CGL_RATIONAL <= 0
//tolerance3=1.0e-6;
#endif
tolerance6=1.0e-7;
tolerance9=1.0e-5;
if (!limit)
limit=numberColumns;
} else {
if((infoOptions&32)==0/*&&numberTimesStalled_<3*/) {
if (!limit) {
if(numberElements>8*numberColumns)
limit=numberColumns;
else
limit = CoinMax(1000,numberRows/4);
}
} else {
//limit=numberColumns;
//numberTimesStalled_++;
}
}
} else {
limit = limit_;
if (!limit) {
if (!info.pass)
limit = dynamicLimitInTree_;
else
limit=50;
}
}
// If big - allow for rows
if (limit>=numberColumns)
limit += numberRows;
#ifdef CLP_INVESTIGATE2
if (limit>saveLimit&&!info.inTree&&(infoOptions&512)==0)
printf("Gomory limit changed from %d to %d, inTree %c, pass %d, r %d,c %d,e %d\n",
saveLimit,limit,info.inTree ? 'Y' : 'N',info.pass,
numberRows,numberColumns,numberElements);
#endif
int nCandidates=0;
for (iColumn=0;iColumn<numberColumns;iColumn++) {
// This returns pivot row for columns or -1 if not basic (C) ====
int iBasic=columnIsBasic[iColumn];
if (iBasic>=0&&intVar[iColumn]) {
double reducedValue=above_integer(colsol[iColumn]);
//printf("col %d bas %d val %.18g\n",iColumn,iBasic,colsol[iColumn]);
if(intVar[iColumn]&&reducedValue<1.0-away&&reducedValue>away) {
if (doSorted)
sort[nCandidates]=fabs(0.5-reducedValue);
which[nCandidates++]=iColumn;
}
}
}
CoinBigIndex nTotalEls=COIN_INT_MAX;
if (doSorted) {
CoinSort_2(sort,sort+nCandidates,which);
CoinBigIndex nElsNow = columnCopy.getNumElements();
CoinBigIndex nAdd;
CoinBigIndex nAdd2;
CoinBigIndex nReasonable;
int depth=info.level;
if (depth<2) {
nAdd=10000;
if (info.pass>0)
nAdd = CoinMin(nAdd,nElsNow+2*numberRows);
nAdd2 = 5*numberColumns;
nReasonable = CoinMax(nAdd2,nElsNow/8+nAdd);
if (!depth&&!info.pass) {
// allow more
nAdd += nElsNow/2;
nAdd2 += nElsNow/2;
nReasonable += nElsNow/2;
limit=numberRows+numberColumns;
}
} else {
nAdd = 200;
nAdd2 = 2*numberColumns;
nReasonable = CoinMax(nAdd2,nElsNow/8+nAdd);
}
nTotalEls=nReasonable;
}
#ifdef MORE_GOMORY_CUTS
CoinBigIndex saveTotalEls=nTotalEls;
#endif
#if MORE_GOMORY_CUTS==2||MORE_GOMORY_CUTS==3
saveLimit=limit;
if (doSorted)
limit=numberRows+numberColumns;
OsiCuts longCuts;
#endif
#if MORE_GOMORY_CUTS==1||MORE_GOMORY_CUTS==3
OsiCuts secondaryCuts;
#endif
for (int kColumn=0;kColumn<nCandidates;kColumn++) {
if (nTotalEls<=0)
break; // Got enough
iColumn=which[kColumn];
double reducedValue=above_integer(colsol[iColumn]);;
// This returns pivot row for columns or -1 if not basic (C) ====
int iBasic=columnIsBasic[iColumn];
double ratio=reducedValue/(1.0-reducedValue);
if (iBasic>=0) {
// Debug code below computes tableau column of basic ====
int j;
#ifdef CGL_DEBUG
{
// put column into array
array.setVector(columnLength[iColumn],row+columnStart[iColumn],
columnElements+columnStart[iColumn]);
// get column in tableau
#ifdef CLP_OSL
if (!alternateFactorization_)
#endif
factorization.updateColumn ( &work, &array );
#ifdef CLP_OSL
else
factorization2->updateColumn ( &work, &array );
#endif
int nn=0;
int numberInArray=array.getNumElements();
for (j=0;j<numberInArray;j++) {
int indexValue=arrayRows[j];
double value=arrayElements[indexValue];
if (fabs(value)>1.0e-5) {
assert (fabs(value-1.0)<1.0e-7);
assert (indexValue==iBasic);
nn++;
}
}
assert (nn==1);
array.clear();
//work.checkClear();
}
#endif
array.clear();
assert(intVar[iColumn]&&reducedValue<1.0-away&&reducedValue>away);
{
#ifdef CGL_DEBUG
//cutVector.checkClear();
#endif
// get row of tableau
double one =1.0;
array.setVector(1,&iBasic,&one);
int numberNonInteger=0;
//Code below computes tableau row ====
// get pi
#ifdef CLP_OSL
if (!alternateFactorization_)
#endif
factorization.updateColumnTranspose ( &work, &array );
#ifdef CLP_OSL
else
factorization2->updateColumnTranspose ( &work, &array );
#endif
int numberInArray=array.getNumElements();
#ifdef CGL_DEBUG
// check pivot on iColumn
{
double value=0.0;
int k;
// add in row of tableau
for (k=columnStart[iColumn];
k<columnStart[iColumn]+columnLength[iColumn];k++) {
iRow = row[k];