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