File: CbcBranchDynamic.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 (793 lines) | stat: -rw-r--r-- 29,163 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
/* $Id: CbcBranchDynamic.cpp 2073 2014-09-14 07:53:50Z 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>
//#define CBC_DEBUG
//#define TRACE_ONE 19
#include "OsiSolverInterface.hpp"
#include "OsiSolverBranch.hpp"
#include "CbcModel.hpp"
#include "CbcMessage.hpp"
#include "CbcBranchDynamic.hpp"
#include "CoinSort.hpp"
#include "CoinError.hpp"

// Removing magic constants.

// This is a very small number, added to something to make sure it's non-zero.
// Useful, for example in denominators of ratios to avoid any possible division by zero
# define nonZeroAmount 1.0e-30

// Increasing the size of an array when it grows to the end of its alloted space.
// In this file, only used for the history of the outcome of a branch.
// New size is   size_scale_numerator* <old value> / size_scale_denominator + additive_size_increase.

#define size_scale_numerator 3
#define size_scale_denominator 2
#define additive_size_increase 100

// Explanation of options used in this file

// TYPE2 defines a strategy for computing pseudocosts
// 0 means to just use the absolute change in objective
// 1 means use the relative change in objective
// 2 means use a convex combination of absolute and relative objective changes

// For option 2 (TYPE2 == 2), the specific combination is controlled by TYPERATIO
// Includes a TYPERATIO fraction of the absolute change and (1 - TYPERATIO) fraction of
// the relative change.  So should in general have 0 <= TYPERATIO <= 1.  But for the
// equality cases, you're better off using the other strategy (TYPE2) options.

#ifdef COIN_DEVELOP
typedef struct {
    double sumUp_;
    double upEst_; // or change in obj in update
    double sumDown_;
    double downEst_; // or movement in value in update
    int sequence_;
    int numberUp_;
    int numberUpInf_;
    int numberDown_;
    int numberDownInf_;
    char where_;
    char status_;
} History;
static History * history = NULL;
static int numberHistory = 0;
static int maxHistory = 0;
static bool getHistoryStatistics_ = true;
static void increaseHistory()
{
    if (numberHistory == maxHistory) {
      // This was originally 3 * maxHistory/2 + 100
        maxHistory = additive_size_increase + (size_scale_numerator * maxHistory) / size_scale_denominator;
        History * temp = new History [maxHistory];
        memcpy(temp, history, numberHistory*sizeof(History));
        delete [] history;
        history = temp;
    }
}
static bool addRecord(History newOne)
{
    //if (!getHistoryStatistics_)
    return false;
    bool fromCompare = false;
    int i;
    for ( i = numberHistory - 1; i >= 0; i--) {
        if (newOne.sequence_ != history[i].sequence_)
            continue;
        if (newOne.where_ != history[i].where_)
            continue;
        if (newOne.numberUp_ != history[i].numberUp_)
            continue;
        if (newOne.sumUp_ != history[i].sumUp_)
            continue;
        if (newOne.numberUpInf_ != history[i].numberUpInf_)
            continue;
        if (newOne.upEst_ != history[i].upEst_)
            continue;
        if (newOne.numberDown_ != history[i].numberDown_)
            continue;
        if (newOne.sumDown_ != history[i].sumDown_)
            continue;
        if (newOne.numberDownInf_ != history[i].numberDownInf_)
            continue;
        if (newOne.downEst_ != history[i].downEst_)
            continue;
        // If B knock out previous B
        if (newOne.where_ == 'C') {
            fromCompare = true;
            if (newOne.status_ == 'B') {
                int j;
                for (j = i - 1; j >= 0; j--) {
                    if (history[j].where_ == 'C') {
                        if (history[j].status_ == 'I') {
                            break;
                        } else if (history[j].status_ == 'B') {
                            history[j].status_ = ' ';
                            break;
                        }
                    }
                }
            }
            break;
        }
    }
    if (i == -1 || fromCompare) {
        //add
        increaseHistory();
        history[numberHistory++] = newOne;
        return true;
    } else {
        return false;
    }
}
#endif


// Default Constructor
CbcBranchDynamicDecision::CbcBranchDynamicDecision()
        : CbcBranchDecision()
{
    bestCriterion_ = 0.0;
    bestChangeUp_ = 0.0;
    bestNumberUp_ = 0;
    bestChangeDown_ = 0.0;
    bestNumberDown_ = 0;
    bestObject_ = NULL;
}

// Copy constructor
CbcBranchDynamicDecision::CbcBranchDynamicDecision (
    const CbcBranchDynamicDecision & rhs)
        : CbcBranchDecision()
{
    bestCriterion_ = rhs.bestCriterion_;
    bestChangeUp_ = rhs.bestChangeUp_;
    bestNumberUp_ = rhs.bestNumberUp_;
    bestChangeDown_ = rhs.bestChangeDown_;
    bestNumberDown_ = rhs.bestNumberDown_;
    bestObject_ = rhs.bestObject_;
}

CbcBranchDynamicDecision::~CbcBranchDynamicDecision()
{
}

// Clone
CbcBranchDecision *
CbcBranchDynamicDecision::clone() const
{
    return new CbcBranchDynamicDecision(*this);
}

// Initialize i.e. before start of choosing at a node
void
CbcBranchDynamicDecision::initialize(CbcModel * /*model*/)
{
    bestCriterion_ = 0.0;
    bestChangeUp_ = 0.0;
    bestNumberUp_ = 0;
    bestChangeDown_ = 0.0;
    bestNumberDown_ = 0;
    bestObject_ = NULL;
#ifdef COIN_DEVELOP
    History hist;
    hist.where_ = 'C';
    hist.status_ = 'I';
    hist.sequence_ = 55555;
    hist.numberUp_ = 0;
    hist.numberUpInf_ = 0;
    hist.sumUp_ = 0.0;
    hist.upEst_ = 0.0;
    hist.numberDown_ = 0;
    hist.numberDownInf_ = 0;
    hist.sumDown_ = 0.0;
    hist.downEst_ = 0.0;
    addRecord(hist);
#endif
}

