rev |
line source |
alpar@9
|
1 /* glpapi14.c (processing models in GNU MathProg language) */
|
alpar@9
|
2
|
alpar@9
|
3 /***********************************************************************
|
alpar@9
|
4 * This code is part of GLPK (GNU Linear Programming Kit).
|
alpar@9
|
5 *
|
alpar@9
|
6 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@9
|
7 * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics,
|
alpar@9
|
8 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@9
|
9 * E-mail: <mao@gnu.org>.
|
alpar@9
|
10 *
|
alpar@9
|
11 * GLPK is free software: you can redistribute it and/or modify it
|
alpar@9
|
12 * under the terms of the GNU General Public License as published by
|
alpar@9
|
13 * the Free Software Foundation, either version 3 of the License, or
|
alpar@9
|
14 * (at your option) any later version.
|
alpar@9
|
15 *
|
alpar@9
|
16 * GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@9
|
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@9
|
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@9
|
19 * License for more details.
|
alpar@9
|
20 *
|
alpar@9
|
21 * You should have received a copy of the GNU General Public License
|
alpar@9
|
22 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@9
|
23 ***********************************************************************/
|
alpar@9
|
24
|
alpar@9
|
25 #define GLP_TRAN_DEFINED
|
alpar@9
|
26 typedef struct MPL glp_tran;
|
alpar@9
|
27
|
alpar@9
|
28 #include "glpmpl.h"
|
alpar@9
|
29 #include "glpapi.h"
|
alpar@9
|
30
|
alpar@9
|
31 glp_tran *glp_mpl_alloc_wksp(void)
|
alpar@9
|
32 { /* allocate the MathProg translator workspace */
|
alpar@9
|
33 glp_tran *tran;
|
alpar@9
|
34 tran = mpl_initialize();
|
alpar@9
|
35 return tran;
|
alpar@9
|
36 }
|
alpar@9
|
37
|
alpar@9
|
38 #if 1 /* 08/XII-2009 */
|
alpar@9
|
39 void _glp_mpl_init_rand(glp_tran *tran, int seed)
|
alpar@9
|
40 { if (tran->phase != 0)
|
alpar@9
|
41 xerror("glp_mpl_init_rand: invalid call sequence\n");
|
alpar@9
|
42 rng_init_rand(tran->rand, seed);
|
alpar@9
|
43 return;
|
alpar@9
|
44 }
|
alpar@9
|
45 #endif
|
alpar@9
|
46
|
alpar@9
|
47 int glp_mpl_read_model(glp_tran *tran, const char *fname, int skip)
|
alpar@9
|
48 { /* read and translate model section */
|
alpar@9
|
49 int ret;
|
alpar@9
|
50 if (tran->phase != 0)
|
alpar@9
|
51 xerror("glp_mpl_read_model: invalid call sequence\n");
|
alpar@9
|
52 ret = mpl_read_model(tran, (char *)fname, skip);
|
alpar@9
|
53 if (ret == 1 || ret == 2)
|
alpar@9
|
54 ret = 0;
|
alpar@9
|
55 else if (ret == 4)
|
alpar@9
|
56 ret = 1;
|
alpar@9
|
57 else
|
alpar@9
|
58 xassert(ret != ret);
|
alpar@9
|
59 return ret;
|
alpar@9
|
60 }
|
alpar@9
|
61
|
alpar@9
|
62 int glp_mpl_read_data(glp_tran *tran, const char *fname)
|
alpar@9
|
63 { /* read and translate data section */
|
alpar@9
|
64 int ret;
|
alpar@9
|
65 if (!(tran->phase == 1 || tran->phase == 2))
|
alpar@9
|
66 xerror("glp_mpl_read_data: invalid call sequence\n");
|
alpar@9
|
67 ret = mpl_read_data(tran, (char *)fname);
|
alpar@9
|
68 if (ret == 2)
|
alpar@9
|
69 ret = 0;
|
alpar@9
|
70 else if (ret == 4)
|
alpar@9
|
71 ret = 1;
|
alpar@9
|
72 else
|
alpar@9
|
73 xassert(ret != ret);
|
alpar@9
|
74 return ret;
|
alpar@9
|
75 }
|
alpar@9
|
76
|
alpar@9
|
77 int glp_mpl_generate(glp_tran *tran, const char *fname)
|
alpar@9
|
78 { /* generate the model */
|
alpar@9
|
79 int ret;
|
alpar@9
|
80 if (!(tran->phase == 1 || tran->phase == 2))
|
alpar@9
|
81 xerror("glp_mpl_generate: invalid call sequence\n");
|
alpar@9
|
82 ret = mpl_generate(tran, (char *)fname);
|
alpar@9
|
83 if (ret == 3)
|
alpar@9
|
84 ret = 0;
|
alpar@9
|
85 else if (ret == 4)
|
alpar@9
|
86 ret = 1;
|
alpar@9
|
87 return ret;
|
alpar@9
|
88 }
|
alpar@9
|
89
|
alpar@9
|
90 void glp_mpl_build_prob(glp_tran *tran, glp_prob *prob)
|
alpar@9
|
91 { /* build LP/MIP problem instance from the model */
|
alpar@9
|
92 int m, n, i, j, t, kind, type, len, *ind;
|
alpar@9
|
93 double lb, ub, *val;
|
alpar@9
|
94 if (tran->phase != 3)
|
alpar@9
|
95 xerror("glp_mpl_build_prob: invalid call sequence\n");
|
alpar@9
|
96 /* erase the problem object */
|
alpar@9
|
97 glp_erase_prob(prob);
|
alpar@9
|
98 /* set problem name */
|
alpar@9
|
99 glp_set_prob_name(prob, mpl_get_prob_name(tran));
|
alpar@9
|
100 /* build rows (constraints) */
|
alpar@9
|
101 m = mpl_get_num_rows(tran);
|
alpar@9
|
102 if (m > 0)
|
alpar@9
|
103 glp_add_rows(prob, m);
|
alpar@9
|
104 for (i = 1; i <= m; i++)
|
alpar@9
|
105 { /* set row name */
|
alpar@9
|
106 glp_set_row_name(prob, i, mpl_get_row_name(tran, i));
|
alpar@9
|
107 /* set row bounds */
|
alpar@9
|
108 type = mpl_get_row_bnds(tran, i, &lb, &ub);
|
alpar@9
|
109 switch (type)
|
alpar@9
|
110 { case MPL_FR: type = GLP_FR; break;
|
alpar@9
|
111 case MPL_LO: type = GLP_LO; break;
|
alpar@9
|
112 case MPL_UP: type = GLP_UP; break;
|
alpar@9
|
113 case MPL_DB: type = GLP_DB; break;
|
alpar@9
|
114 case MPL_FX: type = GLP_FX; break;
|
alpar@9
|
115 default: xassert(type != type);
|
alpar@9
|
116 }
|
alpar@9
|
117 if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
|
alpar@9
|
118 { type = GLP_FX;
|
alpar@9
|
119 if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
|
alpar@9
|
120 }
|
alpar@9
|
121 glp_set_row_bnds(prob, i, type, lb, ub);
|
alpar@9
|
122 /* warn about non-zero constant term */
|
alpar@9
|
123 if (mpl_get_row_c0(tran, i) != 0.0)
|
alpar@9
|
124 xprintf("glp_mpl_build_prob: row %s; constant term %.12g ig"
|
alpar@9
|
125 "nored\n",
|
alpar@9
|
126 mpl_get_row_name(tran, i), mpl_get_row_c0(tran, i));
|
alpar@9
|
127 }
|
alpar@9
|
128 /* build columns (variables) */
|
alpar@9
|
129 n = mpl_get_num_cols(tran);
|
alpar@9
|
130 if (n > 0)
|
alpar@9
|
131 glp_add_cols(prob, n);
|
alpar@9
|
132 for (j = 1; j <= n; j++)
|
alpar@9
|
133 { /* set column name */
|
alpar@9
|
134 glp_set_col_name(prob, j, mpl_get_col_name(tran, j));
|
alpar@9
|
135 /* set column kind */
|
alpar@9
|
136 kind = mpl_get_col_kind(tran, j);
|
alpar@9
|
137 switch (kind)
|
alpar@9
|
138 { case MPL_NUM:
|
alpar@9
|
139 break;
|
alpar@9
|
140 case MPL_INT:
|
alpar@9
|
141 case MPL_BIN:
|
alpar@9
|
142 glp_set_col_kind(prob, j, GLP_IV);
|
alpar@9
|
143 break;
|
alpar@9
|
144 default:
|
alpar@9
|
145 xassert(kind != kind);
|
alpar@9
|
146 }
|
alpar@9
|
147 /* set column bounds */
|
alpar@9
|
148 type = mpl_get_col_bnds(tran, j, &lb, &ub);
|
alpar@9
|
149 switch (type)
|
alpar@9
|
150 { case MPL_FR: type = GLP_FR; break;
|
alpar@9
|
151 case MPL_LO: type = GLP_LO; break;
|
alpar@9
|
152 case MPL_UP: type = GLP_UP; break;
|
alpar@9
|
153 case MPL_DB: type = GLP_DB; break;
|
alpar@9
|
154 case MPL_FX: type = GLP_FX; break;
|
alpar@9
|
155 default: xassert(type != type);
|
alpar@9
|
156 }
|
alpar@9
|
157 if (kind == MPL_BIN)
|
alpar@9
|
158 { if (type == GLP_FR || type == GLP_UP || lb < 0.0) lb = 0.0;
|
alpar@9
|
159 if (type == GLP_FR || type == GLP_LO || ub > 1.0) ub = 1.0;
|
alpar@9
|
160 type = GLP_DB;
|
alpar@9
|
161 }
|
alpar@9
|
162 if (type == GLP_DB && fabs(lb - ub) < 1e-9 * (1.0 + fabs(lb)))
|
alpar@9
|
163 { type = GLP_FX;
|
alpar@9
|
164 if (fabs(lb) <= fabs(ub)) ub = lb; else lb = ub;
|
alpar@9
|
165 }
|
alpar@9
|
166 glp_set_col_bnds(prob, j, type, lb, ub);
|
alpar@9
|
167 }
|
alpar@9
|
168 /* load the constraint matrix */
|
alpar@9
|
169 ind = xcalloc(1+n, sizeof(int));
|
alpar@9
|
170 val = xcalloc(1+n, sizeof(double));
|
alpar@9
|
171 for (i = 1; i <= m; i++)
|
alpar@9
|
172 { len = mpl_get_mat_row(tran, i, ind, val);
|
alpar@9
|
173 glp_set_mat_row(prob, i, len, ind, val);
|
alpar@9
|
174 }
|
alpar@9
|
175 /* build objective function (the first objective is used) */
|
alpar@9
|
176 for (i = 1; i <= m; i++)
|
alpar@9
|
177 { kind = mpl_get_row_kind(tran, i);
|
alpar@9
|
178 if (kind == MPL_MIN || kind == MPL_MAX)
|
alpar@9
|
179 { /* set objective name */
|
alpar@9
|
180 glp_set_obj_name(prob, mpl_get_row_name(tran, i));
|
alpar@9
|
181 /* set optimization direction */
|
alpar@9
|
182 glp_set_obj_dir(prob, kind == MPL_MIN ? GLP_MIN : GLP_MAX);
|
alpar@9
|
183 /* set constant term */
|
alpar@9
|
184 glp_set_obj_coef(prob, 0, mpl_get_row_c0(tran, i));
|
alpar@9
|
185 /* set objective coefficients */
|
alpar@9
|
186 len = mpl_get_mat_row(tran, i, ind, val);
|
alpar@9
|
187 for (t = 1; t <= len; t++)
|
alpar@9
|
188 glp_set_obj_coef(prob, ind[t], val[t]);
|
alpar@9
|
189 break;
|
alpar@9
|
190 }
|
alpar@9
|
191 }
|
alpar@9
|
192 /* free working arrays */
|
alpar@9
|
193 xfree(ind);
|
alpar@9
|
194 xfree(val);
|
alpar@9
|
195 return;
|
alpar@9
|
196 }
|
alpar@9
|
197
|
alpar@9
|
198 int glp_mpl_postsolve(glp_tran *tran, glp_prob *prob, int sol)
|
alpar@9
|
199 { /* postsolve the model */
|
alpar@9
|
200 int i, j, m, n, stat, ret;
|
alpar@9
|
201 double prim, dual;
|
alpar@9
|
202 if (!(tran->phase == 3 && !tran->flag_p))
|
alpar@9
|
203 xerror("glp_mpl_postsolve: invalid call sequence\n");
|
alpar@9
|
204 if (!(sol == GLP_SOL || sol == GLP_IPT || sol == GLP_MIP))
|
alpar@9
|
205 xerror("glp_mpl_postsolve: sol = %d; invalid parameter\n",
|
alpar@9
|
206 sol);
|
alpar@9
|
207 m = mpl_get_num_rows(tran);
|
alpar@9
|
208 n = mpl_get_num_cols(tran);
|
alpar@9
|
209 if (!(m == glp_get_num_rows(prob) &&
|
alpar@9
|
210 n == glp_get_num_cols(prob)))
|
alpar@9
|
211 xerror("glp_mpl_postsolve: wrong problem object\n");
|
alpar@9
|
212 if (!mpl_has_solve_stmt(tran))
|
alpar@9
|
213 { ret = 0;
|
alpar@9
|
214 goto done;
|
alpar@9
|
215 }
|
alpar@9
|
216 for (i = 1; i <= m; i++)
|
alpar@9
|
217 { if (sol == GLP_SOL)
|
alpar@9
|
218 { stat = glp_get_row_stat(prob, i);
|
alpar@9
|
219 prim = glp_get_row_prim(prob, i);
|
alpar@9
|
220 dual = glp_get_row_dual(prob, i);
|
alpar@9
|
221 }
|
alpar@9
|
222 else if (sol == GLP_IPT)
|
alpar@9
|
223 { stat = 0;
|
alpar@9
|
224 prim = glp_ipt_row_prim(prob, i);
|
alpar@9
|
225 dual = glp_ipt_row_dual(prob, i);
|
alpar@9
|
226 }
|
alpar@9
|
227 else if (sol == GLP_MIP)
|
alpar@9
|
228 { stat = 0;
|
alpar@9
|
229 prim = glp_mip_row_val(prob, i);
|
alpar@9
|
230 dual = 0.0;
|
alpar@9
|
231 }
|
alpar@9
|
232 else
|
alpar@9
|
233 xassert(sol != sol);
|
alpar@9
|
234 if (fabs(prim) < 1e-9) prim = 0.0;
|
alpar@9
|
235 if (fabs(dual) < 1e-9) dual = 0.0;
|
alpar@9
|
236 mpl_put_row_soln(tran, i, stat, prim, dual);
|
alpar@9
|
237 }
|
alpar@9
|
238 for (j = 1; j <= n; j++)
|
alpar@9
|
239 { if (sol == GLP_SOL)
|
alpar@9
|
240 { stat = glp_get_col_stat(prob, j);
|
alpar@9
|
241 prim = glp_get_col_prim(prob, j);
|
alpar@9
|
242 dual = glp_get_col_dual(prob, j);
|
alpar@9
|
243 }
|
alpar@9
|
244 else if (sol == GLP_IPT)
|
alpar@9
|
245 { stat = 0;
|
alpar@9
|
246 prim = glp_ipt_col_prim(prob, j);
|
alpar@9
|
247 dual = glp_ipt_col_dual(prob, j);
|
alpar@9
|
248 }
|
alpar@9
|
249 else if (sol == GLP_MIP)
|
alpar@9
|
250 { stat = 0;
|
alpar@9
|
251 prim = glp_mip_col_val(prob, j);
|
alpar@9
|
252 dual = 0.0;
|
alpar@9
|
253 }
|
alpar@9
|
254 else
|
alpar@9
|
255 xassert(sol != sol);
|
alpar@9
|
256 if (fabs(prim) < 1e-9) prim = 0.0;
|
alpar@9
|
257 if (fabs(dual) < 1e-9) dual = 0.0;
|
alpar@9
|
258 mpl_put_col_soln(tran, j, stat, prim, dual);
|
alpar@9
|
259 }
|
alpar@9
|
260 ret = mpl_postsolve(tran);
|
alpar@9
|
261 if (ret == 3)
|
alpar@9
|
262 ret = 0;
|
alpar@9
|
263 else if (ret == 4)
|
alpar@9
|
264 ret = 1;
|
alpar@9
|
265 done: return ret;
|
alpar@9
|
266 }
|
alpar@9
|
267
|
alpar@9
|
268 void glp_mpl_free_wksp(glp_tran *tran)
|
alpar@9
|
269 { /* free the MathProg translator workspace */
|
alpar@9
|
270 mpl_terminate(tran);
|
alpar@9
|
271 return;
|
alpar@9
|
272 }
|
alpar@9
|
273
|
alpar@9
|
274 /* eof */
|