| 1 | /* glpios11.c (process cuts stored in the local cut pool) */cxx . . $ `<file>/external/glpk/glpios11.c`/ |
| 2 | |
| 3 | /*********************************************************************** |
| 4 | * This code is part of GLPK (GNU Linear Programming Kit). |
| 5 | * |
| 6 | * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, |
| 7 | * 2009, 2010 Andrew Makhorin, Department for Applied Informatics, |
| 8 | * Moscow Aviation Institute, Moscow, Russia. All rights reserved. |
| 9 | * E-mail: <[email protected]>. |
| 10 | * |
| 11 | * GLPK is free software: you can redistribute it and/or modify it |
| 12 | * under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation, either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * GLPK is distributed in the hope that it will be useful, but WITHOUT |
| 17 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 18 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 19 | * License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with GLPK. If not, see <http://www.gnu.org/licenses/>. |
| 23 | ***********************************************************************/ |
| 24 | |
| 25 | #include "glpios.h"cxx . . $ `<file>/external/glpk/glpios.h`/ |
| 26 | |
| 27 | /*********************************************************************** |
| 28 | * NAME |
| 29 | * |
| 30 | * ios_process_cuts - process cuts stored in the local cut pool |
| 31 | * |
| 32 | * SYNOPSIS |
| 33 | * |
| 34 | * #include "glpios.h" |
| 35 | * void ios_process_cuts(glp_tree *T); |
| 36 | * |
| 37 | * DESCRIPTION |
| 38 | * |
| 39 | * The routine ios_process_cuts analyzes each cut currently stored in |
| 40 | * the local cut pool, which must be non-empty, and either adds the cut |
| 41 | * to the current subproblem or just discards it. All cuts are assumed |
| 42 | * to be locally valid. On exit the local cut pool remains unchanged. |
| 43 | * |
| 44 | * REFERENCES |
| 45 | * |
| 46 | * 1. E.Balas, S.Ceria, G.Cornuejols, "Mixed 0-1 Programming by |
| 47 | * Lift-and-Project in a Branch-and-Cut Framework", Management Sc., |
| 48 | * 42 (1996) 1229-1246. |
| 49 | * |
| 50 | * 2. G.Andreello, A.Caprara, and M.Fischetti, "Embedding Cuts in |
| 51 | * a Branch&Cut Framework: a Computational Study with {0,1/2}-Cuts", |
| 52 | * Preliminary Draft, October 28, 2003, pp.6-8. */ |
| 53 | |
| 54 | struct infocxx . . $ info# |
| 55 | { /* estimated cut efficiency */ |
| 56 | IOSCUTcxx . . $ IOSCUT# *cutcxx . . $ info#cut.; |
| 57 | /* pointer to cut in the cut pool */ |
| 58 | char flagcxx . . $ info#flag.; |
| 59 | /* if this flag is set, the cut is included into the current |
| 60 | subproblem */ |
| 61 | double effcxx . . $ info#eff.; |
| 62 | /* cut efficacy (normalized residual) */ |
| 63 | double degcxx . . $ info#deg.; |
| 64 | /* lower bound to objective degradation */ |
| 65 | }; |
| 66 | |
| 67 | static int fcmpcxx . . $ fcmp(9148c8c3ff42c9d8).(const void *arg1, const void *arg2) |
| 68 | { const struct infocxx . . $ info# *info1 = arg1, *info2 = arg2; |
| 69 | if (info1->degcxx . . $ info#deg. == 0.0 && info2->degcxx . . $ info#deg. == 0.0) |
| 70 | { if (info1->effcxx . . $ info#eff. > info2->effcxx . . $ info#eff.) return -1; |
| 71 | if (info1->effcxx . . $ info#eff. < info2->effcxx . . $ info#eff.) return +1; |
| 72 | } |
| 73 | else |
| 74 | { if (info1->degcxx . . $ info#deg. > info2->degcxx . . $ info#deg.) return -1; |
| 75 | if (info1->degcxx . . $ info#deg. < info2->degcxx . . $ info#deg.) return +1; |
| 76 | } |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | static double parallelcxx . . $ parallel(34c178a9d8c1b8eb).(IOSCUTcxx . . $ IOSCUT# *a, IOSCUTcxx . . $ IOSCUT# *b, double work[]); |
| 81 | |
| 82 | void ios_process_cutscxx . . $ `external/glpk/glpios.h:579:9`!(glp_treecxx . . $ glp_tree# *T) |
| 83 | { IOSPOOLcxx . . $ IOSPOOL# *pool; |
| 84 | IOSCUTcxx . . $ IOSCUT# *cut; |
| 85 | IOSAIJcxx . . $ IOSAIJ# *aij; |
| 86 | struct infocxx . . $ info# *info; |
| 87 | int k, kk, max_cuts, len, ret, *ind; |
| 88 | double *val, *work; |
| 89 | /* the current subproblem must exist */ |
| 90 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(T->curr != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 91 | /* the pool must exist and be non-empty */ |
| 92 | pool = T->localcxx . . $ glp_tree#local.; |
| 93 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(pool != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!); |
| 94 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(pool->size > 0); |
| 95 | /* allocate working arrays */ |
| 96 | info = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+pool->sizecxx . . $ IOSPOOL#size., sizeof(struct infocxx . . $ info#)); |
| 97 | ind = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+T->ncxx . . $ glp_tree#n., sizeof(int)); |
| 98 | val = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+T->ncxx . . $ glp_tree#n., sizeof(double)); |
| 99 | work = xcalloccxx . . $ `external/glpk/glpenv.h:171:9`!(1+T->ncxx . . $ glp_tree#n., sizeof(double)); |
| 100 | for (k = 1; k <= T->ncxx . . $ glp_tree#n.; k++) work[k] = 0.0; |
| 101 | /* build the list of cuts stored in the cut pool */ |
| 102 | for (k = 0, cut = pool->headcxx . . $ IOSPOOL#head.; cut != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; cut = cut->nextcxx . . $ IOSCUT#next.) |
| 103 | k++, info[k].cutcxx . . $ info#cut. = cut, info[k].flagcxx . . $ info#flag. = 0; |
| 104 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(k == pool->size); |
| 105 | /* estimate efficiency of all cuts in the cut pool */ |
| 106 | for (k = 1; k <= pool->sizecxx . . $ IOSPOOL#size.; k++) |
| 107 | { double temp, dy, dz; |
| 108 | cut = info[k].cutcxx . . $ info#cut.; |
| 109 | /* build the vector of cut coefficients and compute its |
| 110 | Euclidean norm */ |
| 111 | len = 0; temp = 0.0; |
| 112 | for (aij = cut->ptrcxx . . $ IOSCUT#ptr.; aij != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; aij = aij->nextcxx . . $ IOSAIJ#next.) |
| 113 | { xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(1 <= aij->j && aij->j <= T->n); |
| 114 | len++, ind[len] = aij->jcxx . . $ IOSAIJ#j., val[len] = aij->valcxx . . $ IOSAIJ#val.; |
| 115 | temp += aij->valcxx . . $ IOSAIJ#val. * aij->valcxx . . $ IOSAIJ#val.; |
| 116 | } |
| 117 | if (temp < DBL_EPSILONcxx . . $ `<external>/18143172452841944454/float.h:114:9`! * DBL_EPSILONcxx . . $ `<external>/18143172452841944454/float.h:114:9`!) temp = DBL_EPSILONcxx . . $ `<external>/18143172452841944454/float.h:114:9`!; |
| 118 | /* transform the cut to express it only through non-basic |
| 119 | (auxiliary and structural) variables */ |
| 120 | len = glp_transform_rowcxx . . $ glp_transform_row(223ff0a4a32a5807).(T->mipcxx . . $ glp_tree#mip., len, ind, val); |
| 121 | /* determine change in the cut value and in the objective |
| 122 | value for the adjacent basis by simulating one step of the |
| 123 | dual simplex */ |
| 124 | ret = _glp_analyze_rowcxx . . $ _glp_analyze_row(6a48674ece66b1bb).(T->mipcxx . . $ glp_tree#mip., len, ind, val, cut->typecxx . . $ IOSCUT#type., |
| 125 | cut->rhscxx . . $ IOSCUT#rhs., 1e-9, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!, &dy, &dz); |
| 126 | /* determine normalized residual and lower bound to objective |
| 127 | degradation */ |
| 128 | if (ret == 0) |
| 129 | { info[k].effcxx . . $ info#eff. = fabs(dy) / sqrt(temp); |
| 130 | /* if some reduced costs violates (slightly) their zero |
| 131 | bounds (i.e. have wrong signs) due to round-off errors, |
| 132 | dz also may have wrong sign being close to zero */ |
| 133 | if (T->mipcxx . . $ glp_tree#mip.->dircxx . . $ glp_prob#dir. == GLP_MINcxx . . $ `external/glpk/glpk.h:50:9`!) |
| 134 | { if (dz < 0.0) dz = 0.0; |
| 135 | info[k].degcxx . . $ info#deg. = + dz; |
| 136 | } |
| 137 | else /* GLP_MAX */ |
| 138 | { if (dz > 0.0) dz = 0.0; |
| 139 | info[k].degcxx . . $ info#deg. = - dz; |
| 140 | } |
| 141 | } |
| 142 | else if (ret == 1) |
| 143 | { /* the constraint is not violated at the current point */ |
| 144 | info[k].effcxx . . $ info#eff. = info[k].degcxx . . $ info#deg. = 0.0; |
| 145 | } |
| 146 | else if (ret == 2) |
| 147 | { /* no dual feasible adjacent basis exists */ |
| 148 | info[k].effcxx . . $ info#eff. = 1.0; |
| 149 | info[k].degcxx . . $ info#deg. = DBL_MAXcxx . . $ `<external>/18143172452841944454/float.h:105:9`!; |
| 150 | } |
| 151 | else |
| 152 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(ret != ret); |
| 153 | /* if the degradation is too small, just ignore it */ |
| 154 | if (info[k].degcxx . . $ info#deg. < 0.01) info[k].degcxx . . $ info#deg. = 0.0; |
| 155 | } |
| 156 | /* sort the list of cuts by decreasing objective degradation and |
| 157 | then by decreasing efficacy */ |
| 158 | qsortcxx . . $ qsort(ebb60b13a65e77d7).(&info[1], pool->sizecxx . . $ IOSPOOL#size., sizeof(struct infocxx . . $ info#), fcmpcxx . . $ fcmp(9148c8c3ff42c9d8).); |
| 159 | /* only first (most efficient) max_cuts in the list are qualified |
| 160 | as candidates to be added to the current subproblem */ |
| 161 | max_cuts = (T->currcxx . . $ glp_tree#curr.->levelcxx . . $ IOSNPD#level. == 0 ? 90 : 10); |
| 162 | if (max_cuts > pool->sizecxx . . $ IOSPOOL#size.) max_cuts = pool->sizecxx . . $ IOSPOOL#size.; |
| 163 | /* add cuts to the current subproblem */ |
| 164 | #if 0 |
| 165 | xprintf("*** adding cuts ***\n"); |
| 166 | #endif |
| 167 | for (k = 1; k <= max_cuts; k++) |
| 168 | { int i, len; |
| 169 | /* if this cut seems to be inefficient, skip it */ |
| 170 | if (info[k].degcxx . . $ info#deg. < 0.01 && info[k].effcxx . . $ info#eff. < 0.01) continue; |
| 171 | /* if the angle between this cut and every other cut included |
| 172 | in the current subproblem is small, skip this cut */ |
| 173 | for (kk = 1; kk < k; kk++) |
| 174 | { if (info[kk].flagcxx . . $ info#flag.) |
| 175 | { if (parallelcxx . . $ parallel(34c178a9d8c1b8eb).(info[k].cutcxx . . $ info#cut., info[kk].cutcxx . . $ info#cut., work) > 0.90) |
| 176 | break; |
| 177 | } |
| 178 | } |
| 179 | if (kk < k) continue; |
| 180 | /* add this cut to the current subproblem */ |
| 181 | #if 0 |
| 182 | xprintf("eff = %g; deg = %g\n", info[k].eff, info[k].deg); |
| 183 | #endif |
| 184 | cut = info[k].cutcxx . . $ info#cut., info[k].flagcxx . . $ info#flag. = 1; |
| 185 | i = glp_add_rowscxx . . $ glp_add_rows(423829e847ef1a83).(T->mipcxx . . $ glp_tree#mip., 1); |
| 186 | if (cut->namecxx . . $ IOSCUT#name. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) |
| 187 | glp_set_row_namecxx . . $ glp_set_row_name(7764df469f6d9ace).(T->mipcxx . . $ glp_tree#mip., i, cut->namecxx . . $ IOSCUT#name.); |
| 188 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(T->mip->row[i]->origin == GLP_RF_CUTcxx . . $ `external/glpk/glpk.h:209:9`!); |
| 189 | T->mipcxx . . $ glp_tree#mip.->rowcxx . . $ glp_prob#row.[i]->klasscxx . . $ GLPROW#klass. = cut->klasscxx . . $ IOSCUT#klass.; |
| 190 | len = 0; |
| 191 | for (aij = cut->ptrcxx . . $ IOSCUT#ptr.; aij != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; aij = aij->nextcxx . . $ IOSAIJ#next.) |
| 192 | len++, ind[len] = aij->jcxx . . $ IOSAIJ#j., val[len] = aij->valcxx . . $ IOSAIJ#val.; |
| 193 | glp_set_mat_rowcxx . . $ glp_set_mat_row(9cc64c3737aa01c2).(T->mipcxx . . $ glp_tree#mip., i, len, ind, val); |
| 194 | xassertcxx . . $ `external/glpk/glpenv.h:162:9`!(cut->type == GLP_LOcxx . . $ `external/glpk/glpk.h:60:9`! || cut->type == GLP_UPcxx . . $ `external/glpk/glpk.h:61:9`!); |
| 195 | glp_set_row_bndscxx . . $ glp_set_row_bnds(9376c6e48e598b7a).(T->mipcxx . . $ glp_tree#mip., i, cut->typecxx . . $ IOSCUT#type., cut->rhscxx . . $ IOSCUT#rhs., cut->rhscxx . . $ IOSCUT#rhs.); |
| 196 | } |
| 197 | /* free working arrays */ |
| 198 | xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(info); |
| 199 | xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(ind); |
| 200 | xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(val); |
| 201 | xfreecxx . . $ `external/glpk/glpenv.h:175:9`!(work); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | #if 0 |
| 206 | /*********************************************************************** |
| 207 | * Given a cut a * x >= b (<= b) the routine efficacy computes the cut |
| 208 | * efficacy as follows: |
| 209 | * |
| 210 | * eff = d * (a * x~ - b) / ||a||, |
| 211 | * |
| 212 | * where d is -1 (in case of '>= b') or +1 (in case of '<= b'), x~ is |
| 213 | * the vector of values of structural variables in optimal solution to |
| 214 | * LP relaxation of the current subproblem, ||a|| is the Euclidean norm |
| 215 | * of the vector of cut coefficients. |
| 216 | * |
| 217 | * If the cut is violated at point x~, the efficacy eff is positive, |
| 218 | * and its value is the Euclidean distance between x~ and the cut plane |
| 219 | * a * x = b in the space of structural variables. |
| 220 | * |
| 221 | * Following geometrical intuition, it is quite natural to consider |
| 222 | * this distance as a first-order measure of the expected efficacy of |
| 223 | * the cut: the larger the distance the better the cut [1]. */ |
| 224 | |
| 225 | static double efficacy(glp_tree *T, IOSCUT *cut) |
| 226 | { glp_prob *mip = T->mip; |
| 227 | IOSAIJ *aij; |
| 228 | double s = 0.0, t = 0.0, temp; |
| 229 | for (aij = cut->ptr; aij != NULL; aij = aij->next) |
| 230 | { xassert(1 <= aij->j && aij->j <= mip->n); |
| 231 | s += aij->val * mip->col[aij->j]->prim; |
| 232 | t += aij->val * aij->val; |
| 233 | } |
| 234 | temp = sqrt(t); |
| 235 | if (temp < DBL_EPSILON) temp = DBL_EPSILON; |
| 236 | if (cut->type == GLP_LO) |
| 237 | temp = (s >= cut->rhs ? 0.0 : (cut->rhs - s) / temp); |
| 238 | else if (cut->type == GLP_UP) |
| 239 | temp = (s <= cut->rhs ? 0.0 : (s - cut->rhs) / temp); |
| 240 | else |
| 241 | xassert(cut != cut); |
| 242 | return temp; |
| 243 | } |
| 244 | #endif |
| 245 | |
| 246 | /*********************************************************************** |
| 247 | * Given two cuts a1 * x >= b1 (<= b1) and a2 * x >= b2 (<= b2) the |
| 248 | * routine parallel computes the cosine of angle between the cut planes |
| 249 | * a1 * x = b1 and a2 * x = b2 (which is the acute angle between two |
| 250 | * normals to these planes) in the space of structural variables as |
| 251 | * follows: |
| 252 | * |
| 253 | * cos phi = (a1' * a2) / (||a1|| * ||a2||), |
| 254 | * |
| 255 | * where (a1' * a2) is a dot product of vectors of cut coefficients, |
| 256 | * ||a1|| and ||a2|| are Euclidean norms of vectors a1 and a2. |
| 257 | * |
| 258 | * Note that requirement cos phi = 0 forces the cuts to be orthogonal, |
| 259 | * i.e. with disjoint support, while requirement cos phi <= 0.999 means |
| 260 | * only avoiding duplicate (parallel) cuts [1]. */ |
| 261 | |
| 262 | static double parallelcxx . . $ parallel(34c178a9d8c1b8eb).(IOSCUTcxx . . $ IOSCUT# *a, IOSCUTcxx . . $ IOSCUT# *b, double work[]) |
| 263 | { IOSAIJcxx . . $ IOSAIJ# *aij; |
| 264 | double s = 0.0, sa = 0.0, sb = 0.0, temp; |
| 265 | for (aij = a->ptrcxx . . $ IOSCUT#ptr.; aij != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; aij = aij->nextcxx . . $ IOSAIJ#next.) |
| 266 | { work[aij->jcxx . . $ IOSAIJ#j.] = aij->valcxx . . $ IOSAIJ#val.; |
| 267 | sa += aij->valcxx . . $ IOSAIJ#val. * aij->valcxx . . $ IOSAIJ#val.; |
| 268 | } |
| 269 | for (aij = b->ptrcxx . . $ IOSCUT#ptr.; aij != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; aij = aij->nextcxx . . $ IOSAIJ#next.) |
| 270 | { s += work[aij->jcxx . . $ IOSAIJ#j.] * aij->valcxx . . $ IOSAIJ#val.; |
| 271 | sb += aij->valcxx . . $ IOSAIJ#val. * aij->valcxx . . $ IOSAIJ#val.; |
| 272 | } |
| 273 | for (aij = a->ptrcxx . . $ IOSCUT#ptr.; aij != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!; aij = aij->nextcxx . . $ IOSAIJ#next.) |
| 274 | work[aij->jcxx . . $ IOSAIJ#j.] = 0.0; |
| 275 | temp = sqrt(sa) * sqrt(sb); |
| 276 | if (temp < DBL_EPSILONcxx . . $ `<external>/18143172452841944454/float.h:114:9`! * DBL_EPSILONcxx . . $ `<external>/18143172452841944454/float.h:114:9`!) temp = DBL_EPSILONcxx . . $ `<external>/18143172452841944454/float.h:114:9`!; |
| 277 | return s / temp; |
| 278 | } |
| 279 | |
| 280 | /* eof */ |