/farmR/src/glpssx02.c

https://code.google.com/p/javawfm/ · C · 478 lines · 329 code · 10 blank · 139 comment · 92 complexity · 63a2d43f36741838bc1a3bfbb33dd991 MD5 · raw file

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