1 | /* $Id: CbcHeuristicDiveCoefficient.cpp 1899 2013-04-09 18:12:08Z 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 | } |
---|
21 | |
---|
22 | // Constructor from model |
---|
23 | CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(CbcModel & model) |
---|
24 | : CbcHeuristicDive(model) |
---|
25 | { |
---|
26 | } |
---|
27 | |
---|
28 | // Destructor |
---|
29 | CbcHeuristicDiveCoefficient::~CbcHeuristicDiveCoefficient () |
---|
30 | { |
---|
31 | } |
---|
32 | |
---|
33 | // Clone |
---|
34 | CbcHeuristicDiveCoefficient * |
---|
35 | CbcHeuristicDiveCoefficient::clone() const |
---|
36 | { |
---|
37 | return new CbcHeuristicDiveCoefficient(*this); |
---|
38 | } |
---|
39 | |
---|
40 | // Create C++ lines to get to current state |
---|
41 | void |
---|
42 | CbcHeuristicDiveCoefficient::generateCpp( FILE * fp) |
---|
43 | { |
---|
44 | CbcHeuristicDiveCoefficient other; |
---|
45 | fprintf(fp, "0#include \"CbcHeuristicDiveCoefficient.hpp\"\n"); |
---|
46 | fprintf(fp, "3 CbcHeuristicDiveCoefficient heuristicDiveCoefficient(*cbcModel);\n"); |
---|
47 | CbcHeuristic::generateCpp(fp, "heuristicDiveCoefficient"); |
---|
48 | fprintf(fp, "3 cbcModel->addHeuristic(&heuristicDiveCoefficient);\n"); |
---|
49 | } |
---|
50 | |
---|
51 | // Copy constructor |
---|
52 | CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(const CbcHeuristicDiveCoefficient & rhs) |
---|
53 | : |
---|
54 | CbcHeuristicDive(rhs) |
---|
55 | { |
---|
56 | } |
---|
57 | |
---|
58 | // Assignment operator |
---|
59 | CbcHeuristicDiveCoefficient & |
---|
60 | CbcHeuristicDiveCoefficient::operator=( const CbcHeuristicDiveCoefficient & rhs) |
---|
61 | { |
---|
62 | if (this != &rhs) { |
---|
63 | CbcHeuristicDive::operator=(rhs); |
---|
64 | } |
---|
65 | return *this; |
---|
66 | } |
---|
67 | |
---|
68 | bool |
---|
69 | CbcHeuristicDiveCoefficient::selectVariableToBranch(OsiSolverInterface* solver, |
---|
70 | const double* newSolution, |
---|
71 | int& bestColumn, |
---|
72 | int& bestRound) |
---|
73 | { |
---|
74 | int numberIntegers = model_->numberIntegers(); |
---|
75 | const int * integerVariable = model_->integerVariable(); |
---|
76 | double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); |
---|
77 | |
---|
78 | bestColumn = -1; |
---|
79 | bestRound = -1; // -1 rounds down, +1 rounds up |
---|
80 | double bestFraction = COIN_DBL_MAX; |
---|
81 | int bestLocks = COIN_INT_MAX; |
---|
82 | bool allTriviallyRoundableSoFar = true; |
---|
83 | for (int i = 0; i < numberIntegers; i++) { |
---|
84 | int iColumn = integerVariable[i]; |
---|
85 | double value = newSolution[iColumn]; |
---|
86 | double fraction = value - floor(value); |
---|
87 | int round = 0; |
---|
88 | if (fabs(floor(value + 0.5) - value) > integerTolerance) { |
---|
89 | int nDownLocks = downLocks_[i]; |
---|
90 | int nUpLocks = upLocks_[i]; |
---|
91 | if (allTriviallyRoundableSoFar || (nDownLocks > 0 && nUpLocks > 0)) { |
---|
92 | |
---|
93 | if (allTriviallyRoundableSoFar && nDownLocks > 0 && nUpLocks > 0) { |
---|
94 | allTriviallyRoundableSoFar = false; |
---|
95 | bestFraction = COIN_DBL_MAX; |
---|
96 | bestLocks = COIN_INT_MAX; |
---|
97 | } |
---|
98 | |
---|
99 | // the variable cannot be rounded |
---|
100 | int nLocks = nDownLocks; |
---|
101 | if (nDownLocks < nUpLocks) |
---|
102 | round = -1; |
---|
103 | else if (nDownLocks > nUpLocks) { |
---|
104 | round = 1; |
---|
105 | fraction = 1.0 - fraction; |
---|
106 | nLocks = nUpLocks; |
---|
107 | } else if (fraction < 0.5) |
---|
108 | round = -1; |
---|
109 | else { |
---|
110 | round = 1; |
---|
111 | fraction = 1.0 - fraction; |
---|
112 | nLocks = nUpLocks; |
---|
113 | } |
---|
114 | |
---|
115 | // if variable is not binary, penalize it |
---|
116 | if (!solver->isBinary(iColumn)) |
---|
117 | fraction *= 1000.0; |
---|
118 | |
---|
119 | if (nLocks < bestLocks || (nLocks == bestLocks && |
---|
120 | fraction < bestFraction)) { |
---|
121 | bestColumn = iColumn; |
---|
122 | bestLocks = nLocks; |
---|
123 | bestFraction = fraction; |
---|
124 | bestRound = round; |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | } |
---|
129 | return allTriviallyRoundableSoFar; |
---|
130 | } |
---|
131 | |
---|