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