glpios11.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /* glpios11.c (process cuts stored in the local cut pool) */
  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_process_cuts - process cuts stored in the local cut pool
  28. *
  29. * SYNOPSIS
  30. *
  31. * #include "glpios.h"
  32. * void ios_process_cuts(glp_tree *T);
  33. *
  34. * DESCRIPTION
  35. *
  36. * The routine ios_process_cuts analyzes each cut currently stored in
  37. * the local cut pool, which must be non-empty, and either adds the cut
  38. * to the current subproblem or just discards it. All cuts are assumed
  39. * to be locally valid. On exit the local cut pool remains unchanged.
  40. *
  41. * REFERENCES
  42. *
  43. * 1. E.Balas, S.Ceria, G.Cornuejols, "Mixed 0-1 Programming by
  44. * Lift-and-Project in a Branch-and-Cut Framework", Management Sc.,
  45. * 42 (1996) 1229-1246.
  46. *
  47. * 2. G.Andreello, A.Caprara, and M.Fischetti, "Embedding Cuts in
  48. * a Branch&Cut Framework: a Computational Study with {0,1/2}-Cuts",
  49. * Preliminary Draft, October 28, 2003, pp.6-8. */
  50. struct info
  51. { /* estimated cut efficiency */
  52. IOSCUT *cut;
  53. /* pointer to cut in the cut pool */
  54. char flag;
  55. /* if this flag is set, the cut is included into the current
  56. subproblem */
  57. double eff;
  58. /* cut efficacy (normalized residual) */
  59. double deg;
  60. /* lower bound to objective degradation */
  61. };
  62. static int fcmp(const void *arg1, const void *arg2)
  63. { const struct info *info1 = arg1, *info2 = arg2;
  64. if (info1->deg == 0.0 && info2->deg == 0.0)
  65. { if (info1->eff > info2->eff) return -1;
  66. if (info1->eff < info2->eff) return +1;
  67. }
  68. else
  69. { if (info1->deg > info2->deg) return -1;
  70. if (info1->deg < info2->deg) return +1;
  71. }
  72. return 0;
  73. }
  74. static double parallel(IOSCUT *a, IOSCUT *b, double work[]);
  75. void ios_process_cuts(glp_tree *T)
  76. { IOSPOOL *pool;
  77. IOSCUT *cut;
  78. IOSAIJ *aij;
  79. struct info *info;
  80. int k, kk, max_cuts, len, ret, *ind;
  81. double *val, *work;
  82. /* the current subproblem must exist */
  83. xassert(T->curr != NULL);
  84. /* the pool must exist and be non-empty */
  85. pool = T->local;
  86. xassert(pool != NULL);
  87. xassert(pool->size > 0);
  88. /* allocate working arrays */
  89. info = xcalloc(1+pool->size, sizeof(struct info));
  90. ind = xcalloc(1+T->n, sizeof(int));
  91. val = xcalloc(1+T->n, sizeof(double));
  92. work = xcalloc(1+T->n, sizeof(double));
  93. for (k = 1; k <= T->n; k++) work[k] = 0.0;
  94. /* build the list of cuts stored in the cut pool */
  95. for (k = 0, cut = pool->head; cut != NULL; cut = cut->next)
  96. k++, info[k].cut = cut, info[k].flag = 0;
  97. xassert(k == pool->size);
  98. /* estimate efficiency of all cuts in the cut pool */
  99. for (k = 1; k <= pool->size; k++)
  100. { double temp, dy, dz;
  101. cut = info[k].cut;
  102. /* build the vector of cut coefficients and compute its
  103. Euclidean norm */
  104. len = 0; temp = 0.0;
  105. for (aij = cut->ptr; aij != NULL; aij = aij->next)
  106. { xassert(1 <= aij->j && aij->j <= T->n);
  107. len++, ind[len] = aij->j, val[len] = aij->val;
  108. temp += aij->val * aij->val;
  109. }
  110. if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
  111. /* transform the cut to express it only through non-basic
  112. (auxiliary and structural) variables */
  113. len = glp_transform_row(T->mip, len, ind, val);
  114. /* determine change in the cut value and in the objective
  115. value for the adjacent basis by simulating one step of the
  116. dual simplex */
  117. ret = _glp_analyze_row(T->mip, len, ind, val, cut->type,
  118. cut->rhs, 1e-9, NULL, NULL, NULL, NULL, &dy, &dz);
  119. /* determine normalized residual and lower bound to objective
  120. degradation */
  121. if (ret == 0)
  122. { info[k].eff = fabs(dy) / sqrt(temp);
  123. /* if some reduced costs violates (slightly) their zero
  124. bounds (i.e. have wrong signs) due to round-off errors,
  125. dz also may have wrong sign being close to zero */
  126. if (T->mip->dir == GLP_MIN)
  127. { if (dz < 0.0) dz = 0.0;
  128. info[k].deg = + dz;
  129. }
  130. else /* GLP_MAX */
  131. { if (dz > 0.0) dz = 0.0;
  132. info[k].deg = - dz;
  133. }
  134. }
  135. else if (ret == 1)
  136. { /* the constraint is not violated at the current point */
  137. info[k].eff = info[k].deg = 0.0;
  138. }
  139. else if (ret == 2)
  140. { /* no dual feasible adjacent basis exists */
  141. info[k].eff = 1.0;
  142. info[k].deg = DBL_MAX;
  143. }
  144. else
  145. xassert(ret != ret);
  146. /* if the degradation is too small, just ignore it */
  147. if (info[k].deg < 0.01) info[k].deg = 0.0;
  148. }
  149. /* sort the list of cuts by decreasing objective degradation and
  150. then by decreasing efficacy */
  151. qsort(&info[1], pool->size, sizeof(struct info), fcmp);
  152. /* only first (most efficient) max_cuts in the list are qualified
  153. as candidates to be added to the current subproblem */
  154. max_cuts = (T->curr->level == 0 ? 90 : 10);
  155. if (max_cuts > pool->size) max_cuts = pool->size;
  156. /* add cuts to the current subproblem */
  157. #if 0
  158. xprintf("*** adding cuts ***\n");
  159. #endif
  160. for (k = 1; k <= max_cuts; k++)
  161. { int i, len;
  162. /* if this cut seems to be inefficient, skip it */
  163. if (info[k].deg < 0.01 && info[k].eff < 0.01) continue;
  164. /* if the angle between this cut and every other cut included
  165. in the current subproblem is small, skip this cut */
  166. for (kk = 1; kk < k; kk++)
  167. { if (info[kk].flag)
  168. { if (parallel(info[k].cut, info[kk].cut, work) > 0.90)
  169. break;
  170. }
  171. }
  172. if (kk < k) continue;
  173. /* add this cut to the current subproblem */
  174. #if 0
  175. xprintf("eff = %g; deg = %g\n", info[k].eff, info[k].deg);
  176. #endif
  177. cut = info[k].cut, info[k].flag = 1;
  178. i = glp_add_rows(T->mip, 1);
  179. if (cut->name != NULL)
  180. glp_set_row_name(T->mip, i, cut->name);
  181. xassert(T->mip->row[i]->origin == GLP_RF_CUT);
  182. T->mip->row[i]->klass = cut->klass;
  183. len = 0;
  184. for (aij = cut->ptr; aij != NULL; aij = aij->next)
  185. len++, ind[len] = aij->j, val[len] = aij->val;
  186. glp_set_mat_row(T->mip, i, len, ind, val);
  187. xassert(cut->type == GLP_LO || cut->type == GLP_UP);
  188. glp_set_row_bnds(T->mip, i, cut->type, cut->rhs, cut->rhs);
  189. }
  190. /* free working arrays */
  191. xfree(info);
  192. xfree(ind);
  193. xfree(val);
  194. xfree(work);
  195. return;
  196. }
  197. #if 0
  198. /***********************************************************************
  199. * Given a cut a * x >= b (<= b) the routine efficacy computes the cut
  200. * efficacy as follows:
  201. *
  202. * eff = d * (a * x~ - b) / ||a||,
  203. *
  204. * where d is -1 (in case of '>= b') or +1 (in case of '<= b'), x~ is
  205. * the vector of values of structural variables in optimal solution to
  206. * LP relaxation of the current subproblem, ||a|| is the Euclidean norm
  207. * of the vector of cut coefficients.
  208. *
  209. * If the cut is violated at point x~, the efficacy eff is positive,
  210. * and its value is the Euclidean distance between x~ and the cut plane
  211. * a * x = b in the space of structural variables.
  212. *
  213. * Following geometrical intuition, it is quite natural to consider
  214. * this distance as a first-order measure of the expected efficacy of
  215. * the cut: the larger the distance the better the cut [1]. */
  216. static double efficacy(glp_tree *T, IOSCUT *cut)
  217. { glp_prob *mip = T->mip;
  218. IOSAIJ *aij;
  219. double s = 0.0, t = 0.0, temp;
  220. for (aij = cut->ptr; aij != NULL; aij = aij->next)
  221. { xassert(1 <= aij->j && aij->j <= mip->n);
  222. s += aij->val * mip->col[aij->j]->prim;
  223. t += aij->val * aij->val;
  224. }
  225. temp = sqrt(t);
  226. if (temp < DBL_EPSILON) temp = DBL_EPSILON;
  227. if (cut->type == GLP_LO)
  228. temp = (s >= cut->rhs ? 0.0 : (cut->rhs - s) / temp);
  229. else if (cut->type == GLP_UP)
  230. temp = (s <= cut->rhs ? 0.0 : (s - cut->rhs) / temp);
  231. else
  232. xassert(cut != cut);
  233. return temp;
  234. }
  235. #endif
  236. /***********************************************************************
  237. * Given two cuts a1 * x >= b1 (<= b1) and a2 * x >= b2 (<= b2) the
  238. * routine parallel computes the cosine of angle between the cut planes
  239. * a1 * x = b1 and a2 * x = b2 (which is the acute angle between two
  240. * normals to these planes) in the space of structural variables as
  241. * follows:
  242. *
  243. * cos phi = (a1' * a2) / (||a1|| * ||a2||),
  244. *
  245. * where (a1' * a2) is a dot product of vectors of cut coefficients,
  246. * ||a1|| and ||a2|| are Euclidean norms of vectors a1 and a2.
  247. *
  248. * Note that requirement cos phi = 0 forces the cuts to be orthogonal,
  249. * i.e. with disjoint support, while requirement cos phi <= 0.999 means
  250. * only avoiding duplicate (parallel) cuts [1]. */
  251. static double parallel(IOSCUT *a, IOSCUT *b, double work[])
  252. { IOSAIJ *aij;
  253. double s = 0.0, sa = 0.0, sb = 0.0, temp;
  254. for (aij = a->ptr; aij != NULL; aij = aij->next)
  255. { work[aij->j] = aij->val;
  256. sa += aij->val * aij->val;
  257. }
  258. for (aij = b->ptr; aij != NULL; aij = aij->next)
  259. { s += work[aij->j] * aij->val;
  260. sb += aij->val * aij->val;
  261. }
  262. for (aij = a->ptr; aij != NULL; aij = aij->next)
  263. work[aij->j] = 0.0;
  264. temp = sqrt(sa) * sqrt(sb);
  265. if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
  266. return s / temp;
  267. }
  268. /* eof */