[1271] | 1 | /* $Id: CbcHeuristicDivePseudoCost.cpp 1240 2009-10-02 18:41:44Z forrest $ */ |
---|
[945] | 2 | // Copyright (C) 2008, International Business Machines |
---|
| 3 | // Corporation and others. All Rights Reserved. |
---|
[1573] | 4 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
---|
| 5 | |
---|
[945] | 6 | #if defined(_MSC_VER) |
---|
| 7 | // Turn off compiler warning about long names |
---|
| 8 | # pragma warning(disable:4786) |
---|
| 9 | #endif |
---|
| 10 | |
---|
| 11 | #include "CbcHeuristicDivePseudoCost.hpp" |
---|
| 12 | #include "CbcStrategy.hpp" |
---|
[1271] | 13 | #include "CbcBranchDynamic.hpp" |
---|
[945] | 14 | |
---|
| 15 | // Default Constructor |
---|
[1286] | 16 | CbcHeuristicDivePseudoCost::CbcHeuristicDivePseudoCost() |
---|
| 17 | : CbcHeuristicDive() |
---|
[945] | 18 | { |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | // Constructor from model |
---|
| 22 | CbcHeuristicDivePseudoCost::CbcHeuristicDivePseudoCost(CbcModel & model) |
---|
[1286] | 23 | : CbcHeuristicDive(model) |
---|
[945] | 24 | { |
---|
| 25 | } |
---|
| 26 | |
---|
[1286] | 27 | // Destructor |
---|
[945] | 28 | CbcHeuristicDivePseudoCost::~CbcHeuristicDivePseudoCost () |
---|
| 29 | { |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | // Clone |
---|
| 33 | CbcHeuristicDivePseudoCost * |
---|
| 34 | CbcHeuristicDivePseudoCost::clone() const |
---|
| 35 | { |
---|
[1286] | 36 | return new CbcHeuristicDivePseudoCost(*this); |
---|
[945] | 37 | } |
---|
| 38 | |
---|
| 39 | // Create C++ lines to get to current state |
---|
[1286] | 40 | void |
---|
| 41 | CbcHeuristicDivePseudoCost::generateCpp( FILE * fp) |
---|
[945] | 42 | { |
---|
[1286] | 43 | CbcHeuristicDivePseudoCost other; |
---|
| 44 | fprintf(fp, "0#include \"CbcHeuristicDivePseudoCost.hpp\"\n"); |
---|
| 45 | fprintf(fp, "3 CbcHeuristicDivePseudoCost heuristicDivePseudoCost(*cbcModel);\n"); |
---|
| 46 | CbcHeuristic::generateCpp(fp, "heuristicDivePseudoCost"); |
---|
| 47 | fprintf(fp, "3 cbcModel->addHeuristic(&heuristicDivePseudoCost);\n"); |
---|
[945] | 48 | } |
---|
| 49 | |
---|
[1286] | 50 | // Copy constructor |
---|
[945] | 51 | CbcHeuristicDivePseudoCost::CbcHeuristicDivePseudoCost(const CbcHeuristicDivePseudoCost & rhs) |
---|
[1286] | 52 | : |
---|
| 53 | CbcHeuristicDive(rhs) |
---|
[945] | 54 | { |
---|
| 55 | } |
---|
| 56 | |
---|
[1286] | 57 | // Assignment operator |
---|
| 58 | CbcHeuristicDivePseudoCost & |
---|
| 59 | CbcHeuristicDivePseudoCost::operator=( const CbcHeuristicDivePseudoCost & rhs) |
---|
[945] | 60 | { |
---|
[1286] | 61 | if (this != &rhs) { |
---|
| 62 | CbcHeuristicDive::operator=(rhs); |
---|
| 63 | } |
---|
| 64 | return *this; |
---|
[945] | 65 | } |
---|
| 66 | |
---|
| 67 | bool |
---|
| 68 | CbcHeuristicDivePseudoCost::selectVariableToBranch(OsiSolverInterface* solver, |
---|
[1286] | 69 | const double* newSolution, |
---|
| 70 | int& bestColumn, |
---|
| 71 | int& bestRound) |
---|
[945] | 72 | { |
---|
[1286] | 73 | int numberIntegers = model_->numberIntegers(); |
---|
| 74 | const int * integerVariable = model_->integerVariable(); |
---|
| 75 | double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); |
---|
[945] | 76 | |
---|
[1286] | 77 | // get the LP relaxation solution at the root node |
---|
| 78 | double * rootNodeLPSol = model_->continuousSolution(); |
---|
[945] | 79 | |
---|
[1286] | 80 | // get pseudo costs |
---|
| 81 | double * pseudoCostDown = downArray_; |
---|
| 82 | double * pseudoCostUp = upArray_; |
---|
[945] | 83 | |
---|
[1286] | 84 | bestColumn = -1; |
---|
| 85 | bestRound = -1; // -1 rounds down, +1 rounds up |
---|
| 86 | double bestScore = -1.0; |
---|
| 87 | bool allTriviallyRoundableSoFar = true; |
---|
| 88 | for (int i = 0; i < numberIntegers; i++) { |
---|
| 89 | int iColumn = integerVariable[i]; |
---|
| 90 | double rootValue = rootNodeLPSol[iColumn]; |
---|
| 91 | double value = newSolution[iColumn]; |
---|
| 92 | double fraction = value - floor(value); |
---|
| 93 | int round = 0; |
---|
| 94 | if (fabs(floor(value + 0.5) - value) > integerTolerance) { |
---|
| 95 | if (allTriviallyRoundableSoFar || (downLocks_[i] > 0 && upLocks_[i] > 0)) { |
---|
[945] | 96 | |
---|
[1286] | 97 | if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) { |
---|
| 98 | allTriviallyRoundableSoFar = false; |
---|
| 99 | bestScore = -1.0; |
---|
| 100 | } |
---|
[945] | 101 | |
---|
[1286] | 102 | double pCostDown = pseudoCostDown[i]; |
---|
| 103 | double pCostUp = pseudoCostUp[i]; |
---|
| 104 | assert(pCostDown >= 0.0 && pCostUp >= 0.0); |
---|
[945] | 105 | |
---|
[1286] | 106 | if (allTriviallyRoundableSoFar && downLocks_[i] == 0 && upLocks_[i] > 0) |
---|
| 107 | round = 1; |
---|
| 108 | else if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] == 0) |
---|
| 109 | round = -1; |
---|
| 110 | else if (value - rootValue < -0.4) |
---|
| 111 | round = -1; |
---|
| 112 | else if (value - rootValue > 0.4) |
---|
| 113 | round = 1; |
---|
| 114 | else if (fraction < 0.3) |
---|
| 115 | round = -1; |
---|
| 116 | else if (fraction > 0.7) |
---|
| 117 | round = 1; |
---|
| 118 | else if (pCostDown < pCostUp) |
---|
| 119 | round = -1; |
---|
| 120 | else |
---|
| 121 | round = 1; |
---|
[945] | 122 | |
---|
[1286] | 123 | // calculate score |
---|
| 124 | double score; |
---|
| 125 | if (round == 1) |
---|
| 126 | score = fraction * (pCostDown + 1.0) / (pCostUp + 1.0); |
---|
| 127 | else |
---|
| 128 | score = (1.0 - fraction) * (pCostUp + 1.0) / (pCostDown + 1.0); |
---|
[945] | 129 | |
---|
[1286] | 130 | // if variable is binary, increase its chance of being selected |
---|
| 131 | if (solver->isBinary(iColumn)) |
---|
| 132 | score *= 1000.0; |
---|
| 133 | |
---|
| 134 | if (score > bestScore) { |
---|
| 135 | bestColumn = iColumn; |
---|
| 136 | bestScore = score; |
---|
| 137 | bestRound = round; |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
[945] | 141 | } |
---|
| 142 | |
---|
[1286] | 143 | return allTriviallyRoundableSoFar; |
---|
[945] | 144 | } |
---|
[1271] | 145 | void |
---|
| 146 | CbcHeuristicDivePseudoCost::initializeData() |
---|
| 147 | { |
---|
[1286] | 148 | int numberIntegers = model_->numberIntegers(); |
---|
| 149 | if (!downArray_) { |
---|
| 150 | downArray_ = new double [numberIntegers]; |
---|
| 151 | upArray_ = new double [numberIntegers]; |
---|
[1271] | 152 | } |
---|
[1286] | 153 | // get pseudo costs |
---|
| 154 | model_->fillPseudoCosts(downArray_, upArray_); |
---|
| 155 | int diveOptions = when_ / 100; |
---|
| 156 | if (diveOptions) { |
---|
| 157 | // pseudo shadow prices |
---|
| 158 | int k = diveOptions % 100; |
---|
| 159 | if (diveOptions >= 100) |
---|
| 160 | k += 32; |
---|
| 161 | model_->pseudoShadow(k - 1); |
---|
| 162 | int numberInts = CoinMin(model_->numberObjects(), numberIntegers); |
---|
| 163 | OsiObject ** objects = model_->objects(); |
---|
| 164 | for (int i = 0; i < numberInts; i++) { |
---|
| 165 | CbcSimpleIntegerDynamicPseudoCost * obj1 = |
---|
| 166 | dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(objects[i]) ; |
---|
| 167 | if (obj1) { |
---|
| 168 | //int iColumn = obj1->columnNumber(); |
---|
| 169 | double downPseudoCost = 1.0e-2 * obj1->downDynamicPseudoCost(); |
---|
| 170 | double downShadow = obj1->downShadowPrice(); |
---|
| 171 | double upPseudoCost = 1.0e-2 * obj1->upDynamicPseudoCost(); |
---|
| 172 | double upShadow = obj1->upShadowPrice(); |
---|
| 173 | downPseudoCost = CoinMax(downPseudoCost, downShadow); |
---|
| 174 | downPseudoCost = CoinMax(downPseudoCost, 0.001 * upShadow); |
---|
| 175 | downArray_[i] = downPseudoCost; |
---|
| 176 | upPseudoCost = CoinMax(upPseudoCost, upShadow); |
---|
| 177 | upPseudoCost = CoinMax(upPseudoCost, 0.001 * downShadow); |
---|
| 178 | upArray_[i] = upPseudoCost; |
---|
| 179 | } |
---|
| 180 | } |
---|
| 181 | } |
---|
[1271] | 182 | } |
---|
| 183 | // Fix other variables at bounds |
---|
[1286] | 184 | int |
---|
[1271] | 185 | CbcHeuristicDivePseudoCost::fixOtherVariables(OsiSolverInterface * solver, |
---|
[1286] | 186 | const double * solution, |
---|
| 187 | PseudoReducedCost * candidate, |
---|
| 188 | const double * random) |
---|
[1271] | 189 | { |
---|
[1286] | 190 | const double * lower = solver->getColLower(); |
---|
| 191 | const double * upper = solver->getColUpper(); |
---|
| 192 | double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); |
---|
| 193 | double primalTolerance; |
---|
| 194 | solver->getDblParam(OsiPrimalTolerance, primalTolerance); |
---|
[1271] | 195 | |
---|
[1286] | 196 | int numberIntegers = model_->numberIntegers(); |
---|
| 197 | const int * integerVariable = model_->integerVariable(); |
---|
| 198 | const double* reducedCost = solver->getReducedCost(); |
---|
| 199 | // fix other integer variables that are at their bounds |
---|
| 200 | int cnt = 0; |
---|
| 201 | int numberFree = 0; |
---|
| 202 | int numberFixedAlready = 0; |
---|
| 203 | for (int i = 0; i < numberIntegers; i++) { |
---|
| 204 | int iColumn = integerVariable[i]; |
---|
| 205 | if (upper[iColumn] > lower[iColumn]) { |
---|
| 206 | numberFree++; |
---|
| 207 | double value = solution[iColumn]; |
---|
| 208 | if (value - lower[iColumn] <= integerTolerance) { |
---|
| 209 | candidate[cnt].var = iColumn; |
---|
| 210 | candidate[cnt++].pseudoRedCost = CoinMax(1.0e-2 * reducedCost[iColumn], |
---|
| 211 | downArray_[i]) * random[i]; |
---|
| 212 | } else if (upper[iColumn] - value <= integerTolerance) { |
---|
| 213 | candidate[cnt].var = iColumn; |
---|
| 214 | candidate[cnt++].pseudoRedCost = CoinMax(-1.0e-2 * reducedCost[iColumn], |
---|
| 215 | downArray_[i]) * random[i]; |
---|
| 216 | } |
---|
| 217 | } else { |
---|
| 218 | numberFixedAlready++; |
---|
| 219 | } |
---|
[1271] | 220 | } |
---|
| 221 | #ifdef CLP_INVESTIGATE |
---|
[1286] | 222 | printf("cutoff %g obj %g - %d free, %d fixed\n", |
---|
| 223 | model_->getCutoff(), solver->getObjValue(), numberFree, |
---|
| 224 | numberFixedAlready); |
---|
[1271] | 225 | #endif |
---|
[1286] | 226 | return cnt; |
---|
| 227 | //return CbcHeuristicDive::fixOtherVariables(solver, solution, |
---|
| 228 | // candidate, random); |
---|
[1271] | 229 | } |
---|
[1432] | 230 | |
---|