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