1 | // Copyright (C) 2008, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | #if defined(_MSC_VER) |
---|
4 | // Turn off compiler warning about long names |
---|
5 | # pragma warning(disable:4786) |
---|
6 | #endif |
---|
7 | |
---|
8 | //#define PRINT_DEBUG |
---|
9 | |
---|
10 | #include "CbcHeuristicDiveCoefficient.hpp" |
---|
11 | #include "CbcStrategy.hpp" |
---|
12 | |
---|
13 | // Default Constructor |
---|
14 | CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient() |
---|
15 | :CbcHeuristicDive() |
---|
16 | { |
---|
17 | } |
---|
18 | |
---|
19 | // Constructor from model |
---|
20 | CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(CbcModel & model) |
---|
21 | :CbcHeuristicDive(model) |
---|
22 | { |
---|
23 | } |
---|
24 | |
---|
25 | // Destructor |
---|
26 | CbcHeuristicDiveCoefficient::~CbcHeuristicDiveCoefficient () |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | // Clone |
---|
31 | CbcHeuristicDiveCoefficient * |
---|
32 | CbcHeuristicDiveCoefficient::clone() const |
---|
33 | { |
---|
34 | return new CbcHeuristicDiveCoefficient(*this); |
---|
35 | } |
---|
36 | |
---|
37 | // Create C++ lines to get to current state |
---|
38 | void |
---|
39 | CbcHeuristicDiveCoefficient::generateCpp( FILE * fp) |
---|
40 | { |
---|
41 | CbcHeuristicDiveCoefficient other; |
---|
42 | fprintf(fp,"0#include \"CbcHeuristicDiveCoefficient.hpp\"\n"); |
---|
43 | fprintf(fp,"3 CbcHeuristicDiveCoefficient heuristicDiveCoefficient(*cbcModel);\n"); |
---|
44 | CbcHeuristic::generateCpp(fp,"heuristicDiveCoefficient"); |
---|
45 | fprintf(fp,"3 cbcModel->addHeuristic(&heuristicDiveCoefficient);\n"); |
---|
46 | } |
---|
47 | |
---|
48 | // Copy constructor |
---|
49 | CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(const CbcHeuristicDiveCoefficient & rhs) |
---|
50 | : |
---|
51 | CbcHeuristicDive(rhs) |
---|
52 | { |
---|
53 | } |
---|
54 | |
---|
55 | // Assignment operator |
---|
56 | CbcHeuristicDiveCoefficient & |
---|
57 | CbcHeuristicDiveCoefficient::operator=( const CbcHeuristicDiveCoefficient& rhs) |
---|
58 | { |
---|
59 | if (this!=&rhs) { |
---|
60 | CbcHeuristicDive::operator=(rhs); |
---|
61 | } |
---|
62 | return *this; |
---|
63 | } |
---|
64 | |
---|
65 | bool |
---|
66 | CbcHeuristicDiveCoefficient::selectVariableToBranch(OsiSolverInterface* solver, |
---|
67 | const double* newSolution, |
---|
68 | int& bestColumn, |
---|
69 | int& bestRound) |
---|
70 | { |
---|
71 | int numberIntegers = model_->numberIntegers(); |
---|
72 | const int * integerVariable = model_->integerVariable(); |
---|
73 | double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); |
---|
74 | |
---|
75 | bestColumn = -1; |
---|
76 | bestRound = -1; // -1 rounds down, +1 rounds up |
---|
77 | double bestFraction = DBL_MAX; |
---|
78 | int bestLocks = COIN_INT_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 | int nDownLocks = downLocks_[i]; |
---|
87 | int nUpLocks = upLocks_[i]; |
---|
88 | if(allTriviallyRoundableSoFar||(nDownLocks>0 && nUpLocks>0)) { |
---|
89 | |
---|
90 | if(allTriviallyRoundableSoFar&&nDownLocks>0 && nUpLocks>0) { |
---|
91 | allTriviallyRoundableSoFar = false; |
---|
92 | bestFraction = DBL_MAX; |
---|
93 | bestLocks = COIN_INT_MAX; |
---|
94 | } |
---|
95 | |
---|
96 | // the variable cannot be rounded |
---|
97 | int nLocks = nDownLocks; |
---|
98 | if(nDownLocks<nUpLocks) |
---|
99 | round = -1; |
---|
100 | else if(nDownLocks>nUpLocks) { |
---|
101 | round = 1; |
---|
102 | fraction = 1.0 - fraction; |
---|
103 | nLocks = nUpLocks; |
---|
104 | } |
---|
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 | } |
---|