File: CbcHeuristicDiveCoefficient.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 (146 lines) | stat: -rw-r--r-- 4,627 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
/* $Id: CbcHeuristicDiveCoefficient.cpp 2094 2014-11-18 11:15:36Z forrest $ */
// Copyright (C) 2008, 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

//#define PRINT_DEBUG

#include "CbcHeuristicDiveCoefficient.hpp"
#include "CbcStrategy.hpp"

// Default Constructor
CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient()
        : CbcHeuristicDive()
{
  whereFrom_ |= 16*(1+256);
}

// Constructor from model
CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(CbcModel & model)
        : CbcHeuristicDive(model)
{
  whereFrom_ |= 16*(1+256);
}

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

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

// Create C++ lines to get to current state
void
CbcHeuristicDiveCoefficient::generateCpp( FILE * fp)
{
    CbcHeuristicDiveCoefficient other;
    fprintf(fp, "0#include \"CbcHeuristicDiveCoefficient.hpp\"\n");
    fprintf(fp, "3  CbcHeuristicDiveCoefficient heuristicDiveCoefficient(*cbcModel);\n");
    CbcHeuristic::generateCpp(fp, "heuristicDiveCoefficient");
    fprintf(fp, "3  cbcModel->addHeuristic(&heuristicDiveCoefficient);\n");
}

// Copy constructor
CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(const CbcHeuristicDiveCoefficient & rhs)
        :
        CbcHeuristicDive(rhs)
{
}

// Assignment operator
CbcHeuristicDiveCoefficient &
CbcHeuristicDiveCoefficient::operator=( const CbcHeuristicDiveCoefficient & rhs)
{
    if (this != &rhs) {
        CbcHeuristicDive::operator=(rhs);
    }
    return *this;
}

bool
CbcHeuristicDiveCoefficient::selectVariableToBranch(OsiSolverInterface* solver,
        const double* newSolution,
        int& bestColumn,
        int& bestRound)
{
    int numberIntegers = model_->numberIntegers();
    const int * integerVariable = model_->integerVariable();
    double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);

    bestColumn = -1;
    bestRound = -1; // -1 rounds down, +1 rounds up
    double bestFraction = COIN_DBL_MAX;
    int bestLocks = COIN_INT_MAX;
    bool allTriviallyRoundableSoFar = true;
    int bestPriority = COIN_INT_MAX;
    for (int i = 0; i < numberIntegers; i++) {
        int iColumn = integerVariable[i];
        double value = newSolution[iColumn];
        double fraction = value - floor(value);
        int round = 0;
        if (fabs(floor(value + 0.5) - value) > integerTolerance) {
            int nDownLocks = downLocks_[i];
            int nUpLocks = upLocks_[i];
            if (allTriviallyRoundableSoFar || (nDownLocks > 0 && nUpLocks > 0)) {

                if (allTriviallyRoundableSoFar && nDownLocks > 0 && nUpLocks > 0) {
                    allTriviallyRoundableSoFar = false;
                    bestFraction = COIN_DBL_MAX;
                    bestLocks = COIN_INT_MAX;
                }

                // the variable cannot be rounded
                int nLocks = nDownLocks;
                if (nDownLocks < nUpLocks)
                    round = -1;
                else if (nDownLocks > nUpLocks) {
                    round = 1;
                    fraction = 1.0 - fraction;
                    nLocks = nUpLocks;
                } else if (fraction < 0.5)
                    round = -1;
                else {
                    round = 1;
                    fraction = 1.0 - fraction;
                    nLocks = nUpLocks;
                }

                // if variable is not binary, penalize it
                if (!solver->isBinary(iColumn))
                    fraction *= 1000.0;

		// if priorities then use
		if (priority_) {
		  int thisRound=static_cast<int>(priority_[i].direction);
		  if ((thisRound&1)!=0) 
		    round = ((thisRound&2)==0) ? -1 : +1;
		  if (priority_[i].priority>bestPriority) {
		    nLocks=COIN_INT_MAX;
		  } else if (priority_[i].priority<bestPriority) {
		    bestPriority=static_cast<int>(priority_[i].priority);
		    bestLocks=COIN_INT_MAX;
		  }
		}
                if (nLocks < bestLocks || (nLocks == bestLocks &&
                                           fraction < bestFraction)) {
                    bestColumn = iColumn;
                    bestLocks = nLocks;
                    bestFraction = fraction;
                    bestRound = round;
                }
            }
        }
    }
    return allTriviallyRoundableSoFar;
}