src/glpios11.c
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

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