File: CbcBranchLotsize.cpp

package info (click to toggle)
coinor-cbc 2.9.9+repack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • ctags: 5,787
  • sloc: cpp: 104,337; sh: 8,921; xml: 2,950; makefile: 520; ansic: 491; awk: 197
file content (818 lines) | stat: -rw-r--r-- 26,107 bytes parent folder | download | duplicates (3)
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
/* $Id: CbcBranchLotsize.cpp 2043 2014-07-01 13:03:51Z forrest $ */
// Copyright (C) 2002, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

#if defined(_MSC_VER)
// Turn off compiler warning about long names
#  pragma warning(disable:4786)
#endif
#include <cassert>
#include <cstdlib>
#include <cmath>
#include <cfloat>

#include "OsiSolverInterface.hpp"
#include "CbcModel.hpp"
#include "CbcMessage.hpp"
#include "CbcBranchLotsize.hpp"
#include "CoinSort.hpp"
#include "CoinError.hpp"

/*
  CBC_PRINT 1 just does sanity checks - no printing
  Larger values of CBC_PRINT set various printing levels.  Larger
  values print more.
*/
//#define CBC_PRINT 1
// First/last variable to print info on
#if CBC_PRINT
// preset does all - change to x,x to just do x
static int firstPrint = 0;
static int lastPrint = 1000000;
static CbcModel * saveModel = NULL;
#endif
// Just for debug (CBC_PRINT defined in CbcBranchLotsize.cpp)
void
#if CBC_PRINT
CbcLotsize::printLotsize(double value, bool condition, int type) const
#else
CbcLotsize::printLotsize(double , bool , int ) const
#endif
{
#if CBC_PRINT
    if (columnNumber_ >= firstPrint && columnNumber_ <= lastPrint) {
        int printIt = CBC_PRINT - 1;
        // Get details
        OsiSolverInterface * solver = saveModel->solver();
        double currentLower = solver->getColLower()[columnNumber_];
        double currentUpper = solver->getColUpper()[columnNumber_];
        int i;
        // See if in a valid range (with two tolerances)
        bool inRange = false;
        bool inRange2 = false;
        double integerTolerance =
            model_->getDblParam(CbcModel::CbcIntegerTolerance);
        // increase if type 2
        if (type == 2) {
            integerTolerance *= 100.0;
            type = 0;
            printIt = 2; // always print
        }
        // bounds should match some bound
        int rangeL = -1;
        int rangeU = -1;
        if (rangeType_ == 1) {
            for (i = 0; i < numberRanges_; i++) {
                if (fabs(currentLower - bound_[i]) < 1.0e-12)
                    rangeL = i;
                if (fabs(currentUpper - bound_[i]) < 1.0e-12)
                    rangeU = i;
                if (fabs(value - bound_[i]) < integerTolerance)
                    inRange = true;
                if (fabs(value - bound_[i]) < 1.0e8)
                    inRange2 = true;
            }
        } else {
            for (i = 0; i < numberRanges_; i++) {
                if (fabs(currentLower - bound_[2*i]) < 1.0e-12)
                    rangeL = i;
                if (fabs(currentUpper - bound_[2*i+1]) < 1.0e-12)
                    rangeU = i;
                if (value > bound_[2*i] - integerTolerance &&
                        value < bound_[2*i+1] + integerTolerance)
                    inRange = true;
                if (value > bound_[2*i] - integerTolerance &&
                        value < bound_[2*i+1] + integerTolerance)
                    inRange = true;
            }
        }
        assert (rangeL >= 0 && rangeU >= 0);
        bool abortIt = false;
        switch (type) {
            // returning from findRange (fall through to just check)
        case 0:
            if (printIt) {
                printf("findRange returns %s for column %d and value %g",
                       condition ? "true" : "false", columnNumber_, value);
                if (printIt > 1)
                    printf(" LP bounds %g, %g", currentLower, currentUpper);
                printf("\n");
            }
            // Should match
        case 1:
            if (inRange != condition) {
                printIt = 2;
                abortIt = true;
            }
            break;
            //
        case 2:
            break;
            //
        case 3:
            break;
            //
        case 4:
            break;
        }
    }
#endif
}
/** Default Constructor

*/
CbcLotsize::CbcLotsize ()
        : CbcObject(),
        columnNumber_(-1),
        rangeType_(0),
        numberRanges_(0),
        largestGap_(0),
        bound_(NULL),
        range_(0)
{
}

