glpapi08.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* glpapi08.c (interior-point method routines) */
  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. #include "glpapi.h"
  24. #include "glpipm.h"
  25. #include "glpnpp.h"
  26. /***********************************************************************
  27. * NAME
  28. *
  29. * glp_interior - solve LP problem with the interior-point method
  30. *
  31. * SYNOPSIS
  32. *
  33. * int glp_interior(glp_prob *P, const glp_iptcp *parm);
  34. *
  35. * The routine glp_interior is a driver to the LP solver based on the
  36. * interior-point method.
  37. *
  38. * The interior-point solver has a set of control parameters. Values of
  39. * the control parameters can be passed in a structure glp_iptcp, which
  40. * the parameter parm points to.
  41. *
  42. * Currently this routine implements an easy variant of the primal-dual
  43. * interior-point method based on Mehrotra's technique.
  44. *
  45. * This routine transforms the original LP problem to an equivalent LP
  46. * problem in the standard formulation (all constraints are equalities,
  47. * all variables are non-negative), calls the routine ipm_main to solve
  48. * the transformed problem, and then transforms an obtained solution to
  49. * the solution of the original problem.
  50. *
  51. * RETURNS
  52. *
  53. * 0 The LP problem instance has been successfully solved. This code
  54. * does not necessarily mean that the solver has found optimal
  55. * solution. It only means that the solution process was successful.
  56. *
  57. * GLP_EFAIL
  58. * The problem has no rows/columns.
  59. *
  60. * GLP_ENOCVG
  61. * Very slow convergence or divergence.
  62. *
  63. * GLP_EITLIM
  64. * Iteration limit exceeded.
  65. *
  66. * GLP_EINSTAB
  67. * Numerical instability on solving Newtonian system. */
  68. static void transform(NPP *npp)
  69. { /* transform LP to the standard formulation */
  70. NPPROW *row, *prev_row;
  71. NPPCOL *col, *prev_col;
  72. for (row = npp->r_tail; row != NULL; row = prev_row)
  73. { prev_row = row->prev;
  74. if (row->lb == -DBL_MAX && row->ub == +DBL_MAX)
  75. npp_free_row(npp, row);
  76. else if (row->lb == -DBL_MAX)
  77. npp_leq_row(npp, row);
  78. else if (row->ub == +DBL_MAX)
  79. npp_geq_row(npp, row);
  80. else if (row->lb != row->ub)
  81. { if (fabs(row->lb) < fabs(row->ub))
  82. npp_geq_row(npp, row);
  83. else
  84. npp_leq_row(npp, row);
  85. }
  86. }
  87. for (col = npp->c_tail; col != NULL; col = prev_col)
  88. { prev_col = col->prev;
  89. if (col->lb == -DBL_MAX && col->ub == +DBL_MAX)
  90. npp_free_col(npp, col);
  91. else if (col->lb == -DBL_MAX)
  92. npp_ubnd_col(npp, col);
  93. else if (col->ub == +DBL_MAX)
  94. { if (col->lb != 0.0)
  95. npp_lbnd_col(npp, col);
  96. }
  97. else if (col->lb != col->ub)
  98. { if (fabs(col->lb) < fabs(col->ub))
  99. { if (col->lb != 0.0)
  100. npp_lbnd_col(npp, col);
  101. }
  102. else
  103. npp_ubnd_col(npp, col);
  104. npp_dbnd_col(npp, col);
  105. }
  106. else
  107. npp_fixed_col(npp, col);
  108. }
  109. for (row = npp->r_head; row != NULL; row = row->next)
  110. xassert(row->lb == row->ub);
  111. for (col = npp->c_head; col != NULL; col = col->next)
  112. xassert(col->lb == 0.0 && col->ub == +DBL_MAX);
  113. return;
  114. }
  115. int glp_interior(glp_prob *P, const glp_iptcp *parm)
  116. { glp_iptcp _parm;
  117. GLPROW *row;
  118. GLPCOL *col;
  119. NPP *npp = NULL;
  120. glp_prob *prob = NULL;
  121. int i, j, ret;
  122. /* check control parameters */
  123. if (parm == NULL)
  124. glp_init_iptcp(&_parm), parm = &_parm;
  125. if (!(parm->msg_lev == GLP_MSG_OFF ||
  126. parm->msg_lev == GLP_MSG_ERR ||
  127. parm->msg_lev == GLP_MSG_ON ||
  128. parm->msg_lev == GLP_MSG_ALL))
  129. xerror("glp_interior: msg_lev = %d; invalid parameter\n",
  130. parm->msg_lev);
  131. if (!(parm->ord_alg == GLP_ORD_NONE ||
  132. parm->ord_alg == GLP_ORD_QMD ||
  133. parm->ord_alg == GLP_ORD_AMD ||
  134. parm->ord_alg == GLP_ORD_SYMAMD))
  135. xerror("glp_interior: ord_alg = %d; invalid parameter\n",
  136. parm->ord_alg);
  137. /* interior-point solution is currently undefined */
  138. P->ipt_stat = GLP_UNDEF;
  139. P->ipt_obj = 0.0;
  140. /* check bounds of double-bounded variables */
  141. for (i = 1; i <= P->m; i++)
  142. { row = P->row[i];
  143. if (row->type == GLP_DB && row->lb >= row->ub)
  144. { if (parm->msg_lev >= GLP_MSG_ERR)
  145. xprintf("glp_interior: row %d: lb = %g, ub = %g; incorre"
  146. "ct bounds\n", i, row->lb, row->ub);
  147. ret = GLP_EBOUND;
  148. goto done;
  149. }
  150. }
  151. for (j = 1; j <= P->n; j++)
  152. { col = P->col[j];
  153. if (col->type == GLP_DB && col->lb >= col->ub)
  154. { if (parm->msg_lev >= GLP_MSG_ERR)
  155. xprintf("glp_interior: column %d: lb = %g, ub = %g; inco"
  156. "rrect bounds\n", j, col->lb, col->ub);
  157. ret = GLP_EBOUND;
  158. goto done;
  159. }
  160. }
  161. /* transform LP to the standard formulation */
  162. if (parm->msg_lev >= GLP_MSG_ALL)
  163. xprintf("Original LP has %d row(s), %d column(s), and %d non-z"
  164. "ero(s)\n", P->m, P->n, P->nnz);
  165. npp = npp_create_wksp();
  166. npp_load_prob(npp, P, GLP_OFF, GLP_IPT, GLP_ON);
  167. transform(npp);
  168. prob = glp_create_prob();
  169. npp_build_prob(npp, prob);
  170. if (parm->msg_lev >= GLP_MSG_ALL)
  171. xprintf("Working LP has %d row(s), %d column(s), and %d non-ze"
  172. "ro(s)\n", prob->m, prob->n, prob->nnz);
  173. #if 1
  174. /* currently empty problem cannot be solved */
  175. if (!(prob->m > 0 && prob->n > 0))
  176. { if (parm->msg_lev >= GLP_MSG_ERR)
  177. xprintf("glp_interior: unable to solve empty problem\n");
  178. ret = GLP_EFAIL;
  179. goto done;
  180. }
  181. #endif
  182. /* scale the resultant LP */
  183. { ENV *env = get_env_ptr();
  184. int term_out = env->term_out;
  185. env->term_out = GLP_OFF;
  186. glp_scale_prob(prob, GLP_SF_EQ);
  187. env->term_out = term_out;
  188. }
  189. /* warn about dense columns */
  190. if (parm->msg_lev >= GLP_MSG_ON && prob->m >= 200)
  191. { int len, cnt = 0;
  192. for (j = 1; j <= prob->n; j++)
  193. { len = glp_get_mat_col(prob, j, NULL, NULL);
  194. if ((double)len >= 0.20 * (double)prob->m) cnt++;
  195. }
  196. if (cnt == 1)
  197. xprintf("WARNING: PROBLEM HAS ONE DENSE COLUMN\n");
  198. else if (cnt > 0)
  199. xprintf("WARNING: PROBLEM HAS %d DENSE COLUMNS\n", cnt);
  200. }
  201. /* solve the transformed LP */
  202. ret = ipm_solve(prob, parm);
  203. /* postprocess solution from the transformed LP */
  204. npp_postprocess(npp, prob);
  205. /* and store solution to the original LP */
  206. npp_unload_sol(npp, P);
  207. done: /* free working program objects */
  208. if (npp != NULL) npp_delete_wksp(npp);
  209. if (prob != NULL) glp_delete_prob(prob);
  210. /* return to the application program */
  211. return ret;
  212. }
  213. /***********************************************************************
  214. * NAME
  215. *
  216. * glp_init_iptcp - initialize interior-point solver control parameters
  217. *
  218. * SYNOPSIS
  219. *
  220. * void glp_init_iptcp(glp_iptcp *parm);
  221. *
  222. * DESCRIPTION
  223. *
  224. * The routine glp_init_iptcp initializes control parameters, which are
  225. * used by the interior-point solver, with default values.
  226. *
  227. * Default values of the control parameters are stored in the glp_iptcp
  228. * structure, which the parameter parm points to. */
  229. void glp_init_iptcp(glp_iptcp *parm)
  230. { parm->msg_lev = GLP_MSG_ALL;
  231. parm->ord_alg = GLP_ORD_AMD;
  232. return;
  233. }
  234. /***********************************************************************
  235. * NAME
  236. *
  237. * glp_ipt_status - retrieve status of interior-point solution
  238. *
  239. * SYNOPSIS
  240. *
  241. * int glp_ipt_status(glp_prob *lp);
  242. *
  243. * RETURNS
  244. *
  245. * The routine glp_ipt_status reports the status of solution found by
  246. * the interior-point solver as follows:
  247. *
  248. * GLP_UNDEF - interior-point solution is undefined;
  249. * GLP_OPT - interior-point solution is optimal;
  250. * GLP_INFEAS - interior-point solution is infeasible;
  251. * GLP_NOFEAS - no feasible solution exists. */
  252. int glp_ipt_status(glp_prob *lp)
  253. { int ipt_stat = lp->ipt_stat;
  254. return ipt_stat;
  255. }
  256. /***********************************************************************
  257. * NAME
  258. *
  259. * glp_ipt_obj_val - retrieve objective value (interior point)
  260. *
  261. * SYNOPSIS
  262. *
  263. * double glp_ipt_obj_val(glp_prob *lp);
  264. *
  265. * RETURNS
  266. *
  267. * The routine glp_ipt_obj_val returns value of the objective function
  268. * for interior-point solution. */
  269. double glp_ipt_obj_val(glp_prob *lp)
  270. { /*struct LPXCPS *cps = lp->cps;*/
  271. double z;
  272. z = lp->ipt_obj;
  273. /*if (cps->round && fabs(z) < 1e-9) z = 0.0;*/
  274. return z;
  275. }
  276. /***********************************************************************
  277. * NAME
  278. *
  279. * glp_ipt_row_prim - retrieve row primal value (interior point)
  280. *
  281. * SYNOPSIS
  282. *
  283. * double glp_ipt_row_prim(glp_prob *lp, int i);
  284. *
  285. * RETURNS
  286. *
  287. * The routine glp_ipt_row_prim returns primal value of the auxiliary
  288. * variable associated with i-th row. */
  289. double glp_ipt_row_prim(glp_prob *lp, int i)
  290. { /*struct LPXCPS *cps = lp->cps;*/
  291. double pval;
  292. if (!(1 <= i && i <= lp->m))
  293. xerror("glp_ipt_row_prim: i = %d; row number out of range\n",
  294. i);
  295. pval = lp->row[i]->pval;
  296. /*if (cps->round && fabs(pval) < 1e-9) pval = 0.0;*/
  297. return pval;
  298. }
  299. /***********************************************************************
  300. * NAME
  301. *
  302. * glp_ipt_row_dual - retrieve row dual value (interior point)
  303. *
  304. * SYNOPSIS
  305. *
  306. * double glp_ipt_row_dual(glp_prob *lp, int i);
  307. *
  308. * RETURNS
  309. *
  310. * The routine glp_ipt_row_dual returns dual value (i.e. reduced cost)
  311. * of the auxiliary variable associated with i-th row. */
  312. double glp_ipt_row_dual(glp_prob *lp, int i)
  313. { /*struct LPXCPS *cps = lp->cps;*/
  314. double dval;
  315. if (!(1 <= i && i <= lp->m))
  316. xerror("glp_ipt_row_dual: i = %d; row number out of range\n",
  317. i);
  318. dval = lp->row[i]->dval;
  319. /*if (cps->round && fabs(dval) < 1e-9) dval = 0.0;*/
  320. return dval;
  321. }
  322. /***********************************************************************
  323. * NAME
  324. *
  325. * glp_ipt_col_prim - retrieve column primal value (interior point)
  326. *
  327. * SYNOPSIS
  328. *
  329. * double glp_ipt_col_prim(glp_prob *lp, int j);
  330. *
  331. * RETURNS
  332. *
  333. * The routine glp_ipt_col_prim returns primal value of the structural
  334. * variable associated with j-th column. */
  335. double glp_ipt_col_prim(glp_prob *lp, int j)
  336. { /*struct LPXCPS *cps = lp->cps;*/
  337. double pval;
  338. if (!(1 <= j && j <= lp->n))
  339. xerror("glp_ipt_col_prim: j = %d; column number out of range\n"
  340. , j);
  341. pval = lp->col[j]->pval;
  342. /*if (cps->round && fabs(pval) < 1e-9) pval = 0.0;*/
  343. return pval;
  344. }
  345. /***********************************************************************
  346. * NAME
  347. *
  348. * glp_ipt_col_dual - retrieve column dual value (interior point)
  349. *
  350. * SYNOPSIS
  351. *
  352. * #include "glplpx.h"
  353. * double glp_ipt_col_dual(glp_prob *lp, int j);
  354. *
  355. * RETURNS
  356. *
  357. * The routine glp_ipt_col_dual returns dual value (i.e. reduced cost)
  358. * of the structural variable associated with j-th column. */
  359. double glp_ipt_col_dual(glp_prob *lp, int j)
  360. { /*struct LPXCPS *cps = lp->cps;*/
  361. double dval;
  362. if (!(1 <= j && j <= lp->n))
  363. xerror("glp_ipt_col_dual: j = %d; column number out of range\n"
  364. , j);
  365. dval = lp->col[j]->dval;
  366. /*if (cps->round && fabs(dval) < 1e-9) dval = 0.0;*/
  367. return dval;
  368. }
  369. /* eof */