/
heur_localbranching.c
832 lines (672 loc) · 32.5 KB
/
heur_localbranching.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* This file is part of the program and library */
/* SCIP --- Solving Constraint Integer Programs */
/* */
/* Copyright (C) 2002-2014 Konrad-Zuse-Zentrum */
/* fuer Informationstechnik Berlin */
/* */
/* SCIP is distributed under the terms of the ZIB Academic License. */
/* */
/* You should have received a copy of the ZIB Academic License */
/* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
/* */
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/**@file heur_localbranching.c
* @brief Local branching heuristic according to Fischetti and Lodi
* @author Timo Berthold
* @author Marc Pfetsch
*/
/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
#include <assert.h>
#include <string.h>
#include "scip/scip.h"
#include "scip/cons_linear.h"
#include "scip/scipdefplugins.h"
#include "scip/heur_localbranching.h"
#include "scip/pub_misc.h"
#define HEUR_NAME "localbranching"
#define HEUR_DESC "local branching heuristic by Fischetti and Lodi"
#define HEUR_DISPCHAR 'L'
#define HEUR_PRIORITY -1102000
#define HEUR_FREQ -1
#define HEUR_FREQOFS 0
#define HEUR_MAXDEPTH -1
#define HEUR_TIMING SCIP_HEURTIMING_AFTERNODE
#define HEUR_USESSUBSCIP TRUE /**< does the heuristic use a secondary SCIP instance? */
#define DEFAULT_NEIGHBORHOODSIZE 18 /* radius of the incumbents neighborhood to be searched */
#define DEFAULT_NODESOFS 1000 /* number of nodes added to the contingent of the total nodes */
#define DEFAULT_MAXNODES 10000 /* maximum number of nodes to regard in the subproblem */
#define DEFAULT_MINIMPROVE 0.01 /* factor by which localbranching should at least improve the incumbent */
#define DEFAULT_MINNODES 1000 /* minimum number of nodes required to start the subproblem */
#define DEFAULT_NODESQUOT 0.05 /* contingent of sub problem nodes in relation to original nodes */
#define DEFAULT_LPLIMFAC 1.5 /* factor by which the limit on the number of LP depends on the node limit */
#define DEFAULT_NWAITINGNODES 200 /* number of nodes without incumbent change that heuristic should wait */
#define DEFAULT_USELPROWS FALSE /* should subproblem be created out of the rows in the LP rows,
* otherwise, the copy constructors of the constraints handlers are used */
#define DEFAULT_COPYCUTS TRUE /* if DEFAULT_USELPROWS is FALSE, then should all active cuts from the cutpool
* of the original scip be copied to constraints of the subscip
*/
/* event handler properties */
#define EVENTHDLR_NAME "Localbranching"
#define EVENTHDLR_DESC "LP event handler for "HEUR_NAME" heuristic"
#define EXECUTE 0
#define WAITFORNEWSOL 1
/*
* Data structures
*/
/** primal heuristic data */
struct SCIP_HeurData
{
int nwaitingnodes; /**< number of nodes without incumbent change that heuristic should wait */
int nodesofs; /**< number of nodes added to the contingent of the total nodes */
int minnodes; /**< minimum number of nodes required to start the subproblem */
int maxnodes; /**< maximum number of nodes to regard in the subproblem */
SCIP_Longint usednodes; /**< amount of nodes local branching used during all calls */
SCIP_Real nodesquot; /**< contingent of sub problem nodes in relation to original nodes */
SCIP_Real minimprove; /**< factor by which localbranching should at least improve the incumbent */
SCIP_Real nodelimit; /**< the nodelimit employed in the current sub-SCIP, for the event handler*/
SCIP_Real lplimfac; /**< factor by which the limit on the number of LP depends on the node limit */
int neighborhoodsize; /**< radius of the incumbent's neighborhood to be searched */
int callstatus; /**< current status of localbranching heuristic */
SCIP_SOL* lastsol; /**< the last incumbent localbranching used as reference point */
int curneighborhoodsize;/**< current neighborhoodsize */
int curminnodes; /**< current minimal number of nodes required to start the subproblem */
int emptyneighborhoodsize;/**< size of neighborhood that was proven to be empty */
SCIP_Bool uselprows; /**< should subproblem be created out of the rows in the LP rows? */
SCIP_Bool copycuts; /**< if uselprows == FALSE, should all active cuts from cutpool be copied
* to constraints in subproblem?
*/
};
/*
* Local methods
*/
/** copies the problem of scip to the problem of subscip - only necessary if uselprows is false */
static
SCIP_RETCODE createSubproblem(
SCIP* scip, /**< SCIP data structure of the original problem */
SCIP* subscip, /**< SCIP data structure of the subproblem */
SCIP_VAR** subvars /**< variables of the subproblem */
)
{
SCIP_ROW** rows;
int nrows;
int i;
/* get the rows and their number */
SCIP_CALL( SCIPgetLPRowsData(scip, &rows, &nrows) );
for( i = 0; i < nrows; i++ )
{
SCIP_CONS* cons;
SCIP_VAR** consvars;
SCIP_COL** cols;
SCIP_Real constant;
SCIP_Real lhs;
SCIP_Real rhs;
SCIP_Real* vals;
int nnonz;
int j;
/* ignore rows that are only locally valid */
if( SCIProwIsLocal(rows[i]) )
continue;
/* get the row's data */
constant = SCIProwGetConstant(rows[i]);
lhs = SCIProwGetLhs(rows[i]) - constant;
rhs = SCIProwGetRhs(rows[i]) - constant;
vals = SCIProwGetVals(rows[i]);
nnonz = SCIProwGetNNonz(rows[i]);
cols = SCIProwGetCols(rows[i]);
assert(lhs <= rhs);
/* allocate memory array to be filled with the corresponding subproblem variables */
SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nnonz) );
for( j = 0; j < nnonz; j++ )
consvars[j] = subvars[SCIPvarGetProbindex(SCIPcolGetVar(cols[j]))];
/* create new constraint and add it to subscip */
SCIP_CALL( SCIPcreateConsLinear(subscip, &cons, SCIProwGetName(rows[i]), nnonz, consvars, vals, lhs, rhs,
TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE) );
SCIP_CALL( SCIPaddCons(subscip, cons) );
SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
/* free memory */
SCIPfreeBufferArray(scip, &consvars);
}
return SCIP_OKAY;
}
/** create the extra constraint of local branching and add it to subscip */
static
SCIP_RETCODE addLocalBranchingConstraint(
SCIP* scip, /**< SCIP data structure of the original problem */
SCIP* subscip, /**< SCIP data structure of the subproblem */
SCIP_VAR** subvars, /**< variables of the subproblem */
SCIP_HEURDATA* heurdata /**< heuristic's data structure */
)
{
SCIP_CONS* cons; /* local branching constraint to create */
SCIP_VAR** consvars;
SCIP_VAR** vars;
SCIP_SOL* bestsol;
int nbinvars;
int i;
SCIP_Real lhs;
SCIP_Real rhs;
SCIP_Real* consvals;
char consname[SCIP_MAXSTRLEN];
(void) SCIPsnprintf(consname, SCIP_MAXSTRLEN, "%s_localbranchcons", SCIPgetProbName(scip));
/* get the data of the variables and the best solution */
SCIP_CALL( SCIPgetVarsData(scip, &vars, NULL, &nbinvars, NULL, NULL, NULL) );
bestsol = SCIPgetBestSol(scip);
assert( bestsol != NULL );
/* memory allocation */
SCIP_CALL( SCIPallocBufferArray(scip, &consvars, nbinvars) );
SCIP_CALL( SCIPallocBufferArray(scip, &consvals, nbinvars) );
/* set initial left and right hand sides of local branching constraint */
lhs = (SCIP_Real)heurdata->emptyneighborhoodsize + 1.0;
rhs = (SCIP_Real)heurdata->curneighborhoodsize;
/* create the distance (to incumbent) function of the binary variables */
for( i = 0; i < nbinvars; i++ )
{
SCIP_Real solval;
solval = SCIPgetSolVal(scip, bestsol, vars[i]);
assert( SCIPisFeasIntegral(scip,solval) );
/* is variable i part of the binary support of bestsol? */
if( SCIPisFeasEQ(scip,solval,1.0) )
{
consvals[i] = -1.0;
rhs -= 1.0;
lhs -= 1.0;
}
else
consvals[i] = 1.0;
consvars[i] = subvars[i];
assert( SCIPvarGetType(consvars[i]) == SCIP_VARTYPE_BINARY );
}
/* creates localbranching constraint and adds it to subscip */
SCIP_CALL( SCIPcreateConsLinear(subscip, &cons, consname, nbinvars, consvars, consvals,
lhs, rhs, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE) );
SCIP_CALL( SCIPaddCons(subscip, cons) );
SCIP_CALL( SCIPreleaseCons(subscip, &cons) );
/* free local memory */
SCIPfreeBufferArray(scip, &consvals);
SCIPfreeBufferArray(scip, &consvars);
return SCIP_OKAY;
}
/** creates a new solution for the original problem by copying the solution of the subproblem */
static
SCIP_RETCODE createNewSol(
SCIP* scip, /**< SCIP data structure of the original problem */
SCIP* subscip, /**< SCIP data structure of the subproblem */
SCIP_VAR** subvars, /**< the variables of the subproblem */
SCIP_HEUR* heur, /**< the Localbranching heuristic */
SCIP_SOL* subsol, /**< solution of the subproblem */
SCIP_Bool* success /**< pointer to store, whether new solution was found */
)
{
SCIP_VAR** vars;
int nvars;
SCIP_SOL* newsol;
SCIP_Real* subsolvals;
assert( scip != NULL );
assert( subscip != NULL );
assert( subvars != NULL );
assert( subsol != NULL );
/* copy the solution */
SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
/* sub-SCIP may have more variables than the number of active (transformed) variables in the main SCIP
* since constraint copying may have required the copy of variables that are fixed in the main SCIP
*/
assert(nvars <= SCIPgetNOrigVars(subscip));
SCIP_CALL( SCIPallocBufferArray(scip, &subsolvals, nvars) );
/* copy the solution */
SCIP_CALL( SCIPgetSolVals(subscip, subsol, nvars, subvars, subsolvals) );
/* create new solution for the original problem */
SCIP_CALL( SCIPcreateSol(scip, &newsol, heur) );
SCIP_CALL( SCIPsetSolVals(scip, newsol, nvars, vars, subsolvals) );
SCIP_CALL( SCIPtrySolFree(scip, &newsol, FALSE, TRUE, TRUE, TRUE, success) );
SCIPfreeBufferArray(scip, &subsolvals);
return SCIP_OKAY;
}
/* ---------------- Callback methods of event handler ---------------- */
/* exec the event handler
*
* we interrupt the solution process
*/
static
SCIP_DECL_EVENTEXEC(eventExecLocalbranching)
{
SCIP_HEURDATA* heurdata;
assert(eventhdlr != NULL);
assert(eventdata != NULL);
assert(strcmp(SCIPeventhdlrGetName(eventhdlr), EVENTHDLR_NAME) == 0);
assert(event != NULL);
assert(SCIPeventGetType(event) & SCIP_EVENTTYPE_LPSOLVED);
heurdata = (SCIP_HEURDATA*)eventdata;
assert(heurdata != NULL);
/* interrupt solution process of sub-SCIP */
if( SCIPgetNLPs(scip) > heurdata->lplimfac * heurdata->nodelimit )
{
SCIPdebugMessage("interrupt after %"SCIP_LONGINT_FORMAT" LPs\n",SCIPgetNLPs(scip));
SCIP_CALL( SCIPinterruptSolve(scip) );
}
return SCIP_OKAY;
}
/*
* Callback methods of primal heuristic
*/
/** copy method for primal heuristic plugins (called when SCIP copies plugins) */
static
SCIP_DECL_HEURCOPY(heurCopyLocalbranching)
{ /*lint --e{715}*/
assert(scip != NULL);
assert(heur != NULL);
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
/* call inclusion method of primal heuristic */
SCIP_CALL( SCIPincludeHeurLocalbranching(scip) );
return SCIP_OKAY;
}
/** destructor of primal heuristic to free user data (called when SCIP is exiting) */
static
SCIP_DECL_HEURFREE(heurFreeLocalbranching)
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
assert( heur != NULL );
assert( scip != NULL );
/* get heuristic data */
heurdata = SCIPheurGetData(heur);
assert( heurdata != NULL );
/* free heuristic data */
SCIPfreeMemory(scip, &heurdata);
SCIPheurSetData(heur, NULL);
return SCIP_OKAY;
}
/** initialization method of primal heuristic (called after problem was transformed) */
static
SCIP_DECL_HEURINIT(heurInitLocalbranching)
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
assert( heur != NULL );
assert( scip != NULL );
/* get heuristic's data */
heurdata = SCIPheurGetData(heur);
assert( heurdata != NULL );
/* with a little abuse we initialize the heurdata as if localbranching would have finished its last step regularly */
heurdata->callstatus = WAITFORNEWSOL;
heurdata->lastsol = NULL;
heurdata->usednodes = 0;
heurdata->curneighborhoodsize = heurdata->neighborhoodsize;
heurdata->curminnodes = heurdata->minnodes;
heurdata->emptyneighborhoodsize = 0;
return SCIP_OKAY;
}
/** execution method of primal heuristic */
static
SCIP_DECL_HEUREXEC(heurExecLocalbranching)
{ /*lint --e{715}*/
SCIP_Longint maxnnodes; /* maximum number of subnodes */
SCIP_Longint nsubnodes; /* nodelimit for subscip */
SCIP_HEURDATA* heurdata;
SCIP* subscip; /* the subproblem created by localbranching */
SCIP_VAR** subvars; /* subproblem's variables */
SCIP_SOL* bestsol; /* best solution so far */
SCIP_EVENTHDLR* eventhdlr; /* event handler for LP events */
SCIP_Real timelimit; /* timelimit for subscip (equals remaining time of scip) */
SCIP_Real cutoff; /* objective cutoff for the subproblem */
SCIP_Real upperbound;
SCIP_Real memorylimit;
SCIP_HASHMAP* varmapfw; /* mapping of SCIP variables to sub-SCIP variables */
SCIP_VAR** vars;
int nvars;
int i;
SCIP_Bool success;
SCIP_RETCODE retcode;
assert(heur != NULL);
assert(scip != NULL);
assert(result != NULL);
*result = SCIP_DIDNOTRUN;
/* get heuristic's data */
heurdata = SCIPheurGetData(heur);
assert( heurdata != NULL );
/* there should be enough binary variables that a local branching constraint makes sense */
if( SCIPgetNBinVars(scip) < 2*heurdata->neighborhoodsize )
return SCIP_OKAY;
*result = SCIP_DELAYED;
/* only call heuristic, if an IP solution is at hand */
if( SCIPgetNSols(scip) <= 0 )
return SCIP_OKAY;
bestsol = SCIPgetBestSol(scip);
assert(bestsol != NULL);
/* only call heuristic, if the best solution comes from transformed problem */
if( SCIPsolIsOriginal(bestsol) )
return SCIP_OKAY;
/* only call heuristic, if enough nodes were processed since last incumbent */
if( SCIPgetNNodes(scip) - SCIPgetSolNodenum(scip, bestsol) < heurdata->nwaitingnodes)
return SCIP_OKAY;
/* only call heuristic, if the best solution does not come from trivial heuristic */
if( SCIPsolGetHeur(bestsol) != NULL && strcmp(SCIPheurGetName(SCIPsolGetHeur(bestsol)), "trivial") == 0 )
return SCIP_OKAY;
/* reset neighborhood and minnodes, if new solution was found */
if( heurdata->lastsol != bestsol )
{
heurdata->curneighborhoodsize = heurdata->neighborhoodsize;
heurdata->curminnodes = heurdata->minnodes;
heurdata->emptyneighborhoodsize = 0;
heurdata->callstatus = EXECUTE;
heurdata->lastsol = bestsol;
}
/* if no new solution was found and local branching also seems to fail, just keep on waiting */
if( heurdata->callstatus == WAITFORNEWSOL )
return SCIP_OKAY;
*result = SCIP_DIDNOTRUN;
/* calculate the maximal number of branching nodes until heuristic is aborted */
maxnnodes = (SCIP_Longint)(heurdata->nodesquot * SCIPgetNNodes(scip));
/* reward local branching if it succeeded often */
maxnnodes = (SCIP_Longint)(maxnnodes * (1.0 + 2.0*(SCIPheurGetNBestSolsFound(heur)+1.0)/(SCIPheurGetNCalls(heur)+1.0)));
maxnnodes -= 100 * SCIPheurGetNCalls(heur); /* count the setup costs for the sub-MIP as 100 nodes */
maxnnodes += heurdata->nodesofs;
/* determine the node limit for the current process */
nsubnodes = maxnnodes - heurdata->usednodes;
nsubnodes = MIN(nsubnodes, heurdata->maxnodes);
/* check whether we have enough nodes left to call sub problem solving */
if( nsubnodes < heurdata->curminnodes )
return SCIP_OKAY;
if( SCIPisStopped(scip) )
return SCIP_OKAY;
*result = SCIP_DIDNOTFIND;
SCIPdebugMessage("running localbranching heuristic ...\n");
/* get the data of the variables and the best solution */
SCIP_CALL( SCIPgetVarsData(scip, &vars, &nvars, NULL, NULL, NULL, NULL) );
/* initializing the subproblem */
SCIP_CALL( SCIPallocBufferArray(scip, &subvars, nvars) );
SCIP_CALL( SCIPcreate(&subscip) );
/* create the variable mapping hash map */
SCIP_CALL( SCIPhashmapCreate(&varmapfw, SCIPblkmem(subscip), SCIPcalcHashtableSize(5 * nvars)) );
success = FALSE;
eventhdlr = NULL;
if( heurdata->uselprows )
{
char probname[SCIP_MAXSTRLEN];
/* copy all plugins */
SCIP_CALL( SCIPincludeDefaultPlugins(subscip) );
/* get name of the original problem and add the string "_localbranchsub" */
(void) SCIPsnprintf(probname, SCIP_MAXSTRLEN, "%s_localbranchsub", SCIPgetProbName(scip));
/* create the subproblem */
SCIP_CALL( SCIPcreateProb(subscip, probname, NULL, NULL, NULL, NULL, NULL, NULL, NULL) );
/* copy all variables */
SCIP_CALL( SCIPcopyVars(scip, subscip, varmapfw, NULL, TRUE) );
}
else
{
SCIP_CALL( SCIPcopy(scip, subscip, varmapfw, NULL, "localbranchsub", TRUE, FALSE, TRUE, &success) );
if( heurdata->copycuts )
{
/* copies all active cuts from cutpool of sourcescip to linear constraints in targetscip */
SCIP_CALL( SCIPcopyCuts(scip, subscip, varmapfw, NULL, TRUE, NULL) );
}
/* create event handler for LP events */
SCIP_CALL( SCIPincludeEventhdlrBasic(subscip, &eventhdlr, EVENTHDLR_NAME, EVENTHDLR_DESC, eventExecLocalbranching, NULL) );
if( eventhdlr == NULL )
{
SCIPerrorMessage("event handler for "HEUR_NAME" heuristic not found.\n");
return SCIP_PLUGINNOTFOUND;
}
}
SCIPdebugMessage("Copying the plugins was %ssuccessful.\n", success ? "" : "not ");
for (i = 0; i < nvars; ++i)
subvars[i] = (SCIP_VAR*) SCIPhashmapGetImage(varmapfw, vars[i]);
/* free hash map */
SCIPhashmapFree(&varmapfw);
/* if the subproblem could not be created, free memory and return */
if( !success )
{
*result = SCIP_DIDNOTRUN;
goto TERMINATE;
}
/* do not abort subproblem on CTRL-C */
SCIP_CALL( SCIPsetBoolParam(subscip, "misc/catchctrlc", FALSE) );
#ifndef SCIP_DEBUG
/* disable output to console */
SCIP_CALL( SCIPsetIntParam(subscip, "display/verblevel", 0) );
#endif
/* check whether there is enough time and memory left */
SCIP_CALL( SCIPgetRealParam(scip, "limits/time", &timelimit) );
if( !SCIPisInfinity(scip, timelimit) )
timelimit -= SCIPgetSolvingTime(scip);
SCIP_CALL( SCIPgetRealParam(scip, "limits/memory", &memorylimit) );
/* substract the memory already used by the main SCIP and the estimated memory usage of external software */
if( !SCIPisInfinity(scip, memorylimit) )
{
memorylimit -= SCIPgetMemUsed(scip)/1048576.0;
memorylimit -= SCIPgetMemExternEstim(scip)/1048576.0;
}
/* abort if no time is left or not enough memory to create a copy of SCIP, including external memory usage */
if( timelimit <= 0.0 || memorylimit <= 2.0*SCIPgetMemExternEstim(scip)/1048576.0 )
goto TERMINATE;
/* set limits for the subproblem */
heurdata->nodelimit = nsubnodes;
SCIP_CALL( SCIPsetLongintParam(subscip, "limits/nodes", nsubnodes) );
SCIP_CALL( SCIPsetLongintParam(subscip, "limits/stallnodes", MAX(10, nsubnodes/10)) );
SCIP_CALL( SCIPsetIntParam(subscip, "limits/bestsol", 3) );
SCIP_CALL( SCIPsetRealParam(subscip, "limits/time", timelimit) );
SCIP_CALL( SCIPsetRealParam(subscip, "limits/memory", memorylimit) );
/* forbid recursive call of heuristics and separators solving subMIPs */
SCIP_CALL( SCIPsetSubscipsOff(subscip, TRUE) );
/* disable cutting plane separation */
SCIP_CALL( SCIPsetSeparating(subscip, SCIP_PARAMSETTING_OFF, TRUE) );
/* disable expensive presolving */
SCIP_CALL( SCIPsetPresolving(subscip, SCIP_PARAMSETTING_FAST, TRUE) );
/* use best estimate node selection */
if( SCIPfindNodesel(subscip, "estimate") != NULL && !SCIPisParamFixed(subscip, "nodeselection/estimate/stdpriority") )
{
SCIP_CALL( SCIPsetIntParam(subscip, "nodeselection/estimate/stdpriority", INT_MAX/4) );
}
/* use inference branching */
if( SCIPfindBranchrule(subscip, "inference") != NULL && !SCIPisParamFixed(subscip, "branching/inference/priority") )
{
SCIP_CALL( SCIPsetIntParam(subscip, "branching/inference/priority", INT_MAX/4) );
}
/* disable conflict analysis */
if( !SCIPisParamFixed(subscip, "conflict/useprop") )
{
SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useprop", FALSE) );
}
if( !SCIPisParamFixed(subscip, "conflict/useinflp") )
{
SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useinflp", FALSE) );
}
if( !SCIPisParamFixed(subscip, "conflict/useboundlp") )
{
SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/useboundlp", FALSE) );
}
if( !SCIPisParamFixed(subscip, "conflict/usesb") )
{
SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usesb", FALSE) );
}
if( !SCIPisParamFixed(subscip, "conflict/usepseudo") )
{
SCIP_CALL( SCIPsetBoolParam(subscip, "conflict/usepseudo", FALSE) );
}
/* employ a limit on the number of enforcement rounds in the quadratic constraint handler; this fixes the issue that
* sometimes the quadratic constraint handler needs hundreds or thousands of enforcement rounds to determine the
* feasibility status of a single node without fractional branching candidates by separation (namely for uflquad
* instances); however, the solution status of the sub-SCIP might get corrupted by this; hence no deductions shall be
* made for the original SCIP
*/
if( SCIPfindConshdlr(subscip, "quadratic") != NULL && !SCIPisParamFixed(subscip, "constraints/quadratic/enfolplimit") )
{
SCIP_CALL( SCIPsetIntParam(subscip, "constraints/quadratic/enfolplimit", 500) );
}
/* copy the original problem and add the local branching constraint */
if( heurdata->uselprows )
{
SCIP_CALL( createSubproblem(scip, subscip, subvars) );
}
SCIP_CALL( addLocalBranchingConstraint(scip, subscip, subvars, heurdata) );
/* add an objective cutoff */
cutoff = SCIPinfinity(scip);
assert( !SCIPisInfinity(scip,SCIPgetUpperbound(scip)) );
upperbound = SCIPgetUpperbound(scip) - SCIPsumepsilon(scip);
if( !SCIPisInfinity(scip,-1.0*SCIPgetLowerbound(scip)) )
{
cutoff = (1-heurdata->minimprove)*SCIPgetUpperbound(scip) + heurdata->minimprove*SCIPgetLowerbound(scip);
}
else
{
if( SCIPgetUpperbound ( scip ) >= 0 )
cutoff = ( 1 - heurdata->minimprove ) * SCIPgetUpperbound ( scip );
else
cutoff = ( 1 + heurdata->minimprove ) * SCIPgetUpperbound ( scip );
}
cutoff = MIN(upperbound, cutoff );
SCIP_CALL( SCIPsetObjlimit(subscip, cutoff) );
/* catch LP events of sub-SCIP */
if( !heurdata->uselprows )
{
assert(eventhdlr != NULL);
SCIP_CALL( SCIPtransformProb(subscip) );
SCIP_CALL( SCIPcatchEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, NULL) );
}
/* solve the subproblem */
SCIPdebugMessage("solving local branching subproblem with neighborhoodsize %d and maxnodes %"SCIP_LONGINT_FORMAT"\n",
heurdata->curneighborhoodsize, nsubnodes);
retcode = SCIPsolve(subscip);
/* drop LP events of sub-SCIP */
if( !heurdata->uselprows )
{
assert(eventhdlr != NULL);
SCIP_CALL( SCIPdropEvent(subscip, SCIP_EVENTTYPE_LPSOLVED, eventhdlr, (SCIP_EVENTDATA*) heurdata, -1) );
}
/* Errors in solving the subproblem should not kill the overall solving process
* Hence, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
*/
if( retcode != SCIP_OKAY )
{
#ifndef NDEBUG
SCIP_CALL( retcode );
#endif
SCIPwarningMessage(scip, "Error while solving subproblem in local branching heuristic; sub-SCIP terminated with code <%d>\n",retcode);
}
/* print solving statistics of subproblem if we are in SCIP's debug mode */
SCIPdebug( SCIP_CALL( SCIPprintStatistics(subscip, NULL) ) );
heurdata->usednodes += SCIPgetNNodes(subscip);
SCIPdebugMessage("local branching used %"SCIP_LONGINT_FORMAT"/%"SCIP_LONGINT_FORMAT" nodes\n",
SCIPgetNNodes(subscip), nsubnodes);
/* check, whether a solution was found */
if( SCIPgetNSols(subscip) > 0 )
{
SCIP_SOL** subsols;
int nsubsols;
/* check, whether a solution was found;
* due to numerics, it might happen that not all solutions are feasible -> try all solutions until one was accepted
*/
nsubsols = SCIPgetNSols(subscip);
subsols = SCIPgetSols(subscip);
success = FALSE;
for( i = 0; i < nsubsols && !success; ++i )
{
SCIP_CALL( createNewSol(scip, subscip, subvars, heur, subsols[i], &success) );
}
if( success )
{
SCIPdebugMessage("-> accepted solution of value %g\n", SCIPgetSolOrigObj(subscip, subsols[i]));
*result = SCIP_FOUNDSOL;
}
}
/* check the status of the sub-MIP */
switch( SCIPgetStatus(subscip) )
{
case SCIP_STATUS_OPTIMAL:
case SCIP_STATUS_BESTSOLLIMIT:
heurdata->callstatus = WAITFORNEWSOL; /* new solution will immediately be installed at next call */
SCIPdebugMessage(" -> found new solution\n");
break;
case SCIP_STATUS_NODELIMIT:
case SCIP_STATUS_STALLNODELIMIT:
case SCIP_STATUS_TOTALNODELIMIT:
heurdata->callstatus = EXECUTE;
heurdata->curneighborhoodsize = (heurdata->emptyneighborhoodsize + heurdata->curneighborhoodsize)/2;
heurdata->curminnodes *= 2;
SCIPdebugMessage(" -> node limit reached: reduced neighborhood to %d, increased minnodes to %d\n",
heurdata->curneighborhoodsize, heurdata->curminnodes);
if( heurdata->curneighborhoodsize <= heurdata->emptyneighborhoodsize )
{
heurdata->callstatus = WAITFORNEWSOL;
SCIPdebugMessage(" -> new neighborhood was already proven to be empty: wait for new solution\n");
}
break;
case SCIP_STATUS_INFEASIBLE:
case SCIP_STATUS_INFORUNBD:
heurdata->emptyneighborhoodsize = heurdata->curneighborhoodsize;
heurdata->curneighborhoodsize += heurdata->curneighborhoodsize/2;
heurdata->curneighborhoodsize = MAX(heurdata->curneighborhoodsize, heurdata->emptyneighborhoodsize + 2);
heurdata->callstatus = EXECUTE;
SCIPdebugMessage(" -> neighborhood is empty: increased neighborhood to %d\n", heurdata->curneighborhoodsize);
break;
case SCIP_STATUS_UNKNOWN:
case SCIP_STATUS_USERINTERRUPT:
case SCIP_STATUS_TIMELIMIT:
case SCIP_STATUS_MEMLIMIT:
case SCIP_STATUS_GAPLIMIT:
case SCIP_STATUS_SOLLIMIT:
case SCIP_STATUS_UNBOUNDED:
default:
heurdata->callstatus = WAITFORNEWSOL;
SCIPdebugMessage(" -> unexpected sub-MIP status <%d>: waiting for new solution\n", SCIPgetStatus(subscip));
break;
}
TERMINATE:
/* free subproblem */
SCIPfreeBufferArray(scip, &subvars);
SCIP_CALL( SCIPfree(&subscip) );
return SCIP_OKAY;
}
/*
* primal heuristic specific interface methods
*/
/** creates the localbranching primal heuristic and includes it in SCIP */
SCIP_RETCODE SCIPincludeHeurLocalbranching(
SCIP* scip /**< SCIP data structure */
)
{
SCIP_HEURDATA* heurdata;
SCIP_HEUR* heur;
/* create Localbranching primal heuristic data */
SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
/* include primal heuristic */
SCIP_CALL( SCIPincludeHeurBasic(scip, &heur,
HEUR_NAME, HEUR_DESC, HEUR_DISPCHAR, HEUR_PRIORITY, HEUR_FREQ, HEUR_FREQOFS,
HEUR_MAXDEPTH, HEUR_TIMING, HEUR_USESSUBSCIP, heurExecLocalbranching, heurdata) );
assert(heur != NULL);
/* set non-NULL pointers to callback methods */
SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyLocalbranching) );
SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeLocalbranching) );
SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitLocalbranching) );
/* add localbranching primal heuristic parameters */
SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/nodesofs",
"number of nodes added to the contingent of the total nodes",
&heurdata->nodesofs, FALSE, DEFAULT_NODESOFS, 0, INT_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/neighborhoodsize",
"radius (using Manhattan metric) of the incumbent's neighborhood to be searched",
&heurdata->neighborhoodsize, FALSE, DEFAULT_NEIGHBORHOODSIZE, 1, INT_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/nodesquot",
"contingent of sub problem nodes in relation to the number of nodes of the original problem",
&heurdata->nodesquot, FALSE, DEFAULT_NODESQUOT, 0.0, 1.0, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/lplimfac",
"factor by which the limit on the number of LP depends on the node limit",
&heurdata->lplimfac, TRUE, DEFAULT_LPLIMFAC, 1.0, SCIP_REAL_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/minnodes",
"minimum number of nodes required to start the subproblem",
&heurdata->minnodes, TRUE, DEFAULT_MINNODES, 0, INT_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/maxnodes",
"maximum number of nodes to regard in the subproblem",
&heurdata->maxnodes, TRUE, DEFAULT_MAXNODES, 0, INT_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddIntParam(scip, "heuristics/"HEUR_NAME"/nwaitingnodes",
"number of nodes without incumbent change that heuristic should wait",
&heurdata->nwaitingnodes, TRUE, DEFAULT_NWAITINGNODES, 0, INT_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip, "heuristics/"HEUR_NAME"/minimprove",
"factor by which localbranching should at least improve the incumbent",
&heurdata->minimprove, TRUE, DEFAULT_MINIMPROVE, 0.0, 1.0, NULL, NULL) );
SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/uselprows",
"should subproblem be created out of the rows in the LP rows?",
&heurdata->uselprows, TRUE, DEFAULT_USELPROWS, NULL, NULL) );
SCIP_CALL( SCIPaddBoolParam(scip, "heuristics/"HEUR_NAME"/copycuts",
"if uselprows == FALSE, should all active cuts from cutpool be copied to constraints in subproblem?",
&heurdata->copycuts, TRUE, DEFAULT_COPYCUTS, NULL, NULL) );
return SCIP_OKAY;
}