glpios05.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* glpios05.c (Gomory's mixed integer 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. * NAME
  26. *
  27. * ios_gmi_gen - generate Gomory's mixed integer cuts.
  28. *
  29. * SYNOPSIS
  30. *
  31. * #include "glpios.h"
  32. * void ios_gmi_gen(glp_tree *tree, IOSPOOL *pool);
  33. *
  34. * DESCRIPTION
  35. *
  36. * The routine ios_gmi_gen generates Gomory's mixed integer cuts for
  37. * the current point and adds them to the cut pool. */
  38. #define MAXCUTS 50
  39. /* maximal number of cuts to be generated for one round */
  40. struct worka
  41. { /* Gomory's cut generator working area */
  42. int *ind; /* int ind[1+n]; */
  43. double *val; /* double val[1+n]; */
  44. double *phi; /* double phi[1+m+n]; */
  45. };
  46. #define f(x) ((x) - floor(x))
  47. /* compute fractional part of x */
  48. static void gen_cut(glp_tree *tree, struct worka *worka, int j)
  49. { /* this routine tries to generate Gomory's mixed integer cut for
  50. specified structural variable x[m+j] of integer kind, which is
  51. basic and has fractional value in optimal solution to current
  52. LP relaxation */
  53. glp_prob *mip = tree->mip;
  54. int m = mip->m;
  55. int n = mip->n;
  56. int *ind = worka->ind;
  57. double *val = worka->val;
  58. double *phi = worka->phi;
  59. int i, k, len, kind, stat;
  60. double lb, ub, alfa, beta, ksi, phi1, rhs;
  61. /* compute row of the simplex tableau, which (row) corresponds
  62. to specified basic variable xB[i] = x[m+j]; see (23) */
  63. len = glp_eval_tab_row(mip, m+j, ind, val);
  64. /* determine beta[i], which a value of xB[i] in optimal solution
  65. to current LP relaxation; note that this value is the same as
  66. if it would be computed with formula (27); it is assumed that
  67. beta[i] is fractional enough */
  68. beta = mip->col[j]->prim;
  69. /* compute cut coefficients phi and right-hand side rho, which
  70. correspond to formula (30); dense format is used, because rows
  71. of the simplex tableau is usually dense */
  72. for (k = 1; k <= m+n; k++) phi[k] = 0.0;
  73. rhs = f(beta); /* initial value of rho; see (28), (32) */
  74. for (j = 1; j <= len; j++)
  75. { /* determine original number of non-basic variable xN[j] */
  76. k = ind[j];
  77. xassert(1 <= k && k <= m+n);
  78. /* determine the kind, bounds and current status of xN[j] in
  79. optimal solution to LP relaxation */
  80. if (k <= m)
  81. { /* auxiliary variable */
  82. GLPROW *row = mip->row[k];
  83. kind = GLP_CV;
  84. lb = row->lb;
  85. ub = row->ub;
  86. stat = row->stat;
  87. }
  88. else
  89. { /* structural variable */
  90. GLPCOL *col = mip->col[k-m];
  91. kind = col->kind;
  92. lb = col->lb;
  93. ub = col->ub;
  94. stat = col->stat;
  95. }
  96. /* xN[j] cannot be basic */
  97. xassert(stat != GLP_BS);
  98. /* determine row coefficient ksi[i,j] at xN[j]; see (23) */
  99. ksi = val[j];
  100. /* if ksi[i,j] is too large in the magnitude, do not generate
  101. the cut */
  102. if (fabs(ksi) > 1e+05) goto fini;
  103. /* if ksi[i,j] is too small in the magnitude, skip it */
  104. if (fabs(ksi) < 1e-10) goto skip;
  105. /* compute row coefficient alfa[i,j] at y[j]; see (26) */
  106. switch (stat)
  107. { case GLP_NF:
  108. /* xN[j] is free (unbounded) having non-zero ksi[i,j];
  109. do not generate the cut */
  110. goto fini;
  111. case GLP_NL:
  112. /* xN[j] has active lower bound */
  113. alfa = - ksi;
  114. break;
  115. case GLP_NU:
  116. /* xN[j] has active upper bound */
  117. alfa = + ksi;
  118. break;
  119. case GLP_NS:
  120. /* xN[j] is fixed; skip it */
  121. goto skip;
  122. default:
  123. xassert(stat != stat);
  124. }
  125. /* compute cut coefficient phi'[j] at y[j]; see (21), (28) */
  126. switch (kind)
  127. { case GLP_IV:
  128. /* y[j] is integer */
  129. if (fabs(alfa - floor(alfa + 0.5)) < 1e-10)
  130. { /* alfa[i,j] is close to nearest integer; skip it */
  131. goto skip;
  132. }
  133. else if (f(alfa) <= f(beta))
  134. phi1 = f(alfa);
  135. else
  136. phi1 = (f(beta) / (1.0 - f(beta))) * (1.0 - f(alfa));
  137. break;
  138. case GLP_CV:
  139. /* y[j] is continuous */
  140. if (alfa >= 0.0)
  141. phi1 = + alfa;
  142. else
  143. phi1 = (f(beta) / (1.0 - f(beta))) * (- alfa);
  144. break;
  145. default:
  146. xassert(kind != kind);
  147. }
  148. /* compute cut coefficient phi[j] at xN[j] and update right-
  149. hand side rho; see (31), (32) */
  150. switch (stat)
  151. { case GLP_NL:
  152. /* xN[j] has active lower bound */
  153. phi[k] = + phi1;
  154. rhs += phi1 * lb;
  155. break;
  156. case GLP_NU:
  157. /* xN[j] has active upper bound */
  158. phi[k] = - phi1;
  159. rhs -= phi1 * ub;
  160. break;
  161. default:
  162. xassert(stat != stat);
  163. }
  164. skip: ;
  165. }
  166. /* now the cut has the form sum_k phi[k] * x[k] >= rho, where cut
  167. coefficients are stored in the array phi in dense format;
  168. x[1,...,m] are auxiliary variables, x[m+1,...,m+n] are struc-
  169. tural variables; see (30) */
  170. /* eliminate auxiliary variables in order to express the cut only
  171. through structural variables; see (33) */
  172. for (i = 1; i <= m; i++)
  173. { GLPROW *row;
  174. GLPAIJ *aij;
  175. if (fabs(phi[i]) < 1e-10) continue;
  176. /* auxiliary variable x[i] has non-zero cut coefficient */
  177. row = mip->row[i];
  178. /* x[i] cannot be fixed */
  179. xassert(row->type != GLP_FX);
  180. /* substitute x[i] = sum_j a[i,j] * x[m+j] */
  181. for (aij = row->ptr; aij != NULL; aij = aij->r_next)
  182. phi[m+aij->col->j] += phi[i] * aij->val;
  183. }
  184. /* convert the final cut to sparse format and substitute fixed
  185. (structural) variables */
  186. len = 0;
  187. for (j = 1; j <= n; j++)
  188. { GLPCOL *col;
  189. if (fabs(phi[m+j]) < 1e-10) continue;
  190. /* structural variable x[m+j] has non-zero cut coefficient */
  191. col = mip->col[j];
  192. if (col->type == GLP_FX)
  193. { /* eliminate x[m+j] */
  194. rhs -= phi[m+j] * col->lb;
  195. }
  196. else
  197. { len++;
  198. ind[len] = j;
  199. val[len] = phi[m+j];
  200. }
  201. }
  202. if (fabs(rhs) < 1e-12) rhs = 0.0;
  203. /* if the cut inequality seems to be badly scaled, reject it to
  204. avoid numeric difficulties */
  205. for (k = 1; k <= len; k++)
  206. { if (fabs(val[k]) < 1e-03) goto fini;
  207. if (fabs(val[k]) > 1e+03) goto fini;
  208. }
  209. /* add the cut to the cut pool for further consideration */
  210. #if 0
  211. ios_add_cut_row(tree, pool, GLP_RF_GMI, len, ind, val, GLP_LO,
  212. rhs);
  213. #else
  214. glp_ios_add_row(tree, NULL, GLP_RF_GMI, 0, len, ind, val, GLP_LO,
  215. rhs);
  216. #endif
  217. fini: return;
  218. }
  219. struct var { int j; double f; };
  220. static int fcmp(const void *p1, const void *p2)
  221. { const struct var *v1 = p1, *v2 = p2;
  222. if (v1->f > v2->f) return -1;
  223. if (v1->f < v2->f) return +1;
  224. return 0;
  225. }
  226. void ios_gmi_gen(glp_tree *tree)
  227. { /* main routine to generate Gomory's cuts */
  228. glp_prob *mip = tree->mip;
  229. int m = mip->m;
  230. int n = mip->n;
  231. struct var *var;
  232. int k, nv, j, size;
  233. struct worka _worka, *worka = &_worka;
  234. /* allocate working arrays */
  235. var = xcalloc(1+n, sizeof(struct var));
  236. worka->ind = xcalloc(1+n, sizeof(int));
  237. worka->val = xcalloc(1+n, sizeof(double));
  238. worka->phi = xcalloc(1+m+n, sizeof(double));
  239. /* build the list of integer structural variables, which are
  240. basic and have fractional value in optimal solution to current
  241. LP relaxation */
  242. nv = 0;
  243. for (j = 1; j <= n; j++)
  244. { GLPCOL *col = mip->col[j];
  245. double frac;
  246. if (col->kind != GLP_IV) continue;
  247. if (col->type == GLP_FX) continue;
  248. if (col->stat != GLP_BS) continue;
  249. frac = f(col->prim);
  250. if (!(0.05 <= frac && frac <= 0.95)) continue;
  251. /* add variable to the list */
  252. nv++, var[nv].j = j, var[nv].f = frac;
  253. }
  254. /* order the list by descending fractionality */
  255. qsort(&var[1], nv, sizeof(struct var), fcmp);
  256. /* try to generate cuts by one for each variable in the list, but
  257. not more than MAXCUTS cuts */
  258. size = glp_ios_pool_size(tree);
  259. for (k = 1; k <= nv; k++)
  260. { if (glp_ios_pool_size(tree) - size >= MAXCUTS) break;
  261. gen_cut(tree, worka, var[k].j);
  262. }
  263. /* free working arrays */
  264. xfree(var);
  265. xfree(worka->ind);
  266. xfree(worka->val);
  267. xfree(worka->phi);
  268. return;
  269. }
  270. /* eof */