glplpx03.c 8.7 KB

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