/* Saves a clone of current branching object.  Can be used to update
      information on object causing branch - after branch */
void
CbcBranchDynamicDecision::saveBranchingObject(OsiBranchingObject * object)
{
    OsiBranchingObject * obj = object->clone();
#ifndef NDEBUG
    CbcBranchingObject * obj2 =
        dynamic_cast<CbcBranchingObject *>(obj);
    assert (obj2);
#if COIN_DEVELOP>1
    CbcDynamicPseudoCostBranchingObject * branchingObject =
        dynamic_cast<CbcDynamicPseudoCostBranchingObject *>(obj);
    if (!branchingObject)
        printf("no dynamic branching object Dynamic Decision\n");
#endif
#else
    CbcBranchingObject * obj2 =
        static_cast<CbcBranchingObject *>(obj);
#endif
    //object_=branchingObject;
    object_ = obj2;
}
/* Pass in information on branch just done.
   assumes object can get information from solver */
/*
  The expectation is that this method will be called after the branch has been
  imposed on the constraint system and resolve() has executed.

  Note that the CbcBranchDecision is a property of the CbcModel. Note also that
  this method is reaching right through the CbcBranchingObject to update
  information in the underlying CbcObject. That's why we delete the
  branchingObject at the end of the method --- the next time we're called,
  the CbcObject will be different.
*/
void
CbcBranchDynamicDecision::updateInformation(OsiSolverInterface * solver,
        const CbcNode * node)
{
    assert (object_);
    const CbcModel * model = object_->model();
    double originalValue = node->objectiveValue();
    int originalUnsatisfied = node->numberUnsatisfied();
    double objectiveValue = solver->getObjValue() * model->getObjSense();
    int unsatisfied = 0;
    int i;
    int numberIntegers = model->numberIntegers();;
    const double * solution = solver->getColSolution();
    //const double * lower = solver->getColLower();
    //const double * upper = solver->getColUpper();
	/*
	 Gain access to the associated CbcBranchingObject and its underlying
	 CbcObject.

	 Seems like we'd want to distinguish between no branching object and a
	 branching object of the wrong type. Just deleting an object of the wrong
	 type hides many sins.

	 Hmmm ... if we're using the OSI side of the hierarchy, is this indicated by a
	 null object_? Nah, then we have an assert failure off the top.
	*/

    CbcDynamicPseudoCostBranchingObject * branchingObject =
        dynamic_cast<CbcDynamicPseudoCostBranchingObject *>(object_);
    if (!branchingObject) {
        delete object_;
        object_ = NULL;
        return;
    }
    CbcSimpleIntegerDynamicPseudoCost *  object = branchingObject->object();
    /*
	change is the change in objective due to the branch we've just imposed. It's
	possible we may have gone infeasible.
	*/
	double change = CoinMax(0.0, objectiveValue - originalValue);
    // probably should also ignore if stopped
	// FIXME. Could use enum to avoid numbers for iStatus (e.g. optimal, unknown, infeasible)
    int iStatus;
    if (solver->isProvenOptimal())
        iStatus = 0; // optimal
    else if (solver->isIterationLimitReached()
             && !solver->isDualObjectiveLimitReached())
        iStatus = 2; // unknown
    else
        iStatus = 1; // infeasible
	/*
	  If we're feasible according to the solver, evaluate integer feasibility.
	*/
    bool feasible = iStatus != 1;
    if (feasible) {
        double integerTolerance =
            model->getDblParam(CbcModel::CbcIntegerTolerance);
        const int * integerVariable = model->integerVariable();
        for (i = 0; i < numberIntegers; i++) {
            int j = integerVariable[i];
            double value = solution[j];
            double nearest = floor(value + 0.5);
            if (fabs(value - nearest) > integerTolerance)
                unsatisfied++;
        }
    }
	/*
	  Finally, update the object. Defaults (080104) are TYPE2 = 0, INFEAS = 1.

	  Pseudocosts are at heart the average of actual costs for a branch. We just
	  need to update the information used to calculate that average.
	*/
    int way = object_->way();
    double value = object_->value();
    //#define TYPE2 1
    //#define TYPERATIO 0.9
    if (way < 0) {
        // down
        if (feasible) {
            double movement = value - floor(value);
            movement = CoinMax(movement, MINIMUM_MOVEMENT);
            //printf("(down change %g value down %g ",change,movement);
            object->incrementNumberTimesDown();
            object->addToSumDownChange(nonZeroAmount + movement);
            object->addToSumDownDecrease(originalUnsatisfied - unsatisfied);
#if TYPE2==0
            object->addToSumDownCost(change / (nonZeroAmount + movement));
            object->setDownDynamicPseudoCost(object->sumDownCost() /
                                             static_cast<double> (object->numberTimesDown()));
#elif TYPE2==1
            object->addToSumDownCost(change);
            object->setDownDynamicPseudoCost(object->sumDownCost() / object->sumDownChange());
#elif TYPE2==2
            object->addToSumDownCost(change*TYPERATIO + (1.0 - TYPERATIO)*change / (nonZeroAmount + movement));
            object->setDownDynamicPseudoCost(object->sumDownCost()*(TYPERATIO / object->sumDownChange() + (1.0 - TYPERATIO) / (double) object->numberTimesDown()));
#endif
        } else {
            //printf("(down infeasible value down %g ",change,movement);
            object->incrementNumberTimesDown();
            object->incrementNumberTimesDownInfeasible();
#if INFEAS==2
            double distanceToCutoff = 0.0;
            double objectiveValue = model->getCurrentMinimizationObjValue();
            distanceToCutoff =  model->getCutoff()  - originalValue;
            if (distanceToCutoff < 1.0e20)
                change = distanceToCutoff * 2.0;
            else
                change = object->downDynamicPseudoCost() * movement * 10.0;
            change = CoinMax(1.0e-12 * (1.0 + fabs(originalValue)), change);
            object->addToSumDownChange(nonZeroAmount + movement);
            object->addToSumDownDecrease(originalUnsatisfied - unsatisfied);
#if TYPE2==0
            object->addToSumDownCost(change / (nonZeroAmount + movement));
            object->setDownDynamicPseudoCost(object->sumDownCost() / (double) object->numberTimesDown());
#elif TYPE2==1
            object->addToSumDownCost(change);
            object->setDownDynamicPseudoCost(object->sumDownCost() / object->sumDownChange());
#elif TYPE2==2
            object->addToSumDownCost(change*TYPERATIO + (1.0 - TYPERATIO)*change / (nonZeroAmount + movement));
            object->setDownDynamicPseudoCost(object->sumDownCost()*(TYPERATIO / object->sumDownChange() + (1.0 - TYPERATIO) / (double) object->numberTimesDown()));
#endif
#endif
        }
    } else {
        // up
        if (feasible) {
            double movement = ceil(value) - value;
            movement = CoinMax(movement, MINIMUM_MOVEMENT);
            //printf("(up change %g value down %g ",change,movement);
            object->incrementNumberTimesUp();
            object->addToSumUpChange(nonZeroAmount + movement);
            object->addToSumUpDecrease(unsatisfied - originalUnsatisfied);
#if TYPE2==0
            object->addToSumUpCost(change / (nonZeroAmount + movement));
            object->setUpDynamicPseudoCost(object->sumUpCost() /
                                           static_cast<double> (object->numberTimesUp()));
#elif TYPE2==1
            object->addToSumUpCost(change);
            object->setUpDynamicPseudoCost(object->sumUpCost() / object->sumUpChange());
#elif TYPE2==2
            object->addToSumUpCost(change*TYPERATIO + (1.0 - TYPERATIO)*change / (nonZeroAmount + movement));
            object->setUpDynamicPseudoCost(object->sumUpCost()*(TYPERATIO / object->sumUpChange() + (1.0 - TYPERATIO) / (double) object->numberTimesUp()));
#endif
        } else {
            //printf("(up infeasible value down %g ",change,movement);
            object->incrementNumberTimesUp();
            object->incrementNumberTimesUpInfeasible();
#if INFEAS==2
            double distanceToCutoff = 0.0;
            double objectiveValue = model->getCurrentMinimizationObjValue();
            distanceToCutoff =  model->getCutoff()  - originalValue;
            if (distanceToCutoff < 1.0e20)
                change = distanceToCutoff * 2.0;
            else
                change = object->upDynamicPseudoCost() * movement * 10.0;
            change = CoinMax(1.0e-12 * (1.0 + fabs(originalValue)), change);
            object->addToSumUpChange(nonZeroAmount + movement);
            object->addToSumUpDecrease(unsatisfied - originalUnsatisfied);
#if TYPE2==0
            object->addToSumUpCost(change / (nonZeroAmount + movement));
            object->setUpDynamicPseudoCost(object->sumUpCost() / (double) object->numberTimesUp());
#elif TYPE2==1
            object->addToSumUpCost(change);
            object->setUpDynamicPseudoCost(object->sumUpCost() / object->sumUpChange());
#elif TYPE2==2
            object->addToSumUpCost(change*TYPERATIO + (1.0 - TYPERATIO)*change / (nonZeroAmount + movement));
            object->setUpDynamicPseudoCost(object->sumUpCost()*(TYPERATIO / object->sumUpChange() + (1.0 - TYPERATIO) / (double) object->numberTimesUp()));
#endif
#endif
        }
    }
    //object->print(1,0.5);
    delete object_;
    object_ = NULL;
}

