Austin Schuh | 405fa6c | 2015-09-06 18:13:55 -0700 | [diff] [blame] | 1 | /* testlp1.c: Main test program to call the cdd lp library |
| 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 | FILE *reading, *writing; |
| 31 | |
| 32 | int main(int argc, char *argv[]) |
| 33 | { |
| 34 | /* The original LP data m x n matrix |
| 35 | = | b -A | |
| 36 | | c0 c^T |, |
| 37 | |
| 38 | where the LP to be solved is to |
| 39 | maximize c^T x + c0 |
| 40 | subj. to |
| 41 | A x <= b. |
| 42 | */ |
| 43 | |
| 44 | dd_ErrorType error=dd_NoError; |
| 45 | dd_MatrixPtr M,G; |
| 46 | dd_LPSolverType solver=dd_DualSimplex; /* either DualSimplex or CrissCross */ |
| 47 | dd_LPPtr lp; /* pointer to LP data structure that is not visible by user. */ |
| 48 | dd_LPSolutionPtr lps1; |
| 49 | dd_colrange j; |
| 50 | dd_rowset ImL, Lbasis; |
| 51 | |
| 52 | dd_PolyhedraPtr poly; |
| 53 | dd_DataFileType inputfile; |
| 54 | int ans; |
| 55 | |
| 56 | dd_set_global_constants(); /* First, this must be called once to use cddlib. */ |
| 57 | |
| 58 | printf("Welcome to cddlib %s\n",dd_DDVERSION); |
| 59 | |
| 60 | while (error==dd_NoError) { |
| 61 | |
| 62 | /* Input an LP using the cdd library */ |
| 63 | dd_SetInputFile(&reading,inputfile,&error); |
| 64 | if (error!=dd_NoError) goto _L99; |
| 65 | M=dd_PolyFile2Matrix(reading, &error); |
| 66 | if (error!=dd_NoError) goto _L99; |
| 67 | /* dd_WriteMatrix(stdout, M); */ |
| 68 | lp=dd_Matrix2LP(M, &error); |
| 69 | if (error!=dd_NoError) goto _L99; |
| 70 | |
| 71 | |
| 72 | /* Solve the LP by cdd LP solver. */ |
| 73 | printf("\n--- Running dd_LPSolve ---\n"); |
| 74 | dd_LPSolve(lp,solver,&error); |
| 75 | if (error!=dd_NoError) goto _L99; |
| 76 | |
| 77 | /* Write the LP solutions by cdd LP reporter. */ |
| 78 | dd_WriteLPResult(stdout, lp, error); |
| 79 | |
| 80 | /* Generate all vertices of the feasible reagion */ |
| 81 | printf("\nDo you want to compute the generator representation (y/n)? "); |
| 82 | ans=getchar(); |
| 83 | if (ans=='y' || ans=='Y'){ |
| 84 | poly=dd_DDMatrix2Poly(M, &error); |
| 85 | G=dd_CopyGenerators(poly); |
| 86 | printf("\nGenerators (All the vertices of the feasible region if bounded.)\n"); |
| 87 | dd_WriteMatrix(stdout,G); |
| 88 | |
| 89 | /* Free allocated spaces. */ |
| 90 | dd_FreeMatrix(G); |
| 91 | dd_FreePolyhedra(poly); |
| 92 | } |
| 93 | |
| 94 | /* Find an interior point with cdd LP library. */ |
| 95 | printf("\nDo you want to find a relative interior point (y/n)? "); |
| 96 | ans=getchar(); ans=getchar(); |
| 97 | if (ans=='y' || ans=='Y'){ |
| 98 | printf("\n--- Running dd_FindRelativeInteriorPoint ---\n"); |
| 99 | dd_FindRelativeInterior(M, &ImL, &Lbasis, &lps1, &error); |
| 100 | if (error!=dd_NoError) goto _L99; |
| 101 | |
| 102 | /* Write an interior point. */ |
| 103 | if (dd_Positive(lps1->optvalue)){ |
| 104 | printf("A relative interior point found: ("); |
| 105 | for (j=1; j <(lps1->d)-1; j++) { |
| 106 | dd_WriteNumber(stdout,lps1->sol[j]); |
| 107 | } |
| 108 | printf(")\nThe dimension of the region = "); |
| 109 | printf("%ld\n",M->colsize-set_card(Lbasis)-1); |
| 110 | if (set_card(ImL)>0) { |
| 111 | printf("Implicit equations: "); set_write(ImL); printf("\n"); |
| 112 | } |
| 113 | } else { |
| 114 | printf("The feasible region is empty.\n"); |
| 115 | } |
| 116 | dd_FreeLPSolution(lps1); |
| 117 | set_free(ImL); |
| 118 | set_free(Lbasis); |
| 119 | } |
| 120 | |
| 121 | /* Free allocated spaces. */ |
| 122 | dd_FreeMatrix(M); |
| 123 | dd_FreeLPData(lp); |
| 124 | } |
| 125 | _L99:; |
| 126 | fclose(reading); |
| 127 | if (error!=dd_NoError) dd_WriteErrorMessages(stdout, error); |
| 128 | dd_free_global_constants(); /* At the end, this should be called. */ |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /* end of testlp1.c */ |