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