GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
heur_gcgzirounding.c
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the program and library */
4/* GCG --- Generic Column Generation */
5/* a Dantzig-Wolfe decomposition based extension */
6/* of the branch-cut-and-price framework */
7/* SCIP --- Solving Constraint Integer Programs */
8/* */
9/* Copyright (C) 2010-2026 Operations Research, RWTH Aachen University */
10/* Zuse Institute Berlin (ZIB) */
11/* */
12/* Licensed under the Apache License, Version 2.0 (the "License"); */
13/* you may not use this file except in compliance with the License. */
14/* You may obtain a copy of the License at */
15/* */
16/* http://www.apache.org/licenses/LICENSE-2.0 */
17/* */
18/* Unless required by applicable law or agreed to in writing, software */
19/* distributed under the License is distributed on an "AS IS" BASIS, */
20/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
21/* See the License for the specific language governing permissions and */
22/* limitations under the License. */
23/* */
24/* You should have received a copy of the Apache-2.0 license */
25/* along with GCG; see the file LICENSE. If not visit gcg.or.rwth-aachen.de.*/
26/* */
27/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
28
29/**@file heur_gcgzirounding.c
30 * @brief zirounding primal heuristic
31 * @author Gregor Hendel
32 * @author Christian Puchert
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
37#include <string.h>
38
40#include "gcg/gcg.h"
41#include "gcg/relax_gcg.h"
42
43#define HEUR_NAME "gcgzirounding"
44#define HEUR_DESC "rounding heuristic on original variables as suggested by C. Wallace taking row slacks and bounds into account"
45#define HEUR_DISPCHAR 'z'
46#define HEUR_PRIORITY -500
47/* #define HEUR_FREQ 1 */
48#define HEUR_FREQ -1 /* / @todo heuristic deactivated due to false solutions */
49#define HEUR_FREQOFS 0
50#define HEUR_MAXDEPTH -1
51#define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
52#define HEUR_USESSUBSCIP FALSE
53
54#define DEFAULT_MAXROUNDINGLOOPS 2 /**< delimits the number of main loops, can be set to -1 for no limit */
55#define DEFAULT_STOPZIROUND TRUE /**< deactivation check is enabled by default */
56#define DEFAULT_STOPPERCENTAGE 0.02 /**< the tolerance percentage after which zirounding will not be executed anymore */
57#define DEFAULT_MINSTOPNCALLS 1000 /**< number of heuristic calls before deactivation check */
58
59#define MINSHIFT 1e-4 /**< minimum shift value for every single step */
60
61/*
62 * Data structures
63 */
64
65/** primal heuristic data */
66struct SCIP_HeurData
67{
68 GCG* gcg; /**< GCG data structure */
69 SCIP_SOL* sol; /**< working solution */
70 SCIP_Longint lastlp; /**< the number of the last LP for which ZIRounding was called */
71 int maxroundingloops; /**< limits rounding loops in execution */
72 SCIP_Bool stopziround; /**< sets deactivation check */
73 SCIP_Real stoppercentage; /**< threshold for deactivation check */
74 int minstopncalls; /**< number of heuristic calls before deactivation check */
75};
76
82typedef enum Direction DIRECTION;
83
84/*
85 * Local methods
86 */
87
88/** returns the fractionality of a value x, which is calculated as zivalue(x) = min(x-floor(x), ceil(x)-x) */
89static
91 SCIP* scip, /**< pointer to current SCIP data structure */
92 SCIP_Real val /**< the value for which the fractionality should be computed */
93 )
94{
95 SCIP_Real upgap; /* the gap between val and ceil(val) */
96 SCIP_Real downgap; /* the gap between val and floor(val) */
97
98 assert(scip != NULL);
99
100 upgap = SCIPfeasCeil(scip, val) - val;
101 downgap = val - SCIPfeasFloor(scip, val);
102
103 return MIN(upgap, downgap);
104}
105
106/** determines shifting bounds for variable */
107static
109 SCIP* scip, /**< pointer to current SCIP data structure */
110 SCIP_VAR* var, /**< the variable for which lb and ub have to be calculated */
111 SCIP_Real currentvalue, /**< the current value of var in the working solution */
112 SCIP_Real* upperbound, /**< pointer to store the calculated upper bound on the variable shift */
113 SCIP_Real* lowerbound, /**< pointer to store the calculated lower bound on the variable shift */
114 SCIP_Real* upslacks, /**< array that contains the slacks between row activities and the right hand sides of the rows */
115 SCIP_Real* downslacks, /**< array that contains lhs slacks */
116 int nslacks, /**< current number of slacks */
117 SCIP_Bool* numericalerror /**< flag to determine whether a numerical error occurred */
118 )
119{
120 SCIP_COL* col;
121 SCIP_ROW** colrows;
122 SCIP_Real* colvals;
123 int ncolvals;
124 int i;
125
126 assert(scip != NULL);
127 assert(var != NULL);
128 assert(upslacks != NULL);
129 assert(downslacks != NULL);
130 assert(upperbound != NULL);
131 assert(lowerbound != NULL);
132
133 /* get the column associated to the variable, the nonzero rows and the nonzero coefficients */
134 col = SCIPvarGetCol(var);
135 colrows = SCIPcolGetRows(col);
136 colvals = SCIPcolGetVals(col);
137 ncolvals = SCIPcolGetNLPNonz(col);
138
139 /* only proceed, when variable has nonzero coefficients */
140 if( ncolvals == 0 )
141 return;
142
143 assert(colvals != NULL);
144 assert(colrows != NULL);
145
146 /* initialize the bounds on the shift to be the gap of the current solution value to the bounds of the variable */
148 *upperbound = SCIPinfinity(scip);
149 else
150 *upperbound = SCIPvarGetUbGlobal(var) - currentvalue;
151
153 *lowerbound = SCIPinfinity(scip);
154 else
155 *lowerbound = currentvalue - SCIPvarGetLbGlobal(var);
156
157 /* go through every nonzero row coefficient corresponding to var to determine bounds for shifting
158 * in such a way that shifting maintains feasibility in every LP row.
159 * a lower or upper bound as it is calculated in zirounding always has to be >= 0.0.
160 * if one of these values is significantly < 0.0, this will cause the abort of execution of the heuristic so that
161 * infeasible solutions are avoided
162 */
163 for( i = 0; i < ncolvals && MAX(*lowerbound, *upperbound) >= MINSHIFT; ++i )
164 {
165 SCIP_ROW* row;
166 int rowpos;
167
168 row = colrows[i];
169 rowpos = SCIProwGetLPPos(row);
170
171 /* the row might currently not be in the LP, ignore it! */
172 if( rowpos == -1 )
173 continue;
174
175 assert(0 <= rowpos && rowpos < nslacks);
176
177 /* all bounds and slacks as they are calculated in zirounding always have to be greater equal zero.
178 * It might however be due to numerical issues, e.g. with scaling, that they are not. Better abort in this case.
179 */
180 if( SCIPisFeasLT(scip, *lowerbound, 0.0) || SCIPisFeasLT(scip, *upperbound, 0.0)
181 || SCIPisFeasLT(scip, upslacks[rowpos], 0.0) || SCIPisFeasLT(scip, downslacks[rowpos] , 0.0) )
182 {
183 *numericalerror = TRUE;
184 return;
185 }
186
187 SCIPdebugMsg(scip, "colval: %15.8g, downslack: %15.8g, upslack: %5.2g, lb: %5.2g, ub: %5.2g\n", colvals[i], downslacks[rowpos], upslacks[rowpos],
188 *lowerbound, *upperbound);
189
190 /* if coefficient > 0, rounding up might violate up slack and rounding down might violate down slack
191 * thus search for the minimum so that no constraint is violated; vice versa for coefficient < 0
192 */
193 if( colvals[i] > 0 )
194 {
195 if( !SCIPisInfinity(scip, upslacks[rowpos]) )
196 {
197 SCIP_Real upslack;
198 upslack = MAX(upslacks[rowpos], 0.0); /* avoid errors due to numerically slightly infeasible rows */
199 *upperbound = MIN(*upperbound, upslack/colvals[i]);
200 }
201
202 if( !SCIPisInfinity(scip, downslacks[rowpos]) )
203 {
204 SCIP_Real downslack;
205 downslack = MAX(downslacks[rowpos], 0.0); /* avoid errors due to numerically slightly infeasible rows */
206 *lowerbound = MIN(*lowerbound, downslack/colvals[i]);
207 }
208 }
209 else
210 {
211 assert(colvals[i] != 0.0);
212
213 if( !SCIPisInfinity(scip, upslacks[rowpos]) )
214 {
215 SCIP_Real upslack;
216 upslack = MAX(upslacks[rowpos], 0.0); /* avoid errors due to numerically slightly infeasible rows */
217 *lowerbound = MIN(*lowerbound, -upslack/colvals[i]);
218 }
219
220 if( !SCIPisInfinity(scip, downslacks[rowpos]) )
221 {
222 SCIP_Real downslack;
223 downslack = MAX(downslacks[rowpos], 0.0); /* avoid errors due to numerically slightly infeasible rows */
224 *upperbound = MIN(*upperbound, -downslack/colvals[i]);
225 }
226 }
227 }
228}
229
230/** when a variable is shifted, the activities and slacks of all rows it appears in have to be updated */
231static
233 SCIP* scip, /**< pointer to current SCIP data structure */
234 SCIP_SOL* sol, /**< working solution */
235 SCIP_VAR* var, /**< pointer to variable to be modified */
236 SCIP_Real shiftvalue, /**< the value by which the variable is shifted */
237 SCIP_Real* upslacks, /**< upslacks of all rows the variable appears in */
238 SCIP_Real* downslacks, /**< downslacks of all rows the variable appears in */
239 SCIP_Real* activities, /**< activities of the LP rows */
240 SCIP_VAR** slackvars, /**< the slack variables for equality rows */
241 SCIP_Real* slackcoeffs, /**< the slack variable coefficients */
242 int nslacks /**< size of the arrays */
243 )
244{
245 SCIP_COL* col; /* the corresponding column of variable var */
246 SCIP_ROW** rows; /* pointer to the nonzero coefficient rows for variable var */
247 int nrows; /* the number of nonzeros */
248 SCIP_Real* colvals; /* array to store the nonzero coefficients */
249 int i;
250
251 assert(scip != NULL);
252 assert(sol != NULL);
253 assert(var != NULL);
254 assert(upslacks != NULL);
255 assert(downslacks != NULL);
257 assert(nslacks >= 0);
258
259 col = SCIPvarGetCol(var);
260 assert(col != NULL);
261
262 rows = SCIPcolGetRows(col);
263 nrows = SCIPcolGetNLPNonz(col);
264 colvals = SCIPcolGetVals(col);
265 assert(nrows == 0 || (rows != NULL && colvals != NULL));
266
267 /* go through all rows the shifted variable appears in */
268 for( i = 0; i < nrows; ++i )
269 {
270 int rowpos;
271
272 rowpos = SCIProwGetLPPos(rows[i]);
273 assert(-1 <= rowpos && rowpos < nslacks);
274
275 /* if the row is in the LP, update its activity, up and down slack */
276 if( rowpos >= 0 )
277 {
278 SCIP_Real val;
279
280 val = colvals[i] * shiftvalue;
281
282 /* if the row is an equation, we update its slack variable instead of its activities */
283 if( SCIPisFeasEQ(scip, SCIProwGetLhs(rows[i]), SCIProwGetRhs(rows[i])) )
284 {
285 SCIP_Real slackvarshiftval;
286 SCIP_Real slackvarsolval;
287
288 assert(slackvars[rowpos] != NULL);
289 assert(!SCIPisZero(scip, slackcoeffs[rowpos]));
290
291 slackvarsolval = SCIPgetSolVal(scip, sol, slackvars[rowpos]);
292 slackvarshiftval = -val / slackcoeffs[rowpos];
293
294 assert(SCIPisFeasGE(scip, slackvarsolval + slackvarshiftval, SCIPvarGetLbGlobal(slackvars[rowpos])));
295 assert(SCIPisFeasLE(scip, slackvarsolval + slackvarshiftval, SCIPvarGetUbGlobal(slackvars[rowpos])));
296
297 SCIP_CALL( SCIPsetSolVal(scip, sol, slackvars[rowpos], slackvarsolval + slackvarshiftval) );
298 }
299 else if( !SCIPisInfinity(scip, -activities[rowpos]) && !SCIPisInfinity(scip, activities[rowpos]) )
300 activities[rowpos] += val;
301
302 /* the slacks of the row now can be updated independently of its type */
303 if( !SCIPisInfinity(scip, upslacks[rowpos]) )
304 upslacks[rowpos] -= val;
305 if( !SCIPisInfinity(scip, -downslacks[rowpos]) )
306 downslacks[rowpos] += val;
307
308 assert(!SCIPisFeasNegative(scip, upslacks[rowpos]));
309 assert(!SCIPisFeasNegative(scip, downslacks[rowpos]));
310 }
311 }
312 return SCIP_OKAY;
313}
314
315/** finds a continuous slack variable for an equation row, NULL if none exists */
316static
318 SCIP* scip, /**< pointer to current SCIP data structure */
319 SCIP_ROW* row, /**< the row for which a slack variable is searched */
320 SCIP_VAR** varpointer, /**< pointer to store the slack variable */
321 SCIP_Real* coeffpointer /**< pointer to store the coefficient of the slack variable */
322 )
323{
324 int v;
325 SCIP_COL** rowcols;
326 SCIP_Real* rowvals;
327 int nrowvals;
328
329 assert(row != NULL);
330 assert(varpointer != NULL);
331 assert(coeffpointer != NULL);
332
333 rowcols = SCIProwGetCols(row);
334 rowvals = SCIProwGetVals(row);
335 nrowvals = SCIProwGetNNonz(row);
336
337 assert(nrowvals == 0 || rowvals != NULL);
338 assert(nrowvals == 0 || rowcols != NULL);
339
340 /* iterate over the row variables. Stop after the first unfixed continuous variable was found. */
341 for( v = nrowvals - 1; v >= 0; --v )
342 {
343 SCIP_VAR* colvar;
344
345 assert(rowcols[v] != NULL);
346 if( SCIPcolGetLPPos(rowcols[v]) == -1 )
347 continue;
348
349 colvar = SCIPcolGetVar(rowcols[v]);
350
353 && SCIPcolGetNLPNonz(rowcols[v]) == 1 )
354 {
355 SCIPdebugMsg(scip, " slack variable for row %s found: %s\n", SCIProwGetName(row), SCIPvarGetName(colvar));
356
357 *coeffpointer = rowvals[v];
358 *varpointer = colvar;
359
360 return;
361 }
362 }
363
364 *varpointer = NULL;
365 *coeffpointer = 0.0;
366
367 SCIPdebugMsg(scip, "No slack variable for row %s found. \n", SCIProwGetName(row));
368}
369
370/*
371 * Callback methods of primal heuristic
372 */
373
374/** destructor of primal heuristic to free user data (called when SCIP is exiting) */
375static
376SCIP_DECL_HEURFREE(heurFreeGcgzirounding)
377{ /*lint --e{715}*/
379
380 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
381
383 assert(heurdata != NULL);
384
385 /* free heuristic data */
387 SCIPheurSetData(heur, NULL);
388
389 return SCIP_OKAY;
390}
391
392/** initialization method of primal heuristic (called after problem was transformed) */
393static
394SCIP_DECL_HEURINIT(heurInitGcgzirounding)
395{ /*lint --e{715}*/
397
398 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
399
401 assert(heurdata != NULL);
402
403 /* create working solution */
405
406 return SCIP_OKAY;
407}
408
409/** deinitialization method of primal heuristic (called before transformed problem is freed) */
410static
411SCIP_DECL_HEUREXIT(heurExitGcgzirounding) /*lint --e{715}*/
412{ /*lint --e{715}*/
414
415 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
416
419
420 /* free working solution */
422
423 return SCIP_OKAY;
424}
425
426/** solving process initialization method of primal heuristic (called when branch and bound process is about to begin) */
427static
428SCIP_DECL_HEURINITSOL(heurInitsolGcgzirounding)
429{ /*lint --e{715}*/
431
432 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
433
435 assert(heurdata != NULL);
436
437 heurdata->lastlp = -1;
438
439 return SCIP_OKAY;
440}
441
442/** execution method of primal heuristic */
443static
444SCIP_DECL_HEUREXEC(heurExecGcgzirounding)
445{ /*lint --e{715}*/
448 SCIP_SOL* sol;
450 SCIP_VAR** zilpcands;
451 SCIP_VAR** slackvars;
452 SCIP_ROW** rows;
453 SCIP_Real* upslacks;
454 SCIP_Real* downslacks;
456 SCIP_Real* slackvarcoeffs;
457 SCIP_Bool* rowneedsslackvar;
458
460 int currentlpcands;
461 int nlpcands;
462 int i;
463 int c;
464 int nslacks;
465 int nroundings;
466
467 SCIP_Bool improvementfound;
468 SCIP_Bool numericalerror;
469
470 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
471 assert(result != NULL);
472
473 /* get heuristic data */
475 assert(heurdata != NULL);
476
477 /* get master problem */
480
482
483 /* do not call heuristic of node was already detected to be infeasible */
484 if( nodeinfeasible )
485 return SCIP_OKAY;
486
487 /* do not execute the heuristic on invalid relaxation solutions
488 * (which is the case if the node has been cut off)
489 */
491 {
492 SCIPdebugMsg(scip, "skipping GCG ZI rounding: invalid relaxation solution\n");
493 return SCIP_OKAY;
494 }
495
496 /* only call heuristic if an optimal LP-solution is at hand */
498 return SCIP_OKAY;
499
500 /* Do not call heuristic if deactivation check is enabled and percentage of found solutions in relation
501 * to number of calls falls below heurdata->stoppercentage */
504 return SCIP_OKAY;
505
506 /* assure that heuristic has not already been called after the last LP had been solved */
508 if( nlps == heurdata->lastlp )
509 return SCIP_OKAY;
510
512
513 /* get fractional variables */
515
516 /* make sure that there is at least one fractional variable that should be integral */
517 if( nlpcands == 0 )
518 return SCIP_OKAY;
519
520 assert(nlpcands > 0);
521 assert(lpcands != NULL);
522
523 /* get LP rows data */
524 rows = SCIPgetLPRows(scip);
525 nslacks = SCIPgetNLPRows(scip);
526
527 /* cannot do anything if LP is empty */
528 if( nslacks == 0 )
529 return SCIP_OKAY;
530
531 assert(rows != NULL);
532 assert(nslacks > 0);
533
534 /* get the working solution from heuristic's local data */
535 sol = heurdata->sol;
536 assert(sol != NULL);
537
539
540 zilpcands = NULL;
541
542 /* copy the current LP solution to the working solution and allocate memory for local data */
545
546 /* copy necessary data to local arrays */
548
549 /* allocate buffer data arrays */
550 SCIP_CALL( SCIPallocBufferArray(scip, &slackvars, nslacks) );
551 SCIP_CALL( SCIPallocBufferArray(scip, &upslacks, nslacks) );
552 SCIP_CALL( SCIPallocBufferArray(scip, &downslacks, nslacks) );
553 SCIP_CALL( SCIPallocBufferArray(scip, &slackvarcoeffs, nslacks) );
554 SCIP_CALL( SCIPallocBufferArray(scip, &rowneedsslackvar, nslacks) );
556
557 BMSclearMemoryArray(slackvars, nslacks);
558 BMSclearMemoryArray(slackvarcoeffs, nslacks);
559 BMSclearMemoryArray(rowneedsslackvar, nslacks);
560
561 numericalerror = FALSE;
562 nroundings = 0;
563
564 /* loop over fractional variables and involved LP rows to find all rows which require a slack variable */
565 for( c = 0; c < nlpcands; ++c )
566 {
567 SCIP_VAR* cand;
568 SCIP_ROW** candrows;
569 int r;
570 int ncandrows;
571
572 cand = zilpcands[c];
573 assert(cand != NULL);
575
576 candrows = SCIPcolGetRows(SCIPvarGetCol(cand));
577 ncandrows = SCIPcolGetNLPNonz(SCIPvarGetCol(cand));
578
579 assert(candrows == NULL || ncandrows > 0);
580
581 for( r = 0; r < ncandrows; ++r )
582 {
583 int rowpos;
584
585 assert(candrows != NULL); /* to please flexelint */
586 assert(candrows[r] != NULL);
587 rowpos = SCIProwGetLPPos(candrows[r]);
588
589 if( rowpos >= 0 && SCIPisFeasEQ(scip, SCIProwGetLhs(candrows[r]), SCIProwGetRhs(candrows[r])) )
590 {
591 rowneedsslackvar[rowpos] = TRUE;
592 SCIPdebugMsg(scip, " Row %s needs slack variable for variable %s\n", SCIProwGetName(candrows[r]), SCIPvarGetName(cand));
593 }
594 }
595 }
596
597 /* calculate row slacks for every every row that belongs to the current LP and ensure, that the current solution
598 * has no violated constraint -- if any constraint is violated, i.e. a slack is significantly smaller than zero,
599 * this will cause the termination of the heuristic because Zirounding does not provide feasibility recovering
600 */
601 for( i = 0; i < nslacks; ++i )
602 {
603 SCIP_ROW* row;
604 SCIP_Real lhs;
605 SCIP_Real rhs;
606
607 row = rows[i];
608
609 assert(row != NULL);
610
611 lhs = SCIProwGetLhs(row);
612 rhs = SCIProwGetRhs(row);
613
614 /* get row activity */
616 /** @todo This assertion has been commented out due to numerical troubles */
617/* assert( SCIPisFeasLE(scip, lhs, activities[i]) && SCIPisFeasLE(scip, activities[i], rhs) ); */
618
619 /* in special case if LHS or RHS is (-)infinity slacks have to be initialized as infinity */
620 if( SCIPisInfinity(scip, -lhs) )
621 downslacks[i] = SCIPinfinity(scip);
622 else
623 downslacks[i] = activities[i] - lhs;
624
625 if( SCIPisInfinity(scip, rhs) )
626 upslacks[i] = SCIPinfinity(scip);
627 else
628 upslacks[i] = rhs - activities[i];
629
630 SCIPdebugMsg(scip, "lhs:%5.2f <= act:%5.2g <= rhs:%5.2g --> down: %5.2g, up:%5.2g\n", lhs, activities[i], rhs, downslacks[i], upslacks[i]);
631
632 /* row is an equation. Try to find a slack variable in the row, i.e.,
633 * a continuous variable which occurs only in this row. If no such variable exists,
634 * there is no hope for an IP-feasible solution in this round
635 */
636 if( SCIPisFeasEQ(scip, lhs, rhs) && rowneedsslackvar[i] )
637 {
638 /* @todo: This is only necessary for rows containing fractional variables. */
639 rowFindSlackVar(scip, row, &(slackvars[i]), &(slackvarcoeffs[i]));
640
641 if( slackvars[i] == NULL )
642 {
643 SCIPdebugMsg(scip, "No slack variable found for equation %s, terminating ZI Round heuristic\n", SCIProwGetName(row));
644 goto TERMINATE;
645 }
646 else
647 {
648 SCIP_Real ubslackvar;
649 SCIP_Real lbslackvar;
650 SCIP_Real solvalslackvar;
651 SCIP_Real coeffslackvar;
652 SCIP_Real ubgap;
653 SCIP_Real lbgap;
654
656 solvalslackvar = SCIPgetSolVal(scip, sol, slackvars[i]);
657 ubslackvar = SCIPvarGetUbGlobal(slackvars[i]);
658 lbslackvar = SCIPvarGetLbGlobal(slackvars[i]);
659
660 coeffslackvar = slackvarcoeffs[i];
661 assert(!SCIPisFeasZero(scip, coeffslackvar));
662
663 ubgap = MAX(0.0, ubslackvar - solvalslackvar);
664 lbgap = MAX(0.0, solvalslackvar - lbslackvar);
665
666 if( SCIPisPositive(scip, coeffslackvar) )
667 {
668 if( !SCIPisInfinity(scip, lbslackvar) )
669 upslacks[i] += coeffslackvar * lbgap;
670 else
671 upslacks[i] = SCIPinfinity(scip);
672 if( !SCIPisInfinity(scip, ubslackvar) )
673 downslacks[i] += coeffslackvar * ubgap;
674 else
675 downslacks[i] = SCIPinfinity(scip);
676 }
677 else
678 {
679 if( !SCIPisInfinity(scip, ubslackvar) )
680 upslacks[i] -= coeffslackvar * ubgap;
681 else
682 upslacks[i] = SCIPinfinity(scip);
683 if( !SCIPisInfinity(scip, lbslackvar) )
684 downslacks[i] -= coeffslackvar * lbgap;
685 else
686 downslacks[i] = SCIPinfinity(scip);
687 }
688 SCIPdebugMsg(scip, " Slack variable for row %s at pos %d: %g <= %s = %g <= %g; Coeff %g, upslack = %g, downslack = %g \n",
689 SCIProwGetName(row), SCIProwGetLPPos(row), lbslackvar, SCIPvarGetName(slackvars[i]), solvalslackvar, ubslackvar, coeffslackvar,
690 upslacks[i], downslacks[i]);
691 }
692 }
693 /* due to numerical inaccuracies, the rows might be feasible, even if the slacks are
694 * significantly smaller than zero -> terminate
695 */
696 if( SCIPisFeasLT(scip, upslacks[i], 0.0) || SCIPisFeasLT(scip, downslacks[i], 0.0) )
697 goto TERMINATE;
698 }
699
700 assert(nslacks == 0 || (upslacks != NULL && downslacks != NULL && activities != NULL));
701
702 /* initialize number of remaining variables and flag to enter the main loop */
703 currentlpcands = nlpcands;
704 improvementfound = TRUE;
705
706 /* iterate over variables as long as there are fractional variables left */
707 while( currentlpcands > 0 && improvementfound && (heurdata->maxroundingloops == -1 || nroundings < heurdata->maxroundingloops) )
708 { /*lint --e{850}*/
709 improvementfound = FALSE;
710 nroundings++;
711 SCIPdebugMsg(scip, "GCG zirounding enters while loop for %d time with %d candidates left. \n", nroundings, currentlpcands);
712
713 /* check for every remaining fractional variable if a shifting decreases ZI-value of the variable */
714 for( c = 0; c < currentlpcands; ++c )
715 {
716 SCIP_VAR* var;
718 SCIP_Real upperbound;
719 SCIP_Real lowerbound;
720 SCIP_Real up;
721 SCIP_Real down;
722 SCIP_Real ziup;
723 SCIP_Real zidown;
724 SCIP_Real zicurrent;
725 SCIP_Real shiftval;
726 SCIP_Real newsolval;
727
728 DIRECTION direction;
729
730 var = zilpcands[c];
732 newsolval = oldsolval;
733
734 /* It may be that external branching candidates are stored double,
735 * so the variable may have already been considered;
736 * in that case, ignore it
737 */
739 {
740 zilpcands[c] = zilpcands[currentlpcands - 1];
741 currentlpcands--;
742
743 /* counter is decreased if end of candidates array has not been reached yet */
744 if( c < currentlpcands )
745 c--;
746
747 continue;
748 }
749
751
752 /* calculate bounds for variable and make sure that there are no numerical inconsistencies */
753 upperbound = SCIPinfinity(scip);
754 lowerbound = SCIPinfinity(scip);
755 calculateBounds(scip, var, oldsolval, &upperbound, &lowerbound, upslacks, downslacks, nslacks, &numericalerror);
756
757 if( numericalerror )
758 goto TERMINATE;
759
760 /* continue if only marginal shifts are possible */
761 if( MAX(upperbound, lowerbound) < MINSHIFT )
762 {
763 /* stop immediately if a variable has not been rounded during the last round */
764 if( nroundings == heurdata->maxroundingloops )
765 break;
766 else
767 continue;
768 }
769
770 /* calculate the possible values after shifting */
771 up = oldsolval + upperbound;
772 down = oldsolval - lowerbound;
773
774 /* if the variable is integer or implicit binary, do not shift further than the nearest integer */
776 {
777 SCIP_Real ceilx;
778 SCIP_Real floorx;
779
780 ceilx = SCIPfeasCeil(scip, oldsolval);
781 floorx = SCIPfeasFloor(scip, oldsolval);
782 up = MIN(up, ceilx);
783 down = MAX(down, floorx);
784 }
785
786 /* calculate necessary values */
787 ziup = getZiValue(scip, up);
788 zidown = getZiValue(scip, down);
789 zicurrent = getZiValue(scip, oldsolval);
790
791 /* calculate the shifting direction that reduces ZI-value the most,
792 * if both directions improve ZI-value equally, take the direction which improves the objective
793 */
794 if( SCIPisFeasLT(scip, zidown, zicurrent) || SCIPisFeasLT(scip, ziup, zicurrent) )
795 {
796 if( SCIPisFeasEQ(scip,ziup, zidown) )
798 else if( SCIPisFeasLT(scip, zidown, ziup) )
799 direction = DIRECTION_DOWN;
800 else
801 direction = DIRECTION_UP;
802
803 /* once a possible shifting direction and value have been found, variable value is updated */
804 shiftval = (direction == DIRECTION_UP ? up - oldsolval : down - oldsolval);
805
806 /* this improves numerical stability in some cases */
807 if( direction == DIRECTION_UP )
808 shiftval = MIN(shiftval, upperbound);
809 else
810 shiftval = MIN(shiftval, lowerbound);
811
812 /* update the solution */
813 newsolval = direction == DIRECTION_UP ? up : down;
814 SCIP_CALL( SCIPsetSolVal(scip, sol, var, newsolval) );
815
816 /* update the rows activities and slacks */
817 SCIP_CALL( updateSlacks(scip, sol, var, shiftval, upslacks,
818 downslacks, activities, slackvars, slackvarcoeffs, nslacks) );
819
820 SCIPdebugMsg(scip, "GCG zirounding update step : %d var index, oldsolval=%g, shiftval=%g\n",
821 SCIPvarGetIndex(var), oldsolval, shiftval);
822 /* since at least one improvement has been found, heuristic will enter main loop for another time because the improvement
823 * might affect many LP rows and their current slacks and thus make further rounding steps possible */
824 improvementfound = TRUE;
825 }
826
827 /* if solution value of variable has become feasibly integral due to rounding step,
828 * variable is put at the end of remaining candidates array so as not to be considered in future loops
829 */
830 if( SCIPisFeasIntegral(scip, newsolval) )
831 {
832 zilpcands[c] = zilpcands[currentlpcands - 1];
833 currentlpcands--;
834
835 /* counter is decreased if end of candidates array has not been reached yet */
836 if( c < currentlpcands )
837 c--;
838 }
839 else if( nroundings == heurdata->maxroundingloops )
840 goto TERMINATE;
841 }
842 }
843
844 /* in case that no candidate is left for rounding after the final main loop
845 * the found solution has to be checked for feasibility in the original problem
846 */
847 if( currentlpcands == 0 )
848 {
849 SCIP_Bool stored;
851 if( stored )
852 {
853#ifdef SCIP_DEBUG
854 SCIPdebugMsg(scip, "found feasible rounded solution:\n");
856#endif
857 SCIPstatisticMessage(" GCG ZI Round solution value: %g \n", SCIPgetSolOrigObj(scip, sol));
859 }
860 }
861
862 /* free memory for all locally allocated data */
863 TERMINATE:
865 SCIPfreeBufferArrayNull(scip, &rowneedsslackvar);
866 SCIPfreeBufferArrayNull(scip, &slackvarcoeffs);
867 SCIPfreeBufferArrayNull(scip, &downslacks);
868 SCIPfreeBufferArrayNull(scip, &upslacks);
869 SCIPfreeBufferArrayNull(scip, &slackvars);
870 SCIPfreeBufferArrayNull(scip, &zilpcands);
871
872 return SCIP_OKAY;
873}
874
875/*
876 * primal heuristic specific interface methods
877 */
878
879/** creates the GCG zirounding primal heuristic and includes it in SCIP */
881 GCG* gcg /**< GCG data structure */
882 )
883{
885 SCIP_HEUR* heur;
887
888 /* create GCG zirounding primal heuristic data */
890 heurdata->gcg = gcg;
891
892 /* include primal heuristic */
895 HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecGcgzirounding, heurdata) );
896
897 assert(heur != NULL);
898
899 /* set non-NULL pointers to callback methods */
900 SCIP_CALL( SCIPsetHeurFree(origprob, heur, heurFreeGcgzirounding) );
901 SCIP_CALL( SCIPsetHeurInit(origprob, heur, heurInitGcgzirounding) );
902 SCIP_CALL( SCIPsetHeurExit(origprob, heur, heurExitGcgzirounding) );
903 SCIP_CALL( SCIPsetHeurInitsol(origprob, heur, heurInitsolGcgzirounding) );
904
905 /* add GCG zirounding primal heuristic parameters */
906 SCIP_CALL( SCIPaddIntParam(origprob, "heuristics/gcgzirounding/maxroundingloops",
907 "determines maximum number of rounding loops",
909 SCIP_CALL( SCIPaddBoolParam(origprob, "heuristics/gcgzirounding/stopziround",
910 "flag to determine if Zirounding is deactivated after a certain percentage of unsuccessful calls",
912 SCIP_CALL( SCIPaddRealParam(origprob,"heuristics/gcgzirounding/stoppercentage",
913 "if percentage of found solutions falls below this parameter, Zirounding will be deactivated",
915 SCIP_CALL( SCIPaddIntParam(origprob, "heuristics/gcgzirounding/minstopncalls",
916 "determines the minimum number of calls before percentage-based deactivation of"
917 " Zirounding is applied", &heurdata->minstopncalls, TRUE, DEFAULT_MINSTOPNCALLS, 1, INT_MAX, NULL, NULL) );
918
919 return SCIP_OKAY;
920}
SCIP_DECL_HEUREXIT(Heur2opt::scip_exit)
SCIP_DECL_HEURINIT(Heur2opt::scip_init)
SCIP_DECL_HEURINITSOL(Heur2opt::scip_initsol)
SCIP_DECL_HEURFREE(Heur2opt::scip_free)
SCIP_DECL_HEUREXEC(Heur2opt::scip_exec)
static SCIP_RETCODE calculateBounds(SCIP *scip, SCIP_VAR *branchvar, SCIP_Real *downlb, SCIP_Real *downub, SCIP_Real *uplb, SCIP_Real *upub)
SCIP_Real * r
#define SCIP_Longint
#define SCIP_Bool
#define MIN(x, y)
#define SCIP_Real
#define MAX(x, y)
#define TRUE
#define FALSE
GCG interface methods.
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
SCIP * GCGgetMasterprob(GCG *gcg)
Definition gcg.cpp:99
#define SCIPdebugMsg
SCIP_RETCODE SCIPaddIntParam(SCIP *scip, const char *name, const char *desc, int *valueptr, SCIP_Bool isadvanced, int defaultvalue, int minvalue, int maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
SCIP_RETCODE SCIPaddRealParam(SCIP *scip, const char *name, const char *desc, SCIP_Real *valueptr, SCIP_Bool isadvanced, SCIP_Real defaultvalue, SCIP_Real minvalue, SCIP_Real maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
SCIP_RETCODE SCIPgetExternBranchCands(SCIP *scip, SCIP_VAR ***externcands, SCIP_Real **externcandssol, SCIP_Real **externcandsscore, int *nexterncands, int *nprioexterncands, int *nprioexternbins, int *nprioexternints, int *nprioexternimpls)
int SCIPcolGetLPPos(SCIP_COL *col)
SCIP_VAR * SCIPcolGetVar(SCIP_COL *col)
SCIP_Real * SCIPcolGetVals(SCIP_COL *col)
SCIP_ROW ** SCIPcolGetRows(SCIP_COL *col)
int SCIPcolGetNLPNonz(SCIP_COL *col)
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
SCIP_Longint SCIPheurGetNSolsFound(SCIP_HEUR *heur)
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
SCIP_RETCODE SCIPsetHeurInitsol(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINITSOL((*heurinitsol)))
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
const char * SCIPheurGetName(SCIP_HEUR *heur)
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
SCIP_ROW ** SCIPgetLPRows(SCIP *scip)
int SCIPgetNLPRows(SCIP *scip)
SCIP_LPSOLSTAT SCIPgetLPSolstat(SCIP *scip)
#define SCIPallocBufferArray(scip, ptr, num)
#define SCIPfreeBlockMemory(scip, ptr)
#define SCIPfreeBufferArrayNull(scip, ptr)
#define SCIPallocBlockMemory(scip, ptr)
SCIP_Real SCIProwGetLhs(SCIP_ROW *row)
int SCIProwGetNNonz(SCIP_ROW *row)
SCIP_COL ** SCIProwGetCols(SCIP_ROW *row)
SCIP_Real SCIProwGetRhs(SCIP_ROW *row)
int SCIProwGetLPPos(SCIP_ROW *row)
const char * SCIProwGetName(SCIP_ROW *row)
SCIP_Real * SCIProwGetVals(SCIP_ROW *row)
SCIP_Real SCIPgetRowSolActivity(SCIP *scip, SCIP_ROW *row, SCIP_SOL *sol)
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
SCIP_RETCODE SCIPprintSol(SCIP *scip, SCIP_SOL *sol, FILE *file, SCIP_Bool printzeros)
SCIP_RETCODE SCIPlinkRelaxSol(SCIP *scip, SCIP_SOL *sol)
SCIP_RETCODE SCIPtrySol(SCIP *scip, SCIP_SOL *sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
SCIP_RETCODE SCIPsetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real val)
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
SCIP_Longint SCIPgetNLPs(SCIP *scip)
SCIP_Bool SCIPisFeasGE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_Bool SCIPisFeasEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPfeasCeil(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasZero(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPfeasFloor(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisFeasNegative(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
SCIP_COL * SCIPvarGetCol(SCIP_VAR *var)
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
int SCIPvarGetIndex(SCIP_VAR *var)
const char * SCIPvarGetName(SCIP_VAR *var)
SCIP_Bool SCIPisRelaxSolValid(SCIP *scip)
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
#define HEUR_FREQOFS
#define HEUR_DESC
#define HEUR_DISPCHAR
#define HEUR_MAXDEPTH
#define HEUR_PRIORITY
#define HEUR_NAME
static SCIP_VAR ** lpcands
int nlpcands
SCIP * origprob
#define HEUR_FREQ
#define HEUR_TIMING
#define HEUR_USESSUBSCIP
* result
SCIP_VAR * var
int i
static SCIP_HEURDATA * heurdata
SCIP_Real oldsolval
heurdata lastlp
SCIP_SOL * sol
SCIP_Longint nlps
SCIP_Real * activities
#define DEFAULT_MAXROUNDINGLOOPS
static SCIP_RETCODE updateSlacks(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real shiftvalue, SCIP_Real *upslacks, SCIP_Real *downslacks, SCIP_Real *activities, SCIP_VAR **slackvars, SCIP_Real *slackcoeffs, int nslacks)
SCIP_RETCODE GCGincludeHeurGcgzirounding(GCG *gcg)
static void rowFindSlackVar(SCIP *scip, SCIP_ROW *row, SCIP_VAR **varpointer, SCIP_Real *coeffpointer)
#define DEFAULT_STOPPERCENTAGE
#define DEFAULT_STOPZIROUND
#define DEFAULT_MINSTOPNCALLS
#define MINSHIFT
static SCIP_Real getZiValue(SCIP *scip, SCIP_Real val)
zirounding primal heuristic
Direction
DIRECTION_DOWN
DIRECTION_UP
enum Direction DIRECTION
#define NULL
#define BMScopyMemoryArray(ptr, source, num)
#define BMSclearMemoryArray(ptr, num)
#define SCIPstatisticMessage
GCG relaxator.
SCIP_Longint lastlp
SCIP_Real stoppercentage
struct Gcg GCG
Definition type_gcg.h:44
struct SCIP_HeurData SCIP_HEURDATA
struct SCIP_Heur SCIP_HEUR
struct SCIP_Row SCIP_ROW
struct SCIP_Col SCIP_COL
SCIP_LPSOLSTAT_OPTIMAL
SCIP_DIDNOTRUN
SCIP_DIDNOTFIND
SCIP_FOUNDSOL
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
struct SCIP_Sol SCIP_SOL
struct SCIP_Var SCIP_VAR
SCIP_VARTYPE_CONTINUOUS
SCIP_VARTYPE_BINARY
SCIP_VARSTATUS_COLUMN