-
Notifications
You must be signed in to change notification settings - Fork 2
/
lrslib.c
6405 lines (5277 loc) · 164 KB
/
lrslib.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
/* lrslib.c library code for lrs */
/* modified by Gary Roumanis for multithread plrs compatability */
/* truncate needs mod to supress last pivot */
/* need to add a test for non-degenerate pivot step in reverse I guess */
/* Copyright: David Avis 2005,2011 [email protected] */
/* This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*/
#include <stdio.h>
#include <string.h>
#include <setjmp.h>
#include <limits.h>
#include "lrsrestart.h"
#include "lrslib.h"
static unsigned long dict_count, dict_limit, cache_tries, cache_misses;
/* Variables and functions global to this file only */
static long lrs_checkpoint_seconds = 0;
static long lrs_global_count = 0; /* Track how many lrs_dat records are
allocated */
static size_t infileLen; /* length of cache of input file */
static char *infile = NULL; /* cache of input for restart */
static char infilename[PATH_MAX];
static char outfilename[PATH_MAX];
static char tmpfilename[PATH_MAX];
static int tmpfd;
static long overflow=0; /* =0 no overflow =1 restart overwrite =2 restart append */
static long pivoting=FALSE; /* =0 no overflow =1 restart overwrite =2 restart append */
static jmp_buf buf1;
static lrs_dat_p *lrs_global_list[MAX_LRS_GLOBALS + 1];
static lrs_dic *new_lrs_dic (long m, long d, long m_A);
static void cache_dict (lrs_dic ** D_p, lrs_dat * global, long i, long j);
static long check_cache (lrs_dic ** D_p, lrs_dat * global, long *i_p, long *j_p);
static void save_basis (lrs_dic * D, lrs_dat * Q);
static void lrs_dump_state ();
static void pushQ (lrs_dat * global, long m, long d, long m_A);
#ifndef TIMES
static void ptimes (void);
static double get_time(void);
#endif
/*******************************/
/* signals handling */
/*******************************/
#ifndef SIGNALS
static void checkpoint ();
static void die_gracefully ();
static void setup_signals (void);
static void timecheck ();
#endif
/*******************************/
/* functions for external use */
/*******************************/
/******************************************************************/
/* lrs_run is the main reverse search part of lrs */
/* should be called by lrsv2_main which does setup and close also */
/******************************************************************/
long
lrs_run ( lrs_dic *P, lrs_dat * Q)
{
lrs_mp_matrix Lin; /* holds input linearities if any are found */
long col; /* output column index for dictionary */
long startcol = 0;
long prune = FALSE; /* if TRUE, getnextbasis will prune tree and backtrack */
/*********************************************************************************/
/* Step 1: Find a starting cobasis from default of specified order */
/* P is created to hold active dictionary data and may be cached */
/* Lin is created if necessary to hold linearity space */
/* Print linearity space if any, and retrieve output from first dict. */
/*********************************************************************************/
if (!lrs_getfirstbasis (&P, Q, &Lin, FALSE))
return 1;
/* Pivot to a starting dictionary */
/* There may have been column redundancy */
/* If so the linearity space is obtained and redundant */
/* columns are removed. User can access linearity space */
/* from lrs_mp_matrix Lin dimensions nredundcol x d+1 */
if (Q->homogeneous && Q->hull)
startcol++; /* col zero not treated as redundant */
if(!Q->restart)
for (col = startcol; col < Q->nredundcol; col++) /* print linearity space */
lrs_printoutput (Q, Lin[col]); /* Array Lin[][] holds the coeffs. */
if(Q->nredundcol > 0)
lrs_clear_mp_matrix(Lin,Q->nredundcol,Q->n);
/*********************************************************************************/
/* Step 3: Terminate if lponly option set, otherwise initiate a reverse */
/* search from the starting dictionary. Get output for each new dict. */
/*********************************************************************************/
/* We initiate reverse search from this dictionary */
/* getting new dictionaries until the search is complete */
/* User can access each output line from output which is */
/* vertex/ray/facet from the lrs_mp_vector output */
/* prune is TRUE if tree should be pruned at current node */
do
{
//2015.6.5 after maxcobases reached, generate subtrees that have not been enumerated
//2018.1.19 fix printcobasis bug when maxcobases set
//2019.5.8 new givoutput flag to avoid printing restart cobases
prune=lrs_checkbound(P,Q);
if (!prune && Q->giveoutput)
{
lrs_open_outputblock(); /* keeps output together when using mplrs */
for (col = 0; col <= P->d; col++) /* print output if any */
if (lrs_getsolution (P, Q, Q->output, col))
lrs_printoutput (Q, Q->output);
lrs_close_outputblock();
}
else
Q->giveoutput=TRUE; /* first output supressed for restart */
/*2020.3.9 bounds on objective function check corrected */
if ((Q->maxcobases > 0) && (Q->count[2] >=Q->maxcobases))
{
prune=TRUE;
if( !lrs_leaf(P,Q)) /* do not return cobases of a leaf */
lrs_return_unexplored(P,Q);
}
save_basis(P,Q);
}while (!Q->lponly && lrs_getnextbasis (&P, Q, prune)); // do ...
if (Q->lponly)
lrs_lpoutput(P,Q,Q->output);
else
lrs_printtotals (P, Q); /* print final totals, including estimates */
Q->m=P->m;
lrs_free_dic(P,Q); /* note Q is not free here and can be reused */
return 0;
}
/*********************************************/
/* end of model test program for lrs library */
/*********************************************/
/*******************************************************/
/* redund_run is main loop for redundancy removal */
/*******************************************************/
long
redund_run ( lrs_dic *P, lrs_dat * Q)
{
lrs_mp_matrix Ain; /* holds a copy of the input matrix to output at the end */
long ineq; /* input inequality number of current index */
long *redineq;
lrs_mp_matrix Lin; /* holds input linearities if any are found */
long i, j, d, m;
long nlinearity; /* number of linearities in input file */
long lastdv;
long debug;
long index; /* basic index for redundancy test */
/*********************************************************************************/
/* if non-negative flag is set, non-negative constraints are not input */
/* explicitly, and are not checked for redundancy */
m = P->m_A; /* number of rows of A matrix */
d = P->d;
redineq = Q->redineq;
debug = Q->debug;
Q->Ain = lrs_alloc_mp_matrix (m, d); /* make a copy of A matrix for output later */
Ain=Q->Ain;
for (i = 1; i <= m; i++)
{
for (j = 0; j <= d; j++)
copy (Ain[i][j], P->A[i][j]);
if (debug)
lrs_printrow ("*", Q, Ain[i], d);
}
/*********************************************************************************/
/* Step 1: Find a starting cobasis from default of specified order */
/* Lin is created if necessary to hold linearity space */
/*********************************************************************************/
if (!lrs_getfirstbasis (&P, Q, &Lin, TRUE))
return 1;
/* Pivot to a starting dictionary */
/* There may have been column redundancy */
/* If so the linearity space is obtained and redundant */
/* columns are removed. User can access linearity space */
/* from lrs_mp_matrix Lin dimensions nredundcol x d+1 */
/*********************************************************************************/
/* Step 2: Test rows i where redineq[i]=1 for redundancy */
/*********************************************************************************/
/* note some of these may have been changed in getting initial dictionary */
m = P->m_A;
d = P->d;
nlinearity = Q->nlinearity;
lastdv = Q->lastdv;
/* linearities are not considered for redundancy */
for (i = 0; i < nlinearity; i++)
redineq[Q->linearity[i]] = 2L;
if(Q->debug)
fprintf (lrs_ofp, "\nredundcheck=%ld verifyredund=%ld",Q->noredundcheck, Q->verifyredund);
/* Q->verifyredund always false in lrs, set by mplrs to check duplicated redundancy removal */
/* Q->noredundcheck overides this to skip verification */
if(Q->noredundcheck && Q->verifyredund)
goto done;
/* mplrs sets redineq[i]==-1 for guaranteed redundant inequalities */
/* these rows must be zeroed out before testing the others */
if (Q->verifyredund) /* this is never run by lrs, final step of mplrs redund */
for (index = lastdv + Q->redineq[0]; index <= m + d; index++)
{
ineq = Q->inequality[index - lastdv]; /* the input inequality number corr. to this index */
if( redineq[ineq]== -1 )
checkindex (P, Q, -index); /* used to zero correct row of A no LP solved */
}
/* rows 0..lastdv are cost, decision variables, or linearities */
/* other rows need to be tested */
for (index = lastdv + Q->redineq[0]; index <= m + d; index++)
{
ineq = Q->inequality[index - lastdv]; /* the input inequality number corr. to this index */
Q->redineq[0] = ineq; /* used for restarting after arithmetic overflow */
if( redineq[ineq]==1 )
{
redineq[ineq] = checkindex (P, Q, index);
if (debug)
fprintf (lrs_ofp, "\ncheck index=%ld, inequality=%ld, redineq=%ld", index, ineq, redineq[ineq]);
if(!Q->mplrs && Q->verbose)
{
if( redineq[ineq]==1 )
lrs_printrow ("*re ", Q, Ain[ineq], Q->inputd);
else
lrs_printrow ("*nr ", Q, Ain[ineq], Q->inputd);
}
}
} /* end for index ..... */
done:
if(Q->mplrs && !Q->verifyredund)
{ /* return array redineq to consumer */
char *ss;
int len=0;
ss=(char *)malloc(20*m*sizeof(char));
for (i=1; i<=m; i++)
if(redineq[i]==1)
len=len+sprintf(ss+len," %ld",i);
if(len>0)
lrs_post_output("redund", ss);
free(ss);
lrs_clear_mp_matrix(Ain,P->m_A,P->d);
Q->m=P->m;
lrs_free_dic(P,Q); /* note Q is not free here and can be reused */
return 0;
}
if (Q->verbose || Q->debug)
{
fprintf (lrs_ofp, "\n*redineq:");
for (i = 1; i <= m; i++)
fprintf (lrs_ofp, " %ld", redineq[i]);
}
redund_print(Ain,P,Q);
lrs_clear_mp_matrix(Ain,P->m_A,P->d);
Q->m=P->m;
lrs_free_dic(P,Q); /* note Q is not free here and can be reused */
return 0;
}
/*********************************************/
void redund_print(lrs_mp_matrix Ain,lrs_dic *P,lrs_dat *Q)
{
long i, m;
long nlinearity; /* number of linearities in input file */
long nredund; /* number of redundant rows in input file */
long *redineq=Q->redineq;
m = P->m_A; /* number of rows of A matrix */
nlinearity = Q->nlinearity;
/* restore as mplrs loses this */
for (i = 0; i < nlinearity; i++)
redineq[Q->linearity[i]]=2;
/*
fprintf(lrs_ofp,"\nQ->red");
for (i = 1; i <= m; i++)
fprintf(lrs_ofp," %ld",Q->redineq[i]);
*/
if (!Q->hull)
fprintf (lrs_ofp, "\nH-representation");
else
fprintf (lrs_ofp, "\nV-representation");
/* linearities will be printed first in output */
if (nlinearity > 0)
{
fprintf (lrs_ofp, "\nlinearity %ld", nlinearity);
for (i = 1; i <= nlinearity; i++)
fprintf (lrs_ofp, " %ld", i);
}
nredund = 0; /* count number of non-redundant inequalities */
for (i = 1; i <= m; i++)
if (redineq[i] == 0)
nredund++;
fprintf (lrs_ofp, "\nbegin");
fprintf (lrs_ofp, "\n%ld %ld rational", nlinearity+nredund, Q->n);
pivoting=TRUE;
/* print the linearities first */
for (i = 0; i < nlinearity; i++)
lrs_printrow ("", Q, Ain[Q->linearity[i]], Q->inputd);
for (i = 1; i <= m; i++)
if (redineq[i] == 0)
lrs_printrow ("", Q, Ain[i], Q->inputd);
fprintf (lrs_ofp, "\nend");
fprintf (lrs_ofp, "\n*Input had %ld rows and %ld columns", m, Q->n);
if( m==nredund)
fprintf (lrs_ofp, "\n*No redundant rows found");
else
{
fprintf (lrs_ofp, "\n* %ld redundant row(s) found:\n", m - nredund-nlinearity);
for (i=1; i<=m; i++)
if(redineq[i]==1 || redineq[i]==-1)
fprintf (lrs_ofp, " %ld",i);
if (Q->noredundcheck)
fprintf (lrs_ofp, "\n*Warning: not verified - input should be full dimensional and duplicate free");
}
fprintf (lrs_ofp, "\n");
return;
} /* end of redund_print */
/*******************/
/* lrs_printoutput */
/* one line only */
/*******************/
void
lrs_printoutput (lrs_dat * Q, lrs_mp_vector output)
{
char *sss;
char **ss;
long i;
long len=0;
if (Q->countonly)
return;
ss = (char **)malloc((1+Q->n) * sizeof(char*));
if (Q->hull || zero (output[0])) /*non vertex */
for (i = 0; i < Q->n; i++)
{
ss[i]=cpmp ("", output[i]);
len=len+snprintf(NULL, 0, "%s ", ss[i] );
}
else
for (i = 1; i < Q->n; i++)
{
ss[i]=cprat("", output[i], output[0]);
len=len+snprintf(NULL, 0, "%s ", ss[i] );
}
sss=(char*)malloc((len+5)* sizeof(char*));
len=0;
if (Q->hull || zero (output[0])) /*non vertex */
for (i = 0; i < Q->n; i++)
{
len=len+sprintf(sss+len,"%s ",ss[i]);
free(ss[i]);
}
else
{ /* vertex */
len=sprintf (sss, " 1 ");
for (i = 1; i < Q->n; i++)
{
len=len+sprintf(sss+len, "%s ", ss[i] );
free(ss[i]);
}
}
if(Q->mplrs)
lrs_post_output("vertex",sss);
else
fprintf (lrs_ofp, "\n%s",sss);
free(ss);
free(sss);
}
/**************************/
/* end of lrs_printoutput */
/**************************/
/****************/
/* lrs_lpoutput */
/****************/
void lrs_lpoutput(lrs_dic * P,lrs_dat * Q, lrs_mp_vector output)
{
if(Q->unbounded || !Q->messages)
return;
lrs_mp Temp1, Temp2;
long i;
lrs_alloc_mp (Temp1);
lrs_alloc_mp (Temp2);
prat ("\n*Obj=",P->objnum, P->objden);
fprintf (lrs_ofp, " pivots=%ld ",Q->count[3]);
if(Q->verbose)
{
fprintf (lrs_ofp, "\n\n*Primal: ");
for (i = 1; i < Q->n; i++)
{
fprintf(lrs_ofp,"x_%ld=",i);
prat ("", output[i], output[0]);
}
if(Q->nlinearity > 0)
fprintf (lrs_ofp, "\n\n*Linearities in input file - partial dual solution only");
fprintf (lrs_ofp, "\n\n*Dual: ");
for (i = 0; i < P->d; i++)
{
fprintf(lrs_ofp,"y_%ld=",Q->inequality[P->C[i]-Q->lastdv]);
changesign(P->A[0][P->Col[i]]);
mulint(Q->Lcm[P->Col[i]],P->A[0][P->Col[i]],Temp1);
mulint(Q->Gcd[P->Col[i]],P->det,Temp2);
prat("",Temp1,Temp2);
changesign(P->A[0][P->Col[i]]);
}
}
fprintf (lrs_ofp, "\n");
lrs_clear_mp (Temp1);
lrs_clear_mp (Temp2);
}
/***********************/
/* end of lrs_lpoutput */
/***********************/
void
lrs_printrow (const char *name, lrs_dat * Q, lrs_mp_vector output, long rowd)
/* print a row of A matrix in output in "original" form */
/* rowd+1 is the dimension of output vector */
/* if input is H-rep. output[0] contains the RHS */
/* if input is V-rep. vertices are scaled by 1/output[1] */
{
long i;
fprintf (lrs_ofp, "\n%s", name);
if (!Q->hull) /* input was inequalities, print directly */
{
for (i = 0; i <= rowd; i++)
pmp ("", output[i]);
return;
}
/* input was vertex/ray */
if (zero (output[1])) /*non-vertex */
{
for (i = 1; i <= rowd; i++)
pmp ("", output[i]);
}
else
{ /* vertex */
fprintf (lrs_ofp, " 1 ");
for (i = 2; i <= rowd; i++)
prat ("", output[i], output[1]);
}
return;
} /* end of lrs_printrow */
long
lrs_getsolution (lrs_dic * P, lrs_dat * Q, lrs_mp_vector output, long col)
/* check if column indexed by col in this dictionary */
/* contains output */
/* col=0 for vertex 1....d for ray/facet */
{
long j; /* cobasic index */
lrs_mp_matrix A = P->A;
long *Row = P->Row;
if (col == ZERO) /* check for lexmin vertex */
return lrs_getvertex (P, Q, output);
/* check for rays: negative in row 0 , positive if lponly */
if (Q->lponly)
{
if (!positive (A[0][col]))
return FALSE;
}
else if (!negative (A[0][col]))
return FALSE;
/* and non-negative for all basic non decision variables */
j = Q->lastdv + 1;
while (j <= P->m && !negative (A[Row[j]][col]))
j++;
if (j <= P->m)
return FALSE;
if (Q->geometric || Q->allbases || lexmin (P, Q, col) || Q->lponly)
return lrs_getray (P, Q, col, Q->n, output);
return FALSE; /* no more output in this dictionary */
} /* end of lrs_getsolution */
void
lrs_print_header(const char *name)
{
if(lrs_ofp == NULL)
lrs_ofp=stdout;
#ifdef LRS_QUIET
return;
#endif
fprintf (lrs_ofp,"%s", name);
fprintf (lrs_ofp,LRSLIB_TITLE);
fprintf (lrs_ofp,LRSLIB_VERSION);
fprintf (lrs_ofp,"(");
fprintf (lrs_ofp,BIT);
fprintf (lrs_ofp,",");
fprintf (lrs_ofp,ARITH);
#ifdef MA
fprintf (lrs_ofp,",hybrid arithmetic");
#endif
#ifdef LRSLONG
#ifndef SAFE
fprintf (lrs_ofp,",no overflow checking");
#endif
#endif
fprintf (lrs_ofp,")");
if(overflow != 2)
{
#ifdef GMP
fprintf(lrs_ofp," gmp v.%d.%d",__GNU_MP_VERSION,__GNU_MP_VERSION_MINOR);
#elif defined(FLINT)
fprintf(lrs_ofp," %dbit flint v.%s", FLINT_BITS, FLINT_VERSION);
#endif
}
}
long
lrs_init (const char *name) /* returns TRUE if successful, else FALSE */
{
#ifndef PLRS
#ifndef LRS_QUIET
if(overflow!=2)
lrs_print_header(name);
#endif
#endif
if (!lrs_mp_init (ZERO, stdin, stdout)) /* initialize arithmetic */
return FALSE;
lrs_global_count = 0;
lrs_checkpoint_seconds = 0;
#ifndef SIGNALS
setup_signals ();
#endif
return TRUE;
}
void
lrs_close (const char *name)
{
#ifdef PLRS
return;
#endif
#ifdef LRS_QUIET
fprintf (lrs_ofp, "\n");
if (lrs_ofp != stdout)
{
fclose (lrs_ofp);
lrs_ofp=NULL;
}
return;
#endif
#ifdef LRSLONG
#ifdef SAFE
fprintf (lrs_ofp, "\n*Overflow checking on lrslong arithmetic");
#else
fprintf (lrs_ofp, "\n*Caution: no overflow checking on long integer arithemtic");
#endif
#endif
fprintf (lrs_ofp, "\n*%s", name);
fprintf (lrs_ofp, LRSLIB_TITLE);
fprintf (lrs_ofp, LRSLIB_VERSION);
fprintf (lrs_ofp, "(");
fprintf (lrs_ofp, BIT);
fprintf (lrs_ofp, ",");
fprintf (lrs_ofp, ARITH);
#ifdef MA
fprintf (lrs_ofp, ",hybrid arithmetic");
#endif
fprintf (lrs_ofp, ")");
#ifdef MP
fprintf (lrs_ofp, " max digits=%ld/%ld", DIG2DEC (lrs_record_digits), DIG2DEC (lrs_digits));
#endif
#ifndef TIMES
ptimes ();
#endif
if (lrs_ofp != stdout)
{
fclose (lrs_ofp);
lrs_ofp=NULL;
}
}
/***********************************/
/* allocate and initialize lrs_dat */
/***********************************/
lrs_dat *
lrs_alloc_dat (const char *name)
{
lrs_dat *Q;
long i;
if (lrs_global_count >= MAX_LRS_GLOBALS)
{
fprintf (stderr,
"Fatal: Attempt to allocate more than %ld global data blocks\n", MAX_LRS_GLOBALS);
return NULL;
}
Q = (lrs_dat *) malloc (sizeof (lrs_dat));
if (Q == NULL)
return Q; /* failure to allocate */
lrs_global_list[lrs_global_count] = Q;
Q->id = lrs_global_count;
lrs_global_count++;
Q->name=(char *) CALLOC ((unsigned) strlen(name)+1, sizeof (char));
strcpy(Q->name,name);
/* initialize variables */
Q->mplrs=FALSE;
Q->messages=TRUE;
#ifdef PLRS
Q->mplrs=TRUE;
#endif
#ifdef LRS_QUIET
Q->messages=FALSE;
#endif
strcpy(Q->fname,""); /* name of program, filled in later */
Q->m = 0L;
Q->n = 0L;
Q->inputd = 0L;
Q->deepest = 0L;
Q->nlinearity = 0L;
Q->nredundcol = 0L;
Q->runs = 0L;
Q->subtreesize=MAXD;
Q->seed = 1234L;
Q->totalnodes = 0L;
for (i = 0; i < 10; i++)
{
Q->count[i] = 0L;
Q->cest[i] = 0.0;
if(i < 5)
Q->startcount[i] = 0L;
}
Q->count[2] = 1L; /* basis counter */
Q->startcount[2] = 0L; /* starting basis counter */
/* initialize flags */
Q->allbases = FALSE;
Q->bound = FALSE; /* upper/lower bound on objective function given */
Q->countonly = FALSE; /* produce the usual output */
Q->debug = FALSE;
Q->frequency = 0L;
Q->dualdeg = FALSE; /* TRUE if dual degenerate starting dictionary */
Q->geometric = FALSE;
Q->getvolume = FALSE;
Q->homogeneous = TRUE;
Q->polytope = FALSE;
Q->triangulation = FALSE;
Q->hull = FALSE;
Q->incidence = FALSE;
Q->lponly = FALSE;
Q->maxdepth = MAXD;
Q->mindepth = -MAXD;
Q->maxoutput = 0L;
Q->maxcobases = 0L; /* after maxcobases have been found unexplored subtrees reported */
Q->nash = FALSE;
Q->nonnegative = FALSE;
Q->printcobasis = FALSE;
Q->printslack = FALSE;
Q->truncate = FALSE; /* truncate tree when moving from opt vertex */
Q->extract=FALSE;
Q->verbose=FALSE;
Q->voronoi = FALSE;
Q->maximize = FALSE; /*flag for LP maximization */
Q->minimize = FALSE; /*flag for LP minimization */
Q->restart = FALSE; /* TRUE if restarting from some cobasis */
Q->givenstart = FALSE; /* TRUE if a starting cobasis is given */
Q->strace = -1L; /* turn on debug at basis # strace */
Q->etrace = -1L; /* turn off debug at basis # etrace */
Q->newstart=FALSE;
Q->giveoutput=TRUE; /* set to false for first output after restart */
Q->verifyredund=FALSE; /* set to true when mplrs verifies redund output */
Q->noredundcheck=FALSE; /* set to true when mplrs skips verifying output */
Q->nextineq=15; /* start redundancy testing from this row */
Q->startcob=NULL;
Q->saved_flag = 0; /* no cobasis saved initially, db */
lrs_alloc_mp (Q->Nvolume);
lrs_alloc_mp (Q->Dvolume);
lrs_alloc_mp (Q->sumdet);
lrs_alloc_mp (Q->saved_det);
lrs_alloc_mp (Q->boundn);
lrs_alloc_mp (Q->boundd);
itomp (ZERO, Q->Nvolume);
itomp (ONE, Q->Dvolume);
itomp (ZERO, Q->sumdet);
Q->unbounded = FALSE;
return Q;
} /* end of allocate and initialize lrs_dat */
/*******************************/
/* lrs_read_dat */
/*******************************/
long
lrs_read_dat (lrs_dat * Q, int argc, char *argv[])
{
char name[1000];
char writemode[2]="w"; /* will be set to "a" (append) for overflow or newstart */
long dec_digits = DEFAULT_DIGITS;
long infilenum=0; /*input file number to open if any */
long firstline = TRUE; /*flag for picking off name at line 1 */
long i;
int c; /* for fgetc */
int messages = Q->messages; /* print output for each option */
*tmpfilename='\0';
if(overflow==2) /* otherwise overwrite output */
strcpy(writemode,"a");
strcpy(outfilename, "\0");
if(argc > 1 )
{
infilenum=1;
if(Q->nash && argc ==2) /* legacy code to open second nash input file */
infilenum=2;
if(Q->nash && argc ==4) /* legacy code for nash output file */
strcpy(outfilename,argv[3]);
}
if (infilenum > 0 && (lrs_ifp = fopen (argv[infilenum], "r")) == NULL) /* command line overides stdin */
{
fprintf (stderr,"\n*bad input file name\n");
return (FALSE);
}
if (infilenum==1)
{
strcpy(infilename,argv[1]);
if(!Q->mplrs && messages && overflow == 0 )
printf ("\n*Input taken from %s", infilename);
fflush(stdout);
}
#ifdef LRSLONG
if(infilenum==0) /* stdin gets written to a temporary file */
{
strcpy(tmpfilename,"/tmp/lrs_stdinXXXXXX");
mkstemp(tmpfilename);
lrs_stdin_to_file(tmpfilename);
lrs_ifp=fopen (tmpfilename, "r");
strcpy(infilename,tmpfilename);
}
lrs_file_to_cache(lrs_ifp);
#endif
if(argc > 2) /* lrs has commandline arguments for newstart */
{
if (!Q->nash )
{
i=2;
while (i < argc) /* add command line arguments here */
{
if(strcmp(argv[i],"-newstart")==0) /* newstart not currently used ... */
{
strcpy(writemode,"a");
Q->newstart=TRUE;
}
else /* command line argument overides stdout */
strcpy(outfilename,argv[i++]);
}
}
if(strcmp(outfilename,"\0") != 0 )
{
if ((lrs_ofp = fopen (outfilename, writemode)) == NULL)
{
fprintf (stderr,"\n*bad output file name %s\n",outfilename);
return (FALSE);
}
else
if(overflow == 0)
printf ("\n*Output sent to file %s\n", outfilename);
}
}
/*2020.5.19 new redund handling, thanks to DB */
/* symbolic link from redund to lrs needed */
/* similar links if lrs1, lrs2 or lrsgmp used */
/* any redund option in input will overide */
if(!Q->mplrs && lrs_ofp != stdout && overflow != 2 ) /* headers for the output file also */
{
char *name;
name=(char *) malloc(strlen(Q->fname)+5);
strcpy(name,"*");
strcat(name,Q->fname);
strcat(name,":");
lrs_print_header(name);
free(name);
}
/* process input file */
if( fscanf (lrs_ifp, "%s", name) == EOF)
{
fprintf (stderr, "\n*no begin line");
return (FALSE);
}
while (strcmp (name, "begin") != 0) /*skip until "begin" found processing options */
{
if (strncmp (name, "*", 1) == 0) /* skip any line beginning with * */
{
c = name[0];
while (c != EOF && c != '\n')
c = fgetc (lrs_ifp);
}
else if (strcmp (name, "H-representation") == 0)
Q->hull = FALSE;
else if ((strcmp (name, "hull") == 0) || (strcmp (name, "V-representation") == 0))
{
Q->hull = TRUE;
Q->polytope = TRUE; /* will be updated as input read */
}
else if (strcmp (name, "digits") == 0)
{
if (fscanf (lrs_ifp, "%ld", &dec_digits) == EOF)
{
fprintf (stderr, "\n*no begin line");
return (FALSE);
}
if (!lrs_set_digits(dec_digits))
return (FALSE);
}
else if (strcmp (name, "linearity") == 0)
{
if (!readlinearity (Q))
return FALSE;
}
else if (strcmp (name, "nonnegative") == 0)
{
if(Q->nash)
fprintf (stderr, "\nNash incompatibile with nonnegative option - skipped");
else
Q->nonnegative = TRUE;
}
else if (firstline)
{
if(overflow != 2)
lrs_warning(Q,"warning",name);
firstline = FALSE;
}
if (fscanf (lrs_ifp, "%s", name) == EOF)
{