glpssx02.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /* glpssx02.c */
  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 "glpenv.h"
  24. #include "glpssx.h"
  25. static void show_progress(SSX *ssx, int phase)
  26. { /* this auxiliary routine displays information about progress of
  27. the search */
  28. int i, def = 0;
  29. for (i = 1; i <= ssx->m; i++)
  30. if (ssx->type[ssx->Q_col[i]] == SSX_FX) def++;
  31. xprintf("%s%6d: %s = %22.15g (%d)\n", phase == 1 ? " " : "*",
  32. ssx->it_cnt, phase == 1 ? "infsum" : "objval",
  33. mpq_get_d(ssx->bbar[0]), def);
  34. #if 0
  35. ssx->tm_lag = utime();
  36. #else
  37. ssx->tm_lag = xtime();
  38. #endif
  39. return;
  40. }
  41. /*----------------------------------------------------------------------
  42. // ssx_phase_I - find primal feasible solution.
  43. //
  44. // This routine implements phase I of the primal simplex method.
  45. //
  46. // On exit the routine returns one of the following codes:
  47. //
  48. // 0 - feasible solution found;
  49. // 1 - problem has no feasible solution;
  50. // 2 - iterations limit exceeded;
  51. // 3 - time limit exceeded.
  52. ----------------------------------------------------------------------*/
  53. int ssx_phase_I(SSX *ssx)
  54. { int m = ssx->m;
  55. int n = ssx->n;
  56. int *type = ssx->type;
  57. mpq_t *lb = ssx->lb;
  58. mpq_t *ub = ssx->ub;
  59. mpq_t *coef = ssx->coef;
  60. int *A_ptr = ssx->A_ptr;
  61. int *A_ind = ssx->A_ind;
  62. mpq_t *A_val = ssx->A_val;
  63. int *Q_col = ssx->Q_col;
  64. mpq_t *bbar = ssx->bbar;
  65. mpq_t *pi = ssx->pi;
  66. mpq_t *cbar = ssx->cbar;
  67. int *orig_type, orig_dir;
  68. mpq_t *orig_lb, *orig_ub, *orig_coef;
  69. int i, k, ret;
  70. /* save components of the original LP problem, which are changed
  71. by the routine */
  72. orig_type = xcalloc(1+m+n, sizeof(int));
  73. orig_lb = xcalloc(1+m+n, sizeof(mpq_t));
  74. orig_ub = xcalloc(1+m+n, sizeof(mpq_t));
  75. orig_coef = xcalloc(1+m+n, sizeof(mpq_t));
  76. for (k = 1; k <= m+n; k++)
  77. { orig_type[k] = type[k];
  78. mpq_init(orig_lb[k]);
  79. mpq_set(orig_lb[k], lb[k]);
  80. mpq_init(orig_ub[k]);
  81. mpq_set(orig_ub[k], ub[k]);
  82. }
  83. orig_dir = ssx->dir;
  84. for (k = 0; k <= m+n; k++)
  85. { mpq_init(orig_coef[k]);
  86. mpq_set(orig_coef[k], coef[k]);
  87. }
  88. /* build an artificial basic solution, which is primal feasible,
  89. and also build an auxiliary objective function to minimize the
  90. sum of infeasibilities for the original problem */
  91. ssx->dir = SSX_MIN;
  92. for (k = 0; k <= m+n; k++) mpq_set_si(coef[k], 0, 1);
  93. mpq_set_si(bbar[0], 0, 1);
  94. for (i = 1; i <= m; i++)
  95. { int t;
  96. k = Q_col[i]; /* x[k] = xB[i] */
  97. t = type[k];
  98. if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
  99. { /* in the original problem x[k] has lower bound */
  100. if (mpq_cmp(bbar[i], lb[k]) < 0)
  101. { /* which is violated */
  102. type[k] = SSX_UP;
  103. mpq_set(ub[k], lb[k]);
  104. mpq_set_si(lb[k], 0, 1);
  105. mpq_set_si(coef[k], -1, 1);
  106. mpq_add(bbar[0], bbar[0], ub[k]);
  107. mpq_sub(bbar[0], bbar[0], bbar[i]);
  108. }
  109. }
  110. if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
  111. { /* in the original problem x[k] has upper bound */
  112. if (mpq_cmp(bbar[i], ub[k]) > 0)
  113. { /* which is violated */
  114. type[k] = SSX_LO;
  115. mpq_set(lb[k], ub[k]);
  116. mpq_set_si(ub[k], 0, 1);
  117. mpq_set_si(coef[k], +1, 1);
  118. mpq_add(bbar[0], bbar[0], bbar[i]);
  119. mpq_sub(bbar[0], bbar[0], lb[k]);
  120. }
  121. }
  122. }
  123. /* now the initial basic solution should be primal feasible due
  124. to changes of bounds of some basic variables, which turned to
  125. implicit artifical variables */
  126. /* compute simplex multipliers and reduced costs */
  127. ssx_eval_pi(ssx);
  128. ssx_eval_cbar(ssx);
  129. /* display initial progress of the search */
  130. show_progress(ssx, 1);
  131. /* main loop starts here */
  132. for (;;)
  133. { /* display current progress of the search */
  134. #if 0
  135. if (utime() - ssx->tm_lag >= ssx->out_frq - 0.001)
  136. #else
  137. if (xdifftime(xtime(), ssx->tm_lag) >= ssx->out_frq - 0.001)
  138. #endif
  139. show_progress(ssx, 1);
  140. /* we do not need to wait until all artificial variables have
  141. left the basis */
  142. if (mpq_sgn(bbar[0]) == 0)
  143. { /* the sum of infeasibilities is zero, therefore the current
  144. solution is primal feasible for the original problem */
  145. ret = 0;
  146. break;
  147. }
  148. /* check if the iterations limit has been exhausted */
  149. if (ssx->it_lim == 0)
  150. { ret = 2;
  151. break;
  152. }
  153. /* check if the time limit has been exhausted */
  154. #if 0
  155. if (ssx->tm_lim >= 0.0 && ssx->tm_lim <= utime() - ssx->tm_beg)
  156. #else
  157. if (ssx->tm_lim >= 0.0 &&
  158. ssx->tm_lim <= xdifftime(xtime(), ssx->tm_beg))
  159. #endif
  160. { ret = 3;
  161. break;
  162. }
  163. /* choose non-basic variable xN[q] */
  164. ssx_chuzc(ssx);
  165. /* if xN[q] cannot be chosen, the sum of infeasibilities is
  166. minimal but non-zero; therefore the original problem has no
  167. primal feasible solution */
  168. if (ssx->q == 0)
  169. { ret = 1;
  170. break;
  171. }
  172. /* compute q-th column of the simplex table */
  173. ssx_eval_col(ssx);
  174. /* choose basic variable xB[p] */
  175. ssx_chuzr(ssx);
  176. /* the sum of infeasibilities cannot be negative, therefore
  177. the auxiliary lp problem cannot have unbounded solution */
  178. xassert(ssx->p != 0);
  179. /* update values of basic variables */
  180. ssx_update_bbar(ssx);
  181. if (ssx->p > 0)
  182. { /* compute p-th row of the inverse inv(B) */
  183. ssx_eval_rho(ssx);
  184. /* compute p-th row of the simplex table */
  185. ssx_eval_row(ssx);
  186. xassert(mpq_cmp(ssx->aq[ssx->p], ssx->ap[ssx->q]) == 0);
  187. /* update simplex multipliers */
  188. ssx_update_pi(ssx);
  189. /* update reduced costs of non-basic variables */
  190. ssx_update_cbar(ssx);
  191. }
  192. /* xB[p] is leaving the basis; if it is implicit artificial
  193. variable, the corresponding residual vanishes; therefore
  194. bounds of this variable should be restored to the original
  195. values */
  196. if (ssx->p > 0)
  197. { k = Q_col[ssx->p]; /* x[k] = xB[p] */
  198. if (type[k] != orig_type[k])
  199. { /* x[k] is implicit artificial variable */
  200. type[k] = orig_type[k];
  201. mpq_set(lb[k], orig_lb[k]);
  202. mpq_set(ub[k], orig_ub[k]);
  203. xassert(ssx->p_stat == SSX_NL || ssx->p_stat == SSX_NU);
  204. ssx->p_stat = (ssx->p_stat == SSX_NL ? SSX_NU : SSX_NL);
  205. if (type[k] == SSX_FX) ssx->p_stat = SSX_NS;
  206. /* nullify the objective coefficient at x[k] */
  207. mpq_set_si(coef[k], 0, 1);
  208. /* since coef[k] has been changed, we need to compute
  209. new reduced cost of x[k], which it will have in the
  210. adjacent basis */
  211. /* the formula d[j] = cN[j] - pi' * N[j] is used (note
  212. that the vector pi is not changed, because it depends
  213. on objective coefficients at basic variables, but in
  214. the adjacent basis, for which the vector pi has been
  215. just recomputed, x[k] is non-basic) */
  216. if (k <= m)
  217. { /* x[k] is auxiliary variable */
  218. mpq_neg(cbar[ssx->q], pi[k]);
  219. }
  220. else
  221. { /* x[k] is structural variable */
  222. int ptr;
  223. mpq_t temp;
  224. mpq_init(temp);
  225. mpq_set_si(cbar[ssx->q], 0, 1);
  226. for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
  227. { mpq_mul(temp, pi[A_ind[ptr]], A_val[ptr]);
  228. mpq_add(cbar[ssx->q], cbar[ssx->q], temp);
  229. }
  230. mpq_clear(temp);
  231. }
  232. }
  233. }
  234. /* jump to the adjacent vertex of the polyhedron */
  235. ssx_change_basis(ssx);
  236. /* one simplex iteration has been performed */
  237. if (ssx->it_lim > 0) ssx->it_lim--;
  238. ssx->it_cnt++;
  239. }
  240. /* display final progress of the search */
  241. show_progress(ssx, 1);
  242. /* restore components of the original problem, which were changed
  243. by the routine */
  244. for (k = 1; k <= m+n; k++)
  245. { type[k] = orig_type[k];
  246. mpq_set(lb[k], orig_lb[k]);
  247. mpq_clear(orig_lb[k]);
  248. mpq_set(ub[k], orig_ub[k]);
  249. mpq_clear(orig_ub[k]);
  250. }
  251. ssx->dir = orig_dir;
  252. for (k = 0; k <= m+n; k++)
  253. { mpq_set(coef[k], orig_coef[k]);
  254. mpq_clear(orig_coef[k]);
  255. }
  256. xfree(orig_type);
  257. xfree(orig_lb);
  258. xfree(orig_ub);
  259. xfree(orig_coef);
  260. /* return to the calling program */
  261. return ret;
  262. }
  263. /*----------------------------------------------------------------------
  264. // ssx_phase_II - find optimal solution.
  265. //
  266. // This routine implements phase II of the primal simplex method.
  267. //
  268. // On exit the routine returns one of the following codes:
  269. //
  270. // 0 - optimal solution found;
  271. // 1 - problem has unbounded solution;
  272. // 2 - iterations limit exceeded;
  273. // 3 - time limit exceeded.
  274. ----------------------------------------------------------------------*/
  275. int ssx_phase_II(SSX *ssx)
  276. { int ret;
  277. /* display initial progress of the search */
  278. show_progress(ssx, 2);
  279. /* main loop starts here */
  280. for (;;)
  281. { /* display current progress of the search */
  282. #if 0
  283. if (utime() - ssx->tm_lag >= ssx->out_frq - 0.001)
  284. #else
  285. if (xdifftime(xtime(), ssx->tm_lag) >= ssx->out_frq - 0.001)
  286. #endif
  287. show_progress(ssx, 2);
  288. /* check if the iterations limit has been exhausted */
  289. if (ssx->it_lim == 0)
  290. { ret = 2;
  291. break;
  292. }
  293. /* check if the time limit has been exhausted */
  294. #if 0
  295. if (ssx->tm_lim >= 0.0 && ssx->tm_lim <= utime() - ssx->tm_beg)
  296. #else
  297. if (ssx->tm_lim >= 0.0 &&
  298. ssx->tm_lim <= xdifftime(xtime(), ssx->tm_beg))
  299. #endif
  300. { ret = 3;
  301. break;
  302. }
  303. /* choose non-basic variable xN[q] */
  304. ssx_chuzc(ssx);
  305. /* if xN[q] cannot be chosen, the current basic solution is
  306. dual feasible and therefore optimal */
  307. if (ssx->q == 0)
  308. { ret = 0;
  309. break;
  310. }
  311. /* compute q-th column of the simplex table */
  312. ssx_eval_col(ssx);
  313. /* choose basic variable xB[p] */
  314. ssx_chuzr(ssx);
  315. /* if xB[p] cannot be chosen, the problem has no dual feasible
  316. solution (i.e. unbounded) */
  317. if (ssx->p == 0)
  318. { ret = 1;
  319. break;
  320. }
  321. /* update values of basic variables */
  322. ssx_update_bbar(ssx);
  323. if (ssx->p > 0)
  324. { /* compute p-th row of the inverse inv(B) */
  325. ssx_eval_rho(ssx);
  326. /* compute p-th row of the simplex table */
  327. ssx_eval_row(ssx);
  328. xassert(mpq_cmp(ssx->aq[ssx->p], ssx->ap[ssx->q]) == 0);
  329. #if 0
  330. /* update simplex multipliers */
  331. ssx_update_pi(ssx);
  332. #endif
  333. /* update reduced costs of non-basic variables */
  334. ssx_update_cbar(ssx);
  335. }
  336. /* jump to the adjacent vertex of the polyhedron */
  337. ssx_change_basis(ssx);
  338. /* one simplex iteration has been performed */
  339. if (ssx->it_lim > 0) ssx->it_lim--;
  340. ssx->it_cnt++;
  341. }
  342. /* display final progress of the search */
  343. show_progress(ssx, 2);
  344. /* return to the calling program */
  345. return ret;
  346. }
  347. /*----------------------------------------------------------------------
  348. // ssx_driver - base driver to exact simplex method.
  349. //
  350. // This routine is a base driver to a version of the primal simplex
  351. // method using exact (bignum) arithmetic.
  352. //
  353. // On exit the routine returns one of the following codes:
  354. //
  355. // 0 - optimal solution found;
  356. // 1 - problem has no feasible solution;
  357. // 2 - problem has unbounded solution;
  358. // 3 - iterations limit exceeded (phase I);
  359. // 4 - iterations limit exceeded (phase II);
  360. // 5 - time limit exceeded (phase I);
  361. // 6 - time limit exceeded (phase II);
  362. // 7 - initial basis matrix is exactly singular.
  363. ----------------------------------------------------------------------*/
  364. int ssx_driver(SSX *ssx)
  365. { int m = ssx->m;
  366. int *type = ssx->type;
  367. mpq_t *lb = ssx->lb;
  368. mpq_t *ub = ssx->ub;
  369. int *Q_col = ssx->Q_col;
  370. mpq_t *bbar = ssx->bbar;
  371. int i, k, ret;
  372. ssx->tm_beg = xtime();
  373. /* factorize the initial basis matrix */
  374. if (ssx_factorize(ssx))
  375. { xprintf("Initial basis matrix is singular\n");
  376. ret = 7;
  377. goto done;
  378. }
  379. /* compute values of basic variables */
  380. ssx_eval_bbar(ssx);
  381. /* check if the initial basic solution is primal feasible */
  382. for (i = 1; i <= m; i++)
  383. { int t;
  384. k = Q_col[i]; /* x[k] = xB[i] */
  385. t = type[k];
  386. if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
  387. { /* x[k] has lower bound */
  388. if (mpq_cmp(bbar[i], lb[k]) < 0)
  389. { /* which is violated */
  390. break;
  391. }
  392. }
  393. if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
  394. { /* x[k] has upper bound */
  395. if (mpq_cmp(bbar[i], ub[k]) > 0)
  396. { /* which is violated */
  397. break;
  398. }
  399. }
  400. }
  401. if (i > m)
  402. { /* no basic variable violates its bounds */
  403. ret = 0;
  404. goto skip;
  405. }
  406. /* phase I: find primal feasible solution */
  407. ret = ssx_phase_I(ssx);
  408. switch (ret)
  409. { case 0:
  410. ret = 0;
  411. break;
  412. case 1:
  413. xprintf("PROBLEM HAS NO FEASIBLE SOLUTION\n");
  414. ret = 1;
  415. break;
  416. case 2:
  417. xprintf("ITERATIONS LIMIT EXCEEDED; SEARCH TERMINATED\n");
  418. ret = 3;
  419. break;
  420. case 3:
  421. xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
  422. ret = 5;
  423. break;
  424. default:
  425. xassert(ret != ret);
  426. }
  427. /* compute values of basic variables (actually only the objective
  428. value needs to be computed) */
  429. ssx_eval_bbar(ssx);
  430. skip: /* compute simplex multipliers */
  431. ssx_eval_pi(ssx);
  432. /* compute reduced costs of non-basic variables */
  433. ssx_eval_cbar(ssx);
  434. /* if phase I failed, do not start phase II */
  435. if (ret != 0) goto done;
  436. /* phase II: find optimal solution */
  437. ret = ssx_phase_II(ssx);
  438. switch (ret)
  439. { case 0:
  440. xprintf("OPTIMAL SOLUTION FOUND\n");
  441. ret = 0;
  442. break;
  443. case 1:
  444. xprintf("PROBLEM HAS UNBOUNDED SOLUTION\n");
  445. ret = 2;
  446. break;
  447. case 2:
  448. xprintf("ITERATIONS LIMIT EXCEEDED; SEARCH TERMINATED\n");
  449. ret = 4;
  450. break;
  451. case 3:
  452. xprintf("TIME LIMIT EXCEEDED; SEARCH TERMINATED\n");
  453. ret = 6;
  454. break;
  455. default:
  456. xassert(ret != ret);
  457. }
  458. done: /* decrease the time limit by the spent amount of time */
  459. if (ssx->tm_lim >= 0.0)
  460. #if 0
  461. { ssx->tm_lim -= utime() - ssx->tm_beg;
  462. #else
  463. { ssx->tm_lim -= xdifftime(xtime(), ssx->tm_beg);
  464. #endif
  465. if (ssx->tm_lim < 0.0) ssx->tm_lim = 0.0;
  466. }
  467. return ret;
  468. }
  469. /* eof */