/** Useful constructor

  Loads actual upper & lower bounds for the specified variable.
*/
CbcLotsize::CbcLotsize (CbcModel * model,
                        int iColumn, int numberPoints,
                        const double * points, bool range)
        : CbcObject(model)
{
#if CBC_PRINT
    if (!saveModel)
        saveModel = model;
#endif
    assert (numberPoints > 0);
    columnNumber_ = iColumn ;
    // and set id so can be used for branching
    id_ = iColumn;
    // sort ranges
    int * sort = new int[numberPoints];
    double * weight = new double [numberPoints];
    int i;
    if (range) {
        rangeType_ = 2;
    } else {
        rangeType_ = 1;
    }
    for (i = 0; i < numberPoints; i++) {
        sort[i] = i;
        weight[i] = points[i*rangeType_];
    }
    CoinSort_2(weight, weight + numberPoints, sort);
    numberRanges_ = 1;
    largestGap_ = 0;
    if (rangeType_ == 1) {
        bound_ = new double[numberPoints+1];
        bound_[0] = weight[0];
        for (i = 1; i < numberPoints; i++) {
            if (weight[i] != weight[i-1])
                bound_[numberRanges_++] = weight[i];
        }
        // and for safety
        bound_[numberRanges_] = bound_[numberRanges_-1];
        for (i = 1; i < numberRanges_; i++) {
            largestGap_ = CoinMax(largestGap_, bound_[i] - bound_[i-1]);
        }
    } else {
        bound_ = new double[2*numberPoints+2];
        bound_[0] = points[sort[0] * 2];
        bound_[1] = points[sort[0] * 2 + 1];
        double hi = bound_[1];
        assert (hi >= bound_[0]);
        for (i = 1; i < numberPoints; i++) {
            double thisLo = points[sort[i] * 2];
            double thisHi = points[sort[i] * 2 + 1];
            assert (thisHi >= thisLo);
            if (thisLo > hi) {
                bound_[2*numberRanges_] = thisLo;
                bound_[2*numberRanges_+1] = thisHi;
                numberRanges_++;
                hi = thisHi;
            } else {
                //overlap
                hi = CoinMax(hi, thisHi);
                bound_[2*numberRanges_-1] = hi;
            }
        }
        // and for safety
        bound_[2*numberRanges_] = bound_[2*numberRanges_-2];
        bound_[2*numberRanges_+1] = bound_[2*numberRanges_-1];
        for (i = 1; i < numberRanges_; i++) {
            largestGap_ = CoinMax(largestGap_, bound_[2*i] - bound_[2*i-1]);
        }
    }
    delete [] sort;
    delete [] weight;
    range_ = 0;
}

// Copy constructor
CbcLotsize::CbcLotsize ( const CbcLotsize & rhs)
        : CbcObject(rhs)

{
    columnNumber_ = rhs.columnNumber_;
    rangeType_ = rhs.rangeType_;
    numberRanges_ = rhs.numberRanges_;
    range_ = rhs.range_;
    largestGap_ = rhs.largestGap_;
    if (numberRanges_) {
        assert (rangeType_ > 0 && rangeType_ < 3);
        bound_ = new double [(numberRanges_+1)*rangeType_];
        memcpy(bound_, rhs.bound_, (numberRanges_ + 1)*rangeType_*sizeof(double));
    } else {
        bound_ = NULL;
    }
}

