forked from coin-or/Clp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbcMatrix.cpp
3612 lines (3599 loc) · 129 KB
/
AbcMatrix.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, Copyright (C) 2012, FasterCoin. All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).
#include <cstdio>
#include "CoinPragma.hpp"
#include "CoinIndexedVector.hpp"
#include "CoinHelperFunctions.hpp"
#include "CoinAbcHelperFunctions.hpp"
#include "AbcSimplexFactorization.hpp"
#include "AbcPrimalColumnDantzig.hpp"
#include "AbcPrimalColumnSteepest.hpp"
#include "CoinTime.hpp"
#include "AbcSimplex.hpp"
#include "AbcSimplexDual.hpp"
// at end to get min/max!
#include "AbcMatrix.hpp"
#include "ClpMessage.hpp"
#ifdef INTEL_MKL
#include "mkl_spblas.h"
#endif
#if ABC_INSTRUMENT > 1
extern int abcPricing[20];
extern int abcPricingDense[20];
#endif
//=============================================================================
//#############################################################################
// Constructors / Destructor / Assignment
//#############################################################################
//-------------------------------------------------------------------
// Default Constructor
//-------------------------------------------------------------------
AbcMatrix::AbcMatrix()
: matrix_(NULL)
, model_(NULL)
, rowStart_(NULL)
, element_(NULL)
, column_(NULL)
, numberColumnBlocks_(0)
, numberRowBlocks_(0)
,
#ifdef COUNT_COPY
countRealColumn_(NULL)
, countStartLarge_(NULL)
, countRow_(NULL)
, countElement_(NULL)
, smallestCount_(0)
, largestCount_(0)
,
#endif
startFraction_(0.0)
, endFraction_(1.0)
, savedBestDj_(0.0)
, originalWanted_(0)
, currentWanted_(0)
, savedBestSequence_(-1)
, minimumObjectsScan_(-1)
, minimumGoodReducedCosts_(-1)
{
}
//-------------------------------------------------------------------
// Copy constructor
//-------------------------------------------------------------------
AbcMatrix::AbcMatrix(const AbcMatrix &rhs)
{
#ifndef COIN_SPARSE_MATRIX
matrix_ = new CoinPackedMatrix(*(rhs.matrix_), -1, -1);
#else
matrix_ = new CoinPackedMatrix(*(rhs.matrix_), -0, -0);
#endif
model_ = rhs.model_;
rowStart_ = NULL;
element_ = NULL;
column_ = NULL;
#ifdef COUNT_COPY
countRealColumn_ = NULL;
countStartLarge_ = NULL;
countRow_ = NULL;
countElement_ = NULL;
#endif
numberColumnBlocks_ = rhs.numberColumnBlocks_;
CoinAbcMemcpy(startColumnBlock_, rhs.startColumnBlock_, numberColumnBlocks_ + 1);
numberRowBlocks_ = rhs.numberRowBlocks_;
if (numberRowBlocks_) {
assert(model_);
int numberRows = model_->numberRows();
int numberElements = matrix_->getNumElements();
memcpy(blockStart_, rhs.blockStart_, sizeof(blockStart_));
rowStart_ = CoinCopyOfArray(rhs.rowStart_, numberRows * (numberRowBlocks_ + 2));
element_ = CoinCopyOfArray(rhs.element_, numberElements);
column_ = CoinCopyOfArray(rhs.column_, numberElements);
#ifdef COUNT_COPY
smallestCount_ = rhs.smallestCount_;
largestCount_ = rhs.largestCount_;
int numberColumns = model_->numberColumns();
countRealColumn_ = CoinCopyOfArray(rhs.countRealColumn_, numberColumns);
memcpy(countStart_, rhs.countStart_, reinterpret_cast< char * >(&countRealColumn_) - reinterpret_cast< char * >(countStart_));
int numberLarge = numberColumns - countStart_[MAX_COUNT];
countStartLarge_ = CoinCopyOfArray(rhs.countStartLarge_, numberLarge + 1);
numberElements = countStartLarge_[numberLarge];
countElement_ = CoinCopyOfArray(rhs.countElement_, numberElements);
countRow_ = CoinCopyOfArray(rhs.countRow_, numberElements);
#endif
}
}
AbcMatrix::AbcMatrix(const CoinPackedMatrix &rhs)
{
#ifndef COIN_SPARSE_MATRIX
matrix_ = new CoinPackedMatrix(rhs, -1, -1);
#else
matrix_ = new CoinPackedMatrix(rhs, -0, -0);
#endif
matrix_->cleanMatrix();
model_ = NULL;
rowStart_ = NULL;
element_ = NULL;
column_ = NULL;
#ifdef COUNT_COPY
countRealColumn_ = NULL;
countStartLarge_ = NULL;
countRow_ = NULL;
countElement_ = NULL;
smallestCount_ = 0;
largestCount_ = 0;
#endif
numberColumnBlocks_ = 1;
startColumnBlock_[0] = 0;
startColumnBlock_[1] = 0;
numberRowBlocks_ = 0;
startFraction_ = 0;
endFraction_ = 1.0;
savedBestDj_ = 0;
originalWanted_ = 0;
currentWanted_ = 0;
savedBestSequence_ = -1;
minimumObjectsScan_ = -1;
minimumGoodReducedCosts_ = -1;
}
#ifdef ABC_SPRINT
/* Subset constructor (without gaps). */
AbcMatrix::AbcMatrix(const AbcMatrix &wholeMatrix,
int numberRows, const int *whichRows,
int numberColumns, const int *whichColumns)
{
#ifndef COIN_SPARSE_MATRIX
matrix_ = new CoinPackedMatrix(*wholeMatrix.matrix_, numberRows, whichRows,
numberColumns, whichColumns);
#else
matrix_ = new CoinPackedMatrix(rhs, -0, -0);
abort();
#endif
matrix_->cleanMatrix();
model_ = NULL;
rowStart_ = NULL;
element_ = NULL;
column_ = NULL;
#ifdef COUNT_COPY
countRealColumn_ = NULL;
countStartLarge_ = NULL;
countRow_ = NULL;
countElement_ = NULL;
smallestCount_ = 0;
largestCount_ = 0;
#endif
numberColumnBlocks_ = 1;
startColumnBlock_[0] = 0;
startColumnBlock_[1] = 0;
numberRowBlocks_ = 0;
startFraction_ = 0;
endFraction_ = 1.0;
savedBestDj_ = 0;
originalWanted_ = 0;
currentWanted_ = 0;
savedBestSequence_ = -1;
minimumObjectsScan_ = -1;
minimumGoodReducedCosts_ = -1;
}
#endif
//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
AbcMatrix::~AbcMatrix()
{
delete matrix_;
delete[] rowStart_;
delete[] element_;
delete[] column_;
#ifdef COUNT_COPY
delete[] countRealColumn_;
delete[] countStartLarge_;
delete[] countRow_;
delete[] countElement_;
#endif
}
//----------------------------------------------------------------
// Assignment operator
//-------------------------------------------------------------------
AbcMatrix &
AbcMatrix::operator=(const AbcMatrix &rhs)
{
if (this != &rhs) {
delete matrix_;
#ifndef COIN_SPARSE_MATRIX
matrix_ = new CoinPackedMatrix(*(rhs.matrix_));
#else
matrix_ = new CoinPackedMatrix(*(rhs.matrix_), -0, -0);
#endif
model_ = rhs.model_;
delete[] rowStart_;
delete[] element_;
delete[] column_;
#ifdef COUNT_COPY
delete[] countRealColumn_;
delete[] countStartLarge_;
delete[] countRow_;
delete[] countElement_;
#endif
rowStart_ = NULL;
element_ = NULL;
column_ = NULL;
#ifdef COUNT_COPY
countRealColumn_ = NULL;
countStartLarge_ = NULL;
countRow_ = NULL;
countElement_ = NULL;
#endif
numberColumnBlocks_ = rhs.numberColumnBlocks_;
CoinAbcMemcpy(startColumnBlock_, rhs.startColumnBlock_, numberColumnBlocks_ + 1);
numberRowBlocks_ = rhs.numberRowBlocks_;
if (numberRowBlocks_) {
assert(model_);
int numberRows = model_->numberRows();
int numberElements = matrix_->getNumElements();
memcpy(blockStart_, rhs.blockStart_, sizeof(blockStart_));
rowStart_ = CoinCopyOfArray(rhs.rowStart_, numberRows * (numberRowBlocks_ + 2));
element_ = CoinCopyOfArray(rhs.element_, numberElements);
column_ = CoinCopyOfArray(rhs.column_, numberElements);
#ifdef COUNT_COPY
smallestCount_ = rhs.smallestCount_;
largestCount_ = rhs.largestCount_;
int numberColumns = model_->numberColumns();
countRealColumn_ = CoinCopyOfArray(rhs.countRealColumn_, numberColumns);
memcpy(countStart_, rhs.countStart_, reinterpret_cast< char * >(&countRealColumn_) - reinterpret_cast< char * >(countStart_));
int numberLarge = numberColumns - countStart_[MAX_COUNT];
countStartLarge_ = CoinCopyOfArray(rhs.countStartLarge_, numberLarge + 1);
numberElements = countStartLarge_[numberLarge];
countElement_ = CoinCopyOfArray(rhs.countElement_, numberElements);
countRow_ = CoinCopyOfArray(rhs.countRow_, numberElements);
#endif
}
startFraction_ = rhs.startFraction_;
endFraction_ = rhs.endFraction_;
savedBestDj_ = rhs.savedBestDj_;
originalWanted_ = rhs.originalWanted_;
currentWanted_ = rhs.currentWanted_;
savedBestSequence_ = rhs.savedBestSequence_;
minimumObjectsScan_ = rhs.minimumObjectsScan_;
minimumGoodReducedCosts_ = rhs.minimumGoodReducedCosts_;
}
return *this;
}
// Sets model
void AbcMatrix::setModel(AbcSimplex *model)
{
model_ = model;
int numberColumns = model_->numberColumns();
bool needExtension = numberColumns > matrix_->getNumCols();
if (needExtension) {
CoinBigIndex lastElement = matrix_->getNumElements();
matrix_->reserve(numberColumns, lastElement, true);
CoinBigIndex *columnStart = matrix_->getMutableVectorStarts();
for (int i = numberColumns; i >= 0; i--) {
if (columnStart[i] == 0)
columnStart[i] = lastElement;
else
break;
}
assert(lastElement == columnStart[numberColumns]);
}
}
/* Returns a new matrix in reverse order without gaps */
CoinPackedMatrix *
AbcMatrix::reverseOrderedCopy() const
{
CoinPackedMatrix *matrix = new CoinPackedMatrix();
matrix->setExtraGap(0.0);
matrix->setExtraMajor(0.0);
matrix->reverseOrderedCopyOf(*matrix_);
return matrix;
}
/// returns number of elements in column part of basis,
CoinBigIndex
AbcMatrix::countBasis(const int *whichColumn,
int &numberColumnBasic)
{
const int *COIN_RESTRICT columnLength = matrix_->getVectorLengths();
CoinBigIndex numberElements = 0;
int numberRows = model_->numberRows();
// just count - can be over so ignore zero problem
for (int i = 0; i < numberColumnBasic; i++) {
int iColumn = whichColumn[i] - numberRows;
numberElements += columnLength[iColumn];
}
return numberElements;
}
void AbcMatrix::fillBasis(const int *COIN_RESTRICT whichColumn,
int &numberColumnBasic,
int *COIN_RESTRICT indexRowU,
int *COIN_RESTRICT start,
int *COIN_RESTRICT rowCount,
int *COIN_RESTRICT columnCount,
CoinFactorizationDouble *COIN_RESTRICT elementU)
{
const int *COIN_RESTRICT columnLength = matrix_->getVectorLengths();
CoinBigIndex numberElements = start[0];
// fill
const CoinBigIndex *COIN_RESTRICT columnStart = matrix_->getVectorStarts();
const int *COIN_RESTRICT row = matrix_->getIndices();
const double *COIN_RESTRICT elementByColumn = matrix_->getElements();
int numberRows = model_->numberRows();
for (int i = 0; i < numberColumnBasic; i++) {
int iColumn = whichColumn[i] - numberRows;
int length = columnLength[iColumn];
CoinBigIndex startThis = columnStart[iColumn];
columnCount[i] = length;
CoinBigIndex endThis = startThis + length;
for (CoinBigIndex j = startThis; j < endThis; j++) {
int iRow = row[j];
indexRowU[numberElements] = iRow;
rowCount[iRow]++;
assert(elementByColumn[j]);
elementU[numberElements++] = elementByColumn[j];
}
start[i + 1] = numberElements;
}
}
#ifdef ABC_LONG_FACTORIZATION
void AbcMatrix::fillBasis(const int *COIN_RESTRICT whichColumn,
int &numberColumnBasic,
int *COIN_RESTRICT indexRowU,
int *COIN_RESTRICT start,
int *COIN_RESTRICT rowCount,
int *COIN_RESTRICT columnCount,
long double *COIN_RESTRICT elementU)
{
const int *COIN_RESTRICT columnLength = matrix_->getVectorLengths();
CoinBigIndex numberElements = start[0];
// fill
const CoinBigIndex *COIN_RESTRICT columnStart = matrix_->getVectorStarts();
const int *COIN_RESTRICT row = matrix_->getIndices();
const double *COIN_RESTRICT elementByColumn = matrix_->getElements();
int numberRows = model_->numberRows();
for (int i = 0; i < numberColumnBasic; i++) {
int iColumn = whichColumn[i] - numberRows;
int length = columnLength[iColumn];
CoinBigIndex startThis = columnStart[iColumn];
columnCount[i] = length;
CoinBigIndex endThis = startThis + length;
for (CoinBigIndex j = startThis; j < endThis; j++) {
int iRow = row[j];
indexRowU[numberElements] = iRow;
rowCount[iRow]++;
assert(elementByColumn[j]);
elementU[numberElements++] = elementByColumn[j];
}
start[i + 1] = numberElements;
}
}
#endif
#if 0
/// Move largest in column to beginning
void
AbcMatrix::moveLargestToStart()
{
// get matrix data pointers
int * COIN_RESTRICT row = matrix_->getMutableIndices();
const CoinBigIndex * COIN_RESTRICT columnStart = matrix_->getVectorStarts();
double * COIN_RESTRICT elementByColumn = matrix_->getMutableElements();
int numberColumns=model_->numberColumns();
CoinBigIndex start = 0;
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
CoinBigIndex end = columnStart[iColumn+1];
double largest=0.0;
int position=-1;
for (CoinBigIndex j = start; j < end; j++) {
double value = fabs(elementByColumn[j]);
if (value>largest) {
largest=value;
position=j;
}
}
assert (position>=0); // ? empty column
if (position>start) {
double value=elementByColumn[start];
elementByColumn[start]=elementByColumn[position];
elementByColumn[position]=value;
int iRow=row[start];
row[start]=row[position];
row[position]=iRow;
}
start=end;
}
}
#endif
// Creates row copy
void AbcMatrix::createRowCopy()
{
#if ABC_PARALLEL
if (model_->parallelMode() == 0)
#endif
numberRowBlocks_ = 1;
#if ABC_PARALLEL
else
numberRowBlocks_ = CoinMin(NUMBER_ROW_BLOCKS, model_->numberCpus());
#endif
int maximumRows = model_->maximumAbcNumberRows();
int numberRows = model_->numberRows();
int numberColumns = model_->numberColumns();
int numberElements = matrix_->getNumElements();
assert(!rowStart_);
char *whichBlock_ = new char[numberColumns];
rowStart_ = new CoinBigIndex[numberRows * (numberRowBlocks_ + 2)];
element_ = new double[numberElements];
column_ = new int[numberElements];
const CoinBigIndex *COIN_RESTRICT columnStart = matrix_->getVectorStarts();
memset(blockStart_, 0, sizeof(blockStart_));
int ecount[10];
assert(numberRowBlocks_ < 16);
CoinAbcMemset0(ecount, 10);
// allocate to blocks (put a bit less in first as will be dealing with slacks) LATER
CoinBigIndex start = 0;
int block = 0;
CoinBigIndex work = (2 * numberColumns + matrix_->getNumElements() + numberRowBlocks_ - 1) / numberRowBlocks_;
CoinBigIndex thisWork = work;
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
CoinBigIndex end = columnStart[iColumn + 1];
assert(block >= 0 && block < numberRowBlocks_);
whichBlock_[iColumn] = static_cast< char >(block);
thisWork -= 2 + end - start;
ecount[block] += end - start;
start = end;
blockStart_[block]++;
if (thisWork <= 0) {
block++;
thisWork = work;
}
}
#if 0
printf("Blocks ");
for (int i=0;i<numberRowBlocks_;i++)
printf("(%d %d) ",blockStart_[i],ecount[i]);
printf("\n");
#endif
start = 0;
for (int i = 0; i < numberRowBlocks_; i++) {
int next = start + blockStart_[i];
blockStart_[i] = start;
start = next;
}
blockStart_[numberRowBlocks_] = start;
assert(start == numberColumns);
const int *COIN_RESTRICT row = matrix_->getIndices();
const double *COIN_RESTRICT elementByColumn = matrix_->getElements();
// counts
CoinAbcMemset0(rowStart_, numberRows * (numberRowBlocks_ + 2));
int *COIN_RESTRICT last = rowStart_ + numberRows * (numberRowBlocks_ + 1);
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
int block = whichBlock_[iColumn];
blockStart_[block]++;
int base = (block + 1) * numberRows;
for (CoinBigIndex j = columnStart[iColumn]; j < columnStart[iColumn + 1]; j++) {
int iRow = row[j];
rowStart_[base + iRow]++;
last[iRow]++;
}
}
// back to real starts
for (int iBlock = numberRowBlocks_ - 1; iBlock >= 0; iBlock--)
blockStart_[iBlock + 1] = blockStart_[iBlock];
blockStart_[0] = 0;
CoinBigIndex put = 0;
for (int iRow = 1; iRow < numberRows; iRow++) {
int n = last[iRow - 1];
last[iRow - 1] = put;
put += n;
rowStart_[iRow] = put;
}
int n = last[numberRows - 1];
last[numberRows - 1] = put;
put += n;
assert(put == numberElements);
//last[numberRows-1]=put;
// starts
int base = 0;
for (int iBlock = 0; iBlock < numberRowBlocks_; iBlock++) {
for (int iRow = 0; iRow < numberRows; iRow++) {
rowStart_[base + numberRows + iRow] += rowStart_[base + iRow];
}
base += numberRows;
}
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
int block = whichBlock_[iColumn];
int where = blockStart_[block];
blockStart_[block]++;
int base = block * numberRows;
for (CoinBigIndex j = columnStart[iColumn]; j < columnStart[iColumn + 1]; j++) {
int iRow = row[j];
CoinBigIndex put = rowStart_[base + iRow];
rowStart_[base + iRow]++;
column_[put] = where + maximumRows;
element_[put] = elementByColumn[j];
}
}
// back to real starts etc
base = numberRows * numberRowBlocks_;
for (int iBlock = numberRowBlocks_ - 1; iBlock >= 0; iBlock--) {
blockStart_[iBlock + 1] = blockStart_[iBlock] + maximumRows;
CoinAbcMemcpy(rowStart_ + base, rowStart_ + base - numberRows, numberRows);
base -= numberRows;
}
blockStart_[0] = 0; //maximumRows;
delete[] whichBlock_;
// and move
CoinAbcMemcpy(rowStart_, last, numberRows);
// All in useful
CoinAbcMemcpy(rowStart_ + (numberRowBlocks_ + 1) * numberRows,
rowStart_ + (numberRowBlocks_)*numberRows, numberRows);
#ifdef COUNT_COPY
// now blocked by element count
countRealColumn_ = new int[numberColumns];
int counts[2 * MAX_COUNT];
memset(counts, 0, sizeof(counts));
//memset(countFirst_,0,sizeof(countFirst_));
int numberLarge = 0;
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
int n = columnStart[iColumn + 1] - columnStart[iColumn];
if (n < MAX_COUNT)
counts[n]++;
else
numberLarge++;
}
CoinBigIndex numberExtra = 3; // for alignment
#define SMALL_COUNT 1
for (int i = 0; i < MAX_COUNT; i++) {
int n = counts[i];
if (n >= SMALL_COUNT) {
n &= 3;
int extra = (4 - n) & 3;
numberExtra += i * extra;
} else {
// treat as large
numberLarge += n;
}
}
countElement_ = new double[numberElements + numberExtra];
countRow_ = new int[numberElements + numberExtra];
countStartLarge_ = new CoinBigIndex[numberLarge + 1];
countStartLarge_[numberLarge] = numberElements + numberExtra;
//return;
CoinInt64 xx = reinterpret_cast< CoinInt64 >(countElement_);
int iBottom = static_cast< int >(xx & 31);
int offset = iBottom >> 3;
CoinBigIndex firstElementLarge = 0;
if (offset)
firstElementLarge = 4 - offset;
//countStart_[0]=firstElementLarge;
int positionLarge = 0;
smallestCount_ = 0;
largestCount_ = 0;
for (int i = 0; i < MAX_COUNT; i++) {
int n = counts[i];
countFirst_[i] = positionLarge;
countStart_[i] = firstElementLarge;
if (n >= SMALL_COUNT) {
counts[i + MAX_COUNT] = 1;
if (smallestCount_ == 0)
smallestCount_ = i;
largestCount_ = i;
positionLarge += n;
firstElementLarge += n * i;
n &= 3;
int extra = (4 - n) & 3;
firstElementLarge += i * extra;
}
counts[i] = 0;
}
largestCount_++;
countFirst_[MAX_COUNT] = positionLarge;
countStart_[MAX_COUNT] = firstElementLarge;
numberLarge = 0;
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
CoinBigIndex start = columnStart[iColumn];
int n = columnStart[iColumn + 1] - start;
CoinBigIndex put;
int position;
if (n < MAX_COUNT && counts[MAX_COUNT + n] != 0) {
int iWhich = counts[n];
position = countFirst_[n] + iWhich;
int iBlock4 = iWhich & (~3);
iWhich &= 3;
put = countStart_[n] + iBlock4 * n;
put += iWhich;
counts[n]++;
for (int i = 0; i < n; i++) {
countRow_[put] = row[start + i];
countElement_[put] = elementByColumn[start + i];
put += 4;
}
} else {
position = positionLarge + numberLarge;
put = firstElementLarge;
countStartLarge_[numberLarge] = put;
firstElementLarge += n;
numberLarge++;
CoinAbcMemcpy(countRow_ + put, row + start, n);
CoinAbcMemcpy(countElement_ + put, elementByColumn + start, n);
}
countRealColumn_[position] = iColumn + maximumRows;
}
countStartLarge_[numberLarge] = firstElementLarge;
assert(firstElementLarge <= numberElements + numberExtra);
#endif
}
// Make all useful
void AbcMatrix::makeAllUseful(CoinIndexedVector & /*spare*/)
{
int numberRows = model_->numberRows();
CoinBigIndex *COIN_RESTRICT rowEnd = rowStart_ + numberRows;
const CoinBigIndex *COIN_RESTRICT rowReallyEnd = rowStart_ + 2 * numberRows;
for (int iRow = 0; iRow < numberRows; iRow++) {
rowEnd[iRow] = rowReallyEnd[iRow];
}
}
#define SQRT_ARRAY
// Creates scales for column copy (rowCopy in model may be modified)
void AbcMatrix::scale(int numberAlreadyScaled)
{
int numberRows = model_->numberRows();
bool inBranchAndBound = (model_->specialOptions(), 0x1000000) != 0;
bool doScaling = numberAlreadyScaled >= 0;
if (!doScaling)
numberAlreadyScaled = 0;
if (numberAlreadyScaled == numberRows)
return; // no need to do anything
int numberColumns = model_->numberColumns();
double *COIN_RESTRICT rowScale = model_->rowScale2();
double *COIN_RESTRICT inverseRowScale = model_->inverseRowScale2();
double *COIN_RESTRICT columnScale = model_->columnScale2();
double *COIN_RESTRICT inverseColumnScale = model_->inverseColumnScale2();
// we are going to mark bits we are interested in
int whichArray = model_->getAvailableArrayPublic();
char *COIN_RESTRICT usefulColumn = reinterpret_cast< char * >(model_->usefulArray(whichArray)->getIndices());
memset(usefulColumn, 1, numberColumns);
const double *COIN_RESTRICT rowLower = model_->rowLower();
const double *COIN_RESTRICT rowUpper = model_->rowUpper();
const double *COIN_RESTRICT columnLower = model_->columnLower();
const double *COIN_RESTRICT columnUpper = model_->columnUpper();
//#define LEAVE_FIXED
// mark empty and fixed columns
// get matrix data pointers
const int *COIN_RESTRICT row = matrix_->getIndices();
const CoinBigIndex *COIN_RESTRICT columnStart = matrix_->getVectorStarts();
double *COIN_RESTRICT elementByColumn = matrix_->getMutableElements();
CoinPackedMatrix *COIN_RESTRICT rowCopy = reverseOrderedCopy();
const int *column = rowCopy->getIndices();
const CoinBigIndex *COIN_RESTRICT rowStart = rowCopy->getVectorStarts();
const double *COIN_RESTRICT element = rowCopy->getElements();
assert(numberAlreadyScaled >= 0 && numberAlreadyScaled < numberRows);
#ifndef LEAVE_FIXED
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
if (columnUpper[iColumn] == columnLower[iColumn])
usefulColumn[iColumn] = 0;
}
#endif
double overallLargest = -1.0e-20;
double overallSmallest = 1.0e20;
if (!numberAlreadyScaled) {
// may be redundant
CoinFillN(rowScale, numberRows, 1.0);
CoinFillN(columnScale, numberColumns, 1.0);
CoinBigIndex start = 0;
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
CoinBigIndex end = columnStart[iColumn + 1];
if (usefulColumn[iColumn]) {
if (end > start) {
for (CoinBigIndex j = start; j < end; j++) {
double value = fabs(elementByColumn[j]);
overallLargest = CoinMax(overallLargest, value);
overallSmallest = CoinMin(overallSmallest, value);
}
} else {
usefulColumn[iColumn] = 0;
}
}
start = end;
}
} else {
CoinBigIndex start = rowStart[numberAlreadyScaled];
for (int iRow = numberAlreadyScaled; iRow < numberRows; iRow++) {
rowScale[iRow] = 1.0;
CoinBigIndex end = rowStart[iRow + 1];
for (CoinBigIndex j = start; j < end; j++) {
int iColumn = column[j];
if (usefulColumn[iColumn]) {
double value = fabs(elementByColumn[j]) * columnScale[iColumn];
overallLargest = CoinMax(overallLargest, value);
overallSmallest = CoinMin(overallSmallest, value);
}
}
}
}
if ((overallSmallest >= 0.5 && overallLargest <= 2.0) || !doScaling) {
//printf("no scaling\n");
delete rowCopy;
model_->clearArraysPublic(whichArray);
CoinFillN(inverseRowScale + numberAlreadyScaled, numberRows - numberAlreadyScaled, 1.0);
if (!numberAlreadyScaled)
CoinFillN(inverseColumnScale, numberColumns, 1.0);
//moveLargestToStart();
return;
}
// need to scale
double largest;
double smallest;
int scalingMethod = model_->scalingFlag();
if (scalingMethod == 4) {
// As auto
scalingMethod = 3;
} else if (scalingMethod == 5) {
// As geometric
scalingMethod = 2;
}
double savedOverallRatio = 0.0;
double tolerance = 5.0 * model_->primalTolerance();
bool finished = false;
// if scalingMethod 3 then may change
bool extraDetails = (model_->logLevel() > 2);
bool secondTime = false;
while (!finished) {
int numberPass = !numberAlreadyScaled ? 3 : 1;
overallLargest = -1.0e-20;
overallSmallest = 1.0e20;
if (!secondTime) {
secondTime = true;
} else {
CoinFillN(rowScale, numberRows, 1.0);
CoinFillN(columnScale, numberColumns, 1.0);
}
if (scalingMethod == 1 || scalingMethod == 3) {
// Maximum in each row
for (int iRow = numberAlreadyScaled; iRow < numberRows; iRow++) {
largest = 1.0e-10;
for (CoinBigIndex j = rowStart[iRow]; j < rowStart[iRow + 1]; j++) {
int iColumn = column[j];
if (usefulColumn[iColumn]) {
double value = fabs(element[j]);
largest = CoinMax(largest, value);
assert(largest < 1.0e40);
}
}
rowScale[iRow] = 1.0 / largest;
#ifdef COIN_DEVELOP
if (extraDetails) {
overallLargest = CoinMax(overallLargest, largest);
overallSmallest = CoinMin(overallSmallest, largest);
}
#endif
}
} else {
while (numberPass) {
overallLargest = 0.0;
overallSmallest = 1.0e50;
numberPass--;
// Geometric mean on row scales
for (int iRow = numberAlreadyScaled; iRow < numberRows; iRow++) {
largest = 1.0e-50;
smallest = 1.0e50;
for (CoinBigIndex j = rowStart[iRow]; j < rowStart[iRow + 1]; j++) {
int iColumn = column[j];
if (usefulColumn[iColumn]) {
double value = fabs(element[j]);
value *= columnScale[iColumn];
largest = CoinMax(largest, value);
smallest = CoinMin(smallest, value);
}
}
#ifdef SQRT_ARRAY
rowScale[iRow] = smallest * largest;
#else
rowScale[iRow] = 1.0 / sqrt(smallest * largest);
#endif
if (extraDetails) {
overallLargest = CoinMax(largest * rowScale[iRow], overallLargest);
overallSmallest = CoinMin(smallest * rowScale[iRow], overallSmallest);
}
}
if (model_->scalingFlag() == 5)
break; // just scale rows
#ifdef SQRT_ARRAY
CoinAbcInverseSqrts(rowScale, numberRows);
#endif
if (!inBranchAndBound)
model_->messageHandler()->message(CLP_PACKEDSCALE_WHILE, *model_->messagesPointer())
<< overallSmallest
<< overallLargest
<< CoinMessageEol;
// skip last column round
if (numberPass == 1)
break;
// Geometric mean on column scales
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
if (usefulColumn[iColumn]) {
largest = 1.0e-50;
smallest = 1.0e50;
for (CoinBigIndex j = columnStart[iColumn];
j < columnStart[iColumn + 1]; j++) {
int iRow = row[j];
double value = fabs(elementByColumn[j]);
value *= rowScale[iRow];
largest = CoinMax(largest, value);
smallest = CoinMin(smallest, value);
}
#ifdef SQRT_ARRAY
columnScale[iColumn] = smallest * largest;
#else
columnScale[iColumn] = 1.0 / sqrt(smallest * largest);
#endif
}
}
#ifdef SQRT_ARRAY
CoinAbcInverseSqrts(columnScale, numberColumns);
#endif
}
}
// If ranges will make horrid then scale
for (int iRow = numberAlreadyScaled; iRow < numberRows; iRow++) {
double difference = rowUpper[iRow] - rowLower[iRow];
double scaledDifference = difference * rowScale[iRow];
if (scaledDifference > tolerance && scaledDifference < 1.0e-4) {
// make gap larger
rowScale[iRow] *= 1.0e-4 / scaledDifference;
rowScale[iRow] = CoinMax(1.0e-10, CoinMin(1.0e10, rowScale[iRow]));
//printf("Row %d difference %g scaled diff %g => %g\n",iRow,difference,
// scaledDifference,difference*rowScale[iRow]);
}
}
// final pass to scale columns so largest is reasonable
// See what smallest will be if largest is 1.0
if (model_->scalingFlag() != 5) {
overallSmallest = 1.0e50;
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
if (usefulColumn[iColumn]) {
largest = 1.0e-20;
smallest = 1.0e50;
for (CoinBigIndex j = columnStart[iColumn]; j < columnStart[iColumn + 1]; j++) {
int iRow = row[j];
double value = fabs(elementByColumn[j] * rowScale[iRow]);
largest = CoinMax(largest, value);
smallest = CoinMin(smallest, value);
}
if (overallSmallest * largest > smallest)
overallSmallest = smallest / largest;
}
}
}
if (scalingMethod == 1 || scalingMethod == 2) {
finished = true;
} else if (savedOverallRatio == 0.0 && scalingMethod != 4) {
savedOverallRatio = overallSmallest;
scalingMethod = 4;
} else {
assert(scalingMethod == 4);
if (overallSmallest > 2.0 * savedOverallRatio) {
finished = true; // geometric was better
if (model_->scalingFlag() == 4) {
// If in Branch and bound change
if ((model_->specialOptions() & 1024) != 0) {
model_->scaling(2);
}
}
} else {
scalingMethod = 1; // redo equilibrium
if (model_->scalingFlag() == 4) {
// If in Branch and bound change
if ((model_->specialOptions() & 1024) != 0) {
model_->scaling(1);
}
}
}
#if 0
if (extraDetails) {
if (finished)
printf("equilibrium ratio %g, geometric ratio %g , geo chosen\n",
savedOverallRatio, overallSmallest);
else
printf("equilibrium ratio %g, geometric ratio %g , equi chosen\n",
savedOverallRatio, overallSmallest);
}
#endif
}
}
//#define RANDOMIZE
#ifdef RANDOMIZE
// randomize by up to 10%
for (int iRow = numberAlreadyScaled; iRow < numberRows; iRow++) {
double value = 0.5 - randomNumberGenerator_.randomDouble(); //between -0.5 to + 0.5
rowScale[iRow] *= (1.0 + 0.1 * value);
}
#endif
overallLargest = 1.0;
if (overallSmallest < 1.0e-1)
overallLargest = 1.0 / sqrt(overallSmallest);
overallLargest = CoinMin(100.0, overallLargest);
overallSmallest = 1.0e50;
char *COIN_RESTRICT usedRow = reinterpret_cast< char * >(inverseRowScale);
memset(usedRow, 0, numberRows);
//printf("scaling %d\n",model_->scalingFlag());
if (model_->scalingFlag() != 5) {
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
if (usefulColumn[iColumn]) {
largest = 1.0e-20;
smallest = 1.0e50;
for (CoinBigIndex j = columnStart[iColumn]; j < columnStart[iColumn + 1]; j++) {
int iRow = row[j];
usedRow[iRow] = 1;
double value = fabs(elementByColumn[j] * rowScale[iRow]);
largest = CoinMax(largest, value);
smallest = CoinMin(smallest, value);
}
columnScale[iColumn] = overallLargest / largest;
//columnScale[iColumn]=CoinMax(1.0e-10,CoinMin(1.0e10,columnScale[iColumn]));
#ifdef RANDOMIZE
if (!numberAlreadyScaled) {
double value = 0.5 - randomNumberGenerator_.randomDouble(); //between -0.5 to + 0.5
columnScale[iColumn] *= (1.0 + 0.1 * value);
}
#endif
double difference = columnUpper[iColumn] - columnLower[iColumn];
if (difference < 1.0e-5 * columnScale[iColumn]) {
// make gap larger
columnScale[iColumn] = difference / 1.0e-5;
//printf("Column %d difference %g scaled diff %g => %g\n",iColumn,difference,
// scaledDifference,difference*columnScale[iColumn]);
}
double value = smallest * columnScale[iColumn];
if (overallSmallest > value)
overallSmallest = value;
//overallSmallest = CoinMin(overallSmallest,smallest*columnScale[iColumn]);
} else {
assert(columnScale[iColumn] == 1.0);
//columnScale[iColumn]=1.0;
}
}
for (int iRow = numberAlreadyScaled; iRow < numberRows; iRow++) {
if (!usedRow[iRow]) {
rowScale[iRow] = 1.0;
}
}
}
if (!inBranchAndBound)
model_->messageHandler()->message(CLP_PACKEDSCALE_FINAL, *model_->messagesPointer())
<< overallSmallest
<< overallLargest
<< CoinMessageEol;
if (overallSmallest < 1.0e-13) {
// Change factorization zero tolerance
double newTolerance = CoinMax(1.0e-15 * (overallSmallest / 1.0e-13),
1.0e-18);
if (model_->factorization()->zeroTolerance() > newTolerance)
model_->factorization()->zeroTolerance(newTolerance);
newTolerance = CoinMax(overallSmallest * 0.5, 1.0e-18);
model_->setZeroTolerance(newTolerance);
#ifndef NDEBUG
assert(newTolerance < 0.0); // just so we can fix
#endif
}
// make copy (could do faster by using previous values)
// could just do partial
CoinAbcReciprocal(inverseRowScale + numberAlreadyScaled, numberRows - numberAlreadyScaled,
rowScale + numberAlreadyScaled);
if (!numberAlreadyScaled)
CoinAbcReciprocal(inverseColumnScale, numberColumns, columnScale);
// Do scaled copy //NO and move largest to start
CoinBigIndex start = 0;
for (int iColumn = 0; iColumn < numberColumns; iColumn++) {
double scale = columnScale[iColumn];
CoinBigIndex end = columnStart[iColumn + 1];
for (CoinBigIndex j = start; j < end; j++) {
double value = elementByColumn[j];
int iRow = row[j];
if (iRow >= numberAlreadyScaled) {
value *= scale * rowScale[iRow];
elementByColumn[j] = value;
}