Scippy

SCIP

Solving Constraint Integer Programs

heur_guideddiving.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-2020 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 scipopt.org. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file heur_guideddiving.c
17  * @ingroup DEFPLUGINS_HEUR
18  * @brief LP diving heuristic that chooses fixings in direction of incumbent solutions
19  * @author Tobias Achterberg
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include "scip/heur_guideddiving.h"
25 #include "scip/heuristics.h"
26 #include "scip/pub_heur.h"
27 #include "scip/pub_message.h"
28 #include "scip/pub_sol.h"
29 #include "scip/pub_var.h"
30 #include "scip/scip_heur.h"
31 #include "scip/scip_lp.h"
32 #include "scip/scip_mem.h"
33 #include "scip/scip_numerics.h"
34 #include "scip/scip_prob.h"
35 #include "scip/scip_sol.h"
36 #include <string.h>
37 
38 #define HEUR_NAME "guideddiving"
39 #define HEUR_DESC "LP diving heuristic that chooses fixings in direction of incumbent solutions"
40 #define HEUR_DISPCHAR SCIP_HEURDISPCHAR_DIVING
41 #define HEUR_PRIORITY -1007000
42 #define HEUR_FREQ 10
43 #define HEUR_FREQOFS 7
44 #define HEUR_MAXDEPTH -1
45 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
46 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
47 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY /**< bit mask that represents all supported dive types */
48 #define DIVESET_ISPUBLIC TRUE /**< is this dive set publicly available (ie., can be used by other primal heuristics?) */
49 
50 
51 /*
52  * Default parameter settings
53  */
54 
55 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
56 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
57 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
58 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
59 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
60  * where diving is performed (0.0: no limit) */
61 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
62  * where diving is performed (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 FALSE /**< should only LP branching candidates be considered instead of the slower but
67  * more general constraint handler diving variable selection? */
68 #define DEFAULT_RANDSEED 127 /**< initial seed for random number generation */
69 
70 /* locally defined heuristic data */
71 struct SCIP_HeurData
72 {
73  SCIP_SOL* sol; /**< working solution */
74 };
75 
76 /*
77  * local methods
78  */
79 
80 /*
81  * Callback methods
82  */
83 
84 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
85 static
86 SCIP_DECL_HEURCOPY(heurCopyGuideddiving)
87 { /*lint --e{715}*/
88  assert(scip != NULL);
89  assert(heur != NULL);
90  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
91 
92  /* call inclusion method of primal heuristic */
94 
95  return SCIP_OKAY;
96 }
97 
98 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
99 static
100 SCIP_DECL_HEURFREE(heurFreeGuideddiving) /*lint --e{715}*/
101 { /*lint --e{715}*/
102  SCIP_HEURDATA* heurdata;
104  assert(heur != NULL);
105  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
106  assert(scip != NULL);
107 
108  /* free heuristic data */
109  heurdata = SCIPheurGetData(heur);
110  assert(heurdata != NULL);
111 
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(heurInitGuideddiving) /*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(heurExitGuideddiving) /*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 
159 /** execution method of primal heuristic */
160 static
161 SCIP_DECL_HEUREXEC(heurExecGuideddiving) /*lint --e{715}*/
162 { /*lint --e{715}*/
163  SCIP_HEURDATA* heurdata;
164  SCIP_DIVESET* diveset;
165 
166  assert(heur != NULL);
167  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
168  assert(scip != NULL);
169  assert(result != NULL);
170  assert(SCIPhasCurrentNodeLP(scip));
171 
172  *result = SCIP_DIDNOTRUN;
173 
174  /* don't dive, if no feasible solutions exist */
175  if( SCIPgetNSols(scip) == 0 )
176  return SCIP_OKAY;
177 
178  /* get best solution that should guide the search; if this solution lives in the original variable space,
179  * we cannot use it since it might violate the global bounds of the current problem
180  */
182  return SCIP_OKAY;
183 
184  /* get heuristic's data */
185  heurdata = SCIPheurGetData(heur);
186  assert(heurdata != NULL);
187 
188  /* if there are no integer variables (note that, e.g., SOS1 variables may be present) */
189  if ( SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip) == 0 )
190  return SCIP_OKAY;
191 
192  assert(SCIPheurGetNDivesets(heur) > 0);
193  assert(SCIPheurGetDivesets(heur) != NULL);
194  diveset = SCIPheurGetDivesets(heur)[0];
195  assert(diveset != NULL);
196 
197  /* call generic diving algorithm */
198  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible, -1L, SCIP_DIVECONTEXT_SINGLE) );
199 
200  return SCIP_OKAY;
201 }
202 
203 /* callbacks for diving */
204 
205 /** calculate score and preferred rounding direction for the candidate variable; the best candidate maximizes the
206  * score
207  */
208 static
209 SCIP_DECL_DIVESETGETSCORE(divesetGetScoreGuideddiving)
210 {
211  SCIP_SOL* bestsol;
212  SCIP_Real bestsolval;
213  SCIP_Real obj;
214  SCIP_Real objnorm;
215  SCIP_Real objgain;
216 
217  bestsol = SCIPgetBestSol(scip);
218  assert(bestsol != NULL);
219  assert(!SCIPsolIsOriginal(bestsol));
220 
221  bestsolval = SCIPgetSolVal(scip, bestsol, cand);
222 
223  /* variable should be rounded (guided) into the direction of its incumbent solution value */
224  if( candsol < bestsolval )
225  *roundup = TRUE;
226  else
227  *roundup = FALSE;
228 
229  obj = SCIPvarGetObj(cand);
230  objnorm = SCIPgetObjNorm(scip);
231 
232  /* divide by objective norm to normalize obj into [-1,1] */
233  if( SCIPisPositive(scip, objnorm) )
234  obj /= objnorm;
235 
236  /* calculate objective gain and fractionality for the selected rounding direction */
237  if( *roundup )
238  {
239  candsfrac = 1.0 - candsfrac;
240  objgain = obj * candsfrac;
241  }
242  else
243  objgain = -obj * candsfrac;
244 
245  assert(objgain >= -1.0 && objgain <= 1.0);
246 
247  /* penalize too small fractions */
248  if( candsfrac < 0.01 )
249  candsfrac *= 0.1;
250 
251  /* prefer decisions on binary variables */
252  if( !SCIPvarIsBinary(cand) )
253  candsfrac *= 0.1;
254 
255  /* prefer variables which cannot be rounded by scoring their fractionality */
256  if( !(SCIPvarMayRoundDown(cand) || SCIPvarMayRoundUp(cand)) )
257  *score = -candsfrac;
258  else
259  *score = -2.0 - objgain;
260 
261  return SCIP_OKAY;
262 }
263 
264 /** callback to check preconditions for diving, e.g., if an incumbent solution is available */
265 static
266 SCIP_DECL_DIVESETAVAILABLE(divesetAvailableGuideddiving)
267 {
268  /* don't dive with guided diving if no feasible solutions exists or
269  * if this solution lives in the original variable space,
270  * because it might violate the global bounds of the current problem
271  */
273  *available = FALSE;
274  else
275  *available = TRUE;
276 
277  return SCIP_OKAY;
278 }
279 
280 /*
281  * heuristic specific interface methods
282  */
283 
284 /** creates the guideddiving heuristic and includes it in SCIP */
286  SCIP* scip /**< SCIP data structure */
287  )
288 {
289  SCIP_HEURDATA* heurdata;
290  SCIP_HEUR* heur;
291 
292  /* create Guideddiving primal heuristic data */
293  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
294 
295  /* include primal heuristic */
296  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
298  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecGuideddiving, heurdata) );
299 
300  assert(heur != NULL);
301 
302  /* set non-NULL pointers to callback methods */
303  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyGuideddiving) );
304  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeGuideddiving) );
305  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitGuideddiving) );
306  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitGuideddiving) );
307 
308  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
312  divesetGetScoreGuideddiving, divesetAvailableGuideddiving) );
313 
314  return SCIP_OKAY;
315 }
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:977
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
#define DEFAULT_LPSOLVEFREQ
#define DEFAULT_MINRELDEPTH
#define HEUR_MAXDEPTH
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1429
public methods for memory management
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1340
SCIP_EXPORT SCIP_Bool SCIPsolIsOriginal(SCIP_SOL *sol)
Definition: sol.c:2521
static SCIP_DECL_HEURINIT(heurInitGuideddiving)
#define HEUR_PRIORITY
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip_sol.c:1353
static SCIP_DECL_DIVESETAVAILABLE(divesetAvailableGuideddiving)
static SCIP_DECL_DIVESETGETSCORE(divesetGetScoreGuideddiving)
SCIP_EXPORT SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:17192
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible, SCIP_Longint iterlim, SCIP_DIVECONTEXT divecontext)
Definition: heuristics.c:209
#define FALSE
Definition: def.h:73
#define DEFAULT_ONLYLPBRANCHCANDS
#define DEFAULT_LPRESOLVEDOMCHGQUOT
SCIP_EXPORT SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:17510
SCIP_Real SCIPgetObjNorm(SCIP *scip)
Definition: scip_prob.c:1639
#define DEFAULT_MAXDIVEAVGQUOT
#define TRUE
Definition: def.h:72
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
#define DIVESET_ISPUBLIC
methods commonly used by primal heuristics
LP diving heuristic that chooses fixings in direction of incumbent solutions.
static SCIP_DECL_HEURCOPY(heurCopyGuideddiving)
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:67
public methods for problem variables
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
SCIP_RETCODE SCIPincludeHeurGuideddiving(SCIP *scip)
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:78
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip_heur.c:185
SCIP_EXPORT SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3336
#define DEFAULT_MAXLPITERQUOT
SCIP_Bool SCIPhasCurrentNodeLP(SCIP *scip)
Definition: scip_lp.c:74
int SCIPgetNIntVars(SCIP *scip)
Definition: scip_prob.c:2076
public methods for numerical tolerances
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:320
#define HEUR_TIMING
int SCIPgetNSols(SCIP *scip)
Definition: scip_sol.c:2206
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:108
static SCIP_DECL_HEURFREE(heurFreeGuideddiving)
#define DIVESET_DIVETYPES
#define HEUR_USESSUBSCIP
#define HEUR_FREQOFS
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1637
#define DEFAULT_MAXRELDEPTH
SCIP_SOL * sol
Definition: struct_heur.h:62
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1350
#define NULL
Definition: lpi_spx1.cpp:155
public methods for primal CIP solutions
#define SCIP_CALL(x)
Definition: def.h:364
public methods for primal heuristic plugins and divesets
SCIP_EXPORT SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3347
SCIP_DIVESET ** SCIPheurGetDivesets(SCIP_HEUR *heur)
Definition: heur.c:1627
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip_heur.c:201
#define DEFAULT_MAXLPITEROFS
static SCIP_DECL_HEUREXIT(heurExitGuideddiving)
#define HEUR_DESC
#define DEFAULT_MAXDIVEUBQUOT
public methods for the LP relaxation, rows and columns
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 ispublic, SCIP_Bool specificsos1score, SCIP_DECL_DIVESETGETSCORE((*divesetgetscore)), SCIP_DECL_DIVESETAVAILABLE((*divesetavailable)))
Definition: scip_heur.c:311
#define DEFAULT_RANDSEED
public methods for solutions
static SCIP_DECL_HEUREXEC(heurExecGuideddiving)
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip_heur.c:153
#define HEUR_FREQ
public methods for message output
#define DEFAULT_BACKTRACK
#define SCIP_Real
Definition: def.h:163
#define HEUR_DISPCHAR
int SCIPgetNBinVars(SCIP *scip)
Definition: scip_prob.c:2031
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:169
public methods for primal heuristics
public methods for global and local (sub)problems
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2305
#define HEUR_NAME