[1854] | 1 | /* $Id: CbcHeuristicDivePseudoCost.cpp 1922 2013-05-16 07:35:51Z tkr $ */ |
---|
[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_); |
---|
[1922] | 155 | // allow for -999 -> force to run |
---|
| 156 | int diveOptions = (when_>0) ? when_ / 100 : 0; |
---|
[1286] | 157 | if (diveOptions) { |
---|
| 158 | // pseudo shadow prices |
---|
| 159 | int k = diveOptions % 100; |
---|
| 160 | if (diveOptions >= 100) |
---|
| 161 | k += 32; |
---|
| 162 | model_->pseudoShadow(k - 1); |
---|
| 163 | int numberInts = CoinMin(model_->numberObjects(), numberIntegers); |
---|
| 164 | OsiObject ** objects = model_->objects(); |
---|
| 165 | for (int i = 0; i < numberInts; i++) { |
---|
| 166 | CbcSimpleIntegerDynamicPseudoCost * obj1 = |
---|
| 167 | dynamic_cast <CbcSimpleIntegerDynamicPseudoCost *>(objects[i]) ; |
---|
| 168 | if (obj1) { |
---|
| 169 | //int iColumn = obj1->columnNumber(); |
---|
| 170 | double downPseudoCost = 1.0e-2 * obj1->downDynamicPseudoCost(); |
---|
| 171 | double downShadow = obj1->downShadowPrice(); |
---|
| 172 | double upPseudoCost = 1.0e-2 * obj1->upDynamicPseudoCost(); |
---|
| 173 | double upShadow = obj1->upShadowPrice(); |
---|
| 174 | downPseudoCost = CoinMax(downPseudoCost, downShadow); |
---|
| 175 | downPseudoCost = CoinMax(downPseudoCost, 0.001 * upShadow); |
---|
| 176 | downArray_[i] = downPseudoCost; |
---|
| 177 | upPseudoCost = CoinMax(upPseudoCost, upShadow); |
---|
| 178 | upPseudoCost = CoinMax(upPseudoCost, 0.001 * downShadow); |
---|
| 179 | upArray_[i] = upPseudoCost; |
---|
| 180 | } |
---|
| 181 | } |
---|
| 182 | } |
---|
[1271] | 183 | } |
---|
| 184 | // Fix other variables at bounds |
---|
[1286] | 185 | int |
---|
[1271] | 186 | CbcHeuristicDivePseudoCost::fixOtherVariables(OsiSolverInterface * solver, |
---|
[1286] | 187 | const double * solution, |
---|
| 188 | PseudoReducedCost * candidate, |
---|
| 189 | const double * random) |
---|
[1271] | 190 | { |
---|
[1286] | 191 | const double * lower = solver->getColLower(); |
---|
| 192 | const double * upper = solver->getColUpper(); |
---|
| 193 | double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance); |
---|
| 194 | double primalTolerance; |
---|
| 195 | solver->getDblParam(OsiPrimalTolerance, primalTolerance); |
---|
[1271] | 196 | |
---|
[1286] | 197 | int numberIntegers = model_->numberIntegers(); |
---|
| 198 | const int * integerVariable = model_->integerVariable(); |
---|
| 199 | const double* reducedCost = solver->getReducedCost(); |
---|
| 200 | // fix other integer variables that are at their bounds |
---|
| 201 | int cnt = 0; |
---|
| 202 | int numberFree = 0; |
---|
| 203 | int numberFixedAlready = 0; |
---|
| 204 | for (int i = 0; i < numberIntegers; i++) { |
---|
| 205 | int iColumn = integerVariable[i]; |
---|
| 206 | if (upper[iColumn] > lower[iColumn]) { |
---|
| 207 | numberFree++; |
---|
| 208 | double value = solution[iColumn]; |
---|
| 209 | if (value - lower[iColumn] <= integerTolerance) { |
---|
| 210 | candidate[cnt].var = iColumn; |
---|
| 211 | candidate[cnt++].pseudoRedCost = CoinMax(1.0e-2 * reducedCost[iColumn], |
---|
| 212 | downArray_[i]) * random[i]; |
---|
| 213 | } else if (upper[iColumn] - value <= integerTolerance) { |
---|
| 214 | candidate[cnt].var = iColumn; |
---|
| 215 | candidate[cnt++].pseudoRedCost = CoinMax(-1.0e-2 * reducedCost[iColumn], |
---|
| 216 | downArray_[i]) * random[i]; |
---|
| 217 | } |
---|
| 218 | } else { |
---|
| 219 | numberFixedAlready++; |
---|
| 220 | } |
---|
[1271] | 221 | } |
---|
| 222 | #ifdef CLP_INVESTIGATE |
---|
[1286] | 223 | printf("cutoff %g obj %g - %d free, %d fixed\n", |
---|
| 224 | model_->getCutoff(), solver->getObjValue(), numberFree, |
---|
| 225 | numberFixedAlready); |
---|
[1271] | 226 | #endif |
---|
[1286] | 227 | return cnt; |
---|
| 228 | //return CbcHeuristicDive::fixOtherVariables(solver, solution, |
---|
| 229 | // candidate, random); |
---|
[1271] | 230 | } |
---|
[1432] | 231 | |
---|