diff options
author | Alberto Ruiz <aruiz@um.es> | 2010-02-21 18:26:23 +0000 |
---|---|---|
committer | Alberto Ruiz <aruiz@um.es> | 2010-02-21 18:26:23 +0000 |
commit | f38b4a3076cfae023559ce61cb2a443c809b7a6f (patch) | |
tree | 022c127181fb65c34705cdcf44221b4ac89ba50b /packages/glpk/lib/Numeric/LinearProgramming/glpk.c | |
parent | a3d1bb34ae7b1f97b7e9900fc38f145094fe4777 (diff) |
simple glpk interface
Diffstat (limited to 'packages/glpk/lib/Numeric/LinearProgramming/glpk.c')
-rw-r--r-- | packages/glpk/lib/Numeric/LinearProgramming/glpk.c | 147 |
1 files changed, 147 insertions, 0 deletions
diff --git a/packages/glpk/lib/Numeric/LinearProgramming/glpk.c b/packages/glpk/lib/Numeric/LinearProgramming/glpk.c new file mode 100644 index 0000000..0f4ad79 --- /dev/null +++ b/packages/glpk/lib/Numeric/LinearProgramming/glpk.c | |||
@@ -0,0 +1,147 @@ | |||
1 | typedef struct { double r, i; } doublecomplex; | ||
2 | |||
3 | #define DVEC(A) int A##n, double*A##p | ||
4 | #define CVEC(A) int A##n, doublecomplex*A##p | ||
5 | #define DMAT(A) int A##r, int A##c, double*A##p | ||
6 | #define CMAT(A) int A##r, int A##c, doublecomplex*A##p | ||
7 | |||
8 | #define AT(M,r,co) (M##p[(r)*M##c+(co)]) | ||
9 | |||
10 | /*-----------------------------------------------------*/ | ||
11 | |||
12 | #include <stdlib.h> | ||
13 | #include <stdio.h> | ||
14 | #include <glpk.h> | ||
15 | #include <math.h> | ||
16 | |||
17 | int c_simplex_dense(DMAT(c), DMAT(b), DVEC(s)) { | ||
18 | glp_prob *lp; | ||
19 | lp = glp_create_prob(); | ||
20 | glp_set_obj_dir(lp, GLP_MAX); | ||
21 | |||
22 | int m = cr-1; | ||
23 | int n = cc; | ||
24 | glp_add_rows(lp, m); | ||
25 | glp_add_cols(lp, n); | ||
26 | int * ia = malloc((1+m*n)*sizeof(int)); | ||
27 | int * ja = malloc((1+m*n)*sizeof(int)); | ||
28 | double * ar = malloc((1+m*n)*sizeof(double)); | ||
29 | int i,j,k; | ||
30 | k=0; | ||
31 | for (i=1;i<=m;i++) { | ||
32 | for(j=1;j<=n;j++) { | ||
33 | k++; | ||
34 | ia[k] = i; | ||
35 | ja[k] = j; | ||
36 | ar[k] = AT(c,i,j-1); | ||
37 | //printf("%d %d %f\n",ia[k],ja[k],ar[k]); | ||
38 | } | ||
39 | } | ||
40 | glp_load_matrix(lp, k, ia, ja, ar); | ||
41 | for (j=1;j<=n;j++) { | ||
42 | glp_set_obj_coef(lp, j, AT(c,0,j-1)); | ||
43 | } | ||
44 | int t; | ||
45 | for (i=1;i<=m;i++) { | ||
46 | switch((int)rint(AT(b,i-1,0))) { | ||
47 | case 0: { t = GLP_FR; break; } | ||
48 | case 1: { t = GLP_LO; break; } | ||
49 | case 2: { t = GLP_UP; break; } | ||
50 | case 3: { t = GLP_DB; break; } | ||
51 | default: { t = GLP_FX; break; } | ||
52 | } | ||
53 | glp_set_row_bnds(lp, i, t , AT(b,i-1,1), AT(b,i-1,2)); | ||
54 | } | ||
55 | for (j=1;j<=n;j++) { | ||
56 | switch((int)rint(AT(b,m+j-1,0))) { | ||
57 | case 0: { t = GLP_FR; break; } | ||
58 | case 1: { t = GLP_LO; break; } | ||
59 | case 2: { t = GLP_UP; break; } | ||
60 | case 3: { t = GLP_DB; break; } | ||
61 | default: { t = GLP_FX; break; } | ||
62 | } | ||
63 | glp_set_col_bnds(lp, j, t , AT(b,m+j-1,1), AT(b,m+j-1,2)); | ||
64 | } | ||
65 | glp_term_out(0); | ||
66 | glp_simplex(lp, NULL); | ||
67 | sp[0] = glp_get_status(lp); | ||
68 | sp[1] = glp_get_obj_val(lp); | ||
69 | for (k=1; k<=n; k++) { | ||
70 | sp[k+1] = glp_get_col_prim(lp, k); | ||
71 | } | ||
72 | |||
73 | glp_delete_prob(lp); | ||
74 | free(ia); | ||
75 | free(ja); | ||
76 | free(ar); | ||
77 | |||
78 | return 0; | ||
79 | } | ||
80 | |||
81 | /* ---------------------------------------------------- */ | ||
82 | |||
83 | int c_simplex_sparse(int m, int n, DMAT(c), DMAT(b), DVEC(s)) { | ||
84 | glp_prob *lp; | ||
85 | lp = glp_create_prob(); | ||
86 | glp_set_obj_dir(lp, GLP_MAX); | ||
87 | int i,j,k; | ||
88 | int tot = cr - n; | ||
89 | glp_add_rows(lp, m); | ||
90 | glp_add_cols(lp, n); | ||
91 | |||
92 | //printf("%d %d\n",m,n); | ||
93 | |||
94 | // the first n values | ||
95 | for (k=1;k<=n;k++) { | ||
96 | glp_set_obj_coef(lp, k, AT(c, k-1, 2)); | ||
97 | //printf("%d %f\n",k,AT(c, k-1, 2)); | ||
98 | } | ||
99 | |||
100 | int * ia = malloc((1+tot)*sizeof(int)); | ||
101 | int * ja = malloc((1+tot)*sizeof(int)); | ||
102 | double * ar = malloc((1+tot)*sizeof(double)); | ||
103 | |||
104 | for (k=1; k<= tot; k++) { | ||
105 | ia[k] = rint(AT(c,k-1+n,0)); | ||
106 | ja[k] = rint(AT(c,k-1+n,1)); | ||
107 | ar[k] = AT(c,k-1+n,2); | ||
108 | //printf("%d %d %f\n",ia[k],ja[k],ar[k]); | ||
109 | } | ||
110 | glp_load_matrix(lp, tot, ia, ja, ar); | ||
111 | |||
112 | int t; | ||
113 | for (i=1;i<=m;i++) { | ||
114 | switch((int)rint(AT(b,i-1,0))) { | ||
115 | case 0: { t = GLP_FR; break; } | ||
116 | case 1: { t = GLP_LO; break; } | ||
117 | case 2: { t = GLP_UP; break; } | ||
118 | case 3: { t = GLP_DB; break; } | ||
119 | default: { t = GLP_FX; break; } | ||
120 | } | ||
121 | glp_set_row_bnds(lp, i, t , AT(b,i-1,1), AT(b,i-1,2)); | ||
122 | } | ||
123 | for (j=1;j<=n;j++) { | ||
124 | switch((int)rint(AT(b,m+j-1,0))) { | ||
125 | case 0: { t = GLP_FR; break; } | ||
126 | case 1: { t = GLP_LO; break; } | ||
127 | case 2: { t = GLP_UP; break; } | ||
128 | case 3: { t = GLP_DB; break; } | ||
129 | default: { t = GLP_FX; break; } | ||
130 | } | ||
131 | glp_set_col_bnds(lp, j, t , AT(b,m+j-1,1), AT(b,m+j-1,2)); | ||
132 | } | ||
133 | glp_term_out(0); | ||
134 | glp_simplex(lp, NULL); | ||
135 | sp[0] = glp_get_status(lp); | ||
136 | sp[1] = glp_get_obj_val(lp); | ||
137 | for (k=1; k<=n; k++) { | ||
138 | sp[k+1] = glp_get_col_prim(lp, k); | ||
139 | } | ||
140 | glp_delete_prob(lp); | ||
141 | free(ia); | ||
142 | free(ja); | ||
143 | free(ar); | ||
144 | |||
145 | return 0; | ||
146 | } | ||
147 | |||