glpios07.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* glpios07.c (mixed cover cut generator) */
  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. -- COVER INEQUALITIES
  26. --
  27. -- Consider the set of feasible solutions to 0-1 knapsack problem:
  28. --
  29. -- sum a[j]*x[j] <= b, (1)
  30. -- j in J
  31. --
  32. -- x[j] is binary, (2)
  33. --
  34. -- where, wlog, we assume that a[j] > 0 (since 0-1 variables can be
  35. -- complemented) and a[j] <= b (since a[j] > b implies x[j] = 0).
  36. --
  37. -- A set C within J is called a cover if
  38. --
  39. -- sum a[j] > b. (3)
  40. -- j in C
  41. --
  42. -- For any cover C the inequality
  43. --
  44. -- sum x[j] <= |C| - 1 (4)
  45. -- j in C
  46. --
  47. -- is called a cover inequality and is valid for (1)-(2).
  48. --
  49. -- MIXED COVER INEQUALITIES
  50. --
  51. -- Consider the set of feasible solutions to mixed knapsack problem:
  52. --
  53. -- sum a[j]*x[j] + y <= b, (5)
  54. -- j in J
  55. --
  56. -- x[j] is binary, (6)
  57. --
  58. -- 0 <= y <= u is continuous, (7)
  59. --
  60. -- where again we assume that a[j] > 0.
  61. --
  62. -- Let C within J be some set. From (1)-(4) it follows that
  63. --
  64. -- sum a[j] > b - y (8)
  65. -- j in C
  66. --
  67. -- implies
  68. --
  69. -- sum x[j] <= |C| - 1. (9)
  70. -- j in C
  71. --
  72. -- Thus, we need to modify the inequality (9) in such a way that it be
  73. -- a constraint only if the condition (8) is satisfied.
  74. --
  75. -- Consider the following inequality:
  76. --
  77. -- sum x[j] <= |C| - t. (10)
  78. -- j in C
  79. --
  80. -- If 0 < t <= 1, then (10) is equivalent to (9), because all x[j] are
  81. -- binary variables. On the other hand, if t <= 0, (10) being satisfied
  82. -- for any values of x[j] is not a constraint.
  83. --
  84. -- Let
  85. --
  86. -- t' = sum a[j] + y - b. (11)
  87. -- j in C
  88. --
  89. -- It is understood that the condition t' > 0 is equivalent to (8).
  90. -- Besides, from (6)-(7) it follows that t' has an implied upper bound:
  91. --
  92. -- t'max = sum a[j] + u - b. (12)
  93. -- j in C
  94. --
  95. -- This allows to express the parameter t having desired properties:
  96. --
  97. -- t = t' / t'max. (13)
  98. --
  99. -- In fact, t <= 1 by definition, and t > 0 being equivalent to t' > 0
  100. -- is equivalent to (8).
  101. --
  102. -- Thus, the inequality (10), where t is given by formula (13) is valid
  103. -- for (5)-(7).
  104. --
  105. -- Note that if u = 0, then y = 0, so t = 1, and the conditions (8) and
  106. -- (10) is transformed to the conditions (3) and (4).
  107. --
  108. -- GENERATING MIXED COVER CUTS
  109. --
  110. -- To generate a mixed cover cut in the form (10) we need to find such
  111. -- set C which satisfies to the inequality (8) and for which, in turn,
  112. -- the inequality (10) is violated in the current point.
  113. --
  114. -- Substituting t from (13) to (10) gives:
  115. --
  116. -- 1
  117. -- sum x[j] <= |C| - ----- (sum a[j] + y - b), (14)
  118. -- j in C t'max j in C
  119. --
  120. -- and finally we have the cut inequality in the standard form:
  121. --
  122. -- sum x[j] + alfa * y <= beta, (15)
  123. -- j in C
  124. --
  125. -- where:
  126. --
  127. -- alfa = 1 / t'max, (16)
  128. --
  129. -- beta = |C| - alfa * (sum a[j] - b). (17)
  130. -- j in C */
  131. #if 1
  132. #define MAXTRY 1000
  133. #else
  134. #define MAXTRY 10000
  135. #endif
  136. static int cover2(int n, double a[], double b, double u, double x[],
  137. double y, int cov[], double *_alfa, double *_beta)
  138. { /* try to generate mixed cover cut using two-element cover */
  139. int i, j, try = 0, ret = 0;
  140. double eps, alfa, beta, temp, rmax = 0.001;
  141. eps = 0.001 * (1.0 + fabs(b));
  142. for (i = 0+1; i <= n; i++)
  143. for (j = i+1; j <= n; j++)
  144. { /* C = {i, j} */
  145. try++;
  146. if (try > MAXTRY) goto done;
  147. /* check if condition (8) is satisfied */
  148. if (a[i] + a[j] + y > b + eps)
  149. { /* compute parameters for inequality (15) */
  150. temp = a[i] + a[j] - b;
  151. alfa = 1.0 / (temp + u);
  152. beta = 2.0 - alfa * temp;
  153. /* compute violation of inequality (15) */
  154. temp = x[i] + x[j] + alfa * y - beta;
  155. /* choose C providing maximum violation */
  156. if (rmax < temp)
  157. { rmax = temp;
  158. cov[1] = i;
  159. cov[2] = j;
  160. *_alfa = alfa;
  161. *_beta = beta;
  162. ret = 1;
  163. }
  164. }
  165. }
  166. done: return ret;
  167. }
  168. static int cover3(int n, double a[], double b, double u, double x[],
  169. double y, int cov[], double *_alfa, double *_beta)
  170. { /* try to generate mixed cover cut using three-element cover */
  171. int i, j, k, try = 0, ret = 0;
  172. double eps, alfa, beta, temp, rmax = 0.001;
  173. eps = 0.001 * (1.0 + fabs(b));
  174. for (i = 0+1; i <= n; i++)
  175. for (j = i+1; j <= n; j++)
  176. for (k = j+1; k <= n; k++)
  177. { /* C = {i, j, k} */
  178. try++;
  179. if (try > MAXTRY) goto done;
  180. /* check if condition (8) is satisfied */
  181. if (a[i] + a[j] + a[k] + y > b + eps)
  182. { /* compute parameters for inequality (15) */
  183. temp = a[i] + a[j] + a[k] - b;
  184. alfa = 1.0 / (temp + u);
  185. beta = 3.0 - alfa * temp;
  186. /* compute violation of inequality (15) */
  187. temp = x[i] + x[j] + x[k] + alfa * y - beta;
  188. /* choose C providing maximum violation */
  189. if (rmax < temp)
  190. { rmax = temp;
  191. cov[1] = i;
  192. cov[2] = j;
  193. cov[3] = k;
  194. *_alfa = alfa;
  195. *_beta = beta;
  196. ret = 1;
  197. }
  198. }
  199. }
  200. done: return ret;
  201. }
  202. static int cover4(int n, double a[], double b, double u, double x[],
  203. double y, int cov[], double *_alfa, double *_beta)
  204. { /* try to generate mixed cover cut using four-element cover */
  205. int i, j, k, l, try = 0, ret = 0;
  206. double eps, alfa, beta, temp, rmax = 0.001;
  207. eps = 0.001 * (1.0 + fabs(b));
  208. for (i = 0+1; i <= n; i++)
  209. for (j = i+1; j <= n; j++)
  210. for (k = j+1; k <= n; k++)
  211. for (l = k+1; l <= n; l++)
  212. { /* C = {i, j, k, l} */
  213. try++;
  214. if (try > MAXTRY) goto done;
  215. /* check if condition (8) is satisfied */
  216. if (a[i] + a[j] + a[k] + a[l] + y > b + eps)
  217. { /* compute parameters for inequality (15) */
  218. temp = a[i] + a[j] + a[k] + a[l] - b;
  219. alfa = 1.0 / (temp + u);
  220. beta = 4.0 - alfa * temp;
  221. /* compute violation of inequality (15) */
  222. temp = x[i] + x[j] + x[k] + x[l] + alfa * y - beta;
  223. /* choose C providing maximum violation */
  224. if (rmax < temp)
  225. { rmax = temp;
  226. cov[1] = i;
  227. cov[2] = j;
  228. cov[3] = k;
  229. cov[4] = l;
  230. *_alfa = alfa;
  231. *_beta = beta;
  232. ret = 1;
  233. }
  234. }
  235. }
  236. done: return ret;
  237. }
  238. static int cover(int n, double a[], double b, double u, double x[],
  239. double y, int cov[], double *alfa, double *beta)
  240. { /* try to generate mixed cover cut;
  241. input (see (5)):
  242. n is the number of binary variables;
  243. a[1:n] are coefficients at binary variables;
  244. b is the right-hand side;
  245. u is upper bound of continuous variable;
  246. x[1:n] are values of binary variables at current point;
  247. y is value of continuous variable at current point;
  248. output (see (15), (16), (17)):
  249. cov[1:r] are indices of binary variables included in cover C,
  250. where r is the set cardinality returned on exit;
  251. alfa coefficient at continuous variable;
  252. beta is the right-hand side; */
  253. int j;
  254. /* perform some sanity checks */
  255. xassert(n >= 2);
  256. for (j = 1; j <= n; j++) xassert(a[j] > 0.0);
  257. #if 1 /* ??? */
  258. xassert(b > -1e-5);
  259. #else
  260. xassert(b > 0.0);
  261. #endif
  262. xassert(u >= 0.0);
  263. for (j = 1; j <= n; j++) xassert(0.0 <= x[j] && x[j] <= 1.0);
  264. xassert(0.0 <= y && y <= u);
  265. /* try to generate mixed cover cut */
  266. if (cover2(n, a, b, u, x, y, cov, alfa, beta)) return 2;
  267. if (cover3(n, a, b, u, x, y, cov, alfa, beta)) return 3;
  268. if (cover4(n, a, b, u, x, y, cov, alfa, beta)) return 4;
  269. return 0;
  270. }
  271. /*----------------------------------------------------------------------
  272. -- lpx_cover_cut - generate mixed cover cut.
  273. --
  274. -- SYNOPSIS
  275. --
  276. -- #include "glplpx.h"
  277. -- int lpx_cover_cut(LPX *lp, int len, int ind[], double val[],
  278. -- double work[]);
  279. --
  280. -- DESCRIPTION
  281. --
  282. -- The routine lpx_cover_cut generates a mixed cover cut for a given
  283. -- row of the MIP problem.
  284. --
  285. -- The given row of the MIP problem should be explicitly specified in
  286. -- the form:
  287. --
  288. -- sum{j in J} a[j]*x[j] <= b. (1)
  289. --
  290. -- On entry indices (ordinal numbers) of structural variables, which
  291. -- have non-zero constraint coefficients, should be placed in locations
  292. -- ind[1], ..., ind[len], and corresponding constraint coefficients
  293. -- should be placed in locations val[1], ..., val[len]. The right-hand
  294. -- side b should be stored in location val[0].
  295. --
  296. -- The working array work should have at least nb locations, where nb
  297. -- is the number of binary variables in (1).
  298. --
  299. -- The routine generates a mixed cover cut in the same form as (1) and
  300. -- stores the cut coefficients and right-hand side in the same way as
  301. -- just described above.
  302. --
  303. -- RETURNS
  304. --
  305. -- If the cutting plane has been successfully generated, the routine
  306. -- returns 1 <= len' <= n, which is the number of non-zero coefficients
  307. -- in the inequality constraint. Otherwise, the routine returns zero. */
  308. static int lpx_cover_cut(LPX *lp, int len, int ind[], double val[],
  309. double work[])
  310. { int cov[1+4], j, k, nb, newlen, r;
  311. double f_min, f_max, alfa, beta, u, *x = work, y;
  312. /* substitute and remove fixed variables */
  313. newlen = 0;
  314. for (k = 1; k <= len; k++)
  315. { j = ind[k];
  316. if (lpx_get_col_type(lp, j) == LPX_FX)
  317. val[0] -= val[k] * lpx_get_col_lb(lp, j);
  318. else
  319. { newlen++;
  320. ind[newlen] = ind[k];
  321. val[newlen] = val[k];
  322. }
  323. }
  324. len = newlen;
  325. /* move binary variables to the beginning of the list so that
  326. elements 1, 2, ..., nb correspond to binary variables, and
  327. elements nb+1, nb+2, ..., len correspond to rest variables */
  328. nb = 0;
  329. for (k = 1; k <= len; k++)
  330. { j = ind[k];
  331. if (lpx_get_col_kind(lp, j) == LPX_IV &&
  332. lpx_get_col_type(lp, j) == LPX_DB &&
  333. lpx_get_col_lb(lp, j) == 0.0 &&
  334. lpx_get_col_ub(lp, j) == 1.0)
  335. { /* binary variable */
  336. int ind_k;
  337. double val_k;
  338. nb++;
  339. ind_k = ind[nb], val_k = val[nb];
  340. ind[nb] = ind[k], val[nb] = val[k];
  341. ind[k] = ind_k, val[k] = val_k;
  342. }
  343. }
  344. /* now the specified row has the form:
  345. sum a[j]*x[j] + sum a[j]*y[j] <= b,
  346. where x[j] are binary variables, y[j] are rest variables */
  347. /* at least two binary variables are needed */
  348. if (nb < 2) return 0;
  349. /* compute implied lower and upper bounds for sum a[j]*y[j] */
  350. f_min = f_max = 0.0;
  351. for (k = nb+1; k <= len; k++)
  352. { j = ind[k];
  353. /* both bounds must be finite */
  354. if (lpx_get_col_type(lp, j) != LPX_DB) return 0;
  355. if (val[k] > 0.0)
  356. { f_min += val[k] * lpx_get_col_lb(lp, j);
  357. f_max += val[k] * lpx_get_col_ub(lp, j);
  358. }
  359. else
  360. { f_min += val[k] * lpx_get_col_ub(lp, j);
  361. f_max += val[k] * lpx_get_col_lb(lp, j);
  362. }
  363. }
  364. /* sum a[j]*x[j] + sum a[j]*y[j] <= b ===>
  365. sum a[j]*x[j] + (sum a[j]*y[j] - f_min) <= b - f_min ===>
  366. sum a[j]*x[j] + y <= b - f_min,
  367. where y = sum a[j]*y[j] - f_min;
  368. note that 0 <= y <= u, u = f_max - f_min */
  369. /* determine upper bound of y */
  370. u = f_max - f_min;
  371. /* determine value of y at the current point */
  372. y = 0.0;
  373. for (k = nb+1; k <= len; k++)
  374. { j = ind[k];
  375. y += val[k] * lpx_get_col_prim(lp, j);
  376. }
  377. y -= f_min;
  378. if (y < 0.0) y = 0.0;
  379. if (y > u) y = u;
  380. /* modify the right-hand side b */
  381. val[0] -= f_min;
  382. /* now the transformed row has the form:
  383. sum a[j]*x[j] + y <= b, where 0 <= y <= u */
  384. /* determine values of x[j] at the current point */
  385. for (k = 1; k <= nb; k++)
  386. { j = ind[k];
  387. x[k] = lpx_get_col_prim(lp, j);
  388. if (x[k] < 0.0) x[k] = 0.0;
  389. if (x[k] > 1.0) x[k] = 1.0;
  390. }
  391. /* if a[j] < 0, replace x[j] by its complement 1 - x'[j] */
  392. for (k = 1; k <= nb; k++)
  393. { if (val[k] < 0.0)
  394. { ind[k] = - ind[k];
  395. val[k] = - val[k];
  396. val[0] += val[k];
  397. x[k] = 1.0 - x[k];
  398. }
  399. }
  400. /* try to generate a mixed cover cut for the transformed row */
  401. r = cover(nb, val, val[0], u, x, y, cov, &alfa, &beta);
  402. if (r == 0) return 0;
  403. xassert(2 <= r && r <= 4);
  404. /* now the cut is in the form:
  405. sum{j in C} x[j] + alfa * y <= beta */
  406. /* store the right-hand side beta */
  407. ind[0] = 0, val[0] = beta;
  408. /* restore the original ordinal numbers of x[j] */
  409. for (j = 1; j <= r; j++) cov[j] = ind[cov[j]];
  410. /* store cut coefficients at binary variables complementing back
  411. the variables having negative row coefficients */
  412. xassert(r <= nb);
  413. for (k = 1; k <= r; k++)
  414. { if (cov[k] > 0)
  415. { ind[k] = +cov[k];
  416. val[k] = +1.0;
  417. }
  418. else
  419. { ind[k] = -cov[k];
  420. val[k] = -1.0;
  421. val[0] -= 1.0;
  422. }
  423. }
  424. /* substitute y = sum a[j]*y[j] - f_min */
  425. for (k = nb+1; k <= len; k++)
  426. { r++;
  427. ind[r] = ind[k];
  428. val[r] = alfa * val[k];
  429. }
  430. val[0] += alfa * f_min;
  431. xassert(r <= len);
  432. len = r;
  433. return len;
  434. }
  435. /*----------------------------------------------------------------------
  436. -- lpx_eval_row - compute explictily specified row.
  437. --
  438. -- SYNOPSIS
  439. --
  440. -- #include "glplpx.h"
  441. -- double lpx_eval_row(LPX *lp, int len, int ind[], double val[]);
  442. --
  443. -- DESCRIPTION
  444. --
  445. -- The routine lpx_eval_row computes the primal value of an explicitly
  446. -- specified row using current values of structural variables.
  447. --
  448. -- The explicitly specified row may be thought as a linear form:
  449. --
  450. -- y = a[1]*x[m+1] + a[2]*x[m+2] + ... + a[n]*x[m+n],
  451. --
  452. -- where y is an auxiliary variable for this row, a[j] are coefficients
  453. -- of the linear form, x[m+j] are structural variables.
  454. --
  455. -- On entry column indices and numerical values of non-zero elements of
  456. -- the row should be stored in locations ind[1], ..., ind[len] and
  457. -- val[1], ..., val[len], where len is the number of non-zero elements.
  458. -- The array ind and val are not changed on exit.
  459. --
  460. -- RETURNS
  461. --
  462. -- The routine returns a computed value of y, the auxiliary variable of
  463. -- the specified row. */
  464. static double lpx_eval_row(LPX *lp, int len, int ind[], double val[])
  465. { int n = lpx_get_num_cols(lp);
  466. int j, k;
  467. double sum = 0.0;
  468. if (len < 0)
  469. xerror("lpx_eval_row: len = %d; invalid row length\n", len);
  470. for (k = 1; k <= len; k++)
  471. { j = ind[k];
  472. if (!(1 <= j && j <= n))
  473. xerror("lpx_eval_row: j = %d; column number out of range\n",
  474. j);
  475. sum += val[k] * lpx_get_col_prim(lp, j);
  476. }
  477. return sum;
  478. }
  479. /***********************************************************************
  480. * NAME
  481. *
  482. * ios_cov_gen - generate mixed cover cuts
  483. *
  484. * SYNOPSIS
  485. *
  486. * #include "glpios.h"
  487. * void ios_cov_gen(glp_tree *tree);
  488. *
  489. * DESCRIPTION
  490. *
  491. * The routine ios_cov_gen generates mixed cover cuts for the current
  492. * point and adds them to the cut pool. */
  493. void ios_cov_gen(glp_tree *tree)
  494. { glp_prob *prob = tree->mip;
  495. int m = lpx_get_num_rows(prob);
  496. int n = lpx_get_num_cols(prob);
  497. int i, k, type, kase, len, *ind;
  498. double r, *val, *work;
  499. xassert(lpx_get_status(prob) == LPX_OPT);
  500. /* allocate working arrays */
  501. ind = xcalloc(1+n, sizeof(int));
  502. val = xcalloc(1+n, sizeof(double));
  503. work = xcalloc(1+n, sizeof(double));
  504. /* look through all rows */
  505. for (i = 1; i <= m; i++)
  506. for (kase = 1; kase <= 2; kase++)
  507. { type = lpx_get_row_type(prob, i);
  508. if (kase == 1)
  509. { /* consider rows of '<=' type */
  510. if (!(type == LPX_UP || type == LPX_DB)) continue;
  511. len = lpx_get_mat_row(prob, i, ind, val);
  512. val[0] = lpx_get_row_ub(prob, i);
  513. }
  514. else
  515. { /* consider rows of '>=' type */
  516. if (!(type == LPX_LO || type == LPX_DB)) continue;
  517. len = lpx_get_mat_row(prob, i, ind, val);
  518. for (k = 1; k <= len; k++) val[k] = - val[k];
  519. val[0] = - lpx_get_row_lb(prob, i);
  520. }
  521. /* generate mixed cover cut:
  522. sum{j in J} a[j] * x[j] <= b */
  523. len = lpx_cover_cut(prob, len, ind, val, work);
  524. if (len == 0) continue;
  525. /* at the current point the cut inequality is violated, i.e.
  526. sum{j in J} a[j] * x[j] - b > 0 */
  527. r = lpx_eval_row(prob, len, ind, val) - val[0];
  528. if (r < 1e-3) continue;
  529. /* add the cut to the cut pool */
  530. glp_ios_add_row(tree, NULL, GLP_RF_COV, 0, len, ind, val,
  531. GLP_UP, val[0]);
  532. }
  533. /* free working arrays */
  534. xfree(ind);
  535. xfree(val);
  536. xfree(work);
  537. return;
  538. }
  539. /* eof */