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