alpar@1
|
1 |
/* glpios05.c (Gomory's mixed integer cut generator) */
|
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_gmi_gen - generate Gomory's mixed integer cuts.
|
alpar@1
|
31 |
*
|
alpar@1
|
32 |
* SYNOPSIS
|
alpar@1
|
33 |
*
|
alpar@1
|
34 |
* #include "glpios.h"
|
alpar@1
|
35 |
* void ios_gmi_gen(glp_tree *tree, IOSPOOL *pool);
|
alpar@1
|
36 |
*
|
alpar@1
|
37 |
* DESCRIPTION
|
alpar@1
|
38 |
*
|
alpar@1
|
39 |
* The routine ios_gmi_gen generates Gomory's mixed integer cuts for
|
alpar@1
|
40 |
* the current point and adds them to the cut pool. */
|
alpar@1
|
41 |
|
alpar@1
|
42 |
#define MAXCUTS 50
|
alpar@1
|
43 |
/* maximal number of cuts to be generated for one round */
|
alpar@1
|
44 |
|
alpar@1
|
45 |
struct worka
|
alpar@1
|
46 |
{ /* Gomory's cut generator working area */
|
alpar@1
|
47 |
int *ind; /* int ind[1+n]; */
|
alpar@1
|
48 |
double *val; /* double val[1+n]; */
|
alpar@1
|
49 |
double *phi; /* double phi[1+m+n]; */
|
alpar@1
|
50 |
};
|
alpar@1
|
51 |
|
alpar@1
|
52 |
#define f(x) ((x) - floor(x))
|
alpar@1
|
53 |
/* compute fractional part of x */
|
alpar@1
|
54 |
|
alpar@1
|
55 |
static void gen_cut(glp_tree *tree, struct worka *worka, int j)
|
alpar@1
|
56 |
{ /* this routine tries to generate Gomory's mixed integer cut for
|
alpar@1
|
57 |
specified structural variable x[m+j] of integer kind, which is
|
alpar@1
|
58 |
basic and has fractional value in optimal solution to current
|
alpar@1
|
59 |
LP relaxation */
|
alpar@1
|
60 |
glp_prob *mip = tree->mip;
|
alpar@1
|
61 |
int m = mip->m;
|
alpar@1
|
62 |
int n = mip->n;
|
alpar@1
|
63 |
int *ind = worka->ind;
|
alpar@1
|
64 |
double *val = worka->val;
|
alpar@1
|
65 |
double *phi = worka->phi;
|
alpar@1
|
66 |
int i, k, len, kind, stat;
|
alpar@1
|
67 |
double lb, ub, alfa, beta, ksi, phi1, rhs;
|
alpar@1
|
68 |
/* compute row of the simplex tableau, which (row) corresponds
|
alpar@1
|
69 |
to specified basic variable xB[i] = x[m+j]; see (23) */
|
alpar@1
|
70 |
len = glp_eval_tab_row(mip, m+j, ind, val);
|
alpar@1
|
71 |
/* determine beta[i], which a value of xB[i] in optimal solution
|
alpar@1
|
72 |
to current LP relaxation; note that this value is the same as
|
alpar@1
|
73 |
if it would be computed with formula (27); it is assumed that
|
alpar@1
|
74 |
beta[i] is fractional enough */
|
alpar@1
|
75 |
beta = mip->col[j]->prim;
|
alpar@1
|
76 |
/* compute cut coefficients phi and right-hand side rho, which
|
alpar@1
|
77 |
correspond to formula (30); dense format is used, because rows
|
alpar@1
|
78 |
of the simplex tableau is usually dense */
|
alpar@1
|
79 |
for (k = 1; k <= m+n; k++) phi[k] = 0.0;
|
alpar@1
|
80 |
rhs = f(beta); /* initial value of rho; see (28), (32) */
|
alpar@1
|
81 |
for (j = 1; j <= len; j++)
|
alpar@1
|
82 |
{ /* determine original number of non-basic variable xN[j] */
|
alpar@1
|
83 |
k = ind[j];
|
alpar@1
|
84 |
xassert(1 <= k && k <= m+n);
|
alpar@1
|
85 |
/* determine the kind, bounds and current status of xN[j] in
|
alpar@1
|
86 |
optimal solution to LP relaxation */
|
alpar@1
|
87 |
if (k <= m)
|
alpar@1
|
88 |
{ /* auxiliary variable */
|
alpar@1
|
89 |
GLPROW *row = mip->row[k];
|
alpar@1
|
90 |
kind = GLP_CV;
|
alpar@1
|
91 |
lb = row->lb;
|
alpar@1
|
92 |
ub = row->ub;
|
alpar@1
|
93 |
stat = row->stat;
|
alpar@1
|
94 |
}
|
alpar@1
|
95 |
else
|
alpar@1
|
96 |
{ /* structural variable */
|
alpar@1
|
97 |
GLPCOL *col = mip->col[k-m];
|
alpar@1
|
98 |
kind = col->kind;
|
alpar@1
|
99 |
lb = col->lb;
|
alpar@1
|
100 |
ub = col->ub;
|
alpar@1
|
101 |
stat = col->stat;
|
alpar@1
|
102 |
}
|
alpar@1
|
103 |
/* xN[j] cannot be basic */
|
alpar@1
|
104 |
xassert(stat != GLP_BS);
|
alpar@1
|
105 |
/* determine row coefficient ksi[i,j] at xN[j]; see (23) */
|
alpar@1
|
106 |
ksi = val[j];
|
alpar@1
|
107 |
/* if ksi[i,j] is too large in the magnitude, do not generate
|
alpar@1
|
108 |
the cut */
|
alpar@1
|
109 |
if (fabs(ksi) > 1e+05) goto fini;
|
alpar@1
|
110 |
/* if ksi[i,j] is too small in the magnitude, skip it */
|
alpar@1
|
111 |
if (fabs(ksi) < 1e-10) goto skip;
|
alpar@1
|
112 |
/* compute row coefficient alfa[i,j] at y[j]; see (26) */
|
alpar@1
|
113 |
switch (stat)
|
alpar@1
|
114 |
{ case GLP_NF:
|
alpar@1
|
115 |
/* xN[j] is free (unbounded) having non-zero ksi[i,j];
|
alpar@1
|
116 |
do not generate the cut */
|
alpar@1
|
117 |
goto fini;
|
alpar@1
|
118 |
case GLP_NL:
|
alpar@1
|
119 |
/* xN[j] has active lower bound */
|
alpar@1
|
120 |
alfa = - ksi;
|
alpar@1
|
121 |
break;
|
alpar@1
|
122 |
case GLP_NU:
|
alpar@1
|
123 |
/* xN[j] has active upper bound */
|
alpar@1
|
124 |
alfa = + ksi;
|
alpar@1
|
125 |
break;
|
alpar@1
|
126 |
case GLP_NS:
|
alpar@1
|
127 |
/* xN[j] is fixed; skip it */
|
alpar@1
|
128 |
goto skip;
|
alpar@1
|
129 |
default:
|
alpar@1
|
130 |
xassert(stat != stat);
|
alpar@1
|
131 |
}
|
alpar@1
|
132 |
/* compute cut coefficient phi'[j] at y[j]; see (21), (28) */
|
alpar@1
|
133 |
switch (kind)
|
alpar@1
|
134 |
{ case GLP_IV:
|
alpar@1
|
135 |
/* y[j] is integer */
|
alpar@1
|
136 |
if (fabs(alfa - floor(alfa + 0.5)) < 1e-10)
|
alpar@1
|
137 |
{ /* alfa[i,j] is close to nearest integer; skip it */
|
alpar@1
|
138 |
goto skip;
|
alpar@1
|
139 |
}
|
alpar@1
|
140 |
else if (f(alfa) <= f(beta))
|
alpar@1
|
141 |
phi1 = f(alfa);
|
alpar@1
|
142 |
else
|
alpar@1
|
143 |
phi1 = (f(beta) / (1.0 - f(beta))) * (1.0 - f(alfa));
|
alpar@1
|
144 |
break;
|
alpar@1
|
145 |
case GLP_CV:
|
alpar@1
|
146 |
/* y[j] is continuous */
|
alpar@1
|
147 |
if (alfa >= 0.0)
|
alpar@1
|
148 |
phi1 = + alfa;
|
alpar@1
|
149 |
else
|
alpar@1
|
150 |
phi1 = (f(beta) / (1.0 - f(beta))) * (- alfa);
|
alpar@1
|
151 |
break;
|
alpar@1
|
152 |
default:
|
alpar@1
|
153 |
xassert(kind != kind);
|
alpar@1
|
154 |
}
|
alpar@1
|
155 |
/* compute cut coefficient phi[j] at xN[j] and update right-
|
alpar@1
|
156 |
hand side rho; see (31), (32) */
|
alpar@1
|
157 |
switch (stat)
|
alpar@1
|
158 |
{ case GLP_NL:
|
alpar@1
|
159 |
/* xN[j] has active lower bound */
|
alpar@1
|
160 |
phi[k] = + phi1;
|
alpar@1
|
161 |
rhs += phi1 * lb;
|
alpar@1
|
162 |
break;
|
alpar@1
|
163 |
case GLP_NU:
|
alpar@1
|
164 |
/* xN[j] has active upper bound */
|
alpar@1
|
165 |
phi[k] = - phi1;
|
alpar@1
|
166 |
rhs -= phi1 * ub;
|
alpar@1
|
167 |
break;
|
alpar@1
|
168 |
default:
|
alpar@1
|
169 |
xassert(stat != stat);
|
alpar@1
|
170 |
}
|
alpar@1
|
171 |
skip: ;
|
alpar@1
|
172 |
}
|
alpar@1
|
173 |
/* now the cut has the form sum_k phi[k] * x[k] >= rho, where cut
|
alpar@1
|
174 |
coefficients are stored in the array phi in dense format;
|
alpar@1
|
175 |
x[1,...,m] are auxiliary variables, x[m+1,...,m+n] are struc-
|
alpar@1
|
176 |
tural variables; see (30) */
|
alpar@1
|
177 |
/* eliminate auxiliary variables in order to express the cut only
|
alpar@1
|
178 |
through structural variables; see (33) */
|
alpar@1
|
179 |
for (i = 1; i <= m; i++)
|
alpar@1
|
180 |
{ GLPROW *row;
|
alpar@1
|
181 |
GLPAIJ *aij;
|
alpar@1
|
182 |
if (fabs(phi[i]) < 1e-10) continue;
|
alpar@1
|
183 |
/* auxiliary variable x[i] has non-zero cut coefficient */
|
alpar@1
|
184 |
row = mip->row[i];
|
alpar@1
|
185 |
/* x[i] cannot be fixed */
|
alpar@1
|
186 |
xassert(row->type != GLP_FX);
|
alpar@1
|
187 |
/* substitute x[i] = sum_j a[i,j] * x[m+j] */
|
alpar@1
|
188 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
189 |
phi[m+aij->col->j] += phi[i] * aij->val;
|
alpar@1
|
190 |
}
|
alpar@1
|
191 |
/* convert the final cut to sparse format and substitute fixed
|
alpar@1
|
192 |
(structural) variables */
|
alpar@1
|
193 |
len = 0;
|
alpar@1
|
194 |
for (j = 1; j <= n; j++)
|
alpar@1
|
195 |
{ GLPCOL *col;
|
alpar@1
|
196 |
if (fabs(phi[m+j]) < 1e-10) continue;
|
alpar@1
|
197 |
/* structural variable x[m+j] has non-zero cut coefficient */
|
alpar@1
|
198 |
col = mip->col[j];
|
alpar@1
|
199 |
if (col->type == GLP_FX)
|
alpar@1
|
200 |
{ /* eliminate x[m+j] */
|
alpar@1
|
201 |
rhs -= phi[m+j] * col->lb;
|
alpar@1
|
202 |
}
|
alpar@1
|
203 |
else
|
alpar@1
|
204 |
{ len++;
|
alpar@1
|
205 |
ind[len] = j;
|
alpar@1
|
206 |
val[len] = phi[m+j];
|
alpar@1
|
207 |
}
|
alpar@1
|
208 |
}
|
alpar@1
|
209 |
if (fabs(rhs) < 1e-12) rhs = 0.0;
|
alpar@1
|
210 |
/* if the cut inequality seems to be badly scaled, reject it to
|
alpar@1
|
211 |
avoid numeric difficulties */
|
alpar@1
|
212 |
for (k = 1; k <= len; k++)
|
alpar@1
|
213 |
{ if (fabs(val[k]) < 1e-03) goto fini;
|
alpar@1
|
214 |
if (fabs(val[k]) > 1e+03) goto fini;
|
alpar@1
|
215 |
}
|
alpar@1
|
216 |
/* add the cut to the cut pool for further consideration */
|
alpar@1
|
217 |
#if 0
|
alpar@1
|
218 |
ios_add_cut_row(tree, pool, GLP_RF_GMI, len, ind, val, GLP_LO,
|
alpar@1
|
219 |
rhs);
|
alpar@1
|
220 |
#else
|
alpar@1
|
221 |
glp_ios_add_row(tree, NULL, GLP_RF_GMI, 0, len, ind, val, GLP_LO,
|
alpar@1
|
222 |
rhs);
|
alpar@1
|
223 |
#endif
|
alpar@1
|
224 |
fini: return;
|
alpar@1
|
225 |
}
|
alpar@1
|
226 |
|
alpar@1
|
227 |
struct var { int j; double f; };
|
alpar@1
|
228 |
|
alpar@1
|
229 |
static int fcmp(const void *p1, const void *p2)
|
alpar@1
|
230 |
{ const struct var *v1 = p1, *v2 = p2;
|
alpar@1
|
231 |
if (v1->f > v2->f) return -1;
|
alpar@1
|
232 |
if (v1->f < v2->f) return +1;
|
alpar@1
|
233 |
return 0;
|
alpar@1
|
234 |
}
|
alpar@1
|
235 |
|
alpar@1
|
236 |
void ios_gmi_gen(glp_tree *tree)
|
alpar@1
|
237 |
{ /* main routine to generate Gomory's cuts */
|
alpar@1
|
238 |
glp_prob *mip = tree->mip;
|
alpar@1
|
239 |
int m = mip->m;
|
alpar@1
|
240 |
int n = mip->n;
|
alpar@1
|
241 |
struct var *var;
|
alpar@1
|
242 |
int k, nv, j, size;
|
alpar@1
|
243 |
struct worka _worka, *worka = &_worka;
|
alpar@1
|
244 |
/* allocate working arrays */
|
alpar@1
|
245 |
var = xcalloc(1+n, sizeof(struct var));
|
alpar@1
|
246 |
worka->ind = xcalloc(1+n, sizeof(int));
|
alpar@1
|
247 |
worka->val = xcalloc(1+n, sizeof(double));
|
alpar@1
|
248 |
worka->phi = xcalloc(1+m+n, sizeof(double));
|
alpar@1
|
249 |
/* build the list of integer structural variables, which are
|
alpar@1
|
250 |
basic and have fractional value in optimal solution to current
|
alpar@1
|
251 |
LP relaxation */
|
alpar@1
|
252 |
nv = 0;
|
alpar@1
|
253 |
for (j = 1; j <= n; j++)
|
alpar@1
|
254 |
{ GLPCOL *col = mip->col[j];
|
alpar@1
|
255 |
double frac;
|
alpar@1
|
256 |
if (col->kind != GLP_IV) continue;
|
alpar@1
|
257 |
if (col->type == GLP_FX) continue;
|
alpar@1
|
258 |
if (col->stat != GLP_BS) continue;
|
alpar@1
|
259 |
frac = f(col->prim);
|
alpar@1
|
260 |
if (!(0.05 <= frac && frac <= 0.95)) continue;
|
alpar@1
|
261 |
/* add variable to the list */
|
alpar@1
|
262 |
nv++, var[nv].j = j, var[nv].f = frac;
|
alpar@1
|
263 |
}
|
alpar@1
|
264 |
/* order the list by descending fractionality */
|
alpar@1
|
265 |
qsort(&var[1], nv, sizeof(struct var), fcmp);
|
alpar@1
|
266 |
/* try to generate cuts by one for each variable in the list, but
|
alpar@1
|
267 |
not more than MAXCUTS cuts */
|
alpar@1
|
268 |
size = glp_ios_pool_size(tree);
|
alpar@1
|
269 |
for (k = 1; k <= nv; k++)
|
alpar@1
|
270 |
{ if (glp_ios_pool_size(tree) - size >= MAXCUTS) break;
|
alpar@1
|
271 |
gen_cut(tree, worka, var[k].j);
|
alpar@1
|
272 |
}
|
alpar@1
|
273 |
/* free working arrays */
|
alpar@1
|
274 |
xfree(var);
|
alpar@1
|
275 |
xfree(worka->ind);
|
alpar@1
|
276 |
xfree(worka->val);
|
alpar@1
|
277 |
xfree(worka->phi);
|
alpar@1
|
278 |
return;
|
alpar@1
|
279 |
}
|
alpar@1
|
280 |
|
alpar@1
|
281 |
/* eof */
|