/*
  Simple dynamic decision algorithm. Compare based on infeasibility (numInfUp,
  numInfDown) until a solution is found by search, then switch to change in
  objective (changeUp, changeDown). Note that bestSoFar is remembered in
  bestObject_, so the parameter bestSoFar is unused.
*/

int
CbcBranchDynamicDecision::betterBranch(CbcBranchingObject * thisOne,
                                       CbcBranchingObject * /*bestSoFar*/,
                                       double changeUp, int numInfUp,
                                       double changeDown, int numInfDown)
{
    CbcModel * model = thisOne->model();
    int stateOfSearch = model->stateOfSearch() % 10;
    int betterWay = 0;
    double value = 0.0;
    if (!bestObject_) {
        bestCriterion_ = -1.0e30;
        bestNumberUp_ = COIN_INT_MAX;
        bestNumberDown_ = COIN_INT_MAX;
    }
    // maybe branch up more if no solution or not many nodes done?
    if (stateOfSearch <= 2) {
        //#define TRY_STUFF 1
#ifdef TRY_STUFF
        // before solution - choose smallest number
        // could add in depth as well
        int bestNumber = CoinMin(bestNumberUp_, bestNumberDown_);
        if (numInfUp < numInfDown) {
            if (numInfUp < bestNumber) {
                betterWay = 1;
            } else if (numInfUp == bestNumber) {
                if (changeUp < bestChangeUp_)
                    betterWay = 1;
            }
        } else if (numInfUp > numInfDown) {
            if (numInfDown < bestNumber) {
                betterWay = -1;
            } else if (numInfDown == bestNumber) {
                if (changeDown < bestChangeDown_)
                    betterWay = -1;
            }
        } else {
            // up and down have same number
            bool better = false;
            if (numInfUp < bestNumber) {
                better = true;
            } else if (numInfUp == bestNumber) {
                if (CoinMin(changeUp, changeDown) < CoinMin(bestChangeUp_, bestChangeDown_) - 1.0e-5)
                    better = true;;
            }
            if (better) {
                // see which way
                if (changeUp <= changeDown)
                    betterWay = 1;
                else
                    betterWay = -1;
            }
        }
        if (betterWay) {
            value = CoinMin(numInfUp, numInfDown);
        }
#else
        // use pseudo shadow prices modified by locks
        // test testosi
#ifndef JJF_ONE
        double objectiveValue = model->getCurrentMinimizationObjValue();
        double distanceToCutoff =  model->getCutoff()  - objectiveValue;
        if (distanceToCutoff < 1.0e20)
            distanceToCutoff *= 10.0;
        else
            distanceToCutoff = 1.0e2 + fabs(objectiveValue);
        distanceToCutoff = CoinMax(distanceToCutoff, 1.0e-12 * (1.0 + fabs(objectiveValue)));
        double continuousObjective = model->getContinuousObjective();
        double distanceToCutoffC =  model->getCutoff()  - continuousObjective;
        if (distanceToCutoffC > 1.0e20)
            distanceToCutoffC = 1.0e2 + fabs(objectiveValue);
        distanceToCutoffC = CoinMax(distanceToCutoffC, 1.0e-12 * (1.0 + fabs(objectiveValue)));
        int numberInfC = model->getContinuousInfeasibilities();
        double perInf = distanceToCutoffC / static_cast<double> (numberInfC);
        assert (perInf > 0.0);
        //int numberIntegers = model->numberIntegers();
        changeDown += perInf * numInfDown;
        changeUp += perInf * numInfUp;
#ifdef JJF_ZERO
        if (numInfDown == 1) {
            if (numInfUp == 1) {
                changeUp += 1.0e6;
                changeDown += 1.0e6;
            } else if (changeDown <= 1.5*changeUp) {
                changeUp += 1.0e6;
            }
        } else if (numInfUp == 1 && changeUp <= 1.5*changeDown) {
            changeDown += 1.0e6;
        }
#endif
#endif
        double minValue = CoinMin(changeDown, changeUp);
        double maxValue = CoinMax(changeDown, changeUp);
        value = WEIGHT_BEFORE * minValue + (1.0 - WEIGHT_BEFORE) * maxValue;
        if (value > bestCriterion_ + 1.0e-8) {
            if (changeUp <= 1.5*changeDown) {
                betterWay = 1;
            } else {
                betterWay = -1;
            }
        }
#endif
    } else {
#define TRY_STUFF 2
#if TRY_STUFF > 1
        // Get current number of infeasibilities, cutoff and current objective
        CbcNode * node = model->currentNode();
        int numberUnsatisfied = node->numberUnsatisfied();
        double cutoff = model->getCutoff();
        double objectiveValue = node->objectiveValue();
#endif
        // got a solution
        double minValue = CoinMin(changeDown, changeUp);
        double maxValue = CoinMax(changeDown, changeUp);
        // Reduce
#ifdef TRY_STUFF
        //maxValue = CoinMin(maxValue,minValue*4.0);
#else
        //maxValue = CoinMin(maxValue,minValue*2.0);
#endif
#ifndef WEIGHT_PRODUCT
        value = WEIGHT_AFTER * minValue + (1.0 - WEIGHT_AFTER) * maxValue;
#else
        double minProductWeight = model->getDblParam(CbcModel::CbcSmallChange);
        value = CoinMax(minValue, minProductWeight) * CoinMax(maxValue, minProductWeight);
        //value += minProductWeight*minValue;
#endif
        double useValue = value;
        double useBest = bestCriterion_;
#if TRY_STUFF>1
        int thisNumber = CoinMin(numInfUp, numInfDown);
        int bestNumber = CoinMin(bestNumberUp_, bestNumberDown_);
        double distance = cutoff - objectiveValue;
        assert (distance >= 0.0);
        if (useValue + 0.1*distance > useBest && useValue*1.1 > useBest &&
                useBest + 0.1*distance > useValue && useBest*1.1 > useValue) {
            // not much in it - look at unsatisfied
            if (thisNumber < numberUnsatisfied || bestNumber < numberUnsatisfied) {
                double perInteger = distance / (static_cast<double> (numberUnsatisfied));
                useValue += thisNumber * perInteger;
                useBest += bestNumber * perInteger;
            }
        }
#endif
        if (useValue > useBest + 1.0e-8) {
            if (changeUp <= 1.5*changeDown) {
                betterWay = 1;
            } else {
                betterWay = -1;
            }
        }
    }
#ifdef COIN_DEVELOP
    History hist;
    {
        CbcDynamicPseudoCostBranchingObject * branchingObject =
            dynamic_cast<CbcDynamicPseudoCostBranchingObject *>(thisOne);
        if (branchingObject) {
            CbcSimpleIntegerDynamicPseudoCost *  object = branchingObject->object();
            assert (object);
            hist.where_ = 'C';
            hist.status_ = ' ';
            hist.sequence_ = object->columnNumber();
            hist.numberUp_ = object->numberTimesUp();
            hist.numberUpInf_ = numInfUp;
            hist.sumUp_ = object->sumUpCost();
            hist.upEst_ = changeUp;
            hist.numberDown_ = object->numberTimesDown();
            hist.numberDownInf_ = numInfDown;
            hist.sumDown_ = object->sumDownCost();
            hist.downEst_ = changeDown;
        }
    }
#endif
    if (betterWay) {
#ifdef COIN_DEVELOP
        hist.status_ = 'B';
#endif
        // maybe change better way
        CbcDynamicPseudoCostBranchingObject * branchingObject =
            dynamic_cast<CbcDynamicPseudoCostBranchingObject *>(thisOne);
        if (branchingObject) {
            CbcSimpleIntegerDynamicPseudoCost *  object = branchingObject->object();
            double separator = object->upDownSeparator();
            if (separator > 0.0) {
                const double * solution = thisOne->model()->testSolution();
                double valueVariable = solution[object->columnNumber()];
                betterWay = (valueVariable - floor(valueVariable) >= separator) ? 1 : -1;
            }
        }
        bestCriterion_ = value;
        bestChangeUp_ = changeUp;
        bestNumberUp_ = numInfUp;
        bestChangeDown_ = changeDown;
        bestNumberDown_ = numInfDown;
        bestObject_ = thisOne;
        // See if user is overriding way
        if (thisOne->object() && thisOne->object()->preferredWay())
            betterWay = thisOne->object()->preferredWay();
    }
#ifdef COIN_DEVELOP
    addRecord(hist);
#endif
    return betterWay;
}
/* Sets or gets best criterion so far */
void
CbcBranchDynamicDecision::setBestCriterion(double value)
{
    bestCriterion_ = value;
}
double
CbcBranchDynamicDecision::getBestCriterion() const
{
    return bestCriterion_;
}
#ifdef COIN_DEVELOP
void printHistory(const char * file)
{
    if (!numberHistory)
        return;
    FILE * fp = fopen(file, "w");
    assert(fp);
    int numberIntegers = 0;
    int i;
    for (i = 0; i < numberHistory; i++) {
        if (history[i].where_ != 'C' || history[i].status_ != 'I')
            numberIntegers = CoinMax(numberIntegers, history[i].sequence_);
    }
    numberIntegers++;
    for (int iC = 0; iC < numberIntegers; iC++) {
        int n = 0;
        for (i = 0; i < numberHistory; i++) {
            if (history[i].sequence_ == iC) {
                if (!n)
                    fprintf(fp, "XXX %d\n", iC);
                n++;
                fprintf(fp, "%c%c up %8d %8d %12.5f %12.5f down  %8d %8d %12.5f %12.5f\n",
                        history[i].where_,
                        history[i].status_,
                        history[i].numberUp_,
                        history[i].numberUpInf_,
                        history[i].sumUp_,
                        history[i].upEst_,
                        history[i].numberDown_,
                        history[i].numberDownInf_,
                        history[i].sumDown_,
                        history[i].downEst_);
            }
        }
    }
    fclose(fp);
}
#endif

