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