glpssx01.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  1. /* glpssx01.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. #define xfault xerror
  26. /*----------------------------------------------------------------------
  27. // ssx_create - create simplex solver workspace.
  28. //
  29. // This routine creates the workspace used by simplex solver routines,
  30. // and returns a pointer to it.
  31. //
  32. // Parameters m, n, and nnz specify, respectively, the number of rows,
  33. // columns, and non-zero constraint coefficients.
  34. //
  35. // This routine only allocates the memory for the workspace components,
  36. // so the workspace needs to be saturated by data. */
  37. SSX *ssx_create(int m, int n, int nnz)
  38. { SSX *ssx;
  39. int i, j, k;
  40. if (m < 1)
  41. xfault("ssx_create: m = %d; invalid number of rows\n", m);
  42. if (n < 1)
  43. xfault("ssx_create: n = %d; invalid number of columns\n", n);
  44. if (nnz < 0)
  45. xfault("ssx_create: nnz = %d; invalid number of non-zero const"
  46. "raint coefficients\n", nnz);
  47. ssx = xmalloc(sizeof(SSX));
  48. ssx->m = m;
  49. ssx->n = n;
  50. ssx->type = xcalloc(1+m+n, sizeof(int));
  51. ssx->lb = xcalloc(1+m+n, sizeof(mpq_t));
  52. for (k = 1; k <= m+n; k++) mpq_init(ssx->lb[k]);
  53. ssx->ub = xcalloc(1+m+n, sizeof(mpq_t));
  54. for (k = 1; k <= m+n; k++) mpq_init(ssx->ub[k]);
  55. ssx->coef = xcalloc(1+m+n, sizeof(mpq_t));
  56. for (k = 0; k <= m+n; k++) mpq_init(ssx->coef[k]);
  57. ssx->A_ptr = xcalloc(1+n+1, sizeof(int));
  58. ssx->A_ptr[n+1] = nnz+1;
  59. ssx->A_ind = xcalloc(1+nnz, sizeof(int));
  60. ssx->A_val = xcalloc(1+nnz, sizeof(mpq_t));
  61. for (k = 1; k <= nnz; k++) mpq_init(ssx->A_val[k]);
  62. ssx->stat = xcalloc(1+m+n, sizeof(int));
  63. ssx->Q_row = xcalloc(1+m+n, sizeof(int));
  64. ssx->Q_col = xcalloc(1+m+n, sizeof(int));
  65. ssx->binv = bfx_create_binv();
  66. ssx->bbar = xcalloc(1+m, sizeof(mpq_t));
  67. for (i = 0; i <= m; i++) mpq_init(ssx->bbar[i]);
  68. ssx->pi = xcalloc(1+m, sizeof(mpq_t));
  69. for (i = 1; i <= m; i++) mpq_init(ssx->pi[i]);
  70. ssx->cbar = xcalloc(1+n, sizeof(mpq_t));
  71. for (j = 1; j <= n; j++) mpq_init(ssx->cbar[j]);
  72. ssx->rho = xcalloc(1+m, sizeof(mpq_t));
  73. for (i = 1; i <= m; i++) mpq_init(ssx->rho[i]);
  74. ssx->ap = xcalloc(1+n, sizeof(mpq_t));
  75. for (j = 1; j <= n; j++) mpq_init(ssx->ap[j]);
  76. ssx->aq = xcalloc(1+m, sizeof(mpq_t));
  77. for (i = 1; i <= m; i++) mpq_init(ssx->aq[i]);
  78. mpq_init(ssx->delta);
  79. return ssx;
  80. }
  81. /*----------------------------------------------------------------------
  82. // ssx_factorize - factorize the current basis matrix.
  83. //
  84. // This routine computes factorization of the current basis matrix B
  85. // and returns the singularity flag. If the matrix B is non-singular,
  86. // the flag is zero, otherwise non-zero. */
  87. static int basis_col(void *info, int j, int ind[], mpq_t val[])
  88. { /* this auxiliary routine provides row indices and numeric values
  89. of non-zero elements in j-th column of the matrix B */
  90. SSX *ssx = info;
  91. int m = ssx->m;
  92. int n = ssx->n;
  93. int *A_ptr = ssx->A_ptr;
  94. int *A_ind = ssx->A_ind;
  95. mpq_t *A_val = ssx->A_val;
  96. int *Q_col = ssx->Q_col;
  97. int k, len, ptr;
  98. xassert(1 <= j && j <= m);
  99. k = Q_col[j]; /* x[k] = xB[j] */
  100. xassert(1 <= k && k <= m+n);
  101. /* j-th column of the matrix B is k-th column of the augmented
  102. constraint matrix (I | -A) */
  103. if (k <= m)
  104. { /* it is a column of the unity matrix I */
  105. len = 1, ind[1] = k, mpq_set_si(val[1], 1, 1);
  106. }
  107. else
  108. { /* it is a column of the original constraint matrix -A */
  109. len = 0;
  110. for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
  111. { len++;
  112. ind[len] = A_ind[ptr];
  113. mpq_neg(val[len], A_val[ptr]);
  114. }
  115. }
  116. return len;
  117. }
  118. int ssx_factorize(SSX *ssx)
  119. { int ret;
  120. ret = bfx_factorize(ssx->binv, ssx->m, basis_col, ssx);
  121. return ret;
  122. }
  123. /*----------------------------------------------------------------------
  124. // ssx_get_xNj - determine value of non-basic variable.
  125. //
  126. // This routine determines the value of non-basic variable xN[j] in the
  127. // current basic solution defined as follows:
  128. //
  129. // 0, if xN[j] is free variable
  130. // lN[j], if xN[j] is on its lower bound
  131. // uN[j], if xN[j] is on its upper bound
  132. // lN[j] = uN[j], if xN[j] is fixed variable
  133. //
  134. // where lN[j] and uN[j] are lower and upper bounds of xN[j]. */
  135. void ssx_get_xNj(SSX *ssx, int j, mpq_t x)
  136. { int m = ssx->m;
  137. int n = ssx->n;
  138. mpq_t *lb = ssx->lb;
  139. mpq_t *ub = ssx->ub;
  140. int *stat = ssx->stat;
  141. int *Q_col = ssx->Q_col;
  142. int k;
  143. xassert(1 <= j && j <= n);
  144. k = Q_col[m+j]; /* x[k] = xN[j] */
  145. xassert(1 <= k && k <= m+n);
  146. switch (stat[k])
  147. { case SSX_NL:
  148. /* xN[j] is on its lower bound */
  149. mpq_set(x, lb[k]); break;
  150. case SSX_NU:
  151. /* xN[j] is on its upper bound */
  152. mpq_set(x, ub[k]); break;
  153. case SSX_NF:
  154. /* xN[j] is free variable */
  155. mpq_set_si(x, 0, 1); break;
  156. case SSX_NS:
  157. /* xN[j] is fixed variable */
  158. mpq_set(x, lb[k]); break;
  159. default:
  160. xassert(stat != stat);
  161. }
  162. return;
  163. }
  164. /*----------------------------------------------------------------------
  165. // ssx_eval_bbar - compute values of basic variables.
  166. //
  167. // This routine computes values of basic variables xB in the current
  168. // basic solution as follows:
  169. //
  170. // beta = - inv(B) * N * xN,
  171. //
  172. // where B is the basis matrix, N is the matrix of non-basic columns,
  173. // xN is a vector of current values of non-basic variables. */
  174. void ssx_eval_bbar(SSX *ssx)
  175. { int m = ssx->m;
  176. int n = ssx->n;
  177. mpq_t *coef = ssx->coef;
  178. int *A_ptr = ssx->A_ptr;
  179. int *A_ind = ssx->A_ind;
  180. mpq_t *A_val = ssx->A_val;
  181. int *Q_col = ssx->Q_col;
  182. mpq_t *bbar = ssx->bbar;
  183. int i, j, k, ptr;
  184. mpq_t x, temp;
  185. mpq_init(x);
  186. mpq_init(temp);
  187. /* bbar := 0 */
  188. for (i = 1; i <= m; i++)
  189. mpq_set_si(bbar[i], 0, 1);
  190. /* bbar := - N * xN = - N[1] * xN[1] - ... - N[n] * xN[n] */
  191. for (j = 1; j <= n; j++)
  192. { ssx_get_xNj(ssx, j, x);
  193. if (mpq_sgn(x) == 0) continue;
  194. k = Q_col[m+j]; /* x[k] = xN[j] */
  195. if (k <= m)
  196. { /* N[j] is a column of the unity matrix I */
  197. mpq_sub(bbar[k], bbar[k], x);
  198. }
  199. else
  200. { /* N[j] is a column of the original constraint matrix -A */
  201. for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
  202. { mpq_mul(temp, A_val[ptr], x);
  203. mpq_add(bbar[A_ind[ptr]], bbar[A_ind[ptr]], temp);
  204. }
  205. }
  206. }
  207. /* bbar := inv(B) * bbar */
  208. bfx_ftran(ssx->binv, bbar, 0);
  209. #if 1
  210. /* compute value of the objective function */
  211. /* bbar[0] := c[0] */
  212. mpq_set(bbar[0], coef[0]);
  213. /* bbar[0] := bbar[0] + sum{i in B} cB[i] * xB[i] */
  214. for (i = 1; i <= m; i++)
  215. { k = Q_col[i]; /* x[k] = xB[i] */
  216. if (mpq_sgn(coef[k]) == 0) continue;
  217. mpq_mul(temp, coef[k], bbar[i]);
  218. mpq_add(bbar[0], bbar[0], temp);
  219. }
  220. /* bbar[0] := bbar[0] + sum{j in N} cN[j] * xN[j] */
  221. for (j = 1; j <= n; j++)
  222. { k = Q_col[m+j]; /* x[k] = xN[j] */
  223. if (mpq_sgn(coef[k]) == 0) continue;
  224. ssx_get_xNj(ssx, j, x);
  225. mpq_mul(temp, coef[k], x);
  226. mpq_add(bbar[0], bbar[0], temp);
  227. }
  228. #endif
  229. mpq_clear(x);
  230. mpq_clear(temp);
  231. return;
  232. }
  233. /*----------------------------------------------------------------------
  234. // ssx_eval_pi - compute values of simplex multipliers.
  235. //
  236. // This routine computes values of simplex multipliers (shadow prices)
  237. // pi in the current basic solution as follows:
  238. //
  239. // pi = inv(B') * cB,
  240. //
  241. // where B' is a matrix transposed to the basis matrix B, cB is a vector
  242. // of objective coefficients at basic variables xB. */
  243. void ssx_eval_pi(SSX *ssx)
  244. { int m = ssx->m;
  245. mpq_t *coef = ssx->coef;
  246. int *Q_col = ssx->Q_col;
  247. mpq_t *pi = ssx->pi;
  248. int i;
  249. /* pi := cB */
  250. for (i = 1; i <= m; i++) mpq_set(pi[i], coef[Q_col[i]]);
  251. /* pi := inv(B') * cB */
  252. bfx_btran(ssx->binv, pi);
  253. return;
  254. }
  255. /*----------------------------------------------------------------------
  256. // ssx_eval_dj - compute reduced cost of non-basic variable.
  257. //
  258. // This routine computes reduced cost d[j] of non-basic variable xN[j]
  259. // in the current basic solution as follows:
  260. //
  261. // d[j] = cN[j] - N[j] * pi,
  262. //
  263. // where cN[j] is an objective coefficient at xN[j], N[j] is a column
  264. // of the augmented constraint matrix (I | -A) corresponding to xN[j],
  265. // pi is the vector of simplex multipliers (shadow prices). */
  266. void ssx_eval_dj(SSX *ssx, int j, mpq_t dj)
  267. { int m = ssx->m;
  268. int n = ssx->n;
  269. mpq_t *coef = ssx->coef;
  270. int *A_ptr = ssx->A_ptr;
  271. int *A_ind = ssx->A_ind;
  272. mpq_t *A_val = ssx->A_val;
  273. int *Q_col = ssx->Q_col;
  274. mpq_t *pi = ssx->pi;
  275. int k, ptr, end;
  276. mpq_t temp;
  277. mpq_init(temp);
  278. xassert(1 <= j && j <= n);
  279. k = Q_col[m+j]; /* x[k] = xN[j] */
  280. xassert(1 <= k && k <= m+n);
  281. /* j-th column of the matrix N is k-th column of the augmented
  282. constraint matrix (I | -A) */
  283. if (k <= m)
  284. { /* it is a column of the unity matrix I */
  285. mpq_sub(dj, coef[k], pi[k]);
  286. }
  287. else
  288. { /* it is a column of the original constraint matrix -A */
  289. mpq_set(dj, coef[k]);
  290. for (ptr = A_ptr[k-m], end = A_ptr[k-m+1]; ptr < end; ptr++)
  291. { mpq_mul(temp, A_val[ptr], pi[A_ind[ptr]]);
  292. mpq_add(dj, dj, temp);
  293. }
  294. }
  295. mpq_clear(temp);
  296. return;
  297. }
  298. /*----------------------------------------------------------------------
  299. // ssx_eval_cbar - compute reduced costs of all non-basic variables.
  300. //
  301. // This routine computes the vector of reduced costs pi in the current
  302. // basic solution for all non-basic variables, including fixed ones. */
  303. void ssx_eval_cbar(SSX *ssx)
  304. { int n = ssx->n;
  305. mpq_t *cbar = ssx->cbar;
  306. int j;
  307. for (j = 1; j <= n; j++)
  308. ssx_eval_dj(ssx, j, cbar[j]);
  309. return;
  310. }
  311. /*----------------------------------------------------------------------
  312. // ssx_eval_rho - compute p-th row of the inverse.
  313. //
  314. // This routine computes p-th row of the matrix inv(B), where B is the
  315. // current basis matrix.
  316. //
  317. // p-th row of the inverse is computed using the following formula:
  318. //
  319. // rho = inv(B') * e[p],
  320. //
  321. // where B' is a matrix transposed to B, e[p] is a unity vector, which
  322. // contains one in p-th position. */
  323. void ssx_eval_rho(SSX *ssx)
  324. { int m = ssx->m;
  325. int p = ssx->p;
  326. mpq_t *rho = ssx->rho;
  327. int i;
  328. xassert(1 <= p && p <= m);
  329. /* rho := 0 */
  330. for (i = 1; i <= m; i++) mpq_set_si(rho[i], 0, 1);
  331. /* rho := e[p] */
  332. mpq_set_si(rho[p], 1, 1);
  333. /* rho := inv(B') * rho */
  334. bfx_btran(ssx->binv, rho);
  335. return;
  336. }
  337. /*----------------------------------------------------------------------
  338. // ssx_eval_row - compute pivot row of the simplex table.
  339. //
  340. // This routine computes p-th (pivot) row of the current simplex table
  341. // A~ = - inv(B) * N using the following formula:
  342. //
  343. // A~[p] = - N' * inv(B') * e[p] = - N' * rho[p],
  344. //
  345. // where N' is a matrix transposed to the matrix N, rho[p] is p-th row
  346. // of the inverse inv(B). */
  347. void ssx_eval_row(SSX *ssx)
  348. { int m = ssx->m;
  349. int n = ssx->n;
  350. int *A_ptr = ssx->A_ptr;
  351. int *A_ind = ssx->A_ind;
  352. mpq_t *A_val = ssx->A_val;
  353. int *Q_col = ssx->Q_col;
  354. mpq_t *rho = ssx->rho;
  355. mpq_t *ap = ssx->ap;
  356. int j, k, ptr;
  357. mpq_t temp;
  358. mpq_init(temp);
  359. for (j = 1; j <= n; j++)
  360. { /* ap[j] := - N'[j] * rho (inner product) */
  361. k = Q_col[m+j]; /* x[k] = xN[j] */
  362. if (k <= m)
  363. mpq_neg(ap[j], rho[k]);
  364. else
  365. { mpq_set_si(ap[j], 0, 1);
  366. for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
  367. { mpq_mul(temp, A_val[ptr], rho[A_ind[ptr]]);
  368. mpq_add(ap[j], ap[j], temp);
  369. }
  370. }
  371. }
  372. mpq_clear(temp);
  373. return;
  374. }
  375. /*----------------------------------------------------------------------
  376. // ssx_eval_col - compute pivot column of the simplex table.
  377. //
  378. // This routine computes q-th (pivot) column of the current simplex
  379. // table A~ = - inv(B) * N using the following formula:
  380. //
  381. // A~[q] = - inv(B) * N[q],
  382. //
  383. // where N[q] is q-th column of the matrix N corresponding to chosen
  384. // non-basic variable xN[q]. */
  385. void ssx_eval_col(SSX *ssx)
  386. { int m = ssx->m;
  387. int n = ssx->n;
  388. int *A_ptr = ssx->A_ptr;
  389. int *A_ind = ssx->A_ind;
  390. mpq_t *A_val = ssx->A_val;
  391. int *Q_col = ssx->Q_col;
  392. int q = ssx->q;
  393. mpq_t *aq = ssx->aq;
  394. int i, k, ptr;
  395. xassert(1 <= q && q <= n);
  396. /* aq := 0 */
  397. for (i = 1; i <= m; i++) mpq_set_si(aq[i], 0, 1);
  398. /* aq := N[q] */
  399. k = Q_col[m+q]; /* x[k] = xN[q] */
  400. if (k <= m)
  401. { /* N[q] is a column of the unity matrix I */
  402. mpq_set_si(aq[k], 1, 1);
  403. }
  404. else
  405. { /* N[q] is a column of the original constraint matrix -A */
  406. for (ptr = A_ptr[k-m]; ptr < A_ptr[k-m+1]; ptr++)
  407. mpq_neg(aq[A_ind[ptr]], A_val[ptr]);
  408. }
  409. /* aq := inv(B) * aq */
  410. bfx_ftran(ssx->binv, aq, 1);
  411. /* aq := - aq */
  412. for (i = 1; i <= m; i++) mpq_neg(aq[i], aq[i]);
  413. return;
  414. }
  415. /*----------------------------------------------------------------------
  416. // ssx_chuzc - choose pivot column.
  417. //
  418. // This routine chooses non-basic variable xN[q] whose reduced cost
  419. // indicates possible improving of the objective function to enter it
  420. // in the basis.
  421. //
  422. // Currently the standard (textbook) pricing is used, i.e. that
  423. // non-basic variable is preferred which has greatest reduced cost (in
  424. // magnitude).
  425. //
  426. // If xN[q] has been chosen, the routine stores its number q and also
  427. // sets the flag q_dir that indicates direction in which xN[q] has to
  428. // change (+1 means increasing, -1 means decreasing).
  429. //
  430. // If the choice cannot be made, because the current basic solution is
  431. // dual feasible, the routine sets the number q to 0. */
  432. void ssx_chuzc(SSX *ssx)
  433. { int m = ssx->m;
  434. int n = ssx->n;
  435. int dir = (ssx->dir == SSX_MIN ? +1 : -1);
  436. int *Q_col = ssx->Q_col;
  437. int *stat = ssx->stat;
  438. mpq_t *cbar = ssx->cbar;
  439. int j, k, s, q, q_dir;
  440. double best, temp;
  441. /* nothing is chosen so far */
  442. q = 0, q_dir = 0, best = 0.0;
  443. /* look through the list of non-basic variables */
  444. for (j = 1; j <= n; j++)
  445. { k = Q_col[m+j]; /* x[k] = xN[j] */
  446. s = dir * mpq_sgn(cbar[j]);
  447. if ((stat[k] == SSX_NF || stat[k] == SSX_NL) && s < 0 ||
  448. (stat[k] == SSX_NF || stat[k] == SSX_NU) && s > 0)
  449. { /* reduced cost of xN[j] indicates possible improving of
  450. the objective function */
  451. temp = fabs(mpq_get_d(cbar[j]));
  452. xassert(temp != 0.0);
  453. if (q == 0 || best < temp)
  454. q = j, q_dir = - s, best = temp;
  455. }
  456. }
  457. ssx->q = q, ssx->q_dir = q_dir;
  458. return;
  459. }
  460. /*----------------------------------------------------------------------
  461. // ssx_chuzr - choose pivot row.
  462. //
  463. // This routine looks through elements of q-th column of the simplex
  464. // table and chooses basic variable xB[p] which should leave the basis.
  465. //
  466. // The choice is based on the standard (textbook) ratio test.
  467. //
  468. // If xB[p] has been chosen, the routine stores its number p and also
  469. // sets its non-basic status p_stat which should be assigned to xB[p]
  470. // when it has left the basis and become xN[q].
  471. //
  472. // Special case p < 0 means that xN[q] is double-bounded variable and
  473. // it reaches its opposite bound before any basic variable does that,
  474. // so the current basis remains unchanged.
  475. //
  476. // If the choice cannot be made, because xN[q] can infinitely change in
  477. // the feasible direction, the routine sets the number p to 0. */
  478. void ssx_chuzr(SSX *ssx)
  479. { int m = ssx->m;
  480. int n = ssx->n;
  481. int *type = ssx->type;
  482. mpq_t *lb = ssx->lb;
  483. mpq_t *ub = ssx->ub;
  484. int *Q_col = ssx->Q_col;
  485. mpq_t *bbar = ssx->bbar;
  486. int q = ssx->q;
  487. mpq_t *aq = ssx->aq;
  488. int q_dir = ssx->q_dir;
  489. int i, k, s, t, p, p_stat;
  490. mpq_t teta, temp;
  491. mpq_init(teta);
  492. mpq_init(temp);
  493. xassert(1 <= q && q <= n);
  494. xassert(q_dir == +1 || q_dir == -1);
  495. /* nothing is chosen so far */
  496. p = 0, p_stat = 0;
  497. /* look through the list of basic variables */
  498. for (i = 1; i <= m; i++)
  499. { s = q_dir * mpq_sgn(aq[i]);
  500. if (s < 0)
  501. { /* xB[i] decreases */
  502. k = Q_col[i]; /* x[k] = xB[i] */
  503. t = type[k];
  504. if (t == SSX_LO || t == SSX_DB || t == SSX_FX)
  505. { /* xB[i] has finite lower bound */
  506. mpq_sub(temp, bbar[i], lb[k]);
  507. mpq_div(temp, temp, aq[i]);
  508. mpq_abs(temp, temp);
  509. if (p == 0 || mpq_cmp(teta, temp) > 0)
  510. { p = i;
  511. p_stat = (t == SSX_FX ? SSX_NS : SSX_NL);
  512. mpq_set(teta, temp);
  513. }
  514. }
  515. }
  516. else if (s > 0)
  517. { /* xB[i] increases */
  518. k = Q_col[i]; /* x[k] = xB[i] */
  519. t = type[k];
  520. if (t == SSX_UP || t == SSX_DB || t == SSX_FX)
  521. { /* xB[i] has finite upper bound */
  522. mpq_sub(temp, bbar[i], ub[k]);
  523. mpq_div(temp, temp, aq[i]);
  524. mpq_abs(temp, temp);
  525. if (p == 0 || mpq_cmp(teta, temp) > 0)
  526. { p = i;
  527. p_stat = (t == SSX_FX ? SSX_NS : SSX_NU);
  528. mpq_set(teta, temp);
  529. }
  530. }
  531. }
  532. /* if something has been chosen and the ratio test indicates
  533. exact degeneracy, the search can be finished */
  534. if (p != 0 && mpq_sgn(teta) == 0) break;
  535. }
  536. /* if xN[q] is double-bounded, check if it can reach its opposite
  537. bound before any basic variable */
  538. k = Q_col[m+q]; /* x[k] = xN[q] */
  539. if (type[k] == SSX_DB)
  540. { mpq_sub(temp, ub[k], lb[k]);
  541. if (p == 0 || mpq_cmp(teta, temp) > 0)
  542. { p = -1;
  543. p_stat = -1;
  544. mpq_set(teta, temp);
  545. }
  546. }
  547. ssx->p = p;
  548. ssx->p_stat = p_stat;
  549. /* if xB[p] has been chosen, determine its actual change in the
  550. adjacent basis (it has the same sign as q_dir) */
  551. if (p != 0)
  552. { xassert(mpq_sgn(teta) >= 0);
  553. if (q_dir > 0)
  554. mpq_set(ssx->delta, teta);
  555. else
  556. mpq_neg(ssx->delta, teta);
  557. }
  558. mpq_clear(teta);
  559. mpq_clear(temp);
  560. return;
  561. }
  562. /*----------------------------------------------------------------------
  563. // ssx_update_bbar - update values of basic variables.
  564. //
  565. // This routine recomputes the current values of basic variables for
  566. // the adjacent basis.
  567. //
  568. // The simplex table for the current basis is the following:
  569. //
  570. // xB[i] = sum{j in 1..n} alfa[i,j] * xN[q], i = 1,...,m
  571. //
  572. // therefore
  573. //
  574. // delta xB[i] = alfa[i,q] * delta xN[q], i = 1,...,m
  575. //
  576. // where delta xN[q] = xN.new[q] - xN[q] is the change of xN[q] in the
  577. // adjacent basis, and delta xB[i] = xB.new[i] - xB[i] is the change of
  578. // xB[i]. This gives formulae for recomputing values of xB[i]:
  579. //
  580. // xB.new[p] = xN[q] + delta xN[q]
  581. //
  582. // (because xN[q] becomes xB[p] in the adjacent basis), and
  583. //
  584. // xB.new[i] = xB[i] + alfa[i,q] * delta xN[q], i != p
  585. //
  586. // for other basic variables. */
  587. void ssx_update_bbar(SSX *ssx)
  588. { int m = ssx->m;
  589. int n = ssx->n;
  590. mpq_t *bbar = ssx->bbar;
  591. mpq_t *cbar = ssx->cbar;
  592. int p = ssx->p;
  593. int q = ssx->q;
  594. mpq_t *aq = ssx->aq;
  595. int i;
  596. mpq_t temp;
  597. mpq_init(temp);
  598. xassert(1 <= q && q <= n);
  599. if (p < 0)
  600. { /* xN[q] is double-bounded and goes to its opposite bound */
  601. /* nop */;
  602. }
  603. else
  604. { /* xN[q] becomes xB[p] in the adjacent basis */
  605. /* xB.new[p] = xN[q] + delta xN[q] */
  606. xassert(1 <= p && p <= m);
  607. ssx_get_xNj(ssx, q, temp);
  608. mpq_add(bbar[p], temp, ssx->delta);
  609. }
  610. /* update values of other basic variables depending on xN[q] */
  611. for (i = 1; i <= m; i++)
  612. { if (i == p) continue;
  613. /* xB.new[i] = xB[i] + alfa[i,q] * delta xN[q] */
  614. if (mpq_sgn(aq[i]) == 0) continue;
  615. mpq_mul(temp, aq[i], ssx->delta);
  616. mpq_add(bbar[i], bbar[i], temp);
  617. }
  618. #if 1
  619. /* update value of the objective function */
  620. /* z.new = z + d[q] * delta xN[q] */
  621. mpq_mul(temp, cbar[q], ssx->delta);
  622. mpq_add(bbar[0], bbar[0], temp);
  623. #endif
  624. mpq_clear(temp);
  625. return;
  626. }
  627. /*----------------------------------------------------------------------
  628. -- ssx_update_pi - update simplex multipliers.
  629. --
  630. -- This routine recomputes the vector of simplex multipliers for the
  631. -- adjacent basis. */
  632. void ssx_update_pi(SSX *ssx)
  633. { int m = ssx->m;
  634. int n = ssx->n;
  635. mpq_t *pi = ssx->pi;
  636. mpq_t *cbar = ssx->cbar;
  637. int p = ssx->p;
  638. int q = ssx->q;
  639. mpq_t *aq = ssx->aq;
  640. mpq_t *rho = ssx->rho;
  641. int i;
  642. mpq_t new_dq, temp;
  643. mpq_init(new_dq);
  644. mpq_init(temp);
  645. xassert(1 <= p && p <= m);
  646. xassert(1 <= q && q <= n);
  647. /* compute d[q] in the adjacent basis */
  648. mpq_div(new_dq, cbar[q], aq[p]);
  649. /* update the vector of simplex multipliers */
  650. for (i = 1; i <= m; i++)
  651. { if (mpq_sgn(rho[i]) == 0) continue;
  652. mpq_mul(temp, new_dq, rho[i]);
  653. mpq_sub(pi[i], pi[i], temp);
  654. }
  655. mpq_clear(new_dq);
  656. mpq_clear(temp);
  657. return;
  658. }
  659. /*----------------------------------------------------------------------
  660. // ssx_update_cbar - update reduced costs of non-basic variables.
  661. //
  662. // This routine recomputes the vector of reduced costs of non-basic
  663. // variables for the adjacent basis. */
  664. void ssx_update_cbar(SSX *ssx)
  665. { int m = ssx->m;
  666. int n = ssx->n;
  667. mpq_t *cbar = ssx->cbar;
  668. int p = ssx->p;
  669. int q = ssx->q;
  670. mpq_t *ap = ssx->ap;
  671. int j;
  672. mpq_t temp;
  673. mpq_init(temp);
  674. xassert(1 <= p && p <= m);
  675. xassert(1 <= q && q <= n);
  676. /* compute d[q] in the adjacent basis */
  677. /* d.new[q] = d[q] / alfa[p,q] */
  678. mpq_div(cbar[q], cbar[q], ap[q]);
  679. /* update reduced costs of other non-basic variables */
  680. for (j = 1; j <= n; j++)
  681. { if (j == q) continue;
  682. /* d.new[j] = d[j] - (alfa[p,j] / alfa[p,q]) * d[q] */
  683. if (mpq_sgn(ap[j]) == 0) continue;
  684. mpq_mul(temp, ap[j], cbar[q]);
  685. mpq_sub(cbar[j], cbar[j], temp);
  686. }
  687. mpq_clear(temp);
  688. return;
  689. }
  690. /*----------------------------------------------------------------------
  691. // ssx_change_basis - change current basis to adjacent one.
  692. //
  693. // This routine changes the current basis to the adjacent one swapping
  694. // basic variable xB[p] and non-basic variable xN[q]. */
  695. void ssx_change_basis(SSX *ssx)
  696. { int m = ssx->m;
  697. int n = ssx->n;
  698. int *type = ssx->type;
  699. int *stat = ssx->stat;
  700. int *Q_row = ssx->Q_row;
  701. int *Q_col = ssx->Q_col;
  702. int p = ssx->p;
  703. int q = ssx->q;
  704. int p_stat = ssx->p_stat;
  705. int k, kp, kq;
  706. if (p < 0)
  707. { /* special case: xN[q] goes to its opposite bound */
  708. xassert(1 <= q && q <= n);
  709. k = Q_col[m+q]; /* x[k] = xN[q] */
  710. xassert(type[k] == SSX_DB);
  711. switch (stat[k])
  712. { case SSX_NL:
  713. stat[k] = SSX_NU;
  714. break;
  715. case SSX_NU:
  716. stat[k] = SSX_NL;
  717. break;
  718. default:
  719. xassert(stat != stat);
  720. }
  721. }
  722. else
  723. { /* xB[p] leaves the basis, xN[q] enters the basis */
  724. xassert(1 <= p && p <= m);
  725. xassert(1 <= q && q <= n);
  726. kp = Q_col[p]; /* x[kp] = xB[p] */
  727. kq = Q_col[m+q]; /* x[kq] = xN[q] */
  728. /* check non-basic status of xB[p] which becomes xN[q] */
  729. switch (type[kp])
  730. { case SSX_FR:
  731. xassert(p_stat == SSX_NF);
  732. break;
  733. case SSX_LO:
  734. xassert(p_stat == SSX_NL);
  735. break;
  736. case SSX_UP:
  737. xassert(p_stat == SSX_NU);
  738. break;
  739. case SSX_DB:
  740. xassert(p_stat == SSX_NL || p_stat == SSX_NU);
  741. break;
  742. case SSX_FX:
  743. xassert(p_stat == SSX_NS);
  744. break;
  745. default:
  746. xassert(type != type);
  747. }
  748. /* swap xB[p] and xN[q] */
  749. stat[kp] = (char)p_stat, stat[kq] = SSX_BS;
  750. Q_row[kp] = m+q, Q_row[kq] = p;
  751. Q_col[p] = kq, Q_col[m+q] = kp;
  752. /* update factorization of the basis matrix */
  753. if (bfx_update(ssx->binv, p))
  754. { if (ssx_factorize(ssx))
  755. xassert(("Internal error: basis matrix is singular", 0));
  756. }
  757. }
  758. return;
  759. }
  760. /*----------------------------------------------------------------------
  761. // ssx_delete - delete simplex solver workspace.
  762. //
  763. // This routine deletes the simplex solver workspace freeing all the
  764. // memory allocated to this object. */
  765. void ssx_delete(SSX *ssx)
  766. { int m = ssx->m;
  767. int n = ssx->n;
  768. int nnz = ssx->A_ptr[n+1]-1;
  769. int i, j, k;
  770. xfree(ssx->type);
  771. for (k = 1; k <= m+n; k++) mpq_clear(ssx->lb[k]);
  772. xfree(ssx->lb);
  773. for (k = 1; k <= m+n; k++) mpq_clear(ssx->ub[k]);
  774. xfree(ssx->ub);
  775. for (k = 0; k <= m+n; k++) mpq_clear(ssx->coef[k]);
  776. xfree(ssx->coef);
  777. xfree(ssx->A_ptr);
  778. xfree(ssx->A_ind);
  779. for (k = 1; k <= nnz; k++) mpq_clear(ssx->A_val[k]);
  780. xfree(ssx->A_val);
  781. xfree(ssx->stat);
  782. xfree(ssx->Q_row);
  783. xfree(ssx->Q_col);
  784. bfx_delete_binv(ssx->binv);
  785. for (i = 0; i <= m; i++) mpq_clear(ssx->bbar[i]);
  786. xfree(ssx->bbar);
  787. for (i = 1; i <= m; i++) mpq_clear(ssx->pi[i]);
  788. xfree(ssx->pi);
  789. for (j = 1; j <= n; j++) mpq_clear(ssx->cbar[j]);
  790. xfree(ssx->cbar);
  791. for (i = 1; i <= m; i++) mpq_clear(ssx->rho[i]);
  792. xfree(ssx->rho);
  793. for (j = 1; j <= n; j++) mpq_clear(ssx->ap[j]);
  794. xfree(ssx->ap);
  795. for (i = 1; i <= m; i++) mpq_clear(ssx->aq[i]);
  796. xfree(ssx->aq);
  797. mpq_clear(ssx->delta);
  798. xfree(ssx);
  799. return;
  800. }
  801. /* eof */