blob: d963c6f14042f1a70183f0b59006031dd6dd4259 [file] [log] [blame]
Austin Schuh405fa6c2015-09-06 18:13:55 -07001/* testshoot.c: Main test program to call the cdd lp library
2 written by Komei Fukuda, fukuda@ifor.math.ethz.ch
3 Version 0.92, August 24, 2001
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 lps1;
60 /* pointer to LP solution data that is visible by user. */
61
62 dd_rowrange i,m;
63 dd_colrange d;
64 dd_NumberType numb;
65 dd_MatrixPtr A;
66 dd_Arow r;
67 dd_colrange j;
68 int iter;
69
70/* Define an LP */
71/*
72 max 0 + x1 + x2
73 s.t.
74 1 - x2 >= 0
75 1 - x1 >= 0
76 x1 >= 0
77 x2 >= 0
78 2 - x1 - x2 >= 0
79
80 For this LP, we set up a matrix A as 4 x 3 matrix and a row vector:
81
82 1 0 -1 <- 1st constraint
83 1 -1 0
84 0 1 0
85 0 0 1
86 2 -1 -1 <- last constraint
87
88 0 1 1 <- objective row
89*/
90
91 dd_set_global_constants();
92
93 numb=dd_Real; /* set a number type */
94 m=5; /* number of rows */
95 d=3; /* number of columns */
96 A=dd_CreateMatrix(m,d);
97 dd_set_si(A->matrix[0][0],1); dd_set_si(A->matrix[0][1], 0); dd_set_si(A->matrix[0][2],-1);
98 dd_set_si(A->matrix[1][0],1); dd_set_si(A->matrix[1][1],-1); dd_set_si(A->matrix[1][2], 0);
99 dd_set_si(A->matrix[2][0],0); dd_set_si(A->matrix[2][1], 1); dd_set_si(A->matrix[2][2], 0);
100 dd_set_si(A->matrix[3][0],0); dd_set_si(A->matrix[3][1], 0); dd_set_si(A->matrix[3][2], 1);
101 dd_set_si(A->matrix[4][0],2); dd_set_si(A->matrix[4][1],-1); dd_set_si(A->matrix[4][2],-1);
102
103 dd_set_si(A->rowvec[0],0); dd_set_si(A->rowvec[1], 1); dd_set_si(A->rowvec[2], 1);
104
105 A->objective=dd_LPmax;
106 lp=dd_Matrix2LP(A, &err); /* load an LP */
107 if (lp==NULL) goto _L99;
108
109/* Find an interior point with cdd LP library. */
110 printf("\n--- Running dd_FindInteriorPoint ---\n");
111 lp1=dd_MakeLPforInteriorFinding(lp);
112 dd_LPSolve(lp1,solver,&err);
113 if (err!=dd_NoError) goto _L99;
114
115 /* Write an interior point. */
116 lps1=dd_CopyLPSolution(lp1);
117 if (dd_Positive(lps1->optvalue)){
118 printf("An interior point found: (");
119 for (j=1; j <(lps1->d)-1; j++) {
120 dd_WriteNumber(stdout,lps1->sol[j]);
121 }
122 printf(")\n");
123 }
124 if (dd_Negative(lps1->optvalue))
125 printf("The feasible region is empty.\n");
126 if (dd_EqualToZero(lps1->optvalue))
127 printf("The feasible region is nonempty but has no interior point.\n");
128
129/* Do shootings from the interior point. */
130
131 dd_InitializeArow(d, &r);
132
133 printf("Shooting test from the point:");
134 dd_WriteArow(stdout,lps1->sol,d); printf("\n");
135
136 for (iter=1; iter<=3; iter++){
137 dd_set_si(r[0],0); dd_set_si(r[1], 9998+iter); dd_set_si(r[2], 10000);
138 printf("Shooting to the direction:");
139 dd_WriteArow(stdout,r,d); printf("\n");
140 i=dd_RayShooting(A, lps1->sol, r);
141 printf("The first hyperplane hit: %ld.\n\n", i);
142 }
143
144/* Free allocated spaces. */
145 dd_FreeLPData(lp);
146 dd_FreeLPSolution(lps1);
147 dd_FreeLPData(lp1);
148 dd_FreeArow(d, r);
149 dd_FreeMatrix(A);
150
151_L99:;
152 if (err!=dd_NoError) dd_WriteErrorMessages(stdout, err);
153 dd_free_global_constants(); /* At the end, this should be called. */
154 return 0;
155}
156
157/* end of testlp3.c */
158