Scippy

SCIP

Solving Constraint Integer Programs

heur_actconsdiving.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_actconsdiving.c
17  * @brief LP diving heuristic that chooses fixings w.r.t. the active constraints the variable appear in
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 
27 
28 #define HEUR_NAME "actconsdiving"
29 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the active constraints"
30 #define HEUR_DISPCHAR 'a'
31 #define HEUR_PRIORITY -1003700
32 #define HEUR_FREQ -1
33 #define HEUR_FREQOFS 5
34 #define HEUR_MAXDEPTH -1
35 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
36 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
37 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY /**< bit mask that represents all supported dive types */
38 
39 /*
40  * Default parameter settings
41  */
42 
43 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
44 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
45 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
46 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
47 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
48  * where diving is performed (0.0: no limit) */
49 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
50  * where diving is performed (0.0: no limit) */
51 #define DEFAULT_MAXDIVEUBQUOTNOSOL 1.0 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
52 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 1.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
53 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
54 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
55 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
56 #define DEFAULT_ONLYLPBRANCHCANDS TRUE /**< should only LP branching candidates be considered instead of the slower but
57  * more general constraint handler diving variable selection? */
58 #define DEFAULT_RANDSEED 149 /**< default random seed */
59 
60 /* locally defined heuristic data */
61 struct SCIP_HeurData
62 {
63  SCIP_SOL* sol; /**< working solution */
64 };
65 
66 
67 /*
68  * local methods
69  */
70 
71 /** returns a score value for the given variable based on the active constraints that the variable appears in */
72 static
74  SCIP* scip, /**< SCIP data structure */
75  SCIP_SOL* sol, /**< working solution */
76  SCIP_VAR* var, /**< variable to get the score value for */
77  SCIP_Real* downscore, /**< pointer to store the score for branching downwards */
78  SCIP_Real* upscore /**< pointer to store the score for branching upwards */
79  )
80 {
81  SCIP_COL* col;
82  SCIP_ROW** rows;
83  SCIP_Real* vals;
84  int nrows;
85  int r;
86  int nactrows;
87  SCIP_Real nlprows;
88  SCIP_Real downcoefsum;
89  SCIP_Real upcoefsum;
90  SCIP_Real score;
91 
92  assert(downscore != NULL);
93  assert(upscore != NULL);
94 
95  *downscore = 0.0;
96  *upscore = 0.0;
98  return 0.0;
99 
100  col = SCIPvarGetCol(var);
101  assert(col != NULL);
102 
103  rows = SCIPcolGetRows(col);
104  vals = SCIPcolGetVals(col);
105  nrows = SCIPcolGetNLPNonz(col);
106  nactrows = 0;
107  downcoefsum = 0.0;
108  upcoefsum = 0.0;
109  for( r = 0; r < nrows; ++r )
110  {
111  SCIP_ROW* row;
112  SCIP_Real activity;
113  SCIP_Real lhs;
114  SCIP_Real rhs;
115  SCIP_Real dualsol;
116 
117  row = rows[r];
118  /* calculate number of active constraint sides, i.e., count equations as two */
119  lhs = SCIProwGetLhs(row);
120  rhs = SCIProwGetRhs(row);
121 
122  /* @todo this is suboptimal because activity is calculated by looping over all nonzeros of this row, need to
123  * store LP activities instead (which cannot be retrieved if no LP was solved at this node)
124  */
125  activity = SCIPgetRowSolActivity(scip, row, sol);
126 
127  dualsol = SCIProwGetDualsol(row);
128  if( SCIPisFeasEQ(scip, activity, lhs) )
129  {
130  SCIP_Real coef;
131 
132  nactrows++;
133  coef = vals[r] / SCIProwGetNorm(row);
134  if( SCIPisFeasPositive(scip, dualsol) )
135  {
136  if( coef > 0.0 )
137  downcoefsum += coef;
138  else
139  upcoefsum -= coef;
140  }
141  }
142  else if( SCIPisFeasEQ(scip, activity, rhs) )
143  {
144  SCIP_Real coef;
145 
146  nactrows++;
147  coef = vals[r] / SCIProwGetNorm(row);
148  if( SCIPisFeasNegative(scip, dualsol) )
149  {
150  if( coef > 0.0 )
151  upcoefsum += coef;
152  else
153  downcoefsum -= coef;
154  }
155  }
156  }
157 
158  /* use the number of LP rows for normalization */
159  nlprows = (SCIP_Real)SCIPgetNLPRows(scip);
160  upcoefsum /= nlprows;
161  downcoefsum /= nlprows;
162 
163  /* calculate the score using SCIP's branch score. Pass NULL as variable to not have the var branch factor influence
164  * the result
165  */
166  score = nactrows / nlprows + SCIPgetBranchScore(scip, NULL, downcoefsum, upcoefsum);
167 
168  assert(score <= 3.0);
169  assert(score >= 0.0);
170 
171  *downscore = downcoefsum;
172  *upscore = upcoefsum;
173 
174  return score;
175 }
176 
177 
178 /*
179  * Callback methods
180  */
181 
182 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
183 static
184 SCIP_DECL_HEURCOPY(heurCopyActconsdiving)
185 { /*lint --e{715}*/
186  assert(scip != NULL);
187  assert(heur != NULL);
188  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
189 
190  /* call inclusion method of primal heuristic */
192 
193  return SCIP_OKAY;
194 }
195 
196 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
197 static
198 SCIP_DECL_HEURFREE(heurFreeActconsdiving) /*lint --e{715}*/
199 { /*lint --e{715}*/
200  SCIP_HEURDATA* heurdata;
202  assert(heur != NULL);
203  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
204  assert(scip != NULL);
205 
206  /* free heuristic data */
207  heurdata = SCIPheurGetData(heur);
208  assert(heurdata != NULL);
209 
210  SCIPfreeBlockMemory(scip, &heurdata);
211  SCIPheurSetData(heur, NULL);
212 
213  return SCIP_OKAY;
214 }
215 
216 
217 /** initialization method of primal heuristic (called after problem was transformed) */
218 static
219 SCIP_DECL_HEURINIT(heurInitActconsdiving) /*lint --e{715}*/
220 { /*lint --e{715}*/
221  SCIP_HEURDATA* heurdata;
223  assert(heur != NULL);
224  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
225 
226  /* get heuristic data */
227  heurdata = SCIPheurGetData(heur);
228  assert(heurdata != NULL);
229 
230  /* create working solution */
231  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
232 
233  return SCIP_OKAY;
234 }
235 
236 
237 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
238 static
239 SCIP_DECL_HEUREXIT(heurExitActconsdiving) /*lint --e{715}*/
240 { /*lint --e{715}*/
241  SCIP_HEURDATA* heurdata;
243  assert(heur != NULL);
244  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
245 
246  /* get heuristic data */
247  heurdata = SCIPheurGetData(heur);
248  assert(heurdata != NULL);
249 
250 
251  /* free working solution */
252  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
253 
254  return SCIP_OKAY;
255 }
256 
257 
258 /** execution method of primal heuristic */
259 static
260 SCIP_DECL_HEUREXEC(heurExecActconsdiving) /*lint --e{715}*/
261 { /*lint --e{715}*/
262  SCIP_HEURDATA* heurdata;
263  SCIP_DIVESET* diveset;
264 
265  heurdata = SCIPheurGetData(heur);
266 
267  assert(SCIPheurGetNDivesets(heur) > 0);
268  assert(SCIPheurGetDivesets(heur) != NULL);
269  diveset = SCIPheurGetDivesets(heur)[0];
270  assert(diveset != NULL);
271 
272  *result = SCIP_DIDNOTRUN;
273 
274  /* if there are no integer variables, stop execution */
275  if( SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip) == 0 )
276  return SCIP_OKAY;
277 
278  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible) );
279 
280  return SCIP_OKAY;
281 }
282 
283 /** calculate score and preferred rounding direction for the candidate variable; the best candidate maximizes the
284  * score
285  */
286 static
287 SCIP_DECL_DIVESETGETSCORE(divesetGetScoreActconsdiving)
288 {
289  SCIP_Bool mayrounddown;
290  SCIP_Bool mayroundup;
291  SCIP_Real downscore;
292  SCIP_Real upscore;
293 
294  mayrounddown = SCIPvarMayRoundDown(cand);
295  mayroundup = SCIPvarMayRoundUp(cand);
296 
297  /* first, calculate the variable score */
298  assert(SCIPdivesetGetWorkSolution(diveset) != NULL);
299  *score = getNActiveConsScore(scip, SCIPdivesetGetWorkSolution(diveset), cand, &downscore, &upscore);
300 
301  /* get the rounding direction: prefer an unroundable direction */
302  if( mayrounddown && mayroundup )
303  {
304  /* try to avoid variability; decide randomly if the LP solution can contain some noise */
305  if( SCIPisEQ(scip, candsfrac, 0.5) )
306  *roundup = (SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0);
307  else
308  *roundup = (candsfrac > 0.5);
309  }
310  else if( mayrounddown || mayroundup )
311  *roundup = mayrounddown;
312  else
313  *roundup = (downscore > upscore);
314 
315  if( *roundup )
316  candsfrac = 1.0 - candsfrac;
317 
318  /* penalize too small fractions */
319  if( SCIPisEQ(scip, candsfrac, 0.01) )
320  {
321  /* try to avoid variability; decide randomly if the LP solution can contain some noise.
322  * use a 1:SCIP_PROBINGSCORE_PENALTYRATIO chance for scaling the score
323  */
325  (*score) *= 0.01;
326  }
327  else if( candsfrac < 0.01 )
328  (*score) *= 0.01;
329 
330  /* prefer decisions on binary variables */
331  if( !SCIPvarIsBinary(cand) )
332  (*score) *= 0.01;
333 
334  /* penalize variable if it may be rounded */
335  if( mayrounddown || mayroundup )
336  *score -= 3.0;
337 
338  assert(!(mayrounddown || mayroundup) || *score <= 0.0);
339 
340  return SCIP_OKAY;
341 }
342 
343 /*
344  * heuristic specific interface methods
345  */
346 
347 /** creates the actconsdiving heuristic and includes it in SCIP */
349  SCIP* scip /**< SCIP data structure */
350  )
351 {
352  SCIP_HEURDATA* heurdata;
353  SCIP_HEUR* heur;
354 
355  /* create actconsdiving primal heuristic data */
356  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
357 
358  /* include primal heuristic */
359  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
361  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecActconsdiving, heurdata) );
362 
363  assert(heur != NULL);
364 
365  /* set non-NULL pointers to callback methods */
366  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyActconsdiving) );
367  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeActconsdiving) );
368  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitActconsdiving) );
369  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitActconsdiving) );
370 
371  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
375  DEFAULT_BACKTRACK, DEFAULT_ONLYLPBRANCHCANDS, DIVESET_DIVETYPES, divesetGetScoreActconsdiving) );
376 
377  return SCIP_OKAY;
378 }
379 
#define DEFAULT_MAXDIVEAVGQUOT
#define HEUR_USESSUBSCIP
int SCIPgetNIntVars(SCIP *scip)
Definition: scip.c:11721
SCIP_Bool SCIPisFeasEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:46086
#define HEUR_FREQ
#define DEFAULT_RANDSEED
#define DEFAULT_MAXLPITEROFS
#define DEFAULT_BACKTRACK
SCIP_Real * SCIPcolGetVals(SCIP_COL *col)
Definition: lp.c:16190
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip.c:8092
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
SCIP_Real SCIProwGetLhs(SCIP_ROW *row)
Definition: lp.c:16311
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
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
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1102
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip.h:21890
SCIP_Real SCIProwGetDualsol(SCIP_ROW *row)
Definition: lp.c:16331
#define DIVESET_DIVETYPES
#define DEFAULT_MAXDIVEUBQUOTNOSOL
static SCIP_DECL_HEURFREE(heurFreeActconsdiving)
SCIP_Real SCIPgetBranchScore(SCIP *scip, SCIP_VAR *var, SCIP_Real downgain, SCIP_Real upgain)
Definition: scip.c:36583
static SCIP_DECL_HEUREXEC(heurExecActconsdiving)
#define HEUR_FREQOFS
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1181
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:8060
SCIP_ROW ** SCIPcolGetRows(SCIP_COL *col)
Definition: lp.c:16180
#define DEFAULT_ONLYLPBRANCHCANDS
#define HEUR_DISPCHAR
#define DEFAULT_MINRELDEPTH
SCIP_SOL * sol
Definition: struct_heur.h:41
LP diving heuristic that chooses fixings w.r.t. the active constraints the variable appear in...
#define NULL
Definition: lpi_spx1.cpp:137
int SCIPgetNLPRows(SCIP *scip)
Definition: scip.c:29221
#define SCIP_CALL(x)
Definition: def.h:306
static SCIP_DECL_HEURINIT(heurInitActconsdiving)
static SCIP_Real getNActiveConsScore(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real *downscore, SCIP_Real *upscore)
SCIP_Real SCIProwGetRhs(SCIP_ROW *row)
Definition: lp.c:16321
#define HEUR_PRIORITY
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1389
#define SCIP_PROBINGSCORE_PENALTYRATIO
Definition: def.h:248
#define DEFAULT_MAXLPITERQUOT
#define SCIP_Bool
Definition: def.h:61
#define HEUR_DESC
SCIP_RANDNUMGEN * SCIPdivesetGetRandnumgen(SCIP_DIVESET *diveset)
Definition: heur.c:557
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_COL * SCIPvarGetCol(SCIP_VAR *var)
Definition: var.c:16880
SCIP_Real SCIPgetRowSolActivity(SCIP *scip, SCIP_ROW *row, SCIP_SOL *sol)
Definition: scip.c:30713
#define HEUR_TIMING
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:11676
#define HEUR_NAME
#define DEFAULT_MAXDIVEUBQUOT
static SCIP_DECL_HEUREXIT(heurExitActconsdiving)
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible)
Definition: heuristics.c:192
#define DEFAULT_LPSOLVEFREQ
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
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:16671
#define SCIP_Real
Definition: def.h:135
#define HEUR_MAXDEPTH
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:8044
static SCIP_DECL_DIVESETGETSCORE(divesetGetScoreActconsdiving)
#define DEFAULT_LPRESOLVEDOMCHGQUOT
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1092
SCIP_RETCODE SCIPincludeHeurActconsdiving(SCIP *scip)
int SCIPcolGetNLPNonz(SCIP_COL *col)
Definition: lp.c:16169
SCIP_SOL * SCIPdivesetGetWorkSolution(SCIP_DIVESET *diveset)
Definition: heur.c:327
#define DEFAULT_MAXRELDEPTH
SCIP_Real SCIProwGetNorm(SCIP_ROW *row)
Definition: lp.c:16287
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3280
static SCIP_DECL_HEURCOPY(heurCopyActconsdiving)
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3272
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:37005