-
Notifications
You must be signed in to change notification settings - Fork 3
/
heur_shifting.c
870 lines (747 loc) · 30.3 KB
/
heur_shifting.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
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* */
/* 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_shifting.c
* @brief LP rounding heuristic that tries to recover from intermediate infeasibilities and shifts continuous variables
* @author Tobias Achterberg
*/
/*---+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
#include <assert.h>
#include <string.h>
#include "scip/heur_shifting.h"
#define HEUR_NAME "shifting"
#define HEUR_DESC "LP rounding heuristic with infeasibility recovering also using continuous variables"
#define HEUR_DISPCHAR 's'
#define HEUR_PRIORITY -5000
#define HEUR_FREQ 10
#define HEUR_FREQOFS 0
#define HEUR_MAXDEPTH -1
#define HEUR_TIMING SCIP_HEURTIMING_DURINGLPLOOP
#define HEUR_USESSUBSCIP FALSE /**< does the heuristic use a secondary SCIP instance? */
#define MAXSHIFTINGS 50 /**< maximal number of non improving shiftings */
#define WEIGHTFACTOR 1.1
/* locally defined heuristic data */
struct SCIP_HeurData
{
SCIP_SOL* sol; /**< working solution */
SCIP_Longint lastlp; /**< last LP number where the heuristic was applied */
unsigned int randseed; /**< seed value for random number generator */
};
/*
* local methods
*/
/** update row violation arrays after a row's activity value changed */
static
void updateViolations(
SCIP* scip, /**< SCIP data structure */
SCIP_ROW* row, /**< LP row */
SCIP_ROW** violrows, /**< array with currently violated rows */
int* violrowpos, /**< position of LP rows in violrows array */
int* nviolrows, /**< pointer to the number of currently violated rows */
SCIP_Real oldactivity, /**< old activity value of LP row */
SCIP_Real newactivity /**< new activity value of LP row */
)
{
SCIP_Real lhs;
SCIP_Real rhs;
SCIP_Bool oldviol;
SCIP_Bool newviol;
assert(violrows != NULL);
assert(violrowpos != NULL);
assert(nviolrows != NULL);
lhs = SCIProwGetLhs(row);
rhs = SCIProwGetRhs(row);
oldviol = (SCIPisFeasLT(scip, oldactivity, lhs) || SCIPisFeasGT(scip, oldactivity, rhs));
newviol = (SCIPisFeasLT(scip, newactivity, lhs) || SCIPisFeasGT(scip, newactivity, rhs));
if( oldviol != newviol )
{
int rowpos;
int violpos;
rowpos = SCIProwGetLPPos(row);
assert(rowpos >= 0);
if( oldviol )
{
/* the row violation was repaired: remove row from violrows array, decrease violation count */
violpos = violrowpos[rowpos];
assert(0 <= violpos && violpos < *nviolrows);
assert(violrows[violpos] == row);
violrowpos[rowpos] = -1;
if( violpos != *nviolrows-1 )
{
violrows[violpos] = violrows[*nviolrows-1];
violrowpos[SCIProwGetLPPos(violrows[violpos])] = violpos;
}
(*nviolrows)--;
}
else
{
/* the row is now violated: add row to violrows array, increase violation count */
assert(violrowpos[rowpos] == -1);
violrows[*nviolrows] = row;
violrowpos[rowpos] = *nviolrows;
(*nviolrows)++;
}
}
}
/** update row activities after a variable's solution value changed */
static
SCIP_RETCODE updateActivities(
SCIP* scip, /**< SCIP data structure */
SCIP_Real* activities, /**< LP row activities */
SCIP_ROW** violrows, /**< array with currently violated rows */
int* violrowpos, /**< position of LP rows in violrows array */
int* nviolrows, /**< pointer to the number of currently violated rows */
int nlprows, /**< number of rows in current LP */
SCIP_VAR* var, /**< variable that has been changed */
SCIP_Real oldsolval, /**< old solution value of variable */
SCIP_Real newsolval /**< new solution value of variable */
)
{
SCIP_COL* col;
SCIP_ROW** colrows;
SCIP_Real* colvals;
SCIP_Real delta;
int ncolrows;
int r;
assert(activities != NULL);
assert(nviolrows != NULL);
assert(0 <= *nviolrows && *nviolrows <= nlprows);
delta = newsolval - oldsolval;
col = SCIPvarGetCol(var);
colrows = SCIPcolGetRows(col);
colvals = SCIPcolGetVals(col);
ncolrows = SCIPcolGetNLPNonz(col);
assert(ncolrows == 0 || (colrows != NULL && colvals != NULL));
for( r = 0; r < ncolrows; ++r )
{
SCIP_ROW* row;
int rowpos;
row = colrows[r];
rowpos = SCIProwGetLPPos(row);
assert(-1 <= rowpos && rowpos < nlprows);
if( rowpos >= 0 && !SCIProwIsLocal(row) )
{
SCIP_Real oldactivity;
SCIP_Real newactivity;
assert(SCIProwIsInLP(row));
/* update row activity */
oldactivity = activities[rowpos];
if( !SCIPisInfinity(scip, -oldactivity) && !SCIPisInfinity(scip, oldactivity) )
{
newactivity = oldactivity + delta * colvals[r];
if( SCIPisInfinity(scip, newactivity) )
newactivity = SCIPinfinity(scip);
else if( SCIPisInfinity(scip, -newactivity) )
newactivity = -SCIPinfinity(scip);
activities[rowpos] = newactivity;
/* update row violation arrays */
updateViolations(scip, row, violrows, violrowpos, nviolrows, oldactivity, newactivity);
}
}
}
return SCIP_OKAY;
}
/** returns a variable, that pushes activity of the row in the given direction with minimal negative impact on other rows;
* if variables have equal impact, chooses the one with best objective value improvement in corresponding direction;
* prefer fractional integers over other variables in order to become integral during the process;
* shifting in a direction is forbidden, if this forces the objective value over the upper bound, or if the variable
* was already shifted in the opposite direction
*/
static
SCIP_RETCODE selectShifting(
SCIP* scip, /**< SCIP data structure */
SCIP_SOL* sol, /**< primal solution */
SCIP_ROW* row, /**< LP row */
SCIP_Real rowactivity, /**< activity of LP row */
int direction, /**< should the activity be increased (+1) or decreased (-1)? */
SCIP_Real* nincreases, /**< array with weighted number of increasings per variables */
SCIP_Real* ndecreases, /**< array with weighted number of decreasings per variables */
SCIP_Real increaseweight, /**< current weight of increase/decrease updates */
SCIP_VAR** shiftvar, /**< pointer to store the shifting variable, returns NULL if impossible */
SCIP_Real* oldsolval, /**< pointer to store old solution value of shifting variable */
SCIP_Real* newsolval /**< pointer to store new (shifted) solution value of shifting variable */
)
{
SCIP_COL** rowcols;
SCIP_Real* rowvals;
int nrowcols;
SCIP_Real activitydelta;
SCIP_Real bestshiftscore;
SCIP_Real bestdeltaobj;
int c;
assert(direction == +1 || direction == -1);
assert(nincreases != NULL);
assert(ndecreases != NULL);
assert(shiftvar != NULL);
assert(oldsolval != NULL);
assert(newsolval != NULL);
/* get row entries */
rowcols = SCIProwGetCols(row);
rowvals = SCIProwGetVals(row);
nrowcols = SCIProwGetNLPNonz(row);
/* calculate how much the activity must be shifted in order to become feasible */
activitydelta = (direction == +1 ? SCIProwGetLhs(row) - rowactivity : SCIProwGetRhs(row) - rowactivity);
assert((direction == +1 && SCIPisPositive(scip, activitydelta))
|| (direction == -1 && SCIPisNegative(scip, activitydelta)));
/* select shifting variable */
bestshiftscore = SCIP_REAL_MAX;
bestdeltaobj = SCIPinfinity(scip);
*shiftvar = NULL;
*newsolval = 0.0;
*oldsolval = 0.0;
for( c = 0; c < nrowcols; ++c )
{
SCIP_COL* col;
SCIP_VAR* var;
SCIP_Real val;
SCIP_Real solval;
SCIP_Real shiftval;
SCIP_Real shiftscore;
SCIP_Bool isinteger;
SCIP_Bool isfrac;
SCIP_Bool increase;
col = rowcols[c];
var = SCIPcolGetVar(col);
val = rowvals[c];
assert(!SCIPisZero(scip, val));
solval = SCIPgetSolVal(scip, sol, var);
isinteger = (SCIPvarGetType(var) == SCIP_VARTYPE_BINARY || SCIPvarGetType(var) == SCIP_VARTYPE_INTEGER);
isfrac = isinteger && !SCIPisFeasIntegral(scip, solval);
increase = (direction * val > 0.0);
/* calculate the score of the shifting (prefer smaller values) */
if( isfrac )
shiftscore = increase ? -1.0 / (SCIPvarGetNLocksUp(var) + 1.0) :
-1.0 / (SCIPvarGetNLocksDown(var) + 1.0);
else
{
int probindex;
probindex = SCIPvarGetProbindex(var);
if( increase )
shiftscore = ndecreases[probindex]/increaseweight;
else
shiftscore = nincreases[probindex]/increaseweight;
if( isinteger )
shiftscore += 1.0;
}
if( shiftscore <= bestshiftscore )
{
SCIP_Real deltaobj;
if( !increase )
{
/* shifting down */
assert(direction * val < 0.0);
if( isfrac )
shiftval = SCIPfeasFloor(scip, solval);
else
{
SCIP_Real lb;
assert(activitydelta/val < 0.0);
shiftval = solval + activitydelta/val;
assert(shiftval <= solval); /* may be equal due to numerical digit erasement in the subtraction */
if( SCIPvarIsIntegral(var) )
shiftval = SCIPfeasFloor(scip, shiftval);
lb = SCIPvarGetLbGlobal(var);
shiftval = MAX(shiftval, lb);
}
}
else
{
/* shifting up */
assert(direction * val > 0.0);
if( isfrac )
shiftval = SCIPfeasCeil(scip, solval);
else
{
SCIP_Real ub;
assert(activitydelta/val > 0.0);
shiftval = solval + activitydelta/val;
assert(shiftval >= solval); /* may be equal due to numerical digit erasement in the subtraction */
if( SCIPvarIsIntegral(var) )
shiftval = SCIPfeasCeil(scip, shiftval);
ub = SCIPvarGetUbGlobal(var);
shiftval = MIN(shiftval, ub);
}
}
if( SCIPisEQ(scip, shiftval, solval) )
continue;
deltaobj = SCIPvarGetObj(var) * (shiftval - solval);
if( shiftscore < bestshiftscore || deltaobj < bestdeltaobj )
{
bestshiftscore = shiftscore;
bestdeltaobj = deltaobj;
*shiftvar = var;
*oldsolval = solval;
*newsolval = shiftval;
}
}
}
return SCIP_OKAY;
}
/** returns a fractional variable, that has most impact on rows in opposite direction, i.e. that is most crucial to
* fix in the other direction;
* if variables have equal impact, chooses the one with best objective value improvement in corresponding direction;
* shifting in a direction is forbidden, if this forces the objective value over the upper bound
*/
static
SCIP_RETCODE selectEssentialRounding(
SCIP* scip, /**< SCIP data structure */
SCIP_SOL* sol, /**< primal solution */
SCIP_Real minobj, /**< minimal objective value possible after shifting remaining fractional vars */
SCIP_VAR** lpcands, /**< fractional variables in LP */
int nlpcands, /**< number of fractional variables in LP */
SCIP_VAR** shiftvar, /**< pointer to store the shifting variable, returns NULL if impossible */
SCIP_Real* oldsolval, /**< old (fractional) solution value of shifting variable */
SCIP_Real* newsolval /**< new (shifted) solution value of shifting variable */
)
{
SCIP_VAR* var;
SCIP_Real solval;
SCIP_Real shiftval;
SCIP_Real obj;
SCIP_Real deltaobj;
SCIP_Real bestdeltaobj;
int maxnlocks;
int nlocks;
int v;
assert(shiftvar != NULL);
assert(oldsolval != NULL);
assert(newsolval != NULL);
/* select shifting variable */
maxnlocks = -1;
bestdeltaobj = SCIPinfinity(scip);
*shiftvar = NULL;
for( v = 0; v < nlpcands; ++v )
{
var = lpcands[v];
assert(SCIPvarGetType(var) == SCIP_VARTYPE_BINARY || SCIPvarGetType(var) == SCIP_VARTYPE_INTEGER);
solval = SCIPgetSolVal(scip, sol, var);
if( !SCIPisFeasIntegral(scip, solval) )
{
obj = SCIPvarGetObj(var);
/* shifting down */
nlocks = SCIPvarGetNLocksUp(var);
if( nlocks >= maxnlocks )
{
shiftval = SCIPfeasFloor(scip, solval);
deltaobj = obj * (shiftval - solval);
if( (nlocks > maxnlocks || deltaobj < bestdeltaobj) && minobj - obj < SCIPgetCutoffbound(scip) )
{
maxnlocks = nlocks;
bestdeltaobj = deltaobj;
*shiftvar = var;
*oldsolval = solval;
*newsolval = shiftval;
}
}
/* shifting up */
nlocks = SCIPvarGetNLocksDown(var);
if( nlocks >= maxnlocks )
{
shiftval = SCIPfeasCeil(scip, solval);
deltaobj = obj * (shiftval - solval);
if( (nlocks > maxnlocks || deltaobj < bestdeltaobj) && minobj + obj < SCIPgetCutoffbound(scip) )
{
maxnlocks = nlocks;
bestdeltaobj = deltaobj;
*shiftvar = var;
*oldsolval = solval;
*newsolval = shiftval;
}
}
}
}
return SCIP_OKAY;
}
/** adds a given value to the fractionality counters of the rows in which the given variable appears */
static
void addFracCounter(
int* nfracsinrow, /**< array to store number of fractional variables per row */
int nlprows, /**< number of rows in LP */
SCIP_VAR* var, /**< variable for which the counting should be updated */
int incval /**< value that should be added to the corresponding array entries */
)
{
SCIP_COL* col;
SCIP_ROW** rows;
int nrows;
int r;
col = SCIPvarGetCol(var);
rows = SCIPcolGetRows(col);
nrows = SCIPcolGetNLPNonz(col);
for( r = 0; r < nrows; ++r )
{
int rowidx;
rowidx = SCIProwGetLPPos(rows[r]);
assert(0 <= rowidx && rowidx < nlprows);
nfracsinrow[rowidx] += incval;
assert(nfracsinrow[rowidx] >= 0);
}
}
/*
* Callback methods
*/
/** copy method for primal heuristic plugins (called when SCIP copies plugins) */
static
SCIP_DECL_HEURCOPY(heurCopyShifting)
{ /*lint --e{715}*/
assert(scip != NULL);
assert(heur != NULL);
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
/* call inclusion method of primal heuristic */
SCIP_CALL( SCIPincludeHeurShifting(scip) );
return SCIP_OKAY;
}
/** initialization method of primal heuristic (called after problem was transformed) */
static
SCIP_DECL_HEURINIT(heurInitShifting) /*lint --e{715}*/
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
assert(SCIPheurGetData(heur) == NULL);
/* create heuristic data */
SCIP_CALL( SCIPallocMemory(scip, &heurdata) );
SCIP_CALL( SCIPcreateSol(scip, &heurdata->sol, heur) );
heurdata->lastlp = -1;
heurdata->randseed = 0;
SCIPheurSetData(heur, heurdata);
return SCIP_OKAY;
}
/** deinitialization method of primal heuristic (called before transformed problem is freed) */
static
SCIP_DECL_HEUREXIT(heurExitShifting) /*lint --e{715}*/
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
/* free heuristic data */
heurdata = SCIPheurGetData(heur);
assert(heurdata != NULL);
SCIP_CALL( SCIPfreeSol(scip, &heurdata->sol) );
SCIPfreeMemory(scip, &heurdata);
SCIPheurSetData(heur, NULL);
return SCIP_OKAY;
}
/** solving process initialization method of primal heuristic (called when branch and bound process is about to begin) */
static
SCIP_DECL_HEURINITSOL(heurInitsolShifting)
{
SCIP_HEURDATA* heurdata;
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
heurdata = SCIPheurGetData(heur);
assert(heurdata != NULL);
heurdata->lastlp = -1;
return SCIP_OKAY;
}
/** execution method of primal heuristic */
static
SCIP_DECL_HEUREXEC(heurExecShifting) /*lint --e{715}*/
{ /*lint --e{715}*/
SCIP_HEURDATA* heurdata;
SCIP_SOL* sol;
SCIP_VAR** lpcands;
SCIP_Real* lpcandssol;
SCIP_ROW** lprows;
SCIP_Real* activities;
SCIP_ROW** violrows;
SCIP_Real* nincreases;
SCIP_Real* ndecreases;
int* violrowpos;
int* nfracsinrow;
SCIP_Real increaseweight;
SCIP_Real obj;
SCIP_Real bestshiftval;
SCIP_Real minobj;
int nlpcands;
int nlprows;
int nvars;
int nfrac;
int nviolrows;
int nprevviolrows;
int minnviolrows;
int nnonimprovingshifts;
int c;
int r;
SCIP_Longint nlps;
SCIP_Longint ncalls;
SCIP_Longint nsolsfound;
SCIP_Longint nnodes;
assert(strcmp(SCIPheurGetName(heur), HEUR_NAME) == 0);
assert(scip != NULL);
assert(result != NULL);
assert(SCIPhasCurrentNodeLP(scip));
*result = SCIP_DIDNOTRUN;
/* 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;
/* get heuristic data */
heurdata = SCIPheurGetData(heur);
assert(heurdata != NULL);
/* don't call heuristic, if we have already processed the current LP solution */
nlps = SCIPgetNLPs(scip);
if( nlps == heurdata->lastlp )
return SCIP_OKAY;
heurdata->lastlp = nlps;
/* don't call heuristic, if it was not successful enough in the past */
ncalls = SCIPheurGetNCalls(heur);
nsolsfound = 10*SCIPheurGetNBestSolsFound(heur) + SCIPheurGetNSolsFound(heur);
nnodes = SCIPgetNNodes(scip);
if( nnodes % ((ncalls/100)/(nsolsfound+1)+1) != 0 )
return SCIP_OKAY;
/* get fractional variables, that should be integral */
/* todo check if heuristic should include implicit integer variables for its calculations */
SCIP_CALL( SCIPgetLPBranchCands(scip, &lpcands, &lpcandssol, NULL, &nlpcands, NULL, NULL) );
nfrac = nlpcands;
/* only call heuristic, if LP solution is fractional */
if( nfrac == 0 )
return SCIP_OKAY;
*result = SCIP_DIDNOTFIND;
/* get LP rows */
SCIP_CALL( SCIPgetLPRowsData(scip, &lprows, &nlprows) );
SCIPdebugMessage("executing shifting heuristic: %d LP rows, %d fractionals\n", nlprows, nfrac);
/* get memory for activities, violated rows, and row violation positions */
nvars = SCIPgetNVars(scip);
SCIP_CALL( SCIPallocBufferArray(scip, &activities, nlprows) );
SCIP_CALL( SCIPallocBufferArray(scip, &violrows, nlprows) );
SCIP_CALL( SCIPallocBufferArray(scip, &violrowpos, nlprows) );
SCIP_CALL( SCIPallocBufferArray(scip, &nfracsinrow, nlprows) );
SCIP_CALL( SCIPallocBufferArray(scip, &nincreases, nvars) );
SCIP_CALL( SCIPallocBufferArray(scip, &ndecreases, nvars) );
BMSclearMemoryArray(nfracsinrow, nlprows);
BMSclearMemoryArray(nincreases, nvars);
BMSclearMemoryArray(ndecreases, nvars);
/* get the activities for all globally valid rows;
* the rows should be feasible, but due to numerical inaccuracies in the LP solver, they can be violated
*/
nviolrows = 0;
for( r = 0; r < nlprows; ++r )
{
SCIP_ROW* row;
row = lprows[r];
assert(SCIProwGetLPPos(row) == r);
if( !SCIProwIsLocal(row) )
{
activities[r] = SCIPgetRowActivity(scip, row);
if( SCIPisFeasLT(scip, activities[r], SCIProwGetLhs(row))
|| SCIPisFeasGT(scip, activities[r], SCIProwGetRhs(row)) )
{
violrows[nviolrows] = row;
violrowpos[r] = nviolrows;
nviolrows++;
}
else
violrowpos[r] = -1;
}
}
/* calc the current number of fractional variables in rows */
for( c = 0; c < nlpcands; ++c )
addFracCounter(nfracsinrow, nlprows, lpcands[c], +1);
/* get the working solution from heuristic's local data */
sol = heurdata->sol;
assert(sol != NULL);
/* copy the current LP solution to the working solution */
SCIP_CALL( SCIPlinkLPSol(scip, sol) );
/* calculate the minimal objective value possible after rounding fractional variables */
minobj = SCIPgetSolTransObj(scip, sol);
assert(minobj < SCIPgetCutoffbound(scip));
for( c = 0; c < nlpcands; ++c )
{
obj = SCIPvarGetObj(lpcands[c]);
bestshiftval = obj > 0.0 ? SCIPfeasFloor(scip, lpcandssol[c]) : SCIPfeasCeil(scip, lpcandssol[c]);
minobj += obj * (bestshiftval - lpcandssol[c]);
}
/* try to shift remaining variables in order to become/stay feasible */
nnonimprovingshifts = 0;
minnviolrows = INT_MAX;
increaseweight = 1.0;
while( (nfrac > 0 || nviolrows > 0) && nnonimprovingshifts < MAXSHIFTINGS )
{
SCIP_VAR* shiftvar;
SCIP_Real oldsolval;
SCIP_Real newsolval;
SCIP_Bool oldsolvalisfrac;
int probindex;
SCIPdebugMessage("shifting heuristic: nfrac=%d, nviolrows=%d, obj=%g (best possible obj: %g), cutoff=%g\n",
nfrac, nviolrows, SCIPgetSolOrigObj(scip, sol), SCIPretransformObj(scip, minobj),
SCIPretransformObj(scip, SCIPgetCutoffbound(scip)));
nprevviolrows = nviolrows;
/* choose next variable to process:
* - if a violated row exists, shift a variable decreasing the violation, that has least impact on other rows
* - otherwise, shift a variable, that has strongest devastating impact on rows in opposite direction
*/
shiftvar = NULL;
oldsolval = 0.0;
newsolval = 0.0;
if( nviolrows > 0 && (nfrac == 0 || nnonimprovingshifts < MAXSHIFTINGS-1) )
{
SCIP_ROW* row;
int rowidx;
int rowpos;
int direction;
rowidx = -1;
rowpos = -1;
row = NULL;
if( nfrac > 0 )
{
for( rowidx = nviolrows-1; rowidx >= 0; --rowidx )
{
row = violrows[rowidx];
rowpos = SCIProwGetLPPos(row);
assert(violrowpos[rowpos] == rowidx);
if( nfracsinrow[rowpos] > 0 )
break;
}
}
if( rowidx == -1 )
{
rowidx = SCIPgetRandomInt(0, nviolrows-1, &heurdata->randseed);
row = violrows[rowidx];
rowpos = SCIProwGetLPPos(row);
assert(0 <= rowpos && rowpos < nlprows);
assert(violrowpos[rowpos] == rowidx);
assert(nfracsinrow[rowpos] == 0);
}
assert(violrowpos[rowpos] == rowidx);
SCIPdebugMessage("shifting heuristic: try to fix violated row <%s>: %g <= %g <= %g\n",
SCIProwGetName(row), SCIProwGetLhs(row), activities[rowpos], SCIProwGetRhs(row));
SCIPdebug( SCIP_CALL( SCIPprintRow(scip, row, NULL) ) );
/* get direction in which activity must be shifted */
assert(SCIPisFeasLT(scip, activities[rowpos], SCIProwGetLhs(row))
|| SCIPisFeasGT(scip, activities[rowpos], SCIProwGetRhs(row)));
direction = SCIPisFeasLT(scip, activities[rowpos], SCIProwGetLhs(row)) ? +1 : -1;
/* search a variable that can shift the activity in the necessary direction */
SCIP_CALL( selectShifting(scip, sol, row, activities[rowpos], direction,
nincreases, ndecreases, increaseweight, &shiftvar, &oldsolval, &newsolval) );
}
if( shiftvar == NULL && nfrac > 0 )
{
SCIPdebugMessage("shifting heuristic: search rounding variable and try to stay feasible\n");
SCIP_CALL( selectEssentialRounding(scip, sol, minobj, lpcands, nlpcands, &shiftvar, &oldsolval, &newsolval) );
}
/* check, whether shifting was possible */
if( shiftvar == NULL || SCIPisEQ(scip, oldsolval, newsolval) )
{
SCIPdebugMessage("shifting heuristic: -> didn't find a shifting variable\n");
break;
}
SCIPdebugMessage("shifting heuristic: -> shift var <%s>[%g,%g], type=%d, oldval=%g, newval=%g, obj=%g\n",
SCIPvarGetName(shiftvar), SCIPvarGetLbGlobal(shiftvar), SCIPvarGetUbGlobal(shiftvar), SCIPvarGetType(shiftvar),
oldsolval, newsolval, SCIPvarGetObj(shiftvar));
/* update row activities of globally valid rows */
SCIP_CALL( updateActivities(scip, activities, violrows, violrowpos, &nviolrows, nlprows,
shiftvar, oldsolval, newsolval) );
if( nviolrows >= nprevviolrows )
nnonimprovingshifts++;
else if( nviolrows < minnviolrows )
{
minnviolrows = nviolrows;
nnonimprovingshifts = 0;
}
/* store new solution value and decrease fractionality counter */
SCIP_CALL( SCIPsetSolVal(scip, sol, shiftvar, newsolval) );
/* update fractionality counter and minimal objective value possible after shifting remaining variables */
oldsolvalisfrac = !SCIPisFeasIntegral(scip, oldsolval)
&& (SCIPvarGetType(shiftvar) == SCIP_VARTYPE_BINARY || SCIPvarGetType(shiftvar) == SCIP_VARTYPE_INTEGER);
obj = SCIPvarGetObj(shiftvar);
if( (SCIPvarGetType(shiftvar) == SCIP_VARTYPE_BINARY || SCIPvarGetType(shiftvar) == SCIP_VARTYPE_INTEGER)
&& oldsolvalisfrac )
{
assert(SCIPisFeasIntegral(scip, newsolval));
nfrac--;
nnonimprovingshifts = 0;
minnviolrows = INT_MAX;
addFracCounter(nfracsinrow, nlprows, shiftvar, -1);
/* the rounding was already calculated into the minobj -> update only if rounding in "wrong" direction */
if( obj > 0.0 && newsolval > oldsolval )
minobj += obj;
else if( obj < 0.0 && newsolval < oldsolval )
minobj -= obj;
}
else
{
/* update minimal possible objective value */
minobj += obj * (newsolval - oldsolval);
}
/* update increase/decrease arrays */
if( !oldsolvalisfrac )
{
probindex = SCIPvarGetProbindex(shiftvar);
assert(0 <= probindex && probindex < nvars);
increaseweight *= WEIGHTFACTOR;
if( newsolval < oldsolval )
ndecreases[probindex] += increaseweight;
else
nincreases[probindex] += increaseweight;
if( increaseweight >= 1e+09 )
{
int i;
for( i = 0; i < nvars; ++i )
{
nincreases[i] /= increaseweight;
ndecreases[i] /= increaseweight;
}
increaseweight = 1.0;
}
}
SCIPdebugMessage("shifting heuristic: -> nfrac=%d, nviolrows=%d, obj=%g (best possible obj: %g)\n",
nfrac, nviolrows, SCIPgetSolOrigObj(scip, sol), SCIPretransformObj(scip, minobj));
}
/* check, if the new solution is feasible */
if( nfrac == 0 && nviolrows == 0 )
{
SCIP_Bool stored;
/* check solution for feasibility, and add it to solution store if possible
* neither integrality nor feasibility of LP rows has to be checked, because this is already
* done in the shifting heuristic itself; however, we better check feasibility of LP rows,
* because of numerical problems with activity updating
*/
SCIP_CALL( SCIPtrySol(scip, sol, FALSE, FALSE, FALSE, TRUE, &stored) );
if( stored )
{
SCIPdebugMessage("found feasible shifted solution:\n");
SCIPdebug( SCIP_CALL( SCIPprintSol(scip, sol, NULL, FALSE) ) );
*result = SCIP_FOUNDSOL;
}
}
/* free memory buffers */
SCIPfreeBufferArray(scip, &ndecreases);
SCIPfreeBufferArray(scip, &nincreases);
SCIPfreeBufferArray(scip, &nfracsinrow);
SCIPfreeBufferArray(scip, &violrowpos);
SCIPfreeBufferArray(scip, &violrows);
SCIPfreeBufferArray(scip, &activities);
return SCIP_OKAY;
}
/*
* heuristic specific interface methods
*/
/** creates the shifting heuristic with infeasibility recovering and includes it in SCIP */
SCIP_RETCODE SCIPincludeHeurShifting(
SCIP* scip /**< SCIP data structure */
)
{
SCIP_HEUR* heur;
/* 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, heurExecShifting, NULL) );
assert(heur != NULL);
/* set non-NULL pointers to callback methods */
SCIP_CALL( SCIPsetHeurCopy(scip, heur, heurCopyShifting) );
SCIP_CALL( SCIPsetHeurInit(scip, heur, heurInitShifting) );
SCIP_CALL( SCIPsetHeurExit(scip, heur, heurExitShifting) );
SCIP_CALL( SCIPsetHeurInitsol(scip, heur, heurInitsolShifting) );
return SCIP_OKAY;
}