Scippy

SCIP

Solving Constraint Integer Programs

heur_pscostdiving.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2017 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file heur_pscostdiving.c
17  * @brief LP diving heuristic that chooses fixings w.r.t. the pseudo cost values
18  * @author Tobias Achterberg
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include <assert.h>
24 #include <string.h>
25 
26 #include "scip/heur_pscostdiving.h"
27 
28 #define HEUR_NAME "pscostdiving"
29 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the pseudo cost values"
30 #define HEUR_DISPCHAR 'p'
31 #define HEUR_PRIORITY -1002000
32 #define HEUR_FREQ 10
33 #define HEUR_FREQOFS 2
34 #define HEUR_MAXDEPTH -1
35 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
36 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
37 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY /**< bit mask that represents all supported dive types */
38 
39 
40 /*
41  * Default parameter settings
42  */
43 
44 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
45 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
46 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
47 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
48 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
49  * where diving is performed (0.0: no limit) */
50 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
51  * where diving is performed (0.0: no limit) */
52 #define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
53 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
54 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
55 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
56 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
57 #define DEFAULT_ONLYLPBRANCHCANDS TRUE /**< should only LP branching candidates be considered instead of the slower but
58  * more general constraint handler diving variable selection? */
59 #define DEFAULT_RANDSEED 103 /**< initial random seed */
60 
61 
62 /* locally defined heuristic data */
63 struct SCIP_HeurData
64 {
65  SCIP_SOL* sol; /**< working solution */
66 };
67 
68 /*
69  * local methods
70  */
71 
72 /*
73  * Callback methods
74  */
75 
76 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
77 static
78 SCIP_DECL_HEURCOPY(heurCopyPscostdiving)
79 { /*lint --e{715}*/
80  assert(scip != NULL);
81  assert(heur != NULL);
82  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
83 
84  /* call inclusion method of primal heuristic */
86 
87  return SCIP_OKAY;
88 }
89 
90 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
91 static
92 SCIP_DECL_HEURFREE(heurFreePscostdiving) /*lint --e{715}*/
93 { /*lint --e{715}*/
94  SCIP_HEURDATA* heurdata;
95 
96  assert(heur != NULL);
97  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
98  assert(scip != NULL);
99 
100  /* free heuristic data */
101  heurdata = SCIPheurGetData(heur);
102  assert(heurdata != NULL);
103  SCIPfreeBlockMemory(scip, &heurdata);
104  SCIPheurSetData(heur, NULL);
105 
106  return SCIP_OKAY;
107 }
108 
109 
110 /** initialization method of primal heuristic (called after problem was transformed) */
111 static
112 SCIP_DECL_HEURINIT(heurInitPscostdiving) /*lint --e{715}*/
113 { /*lint --e{715}*/
114  SCIP_HEURDATA* heurdata;
116  assert(heur != NULL);
117  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
118 
119  /* get heuristic data */
120  heurdata = SCIPheurGetData(heur);
121  assert(heurdata != NULL);
122 
123  /* create working solution */
124  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
125 
126  return SCIP_OKAY;
127 }
128 
129 
130 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
131 static
132 SCIP_DECL_HEUREXIT(heurExitPscostdiving) /*lint --e{715}*/
133 { /*lint --e{715}*/
134  SCIP_HEURDATA* heurdata;
136  assert(heur != NULL);
137  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
138 
139  /* get heuristic data */
140  heurdata = SCIPheurGetData(heur);
141  assert(heurdata != NULL);
142 
143 
144  /* free working solution */
145  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
146 
147  return SCIP_OKAY;
148 }
149 
150 /** execution method of primal heuristic */
151 static
152 SCIP_DECL_HEUREXEC(heurExecPscostdiving) /*lint --e{715}*/
153 { /*lint --e{715}*/
154  SCIP_HEURDATA* heurdata;
155  SCIP_DIVESET* diveset;
156 
157  heurdata = SCIPheurGetData(heur);
158  assert(heurdata != NULL);
159  assert(SCIPheurGetNDivesets(heur) > 0);
160  assert(SCIPheurGetDivesets(heur) != NULL);
161  diveset = SCIPheurGetDivesets(heur)[0];
162  assert(diveset != NULL);
163 
164  *result = SCIP_DIDNOTRUN;
165 
166  /* terminate if there are no integer variables (note that, e.g., SOS1 variables may be present) */
168  return SCIP_OKAY;
169 
170  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible) );
171 
172  return SCIP_OKAY;
173 }
174 
175 
176 /** returns a score for the given candidate -- the best candidate maximizes the diving score */
177 static
178 SCIP_DECL_DIVESETGETSCORE(divesetGetScorePscostdiving)
179 {
180  SCIP_Real pscostdown;
181  SCIP_Real pscostup;
182  SCIP_Real pscostquot;
183 
184  SCIP_Bool mayrounddown;
185  SCIP_Bool mayroundup;
186 
187  mayrounddown = SCIPvarMayRoundDown(cand);
188  mayroundup = SCIPvarMayRoundUp(cand);
189 
190 
191  /* bound fractions to not prefer variables that are nearly integral */
192  candsfrac = MAX(candsfrac, 0.1);
193  candsfrac = MIN(candsfrac, 0.9);
194 
195  pscostdown = SCIPgetVarPseudocostVal(scip, cand, 0.0 - candsfrac);
196  pscostup = SCIPgetVarPseudocostVal(scip, cand, 1.0 - candsfrac);
197 
198  /* determine the candidate direction. if the variable may be trivially rounded in one direction, take the other direction;
199  * otherwise, consider first the direction from the root solution, second the candidate fractionality, and
200  * last the direction of smaller pseudo costs
201  *
202  * to avoid performance variability caused by numerics we use random numbers to decide whether we want to roundup or
203  * round down if the values to compare are equal within tolerances.
204  */
205  assert(pscostdown >= 0.0 && pscostup >= 0.0);
206  if( mayrounddown != mayroundup )
207  *roundup = mayrounddown;
208  else if( SCIPisLT(scip, candsol, SCIPvarGetRootSol(cand) - 0.4)
209  || (SCIPisEQ(scip, candsol, SCIPvarGetRootSol(cand) - 0.4) && SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0) )
210  *roundup = FALSE;
211  else if( SCIPisGT(scip, candsol, SCIPvarGetRootSol(cand) + 0.4)
212  || (SCIPisEQ(scip, candsol, SCIPvarGetRootSol(cand) + 0.4) && SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0) )
213  *roundup = TRUE;
214  else if( SCIPisLT(scip, candsfrac, 0.3)
215  || (SCIPisEQ(scip, candsfrac, 0.3) && SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0) )
216  *roundup = FALSE;
217  else if( SCIPisGT(scip, candsfrac, 0.7)
218  || (SCIPisEQ(scip, candsfrac, 0.7) && SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0) )
219  *roundup = TRUE;
220  else if( SCIPisEQ(scip, pscostdown, pscostup) )
221  *roundup = (SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0);
222  else if( pscostdown > pscostup )
223  *roundup = TRUE;
224  else
225  *roundup = FALSE;
226 
227  if( *roundup )
228  pscostquot = sqrt(candsfrac) * (1.0 + pscostdown) / (1.0 + pscostup);
229  else
230  pscostquot = sqrt(1.0 - candsfrac) * (1.0 + pscostup) / (1.0 + pscostdown);
231 
232  /* prefer decisions on binary variables */
233  if( SCIPvarIsBinary(cand) && !(SCIPvarMayRoundDown(cand) || SCIPvarMayRoundUp(cand)))
234  pscostquot *= 1000.0;
235 
236  assert(pscostquot >= 0);
237  *score = pscostquot;
238 
239  return SCIP_OKAY;
240 }
241 
242 /*
243  * heuristic specific interface methods
244  */
245 
246 /** creates the pscostdiving heuristic and includes it in SCIP */
248  SCIP* scip /**< SCIP data structure */
249  )
250 {
251  SCIP_HEURDATA* heurdata;
252  SCIP_HEUR* heur;
253 
254  /* create Pscostdiving primal heuristic data */
255  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
256 
257  /* include primal heuristic */
258  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
260  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecPscostdiving, heurdata) );
261 
262  assert(heur != NULL);
263 
264  /* set non-NULL pointers to callback methods */
265  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyPscostdiving) );
266  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreePscostdiving) );
267  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitPscostdiving) );
268  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitPscostdiving) );
269 
270  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
274 
275  return SCIP_OKAY;
276 }
277 
#define HEUR_PRIORITY
int SCIPgetNIntVars(SCIP *scip)
Definition: scip.c:11896
#define DIVESET_DIVETYPES
#define HEUR_MAXDEPTH
SCIP_Real SCIPgetVarPseudocostVal(SCIP *scip, SCIP_VAR *var, SCIP_Real solvaldelta)
Definition: scip.c:26025
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip.c:8177
#define DEFAULT_MAXDIVEUBQUOTNOSOL
SCIP_DIVESET ** SCIPheurGetDivesets(SCIP_HEUR *heur)
Definition: heur.c:1396
#define DEFAULT_ONLYLPBRANCHCANDS
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:16842
SCIP_Real SCIPvarGetRootSol(SCIP_VAR *var)
Definition: var.c:12758
#define FALSE
Definition: def.h:64
#define TRUE
Definition: def.h:63
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip.h:22602
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, SCIP_HEURTIMING timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition: scip.c:8084
int SCIPrandomGetInt(SCIP_RANDNUMGEN *randnumgen, int minrandval, int maxrandval)
Definition: misc.c:9366
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:46957
LP diving heuristic that chooses fixings w.r.t. the pseudo cost values.
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1119
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip.h:22585
#define HEUR_USESSUBSCIP
#define DEFAULT_LPSOLVEFREQ
#define HEUR_FREQ
static SCIP_DECL_HEURFREE(heurFreePscostdiving)
#define HEUR_DESC
#define DEFAULT_MAXLPITEROFS
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1198
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:8145
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:46970
#define DEFAULT_RANDSEED
SCIPInterval sqrt(const SCIPInterval &x)
#define HEUR_DISPCHAR
SCIP_SOL * sol
Definition: struct_heur.h:41
#define HEUR_FREQOFS
static SCIP_DECL_HEUREXIT(heurExitPscostdiving)
#define SCIP_CALL(x)
Definition: def.h:350
#define HEUR_NAME
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1406
#define DEFAULT_BACKTRACK
#define SCIP_Bool
Definition: def.h:61
#define HEUR_TIMING
SCIP_RANDNUMGEN * SCIPdivesetGetRandnumgen(SCIP_DIVESET *diveset)
Definition: heur.c:571
static SCIP_DECL_DIVESETGETSCORE(divesetGetScorePscostdiving)
#define MAX(x, y)
Definition: tclique_def.h:75
SCIP_RETCODE SCIPcreateDiveset(SCIP *scip, SCIP_DIVESET **diveset, SCIP_HEUR *heur, const char *name, SCIP_Real minreldepth, SCIP_Real maxreldepth, SCIP_Real maxlpiterquot, SCIP_Real maxdiveubquot, SCIP_Real maxdiveavgquot, SCIP_Real maxdiveubquotnosol, SCIP_Real maxdiveavgquotnosol, SCIP_Real lpresolvedomchgquot, int lpsolvefreq, int maxlpiterofs, unsigned int initialseed, SCIP_Bool backtrack, SCIP_Bool onlylpbranchcands, SCIP_Bool specificsos1score, SCIP_DECL_DIVESETGETSCORE((*divesetgetscore)))
Definition: scip.c:8521
#define DEFAULT_MAXDIVEAVGQUOT
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:38529
#define DEFAULT_MAXRELDEPTH
#define DEFAULT_MAXDIVEUBQUOT
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:11851
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible)
Definition: heuristics.c:192
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
#define DEFAULT_MINRELDEPTH
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:46996
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip.c:8161
SCIP_RETCODE SCIPincludeHeurPscostdiving(SCIP *scip)
#define SCIP_Real
Definition: def.h:149
static SCIP_DECL_HEURINIT(heurInitPscostdiving)
static SCIP_DECL_HEUREXEC(heurExecPscostdiving)
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:8129
#define DEFAULT_LPRESOLVEDOMCHGQUOT
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1109
#define DEFAULT_MAXLPITERQUOT
static SCIP_DECL_HEURCOPY(heurCopyPscostdiving)
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3280
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3272
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:37872