Scippy

GCG

Branch-and-Price & Column Generation for Everyone

heur_gcgrounding.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_gcgrounding.c
30 * @brief LP gcgrounding heuristic that tries to recover from intermediate infeasibilities
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/relax_gcg.h"
42#include "gcg/gcg.h"
43
44
45#define HEUR_NAME "gcgrounding"
46#define HEUR_DESC "LP rounding heuristic on original variables with infeasibility recovering"
47#define HEUR_DISPCHAR 'R'
48#define HEUR_PRIORITY -1000
49#define HEUR_FREQ 1
50#define HEUR_FREQOFS 0
51#define HEUR_MAXDEPTH -1
52#define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
53#define HEUR_USESSUBSCIP FALSE
54
55#define DEFAULT_SUCCESSFACTOR 100 /**< number of calls per found solution that are considered as standard success,
56 * a higher factor causes the heuristic to be called more often
57 */
58
59
60/* locally defined heuristic data */
61struct SCIP_HeurData
62{
63 GCG* gcg; /**< GCG data structure */
64 SCIP_SOL* sol; /**< working solution */
65 SCIP_Longint lastlp; /**< last LP number where the heuristic was applied */
66 int successfactor; /**< number of calls per found solution that are considered as standard success,
67 * a higher factor causes the heuristic to be called more often
68 */
69};
70
71
72
73
74/*
75 * local methods
76 */
77
78/** update row violation arrays after a row's activity value changed */
79static
81 SCIP* scip, /**< SCIP data structure */
82 SCIP_ROW* row, /**< LP row */
83 SCIP_ROW** violrows, /**< array with currently violated rows */
84 int* violrowpos, /**< position of LP rows in violrows array */
85 int* nviolrows, /**< pointer to the number of currently violated rows */
86 SCIP_Real oldactivity, /**< old activity value of LP row */
87 SCIP_Real newactivity /**< new activity value of LP row */
88 )
89{
90 SCIP_Real lhs;
91 SCIP_Real rhs;
92 SCIP_Bool oldviol;
93 SCIP_Bool newviol;
94
95 assert(violrows != NULL);
96 assert(violrowpos != NULL);
97 assert(nviolrows != NULL);
98
99 lhs = SCIProwGetLhs(row);
100 rhs = SCIProwGetRhs(row);
101 oldviol = (SCIPisFeasLT(scip, oldactivity, lhs) || SCIPisFeasGT(scip, oldactivity, rhs));
102 newviol = (SCIPisFeasLT(scip, newactivity, lhs) || SCIPisFeasGT(scip, newactivity, rhs));
103 if( oldviol != newviol )
104 {
105 int rowpos;
106
107 rowpos = SCIProwGetLPPos(row);
108 assert(rowpos >= 0);
109
110 if( oldviol )
111 {
112 int violpos;
113
114 /* the row violation was repaired: remove row from violrows array, decrease violation count */
115 violpos = violrowpos[rowpos];
116 assert(0 <= violpos && violpos < *nviolrows);
117 assert(violrows[violpos] == row);
118 violrowpos[rowpos] = -1;
119 if( violpos != *nviolrows-1 )
120 {
121 violrows[violpos] = violrows[*nviolrows-1];
122 violrowpos[SCIProwGetLPPos(violrows[violpos])] = violpos;
123 }
124 (*nviolrows)--;
125 }
126 else
127 {
128 /* the row is now violated: add row to violrows array, increase violation count */
129 assert(violrowpos[rowpos] == -1);
130 violrows[*nviolrows] = row;
131 violrowpos[rowpos] = *nviolrows;
132 (*nviolrows)++;
133 }
134 }
135}
136
137/** update row activities after a variable's solution value changed */
138static
140 SCIP* scip, /**< SCIP data structure */
141 SCIP_Real* activities, /**< LP row activities */
142 SCIP_ROW** violrows, /**< array with currently violated rows */
143 int* violrowpos, /**< position of LP rows in violrows array */
144 int* nviolrows, /**< pointer to the number of currently violated rows */
145 int nlprows, /**< number of rows in current LP */
146 SCIP_VAR* var, /**< variable that has been changed */
147 SCIP_Real oldsolval, /**< old solution value of variable */
148 SCIP_Real newsolval /**< new solution value of variable */
149 )
150{
151 SCIP_COL* col;
152 SCIP_ROW** colrows;
153 SCIP_Real* colvals;
154 SCIP_Real delta;
155 int ncolrows;
156 int r;
157
158 assert(activities != NULL);
159 assert(nviolrows != NULL);
160 assert(0 <= *nviolrows && *nviolrows <= nlprows);
161
162 delta = newsolval - oldsolval;
163 col = SCIPvarGetCol(var);
164 colrows = SCIPcolGetRows(col);
165 colvals = SCIPcolGetVals(col);
166 ncolrows = SCIPcolGetNLPNonz(col);
167 assert(ncolrows == 0 || (colrows != NULL && colvals != NULL));
168
169 for( r = 0; r < ncolrows; ++r )
170 {
171 SCIP_ROW* row;
172 int rowpos;
173
174 row = colrows[r];
175 rowpos = SCIProwGetLPPos(row);
176 assert(-1 <= rowpos && rowpos < nlprows);
177
178 if( rowpos >= 0 && !SCIProwIsLocal(row) )
179 {
180 SCIP_Real oldactivity;
181
182 assert(SCIProwIsInLP(row));
183
184 /* update row activity */
185 oldactivity = activities[rowpos];
186 if( !SCIPisInfinity(scip, -oldactivity) && !SCIPisInfinity(scip, oldactivity) )
187 {
188 SCIP_Real newactivity;
189
190 newactivity = oldactivity + delta * colvals[r];
191 if( SCIPisInfinity(scip, newactivity) )
192 newactivity = SCIPinfinity(scip);
193 else if( SCIPisInfinity(scip, -newactivity) )
194 newactivity = -SCIPinfinity(scip);
195 activities[rowpos] = newactivity;
196
197 /* update row violation arrays */
198 updateViolations(scip, row, violrows, violrowpos, nviolrows, oldactivity, newactivity);
199 }
200 }
201 }
202
203 return SCIP_OKAY;
204}
205
206/** returns a variable, that pushes activity of the row in the given direction with minimal negative impact on other rows;
207 * if variables have equal impact, chooses the one with best objective value improvement in corresponding direction;
208 * rounding in a direction is forbidden, if this forces the objective value over the upper bound
209 */
210static
212 SCIP* scip, /**< SCIP data structure */
213 SCIP_SOL* sol, /**< primal solution */
214 SCIP_Real minobj, /**< minimal objective value possible after rounding remaining fractional vars */
215 SCIP_ROW* row, /**< LP row */
216 int direction, /**< should the activity be increased (+1) or decreased (-1)? */
217 SCIP_VAR** roundvar, /**< pointer to store the rounding variable, returns NULL if impossible */
218 SCIP_Real* oldsolval, /**< pointer to store old (fractional) solution value of rounding variable */
219 SCIP_Real* newsolval /**< pointer to store new (rounded) solution value of rounding variable */
220 )
221{
222 SCIP_Real bestdeltaobj;
223 SCIP_COL** rowcols;
224 SCIP_Real* rowvals;
225 int nrowcols;
226 int minnlocks;
227 int c;
228
229 assert(direction == +1 || direction == -1);
230 assert(roundvar != NULL);
231 assert(oldsolval != NULL);
232 assert(newsolval != NULL);
233
234 /* get row entries */
235 rowcols = SCIProwGetCols(row);
236 rowvals = SCIProwGetVals(row);
237 nrowcols = SCIProwGetNLPNonz(row);
238
239 /* select rounding variable */
240 minnlocks = INT_MAX;
241 bestdeltaobj = SCIPinfinity(scip);
242 *roundvar = NULL;
243 for( c = 0; c < nrowcols; ++c )
244 {
245 SCIP_COL* col;
246 SCIP_VAR* var;
247 SCIP_VARTYPE vartype;
248
249 col = rowcols[c];
250 var = SCIPcolGetVar(col);
251
252 vartype = SCIPvarGetType(var);
253 if( vartype == SCIP_VARTYPE_BINARY || vartype == SCIP_VARTYPE_INTEGER )
254 {
255 SCIP_Real solval;
256
257 solval = SCIPgetSolVal(scip, sol, var);
258
259 if( !SCIPisFeasIntegral(scip, solval) )
260 {
261 SCIP_Real val;
262 SCIP_Real roundval;
263 SCIP_Real obj;
264 SCIP_Real deltaobj;
265 int nlocks;
266
267 val = rowvals[c];
268 obj = SCIPvarGetObj(var);
269
270 if( direction * val < 0.0 )
271 {
272 /* rounding down */
273 nlocks = SCIPvarGetNLocksDown(var);
274 if( nlocks <= minnlocks )
275 {
276 roundval = SCIPfeasFloor(scip, solval);
277 deltaobj = obj * (roundval - solval);
278 if( (nlocks < minnlocks || deltaobj < bestdeltaobj) && minobj - obj < SCIPgetCutoffbound(scip) )
279 {
280 minnlocks = nlocks;
281 bestdeltaobj = deltaobj;
282 *roundvar = var;
283 *oldsolval = solval;
284 *newsolval = roundval;
285 }
286 }
287 }
288 else
289 {
290 /* rounding up */
291 assert(direction * val > 0.0);
292 nlocks = SCIPvarGetNLocksUp(var);
293 if( nlocks <= minnlocks )
294 {
295 roundval = SCIPfeasCeil(scip, solval);
296 deltaobj = obj * (roundval - solval);
297 if( (nlocks < minnlocks || deltaobj < bestdeltaobj) && minobj + obj < SCIPgetCutoffbound(scip) )
298 {
299 minnlocks = nlocks;
300 bestdeltaobj = deltaobj;
301 *roundvar = var;
302 *oldsolval = solval;
303 *newsolval = roundval;
304 }
305 }
306 }
307 }
308 }
309 }
310
311 return SCIP_OKAY;
312}
313
314/** returns a variable, that increases the activity of the row */
315static
317 SCIP* scip, /**< SCIP data structure */
318 SCIP_SOL* sol, /**< primal solution */
319 SCIP_Real minobj, /**< minimal objective value possible after rounding remaining fractional vars */
320 SCIP_ROW* row, /**< LP row */
321 SCIP_VAR** roundvar, /**< pointer to store the rounding variable, returns NULL if impossible */
322 SCIP_Real* oldsolval, /**< old (fractional) solution value of rounding variable */
323 SCIP_Real* newsolval /**< new (rounded) solution value of rounding variable */
324 )
325{
326 return selectRounding(scip, sol, minobj, row, +1, roundvar, oldsolval, newsolval);
327}
328
329/** returns a variable, that decreases the activity of the row */
330static
332 SCIP* scip, /**< SCIP data structure */
333 SCIP_SOL* sol, /**< primal solution */
334 SCIP_Real minobj, /**< minimal objective value possible after rounding remaining fractional vars */
335 SCIP_ROW* row, /**< LP row */
336 SCIP_VAR** roundvar, /**< pointer to store the rounding variable, returns NULL if impossible */
337 SCIP_Real* oldsolval, /**< old (fractional) solution value of rounding variable */
338 SCIP_Real* newsolval /**< new (rounded) solution value of rounding variable */
339 )
340{
341 return selectRounding(scip, sol, minobj, row, -1, roundvar, oldsolval, newsolval);
342}
343
344/** returns a fractional variable, that has most impact on rows in opposite direction, i.e. that is most crucial to
345 * fix in the other direction;
346 * if variables have equal impact, chooses the one with best objective value improvement in corresponding direction;
347 * rounding in a direction is forbidden, if this forces the objective value over the upper bound
348 */
349static
351 SCIP* scip, /**< SCIP data structure */
352 SCIP_SOL* sol, /**< primal solution */
353 SCIP_Real minobj, /**< minimal objective value possible after rounding remaining fractional vars */
354 SCIP_VAR** lpcands, /**< fractional variables in LP */
355 int nlpcands, /**< number of fractional variables in LP */
356 SCIP_VAR** roundvar, /**< pointer to store the rounding variable, returns NULL if impossible */
357 SCIP_Real* oldsolval, /**< old (fractional) solution value of rounding variable */
358 SCIP_Real* newsolval /**< new (rounded) solution value of rounding variable */
359 )
360{
361 SCIP_Real bestdeltaobj;
362 int maxnlocks;
363 int v;
364
365 assert(roundvar != NULL);
366 assert(oldsolval != NULL);
367 assert(newsolval != NULL);
368
369 /* select rounding variable */
370 maxnlocks = -1;
371 bestdeltaobj = SCIPinfinity(scip);
372 *roundvar = NULL;
373 for( v = 0; v < nlpcands; ++v )
374 {
375 SCIP_VAR* var;
376 SCIP_Real solval;
377
378 var = lpcands[v];
380
381 solval = SCIPgetSolVal(scip, sol, var);
382 if( !SCIPisFeasIntegral(scip, solval) )
383 {
384 SCIP_Real roundval;
385 SCIP_Real obj;
386 SCIP_Real deltaobj;
387 int nlocks;
388
389 obj = SCIPvarGetObj(var);
390
391 /* rounding down */
392 nlocks = SCIPvarGetNLocksUp(var);
393 if( nlocks >= maxnlocks )
394 {
395 roundval = SCIPfeasFloor(scip, solval);
396 deltaobj = obj * (roundval - solval);
397 if( (nlocks > maxnlocks || deltaobj < bestdeltaobj) && minobj - obj < SCIPgetCutoffbound(scip) )
398 {
399 maxnlocks = nlocks;
400 bestdeltaobj = deltaobj;
401 *roundvar = var;
402 *oldsolval = solval;
403 *newsolval = roundval;
404 }
405 }
406
407 /* rounding up */
408 nlocks = SCIPvarGetNLocksDown(var);
409 if( nlocks >= maxnlocks )
410 {
411 roundval = SCIPfeasCeil(scip, solval);
412 deltaobj = obj * (roundval - solval);
413 if( (nlocks > maxnlocks || deltaobj < bestdeltaobj) && minobj + obj < SCIPgetCutoffbound(scip) )
414 {
415 maxnlocks = nlocks;
416 bestdeltaobj = deltaobj;
417 *roundvar = var;
418 *oldsolval = solval;
419 *newsolval = roundval;
420 }
421 }
422 }
423 }
424
425 return SCIP_OKAY;
426}
427
428
429
430
431/*
432 * Callback methods
433 */
434
435/** copy method for primal heuristic plugins (called when SCIP copies plugins) */
436#define heurCopyGcgrounding NULL
437
438/** destructor of primal heuristic to free user data (called when SCIP is exiting) */
439static
440SCIP_DECL_HEURFREE(heurFreeGcgrounding) /*lint --e{715}*/
441{ /*lint --e{715}*/
442 SCIP_HEURDATA* heurdata;
443
444 assert(heur != NULL);
445 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
446 assert(scip != NULL);
447
448 /* free heuristic data */
449 heurdata = SCIPheurGetData(heur);
450 assert(heurdata != NULL);
451 SCIPfreeMemory(scip, &heurdata);
452 SCIPheurSetData(heur, NULL);
453
454 return SCIP_OKAY;
455}
456
457
458
459/** initialization method of primal heuristic (called after problem was transformed) */
460static
461SCIP_DECL_HEURINIT(heurInitGcgrounding) /*lint --e{715}*/
462{ /*lint --e{715}*/
463 SCIP_HEURDATA* heurdata;
464
465 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
466
467 heurdata = SCIPheurGetData(heur);
468 assert(heurdata != NULL);
469
470 /* create heuristic data */
471 SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
472 heurdata->lastlp = -1;
473
474 return SCIP_OKAY;
475}
476
477/** deinitialization method of primal heuristic (called before transformed problem is freed) */
478static
479SCIP_DECL_HEUREXIT(heurExitGcgrounding) /*lint --e{715}*/
480{ /*lint --e{715}*/
481 SCIP_HEURDATA* heurdata;
482
483 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
484
485 /* free heuristic data */
486 heurdata = SCIPheurGetData(heur);
487 assert(heurdata != NULL);
488 SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
489
490 return SCIP_OKAY;
491}
492
493/** solving process initialization method of primal heuristic (called when branch and bound process is about to begin) */
494static
495SCIP_DECL_HEURINITSOL(heurInitsolGcgrounding)
496{
497 SCIP_HEURDATA* heurdata;
498
499 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
500
501 heurdata = SCIPheurGetData(heur);
502 assert(heurdata != NULL);
503 heurdata->lastlp = -1;
504
505 return SCIP_OKAY;
506}
507
508
509/** solving process deinitialization method of primal heuristic (called before branch and bound process data is freed) */
510#define heurExitsolGcgrounding NULL
511
512
513/** execution method of primal heuristic */
514static
515SCIP_DECL_HEUREXEC(heurExecGcgrounding) /*lint --e{715}*/
516{ /*lint --e{715}*/
517 SCIP* masterprob;
518 SCIP_HEURDATA* heurdata;
519 SCIP_SOL* sol;
520 SCIP_VAR** lpcands;
521 SCIP_Real* lpcandssol;
522 SCIP_ROW** lprows;
523 SCIP_Real* activities;
524 SCIP_ROW** violrows;
525 int* violrowpos;
526 SCIP_Real obj;
527 SCIP_Real minobj;
528 int nlpcands;
529 int nlprows;
530 int nfrac;
531 int nviolrows;
532 int c;
533 int r;
534 SCIP_Longint nlps;
535 SCIP_Longint ncalls;
536 SCIP_Longint nsolsfound;
538
539 assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
540 assert(scip != NULL);
541 assert(result != NULL);
542
543 /* get heuristic data */
544 heurdata = SCIPheurGetData(heur);
545 assert(heurdata != NULL);
546
547 /* get master problem */
548 masterprob = GCGgetMasterprob(heurdata->gcg);
549 assert(masterprob != NULL);
550
551 *result = SCIP_DIDNOTRUN;
552
553 /* do not execute the heuristic on invalid relaxation solutions
554 * (which is the case if the node has been cut off)
555 */
557 {
558 SCIPdebugMessage("skipping GCG rounding: invalid relaxation solution\n");
559 return SCIP_OKAY;
560 }
561
562 /* only call heuristic, if an optimal LP solution is at hand */
563 if( SCIPgetStage(masterprob) > SCIP_STAGE_SOLVING || SCIPgetLPSolstat(masterprob) != SCIP_LPSOLSTAT_OPTIMAL )
564 return SCIP_OKAY;
565
566 /* don't call heuristic, if we have already processed the current LP solution */
567 nlps = SCIPgetNLPs(masterprob);
568 if( nlps == heurdata->lastlp )
569 return SCIP_OKAY;
570 heurdata->lastlp = nlps;
571
572 /* don't call heuristic, if it was not successful enough in the past */
573 ncalls = SCIPheurGetNCalls(heur);
574 nsolsfound = 10*SCIPheurGetNBestSolsFound(heur) + SCIPheurGetNSolsFound(heur);
576 if( nnodes % ((ncalls/heurdata->successfactor)/(nsolsfound+1)+1) != 0 )
577 return SCIP_OKAY;
578
579 /* get fractional variables, that should be integral */
580 SCIP_CALL( SCIPgetExternBranchCands(scip, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL) );
581 nfrac = nlpcands;
582
583 /* only call heuristic, if LP solution is fractional */
584 if( nfrac == 0 )
585 return SCIP_OKAY;
586
587 *result = SCIP_DIDNOTFIND;
588
589 /* get LP rows */
590 SCIP_CALL( SCIPgetLPRowsData(scip, &lprows, &nlprows) );
591
592 SCIPdebugMessage("executing GCG rounding heuristic: %d LP rows, %d fractionals\n", nlprows, nfrac);
593
594 /* get memory for activities, violated rows, and row violation positions */
595 SCIP_CALL( SCIPallocBufferArray(scip, &activities, nlprows) );
596 SCIP_CALL( SCIPallocBufferArray(scip, &violrows, nlprows) );
597 SCIP_CALL( SCIPallocBufferArray(scip, &violrowpos, nlprows) );
598
599 /* get the activities for all globally valid rows;
600 * the rows should be feasible, but due to numerical inaccuracies in the LP solver, they can be violated
601 */
602 nviolrows = 0;
603 for( r = 0; r < nlprows; ++r )
604 {
605 SCIP_ROW* row;
606
607 row = lprows[r];
608 assert(SCIProwGetLPPos(row) == r);
609
610 if( !SCIProwIsLocal(row) )
611 {
612 activities[r] = SCIPgetRowSolActivity(scip, row, GCGrelaxGetCurrentOrigSol(heurdata->gcg));
613 if( SCIPisFeasLT(scip, activities[r], SCIProwGetLhs(row) )
614 || SCIPisFeasGT(scip, activities[r], SCIProwGetRhs(row)) )
615 {
616 violrows[nviolrows] = row;
617 violrowpos[r] = nviolrows;
618 nviolrows++;
619 }
620 else
621 violrowpos[r] = -1;
622 }
623 }
624
625 /* get the working solution from heuristic's local data */
626 sol = heurdata->sol;
627 assert(sol != NULL);
628
629 /* copy the current LP solution to the working solution */
631
632 /* calculate the minimal objective value possible after rounding fractional variables */
633 minobj = SCIPgetSolTransObj(scip, sol);
634 /* since the heuristic timing was changed to AFTERNODE, it might happen that it is called on a
635 * node with has been cut off; in that case, delay the heuristic
636 */
637 if( minobj >= SCIPgetCutoffbound(scip) )
638 {
639 *result = SCIP_DELAYED;
640 SCIPfreeBufferArray(scip, &violrowpos);
641 SCIPfreeBufferArray(scip, &violrows);
642 SCIPfreeBufferArray(scip, &activities);
643 return SCIP_OKAY;
644 }
645 for( c = 0; c < nlpcands; ++c )
646 {
647 SCIP_Real bestroundval;
648
649 obj = SCIPvarGetObj(lpcands[c]);
650 bestroundval = obj > 0.0 ? SCIPfeasFloor(scip, lpcandssol[c]) : SCIPfeasCeil(scip, lpcandssol[c]);
651 minobj += obj * (bestroundval - lpcandssol[c]);
652 }
653
654 /* try to round remaining variables in order to become/stay feasible */
655 while( nfrac > 0 )
656 {
657 SCIP_VAR* roundvar;
658 SCIP_Real oldsolval;
659 SCIP_Real newsolval;
660
661 SCIPdebugMessage("GCG rounding heuristic: nfrac=%d, nviolrows=%d, obj=%g (best possible obj: %g)\n",
662 nfrac, nviolrows, SCIPgetSolOrigObj(scip, sol), SCIPretransformObj(scip, minobj));
663
664 /* minobj < SCIPgetCutoffbound(scip) should be true, otherwise the rounding variable selection
665 * should have returned NULL. Due to possible cancellation we use SCIPisLE. */
666 assert( SCIPisLE(scip, minobj, SCIPgetCutoffbound(scip)) );
667
668 /* choose next variable to process:
669 * - if a violated row exists, round a variable decreasing the violation, that has least impact on other rows
670 * - otherwise, round a variable, that has strongest devastating impact on rows in opposite direction
671 */
672 if( nviolrows > 0 )
673 {
674 SCIP_ROW* row;
675 int rowpos;
676
677 row = violrows[nviolrows-1];
678 rowpos = SCIProwGetLPPos(row);
679 assert(0 <= rowpos && rowpos < nlprows);
680 assert(violrowpos[rowpos] == nviolrows-1);
681
682 SCIPdebugMessage("GCG rounding heuristic: try to fix violated row <%s>: %g <= %g <= %g\n",
683 SCIProwGetName(row), SCIProwGetLhs(row), activities[rowpos], SCIProwGetRhs(row));
684 if( SCIPisFeasLT(scip, activities[rowpos], SCIProwGetLhs(row)) )
685 {
686 /* lhs is violated: select a variable rounding, that increases the activity */
687 SCIP_CALL( selectIncreaseRounding(scip, sol, minobj, row, &roundvar, &oldsolval, &newsolval) );
688 }
689 else
690 {
691 assert(SCIPisFeasGT(scip, activities[rowpos], SCIProwGetRhs(row)));
692 /* rhs is violated: select a variable rounding, that decreases the activity */
693 SCIP_CALL( selectDecreaseRounding(scip, sol, minobj, row, &roundvar, &oldsolval, &newsolval) );
694 }
695 }
696 else
697 {
698 SCIPdebugMessage("GCG rounding heuristic: search rounding variable and try to stay feasible\n");
699 SCIP_CALL( selectEssentialRounding(scip, sol, minobj, lpcands, nlpcands, &roundvar, &oldsolval, &newsolval) );
700 }
701
702 /* check, whether rounding was possible */
703 if( roundvar == NULL )
704 {
705 SCIPdebugMessage("GCG rounding heuristic: -> didn't find a rounding variable\n");
706 break;
707 }
708
709 SCIPdebugMessage("GCG rounding heuristic: -> round var <%s>, oldval=%g, newval=%g, obj=%g\n",
710 SCIPvarGetName(roundvar), oldsolval, newsolval, SCIPvarGetObj(roundvar));
711
712 /* update row activities of globally valid rows */
713 SCIP_CALL( updateActivities(scip, activities, violrows, violrowpos, &nviolrows, nlprows,
714 roundvar, oldsolval, newsolval) );
715
716 /* store new solution value and decrease fractionality counter */
717 SCIP_CALL( SCIPsetSolVal(scip, sol, roundvar, newsolval) );
718 nfrac--;
719
720 /* update minimal objective value possible after rounding remaining variables */
721 obj = SCIPvarGetObj(roundvar);
722 if( obj > 0.0 && newsolval > oldsolval )
723 minobj += obj;
724 else if( obj < 0.0 && newsolval < oldsolval )
725 minobj -= obj;
726
727 SCIPdebugMessage("GCG rounding heuristic: -> nfrac=%d, nviolrows=%d, obj=%g (best possible obj: %g)\n",
728 nfrac, nviolrows, SCIPgetSolOrigObj(scip, sol), SCIPretransformObj(scip, minobj));
729 }
730
731 /* check, if the new solution is feasible */
732 if( nfrac == 0 && nviolrows == 0 )
733 {
734 SCIP_Bool stored;
735
736 /* check solution for feasibility, and add it to solution store if possible
737 * neither integrality nor feasibility of LP rows has to be checked, because this is already
738 * done in the rounding heuristic itself; however, be better check feasibility of LP rows,
739 * because of numerical problems with activity updating
740 */
741 SCIP_CALL( SCIPtrySol(scip, sol, FALSE, FALSE, FALSE, FALSE, TRUE, &stored) );
742
743 if( stored )
744 {
745#ifdef SCIP_DEBUG
746 SCIPdebugMessage("found feasible rounded solution:\n");
747 SCIPprintSol(scip, sol, NULL, FALSE);
748#endif
749 *result = SCIP_FOUNDSOL;
750 }
751 }
752
753 /* free memory buffers */
754 SCIPfreeBufferArray(scip, &violrowpos);
755 SCIPfreeBufferArray(scip, &violrows);
756 SCIPfreeBufferArray(scip, &activities);
757
758 return SCIP_OKAY;
759}
760
761
762
763
764/*
765 * heuristic specific interface methods
766 */
767
768/** creates the GCG rounding heuristic with infeasibility recovering and includes it in SCIP */
770 GCG* gcg /**< GCG data structure */
771 )
772{
773 SCIP_HEURDATA* heurdata;
774 SCIP* origprob = GCGgetOrigprob(gcg);
775
776 /* create heuristic data */
777 SCIP_CALL( SCIPallocMemory(origprob, &heurdata) );
778 heurdata->gcg = gcg;
779
780 /* include heuristic */
784 heurFreeGcgrounding, heurInitGcgrounding, heurExitGcgrounding,
785 heurInitsolGcgrounding, heurExitsolGcgrounding, heurExecGcgrounding,
786 heurdata) );
787
788 /* add rounding primal heuristic parameters */
789 SCIP_CALL( SCIPaddIntParam(origprob, "heuristics/"HEUR_NAME"/successfactor",
790 "number of calls per found solution that are considered as standard success, a higher factor causes the heuristic to be called more often",
791 &heurdata->successfactor, TRUE, DEFAULT_SUCCESSFACTOR, -1, INT_MAX, NULL, NULL) );
792
793 return SCIP_OKAY;
794}
795
SCIP_Real * r
#define SCIP_Longint
#define SCIP_Bool
#define SCIP_Real
#define SCIP_CALL(x)
#define TRUE
#define FALSE
#define nnodes
GCG interface methods.
SCIP * GCGgetOrigprob(GCG *gcg)
Definition: gcg.cpp:87
SCIP * GCGgetMasterprob(GCG *gcg)
Definition: gcg.cpp:99
SCIP_STAGE SCIPgetStage(SCIP *scip)
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 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)
#define SCIPfreeBufferArray(scip, ptr)
#define SCIPfreeMemory(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)
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_Real SCIPfeasCeil(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisLE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
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 SCIPisFeasGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_COL * SCIPvarGetCol(SCIP_VAR *var)
int SCIPvarGetNLocksDown(SCIP_VAR *var)
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
const char * SCIPvarGetName(SCIP_VAR *var)
SCIP_Bool SCIPisRelaxSolValid(SCIP *scip)
int SCIPvarGetNLocksUp(SCIP_VAR *var)
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)
#define DEFAULT_SUCCESSFACTOR
static SCIP_DECL_HEURINIT(heurInitGcgrounding)
#define HEUR_TIMING
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)
#define HEUR_FREQOFS
#define HEUR_DESC
#define HEUR_DISPCHAR
#define HEUR_MAXDEPTH
#define heurExitsolGcgrounding
#define HEUR_PRIORITY
static SCIP_DECL_HEURINITSOL(heurInitsolGcgrounding)
static SCIP_RETCODE selectIncreaseRounding(SCIP *scip, SCIP_SOL *sol, SCIP_Real minobj, SCIP_ROW *row, SCIP_VAR **roundvar, SCIP_Real *oldsolval, SCIP_Real *newsolval)
static SCIP_DECL_HEUREXEC(heurExecGcgrounding)
#define HEUR_NAME
static SCIP_RETCODE selectDecreaseRounding(SCIP *scip, SCIP_SOL *sol, SCIP_Real minobj, SCIP_ROW *row, SCIP_VAR **roundvar, SCIP_Real *oldsolval, SCIP_Real *newsolval)
static SCIP_RETCODE selectRounding(SCIP *scip, SCIP_SOL *sol, SCIP_Real minobj, SCIP_ROW *row, int direction, SCIP_VAR **roundvar, SCIP_Real *oldsolval, SCIP_Real *newsolval)
static SCIP_DECL_HEUREXIT(heurExitGcgrounding)
#define heurCopyGcgrounding
#define HEUR_FREQ
static SCIP_DECL_HEURFREE(heurFreeGcgrounding)
#define HEUR_USESSUBSCIP
SCIP_RETCODE GCGincludeHeurGcgrounding(GCG *gcg)
LP gcgrounding heuristic that tries to recover from intermediate infeasibilities.
#define NULL
#define SCIPdebugMessage
SCIP_SOL * GCGrelaxGetCurrentOrigSol(GCG *gcg)
Definition: relax_gcg.c:4309
GCG relaxator.
Definition: struct_gcg.h:55
SCIP_Longint lastlp
SCIP_SOL * sol
SCIP_LPSOLSTAT_OPTIMAL
SCIP_DIDNOTRUN
SCIP_DELAYED
SCIP_DIDNOTFIND
SCIP_FOUNDSOL
SCIP_OKAY
enum SCIP_Retcode SCIP_RETCODE
SCIP_STAGE_SOLVING
SCIP_VARTYPE_INTEGER
SCIP_VARTYPE_BINARY
enum SCIP_Vartype SCIP_VARTYPE