// Default Constructor
CbcDynamicPseudoCostBranchingObject::CbcDynamicPseudoCostBranchingObject()
        : CbcIntegerBranchingObject()
{
    changeInGuessed_ = 1.0e-5;
    object_ = NULL;
}

// Useful constructor
CbcDynamicPseudoCostBranchingObject::CbcDynamicPseudoCostBranchingObject (CbcModel * model,
        int variable,
        int way , double value,
        CbcSimpleIntegerDynamicPseudoCost * object)
        : CbcIntegerBranchingObject(model, variable, way, value)
{
    changeInGuessed_ = 1.0e-5;
    object_ = object;
}
// Does part of work for constructor
void
CbcDynamicPseudoCostBranchingObject::fillPart (int variable,
        int way , double value,
        CbcSimpleIntegerDynamicPseudoCost * object)
{
    CbcIntegerBranchingObject::fillPart(variable, way, value);
    changeInGuessed_ = 1.0e-5;
    object_ = object;
}
// Useful constructor for fixing
CbcDynamicPseudoCostBranchingObject::CbcDynamicPseudoCostBranchingObject (CbcModel * model,
        int variable, int way,
        double lowerValue,
        double /*upperValue*/)
        : CbcIntegerBranchingObject(model, variable, way, lowerValue)
{
    changeInGuessed_ = 1.0e100;
    object_ = NULL;
}


