Scippy

SCIP

Solving Constraint Integer Programs

heur_localbranching.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-2017 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 email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file heur_localbranching.c
17  * @brief Local branching heuristic according to Fischetti and Lodi
18  * @author Timo Berthold
19  * @author Marc Pfetsch
20  */
21 
22 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
23 
24 #include <assert.h>
25 #include <string.h>
26 #include "scip/scip.h"
27 #include "scip/cons_linear.h"
28 #include "scip/scipdefplugins.h"
30 #include "scip/pub_misc.h"
31 
32 #define HEUR_NAME "localbranching"
33 #define HEUR_DESC "local branching heuristic by Fischetti and Lodi"
34 #define HEUR_DISPCHAR 'L'
35 #define HEUR_PRIORITY -1102000
36 #define HEUR_FREQ -1
37 #define HEUR_FREQOFS 0
38 #define HEUR_MAXDEPTH -1
39 #define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
40 #define HEUR_USESSUBSCIP TRUE /**< does the heuristic use a secondary SCIP instance? */
41 
42 #define DEFAULT_NEIGHBORHOODSIZE 18 /* radius of the incumbents neighborhood to be searched */
43 #define DEFAULT_NODESOFS 1000 /* number of nodes added to the contingent of the total nodes */
44 #define DEFAULT_MAXNODES 10000 /* maximum number of nodes to regard in the subproblem */
45 #define DEFAULT_MINIMPROVE 0.01 /* factor by which localbranching should at least improve the incumbent */
46 #define DEFAULT_MINNODES 1000 /* minimum number of nodes required to start the subproblem */
47 #define DEFAULT_NODESQUOT 0.05 /* contingent of sub problem nodes in relation to original nodes */
48 #define DEFAULT_LPLIMFAC 1.5 /* factor by which the limit on the number of LP depends on the node limit */
49 #define DEFAULT_NWAITINGNODES 200 /* number of nodes without incumbent change that heuristic should wait */
50 #define DEFAULT_USELPROWS FALSE /* should subproblem be created out of the rows in the LP rows,
51  * otherwise, the copy constructors of the constraints handlers are used */
52 #define DEFAULT_COPYCUTS TRUE /* if DEFAULT_USELPROWS is FALSE, then should all active cuts from the cutpool
53  * of the original scip be copied to constraints of the subscip
54  */
55 #define DEFAULT_BESTSOLLIMIT 3 /* limit on number of improving incumbent solutions in sub-CIP */
56 #define DEFAULT_USEUCT FALSE /* should uct node selection be used at the beginning of the search? */
57 
58 /* event handler properties */
59 #define EVENTHDLR_NAME "Localbranching"
60 #define EVENTHDLR_DESC "LP event handler for " HEUR_NAME " heuristic"
61 
62 
63 #define EXECUTE 0
64 #define WAITFORNEWSOL 1
65 
66 
67 /*
68  * Data structures
69  */
70 
71 /** primal heuristic data */
72 struct SCIP_HeurData
73 {
74  int nwaitingnodes; /**< number of nodes without incumbent change that heuristic should wait */
75  int nodesofs; /**< number of nodes added to the contingent of the total nodes */
76  int minnodes; /**< minimum number of nodes required to start the subproblem */
77  int maxnodes; /**< maximum number of nodes to regard in the subproblem */
78  SCIP_Longint usednodes; /**< amount of nodes local branching used during all calls */
79  SCIP_Real nodesquot; /**< contingent of sub problem nodes in relation to original nodes */
80  SCIP_Real minimprove; /**< factor by which localbranching should at least improve the incumbent */
81  SCIP_Real nodelimit; /**< the nodelimit employed in the current sub-SCIP, for the event handler*/
82  SCIP_Real lplimfac; /**< factor by which the limit on the number of LP depends on the node limit */
83  int neighborhoodsize; /**< radius of the incumbent's neighborhood to be searched */
84  int callstatus; /**< current status of localbranching heuristic */
85  SCIP_SOL* lastsol; /**< the last incumbent localbranching used as reference point */
86  int curneighborhoodsize;/**< current neighborhoodsize */
87  int curminnodes; /**< current minimal number of nodes required to start the subproblem */
88  int emptyneighborhoodsize;/**< size of neighborhood that was proven to be empty */
89  SCIP_Bool uselprows; /**< should subproblem be created out of the rows in the LP rows? */
90  SCIP_Bool copycuts; /**< if uselprows == FALSE, should all active cuts from cutpool be copied
91  * to constraints in subproblem?
92  */
93  int bestsollimit; /**< limit on number of improving incumbent solutions in sub-CIP */
94  SCIP_Bool useuct; /**< should uct node selection be used at the beginning of the search? */
95 };
96 
97 
98 /*
99  * Local methods
100  */
101 
102 /** create the extra constraint of local branching and add it to subscip */
103 static
105  SCIP* scip, /**< SCIP data structure of the original problem */
106  SCIP* subscip, /**< SCIP data structure of the subproblem */
107  SCIP_VAR** subvars, /**< variables of the subproblem */
108  SCIP_HEURDATA* heurdata /**< heuristic's data structure */
109  )
110 {
111  SCIP_CONS* cons; /* local branching constraint to create */
112  SCIP_VAR** consvars;
113  SCIP_VAR** vars;
114  SCIP_SOL* bestsol;
115 
116  int nbinvars;
117  int i;
118  SCIP_Real lhs;
119  SCIP_Real rhs;
120  SCIP_Real* consvals;
121  char consname[SCIP_MAXSTRLEN];
122 
123  (void) SCIPsnprintf(consname, SCIP_MAXSTRLEN, "%s_localbranchcons", SCIPgetProbName(scip));
124 
125  /* get the data of the variables and the best solution */
126  SCIP_CALL( SCIPgetVarsData(scip, &vars, NULL, &nbinvars, NULL, NULL, NULL) );
127  bestsol = SCIPgetBestSol(scip);
128  assert( bestsol != NULL );
129 
130  /* memory allocation */
131  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nbinvars) );
132  SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nbinvars) );
133 
134  /* set initial left and right hand sides of local branching constraint */
135  lhs = (SCIP_Real)heurdata->emptyneighborhoodsize + 1.0;
136  rhs = (SCIP_Real)heurdata->curneighborhoodsize;
137 
138  /* create the distance (to incumbent) function of the binary variables */
139  for( i = 0; i < nbinvars; i++ )
140  {
141  SCIP_Real solval;
142 
143  solval = SCIPgetSolVal(scip, bestsol, vars[i]);
144  assert( SCIPisFeasIntegral(scip,solval) );
145 
146  /* is variable i part of the binary support of bestsol? */
147  if( SCIPisFeasEQ(scip,solval,1.0) )
148  {
149  consvals[i] = -1.0;
150  rhs -= 1.0;
151  lhs -= 1.0;
152  }
153  else
154  consvals[i] = 1.0;
155  consvars[i] = subvars[i];
156  assert( SCIPvarGetType(consvars[i]) == SCIP_VARTYPE_BINARY );
157  }
158 
159  /* creates localbranching constraint and adds it to subscip */
160  SCIP_CALL( SCIPcreateConsLinear(subscip, &cons, consname, nbinvars, consvars, consvals,
161  lhs, rhs, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE) );
162  SCIP_CALL( SCIPaddCons(subscip, cons) );
163  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
164 
165  /* free local memory */
166  SCIPfreeBufferArray(scip, &consvals);
167  SCIPfreeBufferArray(scip, &consvars);
168 
169  return SCIP_OKAY;
170 }
171 
172 
173 /** creates a new solution for the original problem by copying the solution of the subproblem */
174 static
176  SCIP* scip, /**< SCIP data structure of the original problem */
177  SCIP* subscip, /**< SCIP data structure of the subproblem */
178  SCIP_VAR** subvars, /**< the variables of the subproblem */
179  SCIP_HEUR* heur, /**< the Localbranching heuristic */
180  SCIP_SOL* subsol, /**< solution of the subproblem */
181  SCIP_Bool* success /**< pointer to store, whether new solution was found */
182  )
183 {
184  SCIP_VAR** vars;
185  int nvars;
186  SCIP_SOL* newsol;
187  SCIP_Real* subsolvals;
188 
189  assert( scip != NULL );
190  assert( subscip != NULL );
191  assert( subvars != NULL );
192  assert( subsol != NULL );
193 
194  /* copy the solution */
195  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
196  /* sub-SCIP may have more variables than the number of active (transformed) variables in the main SCIP
197  * since constraint copying may have required the copy of variables that are fixed in the main SCIP
198  */
199  assert(nvars <= SCIPgetNOrigVars(subscip));
200 
201  SCIP_CALL( SCIPallocBufferArray(scip, &subsolvals, nvars) );
202 
203  /* copy the solution */
204  SCIP_CALL( SCIPgetSolVals(subscip, subsol, nvars, subvars, subsolvals) );
205 
206  /* create new solution for the original problem */
207  SCIP_CALL( SCIPcreateSol(scip, &newsol, heur) );
208  SCIP_CALL( SCIPsetSolVals(scip, newsol, nvars, vars, subsolvals) );
209 
210  SCIP_CALL( SCIPtrySolFree(scip, &newsol, FALSE, FALSE, TRUE, TRUE, TRUE, success) );
211 
212  SCIPfreeBufferArray(scip, &subsolvals);
213 
214  return SCIP_OKAY;
215 }
216 
217 
218 /* ---------------- Callback methods of event handler ---------------- */
219 
220 /* exec the event handler
221  *
222  * we interrupt the solution process
223  */
224 static
225 SCIP_DECL_EVENTEXEC(eventExecLocalbranching)
226 {
227  SCIP_HEURDATA* heurdata;
228 
229  assert(eventhdlr != NULL);
230  assert(eventdata != NULL);
231  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
232  assert(event != NULL);
233  assert(SCIPeventGetType(event) & SCIP_EVENTTYPE_LPSOLVED);
234 
235  heurdata = (SCIP_HEURDATA*)eventdata;
236  assert(heurdata != NULL);
237 
238  /* interrupt solution process of sub-SCIP */
239  if( SCIPgetNLPs(scip) > heurdata->lplimfac * heurdata->nodelimit )
240  {
241  SCIPdebugMsg(scip, "interrupt after %" SCIP_LONGINT_FORMAT " LPs\n",SCIPgetNLPs(scip));
243  }
244 
245  return SCIP_OKAY;
246 }
247 
248 
249 /*
250  * Callback methods of primal heuristic
251  */
252 
253 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
254 static
255 SCIP_DECL_HEURCOPY(heurCopyLocalbranching)
256 { /*lint --e{715}*/
257  assert(scip != NULL);
258  assert(heur != NULL);
259  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
260 
261  /* call inclusion method of primal heuristic */
263 
264  return SCIP_OKAY;
265 }
266 
267 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
268 static
269 SCIP_DECL_HEURFREE(heurFreeLocalbranching)
270 { /*lint --e{715}*/
271  SCIP_HEURDATA* heurdata;
272 
273  assert( heur != NULL );
274  assert( scip != NULL );
275 
276  /* get heuristic data */
277  heurdata = SCIPheurGetData(heur);
278  assert( heurdata != NULL );
279 
280  /* free heuristic data */
281  SCIPfreeBlockMemory(scip, &heurdata);
282  SCIPheurSetData(heur, NULL);
283 
284  return SCIP_OKAY;
285 }
286 
287 
288 /** initialization method of primal heuristic (called after problem was transformed) */
289 static
290 SCIP_DECL_HEURINIT(heurInitLocalbranching)
291 { /*lint --e{715}*/
292  SCIP_HEURDATA* heurdata;
293 
294  assert( heur != NULL );
295  assert( scip != NULL );
296 
297  /* get heuristic's data */
298  heurdata = SCIPheurGetData(heur);
299  assert( heurdata != NULL );
300 
301  /* with a little abuse we initialize the heurdata as if localbranching would have finished its last step regularly */
302  heurdata->callstatus = WAITFORNEWSOL;
303  heurdata->lastsol = NULL;
304  heurdata->usednodes = 0;
305  heurdata->curneighborhoodsize = heurdata->neighborhoodsize;
306  heurdata->curminnodes = heurdata->minnodes;
307  heurdata->emptyneighborhoodsize = 0;
308 
309  return SCIP_OKAY;
310 }
311 
312 
313 /** execution method of primal heuristic */
314 static
315 SCIP_DECL_HEUREXEC(heurExecLocalbranching)
316 { /*lint --e{715}*/
317  SCIP_Longint maxnnodes; /* maximum number of subnodes */
318  SCIP_Longint nsubnodes; /* nodelimit for subscip */
319 
320  SCIP_HEURDATA* heurdata;
321  SCIP* subscip; /* the subproblem created by localbranching */
322  SCIP_VAR** subvars; /* subproblem's variables */
323  SCIP_SOL* bestsol; /* best solution so far */
324  SCIP_EVENTHDLR* eventhdlr; /* event handler for LP events */
325 
326  SCIP_Real cutoff; /* objective cutoff for the subproblem */
327  SCIP_Real upperbound;
328 
329  SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */
330  SCIP_VAR** vars;
331 
332  int nvars;
333  int i;
334 
335  SCIP_Bool success;
336 
337  assert(heur != NULL);
338  assert(scip != NULL);
339  assert(result != NULL);
340 
341  *result = SCIP_DIDNOTRUN;
342 
343  /* get heuristic's data */
344  heurdata = SCIPheurGetData(heur);
345  assert( heurdata != NULL );
346 
347  /* there should be enough binary variables that a local branching constraint makes sense */
348  if( SCIPgetNBinVars(scip) < 2*heurdata->neighborhoodsize )
349  return SCIP_OKAY;
350 
351  *result = SCIP_DELAYED;
352 
353  /* only call heuristic, if an IP solution is at hand */
354  if( SCIPgetNSols(scip) <= 0 )
355  return SCIP_OKAY;
356 
357  bestsol = SCIPgetBestSol(scip);
358  assert(bestsol != NULL);
359 
360  /* only call heuristic, if the best solution comes from transformed problem */
361  if( SCIPsolIsOriginal(bestsol) )
362  return SCIP_OKAY;
363 
364  /* only call heuristic, if enough nodes were processed since last incumbent */
365  if( SCIPgetNNodes(scip) - SCIPgetSolNodenum(scip, bestsol) < heurdata->nwaitingnodes)
366  return SCIP_OKAY;
367 
368  /* only call heuristic, if the best solution does not come from trivial heuristic */
369  if( SCIPsolGetHeur(bestsol) != NULL && strcmp(SCIPheurGetName(SCIPsolGetHeur(bestsol)), "trivial") == 0 )
370  return SCIP_OKAY;
371 
372  /* reset neighborhood and minnodes, if new solution was found */
373  if( heurdata->lastsol != bestsol )
374  {
375  heurdata->curneighborhoodsize = heurdata->neighborhoodsize;
376  heurdata->curminnodes = heurdata->minnodes;
377  heurdata->emptyneighborhoodsize = 0;
378  heurdata->callstatus = EXECUTE;
379  heurdata->lastsol = bestsol;
380  }
381 
382  /* if no new solution was found and local branching also seems to fail, just keep on waiting */
383  if( heurdata->callstatus == WAITFORNEWSOL )
384  return SCIP_OKAY;
385 
386  *result = SCIP_DIDNOTRUN;
387 
388  /* calculate the maximal number of branching nodes until heuristic is aborted */
389  maxnnodes = (SCIP_Longint)(heurdata->nodesquot * SCIPgetNNodes(scip));
390 
391  /* reward local branching if it succeeded often */
392  maxnnodes = (SCIP_Longint)(maxnnodes * (1.0 + 2.0*(SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur)+1.0)));
393  maxnnodes -= 100 * SCIPheurGetNCalls(heur); /* count the setup costs for the sub-MIP as 100 nodes */
394  maxnnodes += heurdata->nodesofs;
395 
396  /* determine the node limit for the current process */
397  nsubnodes = maxnnodes - heurdata->usednodes;
398  nsubnodes = MIN(nsubnodes, heurdata->maxnodes);
399 
400  /* check whether we have enough nodes left to call sub problem solving */
401  if( nsubnodes < heurdata->curminnodes )
402  return SCIP_OKAY;
403 
404  if( SCIPisStopped(scip) )
405  return SCIP_OKAY;
406 
407  /* check whether there is enough time and memory left */
408  SCIP_CALL( SCIPcheckCopyLimits(scip, &success) );
409 
410  /* abort if no time is left or not enough memory to create a copy of SCIP */
411  if( !success )
412  return SCIP_OKAY;
413 
414  *result = SCIP_DIDNOTFIND;
415 
416  SCIPdebugMsg(scip, "running localbranching heuristic ...\n");
417 
418  /* get the data of the variables and the best solution */
419  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
420 
421  /* initializing the subproblem */
422  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
423  SCIP_CALL( SCIPcreate(&subscip) );
424 
425  /* create the variable mapping hash map */
426  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), nvars) );
427  success = FALSE;
428 
429  /* create a problem copy as sub SCIP */
430  SCIP_CALL( SCIPcopyLargeNeighborhoodSearch(scip, subscip, varmapfw, "localbranching", NULL, NULL, 0, heurdata->uselprows,
431  heurdata->copycuts, &success, NULL) );
432 
433  /* create event handler for LP events */
434  eventhdlr = NULL;
435  SCIP_CALL( SCIPincludeEventhdlrBasic(subscip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecLocalbranching, NULL) );
436  if( eventhdlr == NULL )
437  {
438  /* free hash map */
439  SCIPhashmapFree(&varmapfw);
440 
441  SCIPerrorMessage("event handler for " HEUR_NAME " heuristic not found.\n");
442  return SCIP_PLUGINNOTFOUND;
443  }
444  SCIPdebugMsg(scip, "Copying the plugins was %ssuccessful.\n", success ? "" : "not ");
445 
446  for (i = 0; i < nvars; ++i)
447  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
448 
449  /* free hash map */
450  SCIPhashmapFree(&varmapfw);
451 
452  /* if the subproblem could not be created, free memory and return */
453  if( !success )
454  {
455  *result = SCIP_DIDNOTRUN;
456  goto TERMINATE;
457  }
458 
459  /* do not abort subproblem on CTRL-C */
460  SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
461 
462 #ifdef SCIP_DEBUG
463  /* for debugging, enable full output */
464  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 5) );
465  SCIP_CALL( SCIPsetIntParam(subscip, "display/freq", 100000000) );
466 #else
467  /* disable statistic timing inside sub SCIP and output to console */
468  SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
469  SCIP_CALL( SCIPsetBoolParam(subscip, "timing/statistictiming", FALSE) );
470 #endif
471 
472  /* set limits for the subproblem */
473  SCIP_CALL( SCIPcopyLimits(scip, subscip) );
474  heurdata->nodelimit = nsubnodes;
475  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", nsubnodes) );
476  SCIP_CALL( SCIPsetLongintParam(subscip, "limits/stallnodes", MAX(10, nsubnodes/10)) );
477  SCIP_CALL( SCIPsetIntParam(subscip, "limits/bestsol", heurdata->bestsollimit) );
478 
479  /* forbid recursive call of heuristics and separators solving subMIPs */
480  SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
481 
482  /* disable cutting plane separation */
484 
485  /* disable expensive presolving */
487 
488  /* use best estimate node selection */
489  if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
490  {
491  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
492  }
493 
494  /* activate uct node selection at the top of the tree */
495  if( heurdata->useuct && SCIPfindNodesel(subscip, "uct") != NULL && !SCIPisParamFixed(subscip, "nodeselection/uct/stdpriority") )
496  {
497  SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/uct/stdpriority", INT_MAX/2) );
498  }
499 
500  /* use inference branching */
501  if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
502  {
503  SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
504  }
505 
506  /* enable conflict analysis and restrict conflict pool */
507  if( !SCIPisParamFixed(subscip, "conflict/enable") )
508  {
509  SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/enable", TRUE) );
510  }
511  if( !SCIPisParamFixed(subscip, "conflict/maxstoresize") )
512  {
513  SCIP_CALL( SCIPsetIntParam(subscip, "conflict/maxstoresize", 100) );
514  }
515 
516  /* speed up sub-SCIP by not checking dual LP feasibility */
517  SCIP_CALL( SCIPsetBoolParam(subscip, "lp/checkdualfeas", FALSE) );
518 
519  /* employ a limit on the number of enforcement rounds in the quadratic constraint handler; this fixes the issue that
520  * sometimes the quadratic constraint handler needs hundreds or thousands of enforcement rounds to determine the
521  * feasibility status of a single node without fractional branching candidates by separation (namely for uflquad
522  * instances); however, the solution status of the sub-SCIP might get corrupted by this; hence no deductions shall be
523  * made for the original SCIP
524  */
525  if( SCIPfindConshdlr(subscip, "quadratic") != NULL && !SCIPisParamFixed(subscip, "constraints/quadratic/enfolplimit") )
526  {
527  SCIP_CALL( SCIPsetIntParam(subscip, "constraints/quadratic/enfolplimit", 500) );
528  }
529 
530  SCIP_CALL( addLocalBranchingConstraint(scip, subscip, subvars, heurdata) );
531 
532  /* add an objective cutoff */
534 
535  upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
537  {
538  cutoff = (1-heurdata->minimprove)*SCIPgetUpperbound(scip) + heurdata->minimprove*SCIPgetLowerbound(scip);
539  }
540  else
541  {
542  if( SCIPgetUpperbound ( scip ) >= 0 )
543  cutoff = ( 1 - heurdata->minimprove ) * SCIPgetUpperbound ( scip );
544  else
545  cutoff = ( 1 + heurdata->minimprove ) * SCIPgetUpperbound ( scip );
546  }
547  cutoff = MIN(upperbound, cutoff );
548  SCIP_CALL( SCIPsetObjlimit(subscip, cutoff) );
549 
550  /* catch LP events of sub-SCIP */
551  if( !heurdata->uselprows )
552  {
553  assert(eventhdlr != NULL);
554 
555  SCIP_CALL( SCIPtransformProb(subscip) );
556  SCIP_CALL( SCIPcatchEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, NULL) );
557  }
558 
559  /* solve the subproblem */
560  SCIPdebugMsg(scip, "solving local branching subproblem with neighborhoodsize %d and maxnodes %" SCIP_LONGINT_FORMAT "\n",
561  heurdata->curneighborhoodsize, nsubnodes);
562 
563 
564  /* Errors in solving the subproblem should not kill the overall solving process
565  * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
566  */
567  SCIP_CALL_ABORT( SCIPsolve(subscip) );
568 
569  /* drop LP events of sub-SCIP */
570  if( !heurdata->uselprows )
571  {
572  assert(eventhdlr != NULL);
573 
574  SCIP_CALL( SCIPdropEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, -1) );
575  }
576 
577  /* print solving statistics of subproblem if we are in SCIP's debug mode */
579 
580  heurdata->usednodes += SCIPgetNNodes(subscip);
581  SCIPdebugMsg(scip, "local branching used %" SCIP_LONGINT_FORMAT "/%" SCIP_LONGINT_FORMAT " nodes\n",
582  SCIPgetNNodes(subscip), nsubnodes);
583 
584  /* check, whether a solution was found */
585  if( SCIPgetNSols(subscip) > 0 )
586  {
587  SCIP_SOL** subsols;
588  int nsubsols;
589 
590  /* check, whether a solution was found;
591  * due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
592  */
593  nsubsols = SCIPgetNSols(subscip);
594  subsols = SCIPgetSols(subscip);
595  success = FALSE;
596  for( i = 0; i < nsubsols && !success; ++i )
597  {
598  SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
599  }
600  if( success )
601  {
602  SCIPdebugMsg(scip, "-> accepted solution of value %g\n", SCIPgetSolOrigObj(subscip, subsols[i]));
603  *result = SCIP_FOUNDSOL;
604  }
605  }
606 
607  /* check the status of the sub-MIP */
608  switch( SCIPgetStatus(subscip) )
609  {
610  case SCIP_STATUS_OPTIMAL:
612  heurdata->callstatus = WAITFORNEWSOL; /* new solution will immediately be installed at next call */
613  SCIPdebugMsg(scip, " -> found new solution\n");
614  break;
615 
619  heurdata->callstatus = EXECUTE;
620  heurdata->curneighborhoodsize = (heurdata->emptyneighborhoodsize + heurdata->curneighborhoodsize)/2;
621  heurdata->curminnodes *= 2;
622  SCIPdebugMsg(scip, " -> node limit reached: reduced neighborhood to %d, increased minnodes to %d\n",
623  heurdata->curneighborhoodsize, heurdata->curminnodes);
624  if( heurdata->curneighborhoodsize <= heurdata->emptyneighborhoodsize )
625  {
626  heurdata->callstatus = WAITFORNEWSOL;
627  SCIPdebugMsg(scip, " -> new neighborhood was already proven to be empty: wait for new solution\n");
628  }
629  break;
630 
633  heurdata->emptyneighborhoodsize = heurdata->curneighborhoodsize;
634  heurdata->curneighborhoodsize += heurdata->curneighborhoodsize/2;
635  heurdata->curneighborhoodsize = MAX(heurdata->curneighborhoodsize, heurdata->emptyneighborhoodsize + 2);
636  heurdata->callstatus = EXECUTE;
637  SCIPdebugMsg(scip, " -> neighborhood is empty: increased neighborhood to %d\n", heurdata->curneighborhoodsize);
638  break;
639 
640  case SCIP_STATUS_UNKNOWN:
648  default:
649  heurdata->callstatus = WAITFORNEWSOL;
650  SCIPdebugMsg(scip, " -> unexpected sub-MIP status <%d>: waiting for new solution\n", SCIPgetStatus(subscip));
651  break;
652  }
653 
654  TERMINATE:
655  /* free subproblem */
656  SCIPfreeBufferArray(scip, &subvars);
657  SCIP_CALL( SCIPfree(&subscip) );
658 
659  return SCIP_OKAY;
660 }
661 
662 
663 /*
664  * primal heuristic specific interface methods
665  */
666 
667 /** creates the localbranching primal heuristic and includes it in SCIP */
669  SCIP* scip /**< SCIP data structure */
670  )
671 {
672  SCIP_HEURDATA* heurdata;
673  SCIP_HEUR* heur;
674 
675  /* create Localbranching primal heuristic data */
676  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
677 
678  /* include primal heuristic */
679  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
681  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecLocalbranching, heurdata) );
682 
683  assert(heur != NULL);
684 
685  /* set non-NULL pointers to callback methods */
686  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyLocalbranching) );
687  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeLocalbranching) );
688  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitLocalbranching) );
689 
690  /* add localbranching primal heuristic parameters */
691  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/nodesofs",
692  "number of nodes added to the contingent of the total nodes",
693  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0, INT_MAX, NULL, NULL) );
694 
695  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/neighborhoodsize",
696  "radius (using Manhattan metric) of the incumbent's neighborhood to be searched",
697  &heurdata->neighborhoodsize, FALSE, DEFAULT_NEIGHBORHOODSIZE, 1, INT_MAX, NULL, NULL) );
698 
699  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/nodesquot",
700  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
701  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
702 
703  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/lplimfac",
704  "factor by which the limit on the number of LP depends on the node limit",
705  &heurdata->lplimfac, TRUE, DEFAULT_LPLIMFAC, 1.0, SCIP_REAL_MAX, NULL, NULL) );
706 
707  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/minnodes",
708  "minimum number of nodes required to start the subproblem",
709  &heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0, INT_MAX, NULL, NULL) );
710 
711  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/maxnodes",
712  "maximum number of nodes to regard in the subproblem",
713  &heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0, INT_MAX, NULL, NULL) );
714 
715  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/nwaitingnodes",
716  "number of nodes without incumbent change that heuristic should wait",
717  &heurdata->nwaitingnodes, TRUE, DEFAULT_NWAITINGNODES, 0, INT_MAX, NULL, NULL) );
718 
719  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/minimprove",
720  "factor by which localbranching should at least improve the incumbent",
721  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
722 
723  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/uselprows",
724  "should subproblem be created out of the rows in the LP rows?",
725  &heurdata->uselprows, TRUE, DEFAULT_USELPROWS, NULL, NULL) );
726 
727  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/copycuts",
728  "if uselprows == FALSE, should all active cuts from cutpool be copied to constraints in subproblem?",
729  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
730 
731  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/bestsollimit",
732  "limit on number of improving incumbent solutions in sub-CIP",
733  &heurdata->bestsollimit, FALSE, DEFAULT_BESTSOLLIMIT, -1, INT_MAX, NULL, NULL) );
734 
735  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/useuct",
736  "should uct node selection be used at the beginning of the search?",
737  &heurdata->useuct, TRUE, DEFAULT_USEUCT, NULL, NULL) );
738 
739  return SCIP_OKAY;
740 }
SCIP_Bool SCIPsolIsOriginal(SCIP_SOL *sol)
Definition: sol.c:2299
#define DEFAULT_NODESOFS
#define HEUR_DISPCHAR
#define SCIP_EVENTTYPE_LPSOLVED
Definition: type_event.h:84
SCIP_RETCODE SCIPsetSeparating(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:5130
#define DEFAULT_LPLIMFAC
SCIP_Bool SCIPisFeasEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
Definition: scip.c:46320
#define DEFAULT_NODESQUOT
SCIP_CONSHDLR * SCIPfindConshdlr(SCIP *scip, const char *name)
Definition: scip.c:6576
#define SCIP_MAXSTRLEN
Definition: def.h:225
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
Definition: heur.c:1327
#define HEUR_FREQ
int SCIPgetNOrigVars(SCIP *scip)
Definition: scip.c:12120
static SCIP_RETCODE addLocalBranchingConstraint(SCIP *scip, SCIP *subscip, SCIP_VAR **subvars, SCIP_HEURDATA *heurdata)
#define EVENTHDLR_NAME
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip.c:8561
#define WAITFORNEWSOL
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip.c:11554
SCIP_SOL ** SCIPgetSols(SCIP *scip)
Definition: scip.c:39108
#define FALSE
Definition: def.h:64
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:2764
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:278
#define DEFAULT_NWAITINGNODES
SCIP_RETCODE SCIPcopyLimits(SCIP *sourcescip, SCIP *targetscip)
Definition: scip.c:4164
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:9340
#define TRUE
Definition: def.h:63
#define SCIPdebug(x)
Definition: pub_message.h:74
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:53
SCIP_RETCODE SCIPsetPresolving(SCIP *scip, SCIP_PARAMSETTING paramsetting, SCIP_Bool quiet)
Definition: scip.c:5104
SCIP_BRANCHRULE * SCIPfindBranchrule(SCIP *scip, const char *name)
Definition: scip.c:9219
#define HEUR_FREQOFS
static SCIP_DECL_HEURINIT(heurInitLocalbranching)
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:51
SCIP_RETCODE SCIPincludeHeurLocalbranching(SCIP *scip)
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip.h:21973
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.c:8034
#define DEFAULT_BESTSOLLIMIT
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:2902
static SCIP_DECL_HEUREXEC(heurExecLocalbranching)
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip.h:22003
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip.c:696
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1102
#define DEFAULT_MAXNODES
#define SCIPallocBlockMemory(scip, ptr)
Definition: scip.h:21956
#define SCIPdebugMsg
Definition: scip.h:451
SCIP_RETCODE SCIPaddIntParam(SCIP *scip, const char *name, const char *desc, int *valueptr, SCIP_Bool isadvanced, int defaultvalue, int minvalue, int maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:4237
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
Definition: scip.c:44659
#define EXECUTE
const char * SCIPgetProbName(SCIP *scip)
Definition: scip.c:10759
#define DEFAULT_COPYCUTS
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip.c:15846
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1181
#define SCIPerrorMessage
Definition: pub_message.h:45
SCIP_Bool SCIPisParamFixed(SCIP *scip, const char *name)
Definition: scip.c:4373
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip.c:12459
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip.c:8095
SCIP_RETCODE SCIPgetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:38219
SCIP_RETCODE SCIPsetBoolParam(SCIP *scip, const char *name, SCIP_Bool value)
Definition: scip.c:4602
SCIP_STATUS SCIPgetStatus(SCIP *scip)
Definition: scip.c:921
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip.c:45753
Local branching heuristic according to Fischetti and Lodi.
struct SCIP_EventData SCIP_EVENTDATA
Definition: type_event.h:155
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:2797
#define NULL
Definition: lpi_spx1.cpp:137
SCIP_HEUR * SCIPsolGetHeur(SCIP_SOL *sol)
Definition: sol.c:2382
#define SCIP_CALL(x)
Definition: def.h:316
SCIP_Real SCIPgetLowerbound(SCIP *scip)
Definition: scip.c:42550
#define DEFAULT_NEIGHBORHOODSIZE
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
Definition: heur.c:1307
#define HEUR_TIMING
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip.h:21991
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:61
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip.c:40434
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:959
static SCIP_RETCODE createNewSol(SCIP *scip, SCIP *subscip, SCIP_VAR **subvars, SCIP_HEUR *heur, SCIP_SOL *subsol, SCIP_Bool *success)
SCIP_RETCODE SCIPsetObjlimit(SCIP *scip, SCIP_Real objlimit)
Definition: scip.c:11114
#define DEFAULT_MINIMPROVE
#define HEUR_MAXDEPTH
#define MAX(x, y)
Definition: tclique_def.h:75
SCIP_RETCODE SCIPtrySolFree(SCIP *scip, SCIP_SOL **sol, SCIP_Bool printreason, SCIP_Bool completely, SCIP_Bool checkbounds, SCIP_Bool checkintegrality, SCIP_Bool checklprows, SCIP_Bool *stored)
Definition: scip.c:40070
SCIP_RETCODE SCIPsetIntParam(SCIP *scip, const char *name, int value)
Definition: scip.c:4660
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip.c:40468
#define DEFAULT_USEUCT
int SCIPgetNSols(SCIP *scip)
Definition: scip.c:39059
SCIP_Real SCIPgetSolOrigObj(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:38268
Constraint handler for linear constraints in their most general form, .
static SCIP_DECL_HEURFREE(heurFreeLocalbranching)
#define HEUR_NAME
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
Definition: scip.c:46061
int SCIPgetNBinVars(SCIP *scip)
Definition: scip.c:11725
#define SCIP_REAL_MAX
Definition: def.h:146
SCIP_RETCODE SCIPcreateConsLinear(SCIP *scip, SCIP_CONS **cons, const char *name, int nvars, SCIP_VAR **vars, SCIP_Real *vals, SCIP_Real lhs, SCIP_Real rhs, SCIP_Bool initial, SCIP_Bool separate, SCIP_Bool enforce, SCIP_Bool check, SCIP_Bool propagate, SCIP_Bool local, SCIP_Bool modifiable, SCIP_Bool dynamic, SCIP_Bool removable, SCIP_Bool stickingatnode)
#define HEUR_PRIORITY
static SCIP_DECL_EVENTEXEC(eventExecLocalbranching)
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip.c:39158
#define HEUR_USESSUBSCIP
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip.c:27417
#define HEUR_DESC
SCIP_RETCODE SCIPsetHeurInit(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURINIT((*heurinit)))
Definition: scip.c:8111
SCIP_NODESEL * SCIPfindNodesel(SCIP *scip, const char *name)
Definition: scip.c:8907
SCIP_RETCODE SCIPcopyLargeNeighborhoodSearch(SCIP *sourcescip, SCIP *subscip, SCIP_HASHMAP *varmap, const char *suffix, SCIP_VAR **fixedvars, SCIP_Real *fixedvals, int nfixedvars, SCIP_Bool uselprows, SCIP_Bool copycuts, SCIP_Bool *success, SCIP_Bool *valid)
Definition: heuristics.c:899
#define SCIP_Real
Definition: def.h:145
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip.c:1138
#define EVENTHDLR_DESC
#define MIN(x, y)
Definition: memory.c:75
#define SCIP_Longint
Definition: def.h:130
static SCIP_DECL_HEURCOPY(heurCopyLocalbranching)
SCIP_RETCODE SCIPcheckCopyLimits(SCIP *sourcescip, SCIP_Bool *success)
Definition: scip.c:4128
SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition: var.c:16720
SCIP_RETCODE SCIPsetSolVals(SCIP *scip, SCIP_SOL *sol, int nvars, SCIP_VAR **vars, SCIP_Real *vals)
Definition: scip.c:38084
SCIP_RETCODE SCIPtransformProb(SCIP *scip)
Definition: scip.c:13717
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip.c:8079
SCIP_Bool SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
Definition: scip.c:46421
SCIP_Real SCIPsumepsilon(SCIP *scip)
Definition: scip.c:45494
SCIP_RETCODE SCIPinterruptSolve(SCIP *scip)
Definition: scip.c:17019
SCIP_Real SCIPgetUpperbound(SCIP *scip)
Definition: scip.c:42699
#define DEFAULT_USELPROWS
#define SCIP_CALL_ABORT(x)
Definition: def.h:295
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1092
SCIP_Longint SCIPgetNNodes(SCIP *scip)
Definition: scip.c:41409
SCIP_Longint SCIPgetNLPs(SCIP *scip)
Definition: scip.c:41590
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip.c:38182
default SCIP plugins
#define DEFAULT_MINNODES
SCIP_RETCODE SCIPaddRealParam(SCIP *scip, const char *name, const char *desc, SCIP_Real *valueptr, SCIP_Bool isadvanced, SCIP_Real defaultvalue, SCIP_Real minvalue, SCIP_Real maxvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:4293
SCIP_RETCODE SCIPsetSubscipsOff(SCIP *scip, SCIP_Bool quiet)
Definition: scip.c:5055
SCIP_RETCODE SCIPsetLongintParam(SCIP *scip, const char *name, SCIP_Longint value)
Definition: scip.c:4718
SCIP callable library.
SCIP_RETCODE SCIPaddBoolParam(SCIP *scip, const char *name, const char *desc, SCIP_Bool *valueptr, SCIP_Bool isadvanced, SCIP_Bool defaultvalue, SCIP_DECL_PARAMCHGD((*paramchgd)), SCIP_PARAMDATA *paramdata)
Definition: scip.c:4211
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip.c:774
SCIP_RETCODE SCIPcreateSol(SCIP *scip, SCIP_SOL **sol, SCIP_HEUR *heur)
Definition: scip.c:37178
SCIP_Longint SCIPgetSolNodenum(SCIP *scip, SCIP_SOL *sol)
Definition: scip.c:38483