GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
heur_gcgshifting.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_gcgshifting.c
30 * @brief LP gcgrounding heuristic that tries to recover from intermediate infeasibilities and shifts continuous variables
31 * @author Tobias Achterberg
32 * @author Christian Puchert
33 */
34
35/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
36
37#include <assert.h>
38#include <string.h>
39
41#include "gcg/gcg.h"
42#include "gcg/relax_gcg.h"
43#include "scip/misc.h"
44
45
46#define HEUR_NAME "gcgshifting"
47#define HEUR_DESC "LP rounding heuristic on original variables with infeasibility recovering also using continuous variables"
48#define HEUR_DISPCHAR 's'
49#define HEUR_PRIORITY -5000
50#define HEUR_FREQ 10
51#define HEUR_FREQOFS 0
52#define HEUR_MAXDEPTH -1
53#define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
54#define HEUR_USESSUBSCIP FALSE
55
56#define MAXSHIFTINGS 50 /**< maximal number of non improving shiftings */
57#define WEIGHTFACTOR 1.1
58#define DEFAULT_RANDSEED 31 /**< initial random seed */
59
60
61/* locally defined heuristic data */
62struct SCIP_HeurData
63{
64 GCG* gcg; /**< GCG data structure */
65 SCIP_SOL* sol; /**< working solution */
66 SCIP_RANDNUMGEN* randnumgen; /**< random number generator */
67 SCIP_Longint lastlp; /**< last LP number where the heuristic was applied */
68};
69
70
71
72
73/*
74 * local methods
75 */
76
77/** update row violation arrays after a row's activity value changed */
78static
80 SCIP* scip, /**< SCIP data structure */
81 SCIP_ROW* row, /**< LP row */
82 SCIP_ROW** violrows, /**< array with currently violated rows */
83 int* violrowpos, /**< position of LP rows in violrows array */
84 int* nviolrows, /**< pointer to the number of currently violated rows */
85 SCIP_Real oldactivity, /**< old activity value of LP row */
86 SCIP_Real newactivity /**< new activity value of LP row */
87 )
88{
89 SCIP_Real lhs;
90 SCIP_Real rhs;
91 SCIP_Bool oldviol;
92 SCIP_Bool newviol;
93
97
98 lhs = SCIProwGetLhs(row);
99 rhs = SCIProwGetRhs(row);
100 oldviol = (SCIPisFeasLT(scip, oldactivity, lhs) || SCIPisFeasGT(scip, oldactivity, rhs));
101 newviol = (SCIPisFeasLT(scip, newactivity, lhs) || SCIPisFeasGT(scip, newactivity, rhs));
102 if( oldviol != newviol )
103 {
104 int rowpos;
105
106 rowpos = SCIProwGetLPPos(row);
107 assert(rowpos >= 0);
108
109 if( oldviol )
110 {
111 int violpos;
112
113 /* the row violation was repaired: remove row from violrows array, decrease violation count */
114 violpos = violrowpos[rowpos];
115 assert(0 <= violpos && violpos < *nviolrows);
116 assert(violrows[violpos] == row);
117 violrowpos[rowpos] = -1;
118 if( violpos != *nviolrows-1 )
119 {
120 violrows[violpos] = violrows[*nviolrows-1];
121 violrowpos[SCIProwGetLPPos(violrows[violpos])] = violpos;
122 }
123 (*nviolrows)--;
124 }
125 else
126 {
127 /* the row is now violated: add row to violrows array, increase violation count */
128 assert(violrowpos[rowpos] == -1);
129 violrows[*nviolrows] = row;
130 violrowpos[rowpos] = *nviolrows;
131 (*nviolrows)++;
132 }
133 }
134}
135
136/** update row activities after a variable's solution value changed */
137static
139 SCIP* scip, /**< SCIP data structure */
140 SCIP_Real* activities, /**< LP row activities */
141 SCIP_ROW** violrows, /**< array with currently violated rows */
142 int* violrowpos, /**< position of LP rows in violrows array */
143 int* nviolrows, /**< pointer to the number of currently violated rows */
144 int nlprows, /**< number of rows in current LP */
145 SCIP_VAR* var, /**< variable that has been changed */
146 SCIP_Real oldsolval, /**< old solution value of variable */
147 SCIP_Real newsolval /**< new solution value of variable */
148 )
149{
150 SCIP_COL* col;
151 SCIP_ROW** colrows;
152 SCIP_Real* colvals;
153 SCIP_Real delta;
154 int ncolrows;
155 int r;
156
159 assert(0 <= *nviolrows && *nviolrows <= nlprows);
160
161 delta = newsolval - oldsolval;
162 col = SCIPvarGetCol(var);
163 colrows = SCIPcolGetRows(col);
164 colvals = SCIPcolGetVals(col);
165 ncolrows = SCIPcolGetNLPNonz(col);
166 assert(ncolrows == 0 || (colrows != NULL && colvals != NULL));
167
168 for( r = 0; r < ncolrows; ++r )
169 {
170 SCIP_ROW* row;
171 int rowpos;
172
173 row = colrows[r];
174 rowpos = SCIProwGetLPPos(row);
175 assert(-1 <= rowpos && rowpos < nlprows);
176
177 if( rowpos >= 0 && !SCIProwIsLocal(row) )
178 {
179 SCIP_Real oldactivity;
180
181 assert(SCIProwIsInLP(row));
182
183 /* update row activity */
184 oldactivity = activities[rowpos];
185 if( !SCIPisInfinity(scip, -oldactivity) && !SCIPisInfinity(scip, oldactivity) )
186 {
187 SCIP_Real newactivity;
188
189 newactivity = oldactivity + delta * colvals[r];
190 if( SCIPisInfinity(scip, newactivity) )
191 newactivity = SCIPinfinity(scip);
192 else if( SCIPisInfinity(scip, -newactivity) )
193 newactivity = -SCIPinfinity(scip);
194 activities[rowpos] = newactivity;
195
196 /* update row violation arrays */
197 updateViolations(scip, row, violrows, violrowpos, nviolrows, oldactivity, newactivity);
198 }
199 }
200 }
201
202 return SCIP_OKAY;
203}
204
205/** returns a variable, that pushes activity of the row in the given direction with minimal negative impact on other rows;
206 * if variables have equal impact, chooses the one with best objective value improvement in corresponding direction;
207 * prefer fractional integers over other variables in order to become integral during the process;
208 * shifting in a direction is forbidden, if this forces the objective value over the upper bound, or if the variable
209 * was already shifted in the opposite direction
210 */
211static
213 SCIP* scip, /**< SCIP data structure */
214 SCIP_SOL* sol, /**< primal solution */
215 SCIP_ROW* row, /**< LP row */
216 SCIP_Real rowactivity, /**< activity of LP row */
217 int direction, /**< should the activity be increased (+1) or decreased (-1)? */
218 SCIP_Real* nincreases, /**< array with weighted number of increasings per variables */
219 SCIP_Real* ndecreases, /**< array with weighted number of decreasings per variables */
220 SCIP_Real increaseweight, /**< current weight of increase/decrease updates */
221 SCIP_VAR** shiftvar, /**< pointer to store the shifting variable, returns NULL if impossible */
222 SCIP_Real* oldsolval, /**< pointer to store old solution value of shifting variable */
223 SCIP_Real* newsolval /**< pointer to store new (shifted) solution value of shifting variable */
224 )
225{
226 SCIP_COL** rowcols;
227 SCIP_Real* rowvals;
228 int nrowcols;
229 SCIP_Real activitydelta;
230 SCIP_Real bestshiftscore;
231 SCIP_Real bestdeltaobj;
232 int c;
233
234 assert(direction == +1 || direction == -1);
237 assert(shiftvar != NULL);
239 assert(newsolval != NULL);
240
241 /* get row entries */
242 rowcols = SCIProwGetCols(row);
243 rowvals = SCIProwGetVals(row);
244 nrowcols = SCIProwGetNLPNonz(row);
245
246 /* calculate how much the activity must be shifted in order to become feasible */
247 activitydelta = (direction == +1 ? SCIProwGetLhs(row) - rowactivity : SCIProwGetRhs(row) - rowactivity);
248 assert((direction == +1 && SCIPisPositive(scip, activitydelta))
249 || (direction == -1 && SCIPisNegative(scip, activitydelta)));
250
251 /* select shifting variable */
252 bestshiftscore = SCIP_REAL_MAX;
253 bestdeltaobj = SCIPinfinity(scip);
254 *shiftvar = NULL;
255 *newsolval = 0.0;
256 *oldsolval = 0.0;
257 for( c = 0; c < nrowcols; ++c )
258 {
259 SCIP_COL* col;
260 SCIP_VAR* var;
261 SCIP_Real val;
263 SCIP_Real shiftscore;
264 SCIP_Bool isinteger;
265 SCIP_Bool isfrac;
266 SCIP_Bool increase;
267
268 col = rowcols[c];
269 var = SCIPcolGetVar(col);
270 val = rowvals[c];
271 assert(!SCIPisZero(scip, val));
273
275 isfrac = isinteger && !SCIPisFeasIntegral(scip, solval);
276 increase = (direction * val > 0.0);
277
278 /* calculate the score of the shifting (prefer smaller values) */
279 if( isfrac )
280 shiftscore = increase ? -1.0 / (SCIPvarGetNLocksUp(var) + 1.0) :
281 -1.0 / (SCIPvarGetNLocksDown(var) + 1.0);
282 else
283 {
284 int probindex;
285 probindex = SCIPvarGetProbindex(var);
286
287 if( increase )
288 shiftscore = ndecreases[probindex]/increaseweight;
289 else
290 shiftscore = nincreases[probindex]/increaseweight;
291 if( isinteger )
292 shiftscore += 1.0;
293 }
294
295 if( shiftscore <= bestshiftscore )
296 {
297 SCIP_Real deltaobj;
298 SCIP_Real shiftval;
299
300 if( !increase )
301 {
302 /* shifting down */
303 assert(direction * val < 0.0);
304 if( isfrac )
305 shiftval = SCIPfeasFloor(scip, solval);
306 else
307 {
308 SCIP_Real lb;
309
310 assert(activitydelta/val < 0.0);
311 shiftval = solval + activitydelta/val;
312 assert(shiftval <= solval); /* may be equal due to numerical digit erasement in the subtraction */
314 shiftval = SCIPfeasFloor(scip, shiftval);
316 shiftval = MAX(shiftval, lb);
317 }
318 }
319 else
320 {
321 /* shifting up */
322 assert(direction * val > 0.0);
323 if( isfrac )
324 shiftval = SCIPfeasCeil(scip, solval);
325 else
326 {
327 SCIP_Real ub;
328
329 assert(activitydelta/val > 0.0);
330 shiftval = solval + activitydelta/val;
331 assert(shiftval >= solval); /* may be equal due to numerical digit erasement in the subtraction */
333 shiftval = SCIPfeasCeil(scip, shiftval);
335 shiftval = MIN(shiftval, ub);
336 }
337 }
338
339 if( SCIPisEQ(scip, shiftval, solval) )
340 continue;
341
342 deltaobj = SCIPvarGetObj(var) * (shiftval - solval);
343 if( shiftscore < bestshiftscore || deltaobj < bestdeltaobj )
344 {
345 bestshiftscore = shiftscore;
346 bestdeltaobj = deltaobj;
347 *shiftvar = var;
348 *oldsolval = solval;
349 *newsolval = shiftval;
350 }
351 }
352 }
353
354 return SCIP_OKAY;
355}
356
357/** returns a fractional variable, that has most impact on rows in opposite direction, i.e. that is most crucial to
358 * fix in the other direction;
359 * if variables have equal impact, chooses the one with best objective value improvement in corresponding direction;
360 * shifting in a direction is forbidden, if this forces the objective value over the upper bound
361 */
362static
364 SCIP* scip, /**< SCIP data structure */
365 SCIP_SOL* sol, /**< primal solution */
366 SCIP_Real minobj, /**< minimal objective value possible after shifting remaining fractional vars */
367 SCIP_VAR** lpcands, /**< fractional variables in LP */
368 int nlpcands, /**< number of fractional variables in LP */
369 SCIP_VAR** shiftvar, /**< pointer to store the shifting variable, returns NULL if impossible */
370 SCIP_Real* oldsolval, /**< old (fractional) solution value of shifting variable */
371 SCIP_Real* newsolval /**< new (shifted) solution value of shifting variable */
372 )
373{
374 SCIP_Real bestdeltaobj;
375 int maxnlocks;
376 int v;
377
378 assert(shiftvar != NULL);
380 assert(newsolval != NULL);
381
382 /* select shifting variable */
383 maxnlocks = -1;
384 bestdeltaobj = SCIPinfinity(scip);
385 *shiftvar = NULL;
386 for( v = 0; v < nlpcands; ++v )
387 {
388 SCIP_VAR* var;
390
391 var = lpcands[v];
393
396 {
397 SCIP_Real shiftval;
399 SCIP_Real deltaobj;
400 int nlocks;
401
403
404 /* shifting down */
405 nlocks = SCIPvarGetNLocksUp(var);
406 if( nlocks >= maxnlocks )
407 {
408 shiftval = SCIPfeasFloor(scip, solval);
409 deltaobj = obj * (shiftval - solval);
410 if( (nlocks > maxnlocks || deltaobj < bestdeltaobj) && minobj - obj < SCIPgetCutoffbound(scip) )
411 {
412 maxnlocks = nlocks;
413 bestdeltaobj = deltaobj;
414 *shiftvar = var;
415 *oldsolval = solval;
416 *newsolval = shiftval;
417 }
418 }
419
420 /* shifting up */
421 nlocks = SCIPvarGetNLocksDown(var);
422 if( nlocks >= maxnlocks )
423 {
424 shiftval = SCIPfeasCeil(scip, solval);
425 deltaobj = obj * (shiftval - solval);
426 if( (nlocks > maxnlocks || deltaobj < bestdeltaobj) && minobj + obj < SCIPgetCutoffbound(scip) )
427 {
428 maxnlocks = nlocks;
429 bestdeltaobj = deltaobj;
430 *shiftvar = var;
431 *oldsolval = solval;
432 *newsolval = shiftval;
433 }
434 }
435 }
436 }
437
438 return SCIP_OKAY;
439}
440
441/** adds a given value to the fractionality counters of the rows in which the given variable appears */
442static
444 int* nfracsinrow, /**< array to store number of fractional variables per row */
445 int nlprows, /**< number of rows in LP */
446 SCIP_VAR* var, /**< variable for which the counting should be updated */
447 int incval /**< value that should be added to the corresponding array entries */
448 )
449{
450 SCIP_COL* col;
451 SCIP_ROW** rows;
452 int nrows;
453 int r;
454
455 col = SCIPvarGetCol(var);
456 rows = SCIPcolGetRows(col);
457 nrows = SCIPcolGetNLPNonz(col);
458 for( r = 0; r < nrows; ++r )
459 {
460 int rowidx;
461
462 rowidx = SCIProwGetLPPos(rows[r]);
463 assert(0 <= rowidx && rowidx < nlprows);
464 nfracsinrow[rowidx] += incval;
465 assert(nfracsinrow[rowidx] >= 0);
466 }
467}
468
469
470
471/*
472 * Callback methods
473 */
474
475/** copy method for primal heuristic plugins (called when SCIP copies plugins) */
476#define heurCopyGcgshifting NULL
477
478/** destructor of primal heuristic to free user data (called when SCIP is exiting) */
479static
480SCIP_DECL_HEURFREE(heurFreeGcgshifting) /*lint --e{715}*/
481{ /*lint --e{715}*/
483
484 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
485
486 /* free heuristic data */
490
491 return SCIP_OKAY;
492}
493
494
495/** initialization method of primal heuristic (called after problem was transformed) */
496static
497SCIP_DECL_HEURINIT(heurInitGcgshifting) /*lint --e{715}*/
498{ /*lint --e{715}*/
500
501 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
502
503 /* create heuristic data */
505 assert(heurdata != NULL);
508
509 /* create random number generator */
512
514
515 return SCIP_OKAY;
516}
517
518/** deinitialization method of primal heuristic (called before transformed problem is freed) */
519static
520SCIP_DECL_HEUREXIT(heurExitGcgshifting) /*lint --e{715}*/
521{ /*lint --e{715}*/
523
524 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
525
526 /* free heuristic data */
528 assert(heurdata != NULL);
530
531 /* free random number generator */
533
534 return SCIP_OKAY;
535}
536
537/** solving process initialization method of primal heuristic (called when branch and bound process is about to begin) */
538static
539SCIP_DECL_HEURINITSOL(heurInitsolGcgshifting)
540{
542
543 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
544
546 assert(heurdata != NULL);
547 heurdata->lastlp = -1;
548
549 return SCIP_OKAY;
550}
551
552
553/** solving process deinitialization method of primal heuristic (called before branch and bound process data is freed) */
554#define heurExitsolGcgshifting NULL
555
556
557/** execution method of primal heuristic */
558static
559SCIP_DECL_HEUREXEC(heurExecGcgshifting) /*lint --e{715}*/
560{ /*lint --e{715}*/
578 int nvars;
579 int nfrac;
583 int c;
584 int r;
589
590 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
593
594 /* get heuristic data */
596 assert(heurdata != NULL);
597
598 /* get master problem */
601
603
604 /* do not execute the heuristic on invalid relaxation solutions
605 * (which is the case if the node has been cut off)
606 */
608 {
609 SCIPdebugMessage("skipping GCG shifting: invalid relaxation solution\n");
610 return SCIP_OKAY;
611 }
612
613 /* only call heuristic, if an optimal LP solution is at hand */
615 return SCIP_OKAY;
616
617 /* don't call heuristic, if we have already processed the current LP solution */
619 if( nlps == heurdata->lastlp )
620 return SCIP_OKAY;
622
623 /* don't call heuristic, if it was not successful enough in the past */
627 if( nnodes % ((ncalls/100)/(nsolsfound+1)+1) != 0 )
628 return SCIP_OKAY;
629
630 /* get fractional variables, that should be integral */
632 nfrac = nlpcands;
633
634 /* only call heuristic, if LP solution is fractional */
635 if( nfrac == 0 )
636 return SCIP_OKAY;
637
639
640 /* get LP rows */
642
643 SCIPdebugMessage("executing GCG shifting heuristic: %d LP rows, %d fractionals\n", nlprows, nfrac);;
644
645 /* get memory for activities, violated rows, and row violation positions */
656
657 /* get the activities for all globally valid rows;
658 * the rows should be feasible, but due to numerical inaccuracies in the LP solver, they can be violated
659 */
660 nviolrows = 0;
661 for( r = 0; r < nlprows; ++r )
662 {
663 SCIP_ROW* row;
664
665 row = lprows[r];
666 assert(SCIProwGetLPPos(row) == r);
667
668 if( !SCIProwIsLocal(row) )
669 {
673 {
674 violrows[nviolrows] = row;
676 nviolrows++;
677 }
678 else
679 violrowpos[r] = -1;
680 }
681 }
682
683 /* calc the current number of fractional variables in rows */
684 for( c = 0; c < nlpcands; ++c )
686
687 /* get the working solution from heuristic's local data */
688 sol = heurdata->sol;
690
691 /* copy the current LP solution to the working solution */
693
694 /* calculate the minimal objective value possible after rounding fractional variables */
696 /* since the heuristic timing was changed to AFTERNODE, it might happen that it is called on a
697 * node with has been cut off; in that case, delay the heuristic
698 */
710 for( c = 0; c < nlpcands; ++c )
711 {
712 SCIP_Real bestshiftval;
713
715 bestshiftval = obj > 0.0 ? SCIPfeasFloor(scip, lpcandssol[c]) : SCIPfeasCeil(scip, lpcandssol[c]);
716 minobj += obj * (bestshiftval - lpcandssol[c]);
717 }
718
719 /* try to shift remaining variables in order to become/stay feasible */
721 minnviolrows = INT_MAX;
722 increaseweight = 1.0;
724 {
725 SCIP_VAR* shiftvar;
727 SCIP_Real newsolval;
728 SCIP_Bool oldsolvalisfrac;
729 int nprevviolrows;
730
731 SCIPdebugMessage("GCG shifting heuristic: nfrac=%d, nviolrows=%d, obj=%g (best possible obj: %g), cutoff=%g\n",
734
735 nprevviolrows = nviolrows;
736
737 /* choose next variable to process:
738 * - if a violated row exists, shift a variable decreasing the violation, that has least impact on other rows
739 * - otherwise, shift a variable, that has strongest devastating impact on rows in opposite direction
740 */
741 shiftvar = NULL;
742 oldsolval = 0.0;
743 newsolval = 0.0;
744 if( nviolrows > 0 && (nfrac == 0 || nnonimprovingshifts < MAXSHIFTINGS-1) )
745 {
746 SCIP_ROW* row;
747 int rowidx;
748 int rowpos;
749 int direction;
750
751 rowidx = -1;
752 rowpos = -1;
753 row = NULL;
754 if( nfrac > 0 )
755 {
756 for( rowidx = nviolrows-1; rowidx >= 0; --rowidx )
757 {
758 row = violrows[rowidx];
759 rowpos = SCIProwGetLPPos(row);
760 assert(violrowpos[rowpos] == rowidx);
761 if( nfracsinrow[rowpos] > 0 )
762 break;
763 }
764 }
765 if( rowidx == -1 )
766 {
768 row = violrows[rowidx];
769 rowpos = SCIProwGetLPPos(row);
770 assert(0 <= rowpos && rowpos < nlprows);
771 assert(violrowpos[rowpos] == rowidx);
772 assert(nfracsinrow[rowpos] == 0);
773 }
774 assert(violrowpos[rowpos] == rowidx);
775
776 SCIPdebugMessage("GCG shifting heuristic: try to fix violated row <%s>: %g <= %g <= %g\n",
777 SCIProwGetName(row), SCIProwGetLhs(row), activities[rowpos], SCIProwGetRhs(row));
779
780 /* get direction in which activity must be shifted */
782 || SCIPisFeasGT(scip, activities[rowpos], SCIProwGetRhs(row)));
783 direction = SCIPisFeasLT(scip, activities[rowpos], SCIProwGetLhs(row)) ? +1 : -1;
784
785 /* search a variable that can shift the activity in the necessary direction */
786 SCIP_CALL( selectShifting(scip, sol, row, activities[rowpos], direction,
787 nincreases, ndecreases, increaseweight, &shiftvar, &oldsolval, &newsolval) );
788 }
789
790 if( shiftvar == NULL && nfrac > 0 )
791 {
792 SCIPdebugMessage("GCG shifting heuristic: search rounding variable and try to stay feasible\n");
793 SCIP_CALL( selectEssentialRounding(scip, sol, minobj, lpcands, nlpcands, &shiftvar, &oldsolval, &newsolval) );
794 }
795
796 /* check, whether shifting was possible */
797 if( shiftvar == NULL || SCIPisEQ(scip, oldsolval, newsolval) )
798 {
799 SCIPdebugMessage("GCG shifting heuristic: -> didn't find a shifting variable\n");
800 break;
801 }
802
803 SCIPdebugMessage("GCG shifting heuristic: -> shift var <%s>[%g,%g], type=%d, oldval=%g, newval=%g, obj=%g\n",
804 SCIPvarGetName(shiftvar), SCIPvarGetLbGlobal(shiftvar), SCIPvarGetUbGlobal(shiftvar), SCIPvarGetType(shiftvar),
805 oldsolval, newsolval, SCIPvarGetObj(shiftvar));
806
807 /* update row activities of globally valid rows */
809 shiftvar, oldsolval, newsolval) );
810 if( nviolrows >= nprevviolrows )
812 else if( nviolrows < minnviolrows )
813 {
816 }
817
818 /* store new solution value and decrease fractionality counter */
819 SCIP_CALL( SCIPsetSolVal(scip, sol, shiftvar, newsolval) );
820
821 /* update fractionality counter and minimal objective value possible after shifting remaining variables */
822 oldsolvalisfrac = !SCIPisFeasIntegral(scip, oldsolval)
824 obj = SCIPvarGetObj(shiftvar);
826 && oldsolvalisfrac )
827 {
828 assert(SCIPisFeasIntegral(scip, newsolval));
829 nfrac--;
831 minnviolrows = INT_MAX;
832 addFracCounter(nfracsinrow, nlprows, shiftvar, -1);
833
834 /* the rounding was already calculated into the minobj -> update only if rounding in "wrong" direction */
835 if( obj > 0.0 && newsolval > oldsolval )
836 minobj += obj;
837 else if( obj < 0.0 && newsolval < oldsolval )
838 minobj -= obj;
839 }
840 else
841 {
842 /* update minimal possible objective value */
843 minobj += obj * (newsolval - oldsolval);
844 }
845
846 /* update increase/decrease arrays */
847 if( !oldsolvalisfrac )
848 {
849 int probindex;
850
851 probindex = SCIPvarGetProbindex(shiftvar);
852 assert(0 <= probindex && probindex < nvars);
854 if( newsolval < oldsolval )
855 ndecreases[probindex] += increaseweight;
856 else
857 nincreases[probindex] += increaseweight;
858 if( increaseweight >= 1e+09 )
859 {
860 int i;
861
862 for( i = 0; i < nvars; ++i )
863 {
866 }
867 increaseweight = 1.0;
868 }
869 }
870
871 SCIPdebugMessage("gcg shifting heuristic: -> nfrac=%d, nviolrows=%d, obj=%g (best possible obj: %g)\n",
873 }
874
875 /* check, if the new solution is feasible */
876 if( nfrac == 0 && nviolrows == 0 )
877 {
878 SCIP_Bool stored;
879
880 /* check solution for feasibility, and add it to solution store if possible
881 * neither integrality nor feasibility of LP rows has to be checked, because this is already
882 * done in the shifting heuristic itself; however, we better check feasibility of LP rows,
883 * because of numerical problems with activity updating
884 */
886
887 if( stored )
888 {
889 SCIPdebugMessage("found feasible shifted solution:\n");
892 }
893 }
894
895 /* free memory buffers */
902
903 return SCIP_OKAY;
904}
905
906
907
908
909/*
910 * heuristic specific interface methods
911 */
912
913/** creates the GCG shifting heuristic with infeasibility recovering and includes it in SCIP */
915 GCG* gcg /**< GCG data structure */
916 )
917{
920
922 heurdata->gcg = gcg;
923
924 /* include heuristic */
927 heurCopyGcgshifting, heurFreeGcgshifting, heurInitGcgshifting, heurExitGcgshifting,
928 heurInitsolGcgshifting, heurExitsolGcgshifting, heurExecGcgshifting, heurdata) );
929
930 return SCIP_OKAY;
931}
932
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)
SCIP_Real * r
#define DEFAULT_RANDSEED
#define SCIP_Longint
#define SCIP_REAL_MAX
#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
SCIP_STAGE SCIPgetStage(SCIP *scip)
int SCIPgetNVars(SCIP *scip)
SCIP_RETCODE SCIPgetExternBranchCands(SCIP *scip, SCIP_VAR ***externcands, SCIP_Real **externcandssol, SCIP_Real **externcandsscore, int *nexterncands, int *nprioexterncands, int *nprioexternbins, int *nprioexternints, int *nprioexternimpls)
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_Longint SCIPheurGetNSolsFound(SCIP_HEUR *heur)
SCIP_RETCODE SCIPincludeHeur(SCIP *scip, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEURCOPY((*heurcopy)), SCIP_DECL_HEURFREE((*heurfree)), SCIP_DECL_HEURINIT((*heurinit)), SCIP_DECL_HEUREXIT((*heurexit)), SCIP_DECL_HEURINITSOL((*heurinitsol)), SCIP_DECL_HEUREXITSOL((*heurexitsol)), SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
const char * SCIPheurGetName(SCIP_HEUR *heur)
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
SCIP_RETCODE SCIPgetLPRowsData(SCIP *scip, SCIP_ROW ***rows, int *nrows)
SCIP_LPSOLSTAT SCIPgetLPSolstat(SCIP *scip)
#define SCIPallocBufferArray(scip, ptr, num)
#define SCIPallocMemory(scip, ptr)
SCIP_Real SCIProwGetLhs(SCIP_ROW *row)
SCIP_COL ** SCIProwGetCols(SCIP_ROW *row)
SCIP_Real SCIProwGetRhs(SCIP_ROW *row)
int SCIProwGetNLPNonz(SCIP_ROW *row)
int SCIProwGetLPPos(SCIP_ROW *row)
SCIP_Bool SCIProwIsLocal(SCIP_ROW *row)
SCIP_RETCODE SCIPprintRow(SCIP *scip, SCIP_ROW *row, FILE *file)
const char * SCIProwGetName(SCIP_ROW *row)
SCIP_Bool SCIProwIsInLP(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_Real SCIPgetSolTransObj(SCIP *scip, SCIP_SOL *sol)
SCIP_Real SCIPretransformObj(SCIP *scip, SCIP_Real obj)
SCIP_Longint SCIPgetNNodes(SCIP *scip)
SCIP_Longint SCIPgetNLPs(SCIP *scip)
SCIP_Real SCIPgetCutoffbound(SCIP *scip)
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPfeasCeil(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 SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisNegative(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisZero(SCIP *scip, SCIP_Real val)
SCIP_COL * SCIPvarGetCol(SCIP_VAR *var)
int SCIPvarGetNLocksDown(SCIP_VAR *var)
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
SCIP_Real SCIPvarGetUbGlobal(SCIP_VAR *var)
int SCIPvarGetProbindex(SCIP_VAR *var)
const char * SCIPvarGetName(SCIP_VAR *var)
SCIP_Bool SCIPvarIsIntegral(SCIP_VAR *var)
SCIP_Bool SCIPisRelaxSolValid(SCIP *scip)
SCIP_Real SCIPvarGetLbGlobal(SCIP_VAR *var)
int SCIPvarGetNLocksUp(SCIP_VAR *var)
void SCIPfreeRandom(SCIP *scip, SCIP_RANDNUMGEN **randnumgen)
unsigned int SCIPinitializeRandomSeed(SCIP *scip, unsigned int initialseedvalue)
SCIP_RETCODE SCIPcreateRandom(SCIP *scip, SCIP_RANDNUMGEN **randnumgen, unsigned int initialseed, SCIP_Bool useglobalseed)
int SCIPrandomGetInt(SCIP_RANDNUMGEN *randnumgen, int minrandval, int maxrandval)
SCIPfreeMemory(GCGgetOrigprob(gcg), &divingdata)
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
SCIP_Real * lpcandssol
#define HEUR_NAME
static SCIP_VAR ** lpcands
int nlpcands
SCIP * origprob
#define HEUR_FREQ
#define HEUR_TIMING
#define HEUR_USESSUBSCIP
* result
SCIP_VAR * var
SCIP_Longint ncalls
SCIP_Longint nsolsfound
SCIP_Real solval
SCIPdebugMessage("feasibility pump finished [%d iterations done].\n", nloops)
int i
static SCIP_HEURDATA * heurdata
int nvars
SCIPfreeBufferArray(scip, &cycles)
SCIP_Real oldsolval
int nlprows
static SCIP_RETCODE selectEssentialRounding(SCIP *scip, SCIP_SOL *sol, SCIP_Real minobj, SCIP_VAR **lpcands, int nlpcands, SCIP_VAR **roundvar, SCIP_Real *oldsolval, SCIP_Real *newsolval)
static SCIP_RETCODE updateActivities(SCIP *scip, SCIP_Real *activities, SCIP_ROW **violrows, int *violrowpos, int *nviolrows, int nlprows, SCIP_VAR *var, SCIP_Real oldsolval, SCIP_Real newsolval)
static void updateViolations(SCIP *scip, SCIP_ROW *row, SCIP_ROW **violrows, int *violrowpos, int *nviolrows, SCIP_Real oldactivity, SCIP_Real newactivity)
SCIP_ROW ** violrows
heurdata lastlp
int nfrac
SCIP_ROW ** lprows
SCIP_SOL * sol
int * violrowpos
SCIP_Longint nlps
SCIP_Real * activities
int nviolrows
SCIP_Real minobj
SCIP_Real obj
SCIP_Longint nnodes
SCIP_Real * nincreases
#define heurCopyGcgshifting
#define heurExitsolGcgshifting
int * nfracsinrow
int minnviolrows
static SCIP_RETCODE selectShifting(SCIP *scip, SCIP_SOL *sol, SCIP_ROW *row, SCIP_Real rowactivity, int direction, SCIP_Real *nincreases, SCIP_Real *ndecreases, SCIP_Real increaseweight, SCIP_VAR **shiftvar, SCIP_Real *oldsolval, SCIP_Real *newsolval)
SCIP_Real increaseweight
SCIP_Real * ndecreases
#define WEIGHTFACTOR
static void addFracCounter(int *nfracsinrow, int nlprows, SCIP_VAR *var, int incval)
SCIP_RETCODE GCGincludeHeurGcgshifting(GCG *gcg)
int nnonimprovingshifts
#define MAXSHIFTINGS
LP gcgrounding heuristic that tries to recover from intermediate infeasibilities and shifts continuou...
#define NULL
#define BMSclearMemoryArray(ptr, num)
#define SCIPdebug(x)
SCIP_SOL * GCGrelaxGetCurrentOrigSol(GCG *gcg)
Definition relax_gcg.c:4309
GCG relaxator.
SCIP_Longint lastlp
SCIP_RANDNUMGEN * randnumgen
struct Gcg GCG
Definition type_gcg.h:44
struct SCIP_HeurData SCIP_HEURDATA
struct SCIP_Row SCIP_ROW
struct SCIP_Col SCIP_COL
SCIP_LPSOLSTAT_OPTIMAL
struct SCIP_RandNumGen SCIP_RANDNUMGEN
SCIP_DIDNOTRUN
SCIP_DELAYED
SCIP_DIDNOTFIND
SCIP_FOUNDSOL
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
SCIP_STAGE_SOLVING
struct SCIP_Sol SCIP_SOL
struct SCIP_Var SCIP_VAR
SCIP_VARTYPE_INTEGER
SCIP_VARTYPE_BINARY