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-2020 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_localbranching.c
17  * @ingroup DEFPLUGINS_HEUR
18  * @brief Local branching heuristic according to Fischetti and Lodi
19  * @author Timo Berthold
20  * @author Marc Pfetsch
21  */
22 
23 /*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
24 
25 #include "blockmemshell/memory.h"
26 #include "scip/cons_linear.h"
27 #include "scip/heuristics.h"
29 #include "scip/pub_event.h"
30 #include "scip/pub_heur.h"
31 #include "scip/pub_message.h"
32 #include "scip/pub_misc.h"
33 #include "scip/pub_sol.h"
34 #include "scip/pub_var.h"
35 #include "scip/scip_branch.h"
36 #include "scip/scip_cons.h"
37 #include "scip/scip_copy.h"
38 #include "scip/scip_event.h"
39 #include "scip/scip_general.h"
40 #include "scip/scip_heur.h"
41 #include "scip/scip_mem.h"
42 #include "scip/scip_message.h"
43 #include "scip/scip_nodesel.h"
44 #include "scip/scip_numerics.h"
45 #include "scip/scip_param.h"
46 #include "scip/scip_prob.h"
47 #include "scip/scip_sol.h"
48 #include "scip/scip_solve.h"
49 #include "scip/scip_solvingstats.h"
50 #include <string.h>
51 
52 #define HEUR_NAME "localbranching"
53 #define HEUR_DESC "local branching heuristic by Fischetti and Lodi"
54 #define HEUR_DISPCHAR SCIP_HEURDISPCHAR_LNS
55 #define HEUR_PRIORITY -1102000
56 #define HEUR_FREQ -1
57 #define HEUR_FREQOFS 0
58 #define HEUR_MAXDEPTH -1
59 #define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
60 #define HEUR_USESSUBSCIP TRUE /**< does the heuristic use a secondary SCIP instance? */
61 
62 #define DEFAULT_NEIGHBORHOODSIZE 18 /* radius of the incumbents neighborhood to be searched */
63 #define DEFAULT_NODESOFS 1000 /* number of nodes added to the contingent of the total nodes */
64 #define DEFAULT_MAXNODES 10000 /* maximum number of nodes to regard in the subproblem */
65 #define DEFAULT_MINIMPROVE 0.01 /* factor by which localbranching should at least improve the incumbent */
66 #define DEFAULT_MINNODES 1000 /* minimum number of nodes required to start the subproblem */
67 #define DEFAULT_NODESQUOT 0.05 /* contingent of sub problem nodes in relation to original nodes */
68 #define DEFAULT_LPLIMFAC 1.5 /* factor by which the limit on the number of LP depends on the node limit */
69 #define DEFAULT_NWAITINGNODES 200 /* number of nodes without incumbent change that heuristic should wait */
70 #define DEFAULT_USELPROWS FALSE /* should subproblem be created out of the rows in the LP rows,
71  * otherwise, the copy constructors of the constraints handlers are used */
72 #define DEFAULT_COPYCUTS TRUE /* if DEFAULT_USELPROWS is FALSE, then should all active cuts from the cutpool
73  * of the original scip be copied to constraints of the subscip
74  */
75 #define DEFAULT_BESTSOLLIMIT 3 /* limit on number of improving incumbent solutions in sub-CIP */
76 
77 /* event handler properties */
78 #define EVENTHDLR_NAME "Localbranching"
79 #define EVENTHDLR_DESC "LP event handler for " HEUR_NAME " heuristic"
80 
81 
82 #define EXECUTE 0
83 #define WAITFORNEWSOL 1
84 
85 
86 /*
87  * Data structures
88  */
89 
90 /** primal heuristic data */
91 struct SCIP_HeurData
92 {
93  int nwaitingnodes; /**< number of nodes without incumbent change that heuristic should wait */
94  int nodesofs; /**< number of nodes added to the contingent of the total nodes */
95  int minnodes; /**< minimum number of nodes required to start the subproblem */
96  int maxnodes; /**< maximum number of nodes to regard in the subproblem */
97  SCIP_Longint usednodes; /**< amount of nodes local branching used during all calls */
98  SCIP_Real nodesquot; /**< contingent of sub problem nodes in relation to original nodes */
99  SCIP_Real minimprove; /**< factor by which localbranching should at least improve the incumbent */
100  SCIP_Real nodelimit; /**< the nodelimit employed in the current sub-SCIP, for the event handler*/
101  SCIP_Real lplimfac; /**< factor by which the limit on the number of LP depends on the node limit */
102  int neighborhoodsize; /**< radius of the incumbent's neighborhood to be searched */
103  int callstatus; /**< current status of localbranching heuristic */
104  SCIP_SOL* lastsol; /**< the last incumbent localbranching used as reference point */
105  int curneighborhoodsize;/**< current neighborhoodsize */
106  int curminnodes; /**< current minimal number of nodes required to start the subproblem */
107  int emptyneighborhoodsize;/**< size of neighborhood that was proven to be empty */
108  SCIP_Bool uselprows; /**< should subproblem be created out of the rows in the LP rows? */
109  SCIP_Bool copycuts; /**< if uselprows == FALSE, should all active cuts from cutpool be copied
110  * to constraints in subproblem?
111  */
112  int bestsollimit; /**< limit on number of improving incumbent solutions in sub-CIP */
113 };
114 
115 
116 /*
117  * Local methods
118  */
119 
120 /** create the extra constraint of local branching and add it to subscip */
121 static
123  SCIP* scip, /**< SCIP data structure */
124  SCIP* subscip, /**< the subproblem created by localbranching */
125  SCIP_HEUR* heur, /**< the local branching heuristic */
126  SCIP_VAR** subvars /**< the subproblem variables */
127  )
128 {
129  SCIP_HEURDATA* heurdata;
130  SCIP_CONS* cons; /* local branching constraint to create */
131  SCIP_VAR** consvars;
132  SCIP_VAR** vars;
133  SCIP_SOL* bestsol;
134 
135  int nbinvars;
136  int nconsvars;
137  int i;
138  SCIP_Real lhs;
139  SCIP_Real rhs;
140  SCIP_Real* consvals;
141  char consname[SCIP_MAXSTRLEN];
142 
143  SCIP_Real cutoff;
144  SCIP_Real upperbound;
145 
146  assert(scip != NULL);
147  assert(subscip != NULL);
148  assert(heur != NULL);
149 
150  heurdata = SCIPheurGetData(heur);
151  assert(heurdata != NULL);
152 
153  (void) SCIPsnprintf(consname, SCIP_MAXSTRLEN, "%s_localbranchcons", SCIPgetProbName(scip));
154 
155  /* get the data of the variables and the best solution */
156  SCIP_CALL( SCIPgetVarsData(scip, &vars, NULL, &nbinvars, NULL, NULL, NULL) );
157  bestsol = SCIPgetBestSol(scip);
158  assert( bestsol != NULL );
159 
160  /* memory allocation */
161  SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nbinvars) );
162  SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nbinvars) );
163  nconsvars = 0;
164 
165  /* set initial left and right hand sides of local branching constraint */
166  lhs = (SCIP_Real)heurdata->emptyneighborhoodsize + 1.0;
167  rhs = (SCIP_Real)heurdata->curneighborhoodsize;
168 
169  /* create the distance (to incumbent) function of the binary variables */
170  for( i = 0; i < nbinvars; i++ )
171  {
172  SCIP_Real solval;
173 
174  if( subvars[i] == NULL )
175  continue;
176 
177  solval = SCIPgetSolVal(scip, bestsol, vars[i]);
178  assert( SCIPisFeasIntegral(scip, solval) );
179 
180  /* is variable i part of the binary support of bestsol? */
181  if( SCIPisFeasEQ(scip, solval, 1.0) )
182  {
183  consvals[nconsvars] = -1.0;
184  rhs -= 1.0;
185  lhs -= 1.0;
186  }
187  else
188  consvals[nconsvars] = 1.0;
189 
190  consvars[nconsvars] = subvars[i];
191  assert( SCIPvarGetType(consvars[nconsvars]) == SCIP_VARTYPE_BINARY );
192 
193  ++nconsvars;
194  }
195 
196  /* creates localbranching constraint and adds it to subscip */
197  SCIP_CALL( SCIPcreateConsLinear(subscip, &cons, consname, nconsvars, consvars, consvals,
198  lhs, rhs, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE) );
199  SCIP_CALL( SCIPaddCons(subscip, cons) );
200  SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
201 
202  /* add an objective cutoff */
203  assert( !SCIPisInfinity(scip,SCIPgetUpperbound(scip)) );
204 
205  upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
206  if( !SCIPisInfinity(scip,-1.0*SCIPgetLowerbound(scip)) )
207  {
208  cutoff = (1-heurdata->minimprove)*SCIPgetUpperbound(scip) + heurdata->minimprove*SCIPgetLowerbound(scip);
209  }
210  else
211  {
212  if( SCIPgetUpperbound ( scip ) >= 0 )
213  cutoff = ( 1 - heurdata->minimprove ) * SCIPgetUpperbound ( scip );
214  else
215  cutoff = ( 1 + heurdata->minimprove ) * SCIPgetUpperbound ( scip );
216  }
217  cutoff = MIN(upperbound, cutoff );
218  SCIP_CALL( SCIPsetObjlimit(subscip, cutoff) );
219 
220  /* free local memory */
221  SCIPfreeBufferArray(scip, &consvals);
222  SCIPfreeBufferArray(scip, &consvars);
223 
224  return SCIP_OKAY;
225 }
226 
227 
228 /* ---------------- Callback methods of event handler ---------------- */
229 
230 /** event handler execution callback to interrupt the solution process */
231 static
232 SCIP_DECL_EVENTEXEC(eventExecLocalbranching)
233 {
234  SCIP_HEURDATA* heurdata;
235 
236  assert(eventhdlr != NULL);
237  assert(eventdata != NULL);
238  assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
239  assert(event != NULL);
240  assert(SCIPeventGetType(event) & SCIP_EVENTTYPE_LPSOLVED);
241 
242  heurdata = (SCIP_HEURDATA*)eventdata;
243  assert(heurdata != NULL);
244 
245  /* interrupt solution process of sub-SCIP */
246  if( SCIPgetNLPs(scip) > heurdata->lplimfac * heurdata->nodelimit )
247  {
248  SCIPdebugMsg(scip, "interrupt after %" SCIP_LONGINT_FORMAT " LPs\n",SCIPgetNLPs(scip));
250  }
251 
252  return SCIP_OKAY;
253 }
254 
255 
256 /*
257  * Callback methods of primal heuristic
258  */
259 
260 /** copy method for primal heuristic plugins (called when SCIP copies plugins) */
261 static
262 SCIP_DECL_HEURCOPY(heurCopyLocalbranching)
263 { /*lint --e{715}*/
264  assert(scip != NULL);
265  assert(heur != NULL);
266  assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
267 
268  /* call inclusion method of primal heuristic */
270 
271  return SCIP_OKAY;
272 }
273 
274 /** destructor of primal heuristic to free user data (called when SCIP is exiting) */
275 static
276 SCIP_DECL_HEURFREE(heurFreeLocalbranching)
277 { /*lint --e{715}*/
278  SCIP_HEURDATA* heurdata;
279 
280  assert( heur != NULL );
281  assert( scip != NULL );
282 
283  /* get heuristic data */
284  heurdata = SCIPheurGetData(heur);
285  assert( heurdata != NULL );
286 
287  /* free heuristic data */
288  SCIPfreeBlockMemory(scip, &heurdata);
289  SCIPheurSetData(heur, NULL);
290 
291  return SCIP_OKAY;
292 }
293 
294 
295 /** initialization method of primal heuristic (called after problem was transformed) */
296 static
297 SCIP_DECL_HEURINIT(heurInitLocalbranching)
298 { /*lint --e{715}*/
299  SCIP_HEURDATA* heurdata;
300 
301  assert( heur != NULL );
302  assert( scip != NULL );
303 
304  /* get heuristic's data */
305  heurdata = SCIPheurGetData(heur);
306  assert( heurdata != NULL );
307 
308  /* with a little abuse we initialize the heurdata as if localbranching would have finished its last step regularly */
309  heurdata->callstatus = WAITFORNEWSOL;
310  heurdata->lastsol = NULL;
311  heurdata->usednodes = 0;
312  heurdata->curneighborhoodsize = heurdata->neighborhoodsize;
313  heurdata->curminnodes = heurdata->minnodes;
314  heurdata->emptyneighborhoodsize = 0;
315 
316  return SCIP_OKAY;
317 }
318 
319 /** setup And solve local branching subscip */
320 static
322  SCIP* scip, /**< SCIP data structure */
323  SCIP* subscip, /**< the subproblem created by localbranching */
324  SCIP_HEUR* heur, /**< localbranching heuristic */
325  SCIP_Longint nsubnodes, /**< nodelimit for subscip */
326  SCIP_RESULT* result /**< result pointer */
327  )
328 {
329  SCIP_VAR** subvars; /* subproblem's variables */
330  SCIP_EVENTHDLR* eventhdlr; /* event handler for LP events */
331  SCIP_HEURDATA* heurdata;
332  SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */
333  SCIP_VAR** vars;
334 
335  int nvars;
336  int i;
337 
338  SCIP_Bool success;
339 
340  assert(scip != NULL);
341  assert(subscip != NULL);
342  assert(heur != NULL);
343 
344  heurdata = SCIPheurGetData(heur);
345  assert(heurdata != NULL);
346 
347  /* get the data of the variables and the best solution */
348  SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
349 
350  /* create the variable mapping hash map */
351  SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), nvars) );
352  success = FALSE;
353 
354  /* create a problem copy as sub SCIP */
355  SCIP_CALL( SCIPcopyLargeNeighborhoodSearch(scip, subscip, varmapfw, "localbranching", NULL, NULL, 0, heurdata->uselprows,
356  heurdata->copycuts, &success, NULL) );
357 
358  SCIPdebugMsg(scip, "Copying SCIP was %ssuccessful.\n", success ? "" : "not ");
359 
360  /* if the subproblem could not be created, free memory and return */
361  if( !success )
362  {
363  *result = SCIP_DIDNOTRUN;
364  goto TERMINATE;
365  }
366 
367  /* create event handler for LP events */
368  eventhdlr = NULL;
369  SCIP_CALL( SCIPincludeEventhdlrBasic(subscip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecLocalbranching, NULL) );
370  if( eventhdlr == NULL )
371  {
372  /* free hash map */
373  SCIPhashmapFree(&varmapfw);
374 
375  SCIPerrorMessage("event handler for " HEUR_NAME " heuristic not found.\n");
376  return SCIP_PLUGINNOTFOUND;
377  }
378 
379  SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
380  for (i = 0; i < nvars; ++i)
381  subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
382 
383  /* free hash map */
384  SCIPhashmapFree(&varmapfw);
385 
386  heurdata->nodelimit = nsubnodes;
387  SCIP_CALL( SCIPsetCommonSubscipParams(scip, subscip, nsubnodes, MAX(10, nsubnodes/10), heurdata->bestsollimit) );
388 
389  /* adds the local branching constraint and the objective cutoff to the auxiliary problem */
390  SCIP_CALL( addLocalbranchingConstraintAndObjcutoff(scip, subscip, heur, subvars) );
391 
392  /* catch LP events of sub-SCIP */
393  if( !heurdata->uselprows )
394  {
395  assert(eventhdlr != NULL);
396 
397  SCIP_CALL( SCIPtransformProb(subscip) );
398  SCIP_CALL( SCIPcatchEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, NULL) );
399  }
400 
401  /* solve the subproblem */
402  SCIPdebugMsg(scip, "solving local branching subproblem with neighborhoodsize %d and maxnodes %" SCIP_LONGINT_FORMAT "\n",
403  heurdata->curneighborhoodsize, nsubnodes);
404 
405  /* Errors in solving the subproblem should not kill the overall solving process
406  * Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
407  */
408  SCIP_CALL_ABORT( SCIPsolve(subscip) );
409 
410  /* drop LP events of sub-SCIP */
411  if( !heurdata->uselprows )
412  {
413  assert(eventhdlr != NULL);
414 
415  SCIP_CALL( SCIPdropEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, -1) );
416  }
417 
418  /* print solving statistics of subproblem if we are in SCIP's debug mode */
420 
421  heurdata->usednodes += SCIPgetNNodes(subscip);
422  SCIPdebugMsg(scip, "local branching used %" SCIP_LONGINT_FORMAT "/%" SCIP_LONGINT_FORMAT " nodes\n",
423  SCIPgetNNodes(subscip), nsubnodes);
424 
425  /* checks the solutions of the sub SCIP and adds them to the main SCIP if feasible */
426  SCIP_CALL( SCIPtranslateSubSols(scip, subscip, heur, subvars, &success, NULL) );
427 
428  if( success )
429  *result = SCIP_FOUNDSOL;
430 
431  /* check the status of the sub-MIP */
432  switch( SCIPgetStatus(subscip) )
433  {
434  case SCIP_STATUS_OPTIMAL:
436  heurdata->callstatus = WAITFORNEWSOL; /* new solution will immediately be installed at next call */
437  SCIPdebugMsg(scip, " -> found new solution\n");
438  break;
439 
443  heurdata->callstatus = EXECUTE;
444  heurdata->curneighborhoodsize = (heurdata->emptyneighborhoodsize + heurdata->curneighborhoodsize)/2;
445  heurdata->curminnodes *= 2;
446  SCIPdebugMsg(scip, " -> node limit reached: reduced neighborhood to %d, increased minnodes to %d\n",
447  heurdata->curneighborhoodsize, heurdata->curminnodes);
448  if( heurdata->curneighborhoodsize <= heurdata->emptyneighborhoodsize )
449  {
450  heurdata->callstatus = WAITFORNEWSOL;
451  SCIPdebugMsg(scip, " -> new neighborhood was already proven to be empty: wait for new solution\n");
452  }
453  break;
454 
457  heurdata->emptyneighborhoodsize = heurdata->curneighborhoodsize;
458  heurdata->curneighborhoodsize += heurdata->curneighborhoodsize/2;
459  heurdata->curneighborhoodsize = MAX(heurdata->curneighborhoodsize, heurdata->emptyneighborhoodsize + 2);
460  heurdata->callstatus = EXECUTE;
461  SCIPdebugMsg(scip, " -> neighborhood is empty: increased neighborhood to %d\n", heurdata->curneighborhoodsize);
462  break;
463 
464  case SCIP_STATUS_UNKNOWN:
473  default:
474  heurdata->callstatus = WAITFORNEWSOL;
475  SCIPdebugMsg(scip, " -> unexpected sub-MIP status <%d>: waiting for new solution\n", SCIPgetStatus(subscip));
476  break;
477  }
478 
479  TERMINATE:
480  /* free subproblem */
481  SCIPfreeBufferArray(scip, &subvars);
482 
483  return SCIP_OKAY;
484 }
485 
486 
487 /** execution method of primal heuristic */
488 static
489 SCIP_DECL_HEUREXEC(heurExecLocalbranching)
490 { /*lint --e{715}*/
491  SCIP_Longint maxnnodes; /* maximum number of subnodes */
492  SCIP_Longint nsubnodes; /* nodelimit for subscip */
493 
494  SCIP_HEURDATA* heurdata;
495  SCIP* subscip; /* the subproblem created by localbranching */
496 
497  SCIP_SOL* bestsol; /* best solution so far */
498 
499  SCIP_Bool success;
500  SCIP_RETCODE retcode;
501 
502  assert(heur != NULL);
503  assert(scip != NULL);
504  assert(result != NULL);
505 
506  *result = SCIP_DIDNOTRUN;
507 
508  /* get heuristic's data */
509  heurdata = SCIPheurGetData(heur);
510  assert( heurdata != NULL );
511 
512  /* there should be enough binary variables that a local branching constraint makes sense */
513  if( SCIPgetNBinVars(scip) < 2*heurdata->neighborhoodsize )
514  return SCIP_OKAY;
515 
516  *result = SCIP_DELAYED;
517 
518  /* only call heuristic, if an IP solution is at hand */
519  if( SCIPgetNSols(scip) <= 0 )
520  return SCIP_OKAY;
521 
522  bestsol = SCIPgetBestSol(scip);
523  assert(bestsol != NULL);
524 
525  /* only call heuristic, if the best solution comes from transformed problem */
526  if( SCIPsolIsOriginal(bestsol) )
527  return SCIP_OKAY;
528 
529  /* only call heuristic, if enough nodes were processed since last incumbent */
530  if( SCIPgetNNodes(scip) - SCIPgetSolNodenum(scip, bestsol) < heurdata->nwaitingnodes)
531  return SCIP_OKAY;
532 
533  /* only call heuristic, if the best solution does not come from trivial heuristic */
534  if( SCIPsolGetHeur(bestsol) != NULL && strcmp(SCIPheurGetName(SCIPsolGetHeur(bestsol)), "trivial") == 0 )
535  return SCIP_OKAY;
536 
537  /* reset neighborhood and minnodes, if new solution was found */
538  if( heurdata->lastsol != bestsol )
539  {
540  heurdata->curneighborhoodsize = heurdata->neighborhoodsize;
541  heurdata->curminnodes = heurdata->minnodes;
542  heurdata->emptyneighborhoodsize = 0;
543  heurdata->callstatus = EXECUTE;
544  heurdata->lastsol = bestsol;
545  }
546 
547  /* if no new solution was found and local branching also seems to fail, just keep on waiting */
548  if( heurdata->callstatus == WAITFORNEWSOL )
549  return SCIP_OKAY;
550 
551  *result = SCIP_DIDNOTRUN;
552 
553  /* calculate the maximal number of branching nodes until heuristic is aborted */
554  maxnnodes = (SCIP_Longint)(heurdata->nodesquot * SCIPgetNNodes(scip));
555 
556  /* reward local branching if it succeeded often */
557  maxnnodes = (SCIP_Longint)(maxnnodes * (1.0 + 2.0*(SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur)+1.0)));
558  maxnnodes -= 100 * SCIPheurGetNCalls(heur); /* count the setup costs for the sub-MIP as 100 nodes */
559  maxnnodes += heurdata->nodesofs;
560 
561  /* determine the node limit for the current process */
562  nsubnodes = maxnnodes - heurdata->usednodes;
563  nsubnodes = MIN(nsubnodes, heurdata->maxnodes);
564 
565  /* check whether we have enough nodes left to call sub problem solving */
566  if( nsubnodes < heurdata->curminnodes )
567  return SCIP_OKAY;
568 
569  if( SCIPisStopped(scip) )
570  return SCIP_OKAY;
571 
572  /* check whether there is enough time and memory left */
573  SCIP_CALL( SCIPcheckCopyLimits(scip, &success) );
574 
575  /* abort if no time is left or not enough memory to create a copy of SCIP */
576  if( !success )
577  return SCIP_OKAY;
578 
579  *result = SCIP_DIDNOTFIND;
580 
581  SCIPdebugMsg(scip, "running localbranching heuristic ...\n");
582 
583  SCIP_CALL( SCIPcreate(&subscip) );
584 
585  retcode = setupAndSolveSubscipLocalbranching(scip, subscip, heur, nsubnodes, result);
586 
587  SCIP_CALL( SCIPfree(&subscip) );
588 
589  return retcode;
590 }
591 
592 
593 /*
594  * primal heuristic specific interface methods
595  */
596 
597 /** creates the localbranching primal heuristic and includes it in SCIP */
599  SCIP* scip /**< SCIP data structure */
600  )
601 {
602  SCIP_HEURDATA* heurdata;
603  SCIP_HEUR* heur;
604 
605  /* create Localbranching primal heuristic data */
606  SCIP_CALL( SCIPallocBlockMemory(scip, &heurdata) );
607 
608  /* include primal heuristic */
609  SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
611  HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecLocalbranching, heurdata) );
612 
613  assert(heur != NULL);
614 
615  /* set non-NULL pointers to callback methods */
616  SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyLocalbranching) );
617  SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeLocalbranching) );
618  SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitLocalbranching) );
619 
620  /* add localbranching primal heuristic parameters */
621  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/nodesofs",
622  "number of nodes added to the contingent of the total nodes",
623  &heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0, INT_MAX, NULL, NULL) );
624 
625  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/neighborhoodsize",
626  "radius (using Manhattan metric) of the incumbent's neighborhood to be searched",
627  &heurdata->neighborhoodsize, FALSE, DEFAULT_NEIGHBORHOODSIZE, 1, INT_MAX, NULL, NULL) );
628 
629  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/nodesquot",
630  "contingent of sub problem nodes in relation to the number of nodes of the original problem",
631  &heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
632 
633  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/lplimfac",
634  "factor by which the limit on the number of LP depends on the node limit",
635  &heurdata->lplimfac, TRUE, DEFAULT_LPLIMFAC, 1.0, SCIP_REAL_MAX, NULL, NULL) );
636 
637  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/minnodes",
638  "minimum number of nodes required to start the subproblem",
639  &heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0, INT_MAX, NULL, NULL) );
640 
641  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/maxnodes",
642  "maximum number of nodes to regard in the subproblem",
643  &heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0, INT_MAX, NULL, NULL) );
644 
645  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/nwaitingnodes",
646  "number of nodes without incumbent change that heuristic should wait",
647  &heurdata->nwaitingnodes, TRUE, DEFAULT_NWAITINGNODES, 0, INT_MAX, NULL, NULL) );
648 
649  SCIP_CALL( SCIPaddRealParam(scip, "heuristics/" HEUR_NAME "/minimprove",
650  "factor by which localbranching should at least improve the incumbent",
651  &heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
652 
653  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/uselprows",
654  "should subproblem be created out of the rows in the LP rows?",
655  &heurdata->uselprows, TRUE, DEFAULT_USELPROWS, NULL, NULL) );
656 
657  SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/" HEUR_NAME "/copycuts",
658  "if uselprows == FALSE, should all active cuts from cutpool be copied to constraints in subproblem?",
659  &heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
660 
661  SCIP_CALL( SCIPaddIntParam(scip, "heuristics/" HEUR_NAME "/bestsollimit",
662  "limit on number of improving incumbent solutions in sub-CIP",
663  &heurdata->bestsollimit, FALSE, DEFAULT_BESTSOLLIMIT, -1, INT_MAX, NULL, NULL) );
664 
665  return SCIP_OKAY;
666 }
SCIP_RETCODE SCIPsetCommonSubscipParams(SCIP *sourcescip, SCIP *subscip, SCIP_Longint nsubnodes, SCIP_Longint nstallnodes, int bestsollimit)
Definition: scip_copy.c:3269
enum SCIP_Result SCIP_RESULT
Definition: type_result.h:52
#define DEFAULT_NODESOFS
SCIP_Longint SCIPheurGetNCalls(SCIP_HEUR *heur)
Definition: heur.c:1555
#define HEUR_DISPCHAR
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 SCIP_EVENTTYPE_LPSOLVED
Definition: type_event.h:92
SCIP_Bool SCIPisStopped(SCIP *scip)
Definition: scip_general.c:687
SCIP_Real SCIPsumepsilon(SCIP *scip)
#define DEFAULT_LPLIMFAC
const char * SCIPheurGetName(SCIP_HEUR *heur)
Definition: heur.c:1429
public methods for SCIP parameter handling
public methods for node selector plugins
public methods for memory management
#define DEFAULT_NODESQUOT
SCIP_HEURDATA * SCIPheurGetData(SCIP_HEUR *heur)
Definition: heur.c:1340
SCIP_STATUS SCIPgetStatus(SCIP *scip)
Definition: scip_general.c:467
SCIP_RETCODE SCIPinterruptSolve(SCIP *scip)
Definition: scip_solve.c:3407
#define SCIP_MAXSTRLEN
Definition: def.h:273
SCIP_EXPORT SCIP_Bool SCIPsolIsOriginal(SCIP_SOL *sol)
Definition: sol.c:2521
#define HEUR_FREQ
SCIP_Real SCIPgetSolVal(SCIP *scip, SCIP_SOL *sol, SCIP_VAR *var)
Definition: scip_sol.c:1353
public solving methods
#define EVENTHDLR_NAME
SCIP_RETCODE SCIPtransformProb(SCIP *scip)
Definition: scip_solve.c:357
#define WAITFORNEWSOL
SCIP_RETCODE SCIPtranslateSubSols(SCIP *scip, SCIP *subscip, SCIP_HEUR *heur, SCIP_VAR **subvars, SCIP_Bool *success, int *solindex)
Definition: scip_copy.c:1396
const char * SCIPeventhdlrGetName(SCIP_EVENTHDLR *eventhdlr)
Definition: event.c:315
#define FALSE
Definition: def.h:73
#define DEFAULT_NWAITINGNODES
SCIP_EXPORT SCIP_HEUR * SCIPsolGetHeur(SCIP_SOL *sol)
Definition: sol.c:2604
SCIP_EXPORT SCIP_VARTYPE SCIPvarGetType(SCIP_VAR *var)
Definition: var.c:17177
#define TRUE
Definition: def.h:72
#define SCIPdebug(x)
Definition: pub_message.h:84
enum SCIP_Retcode SCIP_RETCODE
Definition: type_retcode.h:54
methods commonly used by primal heuristics
SCIP_RETCODE SCIPsolve(SCIP *scip)
Definition: scip_solve.c:2527
#define HEUR_FREQOFS
static SCIP_DECL_HEURINIT(heurInitLocalbranching)
void * SCIPhashmapGetImage(SCIP_HASHMAP *hashmap, void *origin)
Definition: misc.c:3200
struct SCIP_HeurData SCIP_HEURDATA
Definition: type_heur.h:67
public methods for problem variables
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_param.c:48
#define SCIPfreeBlockMemory(scip, ptr)
Definition: scip_mem.h:95
#define DEFAULT_BESTSOLLIMIT
SCIP_Real SCIPgetUpperbound(SCIP *scip)
static SCIP_DECL_HEUREXEC(heurExecLocalbranching)
#define SCIPfreeBufferArray(scip, ptr)
Definition: scip_mem.h:123
#define DEFAULT_MAXNODES
#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
#define SCIPdebugMsg
Definition: scip_message.h:69
SCIP_Bool SCIPisFeasIntegral(SCIP *scip, SCIP_Real val)
SCIP_RETCODE SCIPcheckCopyLimits(SCIP *sourcescip, SCIP_Bool *success)
Definition: scip_copy.c:3192
SCIP_Longint SCIPheurGetNBestSolsFound(SCIP_HEUR *heur)
Definition: heur.c:1575
SCIP_Bool SCIPisInfinity(SCIP *scip, SCIP_Real val)
#define EXECUTE
public methods for numerical tolerances
SCIP_Longint SCIPgetNLPs(SCIP *scip)
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:916
SCIP_RETCODE SCIPsetObjlimit(SCIP *scip, SCIP_Real objlimit)
Definition: scip_prob.c:1420
public methods for querying solving statistics
int SCIPgetNSols(SCIP *scip)
Definition: scip_sol.c:2206
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
SCIP_Longint SCIPgetNNodes(SCIP *scip)
#define DEFAULT_COPYCUTS
SCIP_RETCODE SCIPcreate(SCIP **scip)
Definition: scip_general.c:283
#define SCIPerrorMessage
Definition: pub_message.h:55
public methods for event handler plugins and event handlers
SCIP_Bool SCIPisFeasEQ(SCIP *scip, SCIP_Real val1, SCIP_Real val2)
SCIP_RETCODE SCIPcatchEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int *filterpos)
Definition: scip_event.c:277
static SCIP_RETCODE setupAndSolveSubscipLocalbranching(SCIP *scip, SCIP *subscip, SCIP_HEUR *heur, SCIP_Longint nsubnodes, SCIP_RESULT *result)
BMS_BLKMEM * SCIPblkmem(SCIP *scip)
Definition: scip_mem.c:48
Local branching heuristic according to Fischetti and Lodi.
struct SCIP_EventData SCIP_EVENTDATA
Definition: type_event.h:164
void SCIPheurSetData(SCIP_HEUR *heur, SCIP_HEURDATA *heurdata)
Definition: heur.c:1350
#define NULL
Definition: lpi_spx1.cpp:155
public methods for problem copies
public methods for primal CIP solutions
#define SCIP_CALL(x)
Definition: def.h:364
#define DEFAULT_NEIGHBORHOODSIZE
SCIP_EVENTTYPE SCIPeventGetType(SCIP_EVENT *event)
Definition: event.c:1021
#define HEUR_TIMING
public methods for primal heuristic plugins and divesets
public methods for constraint handler plugins and constraints
SCIP_Longint SCIPgetSolNodenum(SCIP *scip, SCIP_SOL *sol)
Definition: scip_sol.c:1649
#define SCIPallocBufferArray(scip, ptr, num)
Definition: scip_mem.h:111
public data structures and miscellaneous methods
#define SCIP_Bool
Definition: def.h:70
const char * SCIPgetProbName(SCIP *scip)
Definition: scip_prob.c:1065
SCIP_RETCODE SCIPhashmapCreate(SCIP_HASHMAP **hashmap, BMS_BLKMEM *blkmem, int mapsize)
Definition: misc.c:3013
#define DEFAULT_MINIMPROVE
#define HEUR_MAXDEPTH
#define MAX(x, y)
Definition: tclique_def.h:83
SCIP_RETCODE SCIPprintStatistics(SCIP *scip, FILE *file)
SCIP_RETCODE SCIPincludeHeurLocalbranching(SCIP *scip)
Constraint handler for linear constraints in their most general form, .
static SCIP_DECL_HEURFREE(heurFreeLocalbranching)
#define HEUR_NAME
SCIP_RETCODE SCIPincludeEventhdlrBasic(SCIP *scip, SCIP_EVENTHDLR **eventhdlrptr, const char *name, const char *desc, SCIP_DECL_EVENTEXEC((*eventexec)), SCIP_EVENTHDLRDATA *eventhdlrdata)
Definition: scip_event.c:95
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_param.c:130
#define SCIP_REAL_MAX
Definition: def.h:164
public methods for branching rule plugins and branching
public methods for managing events
general public methods
#define HEUR_PRIORITY
static SCIP_DECL_EVENTEXEC(eventExecLocalbranching)
public methods for solutions
SCIP_Real SCIPgetLowerbound(SCIP *scip)
SCIP_RETCODE SCIPdropEvent(SCIP *scip, SCIP_EVENTTYPE eventtype, SCIP_EVENTHDLR *eventhdlr, SCIP_EVENTDATA *eventdata, int filterpos)
Definition: scip_event.c:311
#define HEUR_USESSUBSCIP
SCIP_RETCODE SCIPsetHeurCopy(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURCOPY((*heurcopy)))
Definition: scip_heur.c:153
#define HEUR_DESC
public methods for message output
SCIP_RETCODE SCIPgetVarsData(SCIP *scip, SCIP_VAR ***vars, int *nvars, int *nbinvars, int *nintvars, int *nimplvars, int *ncontvars)
Definition: scip_prob.c:1860
int SCIPsnprintf(char *t, int len, const char *s,...)
Definition: misc.c:10590
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_param.c:74
void SCIPhashmapFree(SCIP_HASHMAP **hashmap)
Definition: misc.c:3047
#define SCIP_Real
Definition: def.h:163
#define EVENTHDLR_DESC
public methods for message handling
#define SCIP_Longint
Definition: def.h:148
static SCIP_RETCODE addLocalbranchingConstraintAndObjcutoff(SCIP *scip, SCIP *subscip, SCIP_HEUR *heur, SCIP_VAR **subvars)
static SCIP_DECL_HEURCOPY(heurCopyLocalbranching)
int SCIPgetNBinVars(SCIP *scip)
Definition: scip_prob.c:2031
SCIP_RETCODE SCIPsetHeurFree(SCIP *scip, SCIP_HEUR *heur, SCIP_DECL_HEURFREE((*heurfree)))
Definition: scip_heur.c:169
SCIP_RETCODE SCIPaddCons(SCIP *scip, SCIP_CONS *cons)
Definition: scip_prob.c:2764
#define DEFAULT_USELPROWS
public methods for primal heuristics
SCIP_RETCODE SCIPfree(SCIP **scip)
Definition: scip_general.c:315
#define SCIP_CALL_ABORT(x)
Definition: def.h:343
SCIP_RETCODE SCIPreleaseCons(SCIP *scip, SCIP_CONS **cons)
Definition: scip_cons.c:1110
public methods for global and local (sub)problems
#define DEFAULT_MINNODES
SCIP_SOL * SCIPgetBestSol(SCIP *scip)
Definition: scip_sol.c:2305
memory allocation routines