Austin Schuh | 405fa6c | 2015-09-06 18:13:55 -0700 | [diff] [blame] | 1 | /* minkowski.c: Main test program to call the cdd library cddlib |
| 2 | written by Komei Fukuda, fukuda@ifor.math.ethz.ch |
| 3 | Version 0.93, July 15, 2003 |
| 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 | |
| 30 | dd_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 | |
| 45 | dd_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 | |
| 60 | int 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 | |
| 71 | if (argc>1) strcpy(inputfile,argv[1]); |
| 72 | if (argc<=1 || !SetInputFile(&reading,argv[1])){ |
| 73 | dd_WriteProgramDescription(stdout); |
| 74 | dd_SetInputFile(&reading,inputfile, &err); |
| 75 | } |
| 76 | if (err==dd_NoError) { |
| 77 | M=dd_PolyFile2Matrix(reading, &err); |
| 78 | } |
| 79 | else { |
| 80 | printf("Input file not found\n"); |
| 81 | goto _L99; |
| 82 | } |
| 83 | |
| 84 | if (err!=dd_NoError) goto _L99; |
| 85 | |
| 86 | if (M->objective==dd_LPnone){ /* do representation conversion */ |
| 87 | poly=dd_DDMatrix2Poly2(M, dd_LexMin, &err); |
| 88 | /* equivalent to poly=dd_DDMatrix2Poly2(M, &err) when the second argument is set to dd_LexMin. */ |
| 89 | if (err!=dd_NoError) goto _L99; |
| 90 | |
| 91 | dd_SetWriteFileName(inputfile, outputfile, 'o', poly->representation); |
| 92 | SetWriteFile(&writing, outputfile); |
| 93 | dd_WriteProgramDescription(writing); |
| 94 | dd_WriteRunningMode(writing, poly); |
| 95 | switch (poly->representation) { |
| 96 | case dd_Inequality: |
| 97 | fprintf(writing, "ext_file: Generators\n"); |
| 98 | A=dd_CopyGenerators(poly); |
| 99 | dd_WriteMatrix(writing,A); |
| 100 | dd_FreeMatrix(A); |
| 101 | break; |
| 102 | |
| 103 | case dd_Generator: |
| 104 | fprintf(writing, "ine_file: Inequalities\n"); |
| 105 | A=dd_CopyInequalities(poly); |
| 106 | dd_WriteMatrix(writing,A); |
| 107 | dd_FreeMatrix(A); |
| 108 | break; |
| 109 | |
| 110 | default: |
| 111 | break; |
| 112 | } |
| 113 | dd_WriteDDTimes(writing,poly); |
| 114 | fclose(writing); |
| 115 | |
| 116 | dd_SetWriteFileName(inputfile, outputfile, 'a', poly->representation); |
| 117 | SetWriteFile(&writing, outputfile); |
| 118 | dd_WriteAdjacency(writing,poly); |
| 119 | fclose(writing); |
| 120 | |
| 121 | dd_SetWriteFileName(inputfile, outputfile, 'j', poly->representation); |
| 122 | SetWriteFile(&writing, outputfile); |
| 123 | dd_WriteInputAdjacency(writing,poly); |
| 124 | fclose(writing); |
| 125 | |
| 126 | dd_SetWriteFileName(inputfile, outputfile, 'i', poly->representation); |
| 127 | SetWriteFile(&writing, outputfile); |
| 128 | dd_WriteIncidence(writing,poly); |
| 129 | fclose(writing); |
| 130 | |
| 131 | dd_SetWriteFileName(inputfile, outputfile, 'n', poly->representation); |
| 132 | SetWriteFile(&writing, outputfile); |
| 133 | dd_WriteInputIncidence(writing,poly); |
| 134 | fclose(writing); |
| 135 | |
| 136 | dd_FreeMatrix(M); |
| 137 | dd_FreePolyhedra(poly); |
| 138 | |
| 139 | } else { /* solve the LP */ |
| 140 | lp=dd_Matrix2LP(M, &err); if (err!=dd_NoError) goto _L99; |
| 141 | dd_LPSolve(lp,dd_DualSimplex,&err); if (err!=dd_NoError) goto _L99; |
| 142 | |
| 143 | dd_SetWriteFileName(inputfile, outputfile, 's', M->representation); |
| 144 | SetWriteFile(&writing, outputfile); |
| 145 | dd_WriteLPResult(writing, lp, err); |
| 146 | fclose(writing); |
| 147 | |
| 148 | dd_FreeMatrix(M); |
| 149 | dd_FreeLPData(lp); |
| 150 | } |
| 151 | _L99: |
| 152 | if (err!=dd_NoError) dd_WriteErrorMessages(stdout,err); |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /* end of simplecdd.c */ |