alpar@9: /* glpios11.c (process cuts stored in the local cut pool) */ alpar@9: alpar@9: /*********************************************************************** alpar@9: * This code is part of GLPK (GNU Linear Programming Kit). alpar@9: * alpar@9: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@9: * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics, alpar@9: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@9: * E-mail: . alpar@9: * alpar@9: * GLPK is free software: you can redistribute it and/or modify it alpar@9: * under the terms of the GNU General Public License as published by alpar@9: * the Free Software Foundation, either version 3 of the License, or alpar@9: * (at your option) any later version. alpar@9: * alpar@9: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@9: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@9: * License for more details. alpar@9: * alpar@9: * You should have received a copy of the GNU General Public License alpar@9: * along with GLPK. If not, see . alpar@9: ***********************************************************************/ alpar@9: alpar@9: #include "glpios.h" alpar@9: alpar@9: /*********************************************************************** alpar@9: * NAME alpar@9: * alpar@9: * ios_process_cuts - process cuts stored in the local cut pool alpar@9: * alpar@9: * SYNOPSIS alpar@9: * alpar@9: * #include "glpios.h" alpar@9: * void ios_process_cuts(glp_tree *T); alpar@9: * alpar@9: * DESCRIPTION alpar@9: * alpar@9: * The routine ios_process_cuts analyzes each cut currently stored in alpar@9: * the local cut pool, which must be non-empty, and either adds the cut alpar@9: * to the current subproblem or just discards it. All cuts are assumed alpar@9: * to be locally valid. On exit the local cut pool remains unchanged. alpar@9: * alpar@9: * REFERENCES alpar@9: * alpar@9: * 1. E.Balas, S.Ceria, G.Cornuejols, "Mixed 0-1 Programming by alpar@9: * Lift-and-Project in a Branch-and-Cut Framework", Management Sc., alpar@9: * 42 (1996) 1229-1246. alpar@9: * alpar@9: * 2. G.Andreello, A.Caprara, and M.Fischetti, "Embedding Cuts in alpar@9: * a Branch&Cut Framework: a Computational Study with {0,1/2}-Cuts", alpar@9: * Preliminary Draft, October 28, 2003, pp.6-8. */ alpar@9: alpar@9: struct info alpar@9: { /* estimated cut efficiency */ alpar@9: IOSCUT *cut; alpar@9: /* pointer to cut in the cut pool */ alpar@9: char flag; alpar@9: /* if this flag is set, the cut is included into the current alpar@9: subproblem */ alpar@9: double eff; alpar@9: /* cut efficacy (normalized residual) */ alpar@9: double deg; alpar@9: /* lower bound to objective degradation */ alpar@9: }; alpar@9: alpar@9: static int fcmp(const void *arg1, const void *arg2) alpar@9: { const struct info *info1 = arg1, *info2 = arg2; alpar@9: if (info1->deg == 0.0 && info2->deg == 0.0) alpar@9: { if (info1->eff > info2->eff) return -1; alpar@9: if (info1->eff < info2->eff) return +1; alpar@9: } alpar@9: else alpar@9: { if (info1->deg > info2->deg) return -1; alpar@9: if (info1->deg < info2->deg) return +1; alpar@9: } alpar@9: return 0; alpar@9: } alpar@9: alpar@9: static double parallel(IOSCUT *a, IOSCUT *b, double work[]); alpar@9: alpar@9: void ios_process_cuts(glp_tree *T) alpar@9: { IOSPOOL *pool; alpar@9: IOSCUT *cut; alpar@9: IOSAIJ *aij; alpar@9: struct info *info; alpar@9: int k, kk, max_cuts, len, ret, *ind; alpar@9: double *val, *work; alpar@9: /* the current subproblem must exist */ alpar@9: xassert(T->curr != NULL); alpar@9: /* the pool must exist and be non-empty */ alpar@9: pool = T->local; alpar@9: xassert(pool != NULL); alpar@9: xassert(pool->size > 0); alpar@9: /* allocate working arrays */ alpar@9: info = xcalloc(1+pool->size, sizeof(struct info)); alpar@9: ind = xcalloc(1+T->n, sizeof(int)); alpar@9: val = xcalloc(1+T->n, sizeof(double)); alpar@9: work = xcalloc(1+T->n, sizeof(double)); alpar@9: for (k = 1; k <= T->n; k++) work[k] = 0.0; alpar@9: /* build the list of cuts stored in the cut pool */ alpar@9: for (k = 0, cut = pool->head; cut != NULL; cut = cut->next) alpar@9: k++, info[k].cut = cut, info[k].flag = 0; alpar@9: xassert(k == pool->size); alpar@9: /* estimate efficiency of all cuts in the cut pool */ alpar@9: for (k = 1; k <= pool->size; k++) alpar@9: { double temp, dy, dz; alpar@9: cut = info[k].cut; alpar@9: /* build the vector of cut coefficients and compute its alpar@9: Euclidean norm */ alpar@9: len = 0; temp = 0.0; alpar@9: for (aij = cut->ptr; aij != NULL; aij = aij->next) alpar@9: { xassert(1 <= aij->j && aij->j <= T->n); alpar@9: len++, ind[len] = aij->j, val[len] = aij->val; alpar@9: temp += aij->val * aij->val; alpar@9: } alpar@9: if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON; alpar@9: /* transform the cut to express it only through non-basic alpar@9: (auxiliary and structural) variables */ alpar@9: len = glp_transform_row(T->mip, len, ind, val); alpar@9: /* determine change in the cut value and in the objective alpar@9: value for the adjacent basis by simulating one step of the alpar@9: dual simplex */ alpar@9: ret = _glp_analyze_row(T->mip, len, ind, val, cut->type, alpar@9: cut->rhs, 1e-9, NULL, NULL, NULL, NULL, &dy, &dz); alpar@9: /* determine normalized residual and lower bound to objective alpar@9: degradation */ alpar@9: if (ret == 0) alpar@9: { info[k].eff = fabs(dy) / sqrt(temp); alpar@9: /* if some reduced costs violates (slightly) their zero alpar@9: bounds (i.e. have wrong signs) due to round-off errors, alpar@9: dz also may have wrong sign being close to zero */ alpar@9: if (T->mip->dir == GLP_MIN) alpar@9: { if (dz < 0.0) dz = 0.0; alpar@9: info[k].deg = + dz; alpar@9: } alpar@9: else /* GLP_MAX */ alpar@9: { if (dz > 0.0) dz = 0.0; alpar@9: info[k].deg = - dz; alpar@9: } alpar@9: } alpar@9: else if (ret == 1) alpar@9: { /* the constraint is not violated at the current point */ alpar@9: info[k].eff = info[k].deg = 0.0; alpar@9: } alpar@9: else if (ret == 2) alpar@9: { /* no dual feasible adjacent basis exists */ alpar@9: info[k].eff = 1.0; alpar@9: info[k].deg = DBL_MAX; alpar@9: } alpar@9: else alpar@9: xassert(ret != ret); alpar@9: /* if the degradation is too small, just ignore it */ alpar@9: if (info[k].deg < 0.01) info[k].deg = 0.0; alpar@9: } alpar@9: /* sort the list of cuts by decreasing objective degradation and alpar@9: then by decreasing efficacy */ alpar@9: qsort(&info[1], pool->size, sizeof(struct info), fcmp); alpar@9: /* only first (most efficient) max_cuts in the list are qualified alpar@9: as candidates to be added to the current subproblem */ alpar@9: max_cuts = (T->curr->level == 0 ? 90 : 10); alpar@9: if (max_cuts > pool->size) max_cuts = pool->size; alpar@9: /* add cuts to the current subproblem */ alpar@9: #if 0 alpar@9: xprintf("*** adding cuts ***\n"); alpar@9: #endif alpar@9: for (k = 1; k <= max_cuts; k++) alpar@9: { int i, len; alpar@9: /* if this cut seems to be inefficient, skip it */ alpar@9: if (info[k].deg < 0.01 && info[k].eff < 0.01) continue; alpar@9: /* if the angle between this cut and every other cut included alpar@9: in the current subproblem is small, skip this cut */ alpar@9: for (kk = 1; kk < k; kk++) alpar@9: { if (info[kk].flag) alpar@9: { if (parallel(info[k].cut, info[kk].cut, work) > 0.90) alpar@9: break; alpar@9: } alpar@9: } alpar@9: if (kk < k) continue; alpar@9: /* add this cut to the current subproblem */ alpar@9: #if 0 alpar@9: xprintf("eff = %g; deg = %g\n", info[k].eff, info[k].deg); alpar@9: #endif alpar@9: cut = info[k].cut, info[k].flag = 1; alpar@9: i = glp_add_rows(T->mip, 1); alpar@9: if (cut->name != NULL) alpar@9: glp_set_row_name(T->mip, i, cut->name); alpar@9: xassert(T->mip->row[i]->origin == GLP_RF_CUT); alpar@9: T->mip->row[i]->klass = cut->klass; alpar@9: len = 0; alpar@9: for (aij = cut->ptr; aij != NULL; aij = aij->next) alpar@9: len++, ind[len] = aij->j, val[len] = aij->val; alpar@9: glp_set_mat_row(T->mip, i, len, ind, val); alpar@9: xassert(cut->type == GLP_LO || cut->type == GLP_UP); alpar@9: glp_set_row_bnds(T->mip, i, cut->type, cut->rhs, cut->rhs); alpar@9: } alpar@9: /* free working arrays */ alpar@9: xfree(info); alpar@9: xfree(ind); alpar@9: xfree(val); alpar@9: xfree(work); alpar@9: return; alpar@9: } alpar@9: alpar@9: #if 0 alpar@9: /*********************************************************************** alpar@9: * Given a cut a * x >= b (<= b) the routine efficacy computes the cut alpar@9: * efficacy as follows: alpar@9: * alpar@9: * eff = d * (a * x~ - b) / ||a||, alpar@9: * alpar@9: * where d is -1 (in case of '>= b') or +1 (in case of '<= b'), x~ is alpar@9: * the vector of values of structural variables in optimal solution to alpar@9: * LP relaxation of the current subproblem, ||a|| is the Euclidean norm alpar@9: * of the vector of cut coefficients. alpar@9: * alpar@9: * If the cut is violated at point x~, the efficacy eff is positive, alpar@9: * and its value is the Euclidean distance between x~ and the cut plane alpar@9: * a * x = b in the space of structural variables. alpar@9: * alpar@9: * Following geometrical intuition, it is quite natural to consider alpar@9: * this distance as a first-order measure of the expected efficacy of alpar@9: * the cut: the larger the distance the better the cut [1]. */ alpar@9: alpar@9: static double efficacy(glp_tree *T, IOSCUT *cut) alpar@9: { glp_prob *mip = T->mip; alpar@9: IOSAIJ *aij; alpar@9: double s = 0.0, t = 0.0, temp; alpar@9: for (aij = cut->ptr; aij != NULL; aij = aij->next) alpar@9: { xassert(1 <= aij->j && aij->j <= mip->n); alpar@9: s += aij->val * mip->col[aij->j]->prim; alpar@9: t += aij->val * aij->val; alpar@9: } alpar@9: temp = sqrt(t); alpar@9: if (temp < DBL_EPSILON) temp = DBL_EPSILON; alpar@9: if (cut->type == GLP_LO) alpar@9: temp = (s >= cut->rhs ? 0.0 : (cut->rhs - s) / temp); alpar@9: else if (cut->type == GLP_UP) alpar@9: temp = (s <= cut->rhs ? 0.0 : (s - cut->rhs) / temp); alpar@9: else alpar@9: xassert(cut != cut); alpar@9: return temp; alpar@9: } alpar@9: #endif alpar@9: alpar@9: /*********************************************************************** alpar@9: * Given two cuts a1 * x >= b1 (<= b1) and a2 * x >= b2 (<= b2) the alpar@9: * routine parallel computes the cosine of angle between the cut planes alpar@9: * a1 * x = b1 and a2 * x = b2 (which is the acute angle between two alpar@9: * normals to these planes) in the space of structural variables as alpar@9: * follows: alpar@9: * alpar@9: * cos phi = (a1' * a2) / (||a1|| * ||a2||), alpar@9: * alpar@9: * where (a1' * a2) is a dot product of vectors of cut coefficients, alpar@9: * ||a1|| and ||a2|| are Euclidean norms of vectors a1 and a2. alpar@9: * alpar@9: * Note that requirement cos phi = 0 forces the cuts to be orthogonal, alpar@9: * i.e. with disjoint support, while requirement cos phi <= 0.999 means alpar@9: * only avoiding duplicate (parallel) cuts [1]. */ alpar@9: alpar@9: static double parallel(IOSCUT *a, IOSCUT *b, double work[]) alpar@9: { IOSAIJ *aij; alpar@9: double s = 0.0, sa = 0.0, sb = 0.0, temp; alpar@9: for (aij = a->ptr; aij != NULL; aij = aij->next) alpar@9: { work[aij->j] = aij->val; alpar@9: sa += aij->val * aij->val; alpar@9: } alpar@9: for (aij = b->ptr; aij != NULL; aij = aij->next) alpar@9: { s += work[aij->j] * aij->val; alpar@9: sb += aij->val * aij->val; alpar@9: } alpar@9: for (aij = a->ptr; aij != NULL; aij = aij->next) alpar@9: work[aij->j] = 0.0; alpar@9: temp = sqrt(sa) * sqrt(sb); alpar@9: if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON; alpar@9: return s / temp; alpar@9: } alpar@9: alpar@9: /* eof */