forked from cddlib/cddlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testlp2.c
123 lines (96 loc) · 3.28 KB
/
testlp2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* testlp2.c: Main test program to call the cdd lp library
written by Komei Fukuda, [email protected]
*/
/* This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "setoper.h"
#include "cdd.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
FILE *reading, *writing;
int main(int argc, char *argv[])
{
/* The original LP data m x n matrix
= | b -A |
| c0 c^T |,
where the LP to be solved is to
maximize c^T x + c0
subj. to
A x <= b.
*/
dd_ErrorType error=dd_NoError;
dd_LPSolverType solver; /* either DualSimplex or CrissCross */
dd_LPPtr lp;
dd_rowrange m;
dd_colrange n;
dd_NumberType numb;
dd_MatrixPtr A;
dd_ErrorType err;
/* Define an LP */
/*
max 0 + 3 x1 + 4 x2
s.t.
4/3 - 2 x1 - x2 >= 0
2/3 - x2 >= 0
x1 >= 0
x2 >= 0
For this LP, we set up a matrix A as 4 x 3 matrix and a row vector:
4/3 -2 -1 <- 1st constraint
2/3 0 -1
0 1 0
0 0 1 <- last constraint
0 3 4 <- objective row
*/
dd_set_global_constants();
numb=dd_Real; /* set a number type */
m=4; /* number of rows */
n=3; /* number of columns */
A=dd_CreateMatrix(m,n);
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);
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);
dd_set_si(A->matrix[2][0],0); dd_set_si(A->matrix[2][1], 1); dd_set_si(A->matrix[2][2], 0);
dd_set_si(A->matrix[3][0],0); dd_set_si(A->matrix[3][1], 0); dd_set_si(A->matrix[3][2], 1);
dd_set_si(A->rowvec[0],0); dd_set_si(A->rowvec[1], 3); dd_set_si(A->rowvec[2], 4);
A->objective=dd_LPmax;
lp=dd_Matrix2LP(A, &err); /* load an LP */
if (lp==NULL) goto _L99;
/* Print the LP. */
printf("\n--- LP to be solved ---\n");
dd_WriteLP(stdout, lp);
/* Solve the LP by cdd LP solver. */
printf("\n--- Running dd_LPSolve ---\n");
solver=dd_DualSimplex;
dd_LPSolve(lp, solver, &error); /* Solve the LP */
if (error!=dd_NoError) goto _L99;
/* Write the LP solutions by cdd LP reporter. */
dd_WriteLPResult(stdout, lp, error);
/* Free allocated spaces. */
dd_FreeLPData(lp);
dd_FreeMatrix(A);
_L99:;
if (error!=dd_NoError) dd_WriteErrorMessages(stdout, error);
dd_free_global_constants(); /* At the end, this should be called. */
return 0;
}
/* end of testlp2.c */
/* The dual LP is
min 0 + 4 y1 + 2 y2
s.t.
-3 + 2 y1 >= 0
-4 y1 + y2 >= 0
y1 >= 0
y2 >= 0
*/