1 /* testlp3.c: Main test program to call the cdd lp library
2    written by Komei Fukuda, fukuda@ifor.math.ethz.ch
3    Version 0.93b, October 30, 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 
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28 #include <math.h>
29 #include <string.h>
30 
31 FILE *reading, *writing;
32 
33 
SetWriteFile(FILE ** f)34 void SetWriteFile(FILE **f)
35 {
36   char *fname;
37   fname="testlp.out";
38   *f = fopen(fname, "w");
39   printf("file %s is open\n",fname);
40 }
41 
42 
main(int argc,char * argv[])43 int main(int argc, char *argv[])
44 {
45   /* The original LP data m x n matrix
46      = | b   -A  |
47        | c0  c^T |,
48 
49   where the LP to be solved is to
50   maximize  c^T x  +   c0
51   subj. to
52             A   x  <=  b.
53   */
54 
55   dd_ErrorType err=dd_NoError;
56   dd_LPSolverType solver=dd_DualSimplex;
57      /* either DualSimplex or CrissCross */
58   dd_LPPtr lp,lp1;
59     /* pointer to LP data structure that is not visible by user. */
60   dd_LPSolutionPtr lps,lps1;
61     /* pointer to LP solution data that is visible by user. */
62 
63   dd_MatrixPtr M;
64   dd_colrange j;
65   dd_DataFileType inputfile;
66 
67   dd_set_global_constants();
68 
69   printf("\n--- Solving an LP with dd_LPSolve, and Finding an Interior Point  ---\n");
70 
71 /* Input an LP using the cdd library  */
72   dd_SetInputFile(&reading,inputfile,&err);
73   if (err!=dd_NoError) goto _L99;
74   M=dd_PolyFile2Matrix(reading, &err);
75   if (err!=dd_NoError) goto _L99;
76   /* dd_WriteMatrix(stdout, M);  */
77   lp=dd_Matrix2LP(M, &err);
78   if (err!=dd_NoError) goto _L99;
79 
80 /* Solve the LP by cdd LP solver. */
81   printf("\n--- Running dd_LPSolve ---\n");
82   solver=dd_DualSimplex;
83   dd_LPSolve(lp, solver, &err);  /* Solve the LP */
84   if (err!=dd_NoError) goto _L99;
85 
86 /* Write the LP solutions by cdd LP reporter. */
87 /*  dd_WriteLPResult(stdout, lp, err); */
88 /*  dd_WriteLPResult(writing, lp, err); */
89 
90 /* One can access the solutions by loading them.  See dd_WriteLPResult
91    for outputing the results correctly. */
92   lps=dd_CopyLPSolution(lp);
93   if (lps->LPS==dd_Optimal){
94     printf("Optimal solution found:\n");
95     printf("  primal_solution\n");
96     for (j=1; j<lps->d; j++) {
97       printf("  %3ld : ",j);
98       dd_WriteNumber(stdout,lps->sol[j]);
99       printf("\n");
100     }
101     printf("  dual_solution\n");
102     for (j=1; j<lps->d; j++){
103       if (lps->nbindex[j+1]>0) {
104         printf("  %3ld : ",lps->nbindex[j+1]);
105         dd_WriteNumber(stdout,lps->dsol[j]); printf("\n");
106       }
107     }
108     printf("  optimal_value : "); dd_WriteNumber(stdout,lps->optvalue);
109     printf("\n");
110   }
111 
112 /* Find an interior point with cdd LP library. */
113   printf("\n--- Running dd_FindInteriorPoint ---\n");
114   lp1=dd_MakeLPforInteriorFinding(lp);
115   printf("The LP to be solved for finding an interior point:\n");
116   dd_WriteLP(stdout,lp1);
117   dd_LPSolve(lp1,solver,&err);
118   if (err!=dd_NoError) goto _L99;
119 
120   /* Write an interior point. */
121   lps1=dd_CopyLPSolution(lp1);
122   if (dd_Positive(lps1->optvalue)){
123     printf("\nAn interior point found: (");
124     for (j=1; j <(lps1->d)-1; j++) {
125       dd_WriteNumber(stdout,lps1->sol[j]);
126     }
127     printf(")\n");
128   }
129   if (dd_Negative(lps1->optvalue))
130     printf("\nThe feasible region is empty.\n");
131   if (dd_EqualToZero(lps1->optvalue))
132     printf("\nThe feasible region is nonempty but has no interior point.\n");
133 
134 /* Free allocated spaces. */
135   dd_FreeLPSolution(lps);
136   dd_FreeLPData(lp);
137   dd_FreeLPSolution(lps1);
138   dd_FreeLPData(lp1);
139   dd_FreeMatrix(M);
140 
141 _L99:;
142   if (err!=dd_NoError) dd_WriteErrorMessages(stdout, err);
143   dd_free_global_constants();  /* At the end, this should be called. */
144   return 0;
145 }
146 
147 /* end of testlp3.c */
148 
149