// Clone
CbcObject *
CbcLotsize::clone() const
{
    return new CbcLotsize(*this);
}

// Assignment operator
CbcLotsize &
CbcLotsize::operator=( const CbcLotsize & rhs)
{
    if (this != &rhs) {
        CbcObject::operator=(rhs);
        columnNumber_ = rhs.columnNumber_;
        rangeType_ = rhs.rangeType_;
        numberRanges_ = rhs.numberRanges_;
        largestGap_ = rhs.largestGap_;
        delete [] bound_;
        range_ = rhs.range_;
        if (numberRanges_) {
            assert (rangeType_ > 0 && rangeType_ < 3);
            bound_ = new double [(numberRanges_+1)*rangeType_];
            memcpy(bound_, rhs.bound_, (numberRanges_ + 1)*rangeType_*sizeof(double));
        } else {
            bound_ = NULL;
        }
    }
    return *this;
}

// Destructor
CbcLotsize::~CbcLotsize ()
{
    delete [] bound_;
}
/* Finds range of interest so value is feasible in range range_ or infeasible
   between hi[range_] and lo[range_+1].  Returns true if feasible.
*/
bool
CbcLotsize::findRange(double value) const
{
    assert (range_ >= 0 && range_ < numberRanges_ + 1);
    double integerTolerance =
        model_->getDblParam(CbcModel::CbcIntegerTolerance);
    int iLo;
    int iHi;
    double infeasibility = 0.0;
    if (rangeType_ == 1) {
        if (value < bound_[range_] - integerTolerance) {
            iLo = 0;
            iHi = range_ - 1;
        } else if (value < bound_[range_] + integerTolerance) {
#if CBC_PRINT
            printLotsize(value, true, 0);
#endif
            return true;
        } else if (value < bound_[range_+1] - integerTolerance) {
#ifdef CBC_PRINT
            printLotsize(value, false, 0);
#endif
            return false;
        } else {
            iLo = range_ + 1;
            iHi = numberRanges_ - 1;
        }
        // check lo and hi
        bool found = false;
        if (value > bound_[iLo] - integerTolerance && value < bound_[iLo+1] + integerTolerance) {
            range_ = iLo;
            found = true;
        } else if (value > bound_[iHi] - integerTolerance && value < bound_[iHi+1] + integerTolerance) {
            range_ = iHi;
            found = true;
        } else {
            range_ = (iLo + iHi) >> 1;
        }
        //points
        while (!found) {
            if (value < bound_[range_]) {
                if (value >= bound_[range_-1]) {
                    // found
                    range_--;
                    break;
                } else {
                    iHi = range_;
                }
            } else {
                if (value < bound_[range_+1]) {
                    // found
                    break;
                } else {
                    iLo = range_;
                }
            }
            range_ = (iLo + iHi) >> 1;
        }
        if (value - bound_[range_] <= bound_[range_+1] - value) {
            infeasibility = value - bound_[range_];
        } else {
            infeasibility = bound_[range_+1] - value;
            if (infeasibility < integerTolerance)
                range_++;
        }
#ifdef CBC_PRINT
        printLotsize(value, (infeasibility < integerTolerance), 0);
#endif
        return (infeasibility < integerTolerance);
    } else {
        // ranges
        if (value < bound_[2*range_] - integerTolerance) {
            iLo = 0;
            iHi = range_ - 1;
        } else if (value < bound_[2*range_+1] + integerTolerance) {
#ifdef CBC_PRINT
            printLotsize(value, true, 0);
#endif
            return true;
        } else if (value < bound_[2*range_+2] - integerTolerance) {
#ifdef CBC_PRINT
            printLotsize(value, false, 0);
#endif
            return false;
        } else {
            iLo = range_ + 1;
            iHi = numberRanges_ - 1;
        }
        // check lo and hi
        bool found = false;
        if (value > bound_[2*iLo] - integerTolerance && value < bound_[2*iLo+2] - integerTolerance) {
            range_ = iLo;
            found = true;
        } else if (value >= bound_[2*iHi] - integerTolerance) {
            range_ = iHi;
            found = true;
        } else {
            range_ = (iLo + iHi) >> 1;
        }
        //points
        while (!found) {
            if (value < bound_[2*range_]) {
                if (value >= bound_[2*range_-2]) {
                    // found
                    range_--;
                    break;
                } else {
                    iHi = range_;
                }
            } else {
                if (value < bound_[2*range_+2]) {
                    // found
                    break;
                } else {
                    iLo = range_;
                }
            }
            range_ = (iLo + iHi) >> 1;
        }
        if (value >= bound_[2*range_] - integerTolerance && value <= bound_[2*range_+1] + integerTolerance)
            infeasibility = 0.0;
        else if (value - bound_[2*range_+1] < bound_[2*range_+2] - value) {
            infeasibility = value - bound_[2*range_+1];
        } else {
            infeasibility = bound_[2*range_+2] - value;
        }
#ifdef CBC_PRINT
        printLotsize(value, (infeasibility < integerTolerance), 0);
#endif
        return (infeasibility < integerTolerance);
    }
}
/* Returns floor and ceiling
 */
