glpapi14.c 9.1 KB

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