Austin Schuh | 405fa6c | 2015-09-06 18:13:55 -0700 | [diff] [blame] | 1 | /* testlp2.c: Main test program to call the cdd lp library |
| 2 | written by Komei Fukuda, fukuda@ifor.math.ethz.ch |
| 3 | Version 0.93a, July 23, 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 | 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_LPSolverType solver; /* either DualSimplex or CrissCross */ |
| 46 | dd_LPPtr lp; |
| 47 | |
| 48 | dd_rowrange m; |
| 49 | dd_colrange n; |
| 50 | dd_NumberType numb; |
| 51 | dd_MatrixPtr A; |
| 52 | dd_ErrorType err; |
| 53 | |
| 54 | |
| 55 | /* Define an LP */ |
| 56 | /* |
| 57 | max 0 + 3 x1 + 4 x2 |
| 58 | s.t. |
| 59 | 4/3 - 2 x1 - x2 >= 0 |
| 60 | 2/3 - x2 >= 0 |
| 61 | x1 >= 0 |
| 62 | x2 >= 0 |
| 63 | |
| 64 | For this LP, we set up a matrix A as 4 x 3 matrix and a row vector: |
| 65 | |
| 66 | 4/3 -2 -1 <- 1st constraint |
| 67 | 2/3 0 -1 |
| 68 | 0 1 0 |
| 69 | 0 0 1 <- last constraint |
| 70 | |
| 71 | 0 3 4 <- objective row |
| 72 | */ |
| 73 | |
| 74 | dd_set_global_constants(); |
| 75 | |
| 76 | numb=dd_Real; /* set a number type */ |
| 77 | m=4; /* number of rows */ |
| 78 | n=3; /* number of columns */ |
| 79 | A=dd_CreateMatrix(m,n); |
| 80 | dd_set_si2(A->matrix[0][0],4,3); dd_set_si(A->matrix[0][1],-2); dd_set_si(A->matrix[0][2],-1); |
| 81 | dd_set_si2(A->matrix[1][0],2,3); dd_set_si(A->matrix[1][1], 0); dd_set_si(A->matrix[1][2],-1); |
| 82 | dd_set_si(A->matrix[2][0],0); dd_set_si(A->matrix[2][1], 1); dd_set_si(A->matrix[2][2], 0); |
| 83 | dd_set_si(A->matrix[3][0],0); dd_set_si(A->matrix[3][1], 0); dd_set_si(A->matrix[3][2], 1); |
| 84 | |
| 85 | dd_set_si(A->rowvec[0],0); dd_set_si(A->rowvec[1], 3); dd_set_si(A->rowvec[2], 4); |
| 86 | |
| 87 | A->objective=dd_LPmax; |
| 88 | lp=dd_Matrix2LP(A, &err); /* load an LP */ |
| 89 | if (lp==NULL) goto _L99; |
| 90 | |
| 91 | /* Print the LP. */ |
| 92 | printf("\n--- LP to be solved ---\n"); |
| 93 | dd_WriteLP(stdout, lp); |
| 94 | |
| 95 | /* Solve the LP by cdd LP solver. */ |
| 96 | printf("\n--- Running dd_LPSolve ---\n"); |
| 97 | solver=dd_DualSimplex; |
| 98 | dd_LPSolve(lp, solver, &error); /* Solve the LP */ |
| 99 | if (error!=dd_NoError) goto _L99; |
| 100 | |
| 101 | /* Write the LP solutions by cdd LP reporter. */ |
| 102 | dd_WriteLPResult(stdout, lp, error); |
| 103 | |
| 104 | /* Free allocated spaces. */ |
| 105 | dd_FreeLPData(lp); |
| 106 | dd_FreeMatrix(A); |
| 107 | |
| 108 | _L99:; |
| 109 | if (error!=dd_NoError) dd_WriteErrorMessages(stdout, error); |
| 110 | dd_free_global_constants(); /* At the end, this should be called. */ |
| 111 | return 0; |
| 112 | } |
| 113 | |
| 114 | /* end of testlp2.c */ |
| 115 | |
| 116 | /* The dual LP is |
| 117 | |
| 118 | min 0 + 4 y1 + 2 y2 |
| 119 | s.t. |
| 120 | -3 + 2 y1 >= 0 |
| 121 | -4 y1 + y2 >= 0 |
| 122 | y1 >= 0 |
| 123 | y2 >= 0 |
| 124 | */ |