-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlp_simplex.c
2206 lines (1955 loc) · 74.6 KB
/
lp_simplex.c
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
/*
Core optimization drivers for lp_solve v5.0+
----------------------------------------------------------------------------------
Author: Michel Berkelaar (to lp_solve v3.2),
Kjell Eikland (v4.0 and forward)
Contact:
License terms: LGPL.
Requires: lp_lib.h, lp_simplex.h, lp_presolve.h, lp_pricerPSE.h
Release notes:
v5.0.0 1 January 2004 New unit applying stacked basis and bounds storage.
v5.0.1 31 January 2004 Moved B&B routines to separate file and implemented
a new runsolver() general purpose call method.
v5.0.2 1 May 2004 Changed routine names to be more intuitive.
v5.1.0 10 January 2005 Created modular stalling/cycling functions.
Rewrote dualloop() to optimize long dual and
also streamlined primloop() correspondingly.
v5.2.0 20 March 2005 Reimplemented primal phase 1 logic.
Made multiple pricing finally work (primal simplex).
----------------------------------------------------------------------------------
*/
#include <string.h>
#include "commonlib.h"
#include "lp_lib.h"
#include "lp_BFP.h"
#include "lp_simplex.h"
#include "lp_crash.h"
#include "lp_presolve.h"
#include "lp_price.h"
#include "lp_pricePSE.h"
#include "lp_report.h"
#ifdef FORTIFY
# include "lp_fortify.h"
#endif
STATIC void stallMonitor_update(lprec *lp, REAL newOF)
{
int newpos;
OBJmonrec *monitor = lp->monitor;
if(monitor->countstep < OBJ_STEPS)
monitor->countstep++;
else
monitor->startstep = mod(monitor->startstep + 1, OBJ_STEPS);
newpos = mod(monitor->startstep + monitor->countstep - 1, OBJ_STEPS);
monitor->objstep[newpos] = newOF;
monitor->idxstep[newpos] = monitor->Icount;
monitor->currentstep = newpos;
}
STATIC MYBOOL stallMonitor_creepingObj(lprec *lp)
{
OBJmonrec *monitor = lp->monitor;
if(monitor->countstep > 1) {
REAL deltaOF = (monitor->objstep[monitor->currentstep] -
monitor->objstep[monitor->startstep]) / monitor->countstep;
deltaOF /= MAX(1, (monitor->idxstep[monitor->currentstep] -
monitor->idxstep[monitor->startstep]));
deltaOF = my_chsign(monitor->isdual, deltaOF);
return( (MYBOOL) (deltaOF < monitor->epsvalue) );
}
else
return( FALSE );
}
STATIC MYBOOL stallMonitor_shortSteps(lprec *lp)
{
OBJmonrec *monitor = lp->monitor;
if(monitor->countstep == OBJ_STEPS) {
REAL deltaOF = MAX(1, (monitor->idxstep[monitor->currentstep] -
monitor->idxstep[monitor->startstep])) / monitor->countstep;
deltaOF = pow(deltaOF*OBJ_STEPS, 0.66);
return( (MYBOOL) (deltaOF > monitor->limitstall[TRUE]) );
}
else
return( FALSE );
}
STATIC void stallMonitor_reset(lprec *lp)
{
OBJmonrec *monitor = lp->monitor;
monitor->ruleswitches = 0;
monitor->Ncycle = 0;
monitor->Mcycle = 0;
monitor->Icount = 0;
monitor->startstep = 0;
monitor->objstep[monitor->startstep] = lp->infinite;
monitor->idxstep[monitor->startstep] = monitor->Icount;
monitor->prevobj = 0;
monitor->countstep = 1;
}
STATIC MYBOOL stallMonitor_create(lprec *lp, MYBOOL isdual, char *funcname)
{
OBJmonrec *monitor = NULL;
if(lp->monitor != NULL)
return( FALSE );
monitor = (OBJmonrec *) calloc(sizeof(*monitor), 1);
if(monitor == NULL)
return( FALSE );
monitor->lp = lp;
strcpy(monitor->spxfunc, funcname);
monitor->isdual = isdual;
monitor->pivdynamic = is_piv_mode(lp, PRICE_ADAPTIVE);
monitor->oldpivstrategy = lp->piv_strategy;
monitor->oldpivrule = get_piv_rule(lp);
if(MAX_STALLCOUNT <= 1)
monitor->limitstall[FALSE] = 0;
else
monitor->limitstall[FALSE] = MAX(MAX_STALLCOUNT,
(int) pow((REAL) (lp->rows+lp->columns)/2, 0.667));
#if 1
monitor->limitstall[FALSE] *= 2+2; /* Expand degeneracy/stalling tolerance range */
#endif
monitor->limitstall[TRUE] = monitor->limitstall[FALSE];
if(monitor->oldpivrule == PRICER_DEVEX) /* Increase tolerance since primal Steepest Edge is expensive */
monitor->limitstall[TRUE] *= 2;
if(MAX_RULESWITCH <= 0)
monitor->limitruleswitches = MAXINT32;
else
monitor->limitruleswitches = MAX(MAX_RULESWITCH,
lp->rows/MAX_RULESWITCH);
monitor->epsvalue = lp->epsprimal; /* lp->epsvalue; */
lp->monitor = monitor;
stallMonitor_reset(lp);
lp->suminfeas = lp->infinite;
return( TRUE );
}
STATIC MYBOOL stallMonitor_check(lprec *lp, int rownr, int colnr, int lastnr,
MYBOOL minit, MYBOOL approved, MYBOOL *forceoutEQ)
{
OBJmonrec *monitor = lp->monitor;
MYBOOL isStalled, isCreeping, acceptance = TRUE;
int altrule,
#ifdef Paranoia
msglevel = NORMAL;
#else
msglevel = DETAILED;
#endif
REAL deltaobj = lp->suminfeas;
/* Accept unconditionally if this is the first or second call */
monitor->active = FALSE;
if(monitor->Icount <= 1) {
if(monitor->Icount == 1) {
monitor->prevobj = lp->rhs[0];
monitor->previnfeas = deltaobj;
}
monitor->Icount++;
return( acceptance );
}
/* Define progress as primal objective less sum of (primal/dual) infeasibilities */
monitor->thisobj = lp->rhs[0];
monitor->thisinfeas = deltaobj;
if(lp->spx_trace &&
(lastnr > 0))
report(lp, NORMAL, "%s: Objective at iter %10.0f is " RESULTVALUEMASK " (%4d: %4d %s- %4d)\n",
monitor->spxfunc,
(double) get_total_iter(lp), monitor->thisobj, rownr, lastnr,
my_if(minit == ITERATE_MAJORMAJOR, "<","|"), colnr);
monitor->pivrule = get_piv_rule(lp);
/* Check if we have a stationary solution at selected tolerance level;
allow some difference in case we just refactorized the basis. */
deltaobj = my_reldiff(monitor->thisobj, monitor->prevobj);
deltaobj = fabs(deltaobj); /* Pre v5.2 version */
isStalled = (MYBOOL) (deltaobj < monitor->epsvalue);
/* Also require that we have a measure of infeasibility-stalling */
if(isStalled) {
REAL testvalue, refvalue = monitor->epsvalue;
#if 1
if(monitor->isdual)
refvalue *= 1000*log10(9.0+lp->rows);
else
refvalue *= 1000*log10(9.0+lp->columns);
#else
refvalue *= 1000*log10(9.0+lp->sum);
#endif
testvalue = my_reldiff(monitor->thisinfeas, monitor->previnfeas);
isStalled &= (fabs(testvalue) < refvalue);
/* Check if we should force "major" pivoting, i.e. no bound flips;
this is activated when we see the feasibility deteriorate */
/* if(!isStalled && (testvalue > 0) && (TRUE || is_action(lp->anti_degen, ANTIDEGEN_BOUNDFLIP))) */
#if !defined _PRICE_NOBOUNDFLIP
if(!isStalled && (testvalue > 0) && is_action(lp->anti_degen, ANTIDEGEN_BOUNDFLIP))
acceptance = AUTOMATIC;
}
#else
if(!isStalled && (testvalue > 0) && !ISMASKSET(lp->piv_strategy, PRICE_NOBOUNDFLIP)) {
SETMASK(lp->piv_strategy, PRICE_NOBOUNDFLIP);
acceptance = AUTOMATIC;
}
}
else
CLEARMASK(lp->piv_strategy, PRICE_NOBOUNDFLIP);
#endif
#if 1
isCreeping = FALSE;
#else
isCreeping |= stallMonitor_creepingObj(lp);
/* isCreeping |= stallMonitor_shortSteps(lp); */
#endif
if(isStalled || isCreeping) {
/* Update counters along with specific tolerance for bound flips */
#if 1
if(minit != ITERATE_MAJORMAJOR) {
if(++monitor->Mcycle > 2) {
monitor->Mcycle = 0;
monitor->Ncycle++;
}
}
else
#endif
monitor->Ncycle++;
/* Start to monitor for variable cycling if this is the initial stationarity */
if(monitor->Ncycle <= 1) {
monitor->Ccycle = colnr;
monitor->Rcycle = rownr;
}
/* Check if we should change pivoting strategy */
else if(isCreeping || /* We have OF creep */
(monitor->Ncycle > monitor->limitstall[monitor->isdual]) || /* KE empirical value */
((monitor->Ccycle == rownr) && (monitor->Rcycle == colnr))) { /* Obvious cycling */
monitor->active = TRUE;
/* Try to force out equality slacks to combat degeneracy */
if((lp->fixedvars > 0) && (*forceoutEQ != TRUE)) {
*forceoutEQ = TRUE;
goto Proceed;
}
/* Our options are now to select an alternative rule or to do bound perturbation;
check if these options are available to us or if we must signal failure and break out. */
approved &= monitor->pivdynamic && (monitor->ruleswitches < monitor->limitruleswitches);
if(!approved && !is_anti_degen(lp, ANTIDEGEN_STALLING)) {
lp->spx_status = DEGENERATE;
report(lp, msglevel, "%s: Stalling at iter %10.0f; no alternative strategy left.\n",
monitor->spxfunc, (double) get_total_iter(lp));
acceptance = FALSE;
return( acceptance );
}
/* See if we can do the appropriate alternative rule. */
switch (monitor->oldpivrule) {
case PRICER_FIRSTINDEX: altrule = PRICER_DEVEX;
break;
case PRICER_DANTZIG: altrule = PRICER_DEVEX;
break;
case PRICER_DEVEX: altrule = PRICER_STEEPESTEDGE;
break;
case PRICER_STEEPESTEDGE: altrule = PRICER_DEVEX;
break;
default: altrule = PRICER_FIRSTINDEX;
}
if(approved &&
(monitor->pivrule != altrule) && (monitor->pivrule == monitor->oldpivrule)) {
/* Switch rule to combat degeneracy. */
monitor->ruleswitches++;
lp->piv_strategy = altrule;
monitor->Ccycle = 0;
monitor->Rcycle = 0;
monitor->Ncycle = 0;
monitor->Mcycle = 0;
report(lp, msglevel, "%s: Stalling at iter %10.0f; changed to '%s' rule.\n",
monitor->spxfunc, (double) get_total_iter(lp),
get_str_piv_rule(get_piv_rule(lp)));
if((altrule == PRICER_DEVEX) || (altrule == PRICER_STEEPESTEDGE))
restartPricer(lp, AUTOMATIC);
}
/* If not, code for bound relaxation/perturbation */
else {
report(lp, msglevel, "%s: Stalling at iter %10.0f; proceed to bound relaxation.\n",
monitor->spxfunc, (double) get_total_iter(lp));
acceptance = FALSE;
lp->spx_status = DEGENERATE;
return( acceptance );
}
}
}
/* Otherwise change back to original selection strategy as soon as possible */
else {
if(monitor->pivrule != monitor->oldpivrule) {
lp->piv_strategy = monitor->oldpivstrategy;
altrule = monitor->oldpivrule;
if((altrule == PRICER_DEVEX) || (altrule == PRICER_STEEPESTEDGE))
restartPricer(lp, AUTOMATIC);
report(lp, msglevel, "...returned to original pivot selection rule at iter %.0f.\n",
(double) get_total_iter(lp));
}
stallMonitor_update(lp, monitor->thisobj);
monitor->Ccycle = 0;
monitor->Rcycle = 0;
monitor->Ncycle = 0;
monitor->Mcycle = 0;
}
/* Update objective progress tracker */
Proceed:
monitor->Icount++;
if(deltaobj >= monitor->epsvalue)
monitor->prevobj = monitor->thisobj;
monitor->previnfeas = monitor->thisinfeas;
return( acceptance );
}
STATIC void stallMonitor_finish(lprec *lp)
{
OBJmonrec *monitor = lp->monitor;
if(monitor == NULL)
return;
if(lp->piv_strategy != monitor->oldpivstrategy)
lp->piv_strategy = monitor->oldpivstrategy;
FREE(monitor);
lp->monitor = NULL;
}
STATIC MYBOOL add_artificial(lprec *lp, int forrownr, REAL *nzarray, int *idxarray)
/* This routine is called for each constraint at the start of
primloop and the primal problem is infeasible. Its
purpose is to add artificial variables and associated
objective function values to populate primal phase 1. */
{
MYBOOL add;
/* Make sure we don't add unnecessary artificials, i.e. avoid
cases where the slack variable is enough */
add = !isBasisVarFeasible(lp, lp->epspivot, forrownr);
if(add) {
int *rownr = NULL, i, bvar, ii;
REAL *avalue = NULL, rhscoef, acoef;
MATrec *mat = lp->matA;
/* Check the simple case where a slack is basic */
for(i = 1; i <= lp->rows; i++) {
if(lp->var_basic[i] == forrownr)
break;
}
acoef = 1;
/* If not, look for any basic user variable that has a
non-zero coefficient in the current constraint row */
if(i > lp->rows) {
for(i = 1; i <= lp->rows; i++) {
ii = lp->var_basic[i] - lp->rows;
if((ii <= 0) || (ii > (lp->columns-lp->P1extraDim)))
continue;
ii = mat_findelm(mat, forrownr, ii);
if(ii >= 0) {
acoef = COL_MAT_VALUE(ii);
break;
}
}
}
/* If no candidate was found above, gamble on using the densest column available */
#if 0
if(i > lp->rows) {
int len = 0;
bvar = 0;
for(i = 1; i <= lp->rows; i++) {
ii = lp->var_basic[i] - lp->rows;
if((ii <= 0) || (ii > (lp->columns-lp->P1extraDim)))
continue;
if(mat_collength(mat, ii) > len) {
len = mat_collength(mat, ii);
bvar = i;
}
}
i = bvar;
acoef = 1;
}
#endif
bvar = i;
add = (MYBOOL) (bvar <= lp->rows);
if(add) {
rhscoef = lp->rhs[forrownr];
/* Create temporary sparse array storage */
if(nzarray == NULL)
allocREAL(lp, &avalue, 2, FALSE);
else
avalue = nzarray;
if(idxarray == NULL)
allocINT(lp, &rownr, 2, FALSE);
else
rownr = idxarray;
/* Set the objective coefficient */
rownr[0] = 0;
avalue[0] = my_chsign(is_chsign(lp, 0), 1);
/* Set the constraint row coefficient */
rownr[1] = forrownr;
avalue[1] = my_chsign(is_chsign(lp, forrownr), my_sign(rhscoef/acoef));
/* Add the column of artificial variable data to the user data matrix */
add_columnex(lp, 2, avalue, rownr);
/* Free the temporary sparse array storage */
if(idxarray == NULL)
FREE(rownr);
if(nzarray == NULL)
FREE(avalue);
/* Now set the artificial variable to be basic */
set_basisvar(lp, bvar, lp->sum);
lp->P1extraDim++;
}
else {
report(lp, CRITICAL, "add_artificial: Could not find replacement basis variable for row %d\n",
forrownr);
lp->basis_valid = FALSE;
}
}
return(add);
}
STATIC int get_artificialRow(lprec *lp, int colnr)
{
MATrec *mat = lp->matA;
#ifdef Paranoia
if((colnr <= lp->columns-abs(lp->P1extraDim)) || (colnr > lp->columns))
report(lp, SEVERE, "get_artificialRow: Invalid column index %d\n", colnr);
if(mat->col_end[colnr] - mat->col_end[colnr-1] != 1)
report(lp, SEVERE, "get_artificialRow: Invalid column non-zero count\n");
#endif
/* Return the row index of the singleton */
colnr = mat->col_end[colnr-1];
colnr = COL_MAT_ROWNR(colnr);
return( colnr );
}
STATIC int findAnti_artificial(lprec *lp, int colnr)
/* Primal simplex: Find a basic artificial variable to swap
against the non-basic slack variable, if possible */
{
int i, k, rownr = 0, P1extraDim = abs(lp->P1extraDim);
if((P1extraDim == 0) || (colnr > lp->rows) || !lp->is_basic[colnr])
return( rownr );
for(i = 1; i <= lp->rows; i++) {
k = lp->var_basic[i];
if((k > lp->sum-P1extraDim) && (lp->rhs[i] == 0)) {
rownr = get_artificialRow(lp, k-lp->rows);
/* Should we find the artificial's slack direct "antibody"? */
if(rownr == colnr)
break;
rownr = 0;
}
}
return( rownr );
}
STATIC int findBasicArtificial(lprec *lp, int before)
{
int i = 0, P1extraDim = abs(lp->P1extraDim);
if(P1extraDim > 0) {
if(before > lp->rows || before <= 1)
i = lp->rows;
else
i = before;
while((i > 0) && (lp->var_basic[i] <= lp->sum-P1extraDim))
i--;
}
return(i);
}
STATIC void eliminate_artificials(lprec *lp, REAL *prow)
{
int i, j, colnr, rownr, P1extraDim = abs(lp->P1extraDim);
for(i = 1; (i <= lp->rows) && (P1extraDim > 0); i++) {
j = lp->var_basic[i];
if(j <= lp->sum-P1extraDim)
continue;
j -= lp->rows;
rownr = get_artificialRow(lp, j);
colnr = find_rowReplacement(lp, rownr, prow, NULL);
#if 0
performiteration(lp, rownr, colnr, 0.0, TRUE, FALSE, prow, NULL,
NULL, NULL, NULL);
#else
set_basisvar(lp, rownr, colnr);
#endif
del_column(lp, j);
P1extraDim--;
}
lp->P1extraDim = 0;
}
STATIC void clear_artificials(lprec *lp)
{
int i, j, n, P1extraDim;
/* Substitute any basic artificial variable for its slack counterpart */
n = 0;
P1extraDim = abs(lp->P1extraDim);
for(i = 1; (i <= lp->rows) && (n < P1extraDim); i++) {
j = lp->var_basic[i];
if(j <= lp->sum-P1extraDim)
continue;
j = get_artificialRow(lp, j-lp->rows);
set_basisvar(lp, i, j);
n++;
}
#ifdef Paranoia
if(n != lp->P1extraDim)
report(lp, SEVERE, "clear_artificials: Unable to clear all basic artificial variables\n");
#endif
/* Delete any remaining non-basic artificial variables */
while(P1extraDim > 0) {
i = lp->sum-lp->rows;
del_column(lp, i);
P1extraDim--;
}
lp->P1extraDim = 0;
if(n > 0) {
set_action(&lp->spx_action, ACTION_REINVERT);
lp->basis_valid = TRUE;
}
}
STATIC int primloop(lprec *lp, MYBOOL primalfeasible, REAL primaloffset)
{
MYBOOL primal = TRUE, bfpfinal = FALSE, changedphase = FALSE, forceoutEQ = AUTOMATIC,
primalphase1, pricerCanChange, minit, stallaccept, pendingunbounded;
int i, j, k, colnr = 0, rownr = 0, lastnr = 0,
candidatecount = 0, minitcount = 0, ok = TRUE;
LREAL theta = 0.0;
REAL epsvalue, xviolated = 0.0, cviolated = 0.0,
*prow = NULL, *pcol = NULL,
*drow = lp->drow;
int *workINT = NULL,
*nzdrow = lp->nzdrow;
if(lp->spx_trace)
report(lp, DETAILED, "Entered primal simplex algorithm with feasibility %s\n",
my_boolstr(primalfeasible));
/* Add sufficent number of artificial variables to make the problem feasible
through the first phase; delete when primal feasibility has been achieved */
lp->P1extraDim = 0;
if(!primalfeasible) {
lp->simplex_mode = SIMPLEX_Phase1_PRIMAL;
#ifdef Paranoia
if(!verify_basis(lp))
report(lp, SEVERE, "primloop: No valid basis for artificial variables\n");
#endif
#if 0
/* First check if we can get away with a single artificial variable */
if(lp->equalities == 0) {
i = (int) feasibilityOffset(lp, !primal);
add_artificial(lp, i, prow, (int *) pcol);
}
else
#endif
/* Otherwise add as many artificial variables as is necessary
to force primal feasibility. */
for(i = 1; i <= lp->rows; i++) {
add_artificial(lp, i, NULL, NULL);
}
/* Make sure we update the working objective */
if(lp->P1extraDim > 0) {
#if 1 /* v5.1 code: Not really necessary since we do not price the artificial
variables (stored at the end of the column list, they are initially
basic and are never allowed to enter the basis, once they exit) */
ok = allocREAL(lp, &(lp->drow), lp->sum+1, AUTOMATIC) &&
allocINT(lp, &(lp->nzdrow), lp->sum+1, AUTOMATIC);
if(!ok)
goto Finish;
lp->nzdrow[0] = 0;
drow = lp->drow;
nzdrow = lp->nzdrow;
#endif
mat_validate(lp->matA);
set_OF_p1extra(lp, 0.0);
}
if(lp->spx_trace)
report(lp, DETAILED, "P1extraDim count = %d\n", lp->P1extraDim);
simplexPricer(lp, (MYBOOL)!primal);
invert(lp, INITSOL_USEZERO, TRUE);
}
else {
lp->simplex_mode = SIMPLEX_Phase2_PRIMAL;
restartPricer(lp, (MYBOOL)!primal);
}
/* Create work arrays and optionally the multiple pricing structure */
ok = allocREAL(lp, &(lp->bsolveVal), lp->rows + 1, FALSE) &&
allocREAL(lp, &prow, lp->sum + 1, TRUE) &&
allocREAL(lp, &pcol, lp->rows + 1, TRUE);
if(is_piv_mode(lp, PRICE_MULTIPLE) && (lp->multiblockdiv > 1)) {
lp->multivars = multi_create(lp, FALSE);
ok &= (lp->multivars != NULL) &&
multi_resize(lp->multivars, lp->sum / lp->multiblockdiv, 2, FALSE, TRUE);
}
if(!ok)
goto Finish;
/* Initialize regular primal simplex algorithm variables */
lp->spx_status = RUNNING;
minit = ITERATE_MAJORMAJOR;
epsvalue = lp->epspivot;
pendingunbounded = FALSE;
ok = stallMonitor_create(lp, FALSE, "primloop");
if(!ok)
goto Finish;
lp->rejectpivot[0] = 0;
/* Iterate while we are successful; exit when the model is infeasible/unbounded,
or we must terminate due to numeric instability or user-determined reasons */
while((lp->spx_status == RUNNING) && !userabort(lp, -1)) {
primalphase1 = (MYBOOL) (lp->P1extraDim > 0);
clear_action(&lp->spx_action, ACTION_REINVERT | ACTION_ITERATE);
/* Check if we have stalling (from numerics or degenerate cycling) */
pricerCanChange = !primalphase1;
stallaccept = stallMonitor_check(lp, rownr, colnr, lastnr, minit, pricerCanChange, &forceoutEQ);
if(!stallaccept)
break;
/* Find best column to enter the basis */
RetryCol:
#if 0
if(verify_solution(lp, FALSE, "spx_loop") > 0)
i = 1; /* This is just a debug trap */
#endif
if(!changedphase) {
i = 0;
do {
i++;
colnr = colprim(lp, drow, nzdrow, (MYBOOL) (minit == ITERATE_MINORRETRY), i, &candidatecount, TRUE, &xviolated);
} while ((colnr == 0) && (i < partial_countBlocks(lp, (MYBOOL) !primal)) &&
partial_blockStep(lp, (MYBOOL) !primal));
/* Handle direct outcomes */
if(colnr == 0)
lp->spx_status = OPTIMAL;
if(lp->rejectpivot[0] > 0)
minit = ITERATE_MAJORMAJOR;
/* See if accuracy check during compute_reducedcosts flagged refactorization */
if(is_action(lp->spx_action, ACTION_REINVERT))
bfpfinal = TRUE;
}
/* Make sure that we do not erroneously conclude that an unbounded model is optimal */
#ifdef primal_UseRejectionList
if((colnr == 0) && (lp->rejectpivot[0] > 0)) {
lp->spx_status = UNBOUNDED;
if((lp->spx_trace && (lp->bb_totalnodes == 0)) ||
(lp->bb_trace && (lp->bb_totalnodes > 0)))
report(lp, DETAILED, "The model is primal unbounded.\n");
colnr = lp->rejectpivot[1];
rownr = 0;
lp->rejectpivot[0] = 0;
ok = FALSE;
break;
}
#endif
/* Check if we found an entering variable (indicating that we are still dual infeasible) */
if(colnr > 0) {
changedphase = FALSE;
fsolve(lp, colnr, pcol, NULL, lp->epsmachine, 1.0, TRUE); /* Solve entering column for Pi */
/* Do special anti-degeneracy column selection, if specified */
if(is_anti_degen(lp, ANTIDEGEN_COLUMNCHECK) && !check_degeneracy(lp, pcol, NULL)) {
if(lp->rejectpivot[0] < DEF_MAXPIVOTRETRY/3) {
i = ++lp->rejectpivot[0];
lp->rejectpivot[i] = colnr;
report(lp, DETAILED, "Entering column %d found to be non-improving due to degeneracy.\n",
colnr);
minit = ITERATE_MINORRETRY;
goto RetryCol;
}
else {
lp->rejectpivot[0] = 0;
report(lp, DETAILED, "Gave up trying to find a strictly improving entering column.\n");
}
}
/* Find the leaving variable that gives the most stringent bound on the entering variable */
theta = drow[colnr];
rownr = rowprim(lp, colnr, &theta, pcol, workINT, forceoutEQ, &cviolated);
#ifdef AcceptMarginalAccuracy
/* Check for marginal accuracy */
if((rownr > 0) && (xviolated+cviolated < lp->epspivot)) {
if(lp->bb_trace || (lp->bb_totalnodes == 0))
report(lp, DETAILED, "primloop: Assuming convergence with reduced accuracy %g.\n",
MAX(xviolated, cviolated));
rownr = 0;
colnr = 0;
goto Optimality;
}
else
#endif
/* See if we can do a straight artificial<->slack replacement (when "colnr" is a slack) */
if((lp->P1extraDim != 0) && (rownr == 0) && (colnr <= lp->rows))
rownr = findAnti_artificial(lp, colnr);
if(rownr > 0) {
pendingunbounded = FALSE;
lp->rejectpivot[0] = 0;
set_action(&lp->spx_action, ACTION_ITERATE);
if(!lp->obj_in_basis) /* We must manually copy the reduced cost for RHS update */
pcol[0] = my_chsign(!lp->is_lower[colnr], drow[colnr]);
lp->bfp_prepareupdate(lp, rownr, colnr, pcol);
}
/* We may be unbounded... */
else {
/* First make sure that we are not suffering from precision loss */
#ifdef primal_UseRejectionList
if(lp->rejectpivot[0] < DEF_MAXPIVOTRETRY) {
lp->spx_status = RUNNING;
lp->rejectpivot[0]++;
lp->rejectpivot[lp->rejectpivot[0]] = colnr;
report(lp, DETAILED, "...trying to recover via another pivot column.\n");
minit = ITERATE_MINORRETRY;
goto RetryCol;
}
else
#endif
/* Check that we are not having numerical problems */
if(!refactRecent(lp) && !pendingunbounded) {
bfpfinal = TRUE;
pendingunbounded = TRUE;
set_action(&lp->spx_action, ACTION_REINVERT);
}
/* Conclude that the model is unbounded */
else {
lp->spx_status = UNBOUNDED;
report(lp, DETAILED, "The model is primal unbounded.\n");
break;
}
}
}
/* We handle optimality and phase 1 infeasibility ... */
else {
Optimality:
/* Handle possible transition from phase 1 to phase 2 */
if(!primalfeasible || isP1extra(lp)) {
if(feasiblePhase1(lp, epsvalue)) {
lp->spx_status = RUNNING;
if(lp->bb_totalnodes == 0) {
report(lp, NORMAL, "Found feasibility by primal simplex after %10.0f iter.\n",
(double) get_total_iter(lp));
if((lp->usermessage != NULL) && (lp->msgmask & MSG_LPFEASIBLE))
lp->usermessage(lp, lp->msghandle, MSG_LPFEASIBLE);
}
changedphase = FALSE;
primalfeasible = TRUE;
lp->simplex_mode = SIMPLEX_Phase2_PRIMAL;
set_OF_p1extra(lp, 0.0);
/* We can do two things now;
1) delete the rows belonging to those variables, since they are redundant, OR
2) drive out the existing artificial variables via pivoting. */
if(lp->P1extraDim > 0) {
#ifdef Phase1EliminateRedundant
/* If it is not a MIP model we can try to delete redundant rows */
if((lp->bb_totalnodes == 0) && (MIP_count(lp) == 0)) {
while(lp->P1extraDim > 0) {
i = lp->rows;
while((i > 0) && (lp->var_basic[i] <= lp->sum-lp->P1extraDim))
i--;
#ifdef Paranoia
if(i <= 0) {
report(lp, SEVERE, "primloop: Could not find redundant artificial.\n");
break;
}
#endif
/* Obtain column and row indeces */
j = lp->var_basic[i]-lp->rows;
k = get_artificialRow(lp, j);
/* Delete row before column due to basis "compensation logic" */
if(lp->is_basic[k]) {
lp->is_basic[lp->rows+j] = FALSE;
del_constraint(lp, k);
}
else
set_basisvar(lp, i, k);
del_column(lp, j);
lp->P1extraDim--;
}
lp->basis_valid = TRUE;
}
/* Otherwise we drive out the artificials by elimination pivoting */
else
eliminate_artificials(lp, prow);
#else
/* Indicate phase 2 with artificial variables by negating P1extraDim */
lp->P1extraDim = my_flipsign(lp->P1extraDim);
#endif
}
/* We must refactorize since the OF changes from phase 1 to phase 2 */
set_action(&lp->spx_action, ACTION_REINVERT);
bfpfinal = TRUE;
}
/* We are infeasible in phase 1 */
else {
lp->spx_status = INFEASIBLE;
minit = ITERATE_MAJORMAJOR;
if(lp->spx_trace)
report(lp, NORMAL, "Model infeasible by primal simplex at iter %10.0f.\n",
(double) get_total_iter(lp));
}
}
/* Handle phase 1 optimality */
else {
/* (Do nothing special) */
}
/* Check if we are still primal feasible; the default assumes that this check
is not necessary after the relaxed problem has been solved satisfactorily. */
if((lp->bb_level <= 1) || (lp->improve & IMPROVE_BBSIMPLEX) /* || (lp->bb_rule & NODE_RCOSTFIXING) */) { /* NODE_RCOSTFIXING fix */
set_action(&lp->piv_strategy, PRICE_FORCEFULL);
i = rowdual(lp, lp->rhs, FALSE, FALSE, NULL);
clear_action(&lp->piv_strategy, PRICE_FORCEFULL);
if(i > 0) {
lp->spx_status = LOSTFEAS;
if(lp->total_iter == 0)
report(lp, DETAILED, "primloop: Lost primal feasibility at iter %10.0f: will try to recover.\n",
(double) get_total_iter(lp));
}
}
}
/* Pivot row/col and update the inverse */
if(is_action(lp->spx_action, ACTION_ITERATE)) {
lastnr = lp->var_basic[rownr];
if(refactRecent(lp) == AUTOMATIC)
minitcount = 0;
else if(minitcount > MAX_MINITUPDATES) {
recompute_solution(lp, INITSOL_USEZERO);
minitcount = 0;
}
minit = performiteration(lp, rownr, colnr, theta, primal,
(MYBOOL) (/*(candidatecount > 1) && */
(stallaccept != AUTOMATIC)),
NULL, NULL,
pcol, NULL, NULL);
if(minit != ITERATE_MAJORMAJOR)
minitcount++;
if((lp->spx_status == USERABORT) || (lp->spx_status == TIMEOUT))
break;
else if(minit == ITERATE_MINORMAJOR)
continue;
#ifdef UsePrimalReducedCostUpdate
/* Do a fast update of the reduced costs in preparation for the next iteration */
if(minit == ITERATE_MAJORMAJOR)
update_reducedcosts(lp, primal, lastnr, colnr, pcol, drow);
#endif
/* Detect if an auxiliary variable has left the basis and delete it; if
the non-basic variable only changed bound (a "minor iteration"), the
basic artificial variable did not leave and there is nothing to do */
if((minit == ITERATE_MAJORMAJOR) && (lastnr > lp->sum - abs(lp->P1extraDim))) {
#ifdef Paranoia
if(lp->is_basic[lastnr] || !lp->is_basic[colnr])
report(lp, SEVERE, "primloop: Invalid basis indicator for variable %d at iter %10.0f.\n",
lastnr, (double) get_total_iter(lp));
#endif
del_column(lp, lastnr-lp->rows);
if(lp->P1extraDim > 0)
lp->P1extraDim--;
else
lp->P1extraDim++;
if(lp->P1extraDim == 0) {
colnr = 0;
changedphase = TRUE;
stallMonitor_reset(lp);
}
}
}
if(lp->spx_status == SWITCH_TO_DUAL)
;
else if(!changedphase && lp->bfp_mustrefactorize(lp)) {
#ifdef ResetMinitOnReinvert
minit = ITERATE_MAJORMAJOR;
#endif
if(!invert(lp, INITSOL_USEZERO, bfpfinal))
lp->spx_status = SINGULAR_BASIS;
bfpfinal = FALSE;
}
}
/* Remove any remaining artificial variables (feasible or infeasible model) */
lp->P1extraDim = abs(lp->P1extraDim);
/* if((lp->P1extraDim > 0) && (lp->spx_status != DEGENERATE)) { */
if(lp->P1extraDim > 0) {
clear_artificials(lp);
if(lp->spx_status != OPTIMAL)
restore_basis(lp);
i = invert(lp, INITSOL_USEZERO, TRUE);
}
#ifdef Paranoia
if(!verify_basis(lp))
report(lp, SEVERE, "primloop: Invalid basis detected due to internal error\n");
#endif
/* Switch to dual phase 1 simplex for MIP models during
B&B phases, since this is typically far more efficient */
#ifdef ForceDualSimplexInBB
if((lp->bb_totalnodes == 0) && (MIP_count(lp) > 0) &&
((lp->simplex_strategy & SIMPLEX_Phase1_DUAL) == 0)) {
lp->simplex_strategy &= ~SIMPLEX_Phase1_PRIMAL;
lp->simplex_strategy += SIMPLEX_Phase1_DUAL;
}
#endif
Finish:
stallMonitor_finish(lp);
multi_free(&(lp->multivars));
FREE(prow);
FREE(pcol);
FREE(lp->bsolveVal);
return(ok);
} /* primloop */
STATIC int dualloop(lprec *lp, MYBOOL dualfeasible, int dualinfeasibles[], REAL dualoffset)
{
MYBOOL primal = FALSE, inP1extra, dualphase1 = FALSE, changedphase = TRUE,
pricerCanChange, minit, stallaccept, longsteps,
forceoutEQ = FALSE, bfpfinal = FALSE;
int i, colnr = 0, rownr = 0, lastnr = 0,
candidatecount = 0, minitcount = 0,
#ifdef FixInaccurateDualMinit
minitcolnr = 0,
#endif
ok = TRUE;
int *boundswaps = NULL;
LREAL theta = 0.0;
REAL epsvalue, xviolated, cviolated,
*prow = NULL, *pcol = NULL,
*drow = lp->drow;
int *nzprow = NULL, *workINT = NULL,