forked from coin-or/Clp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AbcDualRowSteepest.cpp
1453 lines (1442 loc) · 48.4 KB
/
AbcDualRowSteepest.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 "CoinPragma.hpp"
#include "AbcSimplex.hpp"
#include "ClpMessage.hpp"
#include "AbcDualRowSteepest.hpp"
#include "CoinIndexedVector.hpp"
#include "AbcSimplexFactorization.hpp"
#include "CoinHelperFunctions.hpp"
#include "CoinAbcHelperFunctions.hpp"
#include <cstdio>
//#############################################################################
// Constructors / Destructor / Assignment
//#############################################################################
//#define CLP_DEBUG 2
//-------------------------------------------------------------------
// Default Constructor
//-------------------------------------------------------------------
AbcDualRowSteepest::AbcDualRowSteepest(int mode)
: AbcDualRowPivot()
, norm_(0.0)
, factorizationRatio_(10.0)
, state_(-1)
, mode_(mode)
, persistence_(normal)
, weights_(NULL)
, infeasible_(NULL)
, savedWeights_(NULL)
{
type_ = 2 + 64 * mode;
//printf("XX %x AbcDualRowSteepest constructor\n",this);
}
//-------------------------------------------------------------------
// Copy constructor
//-------------------------------------------------------------------
AbcDualRowSteepest::AbcDualRowSteepest(const AbcDualRowSteepest &rhs)
: AbcDualRowPivot(rhs)
{
//printf("XX %x AbcDualRowSteepest constructor from %x\n",this,&rhs);
norm_ = rhs.norm_;
factorizationRatio_ = rhs.factorizationRatio_;
state_ = rhs.state_;
mode_ = rhs.mode_;
persistence_ = rhs.persistence_;
model_ = rhs.model_;
if ((model_ && model_->whatsChanged() & 1) != 0) {
int number = model_->numberRows();
if (rhs.savedWeights_)
number = CoinMin(number, rhs.savedWeights_->capacity());
if (rhs.infeasible_) {
infeasible_ = new CoinIndexedVector(rhs.infeasible_);
} else {
infeasible_ = NULL;
}
if (rhs.weights_) {
weights_ = new CoinIndexedVector(rhs.weights_);
} else {
weights_ = NULL;
}
if (rhs.savedWeights_) {
savedWeights_ = new CoinIndexedVector(rhs.savedWeights_);
} else {
savedWeights_ = NULL;
}
} else {
infeasible_ = NULL;
weights_ = NULL;
savedWeights_ = NULL;
}
}
//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
AbcDualRowSteepest::~AbcDualRowSteepest()
{
//printf("XX %x AbcDualRowSteepest destructor\n",this);
delete weights_;
delete infeasible_;
delete savedWeights_;
}
//----------------------------------------------------------------
// Assignment operator
//-------------------------------------------------------------------
AbcDualRowSteepest &
AbcDualRowSteepest::operator=(const AbcDualRowSteepest &rhs)
{
if (this != &rhs) {
AbcDualRowPivot::operator=(rhs);
norm_ = rhs.norm_;
factorizationRatio_ = rhs.factorizationRatio_;
state_ = rhs.state_;
mode_ = rhs.mode_;
persistence_ = rhs.persistence_;
model_ = rhs.model_;
delete weights_;
delete infeasible_;
delete savedWeights_;
assert(model_);
int number = model_->numberRows();
if (rhs.savedWeights_)
number = CoinMin(number, rhs.savedWeights_->capacity());
if (rhs.infeasible_ != NULL) {
infeasible_ = new CoinIndexedVector(rhs.infeasible_);
} else {
infeasible_ = NULL;
}
if (rhs.weights_ != NULL) {
weights_ = new CoinIndexedVector(rhs.weights_);
} else {
weights_ = NULL;
}
if (rhs.savedWeights_ != NULL) {
savedWeights_ = new CoinIndexedVector(rhs.savedWeights_);
} else {
savedWeights_ = NULL;
}
}
return *this;
}
// Fill most values
void AbcDualRowSteepest::fill(const AbcDualRowSteepest &rhs)
{
norm_ = rhs.norm_;
factorizationRatio_ = rhs.factorizationRatio_;
state_ = rhs.state_;
mode_ = rhs.mode_;
persistence_ = rhs.persistence_;
assert(model_->numberRows() == rhs.model_->numberRows());
model_ = rhs.model_;
assert(model_);
int number = model_->numberRows();
if (rhs.savedWeights_)
number = CoinMin(number, rhs.savedWeights_->capacity());
if (rhs.infeasible_ != NULL) {
if (!infeasible_)
infeasible_ = new CoinIndexedVector(rhs.infeasible_);
else
*infeasible_ = *rhs.infeasible_;
} else {
delete infeasible_;
infeasible_ = NULL;
}
if (rhs.weights_ != NULL) {
if (!weights_)
weights_ = new CoinIndexedVector(rhs.weights_);
} else {
delete weights_;
weights_ = NULL;
}
if (rhs.savedWeights_ != NULL) {
if (!savedWeights_)
savedWeights_ = new CoinIndexedVector(rhs.savedWeights_);
else
*savedWeights_ = *rhs.savedWeights_;
} else {
delete savedWeights_;
savedWeights_ = NULL;
}
}
#ifdef CHECK_NUMBER_WANTED
static int xTimes = 0;
static int xWanted = 0;
#endif
#if ABC_PARALLEL == 2
#define DO_REDUCE 2
#ifdef DO_REDUCE
#if DO_REDUCE == 1
#include <cilk/reducer_max.h>
static void choose(int &chosenRow, double &largest, int n,
const int *index, const double *infeas,
const double *weights, double tolerance)
{
cilk::reducer_max_index< int, double > maximumIndex(chosenRow, largest);
#pragma cilk grainsize = 128
cilk_for(int i = 0; i < n; i++)
{
int iRow = index[i];
double value = infeas[iRow];
if (value > tolerance) {
double thisWeight = CoinMin(weights[iRow], 1.0e50);
maximumIndex.calc_max(iRow, value / thisWeight);
}
}
chosenRow = maximumIndex.get_index();
largest = maximumIndex.get_value();
}
#else
static void choose(AbcDualRowSteepest *steepest,
int &chosenRowSave, double &largestSave, int first, int last,
double tolerance)
{
if (last - first > 256) {
int mid = (last + first) >> 1;
int chosenRow2 = chosenRowSave;
double largest2 = largestSave;
cilk_spawn choose(steepest, chosenRow2, largest2, first, mid,
tolerance);
choose(steepest, chosenRowSave, largestSave, mid, last,
tolerance);
cilk_sync;
if (largest2 > largestSave) {
largestSave = largest2;
chosenRowSave = chosenRow2;
}
} else {
const int *index = steepest->infeasible()->getIndices();
const double *infeas = steepest->infeasible()->denseVector();
const double *weights = steepest->weights()->denseVector();
double largest = largestSave;
int chosenRow = chosenRowSave;
if ((steepest->model()->stateOfProblem() & VALUES_PASS2) == 0) {
for (int i = first; i < last; i++) {
int iRow = index[i];
double value = infeas[iRow];
if (value > tolerance) {
double thisWeight = CoinMin(weights[iRow], 1.0e50);
if (value > largest * thisWeight) {
largest = value / thisWeight;
chosenRow = iRow;
}
}
}
} else {
const double *fakeDjs = steepest->model()->fakeDjs();
const int *pivotVariable = steepest->model()->pivotVariable();
for (int i = first; i < last; i++) {
int iRow = index[i];
double value = infeas[iRow];
if (value > tolerance) {
int iSequence = pivotVariable[iRow];
value *= (fabs(fakeDjs[iSequence]) + 1.0e-6);
double thisWeight = CoinMin(weights[iRow], 1.0e50);
/*
Ideas
always use fake
use fake until chosen
if successful would move djs to basic region?
how to switch off if cilking - easiest at saveWeights
*/
if (value > largest * thisWeight) {
largest = value / thisWeight;
chosenRow = iRow;
}
}
}
}
chosenRowSave = chosenRow;
largestSave = largest;
}
}
static void choose2(AbcDualRowSteepest *steepest,
int &chosenRowSave, double &largestSave, int first, int last,
double tolerance)
{
if (last - first > 256) {
int mid = (last + first) >> 1;
int chosenRow2 = chosenRowSave;
double largest2 = largestSave;
cilk_spawn choose2(steepest, chosenRow2, largest2, first, mid,
tolerance);
choose2(steepest, chosenRowSave, largestSave, mid, last,
tolerance);
cilk_sync;
if (largest2 > largestSave) {
largestSave = largest2;
chosenRowSave = chosenRow2;
}
} else {
const int *index = steepest->infeasible()->getIndices();
const double *infeas = steepest->infeasible()->denseVector();
double largest = largestSave;
int chosenRow = chosenRowSave;
for (int i = first; i < last; i++) {
int iRow = index[i];
double value = infeas[iRow];
if (value > largest) {
largest = value;
chosenRow = iRow;
}
}
chosenRowSave = chosenRow;
largestSave = largest;
}
}
#endif
#endif
#endif
// Returns pivot row, -1 if none
int AbcDualRowSteepest::pivotRow()
{
assert(model_);
double *COIN_RESTRICT infeas = infeasible_->denseVector();
int *COIN_RESTRICT index = infeasible_->getIndices();
int number = infeasible_->getNumElements();
int lastPivotRow = model_->lastPivotRow();
assert(lastPivotRow < model_->numberRows());
double tolerance = model_->currentPrimalTolerance();
// we can't really trust infeasibilities if there is primal error
// this coding has to mimic coding in checkPrimalSolution
double error = CoinMin(1.0e-2, model_->largestPrimalError());
// allow tolerance at least slightly bigger than standard
tolerance = tolerance + error;
// But cap
tolerance = CoinMin(1000.0, tolerance);
tolerance *= tolerance; // as we are using squares
bool toleranceChanged = false;
const double *COIN_RESTRICT solutionBasic = model_->solutionBasic();
const double *COIN_RESTRICT lowerBasic = model_->lowerBasic();
const double *COIN_RESTRICT upperBasic = model_->upperBasic();
// do last pivot row one here
if (lastPivotRow >= 0 && lastPivotRow < model_->numberRows()) {
double value = solutionBasic[lastPivotRow];
double lower = lowerBasic[lastPivotRow];
double upper = upperBasic[lastPivotRow];
if (value > upper + tolerance) {
value -= upper;
value *= value;
// store square in list
if (infeas[lastPivotRow])
infeas[lastPivotRow] = value; // already there
else
infeasible_->quickAdd(lastPivotRow, value);
} else if (value < lower - tolerance) {
value -= lower;
value *= value;
// store square in list
if (infeas[lastPivotRow])
infeas[lastPivotRow] = value; // already there
else
infeasible_->add(lastPivotRow, value);
} else {
// feasible - was it infeasible - if so set tiny
if (infeas[lastPivotRow])
infeas[lastPivotRow] = COIN_INDEXED_REALLY_TINY_ELEMENT;
}
number = infeasible_->getNumElements();
}
if (model_->numberIterations() < model_->lastBadIteration() + 200) {
// we can't really trust infeasibilities if there is dual error
if (model_->largestDualError() > model_->largestPrimalError()) {
tolerance *= CoinMin(model_->largestDualError() / model_->largestPrimalError(), 1000.0);
toleranceChanged = true;
}
}
int numberWanted;
if (mode_ < 2) {
numberWanted = number + 1;
} else {
if (model_->factorization()->pivots() == 0) {
int numberElements = model_->factorization()->numberElements();
int numberRows = model_->numberRows();
#if 0
int numberSlacks = model_->factorization()->numberSlacks();
factorizationRatio_ = static_cast<double> (numberElements+1) /
static_cast<double> (numberRows+1-numberSlacks);
#else
factorizationRatio_ = static_cast< double >(numberElements) / static_cast< double >(numberRows);
#endif
//printf("%d iterations, numberElements %d ratio %g\n",
// model_->numberIterations(),numberElements,factorizationRatio_);
// Bias so less likely to go to steepest if large
double weights[] = { 1.0, 2.0, 5.0 };
double modifier;
if (numberRows < 100000)
modifier = weights[0];
else if (numberRows < 200000)
modifier = weights[1];
else
modifier = weights[2];
if (mode_ == 2 && factorizationRatio_ > modifier) {
// switch from dantzig to steepest
mode_ = 3;
saveWeights(model_, 5);
}
}
#if 0
// look at this and modify
numberWanted = number+1;
if (factorizationRatio_ < 1.0) {
numberWanted = CoinMax(2000, numberRows / 20);
#if 0
} else if (factorizationRatio_ > 10.0) {
double ratio = number * (factorizationRatio_ / 80.0);
if (ratio > number)
numberWanted = number + 1;
else
numberWanted = CoinMax(2000, static_cast<int> (ratio));
#else
} else {
//double multiplier=CoinMin(factorizationRatio_*0.1,1.0)
#endif
}
#else
numberWanted = CoinMax(2000, number / 8);
if (factorizationRatio_ < 1.5) {
numberWanted = CoinMax(2000, number / 20);
} else if (factorizationRatio_ > 5.0) {
numberWanted = number + 1;
}
#endif
numberWanted = CoinMin(numberWanted, number + 1);
}
if (model_->largestPrimalError() > 1.0e-3)
numberWanted = number + 1; // be safe
#ifdef CHECK_NUMBER_WANTED
xWanted += numberWanted;
xTimes++;
if ((xTimes % 1000) == 0)
printf("time %d average wanted %g\n", xTimes, static_cast< double >(xWanted) / xTimes);
#endif
int iPass;
// Setup two passes
int start[4];
start[1] = number;
start[2] = 0;
double dstart = static_cast< double >(number) * model_->randomNumberGenerator()->randomDouble();
start[0] = static_cast< int >(dstart);
start[3] = start[0];
//double largestWeight=0.0;
//double smallestWeight=1.0e100;
const double *weights = weights_->denseVector();
const int *pivotVariable = model_->pivotVariable();
double savePivotWeight = weights_->denseVector()[lastPivotRow];
weights_->denseVector()[lastPivotRow] *= 1.0e10;
double largest = 0.0;
int chosenRow = -1;
int saveNumberWanted = numberWanted;
#ifdef DO_REDUCE
bool doReduce = true;
int lastChosen = -1;
double lastLargest = 0.0;
#endif
for (iPass = 0; iPass < 2; iPass++) {
int endThis = start[2 * iPass + 1];
int startThis = start[2 * iPass];
while (startThis < endThis) {
int end = CoinMin(endThis, startThis + numberWanted);
#ifdef DO_REDUCE
if (doReduce) {
#if DO_REDUCE == 1
choose(chosenRow, largest, end - startThis,
index + startThis, infeas, weights, tolerance);
#else
if (mode_ != 2)
choose(this, chosenRow, largest, startThis, end, tolerance);
else
choose2(this, chosenRow, largest, startThis, end, tolerance);
#endif
if (chosenRow != lastChosen) {
assert(chosenRow >= 0);
if (model_->flagged(pivotVariable[chosenRow]) || (solutionBasic[chosenRow] <= upperBasic[chosenRow] + tolerance && solutionBasic[chosenRow] >= lowerBasic[chosenRow] - tolerance)) {
doReduce = false;
chosenRow = lastChosen;
largest = lastLargest;
} else {
lastChosen = chosenRow;
lastLargest = largest;
}
}
}
if (!doReduce) {
#endif
for (int i = startThis; i < end; i++) {
int iRow = index[i];
double value = infeas[iRow];
if (value > tolerance) {
//#define OUT_EQ
#ifdef OUT_EQ
if (upper[iRow] == lower[iRow])
value *= 2.0;
#endif
double thisWeight = CoinMin(weights[iRow], 1.0e50);
//largestWeight = CoinMax(largestWeight,weight);
//smallestWeight = CoinMin(smallestWeight,weight);
//double dubious = dubiousWeights_[iRow];
//weight *= dubious;
//if (value>2.0*largest*weight||(value>0.5*largest*weight&&value*largestWeight>dubious*largest*weight)) {
if (value > largest * thisWeight) {
// make last pivot row last resort choice
//if (iRow == lastPivotRow) {
//if (value * 1.0e-10 < largest * thisWeight)
// continue;
//else
// value *= 1.0e-10;
//}
if (!model_->flagged(pivotVariable[iRow])) {
//#define CLP_DEBUG 3
#ifdef CLP_DEBUG
double value2 = 0.0;
if (solutionBasic[iRow] > upperBasic[iRow] + tolerance)
value2 = solutionBasic[iRow] - upperBasic[iRow];
else if (solutionBasic[iRow] < lowerBasic[iRow] - tolerance)
value2 = solutionBasic[iRow] - lowerBasic[iRow];
assert(fabs(value2 * value2 - infeas[iRow]) < 1.0e-8 * CoinMin(value2 * value2, infeas[iRow]));
#endif
if (solutionBasic[iRow] > upperBasic[iRow] + tolerance || solutionBasic[iRow] < lowerBasic[iRow] - tolerance) {
chosenRow = iRow;
largest = value / thisWeight;
//largestWeight = dubious;
}
}
}
}
}
#ifdef DO_REDUCE
}
#endif
numberWanted -= (end - startThis);
if (!numberWanted) {
if (chosenRow >= 0)
break;
else
numberWanted = (saveNumberWanted + 1) >> 1;
}
startThis = end;
}
if (!numberWanted) {
if (chosenRow >= 0)
break;
else
numberWanted = (saveNumberWanted + 1) >> 1;
}
}
weights_->denseVector()[lastPivotRow] = savePivotWeight;
//printf("smallest %g largest %g\n",smallestWeight,largestWeight);
if (chosenRow < 0 && toleranceChanged) {
// won't line up with checkPrimalSolution - do again
double saveError = model_->largestDualError();
model_->setLargestDualError(0.0);
// can't loop
chosenRow = pivotRow();
model_->setLargestDualError(saveError);
}
if (chosenRow < 0 && lastPivotRow < 0) {
int nLeft = 0;
for (int i = 0; i < number; i++) {
int iRow = index[i];
if (fabs(infeas[iRow]) > 1.0e-50) {
index[nLeft++] = iRow;
} else {
infeas[iRow] = 0.0;
}
}
infeasible_->setNumElements(nLeft);
model_->setNumberPrimalInfeasibilities(nLeft);
}
if (true && (model_->stateOfProblem() & VALUES_PASS2) != 0) {
if (chosenRow >= 0) {
double *fakeDjs = model_->fakeDjs();
//const int * pivotVariable = model_->pivotVariable();
fakeDjs[pivotVariable[chosenRow]] = 0.0;
}
}
return chosenRow;
}
//#define PRINT_WEIGHTS_FREQUENCY
#ifdef PRINT_WEIGHTS_FREQUENCY
int xweights1 = 0;
int xweights2 = 0;
int xweights3 = 0;
#endif
/* Updates weights and returns pivot alpha.
Also does FT update */
double
AbcDualRowSteepest::updateWeights(CoinIndexedVector &input,
CoinIndexedVector &updateColumn)
{
double *COIN_RESTRICT weights = weights_->denseVector();
#if CLP_DEBUG > 2
// Very expensive debug
{
int numberRows = model_->numberRows();
CoinIndexedVector temp;
temp.reserve(numberRows);
double *array = temp.denseVector();
int *which = temp.getIndices();
for (int i = 0; i < numberRows; i++) {
double value = 0.0;
array[i] = 1.0;
which[0] = i;
temp.setNumElements(1);
model_->factorization()->updateColumnTranspose(temp);
int number = temp.getNumElements();
for (int j = 0; j < number; j++) {
int iRow = which[j];
value += array[iRow] * array[iRow];
array[iRow] = 0.0;
}
temp.setNumElements(0);
double w = CoinMax(weights[i], value) * .1;
if (fabs(weights[i] - value) > w) {
printf("%d old %g, true %g\n", i, weights[i], value);
weights[i] = value; // to reduce printout
}
}
}
#endif
double alpha = 0.0;
double norm = 0.0;
int i;
double *COIN_RESTRICT work2 = input.denseVector();
int numberNonZero = input.getNumElements();
int *COIN_RESTRICT which = input.getIndices();
//compute norm
for (int i = 0; i < numberNonZero; i++) {
int iRow = which[i];
double value = work2[iRow];
norm += value * value;
}
// Do FT update
if (true) {
model_->factorization()->updateTwoColumnsFT(updateColumn, input);
#if CLP_DEBUG > 3
printf("REGION after %d els\n", input.getNumElements());
input.print();
#endif
} else {
// Leave as old way
model_->factorization()->updateColumnFT(updateColumn);
model_->factorization()->updateColumn(input);
}
//#undef CLP_DEBUG
numberNonZero = input.getNumElements();
int pivotRow = model_->lastPivotRow();
assert(model_->pivotRow() == model_->lastPivotRow());
#ifdef CLP_DEBUG
if (model_->logLevel() > 4 && fabs(norm - weights[pivotRow]) > 1.0e-2 * (1.0 + norm))
printf("on row %d, true weight %g, old %g\n",
pivotRow, sqrt(norm), sqrt(weights[pivotRow]));
#endif
// could re-initialize here (could be expensive)
norm /= model_->alpha() * model_->alpha();
assert(model_->alpha());
assert(norm);
double multiplier = 2.0 / model_->alpha();
#ifdef PRINT_WEIGHTS_FREQUENCY
xweights1++;
if ((xweights1 + xweights2 + xweights3) % 100 == 0) {
printf("ZZ iteration %d sum weights %d - %d %d %d\n",
model_->numberIterations(), xweights1 + xweights2 + xweights3, xweights1, xweights2, xweights3);
assert(abs(model_->numberIterations() - (xweights1 + xweights2 + xweights3)) <= 1);
}
#endif
// look at updated column
double *COIN_RESTRICT work = updateColumn.denseVector();
numberNonZero = updateColumn.getNumElements();
which = updateColumn.getIndices();
// pivot element
alpha = work[pivotRow];
for (i = 0; i < numberNonZero; i++) {
int iRow = which[i];
double theta = work[iRow];
double devex = weights[iRow];
//work3[iRow] = devex; // save old
//which3[nSave++] = iRow;
double value = work2[iRow];
devex += theta * (theta * norm + value * multiplier);
if (devex < DEVEX_TRY_NORM)
devex = DEVEX_TRY_NORM;
weights[iRow] = devex;
}
if (norm < DEVEX_TRY_NORM)
norm = DEVEX_TRY_NORM;
// Try this to make less likely will happen again and stop cycling
//norm *= 1.02;
weights[pivotRow] = norm;
input.clear();
#ifdef CLP_DEBUG
input.checkClear();
#endif
#ifdef CLP_DEBUG
input.checkClear();
#endif
return alpha;
}
double
AbcDualRowSteepest::updateWeights1(CoinIndexedVector &input, CoinIndexedVector &updateColumn)
{
if (mode_ == 2) {
abort();
// no update
// Do FT update
model_->factorization()->updateColumnFT(updateColumn);
// pivot element
double alpha = 0.0;
// look at updated column
double *work = updateColumn.denseVector();
int pivotRow = model_->lastPivotRow();
assert(pivotRow == model_->pivotRow());
assert(!updateColumn.packedMode());
alpha = work[pivotRow];
return alpha;
}
#if CLP_DEBUG > 2
// Very expensive debug
{
double *weights = weights_->denseVector();
int numberRows = model_->numberRows();
const int *pivotVariable = model_->pivotVariable();
CoinIndexedVector temp;
temp.reserve(numberRows);
double *array = temp.denseVector();
int *which = temp.getIndices();
for (int i = 0; i < numberRows; i++) {
double value = 0.0;
array[i] = 1.0;
which[0] = i;
temp.setNumElements(1);
model_->factorization()->updateColumnTranspose(temp);
int number = temp.getNumElements();
for (int j = 0; j < number; j++) {
int iRow = which[j];
value += array[iRow] * array[iRow];
array[iRow] = 0.0;
}
temp.setNumElements(0);
double w = CoinMax(weights[i], value) * .1;
if (fabs(weights[i] - value) > w) {
printf("XX row %d (variable %d) old %g, true %g\n", i, pivotVariable[i],
weights[i], value);
weights[i] = value; // to reduce printout
}
}
}
#endif
norm_ = 0.0;
double *COIN_RESTRICT work2 = input.denseVector();
int numberNonZero = input.getNumElements();
int *COIN_RESTRICT which = input.getIndices();
//compute norm
for (int i = 0; i < numberNonZero; i++) {
int iRow = which[i];
double value = work2[iRow];
norm_ += value * value;
}
// Do FT update
if (true) {
model_->factorization()->updateTwoColumnsFT(updateColumn, input);
#if CLP_DEBUG > 3
printf("REGION after %d els\n", input.getNumElements());
input.print();
#endif
} else {
// Leave as old way
model_->factorization()->updateColumnFT(updateColumn);
model_->factorization()->updateWeights(input);
}
// pivot element
assert(model_->pivotRow() == model_->lastPivotRow());
return updateColumn.denseVector()[model_->lastPivotRow()];
}
void AbcDualRowSteepest::updateWeightsOnly(CoinIndexedVector &input)
{
if (mode_ == 2)
return;
norm_ = 0.0;
double *COIN_RESTRICT work2 = input.denseVector();
int numberNonZero = input.getNumElements();
int *COIN_RESTRICT which = input.getIndices();
//compute norm
for (int i = 0; i < numberNonZero; i++) {
int iRow = which[i];
double value = work2[iRow];
norm_ += value * value;
}
model_->factorization()->updateWeights(input);
}
// Actually updates weights
void AbcDualRowSteepest::updateWeights2(CoinIndexedVector &input, CoinIndexedVector &updateColumn)
{
if (mode_ == 2)
return;
double *COIN_RESTRICT weights = weights_->denseVector();
const double *COIN_RESTRICT work2 = input.denseVector();
int pivotRow = model_->lastPivotRow();
assert(model_->pivotRow() == model_->lastPivotRow());
#ifdef CLP_DEBUG
if (model_->logLevel() > 4 && fabs(norm_ - weights[pivotRow]) > 1.0e-2 * (1.0 + norm_))
printf("on row %d, true weight %g, old %g\n",
pivotRow, sqrt(norm_), sqrt(weights[pivotRow]));
#endif
// pivot element
double alpha = model_->alpha();
// could re-initialize here (could be expensive)
norm_ /= alpha * alpha;
assert(alpha);
assert(norm_);
double multiplier = 2.0 / alpha;
// look at updated column
const double *COIN_RESTRICT work = updateColumn.denseVector();
int numberNonZero = updateColumn.getNumElements();
const int *which = updateColumn.getIndices();
double multiplier2 = 1.0;
#ifdef PRINT_WEIGHTS_FREQUENCY
xweights2++;
if ((xweights1 + xweights2 + xweights3) % 100 == 0) {
printf("ZZ iteration %d sum weights %d - %d %d %d\n",
model_->numberIterations(), xweights1 + xweights2 + xweights3, xweights1, xweights2, xweights3);
assert(abs(model_->numberIterations() - (xweights1 + xweights2 + xweights3)) <= 1);
}
#endif
if (model_->directionOut() < 0) {
#ifdef ABC_DEBUG
printf("Minus out in %d %d, out %d %d\n", model_->sequenceIn(), model_->directionIn(),
model_->sequenceOut(), model_->directionOut());
#endif
multiplier2 = -1.0;
}
for (int i = 0; i < numberNonZero; i++) {
int iRow = which[i];
double theta = multiplier2 * work[iRow];
double devex = weights[iRow];
double value = work2[iRow];
devex += theta * (theta * norm_ + value * multiplier);
if (devex < DEVEX_TRY_NORM)
devex = DEVEX_TRY_NORM;
weights[iRow] = devex;
}
if (norm_ < DEVEX_TRY_NORM)
norm_ = DEVEX_TRY_NORM;
// Try this to make less likely will happen again and stop cycling
//norm_ *= 1.02;
weights[pivotRow] = norm_;
input.clear();
#ifdef CLP_DEBUG
input.checkClear();
#endif
}
/* Updates primal solution (and maybe list of candidates)
Uses input vector which it deletes
Computes change in objective function
*/
void AbcDualRowSteepest::updatePrimalSolution(
CoinIndexedVector &primalUpdate,
double primalRatio)
{
double *COIN_RESTRICT work = primalUpdate.denseVector();
int number = primalUpdate.getNumElements();
int *COIN_RESTRICT which = primalUpdate.getIndices();
double tolerance = model_->currentPrimalTolerance();
double *COIN_RESTRICT infeas = infeasible_->denseVector();
double *COIN_RESTRICT solutionBasic = model_->solutionBasic();
const double *COIN_RESTRICT lowerBasic = model_->lowerBasic();
const double *COIN_RESTRICT upperBasic = model_->upperBasic();
assert(!primalUpdate.packedMode());
for (int i = 0; i < number; i++) {
int iRow = which[i];
double value = solutionBasic[iRow];
double change = primalRatio * work[iRow];
// no need to keep for steepest edge
work[iRow] = 0.0;
value -= change;
double lower = lowerBasic[iRow];
double upper = upperBasic[iRow];
solutionBasic[iRow] = value;
if (value < lower - tolerance) {
value -= lower;
value *= value;
#ifdef CLP_DUAL_FIXED_COLUMN_MULTIPLIER
if (lower == upper)
value *= CLP_DUAL_FIXED_COLUMN_MULTIPLIER; // bias towards taking out fixed variables
#endif
// store square in list
if (infeas[iRow])
infeas[iRow] = value; // already there
else
infeasible_->quickAdd(iRow, value);
} else if (value > upper + tolerance) {
value -= upper;
value *= value;
#ifdef CLP_DUAL_FIXED_COLUMN_MULTIPLIER
if (lower == upper)
value *= CLP_DUAL_FIXED_COLUMN_MULTIPLIER; // bias towards taking out fixed variables
#endif
// store square in list
if (infeas[iRow])
infeas[iRow] = value; // already there
else
infeasible_->quickAdd(iRow, value);
} else {
// feasible - was it infeasible - if so set tiny
if (infeas[iRow])
infeas[iRow] = COIN_INDEXED_REALLY_TINY_ELEMENT;
}
}
// Do pivot row
int iRow = model_->lastPivotRow();
assert(model_->pivotRow() == model_->lastPivotRow());
// feasible - was it infeasible - if so set tiny
//assert (infeas[iRow]);
if (infeas[iRow])
infeas[iRow] = COIN_INDEXED_REALLY_TINY_ELEMENT;
primalUpdate.setNumElements(0);
}
#if ABC_PARALLEL == 2
static void update(int first, int last,
const int *COIN_RESTRICT which, double *COIN_RESTRICT work,
const double *COIN_RESTRICT work2, double *COIN_RESTRICT weights,
const double *COIN_RESTRICT lowerBasic, double *COIN_RESTRICT solutionBasic,
const double *COIN_RESTRICT upperBasic,
double multiplier, double multiplier2,
double norm, double theta, double tolerance)
{
if (last - first > 256) {
int mid = (last + first) >> 1;
cilk_spawn update(first, mid, which, work, work2, weights, lowerBasic, solutionBasic,
upperBasic, multiplier, multiplier2, norm, theta, tolerance);
update(mid, last, which, work, work2, weights, lowerBasic, solutionBasic,
upperBasic, multiplier, multiplier2, norm, theta, tolerance);
cilk_sync;
} else {
for (int i = first; i < last; i++) {
int iRow = which[i];
double updateValue = work[iRow];
double thetaDevex = multiplier2 * updateValue;
double devex = weights[iRow];
double valueDevex = work2[iRow];
devex += thetaDevex * (thetaDevex * norm + valueDevex * multiplier);
if (devex < DEVEX_TRY_NORM)
devex = DEVEX_TRY_NORM;
weights[iRow] = devex;
double value = solutionBasic[iRow];
double change = theta * updateValue;
value -= change;
double lower = lowerBasic[iRow];
double upper = upperBasic[iRow];
solutionBasic[iRow] = value;
if (value < lower - tolerance) {
value -= lower;
value *= value;
#ifdef CLP_DUAL_FIXED_COLUMN_MULTIPLIER
if (lower == upper)
value *= CLP_DUAL_FIXED_COLUMN_MULTIPLIER; // bias towards taking out fixed variables
#endif
} else if (value > upper + tolerance) {
value -= upper;
value *= value;
#ifdef CLP_DUAL_FIXED_COLUMN_MULTIPLIER
if (lower == upper)
value *= CLP_DUAL_FIXED_COLUMN_MULTIPLIER; // bias towards taking out fixed variables
#endif
} else {
// feasible
value = 0.0;
}
// store square
work[iRow] = value;
}
}
}
#endif
void AbcDualRowSteepest::updatePrimalSolutionAndWeights(CoinIndexedVector &weightsVector,
CoinIndexedVector &primalUpdate,
double theta)
{
//printf("updatePrimal iteration %d - weightsVector has %d, primalUpdate has %d\n",
// model_->numberIterations(),weightsVector.getNumElements(),primalUpdate.getNumElements());
double *COIN_RESTRICT weights = weights_->denseVector();
double *COIN_RESTRICT work2 = weightsVector.denseVector();
int pivotRow = model_->lastPivotRow();
assert(model_->pivotRow() == model_->lastPivotRow());
double *COIN_RESTRICT work = primalUpdate.denseVector();
int numberNonZero = primalUpdate.getNumElements();
int *COIN_RESTRICT which = primalUpdate.getIndices();
double tolerance = model_->currentPrimalTolerance();
double *COIN_RESTRICT infeas = infeasible_->denseVector();
double *COIN_RESTRICT solutionBasic = model_->solutionBasic();
const double *COIN_RESTRICT lowerBasic = model_->lowerBasic();
const double *COIN_RESTRICT upperBasic = model_->upperBasic();
assert(!primalUpdate.packedMode());
#ifdef CLP_DEBUG
if (model_->logLevel() > 4 && fabs(norm_ - weights[pivotRow]) > 1.0e-2 * (1.0 + norm_))
printf("on row %d, true weight %g, old %g\n",
pivotRow, sqrt(norm_), sqrt(weights[pivotRow]));
#endif
// pivot element
double alpha = model_->alpha();
// could re-initialize here (could be expensive)
norm_ /= alpha * alpha;
assert(alpha);
assert(norm_);
double multiplier = 2.0 / alpha;
#ifdef PRINT_WEIGHTS_FREQUENCY
// only weights3 is used? - slightly faster to just update weights if going to invert
xweights3++;
if ((xweights1 + xweights2 + xweights3) % 1000 == 0) {
printf("ZZ iteration %d sum weights %d - %d %d %d\n",
model_->numberIterations(), xweights1 + xweights2 + xweights3, xweights1, xweights2, xweights3);
assert(abs(model_->numberIterations() - (xweights1 + xweights2 + xweights3)) <= 1);