/
heur_actconsdiving.c
762 lines (651 loc) · 30 KB
/
heur_actconsdiving.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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* 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_actconsdiving.c
* @brief LP diving heuristic that chooses fixings w.r.t. the active constraints the variable appear in
* @author Tobias Achterberg
*/
/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
#include <assert.h>
#include <string.h>
#include "scip/heur_actconsdiving.h"
#define HEUR_NAME "actconsdiving"
#define HEUR_DESC "LP diving heuristic that chooses fixings w.r.t. the active constraints"
#define HEUR_DISPCHAR 'a'
#define HEUR_PRIORITY -1003700
#define HEUR_FREQ -1
#define HEUR_FREQOFS 5
#define HEUR_MAXDEPTH -1
#define HEUR_TIMING SCIP_HEURTIMING_AFTERLPPLUNGE
#define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
/*
* Default parameter settings
*/
#define DEFAULT_MINRELDEPTH 0.0 /**< minimal relative depth to start diving */
#define DEFAULT_MAXRELDEPTH 1.0 /**< maximal relative depth to start diving */
#define DEFAULT_MAXLPITERQUOT 0.05 /**< maximal fraction of diving LP iterations compared to node LP iterations */
#define DEFAULT_MAXLPITEROFS 1000 /**< additional number of allowed LP iterations */
#define DEFAULT_MAXDIVEUBQUOT 0.8 /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
* where diving is performed (0.0: no limit) */
#define DEFAULT_MAXDIVEAVGQUOT 0.0 /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
* where diving is performed (0.0: no limit) */
#define DEFAULT_MAXDIVEUBQUOTNOSOL 0.1 /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
#define DEFAULT_MAXDIVEAVGQUOTNOSOL 0.0 /**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
#define DEFAULT_BACKTRACK TRUE /**< use one level of backtracking if infeasibility is encountered? */
#define MINLPITER 10000 /**< minimal number of LP iterations allowed in each LP solving call */
/* locally defined heuristic data */
struct SCIP_HeurData
{
SCIP_SOL* sol; /**< working solution */
SCIP_Real minreldepth; /**< minimal relative depth to start diving */
SCIP_Real maxreldepth; /**< maximal relative depth to start diving */
SCIP_Real maxlpiterquot; /**< maximal fraction of diving LP iterations compared to node LP iterations */
int maxlpiterofs; /**< additional number of allowed LP iterations */
SCIP_Real maxdiveubquot; /**< maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound)
* where diving is performed (0.0: no limit) */
SCIP_Real maxdiveavgquot; /**< maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound)
* where diving is performed (0.0: no limit) */
SCIP_Real maxdiveubquotnosol; /**< maximal UBQUOT when no solution was found yet (0.0: no limit) */
SCIP_Real maxdiveavgquotnosol;/**< maximal AVGQUOT when no solution was found yet (0.0: no limit) */
SCIP_Bool backtrack; /**< use one level of backtracking if infeasibility is encountered? */
SCIP_Longint nlpiterations; /**< LP iterations used in this heuristic */
int nsuccess; /**< number of runs that produced at least one feasible solution */
};
/*
* local methods
*/
/** returns a score value for the given variable based on the active constraints that the variable appears in */
static
SCIP_Real getNActiveConsScore(
SCIP* scip, /**< SCIP data structure */
SCIP_VAR* var, /**< variable to get the score value for */
SCIP_Real* downscore, /**< pointer to store the score for branching downwards */
SCIP_Real* upscore /**< pointer to store the score for branching upwards */
)
{
SCIP_COL* col;
SCIP_ROW** rows;
SCIP_Real* vals;
int nrows;
int r;
int nactrows;
SCIP_Real downcoefsum;
SCIP_Real upcoefsum;
SCIP_Real score;
assert(downscore != NULL);
assert(upscore != NULL);
*downscore = 0.0;
*upscore = 0.0;
if( SCIPvarGetStatus(var) != SCIP_VARSTATUS_COLUMN )
return 0.0;
col = SCIPvarGetCol(var);
assert(col != NULL);
rows = SCIPcolGetRows(col);
vals = SCIPcolGetVals(col);
nrows = SCIPcolGetNLPNonz(col);
nactrows = 0;
downcoefsum = 0.0;
upcoefsum = 0.0;
for( r = 0; r < nrows; ++r )
{
SCIP_Real activity;
SCIP_Real lhs;
SCIP_Real rhs;
SCIP_Real dualsol;
/* calculate number of active constraint sides, i.e., count equations as two */
lhs = SCIProwGetLhs(rows[r]);
rhs = SCIProwGetRhs(rows[r]);
activity = SCIPgetRowLPActivity(scip, rows[r]);
dualsol = SCIProwGetDualsol(rows[r]);
if( SCIPisFeasEQ(scip, activity, lhs) )
{
SCIP_Real coef;
nactrows++;
coef = vals[r] / SCIProwGetNorm(rows[r]);
if( SCIPisFeasPositive(scip, dualsol) )
{
if( coef > 0.0 )
downcoefsum += coef;
else
upcoefsum -= coef;
}
}
else if( SCIPisFeasEQ(scip, activity, rhs) )
{
SCIP_Real coef;
nactrows++;
coef = vals[r] / SCIProwGetNorm(rows[r]);
if( SCIPisFeasNegative(scip, dualsol) )
{
if( coef > 0.0 )
upcoefsum += coef;
else
downcoefsum -= coef;
}
}
}
score = 1e-3*nactrows + (downcoefsum + 1e-6) * (upcoefsum + 1e-6);
*downscore = -downcoefsum;
*upscore = -upcoefsum;
return score;
}
/*
* Callback methods
*/
/** copy method for primal heuristic plugins (called when SCIP copies plugins) */
static
SCIP_DECL_HEURCOPY(heurCopyActconsdiving)
{ /*lint --e{715}*/
assert(scip != NULL);
assert(heur != NULL);
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
/* call inclusion method of primal heuristic */
SCIP_CALL( SCIPincludeHeurActconsdiving(scip) );
return SCIP_OKAY;
}
/** destructor of primal heuristic to free user data (called when SCIP is exiting) */
static
SCIP_DECL_HEURFREE(heurFreeActconsdiving) /*lint --e{715}*/
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
assert(heur != NULL);
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
assert(scip != NULL);
/* free heuristic data */
heurdata = SCIPheurGetData(heur);
assert(heurdata != NULL);
SCIPfreeMemory(scip, &heurdata);
SCIPheurSetData(heur, NULL);
return SCIP_OKAY;
}
/** initialization method of primal heuristic (called after problem was transformed) */
static
SCIP_DECL_HEURINIT(heurInitActconsdiving) /*lint --e{715}*/
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
assert(heur != NULL);
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
/* get heuristic data */
heurdata = SCIPheurGetData(heur);
assert(heurdata != NULL);
/* create working solution */
SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
/* initialize data */
heurdata->nlpiterations = 0;
heurdata->nsuccess = 0;
return SCIP_OKAY;
}
/** deinitialization method of primal heuristic (called before transformed problem is freed) */
static
SCIP_DECL_HEUREXIT(heurExitActconsdiving) /*lint --e{715}*/
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
assert(heur != NULL);
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
/* get heuristic data */
heurdata = SCIPheurGetData(heur);
assert(heurdata != NULL);
/* free working solution */
SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
return SCIP_OKAY;
}
/** execution method of primal heuristic */
static
SCIP_DECL_HEUREXEC(heurExecActconsdiving) /*lint --e{715}*/
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
SCIP_LPSOLSTAT lpsolstat;
SCIP_VAR* var;
SCIP_VAR** lpcands;
SCIP_Real* lpcandssol;
SCIP_Real* lpcandsfrac;
SCIP_Real searchubbound;
SCIP_Real searchavgbound;
SCIP_Real searchbound;
SCIP_Real objval;
SCIP_Real oldobjval;
SCIP_Real frac;
SCIP_Real bestfrac;
SCIP_Bool bestcandmayrounddown;
SCIP_Bool bestcandmayroundup;
SCIP_Bool bestcandroundup;
SCIP_Bool mayrounddown;
SCIP_Bool mayroundup;
SCIP_Bool roundup;
SCIP_Bool lperror;
SCIP_Bool cutoff;
SCIP_Bool backtracked;
SCIP_Longint ncalls;
SCIP_Longint nsolsfound;
SCIP_Longint nlpiterations;
SCIP_Longint maxnlpiterations;
int nlpcands;
int startnlpcands;
int depth;
int maxdepth;
int maxdivedepth;
int divedepth;
SCIP_Real actscore;
SCIP_Real downscore;
SCIP_Real upscore;
SCIP_Real bestactscore;
int bestcand;
int c;
assert(heur != NULL);
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
assert(scip != NULL);
assert(result != NULL);
assert(SCIPhasCurrentNodeLP(scip));
*result = SCIP_DELAYED;
/* do not call heuristic of node was already detected to be infeasible */
if( nodeinfeasible )
return SCIP_OKAY;
/* only call heuristic, if an optimal LP solution is at hand */
if( SCIPgetLPSolstat(scip) != SCIP_LPSOLSTAT_OPTIMAL )
return SCIP_OKAY;
/* only call heuristic, if the LP objective value is smaller than the cutoff bound */
if( SCIPisGE(scip, SCIPgetLPObjval(scip), SCIPgetCutoffbound(scip)) )
return SCIP_OKAY;
/* only call heuristic, if the LP solution is basic (which allows fast resolve in diving) */
if( !SCIPisLPSolBasic(scip) )
return SCIP_OKAY;
/* don't dive two times at the same node */
if( SCIPgetLastDivenode(scip) == SCIPgetNNodes(scip) && SCIPgetDepth(scip) > 0 )
return SCIP_OKAY;
*result = SCIP_DIDNOTRUN;
/* get heuristic's data */
heurdata = SCIPheurGetData(heur);
assert(heurdata != NULL);
/* only try to dive, if we are in the correct part of the tree, given by minreldepth and maxreldepth */
depth = SCIPgetDepth(scip);
maxdepth = SCIPgetMaxDepth(scip);
maxdepth = MAX(maxdepth, 30);
if( depth < heurdata->minreldepth*maxdepth || depth > heurdata->maxreldepth*maxdepth )
return SCIP_OKAY;
/* calculate the maximal number of LP iterations until heuristic is aborted */
nlpiterations = SCIPgetNNodeLPIterations(scip);
ncalls = SCIPheurGetNCalls(heur);
nsolsfound = 10*SCIPheurGetNBestSolsFound(heur) + heurdata->nsuccess;
maxnlpiterations = (SCIP_Longint)((1.0 + 10.0*(nsolsfound+1.0)/(ncalls+1.0)) * heurdata->maxlpiterquot * nlpiterations);
maxnlpiterations += heurdata->maxlpiterofs;
/* don't try to dive, if we took too many LP iterations during diving */
if( heurdata->nlpiterations >= maxnlpiterations )
return SCIP_OKAY;
/* allow at least a certain number of LP iterations in this dive */
maxnlpiterations = MAX(maxnlpiterations, heurdata->nlpiterations + MINLPITER);
/* get fractional variables that should be integral */
SCIP_CALL( SCIPgetLPBranchCands(scip, &lpcands, &lpcandssol, &lpcandsfrac, &nlpcands, NULL, NULL) );
/* don't try to dive, if there are no fractional variables */
if( nlpcands == 0 )
return SCIP_OKAY;
/* calculate the objective search bound */
if( SCIPgetNSolsFound(scip) == 0 )
{
if( heurdata->maxdiveubquotnosol > 0.0 )
searchubbound = SCIPgetLowerbound(scip)
+ heurdata->maxdiveubquotnosol * (SCIPgetCutoffbound(scip) - SCIPgetLowerbound(scip));
else
searchubbound = SCIPinfinity(scip);
if( heurdata->maxdiveavgquotnosol > 0.0 )
searchavgbound = SCIPgetLowerbound(scip)
+ heurdata->maxdiveavgquotnosol * (SCIPgetAvgLowerbound(scip) - SCIPgetLowerbound(scip));
else
searchavgbound = SCIPinfinity(scip);
}
else
{
if( heurdata->maxdiveubquot > 0.0 )
searchubbound = SCIPgetLowerbound(scip)
+ heurdata->maxdiveubquot * (SCIPgetCutoffbound(scip) - SCIPgetLowerbound(scip));
else
searchubbound = SCIPinfinity(scip);
if( heurdata->maxdiveavgquot > 0.0 )
searchavgbound = SCIPgetLowerbound(scip)
+ heurdata->maxdiveavgquot * (SCIPgetAvgLowerbound(scip) - SCIPgetLowerbound(scip));
else
searchavgbound = SCIPinfinity(scip);
}
searchbound = MIN(searchubbound, searchavgbound);
if( SCIPisObjIntegral(scip) )
searchbound = SCIPceil(scip, searchbound);
/* calculate the maximal diving depth: 10 * min{number of integer variables, max depth} */
maxdivedepth = SCIPgetNBinVars(scip) + SCIPgetNIntVars(scip);
maxdivedepth = MIN(maxdivedepth, maxdepth);
maxdivedepth *= 10;
*result = SCIP_DIDNOTFIND;
/* start diving */
SCIP_CALL( SCIPstartProbing(scip) );
/* enables collection of variable statistics during probing */
SCIPenableVarHistory(scip);
/* get LP objective value */
lpsolstat = SCIP_LPSOLSTAT_OPTIMAL;
objval = SCIPgetLPObjval(scip);
SCIPdebugMessage("(node %"SCIP_LONGINT_FORMAT") executing actconsdiving heuristic: depth=%d, %d fractionals, dualbound=%g, avgbound=%g, cutoffbound=%g, searchbound=%g\n",
SCIPgetNNodes(scip), SCIPgetDepth(scip), nlpcands, SCIPgetDualbound(scip), SCIPgetAvgDualbound(scip),
SCIPretransformObj(scip, SCIPgetCutoffbound(scip)), SCIPretransformObj(scip, searchbound));
/* dive as long we are in the given objective, depth and iteration limits and fractional variables exist, but
* - if possible, we dive at least with the depth 10
* - if the number of fractional variables decreased at least with 1 variable per 2 dive depths, we continue diving
*/
lperror = FALSE;
cutoff = FALSE;
divedepth = 0;
bestcandmayrounddown = FALSE;
bestcandmayroundup = FALSE;
startnlpcands = nlpcands;
while( !lperror && !cutoff && lpsolstat == SCIP_LPSOLSTAT_OPTIMAL && nlpcands > 0
&& (divedepth < 10
|| nlpcands <= startnlpcands - divedepth/2
|| (divedepth < maxdivedepth && heurdata->nlpiterations < maxnlpiterations && objval < searchbound))
&& !SCIPisStopped(scip) )
{
divedepth++;
SCIP_CALL( SCIPnewProbingNode(scip) );
/* choose variable fixing:
* - prefer variables that may not be rounded without destroying LP feasibility:
* - of these variables, round variable with least number of locks in corresponding direction
* - if all remaining fractional variables may be rounded without destroying LP feasibility:
* - round variable with least number of locks in opposite of its feasible rounding direction
*/
bestcand = -1;
bestactscore = -1.0;
bestfrac = SCIP_INVALID;
bestcandmayrounddown = TRUE;
bestcandmayroundup = TRUE;
bestcandroundup = FALSE;
for( c = 0; c < nlpcands; ++c )
{
var = lpcands[c];
mayrounddown = SCIPvarMayRoundDown(var);
mayroundup = SCIPvarMayRoundUp(var);
frac = lpcandsfrac[c];
if( mayrounddown || mayroundup )
{
/* the candidate may be rounded: choose this candidate only, if the best candidate may also be rounded */
if( bestcandmayrounddown || bestcandmayroundup )
{
/* choose rounding direction:
* - if variable may be rounded in both directions, round corresponding to the fractionality
* - otherwise, round in the infeasible direction, because feasible direction is tried by rounding
* the current fractional solution
*/
if( mayrounddown && mayroundup )
roundup = (frac > 0.5);
else
roundup = mayrounddown;
if( roundup )
frac = 1.0 - frac;
actscore = getNActiveConsScore(scip, var, &downscore, &upscore);
/* penalize too small fractions */
if( frac < 0.01 )
actscore *= 0.01;
/* prefer decisions on binary variables */
if( !SCIPvarIsBinary(var) )
actscore *= 0.01;
/* check, if candidate is new best candidate */
assert(0.0 < frac && frac < 1.0);
if( SCIPisGT(scip, actscore, bestactscore) || (SCIPisGE(scip, actscore, bestactscore) && frac < bestfrac) )
{
bestcand = c;
bestactscore = actscore;
bestfrac = frac;
bestcandmayrounddown = mayrounddown;
bestcandmayroundup = mayroundup;
bestcandroundup = roundup;
}
}
}
else
{
/* the candidate may not be rounded */
actscore = getNActiveConsScore(scip, var, &downscore, &upscore);
roundup = (downscore < upscore);
if( roundup )
frac = 1.0 - frac;
/* penalize too small fractions */
if( frac < 0.01 )
actscore *= 0.01;
/* prefer decisions on binary variables */
if( !SCIPvarIsBinary(var) )
actscore *= 0.01;
/* check, if candidate is new best candidate: prefer unroundable candidates in any case */
assert(0.0 < frac && frac < 1.0);
if( bestcandmayrounddown || bestcandmayroundup || SCIPisGT(scip, actscore, bestactscore) ||
(SCIPisGE(scip, actscore, bestactscore) && frac < bestfrac) )
{
bestcand = c;
bestactscore = actscore;
bestfrac = frac;
bestcandmayrounddown = FALSE;
bestcandmayroundup = FALSE;
bestcandroundup = roundup;
}
assert(bestfrac < SCIP_INVALID);
}
}
assert(bestcand != -1);
/* if all candidates are roundable, try to round the solution */
if( bestcandmayrounddown || bestcandmayroundup )
{
SCIP_Bool success;
/* create solution from diving LP and try to round it */
SCIP_CALL( SCIPlinkLPSol(scip, heurdata->sol) );
SCIP_CALL( SCIProundSol(scip, heurdata->sol, &success) );
if( success )
{
SCIPdebugMessage("actconsdiving found roundable primal solution: obj=%g\n", SCIPgetSolOrigObj(scip, heurdata->sol));
/* try to add solution to SCIP */
SCIP_CALL( SCIPtrySol(scip, heurdata->sol, FALSE, FALSE, FALSE, FALSE, &success) );
/* check, if solution was feasible and good enough */
if( success )
{
SCIPdebugMessage(" -> solution was feasible and good enough\n");
*result = SCIP_FOUNDSOL;
}
}
}
assert(bestcand != -1);
var = lpcands[bestcand];
backtracked = FALSE;
do
{
/* if the variable is already fixed or if the solution value is outside the domain, numerical troubles may have
* occured or variable was fixed by propagation while backtracking => Abort diving!
*/
if( SCIPvarGetLbLocal(var) >= SCIPvarGetUbLocal(var) - 0.5 )
{
SCIPdebugMessage("Selected variable <%s> already fixed to [%g,%g] (solval: %.9f), diving aborted \n",
SCIPvarGetName(var), SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var), lpcandssol[bestcand]);
cutoff = TRUE;
break;
}
if( SCIPisFeasLT(scip, lpcandssol[bestcand], SCIPvarGetLbLocal(var)) || SCIPisFeasGT(scip, lpcandssol[bestcand], SCIPvarGetUbLocal(var)) )
{
SCIPdebugMessage("selected variable's <%s> solution value is outside the domain [%g,%g] (solval: %.9f), diving aborted\n",
SCIPvarGetName(var), SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var), lpcandssol[bestcand]);
assert(backtracked);
break;
}
/* apply rounding of best candidate */
if( bestcandroundup == !backtracked )
{
/* round variable up */
SCIPdebugMessage(" dive %d/%d, LP iter %"SCIP_LONGINT_FORMAT"/%"SCIP_LONGINT_FORMAT": var <%s>, round=%u/%u, sol=%g, oldbounds=[%g,%g], newbounds=[%g,%g]\n",
divedepth, maxdivedepth, heurdata->nlpiterations, maxnlpiterations,
SCIPvarGetName(var), bestcandmayrounddown, bestcandmayroundup,
lpcandssol[bestcand], SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var),
SCIPfeasCeil(scip, lpcandssol[bestcand]), SCIPvarGetUbLocal(var));
SCIP_CALL( SCIPchgVarLbProbing(scip, var, SCIPfeasCeil(scip, lpcandssol[bestcand])) );
}
else
{
/* round variable down */
SCIPdebugMessage(" dive %d/%d, LP iter %"SCIP_LONGINT_FORMAT"/%"SCIP_LONGINT_FORMAT": var <%s>, round=%u/%u, sol=%g, oldbounds=[%g,%g], newbounds=[%g,%g]\n",
divedepth, maxdivedepth, heurdata->nlpiterations, maxnlpiterations,
SCIPvarGetName(var), bestcandmayrounddown, bestcandmayroundup,
lpcandssol[bestcand], SCIPvarGetLbLocal(var), SCIPvarGetUbLocal(var),
SCIPvarGetLbLocal(var), SCIPfeasFloor(scip, lpcandssol[bestcand]));
SCIP_CALL( SCIPchgVarUbProbing(scip, lpcands[bestcand], SCIPfeasFloor(scip, lpcandssol[bestcand])) );
}
/* apply domain propagation */
SCIP_CALL( SCIPpropagateProbing(scip, 0, &cutoff, NULL) );
if( !cutoff )
{
/* resolve the diving LP */
/* Errors in the LP solver should not kill the overall solving process, if the LP is just needed for a heuristic.
* Hence in optimized mode, the return code is caught and a warning is printed, only in debug mode, SCIP will stop.
*/
#ifdef NDEBUG
SCIP_RETCODE retstat;
nlpiterations = SCIPgetNLPIterations(scip);
retstat = SCIPsolveProbingLP(scip, MAX((int)(maxnlpiterations - heurdata->nlpiterations), MINLPITER), &lperror, &cutoff);
if( retstat != SCIP_OKAY )
{
SCIPwarningMessage(scip, "Error while solving LP in Actconsdiving heuristic; LP solve terminated with code <%d>\n",retstat);
}
#else
nlpiterations = SCIPgetNLPIterations(scip);
SCIP_CALL( SCIPsolveProbingLP(scip, MAX((int)(maxnlpiterations - heurdata->nlpiterations), MINLPITER), &lperror, &cutoff) );
#endif
if( lperror )
break;
/* update iteration count */
heurdata->nlpiterations += SCIPgetNLPIterations(scip) - nlpiterations;
/* get LP solution status, objective value, and fractional variables, that should be integral */
lpsolstat = SCIPgetLPSolstat(scip);
assert(cutoff || (lpsolstat != SCIP_LPSOLSTAT_OBJLIMIT && lpsolstat != SCIP_LPSOLSTAT_INFEASIBLE &&
(lpsolstat != SCIP_LPSOLSTAT_OPTIMAL || SCIPisLT(scip, SCIPgetLPObjval(scip), SCIPgetCutoffbound(scip)))));
}
/* perform backtracking if a cutoff was detected */
if( cutoff && !backtracked && heurdata->backtrack )
{
SCIPdebugMessage(" *** cutoff detected at level %d - backtracking\n", SCIPgetProbingDepth(scip));
SCIP_CALL( SCIPbacktrackProbing(scip, SCIPgetProbingDepth(scip)-1) );
SCIP_CALL( SCIPnewProbingNode(scip) );
backtracked = TRUE;
}
else
backtracked = FALSE;
}
while( backtracked );
if( !lperror && !cutoff && lpsolstat == SCIP_LPSOLSTAT_OPTIMAL )
{
/* get new objective value */
oldobjval = objval;
objval = SCIPgetLPObjval(scip);
/* update pseudo cost values */
if( SCIPisGT(scip, objval, oldobjval) )
{
if( bestcandroundup )
{
SCIP_CALL( SCIPupdateVarPseudocost(scip, lpcands[bestcand], 1.0-lpcandsfrac[bestcand],
objval - oldobjval, 1.0) );
}
else
{
SCIP_CALL( SCIPupdateVarPseudocost(scip, lpcands[bestcand], 0.0-lpcandsfrac[bestcand],
objval - oldobjval, 1.0) );
}
}
/* get new fractional variables */
SCIP_CALL( SCIPgetLPBranchCands(scip, &lpcands, &lpcandssol, &lpcandsfrac, &nlpcands, NULL, NULL) );
}
SCIPdebugMessage(" -> lpsolstat=%d, objval=%g/%g, nfrac=%d\n", lpsolstat, objval, searchbound, nlpcands);
}
/* check if a solution has been found */
if( nlpcands == 0 && !lperror && !cutoff && lpsolstat == SCIP_LPSOLSTAT_OPTIMAL )
{
SCIP_Bool success;
/* create solution from diving LP */
SCIP_CALL( SCIPlinkLPSol(scip, heurdata->sol) );
SCIPdebugMessage("actconsdiving found primal solution: obj=%g\n", SCIPgetSolOrigObj(scip, heurdata->sol));
/* try to add solution to SCIP */
SCIP_CALL( SCIPtrySol(scip, heurdata->sol, FALSE, FALSE, FALSE, FALSE, &success) );
/* check, if solution was feasible and good enough */
if( success )
{
SCIPdebugMessage(" -> solution was feasible and good enough\n");
*result = SCIP_FOUNDSOL;
}
}
/* end diving */
SCIP_CALL( SCIPendProbing(scip) );
if( *result == SCIP_FOUNDSOL )
heurdata->nsuccess++;
SCIPdebugMessage("(node %"SCIP_LONGINT_FORMAT") finished actconsdiving heuristic: %d fractionals, dive %d/%d, LP iter %"SCIP_LONGINT_FORMAT"/%"SCIP_LONGINT_FORMAT", objval=%g/%g, lpsolstat=%d, cutoff=%u\n",
SCIPgetNNodes(scip), nlpcands, divedepth, maxdivedepth, heurdata->nlpiterations, maxnlpiterations,
SCIPretransformObj(scip, objval), SCIPretransformObj(scip, searchbound), lpsolstat, cutoff);
return SCIP_OKAY;
}
/*
* heuristic specific interface methods
*/
/** creates the actconsdiving heuristic and includes it in SCIP */
SCIP_RETCODE SCIPincludeHeurActconsdiving(
SCIP* scip /**< SCIP data structure */
)
{
SCIP_HEURDATA* heurdata;
SCIP_HEUR* heur;
/* create actconsdiving 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, heurExecActconsdiving, heurdata) );
assert(heur != NULL);
/* set non-NULL pointers to callback methods */
SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyActconsdiving) );
SCIP_CALL( SCIPsetHeurFree(scip, heur, heurFreeActconsdiving) );
SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitActconsdiving) );
SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitActconsdiving) );
/* actconsdiving heuristic parameters */
SCIP_CALL( SCIPaddRealParam(scip,
"heuristics/actconsdiving/minreldepth",
"minimal relative depth to start diving",
&heurdata->minreldepth, TRUE, DEFAULT_MINRELDEPTH, 0.0, 1.0, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip,
"heuristics/actconsdiving/maxreldepth",
"maximal relative depth to start diving",
&heurdata->maxreldepth, TRUE, DEFAULT_MAXRELDEPTH, 0.0, 1.0, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip,
"heuristics/actconsdiving/maxlpiterquot",
"maximal fraction of diving LP iterations compared to node LP iterations",
&heurdata->maxlpiterquot, FALSE, DEFAULT_MAXLPITERQUOT, 0.0, SCIP_REAL_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddIntParam(scip,
"heuristics/actconsdiving/maxlpiterofs",
"additional number of allowed LP iterations",
&heurdata->maxlpiterofs, FALSE, DEFAULT_MAXLPITEROFS, 0, INT_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip,
"heuristics/actconsdiving/maxdiveubquot",
"maximal quotient (curlowerbound - lowerbound)/(cutoffbound - lowerbound) where diving is performed (0.0: no limit)",
&heurdata->maxdiveubquot, TRUE, DEFAULT_MAXDIVEUBQUOT, 0.0, 1.0, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip,
"heuristics/actconsdiving/maxdiveavgquot",
"maximal quotient (curlowerbound - lowerbound)/(avglowerbound - lowerbound) where diving is performed (0.0: no limit)",
&heurdata->maxdiveavgquot, TRUE, DEFAULT_MAXDIVEAVGQUOT, 0.0, SCIP_REAL_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip,
"heuristics/actconsdiving/maxdiveubquotnosol",
"maximal UBQUOT when no solution was found yet (0.0: no limit)",
&heurdata->maxdiveubquotnosol, TRUE, DEFAULT_MAXDIVEUBQUOTNOSOL, 0.0, 1.0, NULL, NULL) );
SCIP_CALL( SCIPaddRealParam(scip,
"heuristics/actconsdiving/maxdiveavgquotnosol",
"maximal AVGQUOT when no solution was found yet (0.0: no limit)",
&heurdata->maxdiveavgquotnosol, TRUE, DEFAULT_MAXDIVEAVGQUOTNOSOL, 0.0, SCIP_REAL_MAX, NULL, NULL) );
SCIP_CALL( SCIPaddBoolParam(scip,
"heuristics/actconsdiving/backtrack",
"use one level of backtracking if infeasibility is encountered?",
&heurdata->backtrack, FALSE, DEFAULT_BACKTRACK, NULL, NULL) );
return SCIP_OKAY;
}