rev |
line source |
alpar@9
|
1 /* glplpx03.c (OPB format) */
|
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 * Author: Oscar Gustafsson <oscarg@isy.liu.se>.
|
alpar@9
|
7 *
|
alpar@9
|
8 * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
|
alpar@9
|
9 * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics,
|
alpar@9
|
10 * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
|
alpar@9
|
11 * E-mail: <mao@gnu.org>.
|
alpar@9
|
12 *
|
alpar@9
|
13 * GLPK is free software: you can redistribute it and/or modify it
|
alpar@9
|
14 * under the terms of the GNU General Public License as published by
|
alpar@9
|
15 * the Free Software Foundation, either version 3 of the License, or
|
alpar@9
|
16 * (at your option) any later version.
|
alpar@9
|
17 *
|
alpar@9
|
18 * GLPK is distributed in the hope that it will be useful, but WITHOUT
|
alpar@9
|
19 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
alpar@9
|
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
|
alpar@9
|
21 * License for more details.
|
alpar@9
|
22 *
|
alpar@9
|
23 * You should have received a copy of the GNU General Public License
|
alpar@9
|
24 * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
|
alpar@9
|
25 ***********************************************************************/
|
alpar@9
|
26
|
alpar@9
|
27 #define _GLPSTD_ERRNO
|
alpar@9
|
28 #define _GLPSTD_STDIO
|
alpar@9
|
29 #include "glpapi.h"
|
alpar@9
|
30 #if 0 /* 24/XII-2009; by mao */
|
alpar@9
|
31 #include "glpipp.h"
|
alpar@9
|
32 #endif
|
alpar@9
|
33
|
alpar@9
|
34 /*----------------------------------------------------------------------
|
alpar@9
|
35 -- lpx_write_pb - write problem data in (normalized) OPB format.
|
alpar@9
|
36 --
|
alpar@9
|
37 -- *Synopsis*
|
alpar@9
|
38 --
|
alpar@9
|
39 -- #include "glplpx.h"
|
alpar@9
|
40 -- int lpx_write_pb(LPX *lp, const char *fname, int normalized,
|
alpar@9
|
41 -- int binarize);
|
alpar@9
|
42 --
|
alpar@9
|
43 -- *Description*
|
alpar@9
|
44 --
|
alpar@9
|
45 -- The routine lpx_write_pb writes problem data in OPB format
|
alpar@9
|
46 -- to an output text file whose name is the character string fname.
|
alpar@9
|
47 -- If normalized is non-zero the output will be generated in a
|
alpar@9
|
48 -- normalized form with sequentially numbered variables, x1, x2 etc.
|
alpar@9
|
49 -- If binarize, any integer variable will be repalzec by binary ones,
|
alpar@9
|
50 -- see ipp_binarize
|
alpar@9
|
51 --
|
alpar@9
|
52 -- *Returns*
|
alpar@9
|
53 --
|
alpar@9
|
54 -- If the operation was successful, the routine returns zero. Otherwise
|
alpar@9
|
55 -- the routine prints an error message and returns non-zero. */
|
alpar@9
|
56
|
alpar@9
|
57 #if 1 /* 24/XII-2009; by mao (disabled, because IPP was removed) */
|
alpar@9
|
58 int lpx_write_pb(LPX *lp, const char *fname, int normalized,
|
alpar@9
|
59 int binarize)
|
alpar@9
|
60 { xassert(lp == lp);
|
alpar@9
|
61 xassert(fname == fname);
|
alpar@9
|
62 xassert(normalized == normalized);
|
alpar@9
|
63 xassert(binarize == binarize);
|
alpar@9
|
64 xprintf("lpx_write_pb: sorry, currently this operation is not ava"
|
alpar@9
|
65 "ilable\n");
|
alpar@9
|
66 return 1;
|
alpar@9
|
67 }
|
alpar@9
|
68 #else
|
alpar@9
|
69 int lpx_write_pb(LPX *lp, const char *fname, int normalized,
|
alpar@9
|
70 int binarize)
|
alpar@9
|
71 {
|
alpar@9
|
72 FILE* fp;
|
alpar@9
|
73 int m,n,i,j,k,o,nonfree=0, obj_dir, dbl, *ndx, row_type, emptylhs=0;
|
alpar@9
|
74 double coeff, *val, bound, constant/*=0.0*/;
|
alpar@9
|
75 char* objconstname = "dummy_one";
|
alpar@9
|
76 char* emptylhsname = "dummy_zero";
|
alpar@9
|
77
|
alpar@9
|
78 /* Variables needed for possible binarization */
|
alpar@9
|
79 /*LPX* tlp;*/
|
alpar@9
|
80 IPP *ipp = NULL;
|
alpar@9
|
81 /*tlp=lp;*/
|
alpar@9
|
82
|
alpar@9
|
83 if(binarize) /* Transform integer variables to binary ones */
|
alpar@9
|
84 {
|
alpar@9
|
85 ipp = ipp_create_wksp();
|
alpar@9
|
86 ipp_load_orig(ipp, lp);
|
alpar@9
|
87 ipp_binarize(ipp);
|
alpar@9
|
88 lp = ipp_build_prob(ipp);
|
alpar@9
|
89 }
|
alpar@9
|
90 fp = fopen(fname, "w");
|
alpar@9
|
91
|
alpar@9
|
92 if(fp!= NULL)
|
alpar@9
|
93 {
|
alpar@9
|
94 xprintf(
|
alpar@9
|
95 "lpx_write_pb: writing problem in %sOPB format to `%s'...\n",
|
alpar@9
|
96 (normalized?"normalized ":""), fname);
|
alpar@9
|
97
|
alpar@9
|
98 m = glp_get_num_rows(lp);
|
alpar@9
|
99 n = glp_get_num_cols(lp);
|
alpar@9
|
100 for(i=1;i<=m;i++)
|
alpar@9
|
101 {
|
alpar@9
|
102 switch(glp_get_row_type(lp,i))
|
alpar@9
|
103 {
|
alpar@9
|
104 case GLP_LO:
|
alpar@9
|
105 case GLP_UP:
|
alpar@9
|
106 case GLP_FX:
|
alpar@9
|
107 {
|
alpar@9
|
108 nonfree += 1;
|
alpar@9
|
109 break;
|
alpar@9
|
110 }
|
alpar@9
|
111 case GLP_DB:
|
alpar@9
|
112 {
|
alpar@9
|
113 nonfree += 2;
|
alpar@9
|
114 break;
|
alpar@9
|
115 }
|
alpar@9
|
116 }
|
alpar@9
|
117 }
|
alpar@9
|
118 constant=glp_get_obj_coef(lp,0);
|
alpar@9
|
119 fprintf(fp,"* #variables = %d #constraints = %d\n",
|
alpar@9
|
120 n + (constant == 0?1:0), nonfree + (constant == 0?1:0));
|
alpar@9
|
121 /* Objective function */
|
alpar@9
|
122 obj_dir = glp_get_obj_dir(lp);
|
alpar@9
|
123 fprintf(fp,"min: ");
|
alpar@9
|
124 for(i=1;i<=n;i++)
|
alpar@9
|
125 {
|
alpar@9
|
126 coeff = glp_get_obj_coef(lp,i);
|
alpar@9
|
127 if(coeff != 0.0)
|
alpar@9
|
128 {
|
alpar@9
|
129 if(obj_dir == GLP_MAX)
|
alpar@9
|
130 coeff=-coeff;
|
alpar@9
|
131 if(normalized)
|
alpar@9
|
132 fprintf(fp, " %d x%d", (int)coeff, i);
|
alpar@9
|
133 else
|
alpar@9
|
134 fprintf(fp, " %d*%s", (int)coeff,
|
alpar@9
|
135 glp_get_col_name(lp,i));
|
alpar@9
|
136
|
alpar@9
|
137 }
|
alpar@9
|
138 }
|
alpar@9
|
139 if(constant)
|
alpar@9
|
140 {
|
alpar@9
|
141 if(normalized)
|
alpar@9
|
142 fprintf(fp, " %d x%d", (int)constant, n+1);
|
alpar@9
|
143 else
|
alpar@9
|
144 fprintf(fp, " %d*%s", (int)constant, objconstname);
|
alpar@9
|
145 }
|
alpar@9
|
146 fprintf(fp,";\n");
|
alpar@9
|
147
|
alpar@9
|
148 if(normalized && !binarize) /* Name substitution */
|
alpar@9
|
149 {
|
alpar@9
|
150 fprintf(fp,"* Variable name substitution:\n");
|
alpar@9
|
151 for(j=1;j<=n;j++)
|
alpar@9
|
152 {
|
alpar@9
|
153 fprintf(fp, "* x%d = %s\n", j, glp_get_col_name(lp,j));
|
alpar@9
|
154 }
|
alpar@9
|
155 if(constant)
|
alpar@9
|
156 fprintf(fp, "* x%d = %s\n", n+1, objconstname);
|
alpar@9
|
157 }
|
alpar@9
|
158
|
alpar@9
|
159 ndx = xcalloc(1+n, sizeof(int));
|
alpar@9
|
160 val = xcalloc(1+n, sizeof(double));
|
alpar@9
|
161
|
alpar@9
|
162 /* Constraints */
|
alpar@9
|
163 for(j=1;j<=m;j++)
|
alpar@9
|
164 {
|
alpar@9
|
165 row_type=glp_get_row_type(lp,j);
|
alpar@9
|
166 if(row_type!=GLP_FR)
|
alpar@9
|
167 {
|
alpar@9
|
168 if(row_type == GLP_DB)
|
alpar@9
|
169 {
|
alpar@9
|
170 dbl=2;
|
alpar@9
|
171 row_type = GLP_UP;
|
alpar@9
|
172 }
|
alpar@9
|
173 else
|
alpar@9
|
174 {
|
alpar@9
|
175 dbl=1;
|
alpar@9
|
176 }
|
alpar@9
|
177 k=glp_get_mat_row(lp, j, ndx, val);
|
alpar@9
|
178 for(o=1;o<=dbl;o++)
|
alpar@9
|
179 {
|
alpar@9
|
180 if(o==2)
|
alpar@9
|
181 {
|
alpar@9
|
182 row_type = GLP_LO;
|
alpar@9
|
183 }
|
alpar@9
|
184 if(k==0) /* Empty LHS */
|
alpar@9
|
185 {
|
alpar@9
|
186 emptylhs = 1;
|
alpar@9
|
187 if(normalized)
|
alpar@9
|
188 {
|
alpar@9
|
189 fprintf(fp, "0 x%d ", n+2);
|
alpar@9
|
190 }
|
alpar@9
|
191 else
|
alpar@9
|
192 {
|
alpar@9
|
193 fprintf(fp, "0*%s ", emptylhsname);
|
alpar@9
|
194 }
|
alpar@9
|
195 }
|
alpar@9
|
196
|
alpar@9
|
197 for(i=1;i<=k;i++)
|
alpar@9
|
198 {
|
alpar@9
|
199 if(val[i] != 0.0)
|
alpar@9
|
200 {
|
alpar@9
|
201
|
alpar@9
|
202 if(normalized)
|
alpar@9
|
203 {
|
alpar@9
|
204 fprintf(fp, "%d x%d ",
|
alpar@9
|
205 (row_type==GLP_UP)?(-(int)val[i]):((int)val[i]), ndx[i]);
|
alpar@9
|
206 }
|
alpar@9
|
207 else
|
alpar@9
|
208 {
|
alpar@9
|
209 fprintf(fp, "%d*%s ", (int)val[i],
|
alpar@9
|
210 glp_get_col_name(lp,ndx[i]));
|
alpar@9
|
211 }
|
alpar@9
|
212 }
|
alpar@9
|
213 }
|
alpar@9
|
214 switch(row_type)
|
alpar@9
|
215 {
|
alpar@9
|
216 case GLP_LO:
|
alpar@9
|
217 {
|
alpar@9
|
218 fprintf(fp, ">=");
|
alpar@9
|
219 bound = glp_get_row_lb(lp,j);
|
alpar@9
|
220 break;
|
alpar@9
|
221 }
|
alpar@9
|
222 case GLP_UP:
|
alpar@9
|
223 {
|
alpar@9
|
224 if(normalized)
|
alpar@9
|
225 {
|
alpar@9
|
226 fprintf(fp, ">=");
|
alpar@9
|
227 bound = -glp_get_row_ub(lp,j);
|
alpar@9
|
228 }
|
alpar@9
|
229 else
|
alpar@9
|
230 {
|
alpar@9
|
231 fprintf(fp, "<=");
|
alpar@9
|
232 bound = glp_get_row_ub(lp,j);
|
alpar@9
|
233 }
|
alpar@9
|
234
|
alpar@9
|
235 break;
|
alpar@9
|
236 }
|
alpar@9
|
237 case GLP_FX:
|
alpar@9
|
238 {
|
alpar@9
|
239 fprintf(fp, "=");
|
alpar@9
|
240 bound = glp_get_row_lb(lp,j);
|
alpar@9
|
241 break;
|
alpar@9
|
242 }
|
alpar@9
|
243 }
|
alpar@9
|
244 fprintf(fp," %d;\n",(int)bound);
|
alpar@9
|
245 }
|
alpar@9
|
246 }
|
alpar@9
|
247 }
|
alpar@9
|
248 xfree(ndx);
|
alpar@9
|
249 xfree(val);
|
alpar@9
|
250
|
alpar@9
|
251 if(constant)
|
alpar@9
|
252 {
|
alpar@9
|
253 xprintf(
|
alpar@9
|
254 "lpx_write_pb: adding constant objective function variable\n");
|
alpar@9
|
255
|
alpar@9
|
256 if(normalized)
|
alpar@9
|
257 fprintf(fp, "1 x%d = 1;\n", n+1);
|
alpar@9
|
258 else
|
alpar@9
|
259 fprintf(fp, "1*%s = 1;\n", objconstname);
|
alpar@9
|
260 }
|
alpar@9
|
261 if(emptylhs)
|
alpar@9
|
262 {
|
alpar@9
|
263 xprintf(
|
alpar@9
|
264 "lpx_write_pb: adding dummy variable for empty left-hand si"
|
alpar@9
|
265 "de constraint\n");
|
alpar@9
|
266
|
alpar@9
|
267 if(normalized)
|
alpar@9
|
268 fprintf(fp, "1 x%d = 0;\n", n+2);
|
alpar@9
|
269 else
|
alpar@9
|
270 fprintf(fp, "1*%s = 0;\n", emptylhsname);
|
alpar@9
|
271 }
|
alpar@9
|
272
|
alpar@9
|
273 }
|
alpar@9
|
274 else
|
alpar@9
|
275 {
|
alpar@9
|
276 xprintf("Problems opening file for writing: %s\n", fname);
|
alpar@9
|
277 return(1);
|
alpar@9
|
278 }
|
alpar@9
|
279 fflush(fp);
|
alpar@9
|
280 if (ferror(fp))
|
alpar@9
|
281 { xprintf("lpx_write_pb: can't write to `%s' - %s\n", fname,
|
alpar@9
|
282 strerror(errno));
|
alpar@9
|
283 goto fail;
|
alpar@9
|
284 }
|
alpar@9
|
285 fclose(fp);
|
alpar@9
|
286
|
alpar@9
|
287
|
alpar@9
|
288 if(binarize)
|
alpar@9
|
289 {
|
alpar@9
|
290 /* delete the resultant problem object */
|
alpar@9
|
291 if (lp != NULL) lpx_delete_prob(lp);
|
alpar@9
|
292 /* delete MIP presolver workspace */
|
alpar@9
|
293 if (ipp != NULL) ipp_delete_wksp(ipp);
|
alpar@9
|
294 /*lp=tlp;*/
|
alpar@9
|
295 }
|
alpar@9
|
296 return 0;
|
alpar@9
|
297 fail: if (fp != NULL) fclose(fp);
|
alpar@9
|
298 return 1;
|
alpar@9
|
299 }
|
alpar@9
|
300 #endif
|
alpar@9
|
301
|
alpar@9
|
302 /* eof */
|