void
CbcLotsize::floorCeiling(double & floorLotsize, double & ceilingLotsize, double value,
                         double /*tolerance*/) const
{
    bool feasible = findRange(value);
    if (rangeType_ == 1) {
        floorLotsize = bound_[range_];
        ceilingLotsize = bound_[range_+1];
        // may be able to adjust
        if (feasible && fabs(value - floorLotsize) > fabs(value - ceilingLotsize)) {
            floorLotsize = bound_[range_+1];
            ceilingLotsize = bound_[range_+2];
        }
    } else {
        // ranges
        assert (value >= bound_[2*range_+1]);
        floorLotsize = bound_[2*range_+1];
        ceilingLotsize = bound_[2*range_+2];
    }
}
double
CbcLotsize::infeasibility(const OsiBranchingInformation * /*info*/,
                          int &preferredWay) const
{
    OsiSolverInterface * solver = model_->solver();
    const double * solution = model_->testSolution();
    const double * lower = solver->getColLower();
    const double * upper = solver->getColUpper();
    double value = solution[columnNumber_];
    value = CoinMax(value, lower[columnNumber_]);
    value = CoinMin(value, upper[columnNumber_]);
    double integerTolerance =
        model_->getDblParam(CbcModel::CbcIntegerTolerance);
    /*printf("%d %g %g %g %g\n",columnNumber_,value,lower[columnNumber_],
      solution[columnNumber_],upper[columnNumber_]);*/
    assert (value >= bound_[0] - integerTolerance
            && value <= bound_[rangeType_*numberRanges_-1] + integerTolerance);
    double infeasibility = 0.0;
    bool feasible = findRange(value);
    if (!feasible) {
        if (rangeType_ == 1) {
            if (value - bound_[range_] < bound_[range_+1] - value) {
                preferredWay = -1;
                infeasibility = value - bound_[range_];
            } else {
                preferredWay = 1;
                infeasibility = bound_[range_+1] - value;
            }
        } else {
            // ranges
            if (value - bound_[2*range_+1] < bound_[2*range_+2] - value) {
                preferredWay = -1;
                infeasibility = value - bound_[2*range_+1];
            } else {
                preferredWay = 1;
                infeasibility = bound_[2*range_+2] - value;
            }
        }
    } else {
        // always satisfied
        preferredWay = -1;
    }
    if (infeasibility < integerTolerance)
        infeasibility = 0.0;
    else
        infeasibility /= largestGap_;
#ifdef CBC_PRINT
    printLotsize(value, infeasibility, 1);
#endif
    return infeasibility;
}
/* Column number if single column object -1 otherwise,
   so returns >= 0
   Used by heuristics
*/
int
CbcLotsize::columnNumber() const
{
    return columnNumber_;
}
// This looks at solution and sets bounds to contain solution
/** More precisely: it first forces the variable within the existing
    bounds, and then tightens the bounds to make sure the variable is feasible
*/
void
CbcLotsize::feasibleRegion()
{
    OsiSolverInterface * solver = model_->solver();
    const double * lower = solver->getColLower();
    const double * upper = solver->getColUpper();
    const double * solution = model_->testSolution();
    double value = solution[columnNumber_];
    value = CoinMax(value, lower[columnNumber_]);
    value = CoinMin(value, upper[columnNumber_]);
    findRange(value);
    double nearest;
    if (rangeType_ == 1) {
        nearest = bound_[range_];
        solver->setColLower(columnNumber_, nearest);
        solver->setColUpper(columnNumber_, nearest);
    } else {
        // ranges
        solver->setColLower(columnNumber_, CoinMax(bound_[2*range_],
						   lower[columnNumber_]));
	solver->setColUpper(columnNumber_, CoinMin(bound_[2*range_+1],
						   upper[columnNumber_]));
        if (value > bound_[2*range_+1])
            nearest = bound_[2*range_+1];
        else if (value < bound_[2*range_])
            nearest = bound_[2*range_];
        else
            nearest = value;
    }
#ifdef CBC_PRINT
    // print details
    printLotsize(value, true, 2);
#endif
    // Scaling may have moved it a bit
    // Lotsizing variables could be a lot larger
#ifndef NDEBUG
    double integerTolerance =
        model_->getDblParam(CbcModel::CbcIntegerTolerance);
    assert (fabs(value - nearest) <= (100.0 + 10.0*fabs(nearest))*integerTolerance);
#endif
}
CbcBranchingObject *
CbcLotsize::createCbcBranch(OsiSolverInterface * solver, const OsiBranchingInformation * /*info*/, int way)
{
    //OsiSolverInterface * solver = model_->solver();
    const double * solution = model_->testSolution();
    const double * lower = solver->getColLower();
    const double * upper = solver->getColUpper();
    double value = solution[columnNumber_];
    value = CoinMax(value, lower[columnNumber_]);
    value = CoinMin(value, upper[columnNumber_]);
    assert (!findRange(value));
    return new CbcLotsizeBranchingObject(model_, columnNumber_, way,
                                         value, this);
}


