GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
heur_gcgguideddiving.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_gcgguideddiving.c
30 * @brief LP diving heuristic that chooses fixings in direction of incumbent solutions
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/heur_origdiving.h"
42#include "gcg/gcg.h"
43
44
45#define HEUR_NAME "gcgguideddiving"
46#define HEUR_DESC "LP diving heuristic that chooses fixings in direction of incumbent solutions"
47#define HEUR_DISPCHAR 'g'
48#define HEUR_PRIORITY -1007000
49#define HEUR_FREQ 10
50#define HEUR_FREQOFS 7
51#define HEUR_MAXDEPTH -1
52
53
54/*
55 * Default diving rule specific parameter settings
56 */
57
58#define DEFAULT_USEMASTERFRACS FALSE /**< calculate the fractionalities w.r.t. the master LP? */
59
60
61/* locally defined diving heuristic data */
62struct GCG_DivingData
63{
64 SCIP_Bool usemasterfracs; /**< calculate the fractionalities w.r.t. the master LP? */
65 SCIP_SOL* bestsol; /**< best known feasible solution before the diving loop */
66};
67
68
69/*
70 * local methods
71 */
72
73/** check whether an original variable and a master variable belong to the same block */
74static
76 SCIP_VAR* origvar, /**< original variable */
77 SCIP_VAR* mastervar /**< master variable */
78 )
79{
80 int origblock;
81 int masterblock;
82
83 /* get the blocks the variables belong to */
84 origblock = GCGvarGetBlock(origvar);
85 masterblock = GCGvarGetBlock(mastervar);
86
87 /* the original variable is a linking variable:
88 * check whether the master variable is either its direct copy
89 * or in one of its blocks
90 */
91 if( GCGoriginalVarIsLinking(origvar) )
92 {
93 assert(origblock == -2);
94 if( masterblock == -1 )
95 {
97
99
100 return mastervars[0] == mastervar;
101 }
102 else
103 {
104 assert(masterblock >= 0);
105 return GCGisLinkingVarInBlock(origvar, masterblock);
106 }
107 }
108 /* the original variable was directly copied to the master problem:
109 * check whether the master variable is its copy
110 */
111 else if( origblock == -1 )
112 {
114
117
118 return mastervars[0] == mastervar;
119 }
120 /* the original variable belongs to exactly one block */
121 else
122 {
123 assert(origblock >= 0);
124 return origblock == masterblock;
125 }
126}
127
128/** get the 'down' fractionality of an original variable w.r.t. the master problem;
129 * this is the sum of the fractionalities of the master variables
130 * which would have to be fixed to zero if the original variable were rounded down
131 */
132static
134 GCG* gcg, /**< GCG data structure */
135 SCIP_VAR* var, /**< original variable to get fractionality for */
136 SCIP_Real* frac /**< pointer to store fractionality */
137 )
138{
141 SCIP_VAR** origmastervars;
142 SCIP_Real* origmastervals;
143 int nmastervars;
144 int norigmastervars;
145 SCIP_Real roundval;
146 SCIP_Real masterlpval;
147 int i;
149
150 /* get master problem */
153
154 /* get master variable information */
156
157 /* get master variables in which the original variable appears */
158 origmastervars = GCGoriginalVarGetMastervars(var);
159 origmastervals = GCGoriginalVarGetMastervals(var);
160 norigmastervars = GCGoriginalVarGetNMastervars(var);
161
163 *frac = 0.0;
164
165 /* calculate sum of fractionalities over all master variables
166 * which would violate the new original variable bound
167 */
168 if( SCIPisFeasNegative(masterprob, roundval) )
169 {
170 for( i = 0; i < nmastervars; ++i )
171 {
173 {
174 masterlpval = SCIPgetSolVal(masterprob, NULL, mastervars[i]);
175 *frac += SCIPfeasFrac(masterprob, masterlpval);
176 }
177 }
178 for( i = 0; i < norigmastervars; ++i )
179 {
180 masterlpval = SCIPgetSolVal(masterprob, NULL, origmastervars[i]);
181 if( SCIPisFeasLE(masterprob, origmastervals[i], roundval) )
182 *frac -= SCIPfeasFrac(masterprob, masterlpval);
183 }
184 }
185 else
186 {
187 for( i = 0; i < norigmastervars; ++i )
188 {
189 masterlpval = SCIPgetSolVal(masterprob, NULL, mastervars[i]);
190 if( SCIPisFeasGT(masterprob, origmastervals[i], roundval) )
191 *frac += SCIPfeasFrac(masterprob, masterlpval);
192 }
193 }
194
195 return SCIP_OKAY;
196}
197
198/** get the 'up' fractionality of an original variable w.r.t. the master problem;
199 * this is the sum of the fractionalities of the master variables
200 * which would have to be fixed to zero if the original variable were rounded up
201 */
202static
204 GCG* gcg, /**< GCG data structure */
205 SCIP_VAR* var, /**< original variable to get fractionality for */
206 SCIP_Real* frac /**< pointer to store fractionality */
207 )
208{
211 SCIP_VAR** origmastervars;
212 SCIP_Real* origmastervals;
213 int nmastervars;
214 int norigmastervars;
215 SCIP_Real roundval;
216 SCIP_Real masterlpval;
217 int i;
219
220 /* get master problem */
223
224 /* get master variable information */
226
227 /* get master variables in which the original variable appears */
228 origmastervars = GCGoriginalVarGetMastervars(var);
229 origmastervals = GCGoriginalVarGetMastervals(var);
230 norigmastervars = GCGoriginalVarGetNMastervars(var);
231
233 *frac = 0.0;
234
235 /* calculate sum of fractionalities over all master variables
236 * which would violate the new original variable bound
237 */
238 if( SCIPisFeasPositive(masterprob, roundval) )
239 {
240 for( i = 0; i < nmastervars; ++i )
241 {
243 {
244 masterlpval = SCIPgetSolVal(masterprob, NULL, mastervars[i]);
245 *frac += SCIPfeasFrac(masterprob, masterlpval);
246 }
247 }
248 for( i = 0; i < norigmastervars; ++i )
249 {
250 masterlpval = SCIPgetSolVal(masterprob, NULL, origmastervars[i]);
251 if( SCIPisFeasGE(masterprob, origmastervals[i], roundval) )
252 *frac -= SCIPfeasFrac(masterprob, masterlpval);
253 }
254 }
255 else
256 {
257 for( i = 0; i < norigmastervars; ++i )
258 {
259 masterlpval = SCIPgetSolVal(masterprob, NULL, mastervars[i]);
260 if( SCIPisFeasLT(masterprob, origmastervals[i], roundval) )
261 *frac += SCIPfeasFrac(masterprob, masterlpval);
262 }
263 }
264
265 return SCIP_OKAY;
266}
267
268
269/*
270 * Callback methods
271 */
272
273
274/** destructor of diving heuristic to free user data (called when GCG is exiting) */
275static
276GCG_DECL_DIVINGFREE(heurFreeGcgguideddiving) /*lint --e{715}*/
277{ /*lint --e{715}*/
279
280 assert(heur != NULL);
281
282 /* free diving rule specific data */
287
288 return SCIP_OKAY;
289}
290
291
292/** execution initialization method of diving heuristic (called when execution of diving heuristic is about to begin) */
293static
294GCG_DECL_DIVINGINITEXEC(heurInitexecGcgguideddiving) /*lint --e{715}*/
295{ /*lint --e{715}*/
298
299 assert(heur != NULL);
300
301 /* get diving data */
304
305 /* store a copy of the best solution */
307
308 return SCIP_OKAY;
309}
310
311
312/** execution deinitialization method of diving heuristic (called when execution data is freed) */
313static
314GCG_DECL_DIVINGEXITEXEC(heurExitexecGcgguideddiving) /*lint --e{715}*/
315{ /*lint --e{715}*/
318
319 assert(heur != NULL);
320
321 /* get diving data */
324
325 /* free copied best solution */
327
328 return SCIP_OKAY;
329}
330
331
332/** variable selection method of diving heuristic;
333 * finds best candidate variable w.r.t. the incumbent solution:
334 * - prefer variables that may not be rounded without destroying LP feasibility:
335 * - of these variables, round a variable to its value in direction of incumbent solution, and choose the
336 * variable that is closest to its rounded value
337 * - if all remaining fractional variables may be rounded without destroying LP feasibility:
338 * - round variable in direction that destroys LP feasibility (other direction is checked by SCIProundSol())
339 * - round variable with least increasing objective value
340 * - binary variables are prefered
341 * - variables in a minimal cover or variables that are also fractional in an optimal LP solution might
342 * also be prefered if a corresponding parameter is set
343 */
344static
345GCG_DECL_DIVINGSELECTVAR(heurSelectVarGcgguideddiving) /*lint --e{715}*/
346{ /*lint --e{715}*/
353 SCIP_Real bestscore; /* fractionality of best candidate */
356 int c;
358
359 /* check preconditions */
361 assert(heur != NULL);
362 assert(bestcand != NULL);
364 assert(bestcandroundup != NULL);
365
366 /* get diving data */
369 assert(divingdata->bestsol != NULL);
370
371 /* get fractional variables that should be integral */
376
381
382 /* get best candidate */
383 for( c = 0; c < nlpcands; ++c )
384 {
385 SCIP_VAR* var;
386 SCIP_Bool mayrounddown;
387 SCIP_Bool mayroundup;
388 SCIP_Bool roundup;
391 SCIP_Real score;
393 SCIP_Real bestsolval;
394
395 int i;
396
397 var = lpcands[c];
398
399 /* if the variable is on the tabu list, do not choose it */
400 for( i = 0; i < tabulistsize; ++i )
401 if( tabulist[i] == var )
402 break;
403 if( i < tabulistsize )
404 continue;
405
406 mayrounddown = SCIPvarMayRoundDown(var);
407 mayroundup = SCIPvarMayRoundUp(var);
409 frac = lpcandsfrac[c];
411 bestsolval = SCIPgetSolVal(origprob, divingdata->bestsol, var);
412
413 /* select default rounding direction */
414 roundup = (solval < bestsolval);
415
416 if( mayrounddown || mayroundup )
417 {
418 /* the candidate may be rounded: choose this candidate only, if the best candidate may also be rounded */
420 {
421 SCIP_Real objgain;
422
423 /* choose rounding direction:
424 * - if variable may be rounded in both directions, round corresponding to its value in incumbent solution
425 * - otherwise, round in the infeasible direction, because feasible direction is tried by rounding
426 * the current fractional solution with SCIProundSol()
427 */
428 if( !mayrounddown || !mayroundup )
429 roundup = mayrounddown;
430
431 if( roundup )
432 {
433 frac = 1.0 - frac;
434 objgain = frac*obj;
435 }
436 else
437 objgain = -frac*obj;
438
439 /* penalize too small fractions */
440 if( frac < 0.01 )
441 objgain *= 1000.0;
442
443 /* prefer decisions on binary variables */
444 if( !SCIPvarIsBinary(var) )
445 objgain *= 1000.0;
446
447 if( divingdata->usemasterfracs )
448 {
449 SCIP_Real downfrac;
450 SCIP_Real upfrac;
451
452 SCIP_CALL( getMasterDownFrac(gcg, var, &downfrac) );
453 SCIP_CALL( getMasterUpFrac(gcg, var, &upfrac) );
454 score = roundup ? upfrac : downfrac;
455 }
456 else
457 score = frac;
458
459 /* check, if candidate is new best candidate */
460 if( SCIPisLT(origprob, objgain, bestobjgain) || (SCIPisEQ(origprob, objgain, bestobjgain) && score < bestscore) )
461 {
462 *bestcand = var;
463 bestobjgain = objgain;
464 bestscore = score;
465 bestcandmayrounddown = mayrounddown;
466 bestcandmayroundup = mayroundup;
467 *bestcandroundup = roundup;
468 }
469 }
470 }
471 else
472 {
473 /* the candidate may not be rounded */
474
475 if( divingdata->usemasterfracs )
476 {
477 SCIP_Real downfrac;
478 SCIP_Real upfrac;
479
480 SCIP_CALL( getMasterDownFrac(gcg, var, &downfrac) );
481 SCIP_CALL( getMasterUpFrac(gcg, var, &upfrac) );
482 score = roundup ? upfrac : downfrac;
483 }
484 else
485 score = roundup? 1.0 - frac : frac;
486
487 /* penalize too small fractions */
488 if( score < 0.01 )
489 score += 10.0;
490
491 /* prefer decisions on binary variables */
492 if( !SCIPvarIsBinary(var) )
493 score *= 1000.0;
494
495 /* check, if candidate is new best candidate: prefer unroundable candidates in any case */
497 {
498 *bestcand = var;
499 bestscore = score;
502 *bestcandroundup = roundup;
503 }
504 }
505 }
506
508
509 return SCIP_OKAY;
510}
511
512
513/*
514 * heuristic specific interface methods
515 */
516
517/** creates the gcgguideddiving heuristic and includes it in GCG */
519 GCG* gcg /**< GCG data structure */
520 )
521{
522 SCIP_HEUR* heur;
525
526 /* create gcgguideddiving primal heuristic data */
528
529 /* include diving heuristic */
532 HEUR_MAXDEPTH, heurFreeGcgguideddiving, NULL, NULL, NULL, NULL, heurInitexecGcgguideddiving,
533 heurExitexecGcgguideddiving, heurSelectVarGcgguideddiving, divingdata) );
534
535 assert(heur != NULL);
536
537 /* add gcgguideddiving specific parameters */
538 SCIP_CALL( SCIPaddBoolParam(origprob, "heuristics/"HEUR_NAME"/usemasterfracs",
539 "calculate the fractionalities w.r.t. the master LP?",
540 &divingdata->usemasterfracs, TRUE, DEFAULT_USEMASTERFRACS, NULL, NULL) );
541
542 return SCIP_OKAY;
543}
544
#define SCIP_INVALID
#define SCIP_Bool
#define SCIP_Real
#define TRUE
#define FALSE
GCG interface methods.
SCIP_VAR ** GCGoriginalVarGetMastervars(SCIP_VAR *var)
Definition gcgvar.c:666
SCIP_Bool GCGoriginalVarIsLinking(SCIP_VAR *var)
Definition gcgvar.c:247
SCIP_Real * GCGoriginalVarGetMastervals(SCIP_VAR *var)
Definition gcgvar.c:684
SCIP_Bool GCGisLinkingVarInBlock(SCIP_VAR *var, int block)
Definition gcgvar.c:1149
int GCGoriginalVarGetNMastervars(SCIP_VAR *var)
Definition gcgvar.c:648
int GCGvarGetBlock(SCIP_VAR *var)
Definition gcgvar.c:1118
SCIP * GCGgetOrigprob(GCG *gcg)
Definition gcg.cpp:87
SCIP * GCGgetMasterprob(GCG *gcg)
Definition gcg.cpp:99
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
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)
#define SCIPallocMemory(scip, ptr)
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
SCIP_RETCODE SCIPcreateSolCopy(SCIP *scip, SCIP_SOL **sol, SCIP_SOL *sourcesol)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
SCIP_Bool SCIPisFeasGE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Real SCIPinfinity(SCIP *scip)
SCIP_Real SCIPfeasFrac(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPfeasCeil(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPfeasFloor(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 SCIPisFeasGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
SCIP_Real SCIPgetRelaxSolVal(SCIP *scip, SCIP_VAR *var)
SCIPfreeMemory(GCGgetOrigprob(gcg), &divingdata)
* bestcandmayround
static assert(heur !=NULL)
SCIP_CALL(SCIPgetExternBranchCands(origprob, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL, NULL, NULL))
#define HEUR_FREQOFS
#define HEUR_DESC
SCIP_Bool bestcandmayroundup
GCGheurSetDivingDataOrig(heur, NULL)
SCIP_Bool bestcandmayrounddown
#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
static SCIP_Bool areVarsInSameBlock(SCIP_VAR *origvar, SCIP_VAR *mastervar)
SCIP_VAR * var
SCIP_Real frac
SCIP_Real solval
int i
SCIP_Real bestobjgain
static SCIP_RETCODE getMasterUpFrac(GCG *gcg, SCIP_VAR *var, SCIP_Real *frac)
#define DEFAULT_USEMASTERFRACS
static SCIP_RETCODE getMasterDownFrac(GCG *gcg, SCIP_VAR *var, SCIP_Real *frac)
SCIP_Real bestscore
SCIP_Real * lpcandsfrac
SCIP_RETCODE GCGincludeHeurGcgguideddiving(GCG *gcg)
LP diving heuristic that chooses fixings in direction of incumbent solutions.
SCIP_Real obj
SCIP_VAR ** mastervars
int nmastervars
SCIP_VAR ** tabulist
SCIP_RETCODE GCGincludeDivingHeurOrig(GCG *gcg, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, GCG_DECL_DIVINGFREE((*divingfree)), GCG_DECL_DIVINGINIT((*divinginit)), GCG_DECL_DIVINGEXIT((*divingexit)), GCG_DECL_DIVINGINITSOL((*divinginitsol)), GCG_DECL_DIVINGEXITSOL((*divingexitsol)), GCG_DECL_DIVINGINITEXEC((*divinginitexec)), GCG_DECL_DIVINGEXITEXEC((*divingexitexec)), GCG_DECL_DIVINGSELECTVAR((*divingselectvar)), GCG_DIVINGDATA *divingdata)
GCG_DIVINGDATA * GCGheurGetDivingDataOrig(SCIP_HEUR *heur)
primal heuristic interface for LP diving heuristics on the original variables
#define NULL
struct Gcg GCG
Definition type_gcg.h:44
struct SCIP_Heur SCIP_HEUR
#define GCG_DECL_DIVINGFREE(x)
#define GCG_DECL_DIVINGINITEXEC(x)
#define GCG_DECL_DIVINGSELECTVAR(x)
#define GCG_DECL_DIVINGEXITEXEC(x)
struct GCG_DivingData GCG_DIVINGDATA
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
struct SCIP_Sol SCIP_SOL
struct SCIP_Var SCIP_VAR