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-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_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 "scip/heur_coefdiving.h"
27 #include "scip/heuristics.h"
28 #include "scip/pub_heur.h"
29 #include "scip/pub_message.h"
30 #include "scip/pub_misc.h"
31 #include "scip/pub_var.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_sol.h"
37 #include <string.h>
38 
39 #define HEUR_NAME "coefdiving"
40 #define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the matrix coefficients"
41 #define HEUR_DISPCHAR 'c'
42 #define HEUR_PRIORITY -1001000
43 #define HEUR_FREQ 10
44 #define HEUR_FREQOFS 1
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 | SCIP_DIVETYPE_SOS1VARIABLE /**< bit mask that represents all supported dive types */
49 
50 
51 /*
52  * Default parameter settings
53  */
54 
55 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
56 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
57 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
58 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
59 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
60  * where diving is performed (0.0: no limit) */
61 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
62  * where diving is performed (0.0: no limit) */
63 #define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
64 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
65 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
66 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
67 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
68 #define DEFAULT_ONLYLPBRANCHCANDS FALSE /**< should only LP branching candidates be considered instead of the slower but
69  * more general constraint handler diving variable selection? */
70 #define DEFAULT_RANDSEED 83 /**< default random seed */
71 
72 /* locally defined heuristic data */
73 struct SCIP_HeurData
74 {
75  SCIP_SOL* sol; /**< working solution */
76 };
77 
78 /*
79  * local methods
80  */
81 
82 /*
83  * Callback methods
84  */
85 
86 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
87 static
88 SCIP_DECL_HEURCOPY(heurCopyCoefdiving)
89 { /*lint --e{715}*/
90  assert(scip != NULL);
91  assert(heur != NULL);
92  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
93 
94  /* call inclusion method of constraint handler */
96 
97  return SCIP_OKAY;
98 }
99 
100 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
101 static
102 SCIP_DECL_HEURFREE(heurFreeCoefdiving) /*lint --e{715}*/
103 { /*lint --e{715}*/
104  SCIP_HEURDATA* heurdata;
106  assert(heur != NULL);
107  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
108  assert(scip != NULL);
109 
110  /* free heuristic data */
111  heurdata = SCIPheurGetData(heur);
112  assert(heurdata != NULL);
113  SCIPfreeBlockMemory(scip, &heurdata);
114  SCIPheurSetData(heur, NULL);
115 
116  return SCIP_OKAY;
117 }
118 
119 
120 /** initialization method of primal heuristic (called after problem was transformed) */
121 static
122 SCIP_DECL_HEURINIT(heurInitCoefdiving) /*lint --e{715}*/
123 { /*lint --e{715}*/
124  SCIP_HEURDATA* heurdata;
126  assert(heur != NULL);
127  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
128 
129  /* get heuristic data */
130  heurdata = SCIPheurGetData(heur);
131  assert(heurdata != NULL);
132 
133  /* create working solution */
134  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
135 
136  return SCIP_OKAY;
137 }
138 
139 
140 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
141 static
142 SCIP_DECL_HEUREXIT(heurExitCoefdiving) /*lint --e{715}*/
143 { /*lint --e{715}*/
144  SCIP_HEURDATA* heurdata;
146  assert(heur != NULL);
147  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
148 
149  /* get heuristic data */
150  heurdata = SCIPheurGetData(heur);
151  assert(heurdata != NULL);
152 
153  /* free working solution */
154  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
155 
156  return SCIP_OKAY;
157 }
158 
159 
160 /** execution method of primal heuristic */
161 static
162 SCIP_DECL_HEUREXEC(heurExecCoefdiving) /*lint --e{715}*/
163 { /*lint --e{715}*/
164  SCIP_HEURDATA* heurdata;
165  SCIP_DIVESET* diveset;
166 
167  heurdata = SCIPheurGetData(heur);
168  assert(heurdata != NULL);
169 
170  assert(SCIPheurGetNDivesets(heur) > 0);
171  assert(SCIPheurGetDivesets(heur) != NULL);
172  diveset = SCIPheurGetDivesets(heur)[0];
173  assert(diveset != NULL);
174 
175  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible) );
176 
177  return SCIP_OKAY;
178 }
179 
180 /** returns a score for the given candidate -- the best candidate maximizes the diving score */
181 static
182 SCIP_DECL_DIVESETGETSCORE(divesetGetScoreCoefdiving)
183 {
184  SCIP_Bool mayrounddown = SCIPvarMayRoundDown(cand);
185  SCIP_Bool mayroundup = SCIPvarMayRoundUp(cand);
186 
187  if( mayrounddown || mayroundup )
188  {
189  /* choose rounding direction:
190  * - if variable may be rounded in both directions, round corresponding to the fractionality
191  * - otherwise, round in the infeasible direction
192  */
193  if( mayrounddown && mayroundup )
194  {
195  assert( divetype != SCIP_DIVETYPE_SOS1VARIABLE );
196 
197  /* try to avoid variability; decide randomly if the LP solution can contain some noise */
198  if( SCIPisEQ(scip, candsfrac, 0.5) )
199  *roundup = (SCIPrandomGetInt(SCIPdivesetGetRandnumgen(diveset), 0, 1) == 0);
200  else
201  *roundup = (candsfrac > 0.5);
202  }
203  else
204  *roundup = mayrounddown;
205  }
206  else
207  {
208  /* the candidate may not be rounded */
209  int nlocksdown = SCIPvarGetNLocksDownType(cand, SCIP_LOCKTYPE_MODEL);
210  int nlocksup = SCIPvarGetNLocksUpType(cand, SCIP_LOCKTYPE_MODEL);
211  *roundup = (nlocksdown > nlocksup || (nlocksdown == nlocksup && candsfrac > 0.5));
212  }
213 
214  if( *roundup )
215  {
216  switch( divetype )
217  {
219  candsfrac = 1.0 - candsfrac;
220  break;
222  if ( SCIPisFeasPositive(scip, candsol) )
223  candsfrac = 1.0 - candsfrac;
224  break;
225  default:
226  SCIPerrorMessage("Error: Unsupported diving type\n");
227  SCIPABORT();
228  return SCIP_INVALIDDATA; /*lint !e527*/
229  } /*lint !e788*/
231  }
232  else
233  {
234  if ( divetype == SCIP_DIVETYPE_SOS1VARIABLE && SCIPisFeasNegative(scip, candsol) )
235  candsfrac = 1.0 - candsfrac;
237  }
238 
239  /* penalize too small fractions */
240  if( SCIPisEQ(scip, candsfrac, 0.01) )
241  {
242  /* try to avoid variability; decide randomly if the LP solution can contain some noise.
243  * use a 1:SCIP_PROBINGSCORE_PENALTYRATIO chance for scaling the score
244  */
246  (*score) *= 0.01;
247  }
248  else if( candsfrac < 0.01 )
249  (*score) *= 0.01;
250 
251  /* prefer decisions on binary variables */
252  if( !SCIPvarIsBinary(cand) )
253  (*score) *= 0.1;
254 
255  /* penalize the variable if it may be rounded. */
256  if( mayrounddown || mayroundup )
257  (*score) -= SCIPgetNLPRows(scip);
258 
259  /* check, if candidate is new best candidate: prefer unroundable candidates in any case */
260  assert( (0.0 < candsfrac && candsfrac < 1.0) || SCIPvarIsBinary(cand) || divetype == SCIP_DIVETYPE_SOS1VARIABLE );
261 
262  return SCIP_OKAY;
263 }
264 
265 /*
266  * heuristic specific interface methods
267  */
268 
269 /** creates the coefdiving heuristic and includes it in SCIP */
271  SCIP* scip /**< SCIP data structure */
272  )
273 {
274  SCIP_HEURDATA* heurdata;
275  SCIP_HEUR* heur;
276 
277  /* create coefdiving primal heuristic data */
278  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
279 
280  /* include primal heuristic */
281  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
283  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecCoefdiving, heurdata) );
284 
285  assert(heur != NULL);
286 
287  /* set non-NULL pointers to callback methods */
288  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyCoefdiving) );
289  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeCoefdiving) );
290  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitCoefdiving) );
291  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitCoefdiving) );
292 
293  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
297 
298  return SCIP_OKAY;
299 }
300 
#define NULL
Definition: def.h:246
#define DEFAULT_LPRESOLVEDOMCHGQUOT
int SCIPvarGetNLocksDownType(SCIP_VAR *var, SCIP_LOCKTYPE locktype)
Definition: var.c:3176
#define DEFAULT_MINRELDEPTH
#define DEFAULT_MAXLPITERQUOT
public methods for memory management
int SCIPvarGetNLocksUpType(SCIP_VAR *var, SCIP_LOCKTYPE locktype)
Definition: var.c:3233
SCIP_RETCODE SCIPsetHeurExit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEUREXIT((*heurexit)))
Definition: scip_heur.c:280
#define DEFAULT_RANDSEED
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)
static SCIP_DECL_HEUREXIT(heurExitCoefdiving)
#define HEUR_DESC
#define DEFAULT_BACKTRACK
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
methods commonly used by primal heuristics
#define DEFAULT_MAXRELDEPTH
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)
#define DEFAULT_MAXDIVEUBQUOTNOSOL
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1175
#define HEUR_DISPCHAR
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:97
LP diving heuristic that chooses fixings w.r.t. the matrix coefficients.
public methods for numerical tolerances
#define HEUR_USESSUBSCIP
#define HEUR_FREQOFS
#define HEUR_PRIORITY
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1254
#define SCIPerrorMessage
Definition: pub_message.h:45
#define DEFAULT_MAXLPITEROFS
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:248
#define DEFAULT_MAXDIVEUBQUOT
#define DEFAULT_MAXDIVEAVGQUOT
#define HEUR_FREQ
SCIP_SOL * sol
Definition: struct_heur.h:41
static SCIP_DECL_HEUREXEC(heurExecCoefdiving)
#define DEFAULT_MAXDIVEAVGQUOTNOSOL
int SCIPgetNLPRows(SCIP *scip)
Definition: scip_lp.c:629
#define SCIP_CALL(x)
Definition: def.h:358
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1462
#define SCIP_PROBINGSCORE_PENALTYRATIO
Definition: def.h:300
static SCIP_DECL_HEURINIT(heurInitCoefdiving)
static SCIP_DECL_HEURCOPY(heurCopyCoefdiving)
public methods for primal heuristic plugins and divesets
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:69
#define HEUR_TIMING
#define HEUR_NAME
SCIP_RANDNUMGEN * SCIPdivesetGetRandnumgen(SCIP_DIVESET *diveset)
Definition: heur.c:584
#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_heur.c:390
SCIP_RETCODE SCIPfreeSol(SCIP *scip, SCIP_SOL **sol)
Definition: scip_sol.c:1034
SCIP_RETCODE SCIPincludeHeurCoefdiving(SCIP *scip)
public methods for the LP relaxation, rows and columns
SCIP_RETCODE SCIPperformGenericDivingAlgorithm(SCIP *scip, SCIP_DIVESET *diveset, SCIP_SOL *worksol, SCIP_HEUR *heur, SCIP_RESULT *result, SCIP_Bool nodeinfeasible)
Definition: heuristics.c:192
public methods for solutions
#define DEFAULT_LPSOLVEFREQ
public methods for message output
#define DEFAULT_ONLYLPBRANCHCANDS
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)
#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_heur.c:232
public methods for primal heuristics
#define SCIP_DIVETYPE_INTEGRALITY
Definition: type_heur.h:45
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1165
#define SCIPABORT()
Definition: def.h:330
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3330
SCIP_Bool SCIPvarMayRoundDown(SCIP_VAR *var)
Definition: var.c:3319
static SCIP_DECL_DIVESETGETSCORE(divesetGetScoreCoefdiving)
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip_sol.c:377