/* Given valid solution (i.e. satisfied) and reduced costs etc
   returns a branching object which would give a new feasible
   point in direction reduced cost says would be cheaper.
   If no feasible point returns null
*/
CbcBranchingObject *
CbcLotsize::preferredNewFeasible() const
{
    OsiSolverInterface * solver = model_->solver();

    assert (findRange(model_->testSolution()[columnNumber_]));
    double dj = solver->getObjSense() * solver->getReducedCost()[columnNumber_];
    CbcLotsizeBranchingObject * object = NULL;
    double lo, up;
    if (dj >= 0.0) {
        // can we go down
        if (range_) {
            // yes
            if (rangeType_ == 1) {
                lo = bound_[range_-1];
                up = bound_[range_-1];
            } else {
                lo = bound_[2*range_-2];
                up = bound_[2*range_-1];
            }
            object = new CbcLotsizeBranchingObject(model_, columnNumber_, -1,
                                                   lo, up);
        }
    } else {
        // can we go up
        if (range_ < numberRanges_ - 1) {
            // yes
            if (rangeType_ == 1) {
                lo = bound_[range_+1];
                up = bound_[range_+1];
            } else {
                lo = bound_[2*range_+2];
                up = bound_[2*range_+3];
            }
            object = new CbcLotsizeBranchingObject(model_, columnNumber_, -1,
                                                   lo, up);
        }
    }
    return object;
}

