1 | /* $Id: CbcHeuristicDiveFractional.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 "CbcHeuristicDiveFractional.hpp" |
---|
12 | #include "CbcStrategy.hpp" |
---|
13 | |
---|
14 | // Default Constructor |
---|
15 | CbcHeuristicDiveFractional::CbcHeuristicDiveFractional() |
---|
16 | : CbcHeuristicDive() |
---|
17 | { |
---|
18 | } |
---|
19 | |
---|
20 | // Constructor from model |
---|
21 | CbcHeuristicDiveFractional::CbcHeuristicDiveFractional(CbcModel & model) |
---|
22 | : CbcHeuristicDive(model) |
---|
23 | { |
---|
24 | } |
---|
25 | |
---|
26 | // Destructor |
---|
27 | CbcHeuristicDiveFractional::~CbcHeuristicDiveFractional () |
---|
28 | { |
---|
29 | } |
---|
30 | |
---|
31 | // Clone |
---|
32 | CbcHeuristicDiveFractional * |
---|
33 | CbcHeuristicDiveFractional::clone() const |
---|
34 | { |
---|
35 | return new CbcHeuristicDiveFractional(*this); |
---|
36 | } |
---|
37 | |
---|
38 | // Create C++ lines to get to current state |
---|
39 | void |
---|
40 | CbcHeuristicDiveFractional::generateCpp( FILE * fp) |
---|
41 | { |
---|
42 | CbcHeuristicDiveFractional other; |
---|
43 | fprintf(fp, "0#include \"CbcHeuristicDiveFractional.hpp\"\n"); |
---|
44 | fprintf(fp, "3 CbcHeuristicDiveFractional heuristicDiveFractional(*cbcModel);\n"); |
---|
45 | CbcHeuristic::generateCpp(fp, "heuristicDiveFractional"); |
---|
46 | fprintf(fp, "3 cbcModel->addHeuristic(&heuristicDiveFractional);\n"); |
---|
47 | } |
---|
48 | |
---|
49 | // Copy constructor |
---|
50 | CbcHeuristicDiveFractional::CbcHeuristicDiveFractional(const CbcHeuristicDiveFractional & rhs) |
---|
51 | : |
---|
52 | CbcHeuristicDive(rhs) |
---|
53 | { |
---|
54 | } |
---|
55 | |
---|
56 | // Assignment operator |
---|
57 | CbcHeuristicDiveFractional & |
---|
58 | CbcHeuristicDiveFractional::operator=( const CbcHeuristicDiveFractional & rhs) |
---|
59 | { |
---|
60 | if (this != &rhs) { |
---|
61 | CbcHeuristicDive::operator=(rhs); |
---|
62 | } |
---|
63 | return *this; |
---|
64 | } |
---|
65 | |
---|
66 | bool |
---|
67 | CbcHeuristicDiveFractional::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 = COIN_DBL_MAX; |
---|
79 | bool allTriviallyRoundableSoFar = true; |
---|
80 | for (int i = 0; i < numberIntegers; i++) { |
---|
81 | int iColumn = integerVariable[i]; |
---|
82 | double value = newSolution[iColumn]; |
---|
83 | double fraction = value - floor(value); |
---|
84 | int round = 0; |
---|
85 | if (fabs(floor(value + 0.5) - value) > integerTolerance) { |
---|
86 | if (allTriviallyRoundableSoFar || (downLocks_[i] > 0 && upLocks_[i] > 0)) { |
---|
87 | |
---|
88 | if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) { |
---|
89 | allTriviallyRoundableSoFar = false; |
---|
90 | bestFraction = COIN_DBL_MAX; |
---|
91 | } |
---|
92 | |
---|
93 | // the variable cannot be rounded |
---|
94 | if (fraction < 0.5) |
---|
95 | round = -1; |
---|
96 | else { |
---|
97 | round = 1; |
---|
98 | fraction = 1.0 - fraction; |
---|
99 | } |
---|
100 | |
---|
101 | // if variable is not binary, penalize it |
---|
102 | if (!solver->isBinary(iColumn)) |
---|
103 | fraction *= 1000.0; |
---|
104 | |
---|
105 | if (fraction < bestFraction) { |
---|
106 | bestColumn = iColumn; |
---|
107 | bestFraction = fraction; |
---|
108 | bestRound = round; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | return allTriviallyRoundableSoFar; |
---|
114 | } |
---|
115 | |
---|