lemon-project-template-glpk

view deps/glpk/src/glplpx03.c @ 9:33de93886c88

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