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