aboutsummaryrefslogtreecommitdiffstats
path: root/test/monniaux/glpk-4.65/src/draft/glpios11.c
blob: 09fccef6b5bd599d3eaa29df40d49bc70f7e7a80 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* glpios11.c (process cuts stored in the local cut pool) */

/***********************************************************************
*  This code is part of GLPK (GNU Linear Programming Kit).
*
*  Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
*  2009, 2010, 2011, 2013, 2017, 2018 Andrew Makhorin, Department for
*  Applied Informatics, Moscow Aviation Institute, Moscow, Russia. All
*  rights reserved. E-mail: <mao@gnu.org>.
*
*  GLPK is free software: you can redistribute it and/or modify it
*  under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*
*  GLPK is distributed in the hope that it will be useful, but WITHOUT
*  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
*  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
*  License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with GLPK. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************/

#include "draft.h"
#include "env.h"
#include "ios.h"

/***********************************************************************
*  NAME
*
*  ios_process_cuts - process cuts stored in the local cut pool
*
*  SYNOPSIS
*
*  #include "glpios.h"
*  void ios_process_cuts(glp_tree *T);
*
*  DESCRIPTION
*
*  The routine ios_process_cuts analyzes each cut currently stored in
*  the local cut pool, which must be non-empty, and either adds the cut
*  to the current subproblem or just discards it. All cuts are assumed
*  to be locally valid. On exit the local cut pool remains unchanged.
*
*  REFERENCES
*
*  1. E.Balas, S.Ceria, G.Cornuejols, "Mixed 0-1 Programming by
*     Lift-and-Project in a Branch-and-Cut Framework", Management Sc.,
*     42 (1996) 1229-1246.
*
*  2. G.Andreello, A.Caprara, and M.Fischetti, "Embedding Cuts in
*     a Branch&Cut Framework: a Computational Study with {0,1/2}-Cuts",
*     Preliminary Draft, October 28, 2003, pp.6-8. */

struct info
{     /* estimated cut efficiency */
      IOSCUT *cut;
      /* pointer to cut in the cut pool */
      char flag;
      /* if this flag is set, the cut is included into the current
         subproblem */
      double eff;
      /* cut efficacy (normalized residual) */
      double deg;
      /* lower bound to objective degradation */
};

static int CDECL fcmp(const void *arg1, const void *arg2)
{     const struct info *info1 = arg1, *info2 = arg2;
      if (info1->deg == 0.0 && info2->deg == 0.0)
      {  if (info1->eff > info2->eff) return -1;
         if (info1->eff < info2->eff) return +1;
      }
      else
      {  if (info1->deg > info2->deg) return -1;
         if (info1->deg < info2->deg) return +1;
      }
      return 0;
}

static double parallel(IOSCUT *a, IOSCUT *b, double work[]);

