-
Notifications
You must be signed in to change notification settings - Fork 2
/
cddarith.c
2063 lines (1896 loc) · 54.3 KB
/
cddarith.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
/* cddarith.c: Floating Point Arithmetic Procedures for cdd.c
written by Komei Fukuda, [email protected]
Version 0.61, December 1, 1997
*/
/* cdd.c : C-Implementation of the double description method for
computing all vertices and extreme rays of the polyhedron
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 "cdddef.h"
#include "cdd.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
void SetInequalitySets(rowrange m_size, rowindex id)
{
long i;
static long mprev=0;
boolean localdebug=FALSE;
if (mprev!=m_size){
if (localdebug) printf("SetInequalitySets: initializing inequality sets.\n");
if (GroundSet!=NULL) set_free(GroundSet);
if (EqualitySet!=NULL) set_free(EqualitySet);
if (NonequalitySet!=NULL) set_free(NonequalitySet);
set_initialize(&EqualitySet, m_size);
set_initialize(&NonequalitySet, m_size);
set_initialize(&GroundSet, m_size);
}
if (localdebug && mprev==m_size) printf("SetInequalitySets: Resetting inequality sets.\n");
set_emptyset(GroundSet);
set_emptyset(EqualitySet);
set_emptyset(NonequalitySet);
for (i = 1; i <= m_size; i++){
set_addelem(GroundSet, i);
if (id[i]==1) set_addelem(EqualitySet,i);
if (id[i]==-1) set_addelem(NonequalitySet,i);
}
mprev=m_size;
}
double AValue(colrange n_size, Amatrix A, double *p, rowrange i)
{
/*return the ith component of the vector A x p */
colrange j;
double temp;
temp = 0.0;
for (j = 0; j < n_size; j++)
temp += A[i - 1][j] * p[j];
return temp;
}
void StoreRay1(rowrange m_size, colrange n_size, Amatrix A,
double *p, RayRecord *RR, rowindex ordervec, boolean *feasible)
{ /* Original ray storing routine when RelaxedEnumeration is FALSE */
rowrange i,k,fii=m_size+1;
colrange j;
double temp;
boolean localdebug=FALSE;
if (debug) localdebug=TRUE;
*feasible = TRUE;
set_initialize(&(RR->ZeroSet),m_size);
RR->ARay = 0.0;
for (j = 0; j < n_size; j++)
RR->Ray[j] = p[j];
for (i = 1; i <= m_size; i++) {
k=ordervec[i];
temp = AValue(n_size, A, p, k);
if (fabs(temp) < zero)
set_addelem(RR->ZeroSet, k);
if (temp < -zero){
*feasible = FALSE;
if (fii>m_size) fii=i; /* the first violating inequality index */
}
}
RR->FirstInfeasIndex=fii;
RR->feasible = *feasible;
if (localdebug)
if (fii<= m_size)
printf("StoreRay1:store ray with fii= %ld (row =%ld)\n", fii,ordervec[fii]);
else
printf("StoreRay1:store ray with fii= %ld (feasible=%d)\n", fii,*feasible);
}
void StoreRay2(rowrange m_size, colrange n_size, Amatrix A,
double *p, RayRecord *RR, rowindex ordervec,
boolean *feasible, boolean *weaklyfeasible)
/* Ray storing routine when RelaxedEnumeration is TRUE.
weaklyfeasible is true iff it is feasible with
the strict_inequality conditions deleted. */
{
rowrange i,k,fii=m_size+1;
colrange j;
double temp;
boolean localdebug=FALSE;
if (debug) localdebug=TRUE;
*feasible = TRUE;
*weaklyfeasible = TRUE;
set_initialize(&(RR->ZeroSet),m_size);
RR->ARay = 0.0;
for (j = 0; j < n_size; j++)
RR->Ray[j] = p[j];
for (i = 1; i <= m_size; i++) {
k=ordervec[i];
temp = AValue(n_size, A, p, k);
if (fabs(temp) < zero){
set_addelem(RR->ZeroSet, k);
if (EqualityIndex[k]==-1)
*feasible=FALSE; /* strict inequality required */
}
if (temp < -zero){
*feasible = FALSE;
if (fii>m_size && EqualityIndex[k]>=0) {
fii=i; /* the first violating inequality index */
*weaklyfeasible=FALSE;
}
}
}
RR->FirstInfeasIndex=fii;
RR->feasible = *feasible;
if (localdebug) {
if (fii<= m_size)
printf("StoreRay2:store ray with fii= %ld (row =%ld)\n", fii,ordervec[fii]);
else
printf("StoreRay2:store ray with fii= %ld (weaklyfeasible=%d)\n", fii,*weaklyfeasible);
if (*weaklyfeasible) WriteRayRecord(stdout, n_size, RR);
}
}
void AddRay(rowrange m_size, colrange n_size, Amatrix A, double *p, rowindex ordervec)
{
boolean feasible, weaklyfeasible;
double x;
if (FirstRay == NULL) {
FirstRay = (struct RayRecord *) malloc(sizeof *FirstRay);
FirstRay->Ray = (double *) calloc(n_size, sizeof x);
if (debug)
printf("Create the first ray pointer\n");
LastRay = FirstRay;
ArtificialRay->Next = FirstRay;
} else {
LastRay->Next = (struct RayRecord *) malloc(sizeof *FirstRay);
LastRay->Next->Ray = (double *) calloc(n_size, sizeof x);
if (debug)
printf("Create a new ray pointer\n");
LastRay = LastRay->Next;
}
LastRay->Next = NULL;
RayCount++;
TotalRayCount++;
if (DynamicWriteOn) {
if (TotalRayCount % 100 == 0) {
fprintf(writing,
"*Rays (Total, Currently Active, Feasible) =%8ld%8ld%8ld\n",
TotalRayCount, RayCount, FeasibleRayCount);
printf("*Rays (Total, Currently Active, Feasible) =%8ld%8ld%8ld\n",
TotalRayCount, RayCount, FeasibleRayCount);
}
}
if (RelaxedEnumeration){
StoreRay2(m_size, n_size, A, p, LastRay, ordervec, &feasible, &weaklyfeasible);
if (weaklyfeasible) WeaklyFeasibleRayCount++;
} else {
StoreRay1(m_size, n_size, A, p, LastRay, ordervec, &feasible);
if (feasible) WeaklyFeasibleRayCount++;
/* weaklyfeasible is equiv. to feasible in this case. */
}
if (!feasible) return;
else {
FeasibleRayCount++;
if (fabs(LastRay->Ray[0]) > zero && Inequality == NonzeroRHS)
VertexCount++;
if (DynamicRayWriteOn) {
WriteRayRecord(writing, n_size, LastRay);
WriteRayRecord(stdout, n_size, LastRay);
if (IncidenceOutput==IncSet && writing_icd != NULL) {
WriteIncidence(writing_icd, m_size, n_size, LastRay);
}
}
}
}
void AddArtificialRay(rowrange m_size, colrange n_size, Amatrix A, rowindex ordervec)
{
Arow zerovector;
long j;
boolean feasible;
double x;
if (ArtificialRay != NULL) {
printf("Warning !!! FirstRay in not nil. Illegal Call\n");
return;
}
ArtificialRay = (struct RayRecord *) malloc(sizeof *ArtificialRay);
ArtificialRay->Ray = (double *) calloc(n_size, sizeof x);
if (debug)
printf("Create the artificial ray pointer\n");
for (j = 0; j < n_size; j++)
zerovector[j] = 0.0;
StoreRay1(m_size, n_size, A, zerovector, ArtificialRay, ordervec, &feasible);
ArtificialRay->Next = NULL;
}
void ConditionalAddEdge(rowrange m_size, colrange n_size,
RayRecord *Ray1, RayRecord *Ray2, RayRecord *ValidFirstRay,
rowrange iter, rowindex ordervec)
{
long it,it_row,fii1,fii2,fmin,fmax;
boolean adjacent,lastchance;
RayRecord *TempRay,*Rmin,*Rmax;
AdjacencyRecord *NewEdge;
boolean localdebug=FALSE;
rowset ZSmin, ZSmax;
fii1=Ray1->FirstInfeasIndex;
fii2=Ray2->FirstInfeasIndex;
if (fii1<fii2){
fmin=fii1; fmax=fii2;
Rmin=Ray1;
Rmax=Ray2;
}
else{
fmin=fii2; fmax=fii1;
Rmin=Ray2;
Rmax=Ray1;
}
ZSmin = Rmin->ZeroSet;
ZSmax = Rmax->ZeroSet;
if (localdebug) {
printf("ConditionalAddEdge: FMIN = %ld (row%ld) FMAX=%ld\n",
fmin,ordervec[fmin], fmax);
}
if (fmin==fmax){
if (localdebug) printf("ConditionalAddEdge: equal FII value-> No edge added\n");
}
else if (set_member(ordervec[fmin],ZSmax)){
if (localdebug) printf("ConditionalAddEdge: No strong separation -> No edge added\n");
}
else { /* the pair will be separated at the iteration fmin */
lastchance=TRUE;
/* flag to check it will be the last chance to store the edge candidate */
set_int(Face1, ZSmax, ZSmin);
count_int++;
if (localdebug){
printf("Face: ");
for (it=1; it<=m_size; it++) {
it_row=ordervec[it];
if (set_member(it_row, Face1)) printf("%ld ",it_row);
}
printf("\n");
}
for (it=iter+1; it < fmin && lastchance; it++){
it_row=ordervec[it];
if (EqualityIndex[it_row]>=0 && set_member(it_row, Face1)){
lastchance=FALSE;
count_int_bad++;
if (localdebug){
printf("There will be another chance iteration %ld (row %ld) to store the pair\n", it, it_row);
}
}
}
if (lastchance){
adjacent = TRUE;
count_int_good++;
/* adjacent checking */
set_int(Face, Face1, AddedHyperplanes);
if (localdebug){
printf("Check adjacency\n");
printf("AddedHyperplanes: "); set_write(AddedHyperplanes);
printf("Face: ");
for (it=1; it<=m_size; it++) {
it_row=ordervec[it];
if (set_member(it_row, Face)) printf("%ld ",it_row);
}
printf("\n");
}
if (set_card(Face)< n_size - 2) {
adjacent = FALSE;
}
else if (NondegAssumed) {
adjacent = TRUE;
}
else{
TempRay = ValidFirstRay; /* the first ray for adjacency checking */
while (TempRay != NULL && adjacent) {
if (TempRay != Ray1 && TempRay != Ray2) {
set_int(Face1, TempRay->ZeroSet, AddedHyperplanes);
if (set_subset(Face, Face1)) {
if (localdebug) set_write(Face1);
adjacent = FALSE;
}
}
TempRay = TempRay->Next;
}
}
if (adjacent){
if (localdebug) printf("The pair is adjacent and the pair must be stored for iteration %ld (row%ld)\n",
fmin, ordervec[fmin]);
NewEdge=(struct AdjacencyRecord *) malloc(sizeof *NewEdge);
NewEdge->Ray1=Rmax; /* save the one remains in iteration fmin in the first */
NewEdge->Ray2=Rmin; /* save the one deleted in iteration fmin in the second */
NewEdge->Next=NULL;
EdgeCount++;
TotalEdgeCount++;
if (Edges[fmin]==NULL){
Edges[fmin]=NewEdge;
if (localdebug) printf("Create a new edge list of %ld\n", fmin);
}else{
NewEdge->Next=Edges[fmin];
Edges[fmin]=NewEdge;
}
}
}
}
}
void CreateInitialEdges(rowrange m_size, colrange n_size, rowindex ordervec)
{
RayRecord *Ptr1, *Ptr2;
rowrange fii1,fii2;
long count=0;
boolean localdebug=FALSE;
if (FirstRay ==NULL || LastRay==NULL){
printf("Error found: CreateInitialEdges called with NULL pointer(s)\n");
goto _L99;
}
Ptr1=FirstRay;
while(Ptr1!=LastRay && Ptr1!=NULL){
fii1=Ptr1->FirstInfeasIndex;
Ptr2=Ptr1->Next;
while(Ptr2!=NULL){
fii2=Ptr2->FirstInfeasIndex;
count++;
if (localdebug) printf("CreateInitialEdges: edge %ld \n",count);
if (fii1!=fii2)
ConditionalAddEdge(m_size, n_size, Ptr1, Ptr2, FirstRay, n_size, ordervec);
Ptr2=Ptr2->Next;
}
Ptr1=Ptr1->Next;
}
_L99:;
}
void UpdateEdges(rowrange m_size, colrange n_size,
RayRecord *RRbegin, RayRecord *RRend, rowrange iter, rowindex ordervec)
/* This procedure must be called after the ray list is sorted
by EvaluateARay2 so that FirstInfeasIndex's are monotonically
increasing.
*/
{
RayRecord *Ptr1, *Ptr2begin, *Ptr2;
rowrange fii1;
boolean ptr2found,quit,localdebug=FALSE;
long count=0,pos1, pos2;
float workleft,prevworkleft=110.0,totalpairs;
totalpairs=(ZeroRayCount-1.0)*(ZeroRayCount-2.0)+1.0;
Ptr2begin = NULL;
if (RRbegin ==NULL || RRend==NULL){
if (1) printf("Warning: UpdateEdges called with NULL pointer(s)\n");
goto _L99;
}
Ptr1=RRbegin;
pos1=1;
do{
ptr2found=FALSE;
quit=FALSE;
fii1=Ptr1->FirstInfeasIndex;
pos2=2;
for (Ptr2=Ptr1->Next; !ptr2found && !quit; Ptr2=Ptr2->Next,pos2++){
if (Ptr2->FirstInfeasIndex > fii1){
Ptr2begin=Ptr2;
ptr2found=TRUE;
}
else if (Ptr2==RRend) quit=TRUE;
}
if (ptr2found){
quit=FALSE;
for (Ptr2=Ptr2begin; !quit ; Ptr2=Ptr2->Next){
count++;
if (localdebug) printf("UpdateEdges: edge %ld \n",count);
ConditionalAddEdge(m_size, n_size, Ptr1,Ptr2,RRbegin, iter, ordervec);
if (Ptr2==RRend || Ptr2->Next==NULL) quit=TRUE;
}
}
Ptr1=Ptr1->Next;
pos1++;
workleft = 100.0 * (ZeroRayCount-pos1) * (ZeroRayCount - pos1-1.0) / totalpairs;
if (ZeroRayCount>=500 && DynamicWriteOn && pos1%10 ==0 && prevworkleft-workleft>=10 ) {
printf("*Work of iteration %5ld(/%ld): %4ld/%4ld => %4.1f%% left\n",
iter, m_size, pos1, ZeroRayCount, workleft);
fprintf(writing,
"*Work of iteration %5ld(/%ld): %4ld/%4ld => %4.1f%% left\n",
iter, m_size, pos1, ZeroRayCount, workleft);
prevworkleft=workleft;
fflush(writing);
if (writing_icd != NULL) fflush(writing_icd);
}
}while(Ptr1!=RRend && Ptr1!=NULL);
_L99:;
}
void FreeDDMemory(rowindex ordervec)
{
RayRecord *Ptr, *PrevPtr;
long count;
boolean localdebug=FALSE;
PrevPtr=ArtificialRay;
count=0;
for (Ptr=ArtificialRay->Next; Ptr!=NULL; Ptr=Ptr->Next){
free(PrevPtr->Ray);
free(PrevPtr->ZeroSet);
free(PrevPtr);
count++;
PrevPtr=Ptr;
};
LastRay=NULL;
FirstRay=NULL;
ArtificialRay=NULL;
if (localdebug) printf("%ld ray storage spaces freed\n",count);
set_free(InitialHyperplanes);
set_free(AddedHyperplanes);
set_free(GroundSet);
set_free(Face);
set_free(Face1);
free(ordervec);
RayCount = 0;
TotalRayCount = 0;
FeasibleRayCount = 0;
WeaklyFeasibleRayCount = 0;
VertexCount = 0;
}
void Normalize(colrange n_size, double *V)
{
long j;
double min, temp;
min = 1.0e+20;
for (j = 0; j < n_size; j++) {
temp = fabs(V[j]);
if (temp > zero && temp < min)
min = temp;
}
for (j = 0; j < n_size; j++)
V[j] /= min;
}
void ZeroIndexSet(rowrange m_size, colrange n_size, Amatrix A, double *x, rowset ZS)
{
rowrange i;
double temp;
set_emptyset(ZS);
for (i = 1; i <= m_size; i++) {
temp = AValue(n_size, A, x, i);
if (fabs(temp) < zero)
set_addelem(ZS, i);
}
}
void CopyBmatrix(colrange n_size, Bmatrix T, Bmatrix TCOPY)
{
colrange j;
for (j=0; j < n_size; j++) {
TCOPY[j] = T[j];
}
}
void SelectPivot1(rowrange m_size, colrange n_size,
Amatrix X, HyperplaneOrderType roworder, rowindex ordervec,
rowrange rowmax, rowset NopivotRow,
colset NopivotCol, rowrange *r, colrange *s,
boolean *selected)
/* Select a position (*r,*s) in the matrix X such that X[*r][*s] is nonzero
The choice is feasible, i.e., not on NopivotRow and NopivotCol, and
best with respect to the specified roworder
*/
{
boolean stop;
rowrange rtemp;
rowset rowexcluded;
stop = 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 = FALSE;
do {
SelectNextHyperplane(m_size, n_size, X, roworder, rowexcluded,
&rtemp, &RecomputeRowOrder, ordervec);
if (rtemp>=1) {
*r=rtemp;
*s=1;
while (*s <= n_size && !*selected) {
if (!set_member(*s,NopivotCol) && fabs(X[*r - 1][*s - 1]) > zero) {
*selected = TRUE;
stop = TRUE;
} else {
(*s)++;
}
}
if (!*selected) {
set_addelem(rowexcluded, rtemp);
}
}
else {
*r = 0;
*s = 0;
stop = TRUE;
}
} while (!stop);
set_free(rowexcluded);
}
double TableauEntry(rowrange m_size, colrange n_size, Amatrix X, Bmatrix T,
rowrange r, colrange s)
/* Compute the (r,s) entry of X.T */
{
colrange j;
double temp;
temp=0;
for (j=0; j< n_size; j++) {
temp = temp + X[r-1][j] * T[j][s-1];
}
return temp;
}
void WriteTableau(FILE *f, rowrange m_size, colrange n_size, Amatrix X, Bmatrix T,
InequalityType ineq)
/* Write the tableau X.T */
{
colrange j;
rowrange i;
fprintf(f, "begin\n");
if (ineq==ZeroRHS)
fprintf(f, " %ld %ld real\n",m_size, n_size+1);
else
fprintf(f, " %ld %ld real\n",m_size, n_size);
for (i=1; i<= m_size; i++) {
if (ineq==ZeroRHS)
fprintf(f," %5d", 0); /* if RHS==0, the column is not explicitely stored */
for (j=1; j<= n_size; j++) {
fprintf(f," %12.6f",TableauEntry(m_size, n_size, X,T,i,j));
}
fprintf(f,"\n");
}
fprintf(f,"end\n");
}
void SelectPivot2(rowrange m_size, colrange n_size, Amatrix X, Bmatrix T,
HyperplaneOrderType roworder, rowindex ordervec,
rowrange rowmax, rowset NopivotRow,
colset NopivotCol, rowrange *r, colrange *s,
boolean *selected)
/* Select a position (*r,*s) in the matrix X.T such that (X.T)[*r][*s] is nonzero
The choice is feasible, i.e., not on NopivotRow and NopivotCol, and
best with respect to the specified roworder
*/
{
boolean stop;
rowrange i,rtemp;
rowset rowexcluded;
double Xtemp;
boolean localdebug=FALSE;
if (debug) localdebug=TRUE;
stop = FALSE;
set_initialize(&rowexcluded,m_size);
set_copy(rowexcluded,NopivotRow);
if (localdebug) {
switch (roworder) {
case MinIndex:
fprintf(stdout, "*SelectPivot2: MinIndex\n");
break;
case MaxIndex:
fprintf(stdout, "*SelectPivot2: MaxIndex\n");
break;
case MinCutoff:
fprintf(stdout, "*SelectPivot2: MinCutoff\n");
break;
case MaxCutoff:
fprintf(stdout, "*SelectPivot2: MaxCutoff\n");
break;
case MixCutoff:
fprintf(stdout, "*SelectPivot2: MixCutoff\n");
break;
case LexMin:
fprintf(stdout, "*SelectPivot2: LexMin\n");
break;
case LexMax:
fprintf(stdout, "*SelectPivot2: LexMax\n");
break;
case RandomRow:
fprintf(stdout, "*SelectPivot2: Random, Seed = %d\n",rseed);
break;
case LineShelling:
fprintf(stdout, "*SelectPivot2: LineShelling\n");
break;
}
printf("select pivot2: rowexcluded=");
set_fwrite(stdout,rowexcluded);
}
for (rtemp=rowmax+1;rtemp<=m_size;rtemp++) {
set_addelem(rowexcluded,rtemp); /* cannot pivot on any row > rmax */
}
*selected = FALSE;
do {
i=1;rtemp=0;
while (i<=m_size && rtemp==0) { /* EqualitySet vars have highest priorities */
if (set_member(i,EqualitySet) && !set_member(i,rowexcluded)){
if (localdebug) printf("marked set %ld chosen as a candidate\n",i);
rtemp=i;
}
i++;
}
if (rtemp==0) SelectNextHyperplane(m_size, n_size, X,
roworder, rowexcluded, &rtemp, &RecomputeRowOrder, ordervec);
if (rtemp>=1) {
*r=rtemp;
*s=1;
while (*s <= n_size && !*selected) {
Xtemp=TableauEntry(m_size, n_size,X,T,*r,*s);
if (!set_member(*s,NopivotCol) && fabs(Xtemp) > zero) {
*selected = TRUE;
stop = TRUE;
} else {
(*s)++;
}
}
if (!*selected) {
set_addelem(rowexcluded, rtemp);
}
}
else {
*r = 0;
*s = 0;
stop = TRUE;
}
} while (!stop);
set_free(rowexcluded);
}
void GausianColumnPivot1(rowrange m_size, colrange n_size,
Amatrix X, rowrange r, colrange s,
rowrange rowmax)
/* Make a column pivot operation in Amatrix X on position (r,s) */
{
long i, j;
double Xtemp0, Xtemp;
Xtemp0 = X[r - 1][s - 1];
for (j = 0; j < n_size; j++) {
if (j + 1 != s) {
Xtemp = X[r - 1][j];
for (i = 0; i < rowmax; i++) {
if (i + 1 != r)
X[i][j] -= X[i][s - 1] * Xtemp / Xtemp0;
}
X[r - 1][j] = 0.0;
}
}
for (i = 0; i < rowmax; i++) {
if (i + 1 != r)
X[i][s - 1] /= Xtemp0;
}
X[r - 1][s - 1] = 1.0;
}
void GausianColumnPivot2(rowrange m_size, colrange n_size,
Amatrix X, Bmatrix T, 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 X by
updating the dual basis inverse T.
*/
{
long j, j1;
Arow Rtemp;
double Xtemp0, Xtemp;
for (j=1; j<=n_size; j++) Rtemp[j-1]=TableauEntry(m_size, n_size, X, 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;
}
void 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 free_Bmatrix(colrange n_size, Bmatrix T)
{
colrange j;
for (j = 0; j < n_size; j++) {
free(T[j]);
}
}
void 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 ReduceA(rowrange *m_size, colrange *n_size, Amatrix A, rowset ChosenRow, colset ChosenCol)
/* Set the matrix AA to be the submatrix of AA with chosen
rows & columns, and change m_size and n_size accordingly
*/
{
long i,j,inew,jnew,mnew=0,nnew=0;
Amatrix Acopy;
double x;
mnew=set_card(ChosenRow);
nnew=set_card(ChosenCol);
for (i=0; i<mnew; i++){
Acopy[i]=(double *)calloc(nnew,sizeof x);
}
inew=0;
for (i=1; i <= *m_size; i++) {
if (set_member(i,ChosenRow)) {
inew++;
jnew=0;
for (j=1; j <= *n_size; j++) {
if (set_member(j, ChosenCol)){
jnew++;
Acopy[inew-1][jnew-1]=A[i-1][j-1];
if (debug) WriteReal(stdout, A[i-1][j-1]);
}
}
if (debug) fprintf(stdout,"\n");
}
}
for (i=1;i<= *m_size;i++) {
if (i<=mnew)
set_addelem(ChosenRow,i);
else
set_delelem(ChosenRow,i);
}
for (j=1;j<= *n_size;j++) {
if (j<=nnew)
set_addelem(ChosenCol,j);
else
set_delelem(ChosenCol,j);
}
if (debug) {
fprintf(stdout, "new row indices:");set_write(ChosenRow);
fprintf(stdout, "new col indices:");set_write(ChosenCol);
}
for (i=0;i< *m_size;i++){
free(A[i]);
}
for (i=0;i< mnew;i++){
A[i]=Acopy[i];
}
*m_size=mnew; *n_size=nnew;
}
void DualizeA(rowrange *m_size, colrange *n_size, Amatrix A, Bmatrix T)
/* Set the matrix A to be the transpose of the matrix [-A.T | I],
and change m_size and n_size accordingly
*/
{
long i,j,mnew,nnew;
Amatrix Acopy;
double x;
mnew=*n_size+*m_size;
nnew=*m_size;
for (i=0; i< *m_size; i++){
Acopy[i]=(double *)calloc(*n_size,sizeof x);
}
if (mnew > MMAX) {
printf("MMAX is too small for ray computation. MMAX must be >= %ld.\n",mnew);
Error = DimensionTooLarge;
goto _L99;
}
if (nnew > NMAX) {
printf("NMAX is too small for ray computation. NMAX must be >= %ld.\n",nnew);
Error = DimensionTooLarge;
goto _L99;
}
for (i=1;i<= *m_size;i++){
for (j=1;j<= *n_size;j++){
Acopy[i-1][j-1]=TableauEntry(*m_size, *n_size, A,T,i,j);
}
}
for (i=0;i< *m_size;i++){
free(A[i]);
}
for (i=0; i<mnew; i++){
A[i]=(double *)calloc(nnew,sizeof x);
}
for (i=1;i<= *n_size;i++){
for (j=1;j<= *m_size;j++){
A[i-1][j-1]=-Acopy[j-1][i-1];
}
}
for (i=1;i<= *m_size;i++){
for (j=1;j<= *m_size;j++){
if (i==j) A[*n_size+i-1][j-1]=1;
else A[*n_size+i-1][j-1]=0;
}
}
for (i=0; i< *m_size; i++){
free(Acopy[i]);
}
*m_size=mnew; *n_size=nnew;
_L99:;
}
void EnlargeAforInteriorFinding(rowrange *m_size, colrange *n_size, Amatrix A)
/* Add an extra column with all minus ones to the matrix AA,
add an objective row with (0,...,0,1), and
rows & columns, and change m_size and n_size accordingly
*/
{
long i,j,mnew=0,nnew=0;
Amatrix Acopy;
double x;
mnew=*m_size+1;
nnew=*n_size+1;
for (i=0; i<mnew; i++){
Acopy[i]=(double *)calloc(nnew,sizeof x);
}
for (i=1; i <= *m_size; i++) {
for (j=1; j <= *n_size; j++) {
Acopy[i-1][j-1]=A[i-1][j-1];
if (debug) WriteReal(stdout, A[i-1][j-1]);
}
if (debug) fprintf(stdout,"\n");
}
for (i=1;i<= *m_size;i++) {
Acopy[i-1][*n_size]=-1.0; /* new column with all minus one's */
}
for (j=1;j<= *n_size;j++) {
Acopy[*m_size][j-1]=0.0; /* new row with (0,...,0,1) */
}
Acopy[*m_size][*n_size]=1.0; /* new row with (0,...,0,1) */
for (i=0;i<*m_size;i++){
free(A[i]);
}
for (i=0;i< mnew;i++){
A[i]=Acopy[i];
}
*m_size=mnew; *n_size=nnew;
}
void ComputeRank(rowrange m_size, colrange n_size,
Amatrix A1, rowset TargetRows, rowindex ordervec, long *rank)
/* Compute the rank of the submatrix of a Amatrix A1 indexed by TargetRows.
This procedure does not change the matrix A1.
*/
{
boolean stop, chosen;
rowrange r;
colrange s;
rowset NoPivotRow;
colset ColSelected;
Bmatrix Btemp; /* dual basis inverse */
*rank = 0;
stop = FALSE;
set_initialize(&NoPivotRow, m_size);
set_compl(NoPivotRow,TargetRows);
set_initialize(&ColSelected, n_size);
InitializeBmatrix(n_size, Btemp);
SetToIdentity(n_size, Btemp);
if (debug) WriteBmatrix(stdout, n_size, Btemp);
do { /* Find a set of rows for a basis */
SelectPivot2(m_size, n_size, A1, Btemp, MinIndex, ordervec,
m_size, NoPivotRow, ColSelected, &r, &s, &chosen);
if (debug && chosen) printf("Procedure FindBasis: pivot on (r,s) =(%ld, %ld).\n", r, s);
if (chosen) {
set_addelem(NoPivotRow, r);
set_addelem(ColSelected, s);
(*rank)++;
GausianColumnPivot2(m_size, n_size, A1,Btemp, r, s);
if (debug) {
WriteBmatrix(stdout, n_size, Btemp);
printf("%3ldth row added to the initial set (%ldth elem)\n", r, *rank);
}
} else {
stop=TRUE;
}
} while (!stop);
set_free(NoPivotRow);
set_free(ColSelected);
free_Bmatrix(n_size, Btemp);
}
void ComputeBInverse(rowrange m_size, colrange n_size, Amatrix A1, long lastrow,
rowindex ordervec, Bmatrix InvA1, long *rank)
{
boolean stop, chosen;
rowrange r;
colrange s;
rowset RowSelected;
colset ColSelected;
*rank = 0;
stop = FALSE;
SetToIdentity(n_size, InvA1);
set_initialize(&RowSelected, m_size);
set_initialize(&ColSelected, n_size);
do {
SelectPivot2(m_size, n_size, A1, InvA1, MinIndex, ordervec,
lastrow, RowSelected, ColSelected, &r, &s, &chosen);
if (chosen) {
(*rank)++;
if (debug)
printf("%3ldth pivot on%3ld, %3ld\n", *rank, r, s);
GausianColumnPivot2(m_size, n_size, A1, InvA1, r, s);
set_addelem(RowSelected, r);
set_addelem(ColSelected, s);
} else
stop = TRUE;
} while (!stop);
set_free(RowSelected);
set_free(ColSelected);
}
void FindBasis(rowrange m_size, colrange n_size, Amatrix A1,
HyperplaneOrderType roword, rowindex ordervec,
rowset RowSelected,colindex ColInd,
Bmatrix BasisInverse, long *rank)
{
boolean stop, chosen;
rowset NopivotRow;
colset ColSelected;
rowrange r;
colrange j,s;
*rank = 0;
stop = FALSE;
for (j=0;j<=n_size;j++) ColInd[j]=0;
set_emptyset(RowSelected);
set_initialize(&ColSelected, n_size);