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