alpar@1
|
1 |
/* glpnpp04.c */
|
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 "glpnpp.h"
|
alpar@1
|
26 |
|
alpar@1
|
27 |
/***********************************************************************
|
alpar@1
|
28 |
* NAME
|
alpar@1
|
29 |
*
|
alpar@1
|
30 |
* npp_binarize_prob - binarize MIP problem
|
alpar@1
|
31 |
*
|
alpar@1
|
32 |
* SYNOPSIS
|
alpar@1
|
33 |
*
|
alpar@1
|
34 |
* #include "glpnpp.h"
|
alpar@1
|
35 |
* int npp_binarize_prob(NPP *npp);
|
alpar@1
|
36 |
*
|
alpar@1
|
37 |
* DESCRIPTION
|
alpar@1
|
38 |
*
|
alpar@1
|
39 |
* The routine npp_binarize_prob replaces in the original MIP problem
|
alpar@1
|
40 |
* every integer variable:
|
alpar@1
|
41 |
*
|
alpar@1
|
42 |
* l[q] <= x[q] <= u[q], (1)
|
alpar@1
|
43 |
*
|
alpar@1
|
44 |
* where l[q] < u[q], by an equivalent sum of binary variables.
|
alpar@1
|
45 |
*
|
alpar@1
|
46 |
* RETURNS
|
alpar@1
|
47 |
*
|
alpar@1
|
48 |
* The routine returns the number of integer variables for which the
|
alpar@1
|
49 |
* transformation failed, because u[q] - l[q] > d_max.
|
alpar@1
|
50 |
*
|
alpar@1
|
51 |
* PROBLEM TRANSFORMATION
|
alpar@1
|
52 |
*
|
alpar@1
|
53 |
* If variable x[q] has non-zero lower bound, it is first processed
|
alpar@1
|
54 |
* with the routine npp_lbnd_col. Thus, we can assume that:
|
alpar@1
|
55 |
*
|
alpar@1
|
56 |
* 0 <= x[q] <= u[q]. (2)
|
alpar@1
|
57 |
*
|
alpar@1
|
58 |
* If u[q] = 1, variable x[q] is already binary, so further processing
|
alpar@1
|
59 |
* is not needed. Let, therefore, that 2 <= u[q] <= d_max, and n be a
|
alpar@1
|
60 |
* smallest integer such that u[q] <= 2^n - 1 (n >= 2, since u[q] >= 2).
|
alpar@1
|
61 |
* Then variable x[q] can be replaced by the following sum:
|
alpar@1
|
62 |
*
|
alpar@1
|
63 |
* n-1
|
alpar@1
|
64 |
* x[q] = sum 2^k x[k], (3)
|
alpar@1
|
65 |
* k=0
|
alpar@1
|
66 |
*
|
alpar@1
|
67 |
* where x[k] are binary columns (variables). If u[q] < 2^n - 1, the
|
alpar@1
|
68 |
* following additional inequality constraint must be also included in
|
alpar@1
|
69 |
* the transformed problem:
|
alpar@1
|
70 |
*
|
alpar@1
|
71 |
* n-1
|
alpar@1
|
72 |
* sum 2^k x[k] <= u[q]. (4)
|
alpar@1
|
73 |
* k=0
|
alpar@1
|
74 |
*
|
alpar@1
|
75 |
* Note: Assuming that in the transformed problem x[q] becomes binary
|
alpar@1
|
76 |
* variable x[0], this transformation causes new n-1 binary variables
|
alpar@1
|
77 |
* to appear.
|
alpar@1
|
78 |
*
|
alpar@1
|
79 |
* Substituting x[q] from (3) to the objective row gives:
|
alpar@1
|
80 |
*
|
alpar@1
|
81 |
* z = sum c[j] x[j] + c[0] =
|
alpar@1
|
82 |
* j
|
alpar@1
|
83 |
*
|
alpar@1
|
84 |
* = sum c[j] x[j] + c[q] x[q] + c[0] =
|
alpar@1
|
85 |
* j!=q
|
alpar@1
|
86 |
* n-1
|
alpar@1
|
87 |
* = sum c[j] x[j] + c[q] sum 2^k x[k] + c[0] =
|
alpar@1
|
88 |
* j!=q k=0
|
alpar@1
|
89 |
* n-1
|
alpar@1
|
90 |
* = sum c[j] x[j] + sum c[k] x[k] + c[0],
|
alpar@1
|
91 |
* j!=q k=0
|
alpar@1
|
92 |
*
|
alpar@1
|
93 |
* where:
|
alpar@1
|
94 |
*
|
alpar@1
|
95 |
* c[k] = 2^k c[q], k = 0, ..., n-1. (5)
|
alpar@1
|
96 |
*
|
alpar@1
|
97 |
* And substituting x[q] from (3) to i-th constraint row i gives:
|
alpar@1
|
98 |
*
|
alpar@1
|
99 |
* L[i] <= sum a[i,j] x[j] <= U[i] ==>
|
alpar@1
|
100 |
* j
|
alpar@1
|
101 |
*
|
alpar@1
|
102 |
* L[i] <= sum a[i,j] x[j] + a[i,q] x[q] <= U[i] ==>
|
alpar@1
|
103 |
* j!=q
|
alpar@1
|
104 |
* n-1
|
alpar@1
|
105 |
* L[i] <= sum a[i,j] x[j] + a[i,q] sum 2^k x[k] <= U[i] ==>
|
alpar@1
|
106 |
* j!=q k=0
|
alpar@1
|
107 |
* n-1
|
alpar@1
|
108 |
* L[i] <= sum a[i,j] x[j] + sum a[i,k] x[k] <= U[i],
|
alpar@1
|
109 |
* j!=q k=0
|
alpar@1
|
110 |
*
|
alpar@1
|
111 |
* where:
|
alpar@1
|
112 |
*
|
alpar@1
|
113 |
* a[i,k] = 2^k a[i,q], k = 0, ..., n-1. (6)
|
alpar@1
|
114 |
*
|
alpar@1
|
115 |
* RECOVERING SOLUTION
|
alpar@1
|
116 |
*
|
alpar@1
|
117 |
* Value of variable x[q] is computed with formula (3). */
|
alpar@1
|
118 |
|
alpar@1
|
119 |
struct binarize
|
alpar@1
|
120 |
{ int q;
|
alpar@1
|
121 |
/* column reference number for x[q] = x[0] */
|
alpar@1
|
122 |
int j;
|
alpar@1
|
123 |
/* column reference number for x[1]; x[2] has reference number
|
alpar@1
|
124 |
j+1, x[3] - j+2, etc. */
|
alpar@1
|
125 |
int n;
|
alpar@1
|
126 |
/* total number of binary variables, n >= 2 */
|
alpar@1
|
127 |
};
|
alpar@1
|
128 |
|
alpar@1
|
129 |
static int rcv_binarize_prob(NPP *npp, void *info);
|
alpar@1
|
130 |
|
alpar@1
|
131 |
int npp_binarize_prob(NPP *npp)
|
alpar@1
|
132 |
{ /* binarize MIP problem */
|
alpar@1
|
133 |
struct binarize *info;
|
alpar@1
|
134 |
NPPROW *row;
|
alpar@1
|
135 |
NPPCOL *col, *bin;
|
alpar@1
|
136 |
NPPAIJ *aij;
|
alpar@1
|
137 |
int u, n, k, temp, nfails, nvars, nbins, nrows;
|
alpar@1
|
138 |
/* new variables will be added to the end of the column list, so
|
alpar@1
|
139 |
we go from the end to beginning of the column list */
|
alpar@1
|
140 |
nfails = nvars = nbins = nrows = 0;
|
alpar@1
|
141 |
for (col = npp->c_tail; col != NULL; col = col->prev)
|
alpar@1
|
142 |
{ /* skip continuous variable */
|
alpar@1
|
143 |
if (!col->is_int) continue;
|
alpar@1
|
144 |
/* skip fixed variable */
|
alpar@1
|
145 |
if (col->lb == col->ub) continue;
|
alpar@1
|
146 |
/* skip binary variable */
|
alpar@1
|
147 |
if (col->lb == 0.0 && col->ub == 1.0) continue;
|
alpar@1
|
148 |
/* check if the transformation is applicable */
|
alpar@1
|
149 |
if (col->lb < -1e6 || col->ub > +1e6 ||
|
alpar@1
|
150 |
col->ub - col->lb > 4095.0)
|
alpar@1
|
151 |
{ /* unfortunately, not */
|
alpar@1
|
152 |
nfails++;
|
alpar@1
|
153 |
continue;
|
alpar@1
|
154 |
}
|
alpar@1
|
155 |
/* process integer non-binary variable x[q] */
|
alpar@1
|
156 |
nvars++;
|
alpar@1
|
157 |
/* make x[q] non-negative, if its lower bound is non-zero */
|
alpar@1
|
158 |
if (col->lb != 0.0)
|
alpar@1
|
159 |
npp_lbnd_col(npp, col);
|
alpar@1
|
160 |
/* now 0 <= x[q] <= u[q] */
|
alpar@1
|
161 |
xassert(col->lb == 0.0);
|
alpar@1
|
162 |
u = (int)col->ub;
|
alpar@1
|
163 |
xassert(col->ub == (double)u);
|
alpar@1
|
164 |
/* if x[q] is binary, further processing is not needed */
|
alpar@1
|
165 |
if (u == 1) continue;
|
alpar@1
|
166 |
/* determine smallest n such that u <= 2^n - 1 (thus, n is the
|
alpar@1
|
167 |
number of binary variables needed) */
|
alpar@1
|
168 |
n = 2, temp = 4;
|
alpar@1
|
169 |
while (u >= temp)
|
alpar@1
|
170 |
n++, temp += temp;
|
alpar@1
|
171 |
nbins += n;
|
alpar@1
|
172 |
/* create transformation stack entry */
|
alpar@1
|
173 |
info = npp_push_tse(npp,
|
alpar@1
|
174 |
rcv_binarize_prob, sizeof(struct binarize));
|
alpar@1
|
175 |
info->q = col->j;
|
alpar@1
|
176 |
info->j = 0; /* will be set below */
|
alpar@1
|
177 |
info->n = n;
|
alpar@1
|
178 |
/* if u < 2^n - 1, we need one additional row for (4) */
|
alpar@1
|
179 |
if (u < temp - 1)
|
alpar@1
|
180 |
{ row = npp_add_row(npp), nrows++;
|
alpar@1
|
181 |
row->lb = -DBL_MAX, row->ub = u;
|
alpar@1
|
182 |
}
|
alpar@1
|
183 |
else
|
alpar@1
|
184 |
row = NULL;
|
alpar@1
|
185 |
/* in the transformed problem variable x[q] becomes binary
|
alpar@1
|
186 |
variable x[0], so its objective and constraint coefficients
|
alpar@1
|
187 |
are not changed */
|
alpar@1
|
188 |
col->ub = 1.0;
|
alpar@1
|
189 |
/* include x[0] into constraint (4) */
|
alpar@1
|
190 |
if (row != NULL)
|
alpar@1
|
191 |
npp_add_aij(npp, row, col, 1.0);
|
alpar@1
|
192 |
/* add other binary variables x[1], ..., x[n-1] */
|
alpar@1
|
193 |
for (k = 1, temp = 2; k < n; k++, temp += temp)
|
alpar@1
|
194 |
{ /* add new binary variable x[k] */
|
alpar@1
|
195 |
bin = npp_add_col(npp);
|
alpar@1
|
196 |
bin->is_int = 1;
|
alpar@1
|
197 |
bin->lb = 0.0, bin->ub = 1.0;
|
alpar@1
|
198 |
bin->coef = (double)temp * col->coef;
|
alpar@1
|
199 |
/* store column reference number for x[1] */
|
alpar@1
|
200 |
if (info->j == 0)
|
alpar@1
|
201 |
info->j = bin->j;
|
alpar@1
|
202 |
else
|
alpar@1
|
203 |
xassert(info->j + (k-1) == bin->j);
|
alpar@1
|
204 |
/* duplicate constraint coefficients for x[k]; this also
|
alpar@1
|
205 |
automatically includes x[k] into constraint (4) */
|
alpar@1
|
206 |
for (aij = col->ptr; aij != NULL; aij = aij->c_next)
|
alpar@1
|
207 |
npp_add_aij(npp, aij->row, bin, (double)temp * aij->val);
|
alpar@1
|
208 |
}
|
alpar@1
|
209 |
}
|
alpar@1
|
210 |
if (nvars > 0)
|
alpar@1
|
211 |
xprintf("%d integer variable(s) were replaced by %d binary one"
|
alpar@1
|
212 |
"s\n", nvars, nbins);
|
alpar@1
|
213 |
if (nrows > 0)
|
alpar@1
|
214 |
xprintf("%d row(s) were added due to binarization\n", nrows);
|
alpar@1
|
215 |
if (nfails > 0)
|
alpar@1
|
216 |
xprintf("Binarization failed for %d integer variable(s)\n",
|
alpar@1
|
217 |
nfails);
|
alpar@1
|
218 |
return nfails;
|
alpar@1
|
219 |
}
|
alpar@1
|
220 |
|
alpar@1
|
221 |
static int rcv_binarize_prob(NPP *npp, void *_info)
|
alpar@1
|
222 |
{ /* recovery binarized variable */
|
alpar@1
|
223 |
struct binarize *info = _info;
|
alpar@1
|
224 |
int k, temp;
|
alpar@1
|
225 |
double sum;
|
alpar@1
|
226 |
/* compute value of x[q]; see formula (3) */
|
alpar@1
|
227 |
sum = npp->c_value[info->q];
|
alpar@1
|
228 |
for (k = 1, temp = 2; k < info->n; k++, temp += temp)
|
alpar@1
|
229 |
sum += (double)temp * npp->c_value[info->j + (k-1)];
|
alpar@1
|
230 |
npp->c_value[info->q] = sum;
|
alpar@1
|
231 |
return 0;
|
alpar@1
|
232 |
}
|
alpar@1
|
233 |
|
alpar@1
|
234 |
/**********************************************************************/
|
alpar@1
|
235 |
|
alpar@1
|
236 |
struct elem
|
alpar@1
|
237 |
{ /* linear form element a[j] x[j] */
|
alpar@1
|
238 |
double aj;
|
alpar@1
|
239 |
/* non-zero coefficient value */
|
alpar@1
|
240 |
NPPCOL *xj;
|
alpar@1
|
241 |
/* pointer to variable (column) */
|
alpar@1
|
242 |
struct elem *next;
|
alpar@1
|
243 |
/* pointer to another term */
|
alpar@1
|
244 |
};
|
alpar@1
|
245 |
|
alpar@1
|
246 |
static struct elem *copy_form(NPP *npp, NPPROW *row, double s)
|
alpar@1
|
247 |
{ /* copy linear form */
|
alpar@1
|
248 |
NPPAIJ *aij;
|
alpar@1
|
249 |
struct elem *ptr, *e;
|
alpar@1
|
250 |
ptr = NULL;
|
alpar@1
|
251 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
252 |
{ e = dmp_get_atom(npp->pool, sizeof(struct elem));
|
alpar@1
|
253 |
e->aj = s * aij->val;
|
alpar@1
|
254 |
e->xj = aij->col;
|
alpar@1
|
255 |
e->next = ptr;
|
alpar@1
|
256 |
ptr = e;
|
alpar@1
|
257 |
}
|
alpar@1
|
258 |
return ptr;
|
alpar@1
|
259 |
}
|
alpar@1
|
260 |
|
alpar@1
|
261 |
static void drop_form(NPP *npp, struct elem *ptr)
|
alpar@1
|
262 |
{ /* drop linear form */
|
alpar@1
|
263 |
struct elem *e;
|
alpar@1
|
264 |
while (ptr != NULL)
|
alpar@1
|
265 |
{ e = ptr;
|
alpar@1
|
266 |
ptr = e->next;
|
alpar@1
|
267 |
dmp_free_atom(npp->pool, e, sizeof(struct elem));
|
alpar@1
|
268 |
}
|
alpar@1
|
269 |
return;
|
alpar@1
|
270 |
}
|
alpar@1
|
271 |
|
alpar@1
|
272 |
/***********************************************************************
|
alpar@1
|
273 |
* NAME
|
alpar@1
|
274 |
*
|
alpar@1
|
275 |
* npp_is_packing - test if constraint is packing inequality
|
alpar@1
|
276 |
*
|
alpar@1
|
277 |
* SYNOPSIS
|
alpar@1
|
278 |
*
|
alpar@1
|
279 |
* #include "glpnpp.h"
|
alpar@1
|
280 |
* int npp_is_packing(NPP *npp, NPPROW *row);
|
alpar@1
|
281 |
*
|
alpar@1
|
282 |
* RETURNS
|
alpar@1
|
283 |
*
|
alpar@1
|
284 |
* If the specified row (constraint) is packing inequality (see below),
|
alpar@1
|
285 |
* the routine npp_is_packing returns non-zero. Otherwise, it returns
|
alpar@1
|
286 |
* zero.
|
alpar@1
|
287 |
*
|
alpar@1
|
288 |
* PACKING INEQUALITIES
|
alpar@1
|
289 |
*
|
alpar@1
|
290 |
* In canonical format the packing inequality is the following:
|
alpar@1
|
291 |
*
|
alpar@1
|
292 |
* sum x[j] <= 1, (1)
|
alpar@1
|
293 |
* j in J
|
alpar@1
|
294 |
*
|
alpar@1
|
295 |
* where all variables x[j] are binary. This inequality expresses the
|
alpar@1
|
296 |
* condition that in any integer feasible solution at most one variable
|
alpar@1
|
297 |
* from set J can take non-zero (unity) value while other variables
|
alpar@1
|
298 |
* must be equal to zero. W.l.o.g. it is assumed that |J| >= 2, because
|
alpar@1
|
299 |
* if J is empty or |J| = 1, the inequality (1) is redundant.
|
alpar@1
|
300 |
*
|
alpar@1
|
301 |
* In general case the packing inequality may include original variables
|
alpar@1
|
302 |
* x[j] as well as their complements x~[j]:
|
alpar@1
|
303 |
*
|
alpar@1
|
304 |
* sum x[j] + sum x~[j] <= 1, (2)
|
alpar@1
|
305 |
* j in Jp j in Jn
|
alpar@1
|
306 |
*
|
alpar@1
|
307 |
* where Jp and Jn are not intersected. Therefore, using substitution
|
alpar@1
|
308 |
* x~[j] = 1 - x[j] gives the packing inequality in generalized format:
|
alpar@1
|
309 |
*
|
alpar@1
|
310 |
* sum x[j] - sum x[j] <= 1 - |Jn|. (3)
|
alpar@1
|
311 |
* j in Jp j in Jn */
|
alpar@1
|
312 |
|
alpar@1
|
313 |
int npp_is_packing(NPP *npp, NPPROW *row)
|
alpar@1
|
314 |
{ /* test if constraint is packing inequality */
|
alpar@1
|
315 |
NPPCOL *col;
|
alpar@1
|
316 |
NPPAIJ *aij;
|
alpar@1
|
317 |
int b;
|
alpar@1
|
318 |
xassert(npp == npp);
|
alpar@1
|
319 |
if (!(row->lb == -DBL_MAX && row->ub != +DBL_MAX))
|
alpar@1
|
320 |
return 0;
|
alpar@1
|
321 |
b = 1;
|
alpar@1
|
322 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
323 |
{ col = aij->col;
|
alpar@1
|
324 |
if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
|
alpar@1
|
325 |
return 0;
|
alpar@1
|
326 |
if (aij->val == +1.0)
|
alpar@1
|
327 |
;
|
alpar@1
|
328 |
else if (aij->val == -1.0)
|
alpar@1
|
329 |
b--;
|
alpar@1
|
330 |
else
|
alpar@1
|
331 |
return 0;
|
alpar@1
|
332 |
}
|
alpar@1
|
333 |
if (row->ub != (double)b) return 0;
|
alpar@1
|
334 |
return 1;
|
alpar@1
|
335 |
}
|
alpar@1
|
336 |
|
alpar@1
|
337 |
/***********************************************************************
|
alpar@1
|
338 |
* NAME
|
alpar@1
|
339 |
*
|
alpar@1
|
340 |
* npp_hidden_packing - identify hidden packing inequality
|
alpar@1
|
341 |
*
|
alpar@1
|
342 |
* SYNOPSIS
|
alpar@1
|
343 |
*
|
alpar@1
|
344 |
* #include "glpnpp.h"
|
alpar@1
|
345 |
* int npp_hidden_packing(NPP *npp, NPPROW *row);
|
alpar@1
|
346 |
*
|
alpar@1
|
347 |
* DESCRIPTION
|
alpar@1
|
348 |
*
|
alpar@1
|
349 |
* The routine npp_hidden_packing processes specified inequality
|
alpar@1
|
350 |
* constraint, which includes only binary variables, and the number of
|
alpar@1
|
351 |
* the variables is not less than two. If the original inequality is
|
alpar@1
|
352 |
* equivalent to a packing inequality, the routine replaces it by this
|
alpar@1
|
353 |
* equivalent inequality. If the original constraint is double-sided
|
alpar@1
|
354 |
* inequality, it is replaced by a pair of single-sided inequalities,
|
alpar@1
|
355 |
* if necessary.
|
alpar@1
|
356 |
*
|
alpar@1
|
357 |
* RETURNS
|
alpar@1
|
358 |
*
|
alpar@1
|
359 |
* If the original inequality constraint was replaced by equivalent
|
alpar@1
|
360 |
* packing inequality, the routine npp_hidden_packing returns non-zero.
|
alpar@1
|
361 |
* Otherwise, it returns zero.
|
alpar@1
|
362 |
*
|
alpar@1
|
363 |
* PROBLEM TRANSFORMATION
|
alpar@1
|
364 |
*
|
alpar@1
|
365 |
* Consider an inequality constraint:
|
alpar@1
|
366 |
*
|
alpar@1
|
367 |
* sum a[j] x[j] <= b, (1)
|
alpar@1
|
368 |
* j in J
|
alpar@1
|
369 |
*
|
alpar@1
|
370 |
* where all variables x[j] are binary, and |J| >= 2. (In case of '>='
|
alpar@1
|
371 |
* inequality it can be transformed to '<=' format by multiplying both
|
alpar@1
|
372 |
* its sides by -1.)
|
alpar@1
|
373 |
*
|
alpar@1
|
374 |
* Let Jp = {j: a[j] > 0}, Jn = {j: a[j] < 0}. Performing substitution
|
alpar@1
|
375 |
* x[j] = 1 - x~[j] for all j in Jn, we have:
|
alpar@1
|
376 |
*
|
alpar@1
|
377 |
* sum a[j] x[j] <= b ==>
|
alpar@1
|
378 |
* j in J
|
alpar@1
|
379 |
*
|
alpar@1
|
380 |
* sum a[j] x[j] + sum a[j] x[j] <= b ==>
|
alpar@1
|
381 |
* j in Jp j in Jn
|
alpar@1
|
382 |
*
|
alpar@1
|
383 |
* sum a[j] x[j] + sum a[j] (1 - x~[j]) <= b ==>
|
alpar@1
|
384 |
* j in Jp j in Jn
|
alpar@1
|
385 |
*
|
alpar@1
|
386 |
* sum a[j] x[j] - sum a[j] x~[j] <= b - sum a[j].
|
alpar@1
|
387 |
* j in Jp j in Jn j in Jn
|
alpar@1
|
388 |
*
|
alpar@1
|
389 |
* Thus, meaning the transformation above, we can assume that in
|
alpar@1
|
390 |
* inequality (1) all coefficients a[j] are positive. Moreover, we can
|
alpar@1
|
391 |
* assume that a[j] <= b. In fact, let a[j] > b; then the following
|
alpar@1
|
392 |
* three cases are possible:
|
alpar@1
|
393 |
*
|
alpar@1
|
394 |
* 1) b < 0. In this case inequality (1) is infeasible, so the problem
|
alpar@1
|
395 |
* has no feasible solution (see the routine npp_analyze_row);
|
alpar@1
|
396 |
*
|
alpar@1
|
397 |
* 2) b = 0. In this case inequality (1) is a forcing inequality on its
|
alpar@1
|
398 |
* upper bound (see the routine npp_forcing row), from which it
|
alpar@1
|
399 |
* follows that all variables x[j] should be fixed at zero;
|
alpar@1
|
400 |
*
|
alpar@1
|
401 |
* 3) b > 0. In this case inequality (1) defines an implied zero upper
|
alpar@1
|
402 |
* bound for variable x[j] (see the routine npp_implied_bounds), from
|
alpar@1
|
403 |
* which it follows that x[j] should be fixed at zero.
|
alpar@1
|
404 |
*
|
alpar@1
|
405 |
* It is assumed that all three cases listed above have been recognized
|
alpar@1
|
406 |
* by the routine npp_process_prob, which performs basic MIP processing
|
alpar@1
|
407 |
* prior to a call the routine npp_hidden_packing. So, if one of these
|
alpar@1
|
408 |
* cases occurs, we should just skip processing such constraint.
|
alpar@1
|
409 |
*
|
alpar@1
|
410 |
* Thus, let 0 < a[j] <= b. Then it is obvious that constraint (1) is
|
alpar@1
|
411 |
* equivalent to packing inquality only if:
|
alpar@1
|
412 |
*
|
alpar@1
|
413 |
* a[j] + a[k] > b + eps (2)
|
alpar@1
|
414 |
*
|
alpar@1
|
415 |
* for all j, k in J, j != k, where eps is an absolute tolerance for
|
alpar@1
|
416 |
* row (linear form) value. Checking the condition (2) for all j and k,
|
alpar@1
|
417 |
* j != k, requires time O(|J|^2). However, this time can be reduced to
|
alpar@1
|
418 |
* O(|J|), if use minimal a[j] and a[k], in which case it is sufficient
|
alpar@1
|
419 |
* to check the condition (2) only once.
|
alpar@1
|
420 |
*
|
alpar@1
|
421 |
* Once the original inequality (1) is replaced by equivalent packing
|
alpar@1
|
422 |
* inequality, we need to perform back substitution x~[j] = 1 - x[j] for
|
alpar@1
|
423 |
* all j in Jn (see above).
|
alpar@1
|
424 |
*
|
alpar@1
|
425 |
* RECOVERING SOLUTION
|
alpar@1
|
426 |
*
|
alpar@1
|
427 |
* None needed. */
|
alpar@1
|
428 |
|
alpar@1
|
429 |
static int hidden_packing(NPP *npp, struct elem *ptr, double *_b)
|
alpar@1
|
430 |
{ /* process inequality constraint: sum a[j] x[j] <= b;
|
alpar@1
|
431 |
0 - specified row is NOT hidden packing inequality;
|
alpar@1
|
432 |
1 - specified row is packing inequality;
|
alpar@1
|
433 |
2 - specified row is hidden packing inequality. */
|
alpar@1
|
434 |
struct elem *e, *ej, *ek;
|
alpar@1
|
435 |
int neg;
|
alpar@1
|
436 |
double b = *_b, eps;
|
alpar@1
|
437 |
xassert(npp == npp);
|
alpar@1
|
438 |
/* a[j] must be non-zero, x[j] must be binary, for all j in J */
|
alpar@1
|
439 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
440 |
{ xassert(e->aj != 0.0);
|
alpar@1
|
441 |
xassert(e->xj->is_int);
|
alpar@1
|
442 |
xassert(e->xj->lb == 0.0 && e->xj->ub == 1.0);
|
alpar@1
|
443 |
}
|
alpar@1
|
444 |
/* check if the specified inequality constraint already has the
|
alpar@1
|
445 |
form of packing inequality */
|
alpar@1
|
446 |
neg = 0; /* neg is |Jn| */
|
alpar@1
|
447 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
448 |
{ if (e->aj == +1.0)
|
alpar@1
|
449 |
;
|
alpar@1
|
450 |
else if (e->aj == -1.0)
|
alpar@1
|
451 |
neg++;
|
alpar@1
|
452 |
else
|
alpar@1
|
453 |
break;
|
alpar@1
|
454 |
}
|
alpar@1
|
455 |
if (e == NULL)
|
alpar@1
|
456 |
{ /* all coefficients a[j] are +1 or -1; check rhs b */
|
alpar@1
|
457 |
if (b == (double)(1 - neg))
|
alpar@1
|
458 |
{ /* it is packing inequality; no processing is needed */
|
alpar@1
|
459 |
return 1;
|
alpar@1
|
460 |
}
|
alpar@1
|
461 |
}
|
alpar@1
|
462 |
/* substitute x[j] = 1 - x~[j] for all j in Jn to make all a[j]
|
alpar@1
|
463 |
positive; the result is a~[j] = |a[j]| and new rhs b */
|
alpar@1
|
464 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
465 |
if (e->aj < 0) b -= e->aj;
|
alpar@1
|
466 |
/* now a[j] > 0 for all j in J (actually |a[j]| are used) */
|
alpar@1
|
467 |
/* if a[j] > b, skip processing--this case must not appear */
|
alpar@1
|
468 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
469 |
if (fabs(e->aj) > b) return 0;
|
alpar@1
|
470 |
/* now 0 < a[j] <= b for all j in J */
|
alpar@1
|
471 |
/* find two minimal coefficients a[j] and a[k], j != k */
|
alpar@1
|
472 |
ej = NULL;
|
alpar@1
|
473 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
474 |
if (ej == NULL || fabs(ej->aj) > fabs(e->aj)) ej = e;
|
alpar@1
|
475 |
xassert(ej != NULL);
|
alpar@1
|
476 |
ek = NULL;
|
alpar@1
|
477 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
478 |
if (e != ej)
|
alpar@1
|
479 |
if (ek == NULL || fabs(ek->aj) > fabs(e->aj)) ek = e;
|
alpar@1
|
480 |
xassert(ek != NULL);
|
alpar@1
|
481 |
/* the specified constraint is equivalent to packing inequality
|
alpar@1
|
482 |
iff a[j] + a[k] > b + eps */
|
alpar@1
|
483 |
eps = 1e-3 + 1e-6 * fabs(b);
|
alpar@1
|
484 |
if (fabs(ej->aj) + fabs(ek->aj) <= b + eps) return 0;
|
alpar@1
|
485 |
/* perform back substitution x~[j] = 1 - x[j] and construct the
|
alpar@1
|
486 |
final equivalent packing inequality in generalized format */
|
alpar@1
|
487 |
b = 1.0;
|
alpar@1
|
488 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
489 |
{ if (e->aj > 0.0)
|
alpar@1
|
490 |
e->aj = +1.0;
|
alpar@1
|
491 |
else /* e->aj < 0.0 */
|
alpar@1
|
492 |
e->aj = -1.0, b -= 1.0;
|
alpar@1
|
493 |
}
|
alpar@1
|
494 |
*_b = b;
|
alpar@1
|
495 |
return 2;
|
alpar@1
|
496 |
}
|
alpar@1
|
497 |
|
alpar@1
|
498 |
int npp_hidden_packing(NPP *npp, NPPROW *row)
|
alpar@1
|
499 |
{ /* identify hidden packing inequality */
|
alpar@1
|
500 |
NPPROW *copy;
|
alpar@1
|
501 |
NPPAIJ *aij;
|
alpar@1
|
502 |
struct elem *ptr, *e;
|
alpar@1
|
503 |
int kase, ret, count = 0;
|
alpar@1
|
504 |
double b;
|
alpar@1
|
505 |
/* the row must be inequality constraint */
|
alpar@1
|
506 |
xassert(row->lb < row->ub);
|
alpar@1
|
507 |
for (kase = 0; kase <= 1; kase++)
|
alpar@1
|
508 |
{ if (kase == 0)
|
alpar@1
|
509 |
{ /* process row upper bound */
|
alpar@1
|
510 |
if (row->ub == +DBL_MAX) continue;
|
alpar@1
|
511 |
ptr = copy_form(npp, row, +1.0);
|
alpar@1
|
512 |
b = + row->ub;
|
alpar@1
|
513 |
}
|
alpar@1
|
514 |
else
|
alpar@1
|
515 |
{ /* process row lower bound */
|
alpar@1
|
516 |
if (row->lb == -DBL_MAX) continue;
|
alpar@1
|
517 |
ptr = copy_form(npp, row, -1.0);
|
alpar@1
|
518 |
b = - row->lb;
|
alpar@1
|
519 |
}
|
alpar@1
|
520 |
/* now the inequality has the form "sum a[j] x[j] <= b" */
|
alpar@1
|
521 |
ret = hidden_packing(npp, ptr, &b);
|
alpar@1
|
522 |
xassert(0 <= ret && ret <= 2);
|
alpar@1
|
523 |
if (kase == 1 && ret == 1 || ret == 2)
|
alpar@1
|
524 |
{ /* the original inequality has been identified as hidden
|
alpar@1
|
525 |
packing inequality */
|
alpar@1
|
526 |
count++;
|
alpar@1
|
527 |
#ifdef GLP_DEBUG
|
alpar@1
|
528 |
xprintf("Original constraint:\n");
|
alpar@1
|
529 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
530 |
xprintf(" %+g x%d", aij->val, aij->col->j);
|
alpar@1
|
531 |
if (row->lb != -DBL_MAX) xprintf(", >= %g", row->lb);
|
alpar@1
|
532 |
if (row->ub != +DBL_MAX) xprintf(", <= %g", row->ub);
|
alpar@1
|
533 |
xprintf("\n");
|
alpar@1
|
534 |
xprintf("Equivalent packing inequality:\n");
|
alpar@1
|
535 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
536 |
xprintf(" %sx%d", e->aj > 0.0 ? "+" : "-", e->xj->j);
|
alpar@1
|
537 |
xprintf(", <= %g\n", b);
|
alpar@1
|
538 |
#endif
|
alpar@1
|
539 |
if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
|
alpar@1
|
540 |
{ /* the original row is single-sided inequality; no copy
|
alpar@1
|
541 |
is needed */
|
alpar@1
|
542 |
copy = NULL;
|
alpar@1
|
543 |
}
|
alpar@1
|
544 |
else
|
alpar@1
|
545 |
{ /* the original row is double-sided inequality; we need
|
alpar@1
|
546 |
to create its copy for other bound before replacing it
|
alpar@1
|
547 |
with the equivalent inequality */
|
alpar@1
|
548 |
copy = npp_add_row(npp);
|
alpar@1
|
549 |
if (kase == 0)
|
alpar@1
|
550 |
{ /* the copy is for lower bound */
|
alpar@1
|
551 |
copy->lb = row->lb, copy->ub = +DBL_MAX;
|
alpar@1
|
552 |
}
|
alpar@1
|
553 |
else
|
alpar@1
|
554 |
{ /* the copy is for upper bound */
|
alpar@1
|
555 |
copy->lb = -DBL_MAX, copy->ub = row->ub;
|
alpar@1
|
556 |
}
|
alpar@1
|
557 |
/* copy original row coefficients */
|
alpar@1
|
558 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
559 |
npp_add_aij(npp, copy, aij->col, aij->val);
|
alpar@1
|
560 |
}
|
alpar@1
|
561 |
/* replace the original inequality by equivalent one */
|
alpar@1
|
562 |
npp_erase_row(npp, row);
|
alpar@1
|
563 |
row->lb = -DBL_MAX, row->ub = b;
|
alpar@1
|
564 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
565 |
npp_add_aij(npp, row, e->xj, e->aj);
|
alpar@1
|
566 |
/* continue processing lower bound for the copy */
|
alpar@1
|
567 |
if (copy != NULL) row = copy;
|
alpar@1
|
568 |
}
|
alpar@1
|
569 |
drop_form(npp, ptr);
|
alpar@1
|
570 |
}
|
alpar@1
|
571 |
return count;
|
alpar@1
|
572 |
}
|
alpar@1
|
573 |
|
alpar@1
|
574 |
/***********************************************************************
|
alpar@1
|
575 |
* NAME
|
alpar@1
|
576 |
*
|
alpar@1
|
577 |
* npp_implied_packing - identify implied packing inequality
|
alpar@1
|
578 |
*
|
alpar@1
|
579 |
* SYNOPSIS
|
alpar@1
|
580 |
*
|
alpar@1
|
581 |
* #include "glpnpp.h"
|
alpar@1
|
582 |
* int npp_implied_packing(NPP *npp, NPPROW *row, int which,
|
alpar@1
|
583 |
* NPPCOL *var[], char set[]);
|
alpar@1
|
584 |
*
|
alpar@1
|
585 |
* DESCRIPTION
|
alpar@1
|
586 |
*
|
alpar@1
|
587 |
* The routine npp_implied_packing processes specified row (constraint)
|
alpar@1
|
588 |
* of general format:
|
alpar@1
|
589 |
*
|
alpar@1
|
590 |
* L <= sum a[j] x[j] <= U. (1)
|
alpar@1
|
591 |
* j
|
alpar@1
|
592 |
*
|
alpar@1
|
593 |
* If which = 0, only lower bound L, which must exist, is considered,
|
alpar@1
|
594 |
* while upper bound U is ignored. Similarly, if which = 1, only upper
|
alpar@1
|
595 |
* bound U, which must exist, is considered, while lower bound L is
|
alpar@1
|
596 |
* ignored. Thus, if the specified row is a double-sided inequality or
|
alpar@1
|
597 |
* equality constraint, this routine should be called twice for both
|
alpar@1
|
598 |
* lower and upper bounds.
|
alpar@1
|
599 |
*
|
alpar@1
|
600 |
* The routine npp_implied_packing attempts to find a non-trivial (i.e.
|
alpar@1
|
601 |
* having not less than two binary variables) packing inequality:
|
alpar@1
|
602 |
*
|
alpar@1
|
603 |
* sum x[j] - sum x[j] <= 1 - |Jn|, (2)
|
alpar@1
|
604 |
* j in Jp j in Jn
|
alpar@1
|
605 |
*
|
alpar@1
|
606 |
* which is relaxation of the constraint (1) in the sense that any
|
alpar@1
|
607 |
* solution satisfying to that constraint also satisfies to the packing
|
alpar@1
|
608 |
* inequality (2). If such relaxation exists, the routine stores
|
alpar@1
|
609 |
* pointers to descriptors of corresponding binary variables and their
|
alpar@1
|
610 |
* flags, resp., to locations var[1], var[2], ..., var[len] and set[1],
|
alpar@1
|
611 |
* set[2], ..., set[len], where set[j] = 0 means that j in Jp and
|
alpar@1
|
612 |
* set[j] = 1 means that j in Jn.
|
alpar@1
|
613 |
*
|
alpar@1
|
614 |
* RETURNS
|
alpar@1
|
615 |
*
|
alpar@1
|
616 |
* The routine npp_implied_packing returns len, which is the total
|
alpar@1
|
617 |
* number of binary variables in the packing inequality found, len >= 2.
|
alpar@1
|
618 |
* However, if the relaxation does not exist, the routine returns zero.
|
alpar@1
|
619 |
*
|
alpar@1
|
620 |
* ALGORITHM
|
alpar@1
|
621 |
*
|
alpar@1
|
622 |
* If which = 0, the constraint coefficients (1) are multiplied by -1
|
alpar@1
|
623 |
* and b is assigned -L; if which = 1, the constraint coefficients (1)
|
alpar@1
|
624 |
* are not changed and b is assigned +U. In both cases the specified
|
alpar@1
|
625 |
* constraint gets the following format:
|
alpar@1
|
626 |
*
|
alpar@1
|
627 |
* sum a[j] x[j] <= b. (3)
|
alpar@1
|
628 |
* j
|
alpar@1
|
629 |
*
|
alpar@1
|
630 |
* (Note that (3) is a relaxation of (1), because one of bounds L or U
|
alpar@1
|
631 |
* is ignored.)
|
alpar@1
|
632 |
*
|
alpar@1
|
633 |
* Let J be set of binary variables, Kp be set of non-binary (integer
|
alpar@1
|
634 |
* or continuous) variables with a[j] > 0, and Kn be set of non-binary
|
alpar@1
|
635 |
* variables with a[j] < 0. Then the inequality (3) can be written as
|
alpar@1
|
636 |
* follows:
|
alpar@1
|
637 |
*
|
alpar@1
|
638 |
* sum a[j] x[j] <= b - sum a[j] x[j] - sum a[j] x[j]. (4)
|
alpar@1
|
639 |
* j in J j in Kp j in Kn
|
alpar@1
|
640 |
*
|
alpar@1
|
641 |
* To get rid of non-binary variables we can replace the inequality (4)
|
alpar@1
|
642 |
* by the following relaxed inequality:
|
alpar@1
|
643 |
*
|
alpar@1
|
644 |
* sum a[j] x[j] <= b~, (5)
|
alpar@1
|
645 |
* j in J
|
alpar@1
|
646 |
*
|
alpar@1
|
647 |
* where:
|
alpar@1
|
648 |
*
|
alpar@1
|
649 |
* b~ = sup(b - sum a[j] x[j] - sum a[j] x[j]) =
|
alpar@1
|
650 |
* j in Kp j in Kn
|
alpar@1
|
651 |
*
|
alpar@1
|
652 |
* = b - inf sum a[j] x[j] - inf sum a[j] x[j] = (6)
|
alpar@1
|
653 |
* j in Kp j in Kn
|
alpar@1
|
654 |
*
|
alpar@1
|
655 |
* = b - sum a[j] l[j] - sum a[j] u[j].
|
alpar@1
|
656 |
* j in Kp j in Kn
|
alpar@1
|
657 |
*
|
alpar@1
|
658 |
* Note that if lower bound l[j] (if j in Kp) or upper bound u[j]
|
alpar@1
|
659 |
* (if j in Kn) of some non-binary variable x[j] does not exist, then
|
alpar@1
|
660 |
* formally b = +oo, in which case further analysis is not performed.
|
alpar@1
|
661 |
*
|
alpar@1
|
662 |
* Let Bp = {j in J: a[j] > 0}, Bn = {j in J: a[j] < 0}. To make all
|
alpar@1
|
663 |
* the inequality coefficients in (5) positive, we replace all x[j] in
|
alpar@1
|
664 |
* Bn by their complementaries, substituting x[j] = 1 - x~[j] for all
|
alpar@1
|
665 |
* j in Bn, that gives:
|
alpar@1
|
666 |
*
|
alpar@1
|
667 |
* sum a[j] x[j] - sum a[j] x~[j] <= b~ - sum a[j]. (7)
|
alpar@1
|
668 |
* j in Bp j in Bn j in Bn
|
alpar@1
|
669 |
*
|
alpar@1
|
670 |
* This inequality is a relaxation of the original constraint (1), and
|
alpar@1
|
671 |
* it is a binary knapsack inequality. Writing it in the standard format
|
alpar@1
|
672 |
* we have:
|
alpar@1
|
673 |
*
|
alpar@1
|
674 |
* sum alfa[j] z[j] <= beta, (8)
|
alpar@1
|
675 |
* j in J
|
alpar@1
|
676 |
*
|
alpar@1
|
677 |
* where:
|
alpar@1
|
678 |
* ( + a[j], if j in Bp,
|
alpar@1
|
679 |
* alfa[j] = < (9)
|
alpar@1
|
680 |
* ( - a[j], if j in Bn,
|
alpar@1
|
681 |
*
|
alpar@1
|
682 |
* ( x[j], if j in Bp,
|
alpar@1
|
683 |
* z[j] = < (10)
|
alpar@1
|
684 |
* ( 1 - x[j], if j in Bn,
|
alpar@1
|
685 |
*
|
alpar@1
|
686 |
* beta = b~ - sum a[j]. (11)
|
alpar@1
|
687 |
* j in Bn
|
alpar@1
|
688 |
*
|
alpar@1
|
689 |
* In the inequality (8) all coefficients are positive, therefore, the
|
alpar@1
|
690 |
* packing relaxation to be found for this inequality is the following:
|
alpar@1
|
691 |
*
|
alpar@1
|
692 |
* sum z[j] <= 1. (12)
|
alpar@1
|
693 |
* j in P
|
alpar@1
|
694 |
*
|
alpar@1
|
695 |
* It is obvious that set P within J, which we would like to find, must
|
alpar@1
|
696 |
* satisfy to the following condition:
|
alpar@1
|
697 |
*
|
alpar@1
|
698 |
* alfa[j] + alfa[k] > beta + eps for all j, k in P, j != k, (13)
|
alpar@1
|
699 |
*
|
alpar@1
|
700 |
* where eps is an absolute tolerance for value of the linear form.
|
alpar@1
|
701 |
* Thus, it is natural to take P = {j: alpha[j] > (beta + eps) / 2}.
|
alpar@1
|
702 |
* Moreover, if in the equality (8) there exist coefficients alfa[k],
|
alpar@1
|
703 |
* for which alfa[k] <= (beta + eps) / 2, but which, nevertheless,
|
alpar@1
|
704 |
* satisfies to the condition (13) for all j in P, *one* corresponding
|
alpar@1
|
705 |
* variable z[k] (having, for example, maximal coefficient alfa[k]) can
|
alpar@1
|
706 |
* be included in set P, that allows increasing the number of binary
|
alpar@1
|
707 |
* variables in (12) by one.
|
alpar@1
|
708 |
*
|
alpar@1
|
709 |
* Once the set P has been built, for the inequality (12) we need to
|
alpar@1
|
710 |
* perform back substitution according to (10) in order to express it
|
alpar@1
|
711 |
* through the original binary variables. As the result of such back
|
alpar@1
|
712 |
* substitution the relaxed packing inequality get its final format (2),
|
alpar@1
|
713 |
* where Jp = J intersect Bp, and Jn = J intersect Bn. */
|
alpar@1
|
714 |
|
alpar@1
|
715 |
int npp_implied_packing(NPP *npp, NPPROW *row, int which,
|
alpar@1
|
716 |
NPPCOL *var[], char set[])
|
alpar@1
|
717 |
{ struct elem *ptr, *e, *i, *k;
|
alpar@1
|
718 |
int len = 0;
|
alpar@1
|
719 |
double b, eps;
|
alpar@1
|
720 |
/* build inequality (3) */
|
alpar@1
|
721 |
if (which == 0)
|
alpar@1
|
722 |
{ ptr = copy_form(npp, row, -1.0);
|
alpar@1
|
723 |
xassert(row->lb != -DBL_MAX);
|
alpar@1
|
724 |
b = - row->lb;
|
alpar@1
|
725 |
}
|
alpar@1
|
726 |
else if (which == 1)
|
alpar@1
|
727 |
{ ptr = copy_form(npp, row, +1.0);
|
alpar@1
|
728 |
xassert(row->ub != +DBL_MAX);
|
alpar@1
|
729 |
b = + row->ub;
|
alpar@1
|
730 |
}
|
alpar@1
|
731 |
/* remove non-binary variables to build relaxed inequality (5);
|
alpar@1
|
732 |
compute its right-hand side b~ with formula (6) */
|
alpar@1
|
733 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
734 |
{ if (!(e->xj->is_int && e->xj->lb == 0.0 && e->xj->ub == 1.0))
|
alpar@1
|
735 |
{ /* x[j] is non-binary variable */
|
alpar@1
|
736 |
if (e->aj > 0.0)
|
alpar@1
|
737 |
{ if (e->xj->lb == -DBL_MAX) goto done;
|
alpar@1
|
738 |
b -= e->aj * e->xj->lb;
|
alpar@1
|
739 |
}
|
alpar@1
|
740 |
else /* e->aj < 0.0 */
|
alpar@1
|
741 |
{ if (e->xj->ub == +DBL_MAX) goto done;
|
alpar@1
|
742 |
b -= e->aj * e->xj->ub;
|
alpar@1
|
743 |
}
|
alpar@1
|
744 |
/* a[j] = 0 means that variable x[j] is removed */
|
alpar@1
|
745 |
e->aj = 0.0;
|
alpar@1
|
746 |
}
|
alpar@1
|
747 |
}
|
alpar@1
|
748 |
/* substitute x[j] = 1 - x~[j] to build knapsack inequality (8);
|
alpar@1
|
749 |
compute its right-hand side beta with formula (11) */
|
alpar@1
|
750 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
751 |
if (e->aj < 0.0) b -= e->aj;
|
alpar@1
|
752 |
/* if beta is close to zero, the knapsack inequality is either
|
alpar@1
|
753 |
infeasible or forcing inequality; this must never happen, so
|
alpar@1
|
754 |
we skip further analysis */
|
alpar@1
|
755 |
if (b < 1e-3) goto done;
|
alpar@1
|
756 |
/* build set P as well as sets Jp and Jn, and determine x[k] as
|
alpar@1
|
757 |
explained above in comments to the routine */
|
alpar@1
|
758 |
eps = 1e-3 + 1e-6 * b;
|
alpar@1
|
759 |
i = k = NULL;
|
alpar@1
|
760 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
761 |
{ /* note that alfa[j] = |a[j]| */
|
alpar@1
|
762 |
if (fabs(e->aj) > 0.5 * (b + eps))
|
alpar@1
|
763 |
{ /* alfa[j] > (b + eps) / 2; include x[j] in set P, i.e. in
|
alpar@1
|
764 |
set Jp or Jn */
|
alpar@1
|
765 |
var[++len] = e->xj;
|
alpar@1
|
766 |
set[len] = (char)(e->aj > 0.0 ? 0 : 1);
|
alpar@1
|
767 |
/* alfa[i] = min alfa[j] over all j included in set P */
|
alpar@1
|
768 |
if (i == NULL || fabs(i->aj) > fabs(e->aj)) i = e;
|
alpar@1
|
769 |
}
|
alpar@1
|
770 |
else if (fabs(e->aj) >= 1e-3)
|
alpar@1
|
771 |
{ /* alfa[k] = max alfa[j] over all j not included in set P;
|
alpar@1
|
772 |
we skip coefficient a[j] if it is close to zero to avoid
|
alpar@1
|
773 |
numerically unreliable results */
|
alpar@1
|
774 |
if (k == NULL || fabs(k->aj) < fabs(e->aj)) k = e;
|
alpar@1
|
775 |
}
|
alpar@1
|
776 |
}
|
alpar@1
|
777 |
/* if alfa[k] satisfies to condition (13) for all j in P, include
|
alpar@1
|
778 |
x[k] in P */
|
alpar@1
|
779 |
if (i != NULL && k != NULL && fabs(i->aj) + fabs(k->aj) > b + eps)
|
alpar@1
|
780 |
{ var[++len] = k->xj;
|
alpar@1
|
781 |
set[len] = (char)(k->aj > 0.0 ? 0 : 1);
|
alpar@1
|
782 |
}
|
alpar@1
|
783 |
/* trivial packing inequality being redundant must never appear,
|
alpar@1
|
784 |
so we just ignore it */
|
alpar@1
|
785 |
if (len < 2) len = 0;
|
alpar@1
|
786 |
done: drop_form(npp, ptr);
|
alpar@1
|
787 |
return len;
|
alpar@1
|
788 |
}
|
alpar@1
|
789 |
|
alpar@1
|
790 |
/***********************************************************************
|
alpar@1
|
791 |
* NAME
|
alpar@1
|
792 |
*
|
alpar@1
|
793 |
* npp_is_covering - test if constraint is covering inequality
|
alpar@1
|
794 |
*
|
alpar@1
|
795 |
* SYNOPSIS
|
alpar@1
|
796 |
*
|
alpar@1
|
797 |
* #include "glpnpp.h"
|
alpar@1
|
798 |
* int npp_is_covering(NPP *npp, NPPROW *row);
|
alpar@1
|
799 |
*
|
alpar@1
|
800 |
* RETURNS
|
alpar@1
|
801 |
*
|
alpar@1
|
802 |
* If the specified row (constraint) is covering inequality (see below),
|
alpar@1
|
803 |
* the routine npp_is_covering returns non-zero. Otherwise, it returns
|
alpar@1
|
804 |
* zero.
|
alpar@1
|
805 |
*
|
alpar@1
|
806 |
* COVERING INEQUALITIES
|
alpar@1
|
807 |
*
|
alpar@1
|
808 |
* In canonical format the covering inequality is the following:
|
alpar@1
|
809 |
*
|
alpar@1
|
810 |
* sum x[j] >= 1, (1)
|
alpar@1
|
811 |
* j in J
|
alpar@1
|
812 |
*
|
alpar@1
|
813 |
* where all variables x[j] are binary. This inequality expresses the
|
alpar@1
|
814 |
* condition that in any integer feasible solution variables in set J
|
alpar@1
|
815 |
* cannot be all equal to zero at the same time, i.e. at least one
|
alpar@1
|
816 |
* variable must take non-zero (unity) value. W.l.o.g. it is assumed
|
alpar@1
|
817 |
* that |J| >= 2, because if J is empty, the inequality (1) is
|
alpar@1
|
818 |
* infeasible, and if |J| = 1, the inequality (1) is a forcing row.
|
alpar@1
|
819 |
*
|
alpar@1
|
820 |
* In general case the covering inequality may include original
|
alpar@1
|
821 |
* variables x[j] as well as their complements x~[j]:
|
alpar@1
|
822 |
*
|
alpar@1
|
823 |
* sum x[j] + sum x~[j] >= 1, (2)
|
alpar@1
|
824 |
* j in Jp j in Jn
|
alpar@1
|
825 |
*
|
alpar@1
|
826 |
* where Jp and Jn are not intersected. Therefore, using substitution
|
alpar@1
|
827 |
* x~[j] = 1 - x[j] gives the packing inequality in generalized format:
|
alpar@1
|
828 |
*
|
alpar@1
|
829 |
* sum x[j] - sum x[j] >= 1 - |Jn|. (3)
|
alpar@1
|
830 |
* j in Jp j in Jn
|
alpar@1
|
831 |
*
|
alpar@1
|
832 |
* (May note that the inequality (3) cuts off infeasible solutions,
|
alpar@1
|
833 |
* where x[j] = 0 for all j in Jp and x[j] = 1 for all j in Jn.)
|
alpar@1
|
834 |
*
|
alpar@1
|
835 |
* NOTE: If |J| = 2, the inequality (3) is equivalent to packing
|
alpar@1
|
836 |
* inequality (see the routine npp_is_packing). */
|
alpar@1
|
837 |
|
alpar@1
|
838 |
int npp_is_covering(NPP *npp, NPPROW *row)
|
alpar@1
|
839 |
{ /* test if constraint is covering inequality */
|
alpar@1
|
840 |
NPPCOL *col;
|
alpar@1
|
841 |
NPPAIJ *aij;
|
alpar@1
|
842 |
int b;
|
alpar@1
|
843 |
xassert(npp == npp);
|
alpar@1
|
844 |
if (!(row->lb != -DBL_MAX && row->ub == +DBL_MAX))
|
alpar@1
|
845 |
return 0;
|
alpar@1
|
846 |
b = 1;
|
alpar@1
|
847 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
848 |
{ col = aij->col;
|
alpar@1
|
849 |
if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
|
alpar@1
|
850 |
return 0;
|
alpar@1
|
851 |
if (aij->val == +1.0)
|
alpar@1
|
852 |
;
|
alpar@1
|
853 |
else if (aij->val == -1.0)
|
alpar@1
|
854 |
b--;
|
alpar@1
|
855 |
else
|
alpar@1
|
856 |
return 0;
|
alpar@1
|
857 |
}
|
alpar@1
|
858 |
if (row->lb != (double)b) return 0;
|
alpar@1
|
859 |
return 1;
|
alpar@1
|
860 |
}
|
alpar@1
|
861 |
|
alpar@1
|
862 |
/***********************************************************************
|
alpar@1
|
863 |
* NAME
|
alpar@1
|
864 |
*
|
alpar@1
|
865 |
* npp_hidden_covering - identify hidden covering inequality
|
alpar@1
|
866 |
*
|
alpar@1
|
867 |
* SYNOPSIS
|
alpar@1
|
868 |
*
|
alpar@1
|
869 |
* #include "glpnpp.h"
|
alpar@1
|
870 |
* int npp_hidden_covering(NPP *npp, NPPROW *row);
|
alpar@1
|
871 |
*
|
alpar@1
|
872 |
* DESCRIPTION
|
alpar@1
|
873 |
*
|
alpar@1
|
874 |
* The routine npp_hidden_covering processes specified inequality
|
alpar@1
|
875 |
* constraint, which includes only binary variables, and the number of
|
alpar@1
|
876 |
* the variables is not less than three. If the original inequality is
|
alpar@1
|
877 |
* equivalent to a covering inequality (see below), the routine
|
alpar@1
|
878 |
* replaces it by the equivalent inequality. If the original constraint
|
alpar@1
|
879 |
* is double-sided inequality, it is replaced by a pair of single-sided
|
alpar@1
|
880 |
* inequalities, if necessary.
|
alpar@1
|
881 |
*
|
alpar@1
|
882 |
* RETURNS
|
alpar@1
|
883 |
*
|
alpar@1
|
884 |
* If the original inequality constraint was replaced by equivalent
|
alpar@1
|
885 |
* covering inequality, the routine npp_hidden_covering returns
|
alpar@1
|
886 |
* non-zero. Otherwise, it returns zero.
|
alpar@1
|
887 |
*
|
alpar@1
|
888 |
* PROBLEM TRANSFORMATION
|
alpar@1
|
889 |
*
|
alpar@1
|
890 |
* Consider an inequality constraint:
|
alpar@1
|
891 |
*
|
alpar@1
|
892 |
* sum a[j] x[j] >= b, (1)
|
alpar@1
|
893 |
* j in J
|
alpar@1
|
894 |
*
|
alpar@1
|
895 |
* where all variables x[j] are binary, and |J| >= 3. (In case of '<='
|
alpar@1
|
896 |
* inequality it can be transformed to '>=' format by multiplying both
|
alpar@1
|
897 |
* its sides by -1.)
|
alpar@1
|
898 |
*
|
alpar@1
|
899 |
* Let Jp = {j: a[j] > 0}, Jn = {j: a[j] < 0}. Performing substitution
|
alpar@1
|
900 |
* x[j] = 1 - x~[j] for all j in Jn, we have:
|
alpar@1
|
901 |
*
|
alpar@1
|
902 |
* sum a[j] x[j] >= b ==>
|
alpar@1
|
903 |
* j in J
|
alpar@1
|
904 |
*
|
alpar@1
|
905 |
* sum a[j] x[j] + sum a[j] x[j] >= b ==>
|
alpar@1
|
906 |
* j in Jp j in Jn
|
alpar@1
|
907 |
*
|
alpar@1
|
908 |
* sum a[j] x[j] + sum a[j] (1 - x~[j]) >= b ==>
|
alpar@1
|
909 |
* j in Jp j in Jn
|
alpar@1
|
910 |
*
|
alpar@1
|
911 |
* sum m a[j] x[j] - sum a[j] x~[j] >= b - sum a[j].
|
alpar@1
|
912 |
* j in Jp j in Jn j in Jn
|
alpar@1
|
913 |
*
|
alpar@1
|
914 |
* Thus, meaning the transformation above, we can assume that in
|
alpar@1
|
915 |
* inequality (1) all coefficients a[j] are positive. Moreover, we can
|
alpar@1
|
916 |
* assume that b > 0, because otherwise the inequality (1) would be
|
alpar@1
|
917 |
* redundant (see the routine npp_analyze_row). It is then obvious that
|
alpar@1
|
918 |
* constraint (1) is equivalent to covering inequality only if:
|
alpar@1
|
919 |
*
|
alpar@1
|
920 |
* a[j] >= b, (2)
|
alpar@1
|
921 |
*
|
alpar@1
|
922 |
* for all j in J.
|
alpar@1
|
923 |
*
|
alpar@1
|
924 |
* Once the original inequality (1) is replaced by equivalent covering
|
alpar@1
|
925 |
* inequality, we need to perform back substitution x~[j] = 1 - x[j] for
|
alpar@1
|
926 |
* all j in Jn (see above).
|
alpar@1
|
927 |
*
|
alpar@1
|
928 |
* RECOVERING SOLUTION
|
alpar@1
|
929 |
*
|
alpar@1
|
930 |
* None needed. */
|
alpar@1
|
931 |
|
alpar@1
|
932 |
static int hidden_covering(NPP *npp, struct elem *ptr, double *_b)
|
alpar@1
|
933 |
{ /* process inequality constraint: sum a[j] x[j] >= b;
|
alpar@1
|
934 |
0 - specified row is NOT hidden covering inequality;
|
alpar@1
|
935 |
1 - specified row is covering inequality;
|
alpar@1
|
936 |
2 - specified row is hidden covering inequality. */
|
alpar@1
|
937 |
struct elem *e;
|
alpar@1
|
938 |
int neg;
|
alpar@1
|
939 |
double b = *_b, eps;
|
alpar@1
|
940 |
xassert(npp == npp);
|
alpar@1
|
941 |
/* a[j] must be non-zero, x[j] must be binary, for all j in J */
|
alpar@1
|
942 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
943 |
{ xassert(e->aj != 0.0);
|
alpar@1
|
944 |
xassert(e->xj->is_int);
|
alpar@1
|
945 |
xassert(e->xj->lb == 0.0 && e->xj->ub == 1.0);
|
alpar@1
|
946 |
}
|
alpar@1
|
947 |
/* check if the specified inequality constraint already has the
|
alpar@1
|
948 |
form of covering inequality */
|
alpar@1
|
949 |
neg = 0; /* neg is |Jn| */
|
alpar@1
|
950 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
951 |
{ if (e->aj == +1.0)
|
alpar@1
|
952 |
;
|
alpar@1
|
953 |
else if (e->aj == -1.0)
|
alpar@1
|
954 |
neg++;
|
alpar@1
|
955 |
else
|
alpar@1
|
956 |
break;
|
alpar@1
|
957 |
}
|
alpar@1
|
958 |
if (e == NULL)
|
alpar@1
|
959 |
{ /* all coefficients a[j] are +1 or -1; check rhs b */
|
alpar@1
|
960 |
if (b == (double)(1 - neg))
|
alpar@1
|
961 |
{ /* it is covering inequality; no processing is needed */
|
alpar@1
|
962 |
return 1;
|
alpar@1
|
963 |
}
|
alpar@1
|
964 |
}
|
alpar@1
|
965 |
/* substitute x[j] = 1 - x~[j] for all j in Jn to make all a[j]
|
alpar@1
|
966 |
positive; the result is a~[j] = |a[j]| and new rhs b */
|
alpar@1
|
967 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
968 |
if (e->aj < 0) b -= e->aj;
|
alpar@1
|
969 |
/* now a[j] > 0 for all j in J (actually |a[j]| are used) */
|
alpar@1
|
970 |
/* if b <= 0, skip processing--this case must not appear */
|
alpar@1
|
971 |
if (b < 1e-3) return 0;
|
alpar@1
|
972 |
/* now a[j] > 0 for all j in J, and b > 0 */
|
alpar@1
|
973 |
/* the specified constraint is equivalent to covering inequality
|
alpar@1
|
974 |
iff a[j] >= b for all j in J */
|
alpar@1
|
975 |
eps = 1e-9 + 1e-12 * fabs(b);
|
alpar@1
|
976 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
977 |
if (fabs(e->aj) < b - eps) return 0;
|
alpar@1
|
978 |
/* perform back substitution x~[j] = 1 - x[j] and construct the
|
alpar@1
|
979 |
final equivalent covering inequality in generalized format */
|
alpar@1
|
980 |
b = 1.0;
|
alpar@1
|
981 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
982 |
{ if (e->aj > 0.0)
|
alpar@1
|
983 |
e->aj = +1.0;
|
alpar@1
|
984 |
else /* e->aj < 0.0 */
|
alpar@1
|
985 |
e->aj = -1.0, b -= 1.0;
|
alpar@1
|
986 |
}
|
alpar@1
|
987 |
*_b = b;
|
alpar@1
|
988 |
return 2;
|
alpar@1
|
989 |
}
|
alpar@1
|
990 |
|
alpar@1
|
991 |
int npp_hidden_covering(NPP *npp, NPPROW *row)
|
alpar@1
|
992 |
{ /* identify hidden covering inequality */
|
alpar@1
|
993 |
NPPROW *copy;
|
alpar@1
|
994 |
NPPAIJ *aij;
|
alpar@1
|
995 |
struct elem *ptr, *e;
|
alpar@1
|
996 |
int kase, ret, count = 0;
|
alpar@1
|
997 |
double b;
|
alpar@1
|
998 |
/* the row must be inequality constraint */
|
alpar@1
|
999 |
xassert(row->lb < row->ub);
|
alpar@1
|
1000 |
for (kase = 0; kase <= 1; kase++)
|
alpar@1
|
1001 |
{ if (kase == 0)
|
alpar@1
|
1002 |
{ /* process row lower bound */
|
alpar@1
|
1003 |
if (row->lb == -DBL_MAX) continue;
|
alpar@1
|
1004 |
ptr = copy_form(npp, row, +1.0);
|
alpar@1
|
1005 |
b = + row->lb;
|
alpar@1
|
1006 |
}
|
alpar@1
|
1007 |
else
|
alpar@1
|
1008 |
{ /* process row upper bound */
|
alpar@1
|
1009 |
if (row->ub == +DBL_MAX) continue;
|
alpar@1
|
1010 |
ptr = copy_form(npp, row, -1.0);
|
alpar@1
|
1011 |
b = - row->ub;
|
alpar@1
|
1012 |
}
|
alpar@1
|
1013 |
/* now the inequality has the form "sum a[j] x[j] >= b" */
|
alpar@1
|
1014 |
ret = hidden_covering(npp, ptr, &b);
|
alpar@1
|
1015 |
xassert(0 <= ret && ret <= 2);
|
alpar@1
|
1016 |
if (kase == 1 && ret == 1 || ret == 2)
|
alpar@1
|
1017 |
{ /* the original inequality has been identified as hidden
|
alpar@1
|
1018 |
covering inequality */
|
alpar@1
|
1019 |
count++;
|
alpar@1
|
1020 |
#ifdef GLP_DEBUG
|
alpar@1
|
1021 |
xprintf("Original constraint:\n");
|
alpar@1
|
1022 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
1023 |
xprintf(" %+g x%d", aij->val, aij->col->j);
|
alpar@1
|
1024 |
if (row->lb != -DBL_MAX) xprintf(", >= %g", row->lb);
|
alpar@1
|
1025 |
if (row->ub != +DBL_MAX) xprintf(", <= %g", row->ub);
|
alpar@1
|
1026 |
xprintf("\n");
|
alpar@1
|
1027 |
xprintf("Equivalent covering inequality:\n");
|
alpar@1
|
1028 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
1029 |
xprintf(" %sx%d", e->aj > 0.0 ? "+" : "-", e->xj->j);
|
alpar@1
|
1030 |
xprintf(", >= %g\n", b);
|
alpar@1
|
1031 |
#endif
|
alpar@1
|
1032 |
if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
|
alpar@1
|
1033 |
{ /* the original row is single-sided inequality; no copy
|
alpar@1
|
1034 |
is needed */
|
alpar@1
|
1035 |
copy = NULL;
|
alpar@1
|
1036 |
}
|
alpar@1
|
1037 |
else
|
alpar@1
|
1038 |
{ /* the original row is double-sided inequality; we need
|
alpar@1
|
1039 |
to create its copy for other bound before replacing it
|
alpar@1
|
1040 |
with the equivalent inequality */
|
alpar@1
|
1041 |
copy = npp_add_row(npp);
|
alpar@1
|
1042 |
if (kase == 0)
|
alpar@1
|
1043 |
{ /* the copy is for upper bound */
|
alpar@1
|
1044 |
copy->lb = -DBL_MAX, copy->ub = row->ub;
|
alpar@1
|
1045 |
}
|
alpar@1
|
1046 |
else
|
alpar@1
|
1047 |
{ /* the copy is for lower bound */
|
alpar@1
|
1048 |
copy->lb = row->lb, copy->ub = +DBL_MAX;
|
alpar@1
|
1049 |
}
|
alpar@1
|
1050 |
/* copy original row coefficients */
|
alpar@1
|
1051 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
1052 |
npp_add_aij(npp, copy, aij->col, aij->val);
|
alpar@1
|
1053 |
}
|
alpar@1
|
1054 |
/* replace the original inequality by equivalent one */
|
alpar@1
|
1055 |
npp_erase_row(npp, row);
|
alpar@1
|
1056 |
row->lb = b, row->ub = +DBL_MAX;
|
alpar@1
|
1057 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
1058 |
npp_add_aij(npp, row, e->xj, e->aj);
|
alpar@1
|
1059 |
/* continue processing upper bound for the copy */
|
alpar@1
|
1060 |
if (copy != NULL) row = copy;
|
alpar@1
|
1061 |
}
|
alpar@1
|
1062 |
drop_form(npp, ptr);
|
alpar@1
|
1063 |
}
|
alpar@1
|
1064 |
return count;
|
alpar@1
|
1065 |
}
|
alpar@1
|
1066 |
|
alpar@1
|
1067 |
/***********************************************************************
|
alpar@1
|
1068 |
* NAME
|
alpar@1
|
1069 |
*
|
alpar@1
|
1070 |
* npp_is_partitioning - test if constraint is partitioning equality
|
alpar@1
|
1071 |
*
|
alpar@1
|
1072 |
* SYNOPSIS
|
alpar@1
|
1073 |
*
|
alpar@1
|
1074 |
* #include "glpnpp.h"
|
alpar@1
|
1075 |
* int npp_is_partitioning(NPP *npp, NPPROW *row);
|
alpar@1
|
1076 |
*
|
alpar@1
|
1077 |
* RETURNS
|
alpar@1
|
1078 |
*
|
alpar@1
|
1079 |
* If the specified row (constraint) is partitioning equality (see
|
alpar@1
|
1080 |
* below), the routine npp_is_partitioning returns non-zero. Otherwise,
|
alpar@1
|
1081 |
* it returns zero.
|
alpar@1
|
1082 |
*
|
alpar@1
|
1083 |
* PARTITIONING EQUALITIES
|
alpar@1
|
1084 |
*
|
alpar@1
|
1085 |
* In canonical format the partitioning equality is the following:
|
alpar@1
|
1086 |
*
|
alpar@1
|
1087 |
* sum x[j] = 1, (1)
|
alpar@1
|
1088 |
* j in J
|
alpar@1
|
1089 |
*
|
alpar@1
|
1090 |
* where all variables x[j] are binary. This equality expresses the
|
alpar@1
|
1091 |
* condition that in any integer feasible solution exactly one variable
|
alpar@1
|
1092 |
* in set J must take non-zero (unity) value while other variables must
|
alpar@1
|
1093 |
* be equal to zero. W.l.o.g. it is assumed that |J| >= 2, because if
|
alpar@1
|
1094 |
* J is empty, the inequality (1) is infeasible, and if |J| = 1, the
|
alpar@1
|
1095 |
* inequality (1) is a fixing row.
|
alpar@1
|
1096 |
*
|
alpar@1
|
1097 |
* In general case the partitioning equality may include original
|
alpar@1
|
1098 |
* variables x[j] as well as their complements x~[j]:
|
alpar@1
|
1099 |
*
|
alpar@1
|
1100 |
* sum x[j] + sum x~[j] = 1, (2)
|
alpar@1
|
1101 |
* j in Jp j in Jn
|
alpar@1
|
1102 |
*
|
alpar@1
|
1103 |
* where Jp and Jn are not intersected. Therefore, using substitution
|
alpar@1
|
1104 |
* x~[j] = 1 - x[j] leads to the partitioning equality in generalized
|
alpar@1
|
1105 |
* format:
|
alpar@1
|
1106 |
*
|
alpar@1
|
1107 |
* sum x[j] - sum x[j] = 1 - |Jn|. (3)
|
alpar@1
|
1108 |
* j in Jp j in Jn */
|
alpar@1
|
1109 |
|
alpar@1
|
1110 |
int npp_is_partitioning(NPP *npp, NPPROW *row)
|
alpar@1
|
1111 |
{ /* test if constraint is partitioning equality */
|
alpar@1
|
1112 |
NPPCOL *col;
|
alpar@1
|
1113 |
NPPAIJ *aij;
|
alpar@1
|
1114 |
int b;
|
alpar@1
|
1115 |
xassert(npp == npp);
|
alpar@1
|
1116 |
if (row->lb != row->ub) return 0;
|
alpar@1
|
1117 |
b = 1;
|
alpar@1
|
1118 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
1119 |
{ col = aij->col;
|
alpar@1
|
1120 |
if (!(col->is_int && col->lb == 0.0 && col->ub == 1.0))
|
alpar@1
|
1121 |
return 0;
|
alpar@1
|
1122 |
if (aij->val == +1.0)
|
alpar@1
|
1123 |
;
|
alpar@1
|
1124 |
else if (aij->val == -1.0)
|
alpar@1
|
1125 |
b--;
|
alpar@1
|
1126 |
else
|
alpar@1
|
1127 |
return 0;
|
alpar@1
|
1128 |
}
|
alpar@1
|
1129 |
if (row->lb != (double)b) return 0;
|
alpar@1
|
1130 |
return 1;
|
alpar@1
|
1131 |
}
|
alpar@1
|
1132 |
|
alpar@1
|
1133 |
/***********************************************************************
|
alpar@1
|
1134 |
* NAME
|
alpar@1
|
1135 |
*
|
alpar@1
|
1136 |
* npp_reduce_ineq_coef - reduce inequality constraint coefficients
|
alpar@1
|
1137 |
*
|
alpar@1
|
1138 |
* SYNOPSIS
|
alpar@1
|
1139 |
*
|
alpar@1
|
1140 |
* #include "glpnpp.h"
|
alpar@1
|
1141 |
* int npp_reduce_ineq_coef(NPP *npp, NPPROW *row);
|
alpar@1
|
1142 |
*
|
alpar@1
|
1143 |
* DESCRIPTION
|
alpar@1
|
1144 |
*
|
alpar@1
|
1145 |
* The routine npp_reduce_ineq_coef processes specified inequality
|
alpar@1
|
1146 |
* constraint attempting to replace it by an equivalent constraint,
|
alpar@1
|
1147 |
* where magnitude of coefficients at binary variables is smaller than
|
alpar@1
|
1148 |
* in the original constraint. If the inequality is double-sided, it is
|
alpar@1
|
1149 |
* replaced by a pair of single-sided inequalities, if necessary.
|
alpar@1
|
1150 |
*
|
alpar@1
|
1151 |
* RETURNS
|
alpar@1
|
1152 |
*
|
alpar@1
|
1153 |
* The routine npp_reduce_ineq_coef returns the number of coefficients
|
alpar@1
|
1154 |
* reduced.
|
alpar@1
|
1155 |
*
|
alpar@1
|
1156 |
* BACKGROUND
|
alpar@1
|
1157 |
*
|
alpar@1
|
1158 |
* Consider an inequality constraint:
|
alpar@1
|
1159 |
*
|
alpar@1
|
1160 |
* sum a[j] x[j] >= b. (1)
|
alpar@1
|
1161 |
* j in J
|
alpar@1
|
1162 |
*
|
alpar@1
|
1163 |
* (In case of '<=' inequality it can be transformed to '>=' format by
|
alpar@1
|
1164 |
* multiplying both its sides by -1.) Let x[k] be a binary variable;
|
alpar@1
|
1165 |
* other variables can be integer as well as continuous. We can write
|
alpar@1
|
1166 |
* constraint (1) as follows:
|
alpar@1
|
1167 |
*
|
alpar@1
|
1168 |
* a[k] x[k] + t[k] >= b, (2)
|
alpar@1
|
1169 |
*
|
alpar@1
|
1170 |
* where:
|
alpar@1
|
1171 |
*
|
alpar@1
|
1172 |
* t[k] = sum a[j] x[j]. (3)
|
alpar@1
|
1173 |
* j in J\{k}
|
alpar@1
|
1174 |
*
|
alpar@1
|
1175 |
* Since x[k] is binary, constraint (2) is equivalent to disjunction of
|
alpar@1
|
1176 |
* the following two constraints:
|
alpar@1
|
1177 |
*
|
alpar@1
|
1178 |
* x[k] = 0, t[k] >= b (4)
|
alpar@1
|
1179 |
*
|
alpar@1
|
1180 |
* OR
|
alpar@1
|
1181 |
*
|
alpar@1
|
1182 |
* x[k] = 1, t[k] >= b - a[k]. (5)
|
alpar@1
|
1183 |
*
|
alpar@1
|
1184 |
* Let also that for the partial sum t[k] be known some its implied
|
alpar@1
|
1185 |
* lower bound inf t[k].
|
alpar@1
|
1186 |
*
|
alpar@1
|
1187 |
* Case a[k] > 0. Let inf t[k] < b, since otherwise both constraints
|
alpar@1
|
1188 |
* (4) and (5) and therefore constraint (2) are redundant.
|
alpar@1
|
1189 |
* If inf t[k] > b - a[k], only constraint (5) is redundant, in which
|
alpar@1
|
1190 |
* case it can be replaced with the following redundant and therefore
|
alpar@1
|
1191 |
* equivalent constraint:
|
alpar@1
|
1192 |
*
|
alpar@1
|
1193 |
* t[k] >= b - a'[k] = inf t[k], (6)
|
alpar@1
|
1194 |
*
|
alpar@1
|
1195 |
* where:
|
alpar@1
|
1196 |
*
|
alpar@1
|
1197 |
* a'[k] = b - inf t[k]. (7)
|
alpar@1
|
1198 |
*
|
alpar@1
|
1199 |
* Thus, the original constraint (2) is equivalent to the following
|
alpar@1
|
1200 |
* constraint with coefficient at variable x[k] changed:
|
alpar@1
|
1201 |
*
|
alpar@1
|
1202 |
* a'[k] x[k] + t[k] >= b. (8)
|
alpar@1
|
1203 |
*
|
alpar@1
|
1204 |
* From inf t[k] < b it follows that a'[k] > 0, i.e. the coefficient
|
alpar@1
|
1205 |
* at x[k] keeps its sign. And from inf t[k] > b - a[k] it follows that
|
alpar@1
|
1206 |
* a'[k] < a[k], i.e. the coefficient reduces in magnitude.
|
alpar@1
|
1207 |
*
|
alpar@1
|
1208 |
* Case a[k] < 0. Let inf t[k] < b - a[k], since otherwise both
|
alpar@1
|
1209 |
* constraints (4) and (5) and therefore constraint (2) are redundant.
|
alpar@1
|
1210 |
* If inf t[k] > b, only constraint (4) is redundant, in which case it
|
alpar@1
|
1211 |
* can be replaced with the following redundant and therefore equivalent
|
alpar@1
|
1212 |
* constraint:
|
alpar@1
|
1213 |
*
|
alpar@1
|
1214 |
* t[k] >= b' = inf t[k]. (9)
|
alpar@1
|
1215 |
*
|
alpar@1
|
1216 |
* Rewriting constraint (5) as follows:
|
alpar@1
|
1217 |
*
|
alpar@1
|
1218 |
* t[k] >= b - a[k] = b' - a'[k], (10)
|
alpar@1
|
1219 |
*
|
alpar@1
|
1220 |
* where:
|
alpar@1
|
1221 |
*
|
alpar@1
|
1222 |
* a'[k] = a[k] + b' - b = a[k] + inf t[k] - b, (11)
|
alpar@1
|
1223 |
*
|
alpar@1
|
1224 |
* we can see that disjunction of constraint (9) and (10) is equivalent
|
alpar@1
|
1225 |
* to disjunction of constraint (4) and (5), from which it follows that
|
alpar@1
|
1226 |
* the original constraint (2) is equivalent to the following constraint
|
alpar@1
|
1227 |
* with both coefficient at variable x[k] and right-hand side changed:
|
alpar@1
|
1228 |
*
|
alpar@1
|
1229 |
* a'[k] x[k] + t[k] >= b'. (12)
|
alpar@1
|
1230 |
*
|
alpar@1
|
1231 |
* From inf t[k] < b - a[k] it follows that a'[k] < 0, i.e. the
|
alpar@1
|
1232 |
* coefficient at x[k] keeps its sign. And from inf t[k] > b it follows
|
alpar@1
|
1233 |
* that a'[k] > a[k], i.e. the coefficient reduces in magnitude.
|
alpar@1
|
1234 |
*
|
alpar@1
|
1235 |
* PROBLEM TRANSFORMATION
|
alpar@1
|
1236 |
*
|
alpar@1
|
1237 |
* In the routine npp_reduce_ineq_coef the following implied lower
|
alpar@1
|
1238 |
* bound of the partial sum (3) is used:
|
alpar@1
|
1239 |
*
|
alpar@1
|
1240 |
* inf t[k] = sum a[j] l[j] + sum a[j] u[j], (13)
|
alpar@1
|
1241 |
* j in Jp\{k} k in Jn\{k}
|
alpar@1
|
1242 |
*
|
alpar@1
|
1243 |
* where Jp = {j : a[j] > 0}, Jn = {j : a[j] < 0}, l[j] and u[j] are
|
alpar@1
|
1244 |
* lower and upper bounds, resp., of variable x[j].
|
alpar@1
|
1245 |
*
|
alpar@1
|
1246 |
* In order to compute inf t[k] more efficiently, the following formula,
|
alpar@1
|
1247 |
* which is equivalent to (13), is actually used:
|
alpar@1
|
1248 |
*
|
alpar@1
|
1249 |
* ( h - a[k] l[k] = h, if a[k] > 0,
|
alpar@1
|
1250 |
* inf t[k] = < (14)
|
alpar@1
|
1251 |
* ( h - a[k] u[k] = h - a[k], if a[k] < 0,
|
alpar@1
|
1252 |
*
|
alpar@1
|
1253 |
* where:
|
alpar@1
|
1254 |
*
|
alpar@1
|
1255 |
* h = sum a[j] l[j] + sum a[j] u[j] (15)
|
alpar@1
|
1256 |
* j in Jp j in Jn
|
alpar@1
|
1257 |
*
|
alpar@1
|
1258 |
* is the implied lower bound of row (1).
|
alpar@1
|
1259 |
*
|
alpar@1
|
1260 |
* Reduction of positive coefficient (a[k] > 0) does not change value
|
alpar@1
|
1261 |
* of h, since l[k] = 0. In case of reduction of negative coefficient
|
alpar@1
|
1262 |
* (a[k] < 0) from (11) it follows that:
|
alpar@1
|
1263 |
*
|
alpar@1
|
1264 |
* delta a[k] = a'[k] - a[k] = inf t[k] - b (> 0), (16)
|
alpar@1
|
1265 |
*
|
alpar@1
|
1266 |
* so new value of h (accounting that u[k] = 1) can be computed as
|
alpar@1
|
1267 |
* follows:
|
alpar@1
|
1268 |
*
|
alpar@1
|
1269 |
* h := h + delta a[k] = h + (inf t[k] - b). (17)
|
alpar@1
|
1270 |
*
|
alpar@1
|
1271 |
* RECOVERING SOLUTION
|
alpar@1
|
1272 |
*
|
alpar@1
|
1273 |
* None needed. */
|
alpar@1
|
1274 |
|
alpar@1
|
1275 |
static int reduce_ineq_coef(NPP *npp, struct elem *ptr, double *_b)
|
alpar@1
|
1276 |
{ /* process inequality constraint: sum a[j] x[j] >= b */
|
alpar@1
|
1277 |
/* returns: the number of coefficients reduced */
|
alpar@1
|
1278 |
struct elem *e;
|
alpar@1
|
1279 |
int count = 0;
|
alpar@1
|
1280 |
double h, inf_t, new_a, b = *_b;
|
alpar@1
|
1281 |
xassert(npp == npp);
|
alpar@1
|
1282 |
/* compute h; see (15) */
|
alpar@1
|
1283 |
h = 0.0;
|
alpar@1
|
1284 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
1285 |
{ if (e->aj > 0.0)
|
alpar@1
|
1286 |
{ if (e->xj->lb == -DBL_MAX) goto done;
|
alpar@1
|
1287 |
h += e->aj * e->xj->lb;
|
alpar@1
|
1288 |
}
|
alpar@1
|
1289 |
else /* e->aj < 0.0 */
|
alpar@1
|
1290 |
{ if (e->xj->ub == +DBL_MAX) goto done;
|
alpar@1
|
1291 |
h += e->aj * e->xj->ub;
|
alpar@1
|
1292 |
}
|
alpar@1
|
1293 |
}
|
alpar@1
|
1294 |
/* perform reduction of coefficients at binary variables */
|
alpar@1
|
1295 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
1296 |
{ /* skip non-binary variable */
|
alpar@1
|
1297 |
if (!(e->xj->is_int && e->xj->lb == 0.0 && e->xj->ub == 1.0))
|
alpar@1
|
1298 |
continue;
|
alpar@1
|
1299 |
if (e->aj > 0.0)
|
alpar@1
|
1300 |
{ /* compute inf t[k]; see (14) */
|
alpar@1
|
1301 |
inf_t = h;
|
alpar@1
|
1302 |
if (b - e->aj < inf_t && inf_t < b)
|
alpar@1
|
1303 |
{ /* compute reduced coefficient a'[k]; see (7) */
|
alpar@1
|
1304 |
new_a = b - inf_t;
|
alpar@1
|
1305 |
if (new_a >= +1e-3 &&
|
alpar@1
|
1306 |
e->aj - new_a >= 0.01 * (1.0 + e->aj))
|
alpar@1
|
1307 |
{ /* accept a'[k] */
|
alpar@1
|
1308 |
#ifdef GLP_DEBUG
|
alpar@1
|
1309 |
xprintf("+");
|
alpar@1
|
1310 |
#endif
|
alpar@1
|
1311 |
e->aj = new_a;
|
alpar@1
|
1312 |
count++;
|
alpar@1
|
1313 |
}
|
alpar@1
|
1314 |
}
|
alpar@1
|
1315 |
}
|
alpar@1
|
1316 |
else /* e->aj < 0.0 */
|
alpar@1
|
1317 |
{ /* compute inf t[k]; see (14) */
|
alpar@1
|
1318 |
inf_t = h - e->aj;
|
alpar@1
|
1319 |
if (b < inf_t && inf_t < b - e->aj)
|
alpar@1
|
1320 |
{ /* compute reduced coefficient a'[k]; see (11) */
|
alpar@1
|
1321 |
new_a = e->aj + (inf_t - b);
|
alpar@1
|
1322 |
if (new_a <= -1e-3 &&
|
alpar@1
|
1323 |
new_a - e->aj >= 0.01 * (1.0 - e->aj))
|
alpar@1
|
1324 |
{ /* accept a'[k] */
|
alpar@1
|
1325 |
#ifdef GLP_DEBUG
|
alpar@1
|
1326 |
xprintf("-");
|
alpar@1
|
1327 |
#endif
|
alpar@1
|
1328 |
e->aj = new_a;
|
alpar@1
|
1329 |
/* update h; see (17) */
|
alpar@1
|
1330 |
h += (inf_t - b);
|
alpar@1
|
1331 |
/* compute b'; see (9) */
|
alpar@1
|
1332 |
b = inf_t;
|
alpar@1
|
1333 |
count++;
|
alpar@1
|
1334 |
}
|
alpar@1
|
1335 |
}
|
alpar@1
|
1336 |
}
|
alpar@1
|
1337 |
}
|
alpar@1
|
1338 |
*_b = b;
|
alpar@1
|
1339 |
done: return count;
|
alpar@1
|
1340 |
}
|
alpar@1
|
1341 |
|
alpar@1
|
1342 |
int npp_reduce_ineq_coef(NPP *npp, NPPROW *row)
|
alpar@1
|
1343 |
{ /* reduce inequality constraint coefficients */
|
alpar@1
|
1344 |
NPPROW *copy;
|
alpar@1
|
1345 |
NPPAIJ *aij;
|
alpar@1
|
1346 |
struct elem *ptr, *e;
|
alpar@1
|
1347 |
int kase, count[2];
|
alpar@1
|
1348 |
double b;
|
alpar@1
|
1349 |
/* the row must be inequality constraint */
|
alpar@1
|
1350 |
xassert(row->lb < row->ub);
|
alpar@1
|
1351 |
count[0] = count[1] = 0;
|
alpar@1
|
1352 |
for (kase = 0; kase <= 1; kase++)
|
alpar@1
|
1353 |
{ if (kase == 0)
|
alpar@1
|
1354 |
{ /* process row lower bound */
|
alpar@1
|
1355 |
if (row->lb == -DBL_MAX) continue;
|
alpar@1
|
1356 |
#ifdef GLP_DEBUG
|
alpar@1
|
1357 |
xprintf("L");
|
alpar@1
|
1358 |
#endif
|
alpar@1
|
1359 |
ptr = copy_form(npp, row, +1.0);
|
alpar@1
|
1360 |
b = + row->lb;
|
alpar@1
|
1361 |
}
|
alpar@1
|
1362 |
else
|
alpar@1
|
1363 |
{ /* process row upper bound */
|
alpar@1
|
1364 |
if (row->ub == +DBL_MAX) continue;
|
alpar@1
|
1365 |
#ifdef GLP_DEBUG
|
alpar@1
|
1366 |
xprintf("U");
|
alpar@1
|
1367 |
#endif
|
alpar@1
|
1368 |
ptr = copy_form(npp, row, -1.0);
|
alpar@1
|
1369 |
b = - row->ub;
|
alpar@1
|
1370 |
}
|
alpar@1
|
1371 |
/* now the inequality has the form "sum a[j] x[j] >= b" */
|
alpar@1
|
1372 |
count[kase] = reduce_ineq_coef(npp, ptr, &b);
|
alpar@1
|
1373 |
if (count[kase] > 0)
|
alpar@1
|
1374 |
{ /* the original inequality has been replaced by equivalent
|
alpar@1
|
1375 |
one with coefficients reduced */
|
alpar@1
|
1376 |
if (row->lb == -DBL_MAX || row->ub == +DBL_MAX)
|
alpar@1
|
1377 |
{ /* the original row is single-sided inequality; no copy
|
alpar@1
|
1378 |
is needed */
|
alpar@1
|
1379 |
copy = NULL;
|
alpar@1
|
1380 |
}
|
alpar@1
|
1381 |
else
|
alpar@1
|
1382 |
{ /* the original row is double-sided inequality; we need
|
alpar@1
|
1383 |
to create its copy for other bound before replacing it
|
alpar@1
|
1384 |
with the equivalent inequality */
|
alpar@1
|
1385 |
#ifdef GLP_DEBUG
|
alpar@1
|
1386 |
xprintf("*");
|
alpar@1
|
1387 |
#endif
|
alpar@1
|
1388 |
copy = npp_add_row(npp);
|
alpar@1
|
1389 |
if (kase == 0)
|
alpar@1
|
1390 |
{ /* the copy is for upper bound */
|
alpar@1
|
1391 |
copy->lb = -DBL_MAX, copy->ub = row->ub;
|
alpar@1
|
1392 |
}
|
alpar@1
|
1393 |
else
|
alpar@1
|
1394 |
{ /* the copy is for lower bound */
|
alpar@1
|
1395 |
copy->lb = row->lb, copy->ub = +DBL_MAX;
|
alpar@1
|
1396 |
}
|
alpar@1
|
1397 |
/* copy original row coefficients */
|
alpar@1
|
1398 |
for (aij = row->ptr; aij != NULL; aij = aij->r_next)
|
alpar@1
|
1399 |
npp_add_aij(npp, copy, aij->col, aij->val);
|
alpar@1
|
1400 |
}
|
alpar@1
|
1401 |
/* replace the original inequality by equivalent one */
|
alpar@1
|
1402 |
npp_erase_row(npp, row);
|
alpar@1
|
1403 |
row->lb = b, row->ub = +DBL_MAX;
|
alpar@1
|
1404 |
for (e = ptr; e != NULL; e = e->next)
|
alpar@1
|
1405 |
npp_add_aij(npp, row, e->xj, e->aj);
|
alpar@1
|
1406 |
/* continue processing upper bound for the copy */
|
alpar@1
|
1407 |
if (copy != NULL) row = copy;
|
alpar@1
|
1408 |
}
|
alpar@1
|
1409 |
drop_form(npp, ptr);
|
alpar@1
|
1410 |
}
|
alpar@1
|
1411 |
return count[0] + count[1];
|
alpar@1
|
1412 |
}
|
alpar@1
|
1413 |
|
alpar@1
|
1414 |
/* eof */
|