/farmR/src/glpios05.c

https://code.google.com/p/javawfm/ · C · 280 lines · 175 code · 11 blank · 94 comment · 43 complexity · 797a8bab4cca3ffb9510ac0755d78313 MD5 · raw file

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