/* Given valid solution (i.e. satisfied) and reduced costs etc
   returns a branching object which would give a new feasible
   point in direction opposite to one reduced cost says would be cheaper.
   If no feasible point returns null
*/
CbcBranchingObject *
CbcLotsize::notPreferredNewFeasible() const
{
    OsiSolverInterface * solver = model_->solver();

#ifndef NDEBUG
    double value = model_->testSolution()[columnNumber_];
    double nearest = floor(value + 0.5);
    double integerTolerance =
        model_->getDblParam(CbcModel::CbcIntegerTolerance);
    // Scaling may have moved it a bit
    // Lotsizing variables could be a lot larger
    assert (fabs(value - nearest) <= (10.0 + 10.0*fabs(nearest))*integerTolerance);
#endif
    double dj = solver->getObjSense() * solver->getReducedCost()[columnNumber_];
    CbcLotsizeBranchingObject * object = NULL;
    double lo, up;
    if (dj <= 0.0) {
        // can we go down
        if (range_) {
            // yes
            if (rangeType_ == 1) {
                lo = bound_[range_-1];
                up = bound_[range_-1];
            } else {
                lo = bound_[2*range_-2];
                up = bound_[2*range_-1];
            }
            object = new CbcLotsizeBranchingObject(model_, columnNumber_, -1,
                                                   lo, up);
        }
    } else {
        // can we go up
        if (range_ < numberRanges_ - 1) {
            // yes
            if (rangeType_ == 1) {
                lo = bound_[range_+1];
                up = bound_[range_+1];
            } else {
                lo = bound_[2*range_+2];
                up = bound_[2*range_+3];
            }
            object = new CbcLotsizeBranchingObject(model_, columnNumber_, -1,
                                                   lo, up);
        }
    }
    return object;
}

/*
  Bounds may be tightened, so it may be good to be able to refresh the local
  copy of the original bounds.
 */
void
CbcLotsize::resetBounds(const OsiSolverInterface * /*solver*/)
{
}

// Default Constructor
CbcLotsizeBranchingObject::CbcLotsizeBranchingObject()
        : CbcBranchingObject()
{
    down_[0] = 0.0;
    down_[1] = 0.0;
    up_[0] = 0.0;
    up_[1] = 0.0;
}

// Useful constructor
CbcLotsizeBranchingObject::CbcLotsizeBranchingObject (CbcModel * model,
        int variable, int way , double value,
        const CbcLotsize * lotsize)
        : CbcBranchingObject(model, variable, way, value)
{
    int iColumn = lotsize->modelSequence();
    assert (variable == iColumn);
    down_[0] = model_->solver()->getColLower()[iColumn];
    double integerTolerance =
        model_->getDblParam(CbcModel::CbcIntegerTolerance);
    lotsize->floorCeiling(down_[1], up_[0], value, integerTolerance);
    up_[1] = model->getColUpper()[iColumn];
}
// Useful constructor for fixing
CbcLotsizeBranchingObject::CbcLotsizeBranchingObject (CbcModel * model,
        int variable, int way,
        double lowerValue,
        double upperValue)
        : CbcBranchingObject(model, variable, way, lowerValue)
{
    setNumberBranchesLeft(1);
    down_[0] = lowerValue;
    down_[1] = upperValue;
    up_[0] = lowerValue;
    up_[1] = upperValue;
}


// Copy constructor
CbcLotsizeBranchingObject::CbcLotsizeBranchingObject ( const CbcLotsizeBranchingObject & rhs) : CbcBranchingObject(rhs)
{
    down_[0] = rhs.down_[0];
    down_[1] = rhs.down_[1];
    up_[0] = rhs.up_[0];
    up_[1] = rhs.up_[1];
}

// Assignment operator
CbcLotsizeBranchingObject &
CbcLotsizeBranchingObject::operator=( const CbcLotsizeBranchingObject & rhs)
{
    if (this != &rhs) {
        CbcBranchingObject::operator=(rhs);
        down_[0] = rhs.down_[0];
        down_[1] = rhs.down_[1];
        up_[0] = rhs.up_[0];
        up_[1] = rhs.up_[1];
    }
    return *this;
}
CbcBranchingObject *
CbcLotsizeBranchingObject::clone() const
{
    return (new CbcLotsizeBranchingObject(*this));
}


