-
Notifications
You must be signed in to change notification settings - Fork 2
/
dplex.c
1593 lines (1429 loc) · 46.7 KB
/
dplex.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
/* dplex.c: dual simplex method c-code
written by Komei Fukuda, fukuda@ifor.math.ethz.ch
Version 0.61, December 1, 1997
*/
/* dplex.c : C-Implementation of the dual simplex method for
solving an LP: max/min c^T x subject to x in P, where
P= {x : b - A x >= 0}.
Please read COPYING (GNU General Public Licence) and
the manual cddman.tex for detail.
*/
#include "setoper.h" /* set operation library header (Ver. March 16,1995 or later) */
#include "dplex.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
#define COPYRIGHT "Copyright (C) 1997, Komei Fukuda, fukuda@ifor.math.ethz.ch"
#define DPLEXVERSION "Version 0.61 (December 1, 1997)"
#define dp_FALSE 0
#define dp_TRUE 1
typedef enum {
dp_MaxIndex, dp_MinIndex, dp_LexMin, dp_LexMax, dp_RandomRow, dp_LineShelling
} dp_HyperplaneOrderType;
void dp_LPInput(FILE **f, dp_FilenameType, rowrange *m, colrange *n, Amatrix A,
dp_LPConversionType *Conversion, rowrange *objrow, colrange *rhscol,
dp_ErrorType *err);
void dp_InitializeBmatrix(colrange n_size, Bmatrix T);
void CrissCrossMinimize(rowrange, colrange, Amatrix, Bmatrix T,
rowrange, colrange, int,
dp_LPStatusType *, double *optvalue, Arow, Arow, colindex,
rowrange *, colrange *, long *, dp_ErrorType *);
void CrissCrossMaximize(rowrange, colrange, Amatrix,Bmatrix T,
rowrange, colrange, int,
dp_LPStatusType *, double *optvalue, Arow, Arow, colindex,
rowrange *, colrange *, long *, dp_ErrorType *);
void DualSimplexMinimize(rowrange, colrange, Amatrix,Bmatrix,
rowrange, colrange, int,
dp_LPStatusType *, double *, Arow, Arow, colindex,
rowrange *, colrange *, long *, dp_ErrorType *);
void DualSimplexMaximize(rowrange, colrange, Amatrix,Bmatrix,
rowrange, colrange, int,
dp_LPStatusType *, double *, Arow, Arow, colindex,
rowrange *, colrange *, long *, dp_ErrorType *);
void FindLPBasis(rowrange, colrange, Amatrix, Bmatrix, rowindex,
colindex, rowindex, rowrange, colrange, int,
colrange *, int *, dp_LPStatusType *, long *);
void FindDualFeasibleBasis(rowrange, colrange, Amatrix, Bmatrix, rowindex,
colindex, long *, rowrange, colrange,
colrange *, int *, dp_LPStatusType *, long *);
void dp_WriteBmatrix(FILE *f, colrange n_size, Bmatrix T);
void dp_SetNumberType(char *line, dp_NumberType *number, dp_ErrorType *Error);
void dp_ComputeRowOrderVector(rowrange m_size, colrange n_size, Amatrix A,
rowindex OV, dp_HyperplaneOrderType ho, unsigned int rseed);
void dp_SelectPreorderedNext(rowrange m_size, colrange n_size,
rowset excluded, rowindex OV, rowrange *hnext);
void dp_WriteReal(FILE *f, double x);
void dp_SetInputFile(FILE **f, dp_FilenameType inputfile, dp_ErrorType *);
void SetSolutions(rowrange, colrange,
Amatrix, Bmatrix, rowrange, colrange, dp_LPStatusType,
double *, Arow, Arow, colindex, rowrange, colrange);
void dp_SetInputFile(FILE **f, dp_FilenameType inputfile, dp_ErrorType *Error)
{
int opened=0,stop, quit=0;
int i,dotpos=0,trial=0;
char ch;
char *tempname;
*Error=dp_None;
while (!opened && !quit) {
printf("\n>> Input file (*.ine) : ");
scanf("%s",inputfile);
ch=getchar();
stop=dp_FALSE;
for (i=0; i<dp_filenamelen && !stop; i++){
ch=inputfile[i];
switch (ch) {
case '.':
dotpos=i+1;
break;
case ';': case ' ': case '\0': case '\n': case '\t':
stop=dp_TRUE;
tempname=(char*)calloc(dp_filenamelen,sizeof ch);
strncpy(tempname, inputfile, i);
strcpy(inputfile,tempname);
break;
}
}
if ( ( *f = fopen(inputfile, "r") )!= NULL) {
printf("input file %s is open\n", inputfile);
opened=1;
*Error=dp_None;
}
else{
printf("The file %s not found\n",inputfile);
trial++;
if (trial>5) {
*Error=dp_FileNotFound;
quit=1;
}
}
}
}
void dp_LPInput(FILE **f, dp_FilenameType inputfile,
rowrange *m_size, colrange *n_size, Amatrix A,
dp_LPConversionType *Conversion, rowrange *objrow, colrange *rhscol,
dp_ErrorType *err)
{
long i,j;
rowrange m_input;
colrange n_input;
double value, cost;
dp_NumberType Number;
int found=0, localdebug=0;
char command[dp_wordlenmax], numbtype[dp_wordlenmax], line[dp_linelenmax];
*err=dp_None;
dp_SetInputFile(f, inputfile, err);
if (*err!=dp_None){
goto _L99;
}
while (!found)
{
if (fscanf(*f,"%s",command)==EOF) {
*err=dp_ImproperInputFormat;
goto _L99;
}
else if (strncmp(command, "begin", 5)==0) {
found=dp_TRUE;
}
}
fscanf(*f, "%ld %ld %s", &m_input, &n_input, numbtype);
printf("size = %ld x %ld\nNumber Type = %s\n", m_input, n_input, numbtype);
dp_SetNumberType(numbtype, &Number, err);
if (Number==dp_Unknown || Number == dp_Rational) {
goto _L99;
}
*n_size=n_input; *m_size=m_input+1;
if (*n_size > dp_NMAX || *m_size+1 > dp_MMAX) {
*err = dp_DimensionTooLarge;
goto _L99;
}
for (i = 1; i <= m_input; i++) {
A[i-1]=(double *) calloc(n_input, sizeof value);
for (j = 1; j <= n_input; j++) {
fscanf(*f, "%lf", &value);
A[i-1][j-1] = value;
if (localdebug) printf("a(%3ld,%5ld) = %10.4f\n",i,j,value);
} /*of j*/
fgets(line,dp_linelenmax,*f);
if (localdebug) printf("comments to be skipped: %s\n",line);
if (localdebug) putchar('\n');
} /*of i*/
if (fscanf(*f,"%s",command)==EOF) {
*err=dp_ImproperInputFormat;
goto _L99;
}
else if (strncmp(command, "end", 3)!=0) {
if (localdebug) printf("'end' missing or illegal extra data: %s\n",command);
*err=dp_ImproperInputFormat;
goto _L99;
}
*objrow=*m_size;
*rhscol=1L;
A[*m_size-1]=(double *) calloc(n_input, sizeof value);
found=0;
while (!found)
{
if (fscanf(*f,"%s",command)==EOF) {
*err=dp_ImproperInputFormat;
goto _L99;
}
if (strncmp(command, "maximize", 8)==0) {
*Conversion=dp_LPmax;
found=dp_TRUE;
}
if (strncmp(command, "minimize", 8)==0) {
*Conversion=dp_LPmin;
found=dp_TRUE;
}
}
for (j=0;j<n_input;j++) {
fscanf(*f,"%lf",&cost);
A[*m_size-1][j]=cost;
if (localdebug) printf(" cost[%ld] = %.9E\n",j,A[*m_size-1][j]);
}
_L99: ;
if (*f!=NULL) fclose(*f);
}
int dp_Nonnegative(double val)
{
if (val>=-dp_zero) return dp_TRUE;
else return dp_FALSE;
}
int dp_Nonpositive(double val)
{
if (val<=dp_zero) return dp_TRUE;
else return dp_FALSE;
}
int dp_Positive(double val)
{
return !dp_Nonpositive(val);
}
int dp_Negative(double val)
{
return !dp_Nonnegative(val);
}
int dp_Zero(double val)
{
return (dp_Nonnegative(val) && dp_Nonpositive(val));
}
int dp_Nonzero(double val)
{
return (dp_Positive(val) || dp_Negative(val));
}
void dp_SetNumberType(char *line, dp_NumberType *number, dp_ErrorType *Error)
{
if (strncmp(line, "integer", 7)==0) {
*number = dp_Integer;
return;
}
else if (strncmp(line, "rational", 8)==0) {
*number = dp_Rational;
*Error=dp_ImproperInputFormat; /* Rational Input not supported */
return;
}
else if (strncmp(line, "real", 4)==0) {
*number = dp_Real;
return;
}
else {
*number=dp_Unknown;
*Error=dp_ImproperInputFormat;
}
}
void dp_WriteErrorMessages(FILE *f, dp_ErrorType Error)
{
switch (Error) {
case dp_DimensionTooLarge:
fprintf(f, "dp_Error: LP size is too large. Modify dp_NMAX and/or dp_MMAX.\n");
break;
case dp_LowColumnRank:
fprintf(f, "dp_Error: The matrix A is not column full rank.\n");
break;
case dp_ImproperInputFormat:
fprintf(f, "dp_Error: Input file format is not correct.\n");
break;
case dp_FileNotFound:
fprintf(f, "dp_Error: The input file does not exist.\n");
break;
case dp_None:
fprintf(f, "dp_Error: No error found.\n");
break;
default:
fprintf(f, "dp_Error: Unknown error found.\n");
break;
}
}
double dp_TableauEntry(rowrange m_size, colrange n_size, Amatrix A, Bmatrix T,
rowrange r, colrange s)
/* Compute the (r,s) entry of A.T */
{
colrange j;
double temp;
temp=0;
for (j=0; j< n_size; j++) {
temp = temp + A[r-1][j] * T[j][s-1];
}
return temp;
}
void dp_WriteTableau(FILE *f, rowrange m_size, colrange n_size, Amatrix A, Bmatrix T,
colindex nbindex, rowindex bflag)
/* Write the tableau A.T */
{
colrange j;
rowrange i;
fprintf(f, " %ld %ld real\n",m_size, n_size);
fprintf(f," |");
for (j=1; j<= n_size; j++) {
fprintf(f," %12ld", nbindex[j]);
} fprintf(f,"\n");
for (j=1; j<= n_size+1; j++) {
fprintf(f," ------------");
} fprintf(f,"\n");
for (i=1; i<= m_size; i++) {
fprintf(f," %3ld(%3ld) |", i, bflag[i]);
for (j=1; j<= n_size; j++) {
fprintf(f," %12.3f",dp_TableauEntry(m_size, n_size, A,T,i,j));
}
fprintf(f,"\n");
}
fprintf(f,"end\n");
}
void SelectDualSimplexPivot(rowrange m_size, colrange n_size,
int Phase1, Amatrix A, Bmatrix T, rowindex OV,
colindex nbindex, rowindex bflag,
rowrange objrow, colrange rhscol,
rowrange *r, colrange *s, int *selected, dp_LPStatusType *lps)
{ /* selects a dual simplex pivot (*r, *s) if the current
basis is dual feasible and not optimal. If not dual feasible,
the procedure returns *selected=dp_FALSE and *lps=LPSundecided.
If Phase1=dp_TRUE, the RHS column will be considered as the negative
of the column of the largest variable (==m_size). For this case, it is assumed
that the caller used the auxiliary row (with variable m_size) to make the current
dictionary dual feasible before calling this routine so that the nonbasic
column for m_size corresponds to the auxiliary variable.
*/
int colselected=dp_FALSE, rowselected=dp_FALSE,
dualfeasible=dp_TRUE,localdebug=dp_FALSE;
rowrange i;
colrange j;
double val=0, minval=0,rat=0, minrat=0;
static long lastnn=0;
static Arow rcost;
lastnn=n_size;
*r=0; *s=0;
*selected=dp_FALSE;
*lps=dp_LPSundecided;
for (j=1; j<=n_size; j++){
if (j!=rhscol){
if (localdebug) printf("checking the column %ld var %ld\n", j, nbindex[j]);
rcost[j-1]=dp_TableauEntry(m_size, n_size, A,T,objrow,j);
if (localdebug) printf("reduced cost = %f\n", rcost[j-1]);
if (dp_Positive(rcost[j-1])) {
dualfeasible=dp_FALSE;
}
}
}
if (dualfeasible){
while ((*lps==dp_LPSundecided) && (!rowselected) && (!colselected)) {
for (i=1; i<=m_size; i++) {
if (localdebug) printf("checking the row var %ld\n",i);
if (i!=objrow && bflag[i]==-1) { /* i is a basic variable */
if (Phase1){
val=-dp_TableauEntry(m_size, n_size, A,T,i,bflag[m_size]); /* for dual Phase I */
}
else {val=dp_TableauEntry(m_size, n_size, A,T,i,rhscol);}
if (localdebug) printf("RHS val = %f\n", val);
if (val < minval) {
*r=i;
minval=val;
if (localdebug) printf("update minval with = %f *r = %ld\n",minval, *r);
}
}
}
if (dp_Nonnegative(minval)) {
*lps=dp_Optimal;
if (localdebug) printf("Select DualSimplexPivot: Optimal solution found.\n");
}
else {
rowselected=dp_TRUE;
for (j=1; j<=n_size; j++){
val=dp_TableauEntry(m_size, n_size, A,T,*r,j);
if (j!=rhscol && dp_Positive(val)) {
rat=-rcost[j-1]/val;
if (localdebug) printf("new ratio = %f at col %ld\n",rat, j);
if (*s==0 || rat < minrat){
minrat=rat;
*s=j;
if (localdebug) printf("update minrat = %f *s = %ld\n",minrat, *s);
}
}
}
if (localdebug) printf("*s is %ld\n",*s);
if (*s>0) {colselected=dp_TRUE; *selected=dp_TRUE;}
else *lps=dp_Inconsistent;
}
} /* end of while */
}
if (localdebug) {
if (Phase1) printf("Phase 1 : select %ld, %ld\n", *r, *s);
else printf("Phase 2 : select %ld, %ld\n", *r, *s);
}
}
void dp_SelectPivot2(rowrange m_size, colrange n_size, Amatrix A, Bmatrix T,
dp_HyperplaneOrderType roworder, rowindex ordervec,
rowrange rowmax, rowset NopivotRow,
colset NopivotCol, rowrange *r, colrange *s,
int *selected)
/* Select a position (*r,*s) in the matrix A.T such that (A.T)[*r][*s] is nonzero
The choice is feasible, i.e., not on NopivotRow and NopivotCol, and
best with respect to the specified roworder
*/
{
int stop;
rowrange rtemp;
rowset rowexcluded;
double Xtemp;
stop = dp_FALSE;
set_initialize(&rowexcluded,m_size);
set_copy(rowexcluded,NopivotRow);
for (rtemp=rowmax+1;rtemp<=m_size;rtemp++) {
set_addelem(rowexcluded,rtemp); /* cannot pivot on any row > rmax */
}
*selected = dp_FALSE;
do {
rtemp=0;
dp_SelectPreorderedNext(m_size, n_size, rowexcluded, ordervec, &rtemp);
if (rtemp>=1) {
*r=rtemp;
*s=1;
while (*s <= n_size && !*selected) {
Xtemp=dp_TableauEntry(m_size, n_size,A,T,*r,*s);
if (!set_member(*s,NopivotCol) && dp_Nonzero(Xtemp)) {
*selected = dp_TRUE;
stop = dp_TRUE;
} else {
(*s)++;
}
}
if (!*selected) {
set_addelem(rowexcluded, rtemp);
}
}
else {
*r = 0;
*s = 0;
stop = dp_TRUE;
}
} while (!stop);
set_free(rowexcluded);
}
void dp_GaussianColumnPivot(rowrange m_size, colrange n_size,
Amatrix A, Bmatrix T, colindex nbindex, rowindex bflag, rowrange r, colrange s)
/* Update the Transformation matrix T with the pivot operation on (r,s)
This procedure performs a implicit pivot operation on the matrix A by
updating the dual basis inverse T.
*/
{
int localdebug=dp_FALSE;
long j, j1, entering;
Arow Rtemp;
double Xtemp0, Xtemp;
if (localdebug) {
fprintf(stdout, "Column pivot: (leaving, entering) = (%ld, %ld)\n", r, entering);
dp_WriteBmatrix(stdout, n_size, T);
dp_WriteTableau(stdout, m_size, n_size, A, T, nbindex, bflag);
}
for (j=1; j<=n_size; j++) Rtemp[j-1]=dp_TableauEntry(m_size, n_size, A, T, r,j);
Xtemp0 = Rtemp[s-1];
for (j = 1; j <= n_size; j++) {
if (j != s) {
Xtemp = Rtemp[j-1];
for (j1 = 1; j1 <= n_size; j1++)
T[j1-1][j-1] -= T[j1-1][s - 1] * Xtemp / Xtemp0;
}
}
for (j = 1; j <= n_size; j++) T[j-1][s - 1] /= Xtemp0;
entering=nbindex[s];
bflag[r]=s; /* the nonbasic variable r corresponds to column s */
nbindex[s]=r; /* the nonbasic variable on s column is r */
if (entering>0) bflag[entering]=-1;
/* original variables have negative index and should not affect the row index */
}
void dp_InitializeBmatrix(colrange n_size, Bmatrix T)
{
colrange j;
double x;
for (j = 0; j < n_size; j++) {
T[j]=(double *)calloc(n_size, sizeof x);
}
}
void dp_free_Bmatrix(colrange n_size, Bmatrix T)
{
colrange j;
for (j = 0; j < n_size; j++) {
free(T[j]);
}
}
void dp_SetToIdentity(colrange n_size, Bmatrix T)
{
colrange j1, j2;
for (j1 = 1; j1 <= n_size; j1++) {
for (j2 = 1; j2 <= n_size; j2++) {
if (j1 == j2)
T[j1 - 1][j2 - 1] = 1.0;
else
T[j1 - 1][j2 - 1] = 0.0;
}
}
}
void ResetTableau(rowrange m_size, colrange n_size, Bmatrix T,
colindex nbindex, rowindex bflag, rowrange objrow, colrange rhscol,
int UsePrevBasis)
{
rowrange i;
colrange j;
if (!UsePrevBasis) { /* Initialize T and nbindex */
for (j=1; j<=n_size; j++) nbindex[j]=-j;
nbindex[rhscol]=0;
/* RHS is already in nonbasis and is considered to be associated
with the zero-th row of input. */
dp_SetToIdentity(n_size, T);
}
/* Set the bflag according to nbindex */
for (i=1; i<=m_size; i++) bflag[i]=-1;
/* all basic variables have index -1 */
bflag[objrow]= 0;
/* bflag of the objective variable is 0,
different from other basic variables which have -1 */
for (j=1; j<=n_size; j++) if (nbindex[j]>0) bflag[nbindex[j]]=j;
/* bflag of a nonbasic variable is its column number */
}
void dp_SelectCrissCrossPivot(rowrange m_size, colrange n_size, Amatrix A, Bmatrix T,
rowindex bflag, rowrange objrow, colrange rhscol,
rowrange *r, colrange *s,
int *selected, dp_LPStatusType *lps)
{
int colselected=dp_FALSE, rowselected=dp_FALSE;
rowrange i;
double val;
*selected=dp_FALSE;
*lps=dp_LPSundecided;
while ((*lps==dp_LPSundecided) && (!rowselected) && (!colselected)) {
for (i=1; i<=m_size; i++) {
if (i!=objrow && bflag[i]==-1) { /* i is a basic variable */
val=dp_TableauEntry(m_size, n_size, A,T,i,rhscol);
if (dp_Negative(val)) {
rowselected=dp_TRUE;
*r=i;
break;
}
}
else if (bflag[i] >0) { /* i is nonbasic variable */
val=dp_TableauEntry(m_size, n_size, A,T,objrow,bflag[i]);
if (dp_Positive(val)) {
colselected=dp_TRUE;
*s=bflag[i];
break;
}
}
}
if ((!rowselected) && (!colselected)) {
*lps=dp_Optimal;
return;
}
else if (rowselected) {
for (i=1; i<=m_size; i++) {
if (bflag[i] >0) { /* i is nonbasic variable */
val=dp_TableauEntry(m_size, n_size, A,T,*r,bflag[i]);
if (dp_Positive(val)) {
colselected=dp_TRUE;
*s=bflag[i];
*selected=dp_TRUE;
break;
}
}
}
}
else if (colselected) {
for (i=1; i<=m_size; i++) {
if (i!=objrow && bflag[i]==-1) { /* i is a basic variable */
val=dp_TableauEntry(m_size, n_size, A,T,i,*s);
if (dp_Negative(val)) {
rowselected=dp_TRUE;
*r=i;
*selected=dp_TRUE;
break;
}
}
}
}
if (!rowselected) {
*lps=dp_DualInconsistent;
}
else if (!colselected) {
*lps=dp_Inconsistent;
}
}
}
void CrissCrossMinimize(rowrange m_size, colrange n_size,
Amatrix A,Bmatrix T,
rowrange objrow, colrange rhscol, int UsePrevBasis, dp_LPStatusType *LPS,
double *optvalue, Arow sol, Arow dsol, colindex nbindex,
rowrange *re, colrange *se, long *iter, dp_ErrorType *err)
{
colrange j;
*err=dp_None;
for (j=1; j<=n_size; j++)
A[objrow-1][j-1]=-A[objrow-1][j-1];
CrissCrossMaximize(m_size, n_size, A,T, objrow, rhscol,
UsePrevBasis, LPS, optvalue, sol, dsol, nbindex, re, se, iter, err);
*optvalue=-*optvalue;
for (j=1; j<=n_size; j++){
dsol[j-1]=-dsol[j-1];
A[objrow-1][j-1]=-A[objrow-1][j-1];
}
}
void CrissCrossMaximize(rowrange m_size, colrange n_size,
Amatrix A,Bmatrix T,
rowrange objrow, colrange rhscol, int UsePrevBasis, dp_LPStatusType *LPS,
double *optvalue, Arow sol, Arow dsol, colindex nbindex,
rowrange *re, colrange *se, long *iter, dp_ErrorType *err)
/*
When LP is inconsistent then *re returns the evidence row.
When LP is dual-inconsistent then *se returns the evidence column.
*/
{
int stop, chosen, found;
long rank, pivots_p0;
rowrange i,r;
colrange j,s;
static rowindex bflag;
static long mlast=0;
static rowindex OrderVector; /* the permutation vector to store a preordered row indeces */
int localdebug=dp_FALSE;
unsigned int rseed=1;
*err=dp_None;
if (bflag==NULL || mlast!=m_size){
if (mlast!=m_size) {
free(bflag); /* called previously with different m_size */
free(OrderVector);
}
bflag=(long *) calloc(m_size+1, sizeof *bflag);
OrderVector=(long *)calloc(m_size+1, sizeof *OrderVector);
/* initialize only for the first time or when a larger space is needed */
mlast=m_size;
}
/* Initializing control variables. */
dp_ComputeRowOrderVector(m_size, n_size, A, OrderVector, dp_MinIndex, rseed);
*re=0; *se=0; *iter=0;
ResetTableau(m_size,n_size, T, nbindex, bflag,
objrow, rhscol, UsePrevBasis);
FindLPBasis(m_size, n_size, A, T,
OrderVector, nbindex, bflag,
objrow, rhscol, UsePrevBasis, &s, &found, LPS, &pivots_p0);
*iter+=pivots_p0;
if (!found){
*se=s;
goto _L99;
/* No LP basis is found, and thus Inconsistent.
Output the evidence column. */
}
stop=dp_FALSE;
do { /* Criss-Cross Method */
dp_SelectCrissCrossPivot(m_size, n_size, A, T, bflag,
objrow, rhscol, &r, &s, &chosen, LPS);
if (chosen) {
dp_GaussianColumnPivot(m_size, n_size, A, T, nbindex, bflag, r, s);
(*iter)++;
} else {
switch (*LPS){
case dp_Inconsistent: *re=r;
case dp_DualInconsistent: *se=s;
default: break;
}
stop=dp_TRUE;
}
} while(!stop);
_L99:
SetSolutions(m_size, n_size, A, T,
objrow, rhscol, *LPS, optvalue, sol, dsol, nbindex, *re, *se);
}
int dp_LexSmaller(double *v1, double *v2, long nmax)
{ /* nmax is the size of vectors v1,v2 */
int determined, smaller;
colrange j;
smaller = dp_FALSE;
determined = dp_FALSE;
j = 1;
do {
if (dp_Nonzero(v1[j - 1]-v2[j - 1])) {
if (v1[j - 1] < v2[j - 1]) {
smaller = dp_TRUE;
}
determined = dp_TRUE;
} else
j++;
} while (!(determined) && (j <= nmax));
return smaller;
}
int dp_LexLarger(double *v1, double *v2, long nmax)
{
Arow u1, u2;
colrange j;
for (j = 1; j <= nmax; j++) {
u1[j-1] = -v1[j-1];
u2[j-1] = -v2[j-1];
}
return (dp_LexSmaller(u1, u2, nmax));
}
void FindLPBasis(rowrange m_size, colrange n_size,
Amatrix A, Bmatrix T, rowindex OV, colindex nbindex,
rowindex bflag, rowrange objrow, colrange rhscol,int useprevbasis,
colrange *cs, int *found, dp_LPStatusType *lps, long *pivot_no)
{ /* Find a LP basis using Gaussian pivots.
If the problem has an LP basis,
the procedure returns *found=dp_TRUE, *lps=LPSundecided and an LP basis.
If the constraint matrix A (excluding the rhs and objective) is not
column indepent, there are two cases. If the dependency gives a dual
inconsistency, this returns *found=dp_FALSE, *lps=dp_StrucDualInconsistent and
the evidence column *s. Otherwise, this returns *found=dp_TRUE,
*lps=LPSundecided and an LP basis of size less than n_size. Columns j
that do not belong to the basis (i.e. cannot be chosen as pivot because
they are all zero) will be indicated in nbindex vector: nbindex[j] will
be negative and set to -j.
*/
int localdebug=dp_FALSE,chosen,stop;
long pivots_p0=0, rank;
colset ColSelected;
rowset RowSelected;
double val;
rowrange i,r;
colrange j,s;
static Arow rcost;
*found=dp_FALSE; *cs=0; rank=0;
*lps=dp_LPSundecided;
set_initialize(&RowSelected,m_size);
set_initialize(&ColSelected,n_size);
set_addelem(RowSelected, objrow);
set_addelem(ColSelected, rhscol);
stop=dp_FALSE;
do { /* Find a LP basis */
dp_SelectPivot2(m_size, n_size, A, T, dp_MinIndex, OV,
m_size, RowSelected, ColSelected, &r, &s, &chosen);
if (localdebug && chosen) printf("Procedure FindBasis: pivot on (r,s) =(%ld, %ld).\n", r, s);
if (chosen) {
set_addelem(RowSelected, r);
set_addelem(ColSelected, s);
dp_GaussianColumnPivot(m_size, n_size, A, T, nbindex, bflag, r, s);
pivots_p0++;
rank++;
} else {
for (j=1;j<=n_size && *lps==dp_LPSundecided; j++) {
if (j!=rhscol && nbindex[j]<0){
if (localdebug) printf("col%ld %ld\n", j, nbindex[j]);
val=dp_TableauEntry(m_size, n_size, A,T,objrow,j);
if (dp_Nonzero(val)){ /* dual inconsistent */
*lps=dp_StrucDualInconsistent;
*cs=j;
if (localdebug)
printf("dual inconsistent because the nonzero reduced cost: %lf \n",val);
}
}
}
if (*lps==dp_LPSundecided) *found=dp_TRUE;
/* dependent columns but not dual inconsistent. */
stop=dp_TRUE;
}
if (rank==n_size-1) {
stop = dp_TRUE;
*found=dp_TRUE;
}
} while (!stop);
/* Check whether the objrow is in the basis in case of UsePrevBasis. */
if (useprevbasis && (s=bflag[objrow])>0){ /* objrow in the nonbasis. */
for (j=0;j<=n_size;j++){
if (j!=s) set_addelem(ColSelected,j);
if (i=nbindex[j]>0) set_addelem(RowSelected,i);
}
if (localdebug) printf("UsePrevBasis but the current basis does not contain objrow.\n");
dp_SelectPivot2(m_size, n_size, A, T, dp_MinIndex, OV,
m_size, RowSelected, ColSelected, &r, &s, &chosen);
if (localdebug && chosen) printf("Procedure FindBasis: pivot on (r,s) =(%ld, %ld).\n", r, s);
if (chosen) {
set_addelem(RowSelected, r);
set_addelem(ColSelected, s);
dp_GaussianColumnPivot(m_size, n_size, A, T, nbindex, bflag, r, s);
pivots_p0++;
} else {
/* objrow is nonbasic and the corresponding column is zero. */
*found=dp_FALSE;
*lps=dp_StrucDualInconsistent;
*cs=s;
}
}
*pivot_no=pivots_p0;
set_free(RowSelected);
set_free(ColSelected);
}
void FindDualFeasibleBasis(rowrange m_size, colrange n_size,
Amatrix A, Bmatrix T, rowindex OV,
colindex nbindex, rowindex bflag, rowrange objrow, colrange rhscol,
colrange *s, int *found, dp_LPStatusType *lps, long *pivot_no)
{ /* Find a dual feasible basis using Phase I of Dual Simplex method.
If the problem is dual feasible,
the procedure returns *found=dp_TRUE, *lps=LPSundecided and a dual feasible
basis. If the problem is dual infeasible, this returns
*found=dp_FALSE, *lps=DualInconsistent and the evidence column *s.
*/
int phase1, dualfeasible=dp_TRUE,localdebug=dp_FALSE,chosen,stop;
dp_LPStatusType LPSphase1;
long pivots_p1=0;
rowrange i,rtemp;
colrange j,l,mmsave,ms=0,stemp;
double val=0,purezero=0,maxcost=-1;
static long lastnn=0;
static Arow rcost;
*found=dp_TRUE; *lps=dp_LPSundecided; *s=0;
mmsave=m_size;
m_size=m_size+1; /* increase m_size by 1 temporally */
A[m_size-1]= (double *) calloc(n_size, sizeof val); /* create an auxiliary row */
lastnn=n_size;
ms=0; /* ms will be the index of column which has the largest reduced cost */
for (j=1; j<=n_size; j++){
if (j!=rhscol){
if (localdebug) printf("checking the column %ld var %ld\n", j,nbindex[j]);
rcost[j-1]=dp_TableauEntry(m_size, n_size, A,T,objrow,j);
if (localdebug) printf("reduced cost = %f\n",rcost[j-1]);
if (rcost[j-1] > maxcost) {maxcost=rcost[j-1]; ms = j;}
}
}
if (dp_Positive(maxcost)) dualfeasible=dp_FALSE;
if (!dualfeasible){
for (j=1; j<=n_size; j++){
A[m_size-1][j-1]=purezero;
for (l=1; l<=n_size; l++){
if (nbindex[l]>0) {
A[m_size-1][j-1]-=A[nbindex[l]-1][j-1];
/* To make the auxiliary row (0,-1,-1,...,-1). */
}
}
}
if (localdebug){
printf("Auxiliary row =");
for (j=1; j<=n_size; j++){
printf(" ( %ld):%f",j,dp_TableauEntry(m_size, n_size, A,T,m_size,j));
}
printf("\n");
}
if (localdebug){
printf("FindDualFeasibleBasis: curruent basis is not dual feasible.\n");
printf("because of the column %ld assoc. with var %ld dual cost =%f\n",
ms,nbindex[ms],maxcost);
}
/* Pivot on (m_size, ms) so that the dual basic solution becomes feasible */
dp_GaussianColumnPivot(m_size, n_size, A,T, nbindex, bflag, m_size, ms);
pivots_p1=pivots_p1+1;
phase1=dp_TRUE; stop=dp_FALSE;
do { /* Dual Simplex Phase I */
chosen=dp_FALSE; LPSphase1=dp_LPSundecided;
SelectDualSimplexPivot(m_size, n_size, phase1, A, T, OV, nbindex, bflag,
objrow, rhscol, &rtemp, &stemp, &chosen, &LPSphase1);
if (!chosen) {
/* The current dictionary is terminal. There are two cases:
dp_TableauEntry(m_size, n_size, A,T,objrow,ms) is negative or zero.
The first case implies dual infeasible,
and the latter implies dual feasible but m_size is still in nonbasis.
We must pivot in the auxiliary variable m_size. */
double minval=0;
rtemp=0;
for (i=1; i<=m_size; i++){
if (bflag[i]<0) {
/* i is basic and not the objective variable */
val=dp_TableauEntry(m_size, n_size, A,T,i,ms); /* auxiliary column*/
if (val < minval) {
rtemp=i;
minval=val;
if (localdebug) printf("update minval with = %f rtemp = %ld\n",minval, rtemp);
}
}
}
dp_GaussianColumnPivot(m_size, n_size, A, T, nbindex, bflag, rtemp, ms);
pivots_p1=pivots_p1+1;
if (dp_Negative(dp_TableauEntry(m_size, n_size, A, T, objrow, ms))){
if (localdebug){
printf("Dual infeasible.\n");
printf("obj-ms: %f dp_zero = %f\n",
dp_TableauEntry(m_size, n_size, A,T,objrow,ms), dp_zero);
}
*found=dp_FALSE; *lps=dp_DualInconsistent; *s=ms;
}
stop=dp_TRUE;
} else {
dp_GaussianColumnPivot(m_size, n_size, A, T, nbindex, bflag, rtemp, stemp);
pivots_p1=pivots_p1+1;
if (bflag[m_size]<0) {
stop=dp_TRUE;
if (localdebug) printf("Dual Phase I: the auxiliary variable entered the basis, go to phase II\n");
}
}
} while(!stop);
}
free(A[m_size-1]);
m_size=mmsave;
*pivot_no=pivots_p1;
}
void DualSimplexMinimize(rowrange m_size, colrange n_size,
Amatrix A,Bmatrix T,
rowrange objrow, colrange rhscol, int UsePrevBasis, dp_LPStatusType *LPS,
double *optvalue, Arow sol, Arow dsol, colindex nbindex,
rowrange *re, colrange *se, long *iter, dp_ErrorType *err)
{
colrange j;
*err=dp_None;
for (j=1; j<=n_size; j++)
A[objrow-1][j-1]=-A[objrow-1][j-1];
DualSimplexMaximize(m_size, n_size, A,T, objrow, rhscol, UsePrevBasis,
LPS, optvalue, sol, dsol, nbindex, re, se, iter, err);
*optvalue=-*optvalue;
for (j=1; j<=n_size; j++){
dsol[j-1]=-dsol[j-1];
A[objrow-1][j-1]=-A[objrow-1][j-1];
}
}
void DualSimplexMaximize(rowrange m_size, colrange n_size,
Amatrix A, Bmatrix T,
rowrange objrow, colrange rhscol, int UsePrevBasis, dp_LPStatusType *LPS,
double *optvalue, Arow sol, Arow dsol, colindex nbindex,
rowrange *re, colrange *se, long *iter, dp_ErrorType *err)
/*
When LP is inconsistent then *re returns the evidence row.
When LP is dual-inconsistent then *se returns the evidence column.
*/
{
double sw;