1 | /* $Id: CbcHeuristicDiveCoefficient.cpp 2094 2014-11-18 11:15:36Z forrest $ */ |
---|
2 | // Copyright (C) 2008, International Business Machines |
---|
3 | // Corporation and others. All Rights Reserved. |
---|
4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
---|
5 | |
---|
6 | #if defined(_MSC_VER) |
---|
7 | // Turn off compiler warning about long names |
---|
8 | # pragma warning(disable:4786) |
---|
9 | #endif |
---|
10 | |
---|
11 | //#define PRINT_DEBUG |
---|
12 | |
---|
13 | #include "CbcHeuristicDiveCoefficient.hpp" |
---|
14 | #include "CbcStrategy.hpp" |
---|
15 | |
---|
16 | // Default Constructor |
---|
17 | CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient() |
---|
18 | : CbcHeuristicDive() |
---|
19 | { |
---|
20 | whereFrom_ |= 16*(1+256); |
---|
21 | } |
---|
22 | |
---|
23 | // Constructor from model |
---|
24 | CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(CbcModel & model) |
---|
25 | : CbcHeuristicDive(model) |
---|
26 | { |
---|
27 | whereFrom_ |= 16*(1+256); |
---|
28 | } |
---|
29 | |
---|
30 | // Destructor |
---|
31 | CbcHeuristicDiveCoefficient::~CbcHeuristicDiveCoefficient () |
---|
32 | { |
---|
33 | } |
---|
34 | |
---|
35 | // Clone |
---|
36 | CbcHeuristicDiveCoefficient * |
---|
37 | CbcHeuristicDiveCoefficient::clone() const |
---|
38 | { |
---|
39 | return new CbcHeuristicDiveCoefficient(*this); |
---|
40 | } |
---|
41 | |
---|
42 | // Create C++ lines to get to current state |
---|
43 | void |
---|
44 | CbcHeuristicDiveCoefficient::generateCpp( FILE * fp) |
---|
45 | { |
---|
46 | CbcHeuristicDiveCoefficient other; |
---|
47 | fprintf(fp, "0#include \"CbcHeuristicDiveCoefficient.hpp\"\n"); |
---|
48 | fprintf(fp, "3 CbcHeuristicDiveCoefficient heuristicDiveCoefficient(*cbcModel);\n"); |
---|
49 | CbcHeuristic::generateCpp(fp, "heuristicDiveCoefficient"); |
---|
50 | fprintf(fp, "3 cbcModel->addHeuristic(&heuristicDiveCoefficient);\n"); |
---|
51 | } |
---|
52 | |
---|
53 | // Copy constructor |
---|
54 | CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(const CbcHeuristicDiveCoefficient & rhs) |
---|
55 | : |
---|
56 | CbcHeuristicDive(rhs) |
---|
57 | { |
---|
58 | } |
---|
59 | |
---|
60 | // Assignment operator |
---|
61 | CbcHeuristicDiveCoefficient & |
---|
62 | CbcHeuristicDiveCoefficient::operator=( const CbcHeuristicDiveCoefficient & rhs) |
---|
63 | { |
---|
64 | if (this != &rhs) { |
---|
65 | CbcHeuristicDive::operator=(rhs); |
---|
66 | } |
---|
67 | return *this; |
---|
68 | } |
---|
69 | |
---|
70 | bool |
---|
71 | CbcHeuristicDiveCoefficient::selectVariableToBranch(OsiSolverInterface* solver, |
---|
72 | const double* newSolution, |
---|
73 | int& bestColumn, |
---|
74 | int& bestRound) |
---|
75 | { |
---|
76 | int numberIntegers = model_->numberIntegers(); |
---|
77 | const int * integerVariable = model_->integerVariable(); |
---|
78 | double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); |
---|
79 | |
---|
80 | bestColumn = -1; |
---|
81 | bestRound = -1; // -1 rounds down, +1 rounds up |
---|
82 | double bestFraction = COIN_DBL_MAX; |
---|
83 | int bestLocks = COIN_INT_MAX; |
---|
84 | bool allTriviallyRoundableSoFar = true; |
---|
85 | int bestPriority = COIN_INT_MAX; |
---|
86 | for (int i = 0; i < numberIntegers; i++) { |
---|
87 | int iColumn = integerVariable[i]; |
---|
88 | double value = newSolution[iColumn]; |
---|
89 | double fraction = value - floor(value); |
---|
90 | int round = 0; |
---|
91 | if (fabs(floor(value + 0.5) - value) > integerTolerance) { |
---|
92 | int nDownLocks = downLocks_[i]; |
---|
93 | int nUpLocks = upLocks_[i]; |
---|
94 | if (allTriviallyRoundableSoFar || (nDownLocks > 0 && nUpLocks > 0)) { |
---|
95 | |
---|
96 | if (allTriviallyRoundableSoFar && nDownLocks > 0 && nUpLocks > 0) { |
---|
97 | allTriviallyRoundableSoFar = false; |
---|
98 | bestFraction = COIN_DBL_MAX; |
---|
99 | bestLocks = COIN_INT_MAX; |
---|
100 | } |
---|
101 | |
---|
102 | // the variable cannot be rounded |
---|
103 | int nLocks = nDownLocks; |
---|
104 | if (nDownLocks < nUpLocks) |
---|
105 | round = -1; |
---|
106 | else if (nDownLocks > nUpLocks) { |
---|
107 | round = 1; |
---|
108 | fraction = 1.0 - fraction; |
---|
109 | nLocks = nUpLocks; |
---|
110 | } else if (fraction < 0.5) |
---|
111 | round = -1; |
---|
112 | else { |
---|
113 | round = 1; |
---|
114 | fraction = 1.0 - fraction; |
---|
115 | nLocks = nUpLocks; |
---|
116 | } |
---|
117 | |
---|
118 | // if variable is not binary, penalize it |
---|
119 | if (!solver->isBinary(iColumn)) |
---|
120 | fraction *= 1000.0; |
---|
121 | |
---|
122 | // if priorities then use |
---|
123 | if (priority_) { |
---|
124 | int thisRound=static_cast<int>(priority_[i].direction); |
---|
125 | if ((thisRound&1)!=0) |
---|
126 | round = ((thisRound&2)==0) ? -1 : +1; |
---|
127 | if (priority_[i].priority>bestPriority) { |
---|
128 | nLocks=COIN_INT_MAX; |
---|
129 | } else if (priority_[i].priority<bestPriority) { |
---|
130 | bestPriority=static_cast<int>(priority_[i].priority); |
---|
131 | bestLocks=COIN_INT_MAX; |
---|
132 | } |
---|
133 | } |
---|
134 | if (nLocks < bestLocks || (nLocks == bestLocks && |
---|
135 | fraction < bestFraction)) { |
---|
136 | bestColumn = iColumn; |
---|
137 | bestLocks = nLocks; |
---|
138 | bestFraction = fraction; |
---|
139 | bestRound = round; |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | } |
---|
144 | return allTriviallyRoundableSoFar; |
---|
145 | } |
---|
146 | |
---|