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 | void |
---|
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 | for (int i=0; i<numberIntegers; i++) { |
---|
80 | int iColumn = integerVariable[i]; |
---|
81 | double value=newSolution[iColumn]; |
---|
82 | double fraction=value-floor(value); |
---|
83 | int round = 0; |
---|
84 | if (fabs(floor(value+0.5)-value)>integerTolerance) { |
---|
85 | int nDownLocks = downLocks_[i]; |
---|
86 | int nUpLocks = upLocks_[i]; |
---|
87 | if(nDownLocks>0 && nUpLocks>0) { |
---|
88 | // the variable cannot be rounded |
---|
89 | int nLocks = nDownLocks; |
---|
90 | if(nDownLocks<nUpLocks) |
---|
91 | round = -1; |
---|
92 | else if(nDownLocks>nUpLocks) { |
---|
93 | round = 1; |
---|
94 | fraction = 1.0 - fraction; |
---|
95 | nLocks = nUpLocks; |
---|
96 | } |
---|
97 | else if(fraction < 0.5) |
---|
98 | round = -1; |
---|
99 | else { |
---|
100 | round = 1; |
---|
101 | fraction = 1.0 - fraction; |
---|
102 | nLocks = nUpLocks; |
---|
103 | } |
---|
104 | |
---|
105 | // if variable is not binary, penalize it |
---|
106 | if(!solver->isBinary(iColumn)) |
---|
107 | fraction *= 1000.0; |
---|
108 | |
---|
109 | if(nLocks < bestLocks || (nLocks == bestLocks && |
---|
110 | fraction < bestFraction)) { |
---|
111 | bestColumn = iColumn; |
---|
112 | bestLocks = nLocks; |
---|
113 | bestFraction = fraction; |
---|
114 | bestRound = round; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|