Austin Schuh | 405fa6c | 2015-09-06 18:13:55 -0700 | [diff] [blame] | 1 | /* allfaces.c: Test program to call the cdd library cddlib |
| 2 | written by Komei Fukuda, fukuda@ifor.math.ethz.ch |
| 3 | Version 0.94, August 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 | |
| 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 | dd_boolean FaceEnum(dd_MatrixPtr M, dd_rowset R, dd_rowset S, dd_boolean rip, dd_colrange mindim) |
| 60 | { |
| 61 | dd_ErrorType err; |
| 62 | dd_rowset LL, ImL, RR, SS, Lbasis; |
| 63 | dd_rowrange i,iprev=0; |
| 64 | dd_colrange j,dim; |
| 65 | dd_LPSolutionPtr lps=NULL; |
| 66 | dd_boolean success=dd_FALSE; |
| 67 | |
| 68 | set_initialize(&LL, M->rowsize); |
| 69 | set_initialize(&RR, M->rowsize); |
| 70 | set_initialize(&SS, M->rowsize); |
| 71 | set_copy(LL, M->linset); /* rememer the linset. */ |
| 72 | set_copy(RR, R); /* copy of R. */ |
| 73 | set_copy(SS, S); /* copy of S. */ |
| 74 | if (dd_ExistsRestrictedFace(M, R, S, &err)){ |
| 75 | set_uni(M->linset, M->linset, R); |
| 76 | dd_FindRelativeInterior(M, &ImL, &Lbasis, &lps, &err); |
| 77 | dim=M->colsize - set_card(Lbasis)-1; |
| 78 | set_uni(M->linset, M->linset, ImL); |
| 79 | fprintf(stdout,"%ld: ", dim); set_fwrite(stdout,M->linset); |
| 80 | if (rip){ |
| 81 | /* Write an interior point. */ |
| 82 | printf("RIP: ("); |
| 83 | for (j=1; j <(lps->d)-1; j++) { |
| 84 | dd_WriteNumber(stdout,lps->sol[j]); |
| 85 | } |
| 86 | printf(")\n"); |
| 87 | } |
| 88 | dd_FreeLPSolution(lps); |
| 89 | set_free(ImL); |
| 90 | set_free(Lbasis); |
| 91 | |
| 92 | if (dim>mindim){ |
| 93 | for (i=1; i<=M->rowsize; i++){ |
| 94 | if (!set_member(i, M->linset) && !set_member(i, S)){ |
| 95 | set_addelem(RR, i); |
| 96 | if (iprev) { |
| 97 | set_delelem(RR,iprev); |
| 98 | set_delelem(M->linset,iprev); |
| 99 | set_addelem(SS, iprev); |
| 100 | } |
| 101 | iprev=i; |
| 102 | FaceEnum(M, RR, SS, rip, mindim); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | } else if (err!=dd_NoError) goto _L99; |
| 107 | success=dd_TRUE; |
| 108 | |
| 109 | _L99: |
| 110 | set_copy(M->linset, LL); /* restore the linset */ |
| 111 | set_free(LL); |
| 112 | set_free(RR); |
| 113 | set_free(SS); |
| 114 | return success; |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | int main(int argc, char *argv[]) |
| 120 | { |
| 121 | dd_MatrixPtr M=NULL; |
| 122 | dd_rowrange m; |
| 123 | dd_ErrorType err=dd_NoError; |
| 124 | dd_rowset R, S; |
| 125 | dd_DataFileType inputfile; |
| 126 | FILE *reading=NULL; |
| 127 | char ch; |
| 128 | dd_colrange mindim; |
| 129 | dd_boolean rip=dd_FALSE; |
| 130 | |
| 131 | |
| 132 | dd_set_global_constants(); /* First, this must be called. */ |
| 133 | |
| 134 | if (argc>1) strcpy(inputfile,argv[1]); |
| 135 | if (argc<=1 || !SetInputFile(&reading,argv[1])){ |
| 136 | dd_WriteProgramDescription(stdout); |
| 137 | fprintf(stdout,"\ncddlib test program to list all faces of an H-polyhedron.\n"); |
| 138 | dd_SetInputFile(&reading,inputfile, &err); |
| 139 | } |
| 140 | if (err==dd_NoError) { |
| 141 | M=dd_PolyFile2Matrix(reading, &err); |
| 142 | } |
| 143 | else { |
| 144 | fprintf(stderr,"Input file not found\n"); |
| 145 | goto _L99; |
| 146 | } |
| 147 | |
| 148 | if (err!=dd_NoError) goto _L99; |
| 149 | |
| 150 | if (M->representation==dd_Generator){ |
| 151 | printf("The input is V-representation. Consider it as H-representation (N)? "); |
| 152 | ch=getchar(); getchar(); |
| 153 | if (ch!='y' && ch!='Y') goto _L99; |
| 154 | } |
| 155 | |
| 156 | m=M->rowsize; |
| 157 | |
| 158 | set_initialize(&R, M->rowsize); |
| 159 | set_initialize(&S, M->rowsize); |
| 160 | |
| 161 | printf("Output relative interior points (N)? "); |
| 162 | ch=getchar(); |
| 163 | if (ch=='y' || ch=='Y') rip=dd_TRUE; |
| 164 | printf("Minimum dimension of faces to list (0..%ld) ? ",M->colsize-1); |
| 165 | scanf("%ld", &mindim); |
| 166 | if (mindim>=M->colsize) mindim=M->colsize-1; |
| 167 | printf("Minimum dimension is set to %ld.", mindim); |
| 168 | |
| 169 | printf("\n--- FaceEnum (dim: active set) ---\nbegin\n"); |
| 170 | FaceEnum(M, R, S, rip, mindim); |
| 171 | fprintf(stderr,"end\nFaceEnum completed.\n"); |
| 172 | |
| 173 | dd_FreeMatrix(M); |
| 174 | set_free(R); |
| 175 | set_free(S); |
| 176 | _L99:; |
| 177 | if (err!=dd_NoError) dd_WriteErrorMessages(stderr,err); |
| 178 | dd_free_global_constants(); /* At the end, this should be called. */ |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /* end of allfaces.c */ |