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-2016 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 #include "scip/pub_dive.h"
28 
29 #define HEUR_NAME "actconsdiving"
30 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the active constraints"
31 #define HEUR_DISPCHAR 'a'
32 #define HEUR_PRIORITY -1003700
33 #define HEUR_FREQ -1
34 #define HEUR_FREQOFS 5
35 #define HEUR_MAXDEPTH -1
36 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
37 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
38 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY /**< bit mask that represents all supported dive types */
39 
40 /*
41  * Default parameter settings
42  */
43 
44 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
45 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
46 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
47 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
48 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
49  * where diving is performed (0.0: no limit) */
50 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
51  * where diving is performed (0.0: no limit) */
52 #define DEFAULT_MAXDIVEUBQUOTNOSOL 1.0 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
53 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 1.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
54 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
55 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
56 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
57 #define DEFAULT_ONLYLPBRANCHCANDS TRUE /**< should only LP branching candidates be considered instead of the slower but
58  * more general constraint handler diving variable selection? */
59 
60 
61 /* locally defined heuristic data */
62 struct SCIP_HeurData
63 {
64  SCIP_SOL* sol; /**< working solution */
65 };
66 
67 
68 /*
69  * local methods
70  */
71 
72 /** returns a score value for the given variable based on the active constraints that the variable appears in */
73 static
75  SCIP* scip, /**< SCIP data structure */
76  SCIP_SOL* sol, /**< working solution */
77  SCIP_VAR* var, /**< variable to get the score value for */
78  SCIP_Real* downscore, /**< pointer to store the score for branching downwards */
79  SCIP_Real* upscore /**< pointer to store the score for branching upwards */
80  )
81 {
82  SCIP_COL* col;
83  SCIP_ROW** rows;
84  SCIP_Real* vals;
85  int nrows;
86  int r;
87  int nactrows;
88  SCIP_Real nlprows;
89  SCIP_Real downcoefsum;
90  SCIP_Real upcoefsum;
91  SCIP_Real score;
92 
93  assert(downscore != NULL);
94  assert(upscore != NULL);
95 
96  *downscore = 0.0;
97  *upscore = 0.0;
99  return 0.0;
100 
101  col = SCIPvarGetCol(var);
102  assert(col != NULL);
103 
104  rows = SCIPcolGetRows(col);
105  vals = SCIPcolGetVals(col);
106  nrows = SCIPcolGetNLPNonz(col);
107  nactrows = 0;
108  downcoefsum = 0.0;
109  upcoefsum = 0.0;
110  for( r = 0; r < nrows; ++r )
111  {
112  SCIP_ROW* row;
113  SCIP_Real activity;
114  SCIP_Real lhs;
115  SCIP_Real rhs;
116  SCIP_Real dualsol;
117 
118  row = rows[r];
119  /* calculate number of active constraint sides, i.e., count equations as two */
120  lhs = SCIProwGetLhs(row);
121  rhs = SCIProwGetRhs(row);
122 
123  /* @todo this is suboptimal because activity is calculated by looping over all nonzeros of this row, need to
124  * store LP activities instead (which cannot be retrieved if no LP was solved at this node)
125  */
126  activity = SCIPgetRowSolActivity(scip, row, sol);
127 
128  dualsol = SCIProwGetDualsol(row);
129  if( SCIPisFeasEQ(scip, activity, lhs) )
130  {
131  SCIP_Real coef;
132 
133  nactrows++;
134  coef = vals[r] / SCIProwGetNorm(row);
135  if( SCIPisFeasPositive(scip, dualsol) )
136  {
137  if( coef > 0.0 )
138  downcoefsum += coef;
139  else
140  upcoefsum -= coef;
141  }
142  }
143  else if( SCIPisFeasEQ(scip, activity, rhs) )
144  {
145  SCIP_Real coef;
146 
147  nactrows++;
148  coef = vals[r] / SCIProwGetNorm(row);
149  if( SCIPisFeasNegative(scip, dualsol) )
150  {
151  if( coef > 0.0 )
152  upcoefsum += coef;
153  else
154  downcoefsum -= coef;
155  }
156  }
157  }
158 
159  /* use the number of LP rows for normalization */
160  nlprows = (SCIP_Real)SCIPgetNLPRows(scip);
161  upcoefsum /= nlprows;
162  downcoefsum /= nlprows;
163 
164  /* calculate the score using SCIP's branch score. Pass NULL as variable to not have the var branch factor influence
165  * the result
166  */
167  score = nactrows / nlprows + SCIPgetBranchScore(scip, NULL, downcoefsum, upcoefsum);
168 
169  assert(score <= 3.0);
170  assert(score >= 0.0);
171 
172  *downscore = downcoefsum;
173  *upscore = upcoefsum;
174 
175  return score;
176 }
177 
178 
179 /*
180  * Callback methods
181  */
182 
183 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
184 static
185 SCIP_DECL_HEURCOPY(heurCopyActconsdiving)
186 { /*lint --e{715}*/
187  assert(scip != NULL);
188  assert(heur != NULL);
189  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
190 
191  /* call inclusion method of primal heuristic */
193 
194  return SCIP_OKAY;
195 }
196 
197 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
198 static
199 SCIP_DECL_HEURFREE(heurFreeActconsdiving) /*lint --e{715}*/
200 { /*lint --e{715}*/
201  SCIP_HEURDATA* heurdata;
203  assert(heur != NULL);
204  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
205  assert(scip != NULL);
206 
207  /* free heuristic data */
208  heurdata = SCIPheurGetData(heur);
209  assert(heurdata != NULL);
210 
211  SCIPfreeMemory(scip, &heurdata);
212  SCIPheurSetData(heur, NULL);
213 
214  return SCIP_OKAY;
215 }
216 
217 
218 /** initialization method of primal heuristic (called after problem was transformed) */
219 static
220 SCIP_DECL_HEURINIT(heurInitActconsdiving) /*lint --e{715}*/
221 { /*lint --e{715}*/
222  SCIP_HEURDATA* heurdata;
224  assert(heur != NULL);
225  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
226 
227  /* get heuristic data */
228  heurdata = SCIPheurGetData(heur);
229  assert(heurdata != NULL);
230 
231  /* create working solution */
232  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
233 
234  return SCIP_OKAY;
235 }
236 
237 
238 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
239 static
240 SCIP_DECL_HEUREXIT(heurExitActconsdiving) /*lint --e{715}*/
241 { /*lint --e{715}*/
242  SCIP_HEURDATA* heurdata;
244  assert(heur != NULL);
245  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
246 
247  /* get heuristic data */
248  heurdata = SCIPheurGetData(heur);
249  assert(heurdata != NULL);
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 
295  mayrounddown = SCIPvarMayRoundDown(cand);
296  mayroundup = SCIPvarMayRoundUp(cand);
297 
298  /* first, calculate the variable score */
299  assert(SCIPdivesetGetWorkSolution(diveset) != NULL);
300  *score = getNActiveConsScore(scip, SCIPdivesetGetWorkSolution(diveset), cand, &downscore, &upscore);
301 
302  /* get the rounding direction: prefer an unroundable direction */
303  if( mayrounddown && mayroundup )
304  *roundup = (candsfrac > 0.5);
305  else if( mayrounddown || mayroundup )
306  *roundup = mayrounddown;
307  else
308  *roundup = (downscore > upscore);
309 
310  if( *roundup )
311  candsfrac = 1.0 - candsfrac;
312 
313  /* penalize too small fractions */
314  if( candsfrac < 0.01 )
315  (*score) *= 0.01;
316 
317  /* prefer decisions on binary variables */
318  if( !SCIPvarIsBinary(cand) )
319  (*score) *= 0.01;
320 
321  /* penalize variable if it may be rounded */
322  if( mayrounddown || mayroundup )
323  *score -= 3.0;
324 
325  assert(!(mayrounddown || mayroundup) || *score <= 0.0);
326 
327  return SCIP_OKAY;
328 }
329 
330 /*
331  * heuristic specific interface methods
332  */
333 
334 /** creates the actconsdiving heuristic and includes it in SCIP */
336  SCIP* scip /**< SCIP data structure */
337  )
338 {
339  SCIP_HEURDATA* heurdata;
340  SCIP_HEUR* heur;
341 
342  /* create actconsdiving primal heuristic data */
343  SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
344 
345  /* include primal heuristic */
346  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
348  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecActconsdiving, heurdata) );
349 
350  assert(heur != NULL);
351 
352  /* set non-NULL pointers to callback methods */
353  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyActconsdiving) );
354  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeActconsdiving) );
355  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitActconsdiving) );
356  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitActconsdiving) );
357 
358  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
362  DEFAULT_BACKTRACK, DEFAULT_ONLYLPBRANCHCANDS, DIVESET_DIVETYPES, divesetGetScoreActconsdiving) );
363 
364  return SCIP_OKAY;
365 }
366 
#define DEFAULT_MAXDIVEAVGQUOT
#define HEUR_USESSUBSCIP
#define SCIPallocMemory(scip, ptr)
Definition: scip.h:20526
SCIP_Real SCIPgetRowSolActivity(SCIP *scip, SCIP_ROW *row, SCIP_SOL *sol)
Definition: scip.c:28285
#define HEUR_FREQ
SCIP_RETCODE SCIPincludeHeurActconsdiving(SCIP *scip)
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1147
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:7297
SCIP_Bool SCIPvarIsBinary(SCIP_VAR *var)
Definition: var.c:16623
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3259
library methods for diving heuristics
#define DEFAULT_MAXLPITEROFS
#define DEFAULT_BACKTRACK
#define NULL
Definition: lpi_spx.cpp:130
SCIP_Real SCIProwGetLhs(SCIP_ROW *row)
Definition: lp.c:18915
SCIP_RETCODE SCIPincludeHeurBasic(SCIP *scip, SCIP_HEUR **heur, const char *name, const char *desc, char dispchar, int priority, int freq, int freqofs, int maxdepth, unsigned int timingmask, SCIP_Bool usessubscip, SCIP_DECL_HEUREXEC((*heurexec)), SCIP_HEURDATA *heurdata)
Definition: scip.c:7252
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:10743
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
#define SCIP_CALL(x)
Definition: def.h:266
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1355
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
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, SCIP_Bool backtrack, SCIP_Bool onlylpbranchcands, SCIP_Bool specificsos1score, SCIP_DECL_DIVESETGETSCORE((*divesetgetscore)))
Definition: scip.c:7689
SCIP_DIVESET ** SCIPheurGetDivesets(SCIP_HEUR *heur)
Definition: heur.c:1345
#define DIVESET_DIVETYPES
#define DEFAULT_MAXDIVEUBQUOTNOSOL
static SCIP_DECL_HEURFREE(heurFreeActconsdiving)
SCIP_VARSTATUS SCIPvarGetStatus(SCIP_VAR *var)
Definition: var.c:16562
static SCIP_DECL_HEUREXEC(heurExecActconsdiving)
#define SCIPfreeMemory(scip, ptr)
Definition: scip.h:20542
SCIP_ROW ** SCIPcolGetRows(SCIP_COL *col)
Definition: lp.c:18784
#define HEUR_FREQOFS
SCIP_Bool SCIPisFeasPositive(SCIP *scip, SCIP_Real val)
Definition: scip.c:41984
SCIP_Real SCIProwGetRhs(SCIP_ROW *row)
Definition: lp.c:18925
#define DEFAULT_ONLYLPBRANCHCANDS
SCIP_Bool SCIPisFeasNegative(SCIP *scip, SCIP_Real val)
Definition: scip.c:41996
#define HEUR_DISPCHAR
#define DEFAULT_MINRELDEPTH
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:34002
SCIP_Bool SCIPisFeasEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:41907
SCIP_SOL * sol
Definition: struct_heur.h:40
LP diving heuristic that chooses fixings w.r.t. the active constraints the variable appear in...
SCIP_COL * SCIPvarGetCol(SCIP_VAR *var)
Definition: var.c:16771
int SCIPcolGetNLPNonz(SCIP_COL *col)
Definition: lp.c:18773
static SCIP_DECL_HEURINIT(heurInitActconsdiving)
static SCIP_Real getNActiveConsScore(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var, SCIP_Real *downscore, SCIP_Real *upscore)
#define HEUR_PRIORITY
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1068
#define DEFAULT_MAXLPITERQUOT
#define SCIP_Bool
Definition: def.h:53
SCIP_Real SCIProwGetNorm(SCIP_ROW *row)
Definition: lp.c:18891
#define HEUR_DESC
#define HEUR_TIMING
#define HEUR_NAME
#define DEFAULT_MAXDIVEUBQUOT
static SCIP_DECL_HEUREXIT(heurExitActconsdiving)
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip.c:7345
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip.c:7329
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible)
Definition: dive.c:188
SCIP_Real SCIProwGetDualsol(SCIP_ROW *row)
Definition: lp.c:18935
int SCIPgetNIntVars(SCIP *scip)
Definition: scip.c:10788
SCIP_Real SCIPgetBranchScore(SCIP *scip, SCIP_VAR *var, SCIP_Real downgain, SCIP_Real upgain)
Definition: scip.c:33580
#define DEFAULT_LPSOLVEFREQ
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:7313
#define SCIP_Real
Definition: def.h:127
#define HEUR_MAXDEPTH
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3267
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1058
static SCIP_DECL_DIVESETGETSCORE(divesetGetScoreActconsdiving)
#define DEFAULT_LPRESOLVEDOMCHGQUOT
SCIP_Real * SCIPcolGetVals(SCIP_COL *col)
Definition: lp.c:18794
SCIP_SOL * SCIPdivesetGetWorkSolution(SCIP_DIVESET *diveset)
Definition: heur.c:310
#define DEFAULT_MAXRELDEPTH
int SCIPgetNLPRows(SCIP *scip)
Definition: scip.c:26806
static SCIP_DECL_HEURCOPY(heurCopyActconsdiving)
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip.c:34607