GCG 4.1.1 (dev)

Generic Column Generation

Loading...
Searching...
No Matches
heur_gcgveclendiving.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_gcgveclendiving.c
30 * @brief LP diving heuristic that rounds variables with long column vectors
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 "gcgveclendiving"
46#define HEUR_DESC "LP diving heuristic that rounds variables with long column vectors"
47#define HEUR_DISPCHAR 'v'
48#define HEUR_PRIORITY -1003100
49#define HEUR_FREQ 10
50#define HEUR_FREQOFS 4
51#define HEUR_MAXDEPTH -1
52
53
54/*
55 * Default diving rule specific parameter settings
56 */
57
58#define DEFAULT_USEMASTERSCORES FALSE /**< calculate vector length scores w.r.t. the master LP? */
59
60
61/* locally defined diving heuristic data */
62struct GCG_DivingData
63{
64 SCIP_Bool usemasterscores; /**< calculate vector length scores w.r.t. the master LP? */
65 SCIP_Real* masterscores; /**< vector length based scores for the master variables */
66};
67
68
69/*
70 * local methods
71 */
72
73/** for a variable, calculate the vector length score w.r.t. the original problem */
74static
76 SCIP* scip, /**< SCIP data structure */
77 SCIP_VAR* var, /**< variable to calculate score for */
78 SCIP_Real frac, /**< the variable's fractionality in the current LP solution */
79 SCIP_Real* score, /**< pointer to return the score */
80 SCIP_Bool* roundup /**< pointer to return whether the variable is rounded up */
81 )
82{
84 SCIP_Real objdelta;
85 int colveclen;
86
88 *roundup = (obj >= 0.0);
89 objdelta = (*roundup ? (1.0-frac)*obj : -frac * obj);
90 assert(objdelta >= 0.0);
91
93
94 /* smaller score is better */
95 *score = (objdelta + SCIPsumepsilon(scip))/((SCIP_Real)colveclen+1.0);
96
97 /* prefer decisions on binary variables */
99 *score *= 1000.0;
100
101 return SCIP_OKAY;
102}
103
104/** check whether an original variable and a master variable belong to the same block */
105static
107 SCIP_VAR* origvar, /**< original variable */
108 SCIP_VAR* mastervar /**< master variable */
109 )
110{
111 int origblock;
112 int masterblock;
113
114 /* get the blocks the variables belong to */
115 origblock = GCGvarGetBlock(origvar);
116 masterblock = GCGvarGetBlock(mastervar);
117
118 /* the original variable is a linking variable:
119 * check whether the master variable is either its direct copy
120 * or in one of its blocks
121 */
122 if( GCGoriginalVarIsLinking(origvar) )
123 {
124 assert(origblock == -2);
125 if( masterblock == -1 )
126 {
128
130
131 return mastervars[0] == mastervar;
132 }
133 else
134 {
135 assert(masterblock >= 0);
136 return GCGisLinkingVarInBlock(origvar, masterblock);
137 }
138 }
139 /* the original variable was directly copied to the master problem:
140 * check whether the master variable is its copy
141 */
142 else if( origblock == -1 )
143 {
145
148
149 return mastervars[0] == mastervar;
150 }
151 /* the original variable belongs to exactly one block */
152 else
153 {
154 assert(origblock >= 0);
155 return origblock == masterblock;
156 }
157}
158
159/** get the 'down' score of an original variable w.r.t. the master problem;
160 * this is the sum of the vector length scores of the master variables
161 * which would have to be fixed to zero if the original variable were rounded down
162 */
163static
165 GCG* gcg, /**< GCG data structure */
166 GCG_DIVINGDATA* divingdata, /**< diving heuristic data */
167 SCIP_VAR* var, /**< original variable to get fractionality for */
168 SCIP_Real* score /**< pointer to store fractionality */
169 )
170{
173 SCIP_Real* origmastervals;
174 int nmastervars;
175 int norigmastervars;
176 SCIP_Real roundval;
177 int i;
179
180 /* get master problem */
183
184 /* get master variable information */
186
187 /* get master variables in which the original variable appears */
188 origmastervals = GCGoriginalVarGetMastervals(var);
189 norigmastervars = GCGoriginalVarGetNMastervars(var);
190
192 *score = 0.0;
193
194 /* calculate sum of scores over all master variables
195 * which would violate the new original variable bound
196 */
197 if( SCIPisFeasNegative(masterprob, roundval) )
198 {
199 for( i = 0; i < nmastervars; ++i )
201 *score += divingdata->masterscores[i];
202 for( i = 0; i < norigmastervars; ++i )
203 if( SCIPisFeasLE(masterprob, origmastervals[i], roundval) )
204 *score -= divingdata->masterscores[i];
205 }
206 else
207 {
208 for( i = 0; i < norigmastervars; ++i )
209 if( SCIPisFeasGT(masterprob, origmastervals[i], roundval) )
210 *score += divingdata->masterscores[i];
211 }
212
213 return SCIP_OKAY;
214}
215
216/** get the 'up' score of an original variable w.r.t. the master problem;
217 * this is the sum of the scores of the master variables
218 * which would have to be fixed to zero if the original variable were rounded up
219 */
220static
222 GCG* gcg, /**< GCG data structure */
223 GCG_DIVINGDATA* divingdata, /**< diving heuristic data */
224 SCIP_VAR* var, /**< original variable to get fractionality for */
225 SCIP_Real* score /**< pointer to store fractionality */
226 )
227{
230 SCIP_Real* origmastervals;
231 int nmastervars;
232 int norigmastervars;
233 SCIP_Real roundval;
234 int i;
236
237 /* get master problem */
240
241 /* get master variable information */
243
244 /* get master variables in which the original variable appears */
245 origmastervals = GCGoriginalVarGetMastervals(var);
246 norigmastervars = GCGoriginalVarGetNMastervars(var);
247
249 *score = 0.0;
250
251 /* calculate sum of scores over all master variables
252 * which would violate the new original variable bound
253 */
254 if( SCIPisFeasPositive(masterprob, roundval) )
255 {
256 for( i = 0; i < nmastervars; ++i )
258 *score += divingdata->masterscores[i];
259 for( i = 0; i < norigmastervars; ++i )
260 if( SCIPisFeasGE(masterprob, origmastervals[i], roundval) )
261 *score -= divingdata->masterscores[i];
262 }
263 else
264 {
265 for( i = 0; i < norigmastervars; ++i )
266 if( SCIPisFeasLT(masterprob, origmastervals[i], roundval) )
267 *score += divingdata->masterscores[i];
268 }
269
270 return SCIP_OKAY;
271}
272
273/** for a variable, calculate the vector length score w.r.t. the master problem */
274static
276 GCG* gcg, /**< GCG data structure */
277 GCG_DIVINGDATA* divingdata, /**< diving heuristic data */
278 SCIP_VAR* var, /**< variable to calculate score for */
279 SCIP_Real* score, /**< pointer to return the score */
280 SCIP_Bool* roundup /**< pointer to return whether the variable is rounded up */
281 )
282{
283 SCIP_Real downscore;
284 SCIP_Real upscore;
285
286 /* calculate scores for rounding down or up */
289
290 *score = MIN(downscore, upscore);
291 *roundup = upscore <= downscore;
292
293 /* prefer decisions on binary variables */
295 *score *= 1000.0;
296
297 return SCIP_OKAY;
298}
299
300
301/*
302 * Callback methods
303 */
304
305
306/** destructor of diving heuristic to free user data (called when GCG is exiting) */
307static
308GCG_DECL_DIVINGFREE(heurFreeGcgveclendiving) /*lint --e{715}*/
309{ /*lint --e{715}*/
311
312 assert(heur != NULL);
313
314 /* free diving rule specific data */
319
320 return SCIP_OKAY;
321}
322
323
324/** execution initialization method of diving heuristic (called when execution of diving heuristic is about to begin) */
325static
326GCG_DECL_DIVINGINITEXEC(heurInitexecGcgveclendiving) /*lint --e{715}*/
327{ /*lint --e{715}*/
333 int i;
335
336 assert(heur != NULL);
337
338 /* get diving data */
341
342 /* do not collect vector length scores on master variables if not used */
343 if( !divingdata->usemasterscores )
344 return SCIP_OKAY;
345
346 /* get master problem */
349
350 /* get master variables */
352
353 /* allocate memory */
356
357 /* get master LP solution */
359
360 /* for each master variable, calculate a score */
361 for( i = 0; i < nmastervars; ++i )
362 {
363 SCIP_VAR* mastervar;
364 SCIP_Real objdelta;
365 int colveclen;
366
367 mastervar = mastervars[i];
368 objdelta = SCIPfeasFrac(masterprob, SCIPgetSolVal(masterprob, masterlpsol, mastervar)) * SCIPvarGetObj(mastervar);
369 objdelta = ABS(objdelta);
370 colveclen = (SCIPvarGetStatus(mastervar) == SCIP_VARSTATUS_COLUMN ? SCIPcolGetNNonz(SCIPvarGetCol(mastervar)) : 0);
371 divingdata->masterscores[i] = (objdelta + SCIPsumepsilon(origprob))/((SCIP_Real)colveclen+1.0);
372 }
373
374 /* free memory */
376
377 return SCIP_OKAY;
378}
379
380
381/** execution deinitialization method of diving heuristic (called when execution data is freed) */
382static
383GCG_DECL_DIVINGEXITEXEC(heurExitexecGcgveclendiving) /*lint --e{715}*/
384{ /*lint --e{715}*/
387
388 assert(heur != NULL);
389
390 /* get diving data */
393
394 /* memory needs to to be freed if vector length scores on master variables were not used */
395 if( !divingdata->usemasterscores )
396 return SCIP_OKAY;
397
398 /* free memory */
400
401 return SCIP_OKAY;
402}
403
404
405/** variable selection method of diving heuristic;
406 * finds best candidate variable w.r.t. vector length:
407 * - round variables in direction where objective value gets worse; for zero objective coefficient, round upwards
408 * - round variable with least objective value deficit per row the variable appears in
409 * (we want to "fix" as many rows as possible with the least damage to the objective function)
410 */
411static
412GCG_DECL_DIVINGSELECTVAR(heurSelectVarGcgveclendiving) /*lint --e{715}*/
413{ /*lint --e{715}*/
419 int c;
421
422 /* check preconditions */
424 assert(heur != NULL);
425 assert(bestcand != NULL);
427 assert(bestcandroundup != NULL);
428
429 /* get diving data */
432
433 /* get fractional variables that should be integral */
437
439
440 /* get best candidate */
441 for( c = 0; c < nlpcands; ++c )
442 {
443 SCIP_Real score;
444 SCIP_Bool roundup;
445
446 int i;
447
448 /* if the variable is on the tabu list, do not choose it */
449 for( i = 0; i < tabulistsize; ++i )
450 if( tabulist[i] == lpcands[c] )
451 break;
452 if( i < tabulistsize )
453 continue;
454
455 /* calculate score */
456 if( divingdata->usemasterscores )
457 {
458 SCIP_CALL( calculateScoreMaster(gcg, divingdata, lpcands[c], &score, &roundup) );
459 }
460 else
461 {
463 }
464
465 /* check whether the variable is roundable */
467
468 /* check, if candidate is new best candidate */
469 if( score < bestscore )
470 {
471 *bestcand = lpcands[c];
472 bestscore = score;
473 *bestcandroundup = roundup;
474 }
475 }
476
477 return SCIP_OKAY;
478}
479
480
481/*
482 * heuristic specific interface methods
483 */
484
485/** creates the gcgveclendiving heuristic and includes it in GCG */
487 GCG* gcg /**< GCG data structure */
488 )
489{
490 SCIP_HEUR* heur;
493
494 /* create gcgguideddiving primal heuristic data */
496
497 /* include diving heuristic */
500 HEUR_MAXDEPTH, heurFreeGcgveclendiving, NULL, NULL, NULL, NULL, heurInitexecGcgveclendiving,
501 heurExitexecGcgveclendiving, heurSelectVarGcgveclendiving, divingdata) );
502
503 assert(heur != NULL);
504
505 /* add gcgveclendiving specific parameters */
506 SCIP_CALL( SCIPaddBoolParam(origprob, "heuristics/"HEUR_NAME"/usemasterscores",
507 "calculate vector length scores w.r.t. the master LP?",
508 &divingdata->usemasterscores, TRUE, DEFAULT_USEMASTERSCORES, NULL, NULL) );
509
510 return SCIP_OKAY;
511}
512
#define SCIP_REAL_MAX
#define SCIP_Bool
#define MIN(x, y)
#define SCIP_Real
#define ABS(x)
#define TRUE
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)
int SCIPcolGetNNonz(SCIP_COL *col)
#define SCIPallocBufferArray(scip, ptr, num)
#define SCIPallocMemory(scip, ptr)
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
SCIP_RETCODE SCIPlinkLPSol(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 SCIPfeasFrac(SCIP *scip, SCIP_Real val)
SCIP_Real SCIPfeasCeil(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_Real SCIPsumepsilon(SCIP *scip)
SCIP_Bool SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
SCIP_COL * SCIPvarGetCol(SCIP_VAR *var)
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
SCIP_VARTYPE SCIPvarGetType(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
GCGheurSetDivingDataOrig(heur, NULL)
#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
int i
SCIPfreeBufferArray(scip, &cycles)
SCIP_Real bestscore
SCIP_Real obj
#define DEFAULT_USEMASTERSCORES
SCIP_SOL * masterlpsol
SCIP_VAR ** mastervars
static SCIP_RETCODE calculateScoreMaster(GCG *gcg, GCG_DIVINGDATA *divingdata, SCIP_VAR *var, SCIP_Real *score, SCIP_Bool *roundup)
int nmastervars
SCIP_RETCODE GCGincludeHeurGcgveclendiving(GCG *gcg)
static SCIP_RETCODE getMasterDownScore(GCG *gcg, GCG_DIVINGDATA *divingdata, SCIP_VAR *var, SCIP_Real *score)
static SCIP_RETCODE getMasterUpScore(GCG *gcg, GCG_DIVINGDATA *divingdata, SCIP_VAR *var, SCIP_Real *score)
static SCIP_RETCODE calculateScoreOrig(SCIP *scip, SCIP_VAR *var, SCIP_Real frac, SCIP_Real *score, SCIP_Bool *roundup)
LP diving heuristic that rounds variables with long column vectors.
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
SCIP_VARTYPE_BINARY
SCIP_VARSTATUS_COLUMN