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