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-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_fracdiving.c
17  * @ingroup DEFPLUGINS_HEUR
18  * @brief LP diving heuristic that chooses fixings w.r.t. the fractionalities
19  * @author Tobias Achterberg
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include "scip/heur_fracdiving.h"
25 #include "scip/heuristics.h"
26 #include "scip/pub_heur.h"
27 #include "scip/pub_message.h"
28 #include "scip/pub_misc.h"
29 #include "scip/pub_var.h"
30 #include "scip/scip_heur.h"
31 #include "scip/scip_mem.h"
32 #include "scip/scip_numerics.h"
33 #include "scip/scip_prob.h"
34 #include "scip/scip_sol.h"
35 #include <string.h>
36 
37 #define HEUR_NAME "fracdiving"
38 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the fractionalities"
39 #define HEUR_DISPCHAR SCIP_HEURDISPCHAR_DIVING
40 #define HEUR_PRIORITY -1003000
41 #define HEUR_FREQ 10
42 #define HEUR_FREQOFS 3
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 | SCIP_DIVETYPE_SOS1VARIABLE /**< bit mask that represents all supported dive types */
47 #define DIVESET_ISPUBLIC TRUE /**< is this dive set publicly available (ie., can be used by other primal heuristics?) */
48 
49 
50 /*
51  * Default parameter settings
52  */
53 
54 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
55 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
56 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
57 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
58 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
59  * where diving is performed (0.0: no limit) */
60 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
61  * where diving is performed (0.0: no limit) */
62 #define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
63 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
64 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
65 
66 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
67 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
68 #define DEFAULT_ONLYLPBRANCHCANDS FALSE /**< should only LP branching candidates be considered instead of the slower but
69  * more general constraint handler diving variable selection? */
70 #define DEFAULT_RANDSEED 89 /**< initial random seed */
71 
72 /* locally defined heuristic data */
73 struct SCIP_HeurData
74 {
75  SCIP_SOL* sol; /**< working solution */
76 };
77 
78 
79 /*
80  * local methods
81  */
82 
83 /*
84  * Callback methods
85  */
86 
87 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
88 static
89 SCIP_DECL_HEURCOPY(heurCopyFracdiving)
90 { /*lint --e{715}*/
91  assert(scip != NULL);
92  assert(heur != NULL);
93  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
94 
95  /* call inclusion method of primal heuristic */
97 
98  return SCIP_OKAY;
99 }
100 
101 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
102 static
103 SCIP_DECL_HEURFREE(heurFreeFracdiving) /*lint --e{715}*/
104 { /*lint --e{715}*/
105  SCIP_HEURDATA* heurdata;
107  assert(heur != NULL);
108  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
109  assert(scip != NULL);
110 
111  /* free heuristic data */
112  heurdata = SCIPheurGetData(heur);
113  assert(heurdata != NULL);
114 
115  SCIPfreeBlockMemory(scip, &heurdata);
116  SCIPheurSetData(heur, NULL);
117 
118  return SCIP_OKAY;
119 }
120 
121 
122 /** initialization method of primal heuristic (called after problem was transformed) */
123 static
124 SCIP_DECL_HEURINIT(heurInitFracdiving) /*lint --e{715}*/
125 { /*lint --e{715}*/
126  SCIP_HEURDATA* heurdata;
128  assert(heur != NULL);
129  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
130 
131  /* get heuristic data */
132  heurdata = SCIPheurGetData(heur);
133  assert(heurdata != NULL);
134 
135  /* create working solution */
136  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
137 
138  return SCIP_OKAY;
139 }
140 
141 
142 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
143 static
144 SCIP_DECL_HEUREXIT(heurExitFracdiving) /*lint --e{715}*/
145 { /*lint --e{715}*/
146  SCIP_HEURDATA* heurdata;
148  assert(heur != NULL);
149  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
150 
151  /* get heuristic data */
152  heurdata = SCIPheurGetData(heur);
153  assert(heurdata != NULL);
154 
155  /* free working solution */
156  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
157 
158  return SCIP_OKAY;
159 }
160 
161 
162 /** execution method of primal heuristic */
163 static
164 SCIP_DECL_HEUREXEC(heurExecFracdiving) /*lint --e{715}*/
165 { /*lint --e{715}*/
166  SCIP_HEURDATA* heurdata;
167  SCIP_DIVESET* diveset;
168 
169  heurdata = SCIPheurGetData(heur);
170  assert(heurdata != NULL);
171 
172  assert(SCIPheurGetNDivesets(heur) > 0);
173  assert(SCIPheurGetDivesets(heur) != NULL);
174  diveset = SCIPheurGetDivesets(heur)[0];
175  assert(diveset != NULL);
176 
177  *result = SCIP_DIDNOTRUN;
178 
179  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible, -1L, SCIP_DIVECONTEXT_SINGLE) );
180 
181  return SCIP_OKAY;
182 }
183 
184 /** calculate score and preferred rounding direction for the candidate variable; the best candidate maximizes the
185  * score
186  */
187 static
188 SCIP_DECL_DIVESETGETSCORE(divesetGetScoreFracdiving)
189 {
190  SCIP_Real obj;
191  SCIP_Real objnorm;
192  SCIP_Real objgain;
193  SCIP_Bool mayrounddown;
194  SCIP_Bool mayroundup;
195 
196  /* score fractionality if candidate is an SOS1 variable */
197  if ( divetype == SCIP_DIVETYPE_SOS1VARIABLE )
198  {
199  *score = candsfrac;
200 
201  /* 'round' in nonzero direction, i.e., fix the candidates neighbors in the conflict graph to zero */
202  *roundup = SCIPisFeasPositive(scip, candsol);
203 
204  return SCIP_OKAY;
205  }
206 
207  mayrounddown = SCIPvarMayRoundDown(cand);
208  mayroundup = SCIPvarMayRoundUp(cand);
209 
210  /* choose rounding direction:
211  * - if variable may be rounded in either both or neither direction, round corresponding to the fractionality
212  * - otherwise, round in the infeasible direction, because feasible direction is tried by rounding
213  * the current fractional solution
214  */
215  if( mayrounddown != mayroundup )
216  *roundup = mayrounddown;
217  /* try to avoid variability; decide randomly if the LP solution can contain some noise. */
218  else if( SCIPisEQ(scip, candsfrac, 0.5) )
219  *roundup = (SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0);
220  else
221  *roundup = (candsfrac > 0.5);
222 
223  obj = SCIPvarGetObj(cand);
224  objnorm = SCIPgetObjNorm(scip);
225 
226  /* divide by objective norm to normalize obj into [-1,1] */
227  if( SCIPisPositive(scip, objnorm) )
228  obj /= objnorm;
229 
230  /* calculate objective gain and fractionality for the selected rounding direction */
231  if( *roundup )
232  {
233  candsfrac = 1.0 - candsfrac;
234  objgain = obj * candsfrac;
235  }
236  else
237  objgain = -obj * candsfrac;
238 
239  assert(objgain >= -1.0 && objgain <= 1.0);
240 
241  /* penalize too small fractions */
242  if( SCIPisEQ(scip, candsfrac, 0.01) )
243  {
244  /* try to avoid variability; decide randomly if the LP solution can contain some noise.
245  * use a 1:SCIP_PROBINGSCORE_PENALTYRATIO chance for increasing the fractionality, i.e., the score.
246  */
248  candsfrac += 10.0;
249  }
250  else if( candsfrac < 0.01 )
251  candsfrac += 10.0;
252 
253  /* prefer decisions on binary variables */
254  if( !SCIPvarIsBinary(cand) )
255  candsfrac *= 1000.0;
256 
257  /* prefer variables which cannot be rounded by scoring their fractionality */
258  if( !(mayrounddown || mayroundup) )
259  *score = -candsfrac;
260  else
261  *score = -2.0 - objgain;
262 
263  return SCIP_OKAY;
264 }
265 
266 #define divesetAvailableFracdiving NULL
267 
268 /*
269  * heuristic specific interface methods
270  */
271 
272 /** creates the fracdiving heuristic and includes it in SCIP */
274  SCIP* scip /**< SCIP data structure */
275  )
276 {
277  SCIP_HEURDATA* heurdata;
278  SCIP_HEUR* heur;
279 
280  /* create Fracdiving primal heuristic data */
281  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
282 
283  /* include primal heuristic */
284  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
286  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecFracdiving, heurdata) );
287 
288  assert(heur != NULL);
289 
290  /* set non-NULL pointers to callback methods */
291  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyFracdiving) );
292  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeFracdiving) );
293  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitFracdiving) );
294  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitFracdiving) );
295 
296  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
301  DIVESET_ISPUBLIC, DIVESET_DIVETYPES, divesetGetScoreFracdiving, divesetAvailableFracdiving) );
302 
303  return SCIP_OKAY;
304 }
305 
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:977
static SCIP_DECL_HEUREXIT(heurExitFracdiving)
SCIP_Bool SCIPisPositive(SCIP *scip, SCIP_Real val)
#define HEUR_PRIORITY
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1429
#define HEUR_DISPCHAR
#define DEFAULT_MAXDIVEUBQUOT
public methods for memory management
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1340
#define DIVESET_ISPUBLIC
SCIP_RANDNUMGEN * SCIPdivesetGetRandnumgen(SCIP_DIVESET *diveset)
Definition: heur.c:700
#define DEFAULT_ONLYLPBRANCHCANDS
#define HEUR_MAXDEPTH
SCIP_EXPORT SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:17197
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
SCIP_EXPORT SCIP_Real SCIPvarGetObj(SCIP_VAR *var)
Definition: var.c:17515
SCIP_Real SCIPgetObjNorm(SCIP *scip)
Definition: scip_prob.c:1639
#define HEUR_DESC
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
methods commonly used by primal heuristics
#define DIVESET_DIVETYPES
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:67
public methods for problem variables
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
LP diving heuristic that chooses fixings w.r.t. the fractionalities.
#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
public methods for numerical tolerances
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:320
static SCIP_DECL_DIVESETGETSCORE(divesetGetScoreFracdiving)
#define DEFAULT_RANDSEED
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
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1637
static SCIP_DECL_HEURCOPY(heurCopyFracdiving)
#define DEFAULT_MAXLPITERQUOT
SCIP_RETCODE SCIPincludeHeurFracdiving(SCIP *scip)
#define HEUR_TIMING
SCIP_SOL * sol
Definition: struct_heur.h:62
#define DEFAULT_MAXLPITEROFS
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1350
#define NULL
Definition: lpi_spx1.cpp:155
#define DEFAULT_MINRELDEPTH
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
#define SCIP_CALL(x)
Definition: def.h:364
#define DEFAULT_MAXDIVEAVGQUOT
static SCIP_DECL_HEUREXEC(heurExecFracdiving)
#define SCIP_PROBINGSCORE_PENALTYRATIO
Definition: def.h:306
public methods for primal heuristic plugins and divesets
SCIP_EXPORT SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3347
public data structures and miscellaneous methods
int SCIPrandomGetInt(SCIP_RANDNUMGEN *randnumgen, int minrandval, int maxrandval)
Definition: misc.c:9945
SCIP_DIVESET ** SCIPheurGetDivesets(SCIP_HEUR *heur)
Definition: heur.c:1627
#define SCIP_Bool
Definition: def.h:70
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip_heur.c:201
#define HEUR_NAME
#define DEFAULT_LPSOLVEFREQ
#define DEFAULT_MAXRELDEPTH
static SCIP_DECL_HEURINIT(heurInitFracdiving)
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
static SCIP_DECL_HEURFREE(heurFreeFracdiving)
public methods for solutions
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip_heur.c:153
public methods for message output
#define divesetAvailableFracdiving
#define SCIP_Real
Definition: def.h:163
#define SCIP_DIVETYPE_SOS1VARIABLE
Definition: type_heur.h:52
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:169
#define DEFAULT_BACKTRACK
#define DEFAULT_MAXDIVEUBQUOTNOSOL
public methods for primal heuristics
#define HEUR_FREQ
public methods for global and local (sub)problems
#define HEUR_FREQOFS
#define HEUR_USESSUBSCIP
SCIP_Bool SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
#define DEFAULT_LPRESOLVEDOMCHGQUOT