// Destructor
CbcLotsizeBranchingObject::~CbcLotsizeBranchingObject ()
{
}

/*
  Perform a branch by adjusting the bounds of the specified variable. Note
  that each arm of the branch advances the object to the next arm by
  advancing the value of way_.

  Providing new values for the variable's lower and upper bounds for each
  branching direction gives a little bit of additional flexibility and will
  be easily extensible to multi-way branching.
*/
double
CbcLotsizeBranchingObject::branch()
{
    decrementNumberBranchesLeft();
    int iColumn = variable_;
    if (way_ < 0) {
#ifndef NDEBUG
        { double olb, oub ;
            olb = model_->solver()->getColLower()[iColumn] ;
            oub = model_->solver()->getColUpper()[iColumn] ;
#ifdef CBC_DEBUG
            printf("branching down on var %d: [%g,%g] => [%g,%g]\n",
                   iColumn, olb, oub, down_[0], down_[1]) ;
#endif
	    assert (olb<down_[0]+1.0e-7&&oub>down_[1]-1.0e-7);
        }
#endif
        model_->solver()->setColLower(iColumn, down_[0]);
        model_->solver()->setColUpper(iColumn, down_[1]);
        way_ = 1;
    } else {
#ifndef NDEBUG
        { double olb, oub ;
            olb = model_->solver()->getColLower()[iColumn] ;
            oub = model_->solver()->getColUpper()[iColumn] ;
#ifdef CBC_DEBUG
            printf("branching up on var %d: [%g,%g] => [%g,%g]\n",
                   iColumn, olb, oub, up_[0], up_[1]) ;
#endif
	    assert (olb<up_[0]+1.0e-7&&oub>up_[1]-1.0e-7);
        }
#endif
        model_->solver()->setColLower(iColumn, up_[0]);
        model_->solver()->setColUpper(iColumn, up_[1]);
        way_ = -1;	  // Swap direction
    }
    return 0.0;
}
// Print
void
CbcLotsizeBranchingObject::print()
{
    int iColumn = variable_;
    if (way_ < 0) {
        {
            double olb, oub ;
            olb = model_->solver()->getColLower()[iColumn] ;
            oub = model_->solver()->getColUpper()[iColumn] ;
            printf("branching down on var %d: [%g,%g] => [%g,%g]\n",
                   iColumn, olb, oub, down_[0], down_[1]) ;
        }
    } else {
        {
            double olb, oub ;
            olb = model_->solver()->getColLower()[iColumn] ;
            oub = model_->solver()->getColUpper()[iColumn] ;
            printf("branching up on var %d: [%g,%g] => [%g,%g]\n",
                   iColumn, olb, oub, up_[0], up_[1]) ;
        }
    }
}

/** Compare the \c this with \c brObj. \c this and \c brObj must be os the
    same type and must have the same original object, but they may have
    different feasible regions.
    Return the appropriate CbcRangeCompare value (first argument being the
    sub/superset if that's the case). In case of overlap (and if \c
    replaceIfOverlap is true) replace the current branching object with one
    whose feasible region is the overlap.
*/
CbcRangeCompare
CbcLotsizeBranchingObject::compareBranchingObject
(const CbcBranchingObject* brObj, const bool replaceIfOverlap)
{
    const CbcLotsizeBranchingObject* br =
        dynamic_cast<const CbcLotsizeBranchingObject*>(brObj);
    assert(br);
    double* thisBd = way_ == -1 ? down_ : up_;
    const double* otherBd = br->way_ == -1 ? br->down_ : br->up_;
    return CbcCompareRanges(thisBd, otherBd, replaceIfOverlap);
}