blob: fe800a28d3072808b92cc21a8f48cfac7207d478 [file] [log] [blame]
Austin Schuh405fa6c2015-09-06 18:13:55 -07001/* simplecdd.c: Main test program to call the cdd library cddlib
2 written by Komei Fukuda, fukuda@ifor.math.ethz.ch
3 Version 0.94, Aug. 4, 2005
4 Standard ftp site: ftp.ifor.math.ethz.ch, Directory: pub/fukuda/cdd
5*/
6
7/* This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20*/
21
22#include "setoper.h"
23#include "cdd.h"
24#include <stdio.h>
25#include <stdlib.h>
26#include <time.h>
27#include <math.h>
28#include <string.h>
29
30dd_boolean SetInputFile(FILE **f, dd_DataFileType fname)
31{
32 dd_boolean success=dd_FALSE;
33 success=dd_FALSE;
34
35 if ( ( *f = fopen(fname, "r") )!= NULL) {
36 printf("input file %s is open\n", fname);
37 success=dd_TRUE;
38 }
39 else{
40 printf("The input file %s not found\n",fname);
41 }
42 return success;
43}
44
45dd_boolean SetWriteFile(FILE **f, dd_DataFileType fname)
46{
47 dd_boolean success=dd_FALSE;
48
49 if ( (*f = fopen(fname, "w")) != NULL){
50 printf("output file %s is open\n",fname);
51 success=dd_TRUE;
52 }
53 else{
54 printf("The output file %s cannot be opened\n",fname);
55 }
56 return success;
57}
58
59
60int main(int argc, char *argv[])
61{
62 dd_PolyhedraPtr poly;
63 dd_LPPtr lp;
64 dd_MatrixPtr M,A;
65 dd_ErrorType err=dd_NoError;
66 dd_DataFileType inputfile,outputfile;
67 FILE *reading=NULL, *writing;
68
69 dd_set_global_constants(); /* First, this must be called. */
70 dd_log=dd_TRUE; /* output log */
71
72 if (argc>1) strcpy(inputfile,argv[1]);
73 if (argc<=1 || !SetInputFile(&reading,argv[1])){
74 dd_WriteProgramDescription(stdout);
75 dd_SetInputFile(&reading,inputfile, &err);
76 }
77 if (err==dd_NoError) {
78 M=dd_PolyFile2Matrix(reading, &err);
79 }
80 else {
81 printf("Input file not found\n");
82 goto _L99;
83 }
84
85 if (err!=dd_NoError) goto _L99;
86
87 if (M->objective==dd_LPnone){ /* do representation conversion */
88 poly=dd_DDMatrix2Poly2(M, dd_LexMin, &err);
89 /* equivalent to poly=dd_DDMatrix2Poly2(M, &err) when the second argument is set to dd_LexMin. */
90 if (err!=dd_NoError) goto _L99;
91
92 dd_SetWriteFileName(inputfile, outputfile, 'o', poly->representation);
93 SetWriteFile(&writing, outputfile);
94 dd_WriteProgramDescription(writing);
95 dd_WriteRunningMode(writing, poly);
96 switch (poly->representation) {
97 case dd_Inequality:
98 fprintf(writing, "ext_file: Generators\n");
99 A=dd_CopyGenerators(poly);
100 dd_WriteMatrix(writing,A);
101 dd_FreeMatrix(A);
102 break;
103
104 case dd_Generator:
105 fprintf(writing, "ine_file: Inequalities\n");
106 A=dd_CopyInequalities(poly);
107 dd_WriteMatrix(writing,A);
108 dd_FreeMatrix(A);
109 break;
110
111 default:
112 break;
113 }
114 dd_WriteDDTimes(writing,poly);
115 fclose(writing);
116
117 dd_SetWriteFileName(inputfile, outputfile, 'a', poly->representation);
118 SetWriteFile(&writing, outputfile);
119 dd_WriteAdjacency(writing,poly);
120 fclose(writing);
121
122 dd_SetWriteFileName(inputfile, outputfile, 'j', poly->representation);
123 SetWriteFile(&writing, outputfile);
124 dd_WriteInputAdjacency(writing,poly);
125 fclose(writing);
126
127 dd_SetWriteFileName(inputfile, outputfile, 'i', poly->representation);
128 SetWriteFile(&writing, outputfile);
129 dd_WriteIncidence(writing,poly);
130 fclose(writing);
131
132 dd_SetWriteFileName(inputfile, outputfile, 'n', poly->representation);
133 SetWriteFile(&writing, outputfile);
134 dd_WriteInputIncidence(writing,poly);
135 fclose(writing);
136
137 dd_FreeMatrix(M);
138 dd_FreePolyhedra(poly);
139
140 } else { /* solve the LP */
141 lp=dd_Matrix2LP(M, &err); if (err!=dd_NoError) goto _L99;
142 dd_LPSolve(lp,dd_DualSimplex,&err); if (err!=dd_NoError) goto _L99;
143
144 dd_SetWriteFileName(inputfile, outputfile, 's', M->representation);
145 SetWriteFile(&writing, outputfile);
146 dd_WriteLPResult(writing, lp, err);
147 fclose(writing);
148
149 dd_FreeMatrix(M);
150 dd_FreeLPData(lp);
151 }
152_L99:
153 if (err!=dd_NoError) dd_WriteErrorMessages(stdout,err);
154 return 0;
155}
156
157
158/* end of simplecdd.c */