// Copy constructor
CbcDynamicPseudoCostBranchingObject::CbcDynamicPseudoCostBranchingObject (
    const CbcDynamicPseudoCostBranchingObject & rhs)
        : CbcIntegerBranchingObject(rhs)
{
    changeInGuessed_ = rhs.changeInGuessed_;
    object_ = rhs.object_;
}

// Assignment operator
CbcDynamicPseudoCostBranchingObject &
CbcDynamicPseudoCostBranchingObject::operator=( const CbcDynamicPseudoCostBranchingObject & rhs)
{
    if (this != &rhs) {
        CbcIntegerBranchingObject::operator=(rhs);
        changeInGuessed_ = rhs.changeInGuessed_;
        object_ = rhs.object_;
    }
    return *this;
}
CbcBranchingObject *
CbcDynamicPseudoCostBranchingObject::clone() const
{
    return (new CbcDynamicPseudoCostBranchingObject(*this));
}

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

/*
  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.
  Returns change in guessed objective on next branch
*/
double
CbcDynamicPseudoCostBranchingObject::branch()
{
    CbcIntegerBranchingObject::branch();
    return changeInGuessed_;
}
/* Some branchingObjects may claim to be able to skip
   strong branching.  If so they have to fill in CbcStrongInfo.
   The object mention in incoming CbcStrongInfo must match.
   Returns nonzero if skip is wanted */
