GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
heur_gcgcoefdiving.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_gcgcoefdiving.c
30 * @brief LP diving heuristic that chooses fixings w.r.t. the matrix coefficients
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 "gcgcoefdiving"
46#define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the matrix coefficients"
47#define HEUR_DISPCHAR 'c'
48#define HEUR_PRIORITY -1001000
49#define HEUR_FREQ 10
50#define HEUR_FREQOFS 1
51#define HEUR_MAXDEPTH -1
52
53
54/*
55 * Default diving rule specific parameter settings
56 */
57
58#define DEFAULT_USEMASTERLOCKS FALSE /**< calculate the number of locks w.r.t. the master LP? */
59
60
61/* locally defined diving heuristic data */
63{
64 SCIP_Bool usemasterlocks; /**< calculate the number of locks w.r.t. the master LP? */
65};
66
67
68/*
69 * local methods
70 */
71
72/** check whether an original variable and a master variable belong to the same block */
73static
75 SCIP_VAR* origvar, /**< original variable */
76 SCIP_VAR* mastervar /**< master variable */
77 )
78{
79 int origblock;
80 int masterblock;
81
82 /* get the blocks the variables belong to */
83 origblock = GCGvarGetBlock(origvar);
84 masterblock = GCGvarGetBlock(mastervar);
85
86 /* the original variable is a linking variable:
87 * check whether the master variable is either its direct copy
88 * or in one of its blocks
89 */
90 if( GCGoriginalVarIsLinking(origvar) )
91 {
92 assert(origblock == -2);
93 if( masterblock == -1 )
94 {
96
98
99 return mastervars[0] == mastervar;
100 }
101 else
102 {
103 assert(masterblock >= 0);
104 return GCGisLinkingVarInBlock(origvar, masterblock);
105 }
106 }
107 /* the original variable was directly copied to the master problem:
108 * check whether the master variable is its copy
109 */
110 else if( origblock == -1 )
111 {
113
116
117 return mastervars[0] == mastervar;
118 }
119 /* the original variable belongs to exactly one block */
120 else
121 {
122 assert(origblock >= 0);
123 return origblock == masterblock;
124 }
125}
126
127/** get the number of down-locks for an original variable w.r.t. the master problem */
128static
130 GCG* gcg, /**< GCG data structure */
131 SCIP_VAR* var, /**< original variable to get locks for */
132 int* nlocksdown /**< pointer to store number of down-locks */
133 )
134{
137 SCIP_VAR** origmastervars;
138 SCIP_Real* origmastervals;
139 int nmastervars;
140 int norigmastervars;
141 SCIP_Real roundval;
142 int i;
144
145 /* get master problem */
148
149 /* get master variable information */
151
152 /* get master variables in which the original variable appears */
153 origmastervars = GCGoriginalVarGetMastervars(var);
154 origmastervals = GCGoriginalVarGetMastervals(var);
155 norigmastervars = GCGoriginalVarGetNMastervars(var);
156
158 *nlocksdown = 0;
159
160 /* calculate locks = the sum of down-locks of all master variables
161 * which would have to be fixed to zero if the original variable were rounded down
162 */
163 if( SCIPisFeasNegative(masterprob, roundval) )
164 {
165 for( i = 0; i < nmastervars; ++i )
166 {
169 *nlocksdown += SCIPvarGetNLocksDown(mastervars[i]);
170 }
171 for( i = 0; i < norigmastervars; ++i )
172 if( !SCIPisFeasZero(masterprob, SCIPgetSolVal(masterprob, NULL, origmastervars[i]) )
173 && SCIPisFeasLE(masterprob, origmastervals[i], roundval) )
174 *nlocksdown -= SCIPvarGetNLocksDown(origmastervars[i]);
175 }
176 else
177 {
178 for( i = 0; i < norigmastervars; ++i )
179 if( !SCIPisFeasZero(masterprob, SCIPgetSolVal(masterprob, NULL, origmastervars[i]) )
180 && SCIPisFeasGT(masterprob, origmastervals[i], roundval) )
181 *nlocksdown += SCIPvarGetNLocksDown(origmastervars[i]);
182 }
183 assert(*nlocksdown >= 0);
184
185 return SCIP_OKAY;
186}
187
188/** get the number of up-locks for an original variable w.r.t. the master problem */
189static
191 GCG* gcg, /**< GCG data structure */
192 SCIP_VAR* var, /**< original variable to get locks for */
193 int* nlocksup /**< pointer to store number of up-locks */
194 )
195{
198 SCIP_VAR** origmastervars;
199 SCIP_Real* origmastervals;
200 int nmastervars;
201 int norigmastervars;
202 SCIP_Real roundval;
203 int i;
205
206 /* get master problem */
209
210 /* get master variable information */
212
213 /* get master variables in which the original variable appears */
214 origmastervars = GCGoriginalVarGetMastervars(var);
215 origmastervals = GCGoriginalVarGetMastervals(var);
216 norigmastervars = GCGoriginalVarGetNMastervars(var);
217
219 *nlocksup = 0;
220
221 /* calculate locks = the sum of down-locks of all master variables
222 * which would have to be fixed to zero if the original variable were rounded up
223 */
224 if( SCIPisFeasPositive(masterprob, roundval) )
225 {
226 for( i = 0; i < nmastervars; ++i )
227 {
230 *nlocksup += SCIPvarGetNLocksDown(mastervars[i]);
231 }
232 for( i = 0; i < norigmastervars; ++i )
233 if( !SCIPisFeasZero(masterprob, SCIPgetSolVal(masterprob, NULL, origmastervars[i]) )
234 && SCIPisFeasGE(masterprob, origmastervals[i], roundval) )
235 *nlocksup -= SCIPvarGetNLocksDown(origmastervars[i]);
236 }
237 else
238 {
239 for( i = 0; i < norigmastervars; ++i )
240 if( !SCIPisFeasZero(masterprob, SCIPgetSolVal(masterprob, NULL, origmastervars[i]) )
241 && SCIPisFeasLT(masterprob, origmastervals[i], roundval) )
242 *nlocksup += SCIPvarGetNLocksDown(origmastervars[i]);
243 }
244 assert(*nlocksup >= 0);
245
246 return SCIP_OKAY;
247}
248
249
250/*
251 * Callback methods
252 */
253
254/** destructor of diving heuristic to free user data (called when GCG is exiting) */
255static
256GCG_DECL_DIVINGFREE(heurFreeGcgcoefdiving) /*lint --e{715}*/
257{ /*lint --e{715}*/
259
260 assert(heur != NULL);
261
262 /* free diving rule specific data */
267
268 return SCIP_OKAY;
269}
270
271
272/** variable selection method of diving heuristic;
273 * finds best candidate variable w.r.t. locking numbers:
274 * - prefer variables that may not be rounded without destroying LP feasibility:
275 * - of these variables, round variable with least number of locks in corresponding direction
276 * - if all remaining fractional variables may be rounded without destroying LP feasibility:
277 * - round variable with least number of locks in opposite of its feasible rounding direction
278 * - binary variables are preferred
279 */
280static
281GCG_DECL_DIVINGSELECTVAR(heurSelectVarGcgcoefdiving) /*lint --e{715}*/
282{ /*lint --e{715}*/
289 int bestnviolrows; /* number of violated rows for best candidate */
290 SCIP_Real bestcandfrac; /* fractionality of best candidate */
291 int c;
293
294 /* check preconditions */
296 assert(heur != NULL);
297 assert(bestcand != NULL);
299 assert(bestcandroundup != NULL);
300
301 /* get diving data */
304
305 /* get fractional variables that should be integral */
309
312 bestnviolrows = INT_MAX;
314
315 /* get best candidate */
316 for( c = 0; c < nlpcands; ++c )
317 {
318 SCIP_VAR* var;
319
320 int nlocksdown;
321 int nlocksup;
322 int nviolrows;
323
324 int i;
325
326 SCIP_Bool mayrounddown;
327 SCIP_Bool mayroundup;
328 SCIP_Bool roundup;
330
331 var = lpcands[c];
333
334 /* if the variable is on the tabu list, do not choose it */
335 for( i = 0; i < tabulistsize; ++i )
336 if( tabulist[i] == var )
337 break;
338 if( i < tabulistsize )
339 continue;
340
341 if( divingdata->usemasterlocks )
342 {
343 SCIP_CALL( getNLocksDown(gcg, var, &nlocksdown) );
344 SCIP_CALL( getNLocksUp(gcg, var, &nlocksup) );
345 mayrounddown = nlocksdown == 0;
346 mayroundup = nlocksup == 0;
347 }
348 else
349 {
350 nlocksdown = SCIPvarGetNLocksDown(var);
351 nlocksup = SCIPvarGetNLocksUp(var);
352 mayrounddown = SCIPvarMayRoundDown(var);
353 mayroundup = SCIPvarMayRoundUp(var);
354 }
355
356 if( mayrounddown || mayroundup )
357 {
358 /* the candidate may be rounded: choose this candidate only, if the best candidate may also be rounded */
360 {
361 /* choose rounding direction:
362 * - if variable may be rounded in both directions, round corresponding to the fractionality
363 * - otherwise, round in the infeasible direction, because feasible direction is tried by rounding
364 * the current fractional solution
365 */
366 if( mayrounddown && mayroundup )
367 roundup = (frac > 0.5);
368 else
369 roundup = mayrounddown;
370
371 if( roundup )
372 {
373 frac = 1.0 - frac;
374 nviolrows = nlocksup;
375 }
376 else
377 nviolrows = nlocksdown;
378
379 /* penalize too small fractions */
380 if( frac < 0.01 )
381 nviolrows *= 100;
382
383 /* prefer decisions on binary variables */
384 if( !SCIPvarIsBinary(var) )
385 nviolrows *= 100;
386
387 /* check, if candidate is new best candidate */
388 assert(0.0 < frac && frac < 1.0);
390 {
391 *bestcand = var;
394 bestcandmayrounddown = mayrounddown;
395 bestcandmayroundup = mayroundup;
396 *bestcandroundup = roundup;
397 }
398 }
399 }
400 else
401 {
402 /* the candidate may not be rounded */
403 roundup = (nlocksdown > nlocksup || (nlocksdown == nlocksup && frac > 0.5));
404 if( roundup )
405 {
406 nviolrows = nlocksup;
407 frac = 1.0 - frac;
408 }
409 else
410 nviolrows = nlocksdown;
411
412 /* penalize too small fractions */
413 if( frac < 0.01 )
414 nviolrows *= 100;
415
416 /* prefer decisions on binary variables */
417 if( !SCIPvarIsBinary(var) )
418 nviolrows *= 100;
419
420 /* check, if candidate is new best candidate: prefer unroundable candidates in any case */
421 assert(0.0 < frac && frac < 1.0);
423 {
424 *bestcand = var;
429 *bestcandroundup = roundup;
430 }
432 }
433 }
434
436
437 return SCIP_OKAY;
438}
439
440
441/*
442 * heuristic specific interface methods
443 */
444
445/** creates the gcgcoefdiving heuristic and includes it in GCG */
447 GCG* gcg /**< GCG data structure */
448 )
449{
450 SCIP_HEUR* heur;
453
454 /* create gcgcoefdiving primal heuristic data */
456
457 /* include diving heuristic */
460 HEUR_MAXDEPTH, heurFreeGcgcoefdiving, NULL, NULL, NULL, NULL, NULL, NULL,
461 heurSelectVarGcgcoefdiving, divingdata) );
462
463 assert(heur != NULL);
464
465 /* add gcgcoefdiving specific parameters */
466 SCIP_CALL( SCIPaddBoolParam(origprob, "heuristics/"HEUR_NAME"/usemasterlocks",
467 "calculate the number of locks w.r.t. the master LP?",
468 &divingdata->usemasterlocks, TRUE, DEFAULT_USEMASTERLOCKS, NULL, NULL) );
469
470 return SCIP_OKAY;
471}
472
#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_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
SCIP_Bool SCIPisFeasGE(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Real SCIPfeasCeil(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPisFeasZero(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPfloor(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 SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
int SCIPvarGetNLocksDown(SCIP_VAR *var)
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
SCIP_Real SCIPgetRelaxSolVal(SCIP *scip, SCIP_VAR *var)
int SCIPvarGetNLocksUp(SCIP_VAR *var)
SCIPfreeMemory(GCGgetOrigprob(gcg), &divingdata)
* bestcandmayround
SCIP_RETCODE GCGincludeHeurGcgcoefdiving(GCG *gcg)
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 DEFAULT_USEMASTERLOCKS
#define HEUR_NAME
static SCIP_VAR ** lpcands
int bestnviolrows
static SCIP_RETCODE getNLocksUp(GCG *gcg, SCIP_VAR *var, int *nlocksup)
int nlpcands
static SCIP_RETCODE getNLocksDown(GCG *gcg, SCIP_VAR *var, int *nlocksdown)
SCIP * origprob
#define HEUR_FREQ
static SCIP_Bool areVarsInSameBlock(SCIP_VAR *origvar, SCIP_VAR *mastervar)
SCIP_Real bestcandfrac
LP diving heuristic that chooses fixings w.r.t. the matrix coefficients.
SCIP_VAR * var
SCIP_Real frac
int i
int nviolrows
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_DIVINGSELECTVAR(x)
struct GCG_DivingData GCG_DIVINGDATA
enum SCIP_Retcode SCIP_RETCODE
struct Scip SCIP
struct SCIP_Var SCIP_VAR