Scippy

SCIP

Solving Constraint Integer Programs

heur_coefdiving.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-2017 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_coefdiving.c
17  * @brief LP diving heuristic that chooses fixings w.r.t. the matrix coefficients
18  * @author Tobias Achterberg
19  * @author Marc Pfetsch
20  *
21  * Indicator constraints are taken into account if present.
22  */
23 
24 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
25 
26 #include <assert.h>
27 #include <string.h>
28 
29 #include "scip/heur_coefdiving.h"
30 
31 #define HEUR_NAME "coefdiving"
32 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the matrix coefficients"
33 #define HEUR_DISPCHAR 'c'
34 #define HEUR_PRIORITY -1001000
35 #define HEUR_FREQ 10
36 #define HEUR_FREQOFS 1
37 #define HEUR_MAXDEPTH -1
38 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
39 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
40 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY | SCIP_DIVETYPE_SOS1VARIABLE /**< bit mask that represents all supported dive types */
41 
42 
43 /*
44  * Default parameter settings
45  */
46 
47 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
48 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
49 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
50 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
51 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
52  * where diving is performed (0.0: no limit) */
53 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
54  * where diving is performed (0.0: no limit) */
55 #define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
56 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
57 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
58 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
59 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
60 #define DEFAULT_ONLYLPBRANCHCANDS FALSE /**< should only LP branching candidates be considered instead of the slower but
61  * more general constraint handler diving variable selection? */
62 #define DEFAULT_RANDSEED 83 /**< default random seed */
63 
64 /* locally defined heuristic data */
65 struct SCIP_HeurData
66 {
67  SCIP_SOL* sol; /**< working solution */
68 };
69 
70 /*
71  * local methods
72  */
73 
74 /*
75  * Callback methods
76  */
77 
78 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
79 static
80 SCIP_DECL_HEURCOPY(heurCopyCoefdiving)
81 { /*lint --e{715}*/
82  assert(scip != NULL);
83  assert(heur != NULL);
84  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
85 
86  /* call inclusion method of constraint handler */
88 
89  return SCIP_OKAY;
90 }
91 
92 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
93 static
94 SCIP_DECL_HEURFREE(heurFreeCoefdiving) /*lint --e{715}*/
95 { /*lint --e{715}*/
96  SCIP_HEURDATA* heurdata;
97 
98  assert(heur != NULL);
99  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
100  assert(scip != NULL);
101 
102  /* free heuristic data */
103  heurdata = SCIPheurGetData(heur);
104  assert(heurdata != NULL);
105  SCIPfreeBlockMemory(scip, &heurdata);
106  SCIPheurSetData(heur, NULL);
107 
108  return SCIP_OKAY;
109 }
110 
111 
112 /** initialization method of primal heuristic (called after problem was transformed) */
113 static
114 SCIP_DECL_HEURINIT(heurInitCoefdiving) /*lint --e{715}*/
115 { /*lint --e{715}*/
116  SCIP_HEURDATA* heurdata;
118  assert(heur != NULL);
119  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
120 
121  /* get heuristic data */
122  heurdata = SCIPheurGetData(heur);
123  assert(heurdata != NULL);
124 
125  /* create working solution */
126  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
127 
128  return SCIP_OKAY;
129 }
130 
131 
132 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
133 static
134 SCIP_DECL_HEUREXIT(heurExitCoefdiving) /*lint --e{715}*/
135 { /*lint --e{715}*/
136  SCIP_HEURDATA* heurdata;
138  assert(heur != NULL);
139  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
140 
141  /* get heuristic data */
142  heurdata = SCIPheurGetData(heur);
143  assert(heurdata != NULL);
144 
145 
146  /* free working solution */
147  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
148 
149  return SCIP_OKAY;
150 }
151 
152 
153 /** execution method of primal heuristic */
154 static
155 SCIP_DECL_HEUREXEC(heurExecCoefdiving) /*lint --e{715}*/
156 { /*lint --e{715}*/
157  SCIP_HEURDATA* heurdata;
158  SCIP_DIVESET* diveset;
159 
160  heurdata = SCIPheurGetData(heur);
161  assert(heurdata != NULL);
162 
163  assert(SCIPheurGetNDivesets(heur) > 0);
164  assert(SCIPheurGetDivesets(heur) != NULL);
165  diveset = SCIPheurGetDivesets(heur)[0];
166  assert(diveset != NULL);
167 
168  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible) );
169 
170  return SCIP_OKAY;
171 }
172 
173 /** returns a score for the given candidate -- the best candidate maximizes the diving score */
174 static
175 SCIP_DECL_DIVESETGETSCORE(divesetGetScoreCoefdiving)
176 {
177  SCIP_Bool mayrounddown = SCIPvarMayRoundDown(cand);
178  SCIP_Bool mayroundup = SCIPvarMayRoundUp(cand);
179 
180  if( mayrounddown || mayroundup )
181  {
182  /* choose rounding direction:
183  * - if variable may be rounded in both directions, round corresponding to the fractionality
184  * - otherwise, round in the infeasible direction
185  */
186  if( mayrounddown && mayroundup )
187  {
188  assert( divetype != SCIP_DIVETYPE_SOS1VARIABLE );
189 
190  /* try to avoid variability; decide randomly if the LP solution can contain some noise */
191  if( SCIPisEQ(scip, candsfrac, 0.5) )
192  *roundup = (SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0);
193  else
194  *roundup = (candsfrac > 0.5);
195  }
196  else
197  *roundup = mayrounddown;
198  }
199  else
200  {
201  /* the candidate may not be rounded */
202  int nlocksdown = SCIPvarGetNLocksDown(cand);
203  int nlocksup = SCIPvarGetNLocksUp(cand);
204  *roundup = (nlocksdown > nlocksup || (nlocksdown == nlocksup && candsfrac > 0.5));
205  }
206 
207  if( *roundup )
208  {
209  switch( divetype )
210  {
212  candsfrac = 1.0 - candsfrac;
213  break;
215  if ( SCIPisFeasPositive(scip, candsol) )
216  candsfrac = 1.0 - candsfrac;
217  break;
218  default:
219  SCIPerrorMessage("Error: Unsupported diving type\n");
220  SCIPABORT();
221  return SCIP_INVALIDDATA; /*lint !e527*/
222  } /*lint !e788*/
223  *score = SCIPvarGetNLocksUp(cand);
224  }
225  else
226  {
227  if ( divetype == SCIP_DIVETYPE_SOS1VARIABLE && SCIPisFeasNegative(scip, candsol) )
228  candsfrac = 1.0 - candsfrac;
229  *score = SCIPvarGetNLocksDown(cand);
230  }
231 
232 
233  /* penalize too small fractions */
234  if( SCIPisEQ(scip, candsfrac, 0.01) )
235  {
236  /* try to avoid variability; decide randomly if the LP solution can contain some noise.
237  * use a 1:SCIP_PROBINGSCORE_PENALTYRATIO chance for scaling the score
238  */
240  (*score) *= 0.01;
241  }
242  else if( candsfrac < 0.01 )
243  (*score) *= 0.1;
244 
245  /* prefer decisions on binary variables */
246  if( !SCIPvarIsBinary(cand) )
247  (*score) *= 0.1;
248 
249  /* penalize the variable if it may be rounded. */
250  if( mayrounddown || mayroundup )
251  (*score) -= SCIPgetNLPRows(scip);
252 
253  /* check, if candidate is new best candidate: prefer unroundable candidates in any case */
254  assert( (0.0 < candsfrac && candsfrac < 1.0) || SCIPvarIsBinary(cand) || divetype == SCIP_DIVETYPE_SOS1VARIABLE );
255 
256  return SCIP_OKAY;
257 }
258 
259 /*
260  * heuristic specific interface methods
261  */
262 
263 /** creates the coefdiving heuristic and includes it in SCIP */
265  SCIP* scip /**< SCIP data structure */
266  )
267 {
268  SCIP_HEURDATA* heurdata;
269  SCIP_HEUR* heur;
270 
271  /* create coefdiving primal heuristic data */
272  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
273 
274  /* include primal heuristic */
275  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
277  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecCoefdiving, heurdata) );
278 
279  assert(heur != NULL);
280 
281  /* set non-NULL pointers to callback methods */
282  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyCoefdiving) );
283  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeCoefdiving) );
284  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitCoefdiving) );
285  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitCoefdiving) );
286 
287  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
291 
292  return SCIP_OKAY;
293 }
294 
#define DEFAULT_LPRESOLVEDOMCHGQUOT
#define DEFAULT_MINRELDEPTH
#define DEFAULT_MAXLPITERQUOT
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip.c:8092
#define DEFAULT_RANDSEED
SCIP_DIVESET ** SCIPheurGetDivesets(SCIP_HEUR *heur)
Definition: heur.c:1379
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:16732
SCIP_Bool SCIPisFeasNegative(SCIP *scip, SCIP_Real val)
Definition: scip.c:46175
static SCIP_DECL_HEUREXIT(heurExitCoefdiving)
#define HEUR_DESC
#define DEFAULT_BACKTRACK
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define DEFAULT_MAXRELDEPTH
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip.h:21907
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.c:7999
int SCIPrandomGetInt(SCIP_RANDNUMGEN *randnumgen, int minrandval, int maxrandval)
Definition: misc.c:8723
SCIP_Bool SCIPisEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:45751
#define DEFAULT_MAXDIVEUBQUOTNOSOL
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1102
#define HEUR_DISPCHAR
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip.h:21890
LP diving heuristic that chooses fixings w.r.t. the matrix coefficients.
#define HEUR_USESSUBSCIP
#define HEUR_FREQOFS
#define HEUR_PRIORITY
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1181
#define SCIPerrorMessage
Definition: pub_message.h:45
#define DEFAULT_MAXLPITEROFS
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:8060
#define DEFAULT_MAXDIVEUBQUOT
#define DEFAULT_MAXDIVEAVGQUOT
#define HEUR_FREQ
SCIP_SOL * sol
Definition: struct_heur.h:41
#define NULL
Definition: lpi_spx1.cpp:137
static SCIP_DECL_HEUREXEC(heurExecCoefdiving)
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
int SCIPgetNLPRows(SCIP *scip)
Definition: scip.c:29221
#define SCIP_CALL(x)
Definition: def.h:306
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1389
#define SCIP_PROBINGSCORE_PENALTYRATIO
Definition: def.h:248
static SCIP_DECL_HEURINIT(heurInitCoefdiving)
static SCIP_DECL_HEURCOPY(heurCopyCoefdiving)
#define SCIP_Bool
Definition: def.h:61
#define HEUR_TIMING
#define HEUR_NAME
SCIP_RANDNUMGEN * SCIPdivesetGetRandnumgen(SCIP_DIVESET *diveset)
Definition: heur.c:557
int SCIPvarGetNLocksUp(SCIP_VAR *var)
Definition: var.c:3217
#define DIVESET_DIVETYPES
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.c:8436
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:37631
SCIP_RETCODE SCIPincludeHeurCoefdiving(SCIP *scip)
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible)
Definition: heuristics.c:192
int SCIPvarGetNLocksDown(SCIP_VAR *var)
Definition: var.c:3162
#define DEFAULT_LPSOLVEFREQ
#define DEFAULT_ONLYLPBRANCHCANDS
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip.c:8076
SCIP_Bool SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
Definition: scip.c:46163
#define HEUR_MAXDEPTH
static SCIP_DECL_HEURFREE(heurFreeCoefdiving)
#define SCIP_DIVETYPE_SOS1VARIABLE
Definition: type_heur.h:46
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:8044
#define SCIP_DIVETYPE_INTEGRALITY
Definition: type_heur.h:45
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1092
#define SCIPABORT()
Definition: def.h:278
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3280
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3272
static SCIP_DECL_DIVESETGETSCORE(divesetGetScoreCoefdiving)
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:37005