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-2019 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 visit 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 "scip/heuristics.h"
24 #include "scip/heur_pscostdiving.h"
25 #include "scip/pub_heur.h"
26 #include "scip/pub_message.h"
27 #include "scip/pub_misc.h"
28 #include "scip/pub_var.h"
29 #include "scip/scip_heur.h"
30 #include "scip/scip_mem.h"
31 #include "scip/scip_numerics.h"
32 #include "scip/scip_prob.h"
33 #include "scip/scip_sol.h"
34 #include "scip/scip_var.h"
35 #include <string.h>
36 
37 #define HEUR_NAME "pscostdiving"
38 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the pseudo cost values"
39 #define HEUR_DISPCHAR 'p'
40 #define HEUR_PRIORITY -1002000
41 #define HEUR_FREQ 10
42 #define HEUR_FREQOFS 2
43 #define HEUR_MAXDEPTH -1
44 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
45 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
46 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY /**< bit mask that represents all supported dive types */
47 
48 
49 /*
50  * Default parameter settings
51  */
52 
53 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
54 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
55 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
56 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
57 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
58  * where diving is performed (0.0: no limit) */
59 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
60  * where diving is performed (0.0: no limit) */
61 #define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
62 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
63 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
64 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
65 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
66 #define DEFAULT_ONLYLPBRANCHCANDS TRUE /**< should only LP branching candidates be considered instead of the slower but
67  * more general constraint handler diving variable selection? */
68 #define DEFAULT_RANDSEED 103 /**< initial random seed */
69 
70 
71 /* locally defined heuristic data */
72 struct SCIP_HeurData
73 {
74  SCIP_SOL* sol; /**< working solution */
75 };
76 
77 /*
78  * local methods
79  */
80 
81 /*
82  * Callback methods
83  */
84 
85 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
86 static
87 SCIP_DECL_HEURCOPY(heurCopyPscostdiving)
88 { /*lint --e{715}*/
89  assert(scip != NULL);
90  assert(heur != NULL);
91  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
92 
93  /* call inclusion method of primal heuristic */
95 
96  return SCIP_OKAY;
97 }
98 
99 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
100 static
101 SCIP_DECL_HEURFREE(heurFreePscostdiving) /*lint --e{715}*/
102 { /*lint --e{715}*/
103  SCIP_HEURDATA* heurdata;
105  assert(heur != NULL);
106  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
107  assert(scip != NULL);
108 
109  /* free heuristic data */
110  heurdata = SCIPheurGetData(heur);
111  assert(heurdata != NULL);
112  SCIPfreeBlockMemory(scip, &heurdata);
113  SCIPheurSetData(heur, NULL);
114 
115  return SCIP_OKAY;
116 }
117 
118 
119 /** initialization method of primal heuristic (called after problem was transformed) */
120 static
121 SCIP_DECL_HEURINIT(heurInitPscostdiving) /*lint --e{715}*/
122 { /*lint --e{715}*/
123  SCIP_HEURDATA* heurdata;
125  assert(heur != NULL);
126  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
127 
128  /* get heuristic data */
129  heurdata = SCIPheurGetData(heur);
130  assert(heurdata != NULL);
131 
132  /* create working solution */
133  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
134 
135  return SCIP_OKAY;
136 }
137 
138 
139 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
140 static
141 SCIP_DECL_HEUREXIT(heurExitPscostdiving) /*lint --e{715}*/
142 { /*lint --e{715}*/
143  SCIP_HEURDATA* heurdata;
145  assert(heur != NULL);
146  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
147 
148  /* get heuristic data */
149  heurdata = SCIPheurGetData(heur);
150  assert(heurdata != NULL);
151 
152  /* free working solution */
153  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
154 
155  return SCIP_OKAY;
156 }
157 
158 /** execution method of primal heuristic */
159 static
160 SCIP_DECL_HEUREXEC(heurExecPscostdiving) /*lint --e{715}*/
161 { /*lint --e{715}*/
162  SCIP_HEURDATA* heurdata;
163  SCIP_DIVESET* diveset;
164 
165  heurdata = SCIPheurGetData(heur);
166  assert(heurdata != NULL);
167  assert(SCIPheurGetNDivesets(heur) > 0);
168  assert(SCIPheurGetDivesets(heur) != NULL);
169  diveset = SCIPheurGetDivesets(heur)[0];
170  assert(diveset != NULL);
171 
172  *result = SCIP_DIDNOTRUN;
173 
174  /* terminate if there are no integer variables (note that, e.g., SOS1 variables may be present) */
176  return SCIP_OKAY;
177 
178  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible) );
179 
180  return SCIP_OKAY;
181 }
182 
183 
184 /** returns a score for the given candidate -- the best candidate maximizes the diving score */
185 static
186 SCIP_DECL_DIVESETGETSCORE(divesetGetScorePscostdiving)
187 {
188  SCIP_Real pscostdown;
189  SCIP_Real pscostup;
190  SCIP_Real pscostquot;
191 
192  SCIP_Bool mayrounddown;
193  SCIP_Bool mayroundup;
194 
195  mayrounddown = SCIPvarMayRoundDown(cand);
196  mayroundup = SCIPvarMayRoundUp(cand);
197 
198  /* bound fractions to not prefer variables that are nearly integral */
199  candsfrac = MAX(candsfrac, 0.1);
200  candsfrac = MIN(candsfrac, 0.9);
201 
202  pscostdown = SCIPgetVarPseudocostVal(scip, cand, 0.0 - candsfrac);
203  pscostup = SCIPgetVarPseudocostVal(scip, cand, 1.0 - candsfrac);
204 
205  /* determine the candidate direction. if the variable may be trivially rounded in one direction, take the other direction;
206  * otherwise, consider first the direction from the root solution, second the candidate fractionality, and
207  * last the direction of smaller pseudo costs
208  *
209  * to avoid performance variability caused by numerics we use random numbers to decide whether we want to roundup or
210  * round down if the values to compare are equal within tolerances.
211  */
212  assert(pscostdown >= 0.0 && pscostup >= 0.0);
213  if( mayrounddown != mayroundup )
214  *roundup = mayrounddown;
215  else if( SCIPisLT(scip, candsol, SCIPvarGetRootSol(cand) - 0.4)
216  || (SCIPisEQ(scip, candsol, SCIPvarGetRootSol(cand) - 0.4) && SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0) )
217  *roundup = FALSE;
218  else if( SCIPisGT(scip, candsol, SCIPvarGetRootSol(cand) + 0.4)
219  || (SCIPisEQ(scip, candsol, SCIPvarGetRootSol(cand) + 0.4) && SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0) )
220  *roundup = TRUE;
221  else if( SCIPisLT(scip, candsfrac, 0.3)
222  || (SCIPisEQ(scip, candsfrac, 0.3) && SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0) )
223  *roundup = FALSE;
224  else if( SCIPisGT(scip, candsfrac, 0.7)
225  || (SCIPisEQ(scip, candsfrac, 0.7) && SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0) )
226  *roundup = TRUE;
227  else if( SCIPisEQ(scip, pscostdown, pscostup) )
228  *roundup = (SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0);
229  else if( pscostdown > pscostup )
230  *roundup = TRUE;
231  else
232  *roundup = FALSE;
233 
234  if( *roundup )
235  pscostquot = sqrt(candsfrac) * (1.0 + pscostdown) / (1.0 + pscostup);
236  else
237  pscostquot = sqrt(1.0 - candsfrac) * (1.0 + pscostup) / (1.0 + pscostdown);
238 
239  /* prefer decisions on binary variables */
240  if( SCIPvarIsBinary(cand) && !(SCIPvarMayRoundDown(cand) || SCIPvarMayRoundUp(cand)))
241  pscostquot *= 1000.0;
242 
243  assert(pscostquot >= 0);
244  *score = pscostquot;
245 
246  return SCIP_OKAY;
247 }
248 
249 /*
250  * heuristic specific interface methods
251  */
252 
253 /** creates the pscostdiving heuristic and includes it in SCIP */
255  SCIP* scip /**< SCIP data structure */
256  )
257 {
258  SCIP_HEURDATA* heurdata;
259  SCIP_HEUR* heur;
260 
261  /* create Pscostdiving primal heuristic data */
262  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
263 
264  /* include primal heuristic */
265  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
267  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecPscostdiving, heurdata) );
268 
269  assert(heur != NULL);
270 
271  /* set non-NULL pointers to callback methods */
272  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyPscostdiving) );
273  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreePscostdiving) );
274  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitPscostdiving) );
275  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitPscostdiving) );
276 
277  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
281 
282  return SCIP_OKAY;
283 }
284 
#define HEUR_PRIORITY
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:976
#define DIVESET_DIVETYPES
#define NULL
Definition: def.h:253
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1254
public methods for memory management
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1165
#define HEUR_MAXDEPTH
SCIP_RANDNUMGEN * SCIPdivesetGetRandnumgen(SCIP_DIVESET *diveset)
Definition: heur.c:584
#define DEFAULT_MAXDIVEUBQUOTNOSOL
#define DEFAULT_ONLYLPBRANCHCANDS
SCIP_EXPORT SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:16918
#define FALSE
Definition: def.h:73
#define TRUE
Definition: def.h:72
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible)
Definition: heuristics.c:192
methods commonly used by primal heuristics
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
public methods for problem variables
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
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_heur.c:310
LP diving heuristic that chooses fixings w.r.t. the pseudo cost values.
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:78
public methods for SCIP variables
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip_heur.c:184
SCIP_EXPORT SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3327
#define HEUR_USESSUBSCIP
int SCIPgetNIntVars(SCIP *scip)
Definition: scip_prob.c:2077
public methods for numerical tolerances
#define DEFAULT_LPSOLVEFREQ
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:319
SCIP_EXPORT SCIP_Real SCIPvarGetRootSol(SCIP_VAR *var)
Definition: var.c:12842
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_heur.c:107
#define HEUR_FREQ
static SCIP_DECL_HEURFREE(heurFreePscostdiving)
#define HEUR_DESC
#define DEFAULT_MAXLPITEROFS
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1462
#define DEFAULT_RANDSEED
SCIPInterval sqrt(const SCIPInterval &x)
#define HEUR_DISPCHAR
SCIP_SOL * sol
Definition: struct_heur.h:41
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1175
#define HEUR_FREQOFS
static SCIP_DECL_HEUREXIT(heurExitPscostdiving)
#define SCIP_CALL(x)
Definition: def.h:365
#define HEUR_NAME
#define DEFAULT_BACKTRACK
public methods for primal heuristic plugins and divesets
SCIP_EXPORT SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3338
public data structures and miscellaneous methods
int SCIPrandomGetInt(SCIP_RANDNUMGEN *randnumgen, int minrandval, int maxrandval)
Definition: misc.c:9618
SCIP_DIVESET ** SCIPheurGetDivesets(SCIP_HEUR *heur)
Definition: heur.c:1452
#define SCIP_Bool
Definition: def.h:70
SCIP_RETCODE SCIPincludeHeurPscostdiving(SCIP *scip)
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip_heur.c:200
#define HEUR_TIMING
static SCIP_DECL_DIVESETGETSCORE(divesetGetScorePscostdiving)
#define MIN(x, y)
Definition: def.h:223
#define DEFAULT_MAXDIVEAVGQUOT
#define DEFAULT_MAXRELDEPTH
#define DEFAULT_MAXDIVEUBQUOT
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
#define MAX(x, y)
Definition: def.h:222
#define DEFAULT_MINRELDEPTH
public methods for solutions
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip_heur.c:152
public methods for message output
SCIP_Real SCIPgetVarPseudocostVal(SCIP *scip, SCIP_VAR *var, SCIP_Real solvaldelta)
Definition: scip_var.c:8641
#define SCIP_Real
Definition: def.h:164
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
int SCIPgetNBinVars(SCIP *scip)
Definition: scip_prob.c:2032
static SCIP_DECL_HEURINIT(heurInitPscostdiving)
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:168
static SCIP_DECL_HEUREXEC(heurExecPscostdiving)
public methods for primal heuristics
#define DEFAULT_LPRESOLVEDOMCHGQUOT
#define DEFAULT_MAXLPITERQUOT
public methods for global and local (sub)problems
static SCIP_DECL_HEURCOPY(heurCopyPscostdiving)