#ifdef NEW_LOCAL /* 02/II-2018 */
void ios_process_cuts(glp_tree *T)
{     IOSPOOL *pool;
      IOSCUT *cut;
      GLPAIJ *aij;
      struct info *info;
      int k, kk, max_cuts, len, ret, *ind;
      double *val, *work, rhs;
      /* the current subproblem must exist */
      xassert(T->curr != NULL);
      /* the pool must exist and be non-empty */
      pool = T->local;
      xassert(pool != NULL);
      xassert(pool->m > 0);
      /* allocate working arrays */
      info = xcalloc(1+pool->m, sizeof(struct info));
      ind = xcalloc(1+T->n, sizeof(int));
      val = xcalloc(1+T->n, sizeof(double));
      work = xcalloc(1+T->n, sizeof(double));
      for (k = 1; k <= T->n; k++) work[k] = 0.0;
      /* build the list of cuts stored in the cut pool */
      for (k = 1; k <= pool->m; k++)
         info[k].cut = pool->row[k], info[k].flag = 0;
      /* estimate efficiency of all cuts in the cut pool */
      for (k = 1; k <= pool->m; k++)
      {  double temp, dy, dz;
         cut = info[k].cut;
         /* build the vector of cut coefficients and compute its
            Euclidean norm */
         len = 0; temp = 0.0;
         for (aij = cut->ptr; aij != NULL; aij = aij->r_next)
         {  xassert(1 <= aij->col->j && aij->col->j <= T->n);
            len++, ind[len] = aij->col->j, val[len] = aij->val;
            temp += aij->val * aij->val;
         }
         if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
         /* transform the cut to express it only through non-basic
            (auxiliary and structural) variables */
         len = glp_transform_row(T->mip, len, ind, val);
         /* determine change in the cut value and in the objective
            value for the adjacent basis by simulating one step of the
            dual simplex */
         switch (cut->type)
         {  case GLP_LO: rhs = cut->lb; break;
            case GLP_UP: rhs = cut->ub; break;
            default: xassert(cut != cut);
         }
         ret = _glp_analyze_row(T->mip, len, ind, val, cut->type,
            rhs, 1e-9, NULL, NULL, NULL, NULL, &dy, &dz);
         /* determine normalized residual and lower bound to objective
            degradation */
         if (ret == 0)
         {  info[k].eff = fabs(dy) / sqrt(temp);
            /* if some reduced costs violates (slightly) their zero
               bounds (i.e. have wrong signs) due to round-off errors,
               dz also may have wrong sign being close to zero */
            if (T->mip->dir == GLP_MIN)
            {  if (dz < 0.0) dz = 0.0;
               info[k].deg = + dz;
            }
            else /* GLP_MAX */
            {  if (dz > 0.0) dz = 0.0;
               info[k].deg = - dz;
            }
         }
         else if (ret == 1)
         {  /* the constraint is not violated at the current point */
            info[k].eff = info[k].deg = 0.0;
         }
         else if (ret == 2)
         {  /* no dual feasible adjacent basis exists */
            info[k].eff = 1.0;
            info[k].deg = DBL_MAX;
         }
         else
            xassert(ret != ret);
         /* if the degradation is too small, just ignore it */
         if (info[k].deg < 0.01) info[k].deg = 0.0;
      }
      /* sort the list of cuts by decreasing objective degradation and
         then by decreasing efficacy */
      qsort(&info[1], pool->m, sizeof(struct info), fcmp);
      /* only first (most efficient) max_cuts in the list are qualified
         as candidates to be added to the current subproblem */
      max_cuts = (T->curr->level == 0 ? 90 : 10);
      if (max_cuts > pool->m) max_cuts = pool->m;
      /* add cuts to the current subproblem */
#if 0
      xprintf("*** adding cuts ***\n");
#endif
      for (k = 1; k <= max_cuts; k++)
      {  int i, len;
         /* if this cut seems to be inefficient, skip it */
         if (info[k].deg < 0.01 && info[k].eff < 0.01) continue;
         /* if the angle between this cut and every other cut included
            in the current subproblem is small, skip this cut */
         for (kk = 1; kk < k; kk++)
         {  if (info[kk].flag)
            {  if (parallel(info[k].cut, info[kk].cut, work) > 0.90)
                  break;
            }
         }
         if (kk < k) continue;
         /* add this cut to the current subproblem */
#if 0
         xprintf("eff = %g; deg = %g\n", info[k].eff, info[k].deg);
#endif
         cut = info[k].cut, info[k].flag = 1;
         i = glp_add_rows(T->mip, 1);
         if (cut->name != NULL)
            glp_set_row_name(T->mip, i, cut->name);
         xassert(T->mip->row[i]->origin == GLP_RF_CUT);
         T->mip->row[i]->klass = cut->klass;
         len = 0;
         for (aij = cut->ptr; aij != NULL; aij = aij->r_next)
            len++, ind[len] = aij->col->j, val[len] = aij->val;
         glp_set_mat_row(T->mip, i, len, ind, val);
         switch (cut->type)
         {  case GLP_LO: rhs = cut->lb; break;
            case GLP_UP: rhs = cut->ub; break;
            default: xassert(cut != cut);
         }
         glp_set_row_bnds(T->mip, i, cut->type, rhs, rhs);
      }
      /* free working arrays */
      xfree(info);
      xfree(ind);
      xfree(val);
      xfree(work);
      return;
}
#else
void ios_process_cuts(glp_tree *T)
{     IOSPOOL *pool;
      IOSCUT *cut;
      IOSAIJ *aij;
      struct info *info;
      int k, kk, max_cuts, len, ret, *ind;
      double *val, *work;
      /* the current subproblem must exist */
      xassert(T->curr != NULL);
      /* the pool must exist and be non-empty */
      pool = T->local;
      xassert(pool != NULL);
      xassert(pool->size > 0);
      /* allocate working arrays */
      info = xcalloc(1+pool->size, sizeof(struct info));
      ind = xcalloc(1+T->n, sizeof(int));
      val = xcalloc(1+T->n, sizeof(double));
      work = xcalloc(1+T->n, sizeof(double));
      for (k = 1; k <= T->n; k++) work[k] = 0.0;
      /* build the list of cuts stored in the cut pool */
      for (k = 0, cut = pool->head; cut != NULL; cut = cut->next)
         k++, info[k].cut = cut, info[k].flag = 0;
      xassert(k == pool->size);
      /* estimate efficiency of all cuts in the cut pool */
      for (k = 1; k <= pool->size; k++)
      {  double temp, dy, dz;
         cut = info[k].cut;
         /* build the vector of cut coefficients and compute its
            Euclidean norm */
         len = 0; temp = 0.0;
         for (aij = cut->ptr; aij != NULL; aij = aij->next)
         {  xassert(1 <= aij->j && aij->j <= T->n);
            len++, ind[len] = aij->j, val[len] = aij->val;
            temp += aij->val * aij->val;
         }
         if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
         /* transform the cut to express it only through non-basic
            (auxiliary and structural) variables */
         len = glp_transform_row(T->mip, len, ind, val);
         /* determine change in the cut value and in the objective
            value for the adjacent basis by simulating one step of the
            dual simplex */
         ret = _glp_analyze_row(T->mip, len, ind, val, cut->type,
            cut->rhs, 1e-9, NULL, NULL, NULL, NULL, &dy, &dz);
         /* determine normalized residual and lower bound to objective
            degradation */
         if (ret == 0)
         {  info[k].eff = fabs(dy) / sqrt(temp);
            /* if some reduced costs violates (slightly) their zero
               bounds (i.e. have wrong signs) due to round-off errors,
               dz also may have wrong sign being close to zero */
            if (T->mip->dir == GLP_MIN)
            {  if (dz < 0.0) dz = 0.0;
               info[k].deg = + dz;
            }
            else /* GLP_MAX */
            {  if (dz > 0.0) dz = 0.0;
               info[k].deg = - dz;
            }
         }
         else if (ret == 1)
         {  /* the constraint is not violated at the current point */
            info[k].eff = info[k].deg = 0.0;
         }
         else if (ret == 2)
         {  /* no dual feasible adjacent basis exists */
            info[k].eff = 1.0;
            info[k].deg = DBL_MAX;
         }
         else
            xassert(ret != ret);
         /* if the degradation is too small, just ignore it */
         if (info[k].deg < 0.01) info[k].deg = 0.0;
      }
      /* sort the list of cuts by decreasing objective degradation and
         then by decreasing efficacy */
      qsort(&info[1], pool->size, sizeof(struct info), fcmp);
      /* only first (most efficient) max_cuts in the list are qualified
         as candidates to be added to the current subproblem */
      max_cuts = (T->curr->level == 0 ? 90 : 10);
      if (max_cuts > pool->size) max_cuts = pool->size;
      /* add cuts to the current subproblem */
#if 0
      xprintf("*** adding cuts ***\n");
#endif
      for (k = 1; k <= max_cuts; k++)
      {  int i, len;
         /* if this cut seems to be inefficient, skip it */
         if (info[k].deg < 0.01 && info[k].eff < 0.01) continue;
         /* if the angle between this cut and every other cut included
            in the current subproblem is small, skip this cut */
         for (kk = 1; kk < k; kk++)
         {  if (info[kk].flag)
            {  if (parallel(info[k].cut, info[kk].cut, work) > 0.90)
                  break;
            }
         }
         if (kk < k) continue;
         /* add this cut to the current subproblem */
#if 0
         xprintf("eff = %g; deg = %g\n", info[k].eff, info[k].deg);
#endif
         cut = info[k].cut, info[k].flag = 1;
         i = glp_add_rows(T->mip, 1);
         if (cut->name != NULL)
            glp_set_row_name(T->mip, i, cut->name);
         xassert(T->mip->row[i]->origin == GLP_RF_CUT);
         T->mip->row[i]->klass = cut->klass;
         len = 0;
         for (aij = cut->ptr; aij != NULL; aij = aij->next)
            len++, ind[len] = aij->j, val[len] = aij->val;
         glp_set_mat_row(T->mip, i, len, ind, val);
         xassert(cut->type == GLP_LO || cut->type == GLP_UP);
         glp_set_row_bnds(T->mip, i, cut->type, cut->rhs, cut->rhs);
      }
      /* free working arrays */
      xfree(info);
      xfree(ind);
      xfree(val);
      xfree(work);
      return;
}
#endif

