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 | void |
---|
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 | 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(downLocks_[i]>0&&upLocks_[i]>0) { |
---|
87 | // the variable cannot be rounded |
---|
88 | double obj = direction * objective[iColumn]; |
---|
89 | if(obj >= 0.0) |
---|
90 | round = 1; // round up |
---|
91 | else |
---|
92 | round = -1; // round down |
---|
93 | double objDelta; |
---|
94 | if(round == 1) |
---|
95 | objDelta = (1.0 - fraction) * obj; |
---|
96 | else |
---|
97 | objDelta = - fraction * obj; |
---|
98 | |
---|
99 | // we want the smaller score |
---|
100 | double score = objDelta / ((double) columnLength[iColumn] + 1.0); |
---|
101 | |
---|
102 | // if variable is not binary, penalize it |
---|
103 | if(!solver->isBinary(iColumn)) |
---|
104 | score *= 1000.0; |
---|
105 | |
---|
106 | if(score < bestScore) { |
---|
107 | bestColumn = iColumn; |
---|
108 | bestScore = score; |
---|
109 | bestRound = round; |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|