int
CbcDynamicPseudoCostBranchingObject::fillStrongInfo( CbcStrongInfo & info)
{
    assert (object_);
    assert (info.possibleBranch == this);
    info.upMovement = object_->upDynamicPseudoCost() * (ceil(value_) - value_);
    info.downMovement = object_->downDynamicPseudoCost() * (value_ - floor(value_));
    info.numIntInfeasUp  -= static_cast<int> (object_->sumUpDecrease() /
                            (1.0e-12 + static_cast<double> (object_->numberTimesUp())));
    info.numIntInfeasUp = CoinMax(info.numIntInfeasUp, 0);
    info.numObjInfeasUp = 0;
    info.finishedUp = false;
    info.numItersUp = 0;
    info.numIntInfeasDown  -= static_cast<int> (object_->sumDownDecrease() /
                              (1.0e-12 + static_cast<double> (object_->numberTimesDown())));
    info.numIntInfeasDown = CoinMax(info.numIntInfeasDown, 0);
    info.numObjInfeasDown = 0;
    info.finishedDown = false;
    info.numItersDown = 0;
    info.fix = 0;
    if (object_->numberTimesUp() < object_->numberBeforeTrust() +
            2*object_->numberTimesUpInfeasible() ||
            object_->numberTimesDown() < object_->numberBeforeTrust() +
            2*object_->numberTimesDownInfeasible()) {
        return 0;
    } else {
        return 1;
    }
}