#if 0
/***********************************************************************
*  Given a cut a * x >= b (<= b) the routine efficacy computes the cut
*  efficacy as follows:
*
*     eff = d * (a * x~ - b) / ||a||,
*
*  where d is -1 (in case of '>= b') or +1 (in case of '<= b'), x~ is
*  the vector of values of structural variables in optimal solution to
*  LP relaxation of the current subproblem, ||a|| is the Euclidean norm
*  of the vector of cut coefficients.
*
*  If the cut is violated at point x~, the efficacy eff is positive,
*  and its value is the Euclidean distance between x~ and the cut plane
*  a * x = b in the space of structural variables.
*
*  Following geometrical intuition, it is quite natural to consider
*  this distance as a first-order measure of the expected efficacy of
*  the cut: the larger the distance the better the cut [1]. */

static double efficacy(glp_tree *T, IOSCUT *cut)
{     glp_prob *mip = T->mip;
      IOSAIJ *aij;
      double s = 0.0, t = 0.0, temp;
      for (aij = cut->ptr; aij != NULL; aij = aij->next)
      {  xassert(1 <= aij->j && aij->j <= mip->n);
         s += aij->val * mip->col[aij->j]->prim;
         t += aij->val * aij->val;
      }
      temp = sqrt(t);
      if (temp < DBL_EPSILON) temp = DBL_EPSILON;
      if (cut->type == GLP_LO)
         temp = (s >= cut->rhs ? 0.0 : (cut->rhs - s) / temp);
      else if (cut->type == GLP_UP)
         temp = (s <= cut->rhs ? 0.0 : (s - cut->rhs) / temp);
      else
         xassert(cut != cut);
      return temp;
}
#endif

