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-2016 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 #include "scip/pub_dive.h"
28 
29 #define HEUR_NAME "pscostdiving"
30 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the pseudo cost values"
31 #define HEUR_DISPCHAR 'p'
32 #define HEUR_PRIORITY -1002000
33 #define HEUR_FREQ 10
34 #define HEUR_FREQOFS 2
35 #define HEUR_MAXDEPTH -1
36 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
37 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
38 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY /**< bit mask that represents all supported dive types */
39 
40 
41 /*
42  * Default parameter settings
43  */
44 
45 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
46 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
47 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
48 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
49 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
50  * where diving is performed (0.0: no limit) */
51 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
52  * where diving is performed (0.0: no limit) */
53 #define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
54 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
55 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
56 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
57 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
58 #define DEFAULT_ONLYLPBRANCHCANDS TRUE /**< should only LP branching candidates be considered instead of the slower but
59  * more general constraint handler diving variable selection? */
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  SCIPfreeMemory(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  /* free working solution */
144  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
145 
146  return SCIP_OKAY;
147 }
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  assert(pscostdown >= 0.0 && pscostup >= 0.0);
203  if( mayrounddown != mayroundup )
204  *roundup = mayrounddown;
205  else if( candsol < SCIPvarGetRootSol(cand) - 0.4 )
206  *roundup = FALSE;
207  else if( candsol > SCIPvarGetRootSol(cand) + 0.4 )
208  *roundup = TRUE;
209  else if( candsfrac < 0.3 )
210  *roundup = FALSE;
211  else if( candsfrac > 0.7 )
212  *roundup = TRUE;
213  else
214  *roundup = (pscostdown >= pscostup);
215 
216  if( *roundup )
217  pscostquot = sqrt(candsfrac) * (1.0 + pscostdown) / (1.0 + pscostup);
218  else
219  pscostquot = sqrt(1.0 - candsfrac) * (1.0 + pscostup) / (1.0 + pscostdown);
220 
221  /* prefer decisions on binary variables */
222  if( SCIPvarIsBinary(cand) && !(SCIPvarMayRoundDown(cand) || SCIPvarMayRoundUp(cand)))
223  pscostquot *= 1000.0;
224 
225  assert(pscostquot >= 0);
226  *score = pscostquot;
227 
228  return SCIP_OKAY;
229 }
230 
231 /*
232  * heuristic specific interface methods
233  */
234 
235 /** creates the pscostdiving heuristic and includes it in SCIP */
237  SCIP* scip /**< SCIP data structure */
238  )
239 {
240  SCIP_HEURDATA* heurdata;
241  SCIP_HEUR* heur;
242 
243  /* create Pscostdiving primal heuristic data */
244  SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
245 
246  /* include primal heuristic */
247  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
249  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecPscostdiving, heurdata) );
250 
251  assert(heur != NULL);
252 
253  /* set non-NULL pointers to callback methods */
254  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyPscostdiving) );
255  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreePscostdiving) );
256  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitPscostdiving) );
257  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitPscostdiving) );
258 
259  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
263 
264  return SCIP_OKAY;
265 }
266 
#define HEUR_PRIORITY
#define SCIPallocMemory(scip, ptr)
Definition: scip.h:20526
#define DIVESET_DIVETYPES
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1147
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:7297
#define HEUR_MAXDEPTH
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:16623
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3259
library methods for diving heuristics
#define NULL
Definition: lpi_spx.cpp:130
#define DEFAULT_MAXDIVEUBQUOTNOSOL
#define DEFAULT_ONLYLPBRANCHCANDS
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, unsigned int timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition: scip.c:7252
#define FALSE
Definition: def.h:56
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:10743
#define TRUE
Definition: def.h:55
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define SCIP_CALL(x)
Definition: def.h:266
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1355
SCIP_Real SCIPgetVarPseudocostVal(SCIP *scip, SCIP_VAR *var, SCIP_Real solvaldelta)
Definition: scip.c:23218
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
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, SCIP_Bool backtrack, SCIP_Bool onlylpbranchcands, SCIP_Bool specificsos1score, SCIP_DECL_DIVESETGETSCORE((*divesetgetscore)))
Definition: scip.c:7689
LP diving heuristic that chooses fixings w.r.t. the pseudo cost values.
SCIP_DIVESET ** SCIPheurGetDivesets(SCIP_HEUR *heur)
Definition: heur.c:1345
#define HEUR_USESSUBSCIP
#define DEFAULT_LPSOLVEFREQ
SCIP_Real SCIPvarGetRootSol(SCIP_VAR *var)
Definition: var.c:12607
#define HEUR_FREQ
static SCIP_DECL_HEURFREE(heurFreePscostdiving)
#define HEUR_DESC
#define SCIPfreeMemory(scip, ptr)
Definition: scip.h:20542
#define DEFAULT_MAXLPITEROFS
SCIPInterval sqrt(const SCIPInterval &x)
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:34002
#define HEUR_DISPCHAR
SCIP_SOL * sol
Definition: struct_heur.h:40
#define HEUR_FREQOFS
static SCIP_DECL_HEUREXIT(heurExitPscostdiving)
#define HEUR_NAME
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1068
#define DEFAULT_BACKTRACK
#define SCIP_Bool
Definition: def.h:53
#define HEUR_TIMING
static SCIP_DECL_DIVESETGETSCORE(divesetGetScorePscostdiving)
#define MAX(x, y)
Definition: tclique_def.h:75
#define DEFAULT_MAXDIVEAVGQUOT
#define DEFAULT_MAXRELDEPTH
#define DEFAULT_MAXDIVEUBQUOT
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip.c:7345
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip.c:7329
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible)
Definition: dive.c:188
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
#define DEFAULT_MINRELDEPTH
int SCIPgetNIntVars(SCIP *scip)
Definition: scip.c:10788
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:7313
#define SCIP_Real
Definition: def.h:127
#define MIN(x, y)
Definition: memory.c:67
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3267
static SCIP_DECL_HEURINIT(heurInitPscostdiving)
static SCIP_DECL_HEUREXEC(heurExecPscostdiving)
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1058
SCIP_RETCODE SCIPincludeHeurPscostdiving(SCIP *scip)
#define DEFAULT_LPRESOLVEDOMCHGQUOT
#define DEFAULT_MAXLPITERQUOT
static SCIP_DECL_HEURCOPY(heurCopyPscostdiving)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:34607