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