glpios02.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /* glpios02.c (preprocess current subproblem) */
  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 "glpios.h"
  24. /***********************************************************************
  25. * prepare_row_info - prepare row info to determine implied bounds
  26. *
  27. * Given a row (linear form)
  28. *
  29. * n
  30. * sum a[j] * x[j] (1)
  31. * j=1
  32. *
  33. * and bounds of columns (variables)
  34. *
  35. * l[j] <= x[j] <= u[j] (2)
  36. *
  37. * this routine computes f_min, j_min, f_max, j_max needed to determine
  38. * implied bounds.
  39. *
  40. * ALGORITHM
  41. *
  42. * Let J+ = {j : a[j] > 0} and J- = {j : a[j] < 0}.
  43. *
  44. * Parameters f_min and j_min are computed as follows:
  45. *
  46. * 1) if there is no x[k] such that k in J+ and l[k] = -inf or k in J-
  47. * and u[k] = +inf, then
  48. *
  49. * f_min := sum a[j] * l[j] + sum a[j] * u[j]
  50. * j in J+ j in J-
  51. * (3)
  52. * j_min := 0
  53. *
  54. * 2) if there is exactly one x[k] such that k in J+ and l[k] = -inf
  55. * or k in J- and u[k] = +inf, then
  56. *
  57. * f_min := sum a[j] * l[j] + sum a[j] * u[j]
  58. * j in J+\{k} j in J-\{k}
  59. * (4)
  60. * j_min := k
  61. *
  62. * 3) if there are two or more x[k] such that k in J+ and l[k] = -inf
  63. * or k in J- and u[k] = +inf, then
  64. *
  65. * f_min := -inf
  66. * (5)
  67. * j_min := 0
  68. *
  69. * Parameters f_max and j_max are computed in a similar way as follows:
  70. *
  71. * 1) if there is no x[k] such that k in J+ and u[k] = +inf or k in J-
  72. * and l[k] = -inf, then
  73. *
  74. * f_max := sum a[j] * u[j] + sum a[j] * l[j]
  75. * j in J+ j in J-
  76. * (6)
  77. * j_max := 0
  78. *
  79. * 2) if there is exactly one x[k] such that k in J+ and u[k] = +inf
  80. * or k in J- and l[k] = -inf, then
  81. *
  82. * f_max := sum a[j] * u[j] + sum a[j] * l[j]
  83. * j in J+\{k} j in J-\{k}
  84. * (7)
  85. * j_max := k
  86. *
  87. * 3) if there are two or more x[k] such that k in J+ and u[k] = +inf
  88. * or k in J- and l[k] = -inf, then
  89. *
  90. * f_max := +inf
  91. * (8)
  92. * j_max := 0 */
  93. struct f_info
  94. { int j_min, j_max;
  95. double f_min, f_max;
  96. };
  97. static void prepare_row_info(int n, const double a[], const double l[],
  98. const double u[], struct f_info *f)
  99. { int j, j_min, j_max;
  100. double f_min, f_max;
  101. xassert(n >= 0);
  102. /* determine f_min and j_min */
  103. f_min = 0.0, j_min = 0;
  104. for (j = 1; j <= n; j++)
  105. { if (a[j] > 0.0)
  106. { if (l[j] == -DBL_MAX)
  107. { if (j_min == 0)
  108. j_min = j;
  109. else
  110. { f_min = -DBL_MAX, j_min = 0;
  111. break;
  112. }
  113. }
  114. else
  115. f_min += a[j] * l[j];
  116. }
  117. else if (a[j] < 0.0)
  118. { if (u[j] == +DBL_MAX)
  119. { if (j_min == 0)
  120. j_min = j;
  121. else
  122. { f_min = -DBL_MAX, j_min = 0;
  123. break;
  124. }
  125. }
  126. else
  127. f_min += a[j] * u[j];
  128. }
  129. else
  130. xassert(a != a);
  131. }
  132. f->f_min = f_min, f->j_min = j_min;
  133. /* determine f_max and j_max */
  134. f_max = 0.0, j_max = 0;
  135. for (j = 1; j <= n; j++)
  136. { if (a[j] > 0.0)
  137. { if (u[j] == +DBL_MAX)
  138. { if (j_max == 0)
  139. j_max = j;
  140. else
  141. { f_max = +DBL_MAX, j_max = 0;
  142. break;
  143. }
  144. }
  145. else
  146. f_max += a[j] * u[j];
  147. }
  148. else if (a[j] < 0.0)
  149. { if (l[j] == -DBL_MAX)
  150. { if (j_max == 0)
  151. j_max = j;
  152. else
  153. { f_max = +DBL_MAX, j_max = 0;
  154. break;
  155. }
  156. }
  157. else
  158. f_max += a[j] * l[j];
  159. }
  160. else
  161. xassert(a != a);
  162. }
  163. f->f_max = f_max, f->j_max = j_max;
  164. return;
  165. }
  166. /***********************************************************************
  167. * row_implied_bounds - determine row implied bounds
  168. *
  169. * Given a row (linear form)
  170. *
  171. * n
  172. * sum a[j] * x[j]
  173. * j=1
  174. *
  175. * and bounds of columns (variables)
  176. *
  177. * l[j] <= x[j] <= u[j]
  178. *
  179. * this routine determines implied bounds of the row.
  180. *
  181. * ALGORITHM
  182. *
  183. * Let J+ = {j : a[j] > 0} and J- = {j : a[j] < 0}.
  184. *
  185. * The implied lower bound of the row is computed as follows:
  186. *
  187. * L' := sum a[j] * l[j] + sum a[j] * u[j] (9)
  188. * j in J+ j in J-
  189. *
  190. * and as it follows from (3), (4), and (5):
  191. *
  192. * L' := if j_min = 0 then f_min else -inf (10)
  193. *
  194. * The implied upper bound of the row is computed as follows:
  195. *
  196. * U' := sum a[j] * u[j] + sum a[j] * l[j] (11)
  197. * j in J+ j in J-
  198. *
  199. * and as it follows from (6), (7), and (8):
  200. *
  201. * U' := if j_max = 0 then f_max else +inf (12)
  202. *
  203. * The implied bounds are stored in locations LL and UU. */
  204. static void row_implied_bounds(const struct f_info *f, double *LL,
  205. double *UU)
  206. { *LL = (f->j_min == 0 ? f->f_min : -DBL_MAX);
  207. *UU = (f->j_max == 0 ? f->f_max : +DBL_MAX);
  208. return;
  209. }
  210. /***********************************************************************
  211. * col_implied_bounds - determine column implied bounds
  212. *
  213. * Given a row (constraint)
  214. *
  215. * n
  216. * L <= sum a[j] * x[j] <= U (13)
  217. * j=1
  218. *
  219. * and bounds of columns (variables)
  220. *
  221. * l[j] <= x[j] <= u[j]
  222. *
  223. * this routine determines implied bounds of variable x[k].
  224. *
  225. * It is assumed that if L != -inf, the lower bound of the row can be
  226. * active, and if U != +inf, the upper bound of the row can be active.
  227. *
  228. * ALGORITHM
  229. *
  230. * From (13) it follows that
  231. *
  232. * L <= sum a[j] * x[j] + a[k] * x[k] <= U
  233. * j!=k
  234. * or
  235. *
  236. * L - sum a[j] * x[j] <= a[k] * x[k] <= U - sum a[j] * x[j]
  237. * j!=k j!=k
  238. *
  239. * Thus, if the row lower bound L can be active, implied lower bound of
  240. * term a[k] * x[k] can be determined as follows:
  241. *
  242. * ilb(a[k] * x[k]) = min(L - sum a[j] * x[j]) =
  243. * j!=k
  244. * (14)
  245. * = L - max sum a[j] * x[j]
  246. * j!=k
  247. *
  248. * where, as it follows from (6), (7), and (8)
  249. *
  250. * / f_max - a[k] * u[k], j_max = 0, a[k] > 0
  251. * |
  252. * | f_max - a[k] * l[k], j_max = 0, a[k] < 0
  253. * max sum a[j] * x[j] = {
  254. * j!=k | f_max, j_max = k
  255. * |
  256. * \ +inf, j_max != 0
  257. *
  258. * and if the upper bound U can be active, implied upper bound of term
  259. * a[k] * x[k] can be determined as follows:
  260. *
  261. * iub(a[k] * x[k]) = max(U - sum a[j] * x[j]) =
  262. * j!=k
  263. * (15)
  264. * = U - min sum a[j] * x[j]
  265. * j!=k
  266. *
  267. * where, as it follows from (3), (4), and (5)
  268. *
  269. * / f_min - a[k] * l[k], j_min = 0, a[k] > 0
  270. * |
  271. * | f_min - a[k] * u[k], j_min = 0, a[k] < 0
  272. * min sum a[j] * x[j] = {
  273. * j!=k | f_min, j_min = k
  274. * |
  275. * \ -inf, j_min != 0
  276. *
  277. * Since
  278. *
  279. * ilb(a[k] * x[k]) <= a[k] * x[k] <= iub(a[k] * x[k])
  280. *
  281. * implied lower and upper bounds of x[k] are determined as follows:
  282. *
  283. * l'[k] := if a[k] > 0 then ilb / a[k] else ulb / a[k] (16)
  284. *
  285. * u'[k] := if a[k] > 0 then ulb / a[k] else ilb / a[k] (17)
  286. *
  287. * The implied bounds are stored in locations ll and uu. */
  288. static void col_implied_bounds(const struct f_info *f, int n,
  289. const double a[], double L, double U, const double l[],
  290. const double u[], int k, double *ll, double *uu)
  291. { double ilb, iub;
  292. xassert(n >= 0);
  293. xassert(1 <= k && k <= n);
  294. /* determine implied lower bound of term a[k] * x[k] (14) */
  295. if (L == -DBL_MAX || f->f_max == +DBL_MAX)
  296. ilb = -DBL_MAX;
  297. else if (f->j_max == 0)
  298. { if (a[k] > 0.0)
  299. { xassert(u[k] != +DBL_MAX);
  300. ilb = L - (f->f_max - a[k] * u[k]);
  301. }
  302. else if (a[k] < 0.0)
  303. { xassert(l[k] != -DBL_MAX);
  304. ilb = L - (f->f_max - a[k] * l[k]);
  305. }
  306. else
  307. xassert(a != a);
  308. }
  309. else if (f->j_max == k)
  310. ilb = L - f->f_max;
  311. else
  312. ilb = -DBL_MAX;
  313. /* determine implied upper bound of term a[k] * x[k] (15) */
  314. if (U == +DBL_MAX || f->f_min == -DBL_MAX)
  315. iub = +DBL_MAX;
  316. else if (f->j_min == 0)
  317. { if (a[k] > 0.0)
  318. { xassert(l[k] != -DBL_MAX);
  319. iub = U - (f->f_min - a[k] * l[k]);
  320. }
  321. else if (a[k] < 0.0)
  322. { xassert(u[k] != +DBL_MAX);
  323. iub = U - (f->f_min - a[k] * u[k]);
  324. }
  325. else
  326. xassert(a != a);
  327. }
  328. else if (f->j_min == k)
  329. iub = U - f->f_min;
  330. else
  331. iub = +DBL_MAX;
  332. /* determine implied bounds of x[k] (16) and (17) */
  333. #if 1
  334. /* do not use a[k] if it has small magnitude to prevent wrong
  335. implied bounds; for example, 1e-15 * x1 >= x2 + x3, where
  336. x1 >= -10, x2, x3 >= 0, would lead to wrong conclusion that
  337. x1 >= 0 */
  338. if (fabs(a[k]) < 1e-6)
  339. *ll = -DBL_MAX, *uu = +DBL_MAX; else
  340. #endif
  341. if (a[k] > 0.0)
  342. { *ll = (ilb == -DBL_MAX ? -DBL_MAX : ilb / a[k]);
  343. *uu = (iub == +DBL_MAX ? +DBL_MAX : iub / a[k]);
  344. }
  345. else if (a[k] < 0.0)
  346. { *ll = (iub == +DBL_MAX ? -DBL_MAX : iub / a[k]);
  347. *uu = (ilb == -DBL_MAX ? +DBL_MAX : ilb / a[k]);
  348. }
  349. else
  350. xassert(a != a);
  351. return;
  352. }
  353. /***********************************************************************
  354. * check_row_bounds - check and relax original row bounds
  355. *
  356. * Given a row (constraint)
  357. *
  358. * n
  359. * L <= sum a[j] * x[j] <= U
  360. * j=1
  361. *
  362. * and bounds of columns (variables)
  363. *
  364. * l[j] <= x[j] <= u[j]
  365. *
  366. * this routine checks the original row bounds L and U for feasibility
  367. * and redundancy. If the original lower bound L or/and upper bound U
  368. * cannot be active due to bounds of variables, the routine remove them
  369. * replacing by -inf or/and +inf, respectively.
  370. *
  371. * If no primal infeasibility is detected, the routine returns zero,
  372. * otherwise non-zero. */
  373. static int check_row_bounds(const struct f_info *f, double *L_,
  374. double *U_)
  375. { int ret = 0;
  376. double L = *L_, U = *U_, LL, UU;
  377. /* determine implied bounds of the row */
  378. row_implied_bounds(f, &LL, &UU);
  379. /* check if the original lower bound is infeasible */
  380. if (L != -DBL_MAX)
  381. { double eps = 1e-3 * (1.0 + fabs(L));
  382. if (UU < L - eps)
  383. { ret = 1;
  384. goto done;
  385. }
  386. }
  387. /* check if the original upper bound is infeasible */
  388. if (U != +DBL_MAX)
  389. { double eps = 1e-3 * (1.0 + fabs(U));
  390. if (LL > U + eps)
  391. { ret = 1;
  392. goto done;
  393. }
  394. }
  395. /* check if the original lower bound is redundant */
  396. if (L != -DBL_MAX)
  397. { double eps = 1e-12 * (1.0 + fabs(L));
  398. if (LL > L - eps)
  399. { /* it cannot be active, so remove it */
  400. *L_ = -DBL_MAX;
  401. }
  402. }
  403. /* check if the original upper bound is redundant */
  404. if (U != +DBL_MAX)
  405. { double eps = 1e-12 * (1.0 + fabs(U));
  406. if (UU < U + eps)
  407. { /* it cannot be active, so remove it */
  408. *U_ = +DBL_MAX;
  409. }
  410. }
  411. done: return ret;
  412. }
  413. /***********************************************************************
  414. * check_col_bounds - check and tighten original column bounds
  415. *
  416. * Given a row (constraint)
  417. *
  418. * n
  419. * L <= sum a[j] * x[j] <= U
  420. * j=1
  421. *
  422. * and bounds of columns (variables)
  423. *
  424. * l[j] <= x[j] <= u[j]
  425. *
  426. * for column (variable) x[j] this routine checks the original column
  427. * bounds l[j] and u[j] for feasibility and redundancy. If the original
  428. * lower bound l[j] or/and upper bound u[j] cannot be active due to
  429. * bounds of the constraint and other variables, the routine tighten
  430. * them replacing by corresponding implied bounds, if possible.
  431. *
  432. * NOTE: It is assumed that if L != -inf, the row lower bound can be
  433. * active, and if U != +inf, the row upper bound can be active.
  434. *
  435. * The flag means that variable x[j] is required to be integer.
  436. *
  437. * New actual bounds for x[j] are stored in locations lj and uj.
  438. *
  439. * If no primal infeasibility is detected, the routine returns zero,
  440. * otherwise non-zero. */
  441. static int check_col_bounds(const struct f_info *f, int n,
  442. const double a[], double L, double U, const double l[],
  443. const double u[], int flag, int j, double *_lj, double *_uj)
  444. { int ret = 0;
  445. double lj, uj, ll, uu;
  446. xassert(n >= 0);
  447. xassert(1 <= j && j <= n);
  448. lj = l[j], uj = u[j];
  449. /* determine implied bounds of the column */
  450. col_implied_bounds(f, n, a, L, U, l, u, j, &ll, &uu);
  451. /* if x[j] is integral, round its implied bounds */
  452. if (flag)
  453. { if (ll != -DBL_MAX)
  454. ll = (ll - floor(ll) < 1e-3 ? floor(ll) : ceil(ll));
  455. if (uu != +DBL_MAX)
  456. uu = (ceil(uu) - uu < 1e-3 ? ceil(uu) : floor(uu));
  457. }
  458. /* check if the original lower bound is infeasible */
  459. if (lj != -DBL_MAX)
  460. { double eps = 1e-3 * (1.0 + fabs(lj));
  461. if (uu < lj - eps)
  462. { ret = 1;
  463. goto done;
  464. }
  465. }
  466. /* check if the original upper bound is infeasible */
  467. if (uj != +DBL_MAX)
  468. { double eps = 1e-3 * (1.0 + fabs(uj));
  469. if (ll > uj + eps)
  470. { ret = 1;
  471. goto done;
  472. }
  473. }
  474. /* check if the original lower bound is redundant */
  475. if (ll != -DBL_MAX)
  476. { double eps = 1e-3 * (1.0 + fabs(ll));
  477. if (lj < ll - eps)
  478. { /* it cannot be active, so tighten it */
  479. lj = ll;
  480. }
  481. }
  482. /* check if the original upper bound is redundant */
  483. if (uu != +DBL_MAX)
  484. { double eps = 1e-3 * (1.0 + fabs(uu));
  485. if (uj > uu + eps)
  486. { /* it cannot be active, so tighten it */
  487. uj = uu;
  488. }
  489. }
  490. /* due to round-off errors it may happen that lj > uj (although
  491. lj < uj + eps, since no primal infeasibility is detected), so
  492. adjuct the new actual bounds to provide lj <= uj */
  493. if (!(lj == -DBL_MAX || uj == +DBL_MAX))
  494. { double t1 = fabs(lj), t2 = fabs(uj);
  495. double eps = 1e-10 * (1.0 + (t1 <= t2 ? t1 : t2));
  496. if (lj > uj - eps)
  497. { if (lj == l[j])
  498. uj = lj;
  499. else if (uj == u[j])
  500. lj = uj;
  501. else if (t1 <= t2)
  502. uj = lj;
  503. else
  504. lj = uj;
  505. }
  506. }
  507. *_lj = lj, *_uj = uj;
  508. done: return ret;
  509. }
  510. /***********************************************************************
  511. * check_efficiency - check if change in column bounds is efficient
  512. *
  513. * Given the original bounds of a column l and u and its new actual
  514. * bounds l' and u' (possibly tighten by the routine check_col_bounds)
  515. * this routine checks if the change in the column bounds is efficient
  516. * enough. If so, the routine returns non-zero, otherwise zero.
  517. *
  518. * The flag means that the variable is required to be integer. */
  519. static int check_efficiency(int flag, double l, double u, double ll,
  520. double uu)
  521. { int eff = 0;
  522. /* check efficiency for lower bound */
  523. if (l < ll)
  524. { if (flag || l == -DBL_MAX)
  525. eff++;
  526. else
  527. { double r;
  528. if (u == +DBL_MAX)
  529. r = 1.0 + fabs(l);
  530. else
  531. r = 1.0 + (u - l);
  532. if (ll - l >= 0.25 * r)
  533. eff++;
  534. }
  535. }
  536. /* check efficiency for upper bound */
  537. if (u > uu)
  538. { if (flag || u == +DBL_MAX)
  539. eff++;
  540. else
  541. { double r;
  542. if (l == -DBL_MAX)
  543. r = 1.0 + fabs(u);
  544. else
  545. r = 1.0 + (u - l);
  546. if (u - uu >= 0.25 * r)
  547. eff++;
  548. }
  549. }
  550. return eff;
  551. }
  552. /***********************************************************************
  553. * basic_preprocessing - perform basic preprocessing
  554. *
  555. * This routine performs basic preprocessing of the specified MIP that
  556. * includes relaxing some row bounds and tightening some column bounds.
  557. *
  558. * On entry the arrays L and U contains original row bounds, and the
  559. * arrays l and u contains original column bounds:
  560. *
  561. * L[0] is the lower bound of the objective row;
  562. * L[i], i = 1,...,m, is the lower bound of i-th row;
  563. * U[0] is the upper bound of the objective row;
  564. * U[i], i = 1,...,m, is the upper bound of i-th row;
  565. * l[0] is not used;
  566. * l[j], j = 1,...,n, is the lower bound of j-th column;
  567. * u[0] is not used;
  568. * u[j], j = 1,...,n, is the upper bound of j-th column.
  569. *
  570. * On exit the arrays L, U, l, and u contain new actual bounds of rows
  571. * and column in the same locations.
  572. *
  573. * The parameters nrs and num specify an initial list of rows to be
  574. * processed:
  575. *
  576. * nrs is the number of rows in the initial list, 0 <= nrs <= m+1;
  577. * num[0] is not used;
  578. * num[1,...,nrs] are row numbers (0 means the objective row).
  579. *
  580. * The parameter max_pass specifies the maximal number of times that
  581. * each row can be processed, max_pass > 0.
  582. *
  583. * If no primal infeasibility is detected, the routine returns zero,
  584. * otherwise non-zero. */
  585. static int basic_preprocessing(glp_prob *mip, double L[], double U[],
  586. double l[], double u[], int nrs, const int num[], int max_pass)
  587. { int m = mip->m;
  588. int n = mip->n;
  589. struct f_info f;
  590. int i, j, k, len, size, ret = 0;
  591. int *ind, *list, *mark, *pass;
  592. double *val, *lb, *ub;
  593. xassert(0 <= nrs && nrs <= m+1);
  594. xassert(max_pass > 0);
  595. /* allocate working arrays */
  596. ind = xcalloc(1+n, sizeof(int));
  597. list = xcalloc(1+m+1, sizeof(int));
  598. mark = xcalloc(1+m+1, sizeof(int));
  599. memset(&mark[0], 0, (m+1) * sizeof(int));
  600. pass = xcalloc(1+m+1, sizeof(int));
  601. memset(&pass[0], 0, (m+1) * sizeof(int));
  602. val = xcalloc(1+n, sizeof(double));
  603. lb = xcalloc(1+n, sizeof(double));
  604. ub = xcalloc(1+n, sizeof(double));
  605. /* initialize the list of rows to be processed */
  606. size = 0;
  607. for (k = 1; k <= nrs; k++)
  608. { i = num[k];
  609. xassert(0 <= i && i <= m);
  610. /* duplicate row numbers are not allowed */
  611. xassert(!mark[i]);
  612. list[++size] = i, mark[i] = 1;
  613. }
  614. xassert(size == nrs);
  615. /* process rows in the list until it becomes empty */
  616. while (size > 0)
  617. { /* get a next row from the list */
  618. i = list[size--], mark[i] = 0;
  619. /* increase the row processing count */
  620. pass[i]++;
  621. /* if the row is free, skip it */
  622. if (L[i] == -DBL_MAX && U[i] == +DBL_MAX) continue;
  623. /* obtain coefficients of the row */
  624. len = 0;
  625. if (i == 0)
  626. { for (j = 1; j <= n; j++)
  627. { GLPCOL *col = mip->col[j];
  628. if (col->coef != 0.0)
  629. len++, ind[len] = j, val[len] = col->coef;
  630. }
  631. }
  632. else
  633. { GLPROW *row = mip->row[i];
  634. GLPAIJ *aij;
  635. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  636. len++, ind[len] = aij->col->j, val[len] = aij->val;
  637. }
  638. /* determine lower and upper bounds of columns corresponding
  639. to non-zero row coefficients */
  640. for (k = 1; k <= len; k++)
  641. j = ind[k], lb[k] = l[j], ub[k] = u[j];
  642. /* prepare the row info to determine implied bounds */
  643. prepare_row_info(len, val, lb, ub, &f);
  644. /* check and relax bounds of the row */
  645. if (check_row_bounds(&f, &L[i], &U[i]))
  646. { /* the feasible region is empty */
  647. ret = 1;
  648. goto done;
  649. }
  650. /* if the row became free, drop it */
  651. if (L[i] == -DBL_MAX && U[i] == +DBL_MAX) continue;
  652. /* process columns having non-zero coefficients in the row */
  653. for (k = 1; k <= len; k++)
  654. { GLPCOL *col;
  655. int flag, eff;
  656. double ll, uu;
  657. /* take a next column in the row */
  658. j = ind[k], col = mip->col[j];
  659. flag = col->kind != GLP_CV;
  660. /* check and tighten bounds of the column */
  661. if (check_col_bounds(&f, len, val, L[i], U[i], lb, ub,
  662. flag, k, &ll, &uu))
  663. { /* the feasible region is empty */
  664. ret = 1;
  665. goto done;
  666. }
  667. /* check if change in the column bounds is efficient */
  668. eff = check_efficiency(flag, l[j], u[j], ll, uu);
  669. /* set new actual bounds of the column */
  670. l[j] = ll, u[j] = uu;
  671. /* if the change is efficient, add all rows affected by the
  672. corresponding column, to the list */
  673. if (eff > 0)
  674. { GLPAIJ *aij;
  675. for (aij = col->ptr; aij != NULL; aij = aij->c_next)
  676. { int ii = aij->row->i;
  677. /* if the row was processed maximal number of times,
  678. skip it */
  679. if (pass[ii] >= max_pass) continue;
  680. /* if the row is free, skip it */
  681. if (L[ii] == -DBL_MAX && U[ii] == +DBL_MAX) continue;
  682. /* put the row into the list */
  683. if (mark[ii] == 0)
  684. { xassert(size <= m);
  685. list[++size] = ii, mark[ii] = 1;
  686. }
  687. }
  688. }
  689. }
  690. }
  691. done: /* free working arrays */
  692. xfree(ind);
  693. xfree(list);
  694. xfree(mark);
  695. xfree(pass);
  696. xfree(val);
  697. xfree(lb);
  698. xfree(ub);
  699. return ret;
  700. }
  701. /***********************************************************************
  702. * NAME
  703. *
  704. * ios_preprocess_node - preprocess current subproblem
  705. *
  706. * SYNOPSIS
  707. *
  708. * #include "glpios.h"
  709. * int ios_preprocess_node(glp_tree *tree, int max_pass);
  710. *
  711. * DESCRIPTION
  712. *
  713. * The routine ios_preprocess_node performs basic preprocessing of the
  714. * current subproblem.
  715. *
  716. * RETURNS
  717. *
  718. * If no primal infeasibility is detected, the routine returns zero,
  719. * otherwise non-zero. */
  720. int ios_preprocess_node(glp_tree *tree, int max_pass)
  721. { glp_prob *mip = tree->mip;
  722. int m = mip->m;
  723. int n = mip->n;
  724. int i, j, nrs, *num, ret = 0;
  725. double *L, *U, *l, *u;
  726. /* the current subproblem must exist */
  727. xassert(tree->curr != NULL);
  728. /* determine original row bounds */
  729. L = xcalloc(1+m, sizeof(double));
  730. U = xcalloc(1+m, sizeof(double));
  731. switch (mip->mip_stat)
  732. { case GLP_UNDEF:
  733. L[0] = -DBL_MAX, U[0] = +DBL_MAX;
  734. break;
  735. case GLP_FEAS:
  736. switch (mip->dir)
  737. { case GLP_MIN:
  738. L[0] = -DBL_MAX, U[0] = mip->mip_obj - mip->c0;
  739. break;
  740. case GLP_MAX:
  741. L[0] = mip->mip_obj - mip->c0, U[0] = +DBL_MAX;
  742. break;
  743. default:
  744. xassert(mip != mip);
  745. }
  746. break;
  747. default:
  748. xassert(mip != mip);
  749. }
  750. for (i = 1; i <= m; i++)
  751. { L[i] = glp_get_row_lb(mip, i);
  752. U[i] = glp_get_row_ub(mip, i);
  753. }
  754. /* determine original column bounds */
  755. l = xcalloc(1+n, sizeof(double));
  756. u = xcalloc(1+n, sizeof(double));
  757. for (j = 1; j <= n; j++)
  758. { l[j] = glp_get_col_lb(mip, j);
  759. u[j] = glp_get_col_ub(mip, j);
  760. }
  761. /* build the initial list of rows to be analyzed */
  762. nrs = m + 1;
  763. num = xcalloc(1+nrs, sizeof(int));
  764. for (i = 1; i <= nrs; i++) num[i] = i - 1;
  765. /* perform basic preprocessing */
  766. if (basic_preprocessing(mip , L, U, l, u, nrs, num, max_pass))
  767. { ret = 1;
  768. goto done;
  769. }
  770. /* set new actual (relaxed) row bounds */
  771. for (i = 1; i <= m; i++)
  772. { /* consider only non-active rows to keep dual feasibility */
  773. if (glp_get_row_stat(mip, i) == GLP_BS)
  774. { if (L[i] == -DBL_MAX && U[i] == +DBL_MAX)
  775. glp_set_row_bnds(mip, i, GLP_FR, 0.0, 0.0);
  776. else if (U[i] == +DBL_MAX)
  777. glp_set_row_bnds(mip, i, GLP_LO, L[i], 0.0);
  778. else if (L[i] == -DBL_MAX)
  779. glp_set_row_bnds(mip, i, GLP_UP, 0.0, U[i]);
  780. }
  781. }
  782. /* set new actual (tightened) column bounds */
  783. for (j = 1; j <= n; j++)
  784. { int type;
  785. if (l[j] == -DBL_MAX && u[j] == +DBL_MAX)
  786. type = GLP_FR;
  787. else if (u[j] == +DBL_MAX)
  788. type = GLP_LO;
  789. else if (l[j] == -DBL_MAX)
  790. type = GLP_UP;
  791. else if (l[j] != u[j])
  792. type = GLP_DB;
  793. else
  794. type = GLP_FX;
  795. glp_set_col_bnds(mip, j, type, l[j], u[j]);
  796. }
  797. done: /* free working arrays and return */
  798. xfree(L);
  799. xfree(U);
  800. xfree(l);
  801. xfree(u);
  802. xfree(num);
  803. return ret;
  804. }
  805. /* eof */