Scippy

SCIP

Solving Constraint Integer Programs

heur_linesearchdiving.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-2018 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_linesearchdiving.c
17  * @brief LP diving heuristic that fixes variables with a large difference to their root solution
18  * @author Tobias Achterberg
19  */
20 
21 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
22 
23 #include "scip/heuristics.h"
25 #include "scip/pub_heur.h"
26 #include "scip/pub_message.h"
27 #include "scip/pub_var.h"
28 #include "scip/scip_heur.h"
29 #include "scip/scip_mem.h"
30 #include "scip/scip_numerics.h"
31 #include "scip/scip_sol.h"
32 #include <string.h>
33 
34 #define HEUR_NAME "linesearchdiving"
35 #define HEUR_DESC "LP diving heuristic that chooses fixings following the line from root solution to current solution"
36 #define HEUR_DISPCHAR 'l'
37 #define HEUR_PRIORITY -1006000
38 #define HEUR_FREQ 10
39 #define HEUR_FREQOFS 6
40 #define HEUR_MAXDEPTH -1
41 #define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
42 #define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
43 #define DIVESET_DIVETYPES SCIP_DIVETYPE_INTEGRALITY | SCIP_DIVETYPE_SOS1VARIABLE /**< bit mask that represents all supported dive types */
44 
45 
46 /*
47  * Default parameter settings
48  */
49 
50 #define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
51 #define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
52 #define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
53 #define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
54 #define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
55  * where diving is performed (0.0: no limit) */
56 #define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
57  * where diving is performed (0.0: no limit) */
58 #define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
59 #define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
60 #define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
61 #define DEFAULT_LPRESOLVEDOMCHGQUOT 0.15 /**< percentage of immediate domain changes during probing to trigger LP resolve */
62 #define DEFAULT_LPSOLVEFREQ 0 /**< LP solve frequency for diving heuristics */
63 #define DEFAULT_ONLYLPBRANCHCANDS FALSE /**< should only LP branching candidates be considered instead of the slower but
64  * more general constraint handler diving variable selection? */
65 #define DEFAULT_RANDSEED 137 /**< default initialization for random seed number generation */
66 /*
67  * Data structures
68  */
69 
70 /** primal heuristic data */
71 struct SCIP_HeurData
72 {
73  SCIP_SOL* sol; /**< working solution */
74 };
75 
76 
77 /*
78  * Local methods
79  */
80 
81 
82 /*
83  * Callback methods of primal heuristic
84  */
85 
86 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
87 static
88 SCIP_DECL_HEURCOPY(heurCopyLinesearchdiving)
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 primal heuristic */
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(heurFreeLinesearchdiving)
103 { /*lint --e{715}*/
104  SCIP_HEURDATA* heurdata;
106  assert(heur != NULL);
107  assert(scip != NULL);
108 
109  /* free heuristic data */
110  heurdata = SCIPheurGetData(heur);
111  assert(heurdata != NULL);
112  SCIPfreeBlockMemory(scip, &heurdata);
113  SCIPheurSetData(heur, NULL);
114 
115  return SCIP_OKAY;
116 }
117 
118 
119 /** initialization method of primal heuristic (called after problem was transformed) */
120 static
121 SCIP_DECL_HEURINIT(heurInitLinesearchdiving)
122 { /*lint --e{715}*/
123  SCIP_HEURDATA* heurdata;
125  assert(heur != NULL);
126 
127  /* get heuristic data */
128  heurdata = SCIPheurGetData(heur);
129  assert(heurdata != NULL);
130 
131  /* create working solution */
132  SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
133 
134  return SCIP_OKAY;
135 }
136 
137 
138 /** deinitialization method of primal heuristic (called before transformed problem is freed) */
139 static
140 SCIP_DECL_HEUREXIT(heurExitLinesearchdiving)
141 { /*lint --e{715}*/
142  SCIP_HEURDATA* heurdata;
144  assert(heur != NULL);
145 
146  /* get heuristic data */
147  heurdata = SCIPheurGetData(heur);
148  assert(heurdata != NULL);
149 
150  /* free working solution */
151  SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
152 
153  return SCIP_OKAY;
154 }
155 
156 
157 /** execution method of primal heuristic */
158 static
159 SCIP_DECL_HEUREXEC(heurExecLinesearchdiving)
160 { /*lint --e{715}*/
161  SCIP_HEURDATA* heurdata;
162  SCIP_DIVESET* diveset;
163 
164  heurdata = SCIPheurGetData(heur);
165  assert(SCIPheurGetNDivesets(heur) > 0);
166  assert(SCIPheurGetDivesets(heur) != NULL);
167  diveset = SCIPheurGetDivesets(heur)[0];
168  assert(diveset != NULL);
169 
170  *result = SCIP_DIDNOTRUN;
171 
172  SCIP_CALL( SCIPperformGenericDivingAlgorithm(scip, diveset, heurdata->sol, heur, result, nodeinfeasible) );
173 
174  return SCIP_OKAY;
175 }
176 
177 /* diving setting callbacks */
178 
179 /** returns a score for the given candidate -- the best candidate maximizes the diving score */
180 static
181 SCIP_DECL_DIVESETGETSCORE(divesetGetScoreLinesearchdiving)
182 { /*lint --e{715}*/
183  SCIP_Real rootsolval;
184  SCIP_Real distquot;
185 
186  rootsolval = SCIPvarGetRootSol(cand);
187 
188  /* preferred branching direction is further away from the root LP solution */
189  if( SCIPisLT(scip, candsol, rootsolval) )
190  {
191  /* round down*/
192  *roundup = FALSE;
193 
194  switch( divetype )
195  {
197  distquot = (candsfrac + SCIPsumepsilon(scip)) / (rootsolval - candsol);
198  break;
200  if ( SCIPisFeasPositive(scip, candsol) )
201  distquot = (candsfrac + SCIPsumepsilon(scip)) / (rootsolval - candsol);
202  else
203  distquot = (1.0 - candsfrac + SCIPsumepsilon(scip)) / (candsol - rootsolval);
204  break;
205  default:
206  SCIPerrorMessage("Error: Unsupported diving type\n");
207  SCIPABORT();
208  return SCIP_INVALIDDATA; /*lint !e527*/
209  } /*lint !e788*/
210 
211  /* avoid roundable candidates */
212  if( SCIPvarMayRoundDown(cand) )
213  distquot *= 1000.0;
214  }
215  else if( SCIPisGT(scip, candsol, rootsolval) )
216  {
217  /* round up */
218  switch( divetype )
219  {
221  distquot = (1.0 - candsfrac + SCIPsumepsilon(scip)) / (candsol - rootsolval);
222  break;
224  if ( SCIPisFeasPositive(scip, candsol) )
225  distquot = (1.0 - candsfrac + SCIPsumepsilon(scip)) / (candsol - rootsolval);
226  else
227  distquot = (candsfrac + SCIPsumepsilon(scip)) / (rootsolval - candsol);
228  break;
229  default:
230  SCIPerrorMessage("Error: Unsupported diving type\n");
231  SCIPABORT();
232  return SCIP_INVALIDDATA; /*lint !e527*/
233  } /*lint !e788*/
234 
235  /* avoid roundable candidates */
236  if( SCIPvarMayRoundUp(cand) )
237  distquot *= 1000.0;
238  *roundup = TRUE;
239  }
240  else
241  {
242  /* if the solution values are equal, we arbitrarily select branching downwards;
243  * candidates with equal LP solution values are penalized with an infinite score
244  */
245  *roundup = FALSE;
246  distquot = SCIPinfinity(scip);
247  }
248 
249  *score = -distquot;
250 
251  return SCIP_OKAY;
252 }
253 
254 /*
255  * primal heuristic specific interface methods
256  */
257 
258 /** creates the linesearchdiving primal heuristic and includes it in SCIP */
260  SCIP* scip /**< SCIP data structure */
261  )
262 {
263  SCIP_HEURDATA* heurdata;
264  SCIP_HEUR* heur;
265 
266  /* create Linesearchdiving primal heuristic data */
267  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
268 
269  /* include primal heuristic */
270  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
272  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecLinesearchdiving, heurdata) );
273 
274  assert(heur != NULL);
275 
276  /* set non-NULL pointers to callback methods */
277  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyLinesearchdiving) );
278  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeLinesearchdiving) );
279  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitLinesearchdiving) );
280  SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitLinesearchdiving) );
281 
282  /* create a diveset (this will automatically install some additional parameters for the heuristic)*/
286  DEFAULT_BACKTRACK, DEFAULT_ONLYLPBRANCHCANDS, DIVESET_DIVETYPES, divesetGetScoreLinesearchdiving) );
287 
288  return SCIP_OKAY;
289 }
#define DEFAULT_LPRESOLVEDOMCHGQUOT
#define HEUR_FREQOFS
#define DEFAULT_MAXLPITEROFS
#define HEUR_NAME
#define NULL
Definition: def.h:239
#define HEUR_FREQ
public methods for memory management
static SCIP_DECL_DIVESETGETSCORE(divesetGetScoreLinesearchdiving)
static SCIP_DECL_HEURFREE(heurFreeLinesearchdiving)
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_Real SCIPvarGetRootSol(SCIP_VAR *var)
Definition: var.c:12827
#define FALSE
Definition: def.h:65
SCIP_Real SCIPinfinity(SCIP *scip)
#define TRUE
Definition: def.h:64
#define HEUR_DISPCHAR
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
methods commonly used by primal heuristics
#define DEFAULT_MINRELDEPTH
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
static SCIP_DECL_HEUREXIT(heurExitLinesearchdiving)
static SCIP_DECL_HEURCOPY(heurCopyLinesearchdiving)
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1175
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip_mem.h:97
static SCIP_DECL_HEURINIT(heurInitLinesearchdiving)
public methods for numerical tolerances
#define HEUR_MAXDEPTH
#define DEFAULT_MAXLPITERQUOT
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1254
#define SCIPerrorMessage
Definition: pub_message.h:45
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:248
SCIP_Bool SCIPisLT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
#define DEFAULT_RANDSEED
SCIP_SOL * sol
Definition: struct_heur.h:41
#define DEFAULT_MAXRELDEPTH
#define SCIP_CALL(x)
Definition: def.h:351
#define HEUR_DESC
int SCIPheurGetNDivesets(SCIP_HEUR *heur)
Definition: heur.c:1462
#define HEUR_PRIORITY
public methods for primal heuristic plugins and divesets
#define HEUR_USESSUBSCIP
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
#define DEFAULT_BACKTRACK
#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
LP diving heuristic that fixes variables with a large difference to their root solution.
SCIP_Bool SCIPisGT(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
#define DEFAULT_LPSOLVEFREQ
public methods for solutions
static SCIP_DECL_HEUREXEC(heurExecLinesearchdiving)
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)
#define DEFAULT_ONLYLPBRANCHCANDS
#define SCIP_Real
Definition: def.h:150
#define DEFAULT_MAXDIVEUBQUOT
#define DEFAULT_MAXDIVEUBQUOTNOSOL
#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
SCIP_RETCODE SCIPincludeHeurLinesearchdiving(SCIP *scip)
SCIP_Real SCIPsumepsilon(SCIP *scip)
#define DIVESET_DIVETYPES
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:323
#define HEUR_TIMING
#define DEFAULT_MAXDIVEAVGQUOT
SCIP_Bool SCIPvarMayRoundUp(SCIP_VAR *var)
Definition: var.c:3330
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