/***********************************************************************
*  Given two cuts a1 * x >= b1 (<= b1) and a2 * x >= b2 (<= b2) the
*  routine parallel computes the cosine of angle between the cut planes
*  a1 * x = b1 and a2 * x = b2 (which is the acute angle between two
*  normals to these planes) in the space of structural variables as
*  follows:
*
*     cos phi = (a1' * a2) / (||a1|| * ||a2||),
*
*  where (a1' * a2) is a dot product of vectors of cut coefficients,
*  ||a1|| and ||a2|| are Euclidean norms of vectors a1 and a2.
*
*  Note that requirement cos phi = 0 forces the cuts to be orthogonal,
*  i.e. with disjoint support, while requirement cos phi <= 0.999 means
*  only avoiding duplicate (parallel) cuts [1]. */

#ifdef NEW_LOCAL /* 02/II-2018 */
static double parallel(IOSCUT *a, IOSCUT *b, double work[])
{     GLPAIJ *aij;
      double s = 0.0, sa = 0.0, sb = 0.0, temp;
      for (aij = a->ptr; aij != NULL; aij = aij->r_next)
      {  work[aij->col->j] = aij->val;
         sa += aij->val * aij->val;
      }
      for (aij = b->ptr; aij != NULL; aij = aij->r_next)
      {  s += work[aij->col->j] * aij->val;
         sb += aij->val * aij->val;
      }
      for (aij = a->ptr; aij != NULL; aij = aij->r_next)
         work[aij->col->j] = 0.0;
      temp = sqrt(sa) * sqrt(sb);
      if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
      return s / temp;
}
#else
static double parallel(IOSCUT *a, IOSCUT *b, double work[])
{     IOSAIJ *aij;
      double s = 0.0, sa = 0.0, sb = 0.0, temp;
      for (aij = a->ptr; aij != NULL; aij = aij->next)
      {  work[aij->j] = aij->val;
         sa += aij->val * aij->val;
      }
      for (aij = b->ptr; aij != NULL; aij = aij->next)
      {  s += work[aij->j] * aij->val;
         sb += aij->val * aij->val;
      }
      for (aij = a->ptr; aij != NULL; aij = aij->next)
         work[aij->j] = 0.0;
      temp = sqrt(sa) * sqrt(sb);
      if (temp < DBL_EPSILON * DBL_EPSILON) temp = DBL_EPSILON;
      return s / temp;
}
#endif

/* eof */