Scippy

SCIP

Solving Constraint Integer Programs

heur_fracdiving.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_fracdiving.c
17  * @brief LP diving heuristic that chooses fixings w.r.t. the fractionalities
18  * @author Tobias Achterberg
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include "scip/heur_fracdiving.h"
24 #include "scip/heuristics.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 <string.h>
35 
36 #define HEUR_NAME "fracdiving"
37 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the fractionalities"
38 #define HEUR_DISPCHAR 'f'
39 #define HEUR_PRIORITY -1003000
40 #define HEUR_FREQ 10
41 #define HEUR_FREQOFS 3
42 #define HEUR_MAXDEPTH -1
43 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
44 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
45 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY | SCIP_DIVETYPE_SOS1VARIABLE /**< bit mask that represents all supported dive types */
46 
47 
48 /*
49  * Default parameter settings
50  */
51 
52 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
53 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
54 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
55 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
56 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
57  * where diving is performed (0.0: no limit) */
58 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
59  * where diving is performed (0.0: no limit) */
60 #define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
61 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
62 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
63 
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 89 /**< initial random seed */
69 
70 /* locally defined heuristic data */
71 struct SCIP_HeurData
72 {
73  SCIP_SOL* sol; /**< working solution */
74 };
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(heurCopyFracdiving)
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(heurFreeFracdiving) /*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 
113  SCIPfreeBlockMemory(scip, &heurdata);
114  SCIPheurSetData(heur, NULL);
115 
116  return SCIP_OKAY;
117 }
118 
119 
120 /** initialization method of primal heuristic (called after problem was transformed) */
121 static
122 SCIP_DECL_HEURINIT(heurInitFracdiving) /*lint --e{715}*/
123 { /*lint --e{715}*/
124  SCIP_HEURDATA* heurdata;
126  assert(heur != NULL);
127  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
128 
129  /* get heuristic data */
130  heurdata = SCIPheurGetData(heur);
131  assert(heurdata != NULL);
132 
133  /* create working solution */
134  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
135 
136  return SCIP_OKAY;
137 }
138 
139 
140 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
141 static
142 SCIP_DECL_HEUREXIT(heurExitFracdiving) /*lint --e{715}*/
143 { /*lint --e{715}*/
144  SCIP_HEURDATA* heurdata;
146  assert(heur != NULL);
147  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
148 
149  /* get heuristic data */
150  heurdata = SCIPheurGetData(heur);
151  assert(heurdata != NULL);
152 
153  /* free working solution */
154  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
155 
156  return SCIP_OKAY;
157 }
158 
159 
160 /** execution method of primal heuristic */
161 static
162 SCIP_DECL_HEUREXEC(heurExecFracdiving) /*lint --e{715}*/
163 { /*lint --e{715}*/
164  SCIP_HEURDATA* heurdata;
165  SCIP_DIVESET* diveset;
166 
167  heurdata = SCIPheurGetData(heur);
168  assert(heurdata != NULL);
169 
170  assert(SCIPheurGetNDivesets(heur) > 0);
171  assert(SCIPheurGetDivesets(heur) != NULL);
172  diveset = SCIPheurGetDivesets(heur)[0];
173  assert(diveset != NULL);
174 
175  *result = SCIP_DIDNOTRUN;
176 
177  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible) );
178 
179  return SCIP_OKAY;
180 }
181 
182 /** calculate score and preferred rounding direction for the candidate variable; the best candidate maximizes the
183  * score
184  */
185 static
186 SCIP_DECL_DIVESETGETSCORE(divesetGetScoreFracdiving)
187 {
188  SCIP_Real obj;
189  SCIP_Real objnorm;
190  SCIP_Real objgain;
191  SCIP_Bool mayrounddown;
192  SCIP_Bool mayroundup;
193 
194  /* score fractionality if candidate is an SOS1 variable */
195  if ( divetype == SCIP_DIVETYPE_SOS1VARIABLE )
196  {
197  *score = candsfrac;
198 
199  /* 'round' in nonzero direction, i.e., fix the candidates neighbors in the conflict graph to zero */
200  *roundup = SCIPisFeasPositive(scip, candsol);
201 
202  return SCIP_OKAY;
203  }
204 
205  mayrounddown = SCIPvarMayRoundDown(cand);
206  mayroundup = SCIPvarMayRoundUp(cand);
207 
208  /* choose rounding direction:
209  * - if variable may be rounded in either both or neither direction, round corresponding to the fractionality
210  * - otherwise, round in the infeasible direction, because feasible direction is tried by rounding
211  * the current fractional solution
212  */
213  if( mayrounddown != mayroundup )
214  *roundup = mayrounddown;
215  /* try to avoid variability; decide randomly if the LP solution can contain some noise. */
216  else if( SCIPisEQ(scip, candsfrac, 0.5) )
217  *roundup = (SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0);
218  else
219  *roundup = (candsfrac > 0.5);
220 
221  obj = SCIPvarGetObj(cand);
222  objnorm = SCIPgetObjNorm(scip);
223 
224  /* divide by objective norm to normalize obj into [-1,1] */
225  if( SCIPisPositive(scip, objnorm) )
226  obj /= objnorm;
227 
228  /* calculate objective gain and fractionality for the selected rounding direction */
229  if( *roundup )
230  {
231  candsfrac = 1.0 - candsfrac;
232  objgain = obj * candsfrac;
233  }
234  else
235  objgain = -obj * candsfrac;
236 
237  assert(objgain >= -1.0 && objgain <= 1.0);
238 
239  /* penalize too small fractions */
240  if( SCIPisEQ(scip, candsfrac, 0.01) )
241  {
242  /* try to avoid variability; decide randomly if the LP solution can contain some noise.
243  * use a 1:SCIP_PROBINGSCORE_PENALTYRATIO chance for increasing the fractionality, i.e., the score.
244  */
246  candsfrac += 10.0;
247  }
248  else if( candsfrac < 0.01 )
249  candsfrac += 10.0;
250 
251  /* prefer decisions on binary variables */
252  if( !SCIPvarIsBinary(cand) )
253  candsfrac *= 1000.0;
254 
255  /* prefer variables which cannot be rounded by scoring their fractionality */
256  if( !(mayrounddown || mayroundup) )
257  *score = -candsfrac;
258  else
259  *score = -2.0 - objgain;
260 
261  return SCIP_OKAY;
262 }
263 
264 /*
265  * heuristic specific interface methods
266  */
267 
268 /** creates the fracdiving heuristic and includes it in SCIP */
270  SCIP* scip /**< SCIP data structure */
271  )
272 {
273  SCIP_HEURDATA* heurdata;
274  SCIP_HEUR* heur;
275 
276  /* create Fracdiving primal heuristic data */
277  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
278 
279  /* include primal heuristic */
280  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
282  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecFracdiving, heurdata) );
283 
284  assert(heur != NULL);
285 
286  /* set non-NULL pointers to callback methods */
287  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyFracdiving) );
288  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeFracdiving) );
289  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitFracdiving) );
290  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitFracdiving) );
291 
292  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
296  DEFAULT_BACKTRACK, DEFAULT_ONLYLPBRANCHCANDS, DIVESET_DIVETYPES, divesetGetScoreFracdiving) );
297 
298  return SCIP_OKAY;
299 }
300 
static SCIP_DECL_HEUREXIT(heurExitFracdiving)
#define NULL
Definition: def.h:246
#define HEUR_PRIORITY
#define HEUR_DISPCHAR
#define DEFAULT_MAXDIVEUBQUOT
public methods for memory management
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip_heur.c:280
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
SCIP_DIVESET ** SCIPheurGetDivesets(SCIP_HEUR *heur)
Definition: heur.c:1452
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:16910
#define DEFAULT_ONLYLPBRANCHCANDS
#define HEUR_MAXDEPTH
#define HEUR_DESC
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
methods commonly used by primal heuristics
#define DIVESET_DIVETYPES
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
public methods for problem variables
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:114
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:187
SCIP_RETCODE SCIPincludeHeurFracdiving(SCIP *scip)
LP diving heuristic that chooses fixings w.r.t. the fractionalities.
int SCIPrandomGetInt(SCIP_RANDNUMGEN *randnumgen, int minrandval, int maxrandval)
Definition: misc.c:9608
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1175
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:97
SCIP_Real SCIPgetObjNorm(SCIP *scip)
Definition: scip_prob.c:1697
public methods for numerical tolerances
static SCIP_DECL_DIVESETGETSCORE(divesetGetScoreFracdiving)
#define DEFAULT_RANDSEED
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1254
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:248
static SCIP_DECL_HEURCOPY(heurCopyFracdiving)
#define DEFAULT_MAXLPITERQUOT
#define HEUR_TIMING
SCIP_SOL * sol
Definition: struct_heur.h:41
#define DEFAULT_MAXLPITEROFS
#define DEFAULT_MINRELDEPTH
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
#define SCIP_CALL(x)
Definition: def.h:358
#define DEFAULT_MAXDIVEAVGQUOT
static SCIP_DECL_HEUREXEC(heurExecFracdiving)
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1462
#define SCIP_PROBINGSCORE_PENALTYRATIO
Definition: def.h:300
public methods for primal heuristic plugins and divesets
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:69
#define HEUR_NAME
#define DEFAULT_LPSOLVEFREQ
SCIP_RANDNUMGEN * SCIPdivesetGetRandnumgen(SCIP_DIVESET *diveset)
Definition: heur.c:584
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:390
#define DEFAULT_MAXRELDEPTH
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:1034
SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:17192
static SCIP_DECL_HEURINIT(heurInitFracdiving)
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible)
Definition: heuristics.c:192
static SCIP_DECL_HEURFREE(heurFreeFracdiving)
public methods for solutions
public methods for message output
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip_heur.c:264
SCIP_Bool SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
#define SCIP_Real
Definition: def.h:157
#define SCIP_DIVETYPE_SOS1VARIABLE
Definition: type_heur.h:46
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip_heur.c:232
#define DEFAULT_BACKTRACK
#define DEFAULT_MAXDIVEUBQUOTNOSOL
public methods for primal heuristics
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1165
#define HEUR_FREQ
public methods for global and local (sub)problems
#define HEUR_FREQOFS
#define HEUR_USESSUBSCIP
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3330
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3319
#define DEFAULT_LPRESOLVEDOMCHGQUOT
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:377