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