Changeset 1286 for branches/sandbox/Cbc/src/CbcHeuristicDiveGuided.cpp
 Timestamp:
 Nov 9, 2009 6:33:07 PM (10 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

branches/sandbox/Cbc/src/CbcHeuristicDiveGuided.cpp
r1271 r1286 11 11 12 12 // Default Constructor 13 CbcHeuristicDiveGuided::CbcHeuristicDiveGuided() 14 :CbcHeuristicDive()13 CbcHeuristicDiveGuided::CbcHeuristicDiveGuided() 14 : CbcHeuristicDive() 15 15 { 16 16 } … … 18 18 // Constructor from model 19 19 CbcHeuristicDiveGuided::CbcHeuristicDiveGuided(CbcModel & model) 20 :CbcHeuristicDive(model)20 : CbcHeuristicDive(model) 21 21 { 22 22 } 23 23 24 // Destructor 24 // Destructor 25 25 CbcHeuristicDiveGuided::~CbcHeuristicDiveGuided () 26 26 { … … 31 31 CbcHeuristicDiveGuided::clone() const 32 32 { 33 return new CbcHeuristicDiveGuided(*this);33 return new CbcHeuristicDiveGuided(*this); 34 34 } 35 35 36 36 // Create C++ lines to get to current state 37 void 38 CbcHeuristicDiveGuided::generateCpp( FILE * fp) 37 void 38 CbcHeuristicDiveGuided::generateCpp( FILE * fp) 39 39 { 40 CbcHeuristicDiveGuided other;41 fprintf(fp,"0#include \"CbcHeuristicDiveGuided.hpp\"\n");42 fprintf(fp,"3 CbcHeuristicDiveGuided heuristicDiveGuided(*cbcModel);\n");43 CbcHeuristic::generateCpp(fp,"heuristicDiveGuided");44 fprintf(fp,"3 cbcModel>addHeuristic(&heuristicDiveGuided);\n");40 CbcHeuristicDiveGuided other; 41 fprintf(fp, "0#include \"CbcHeuristicDiveGuided.hpp\"\n"); 42 fprintf(fp, "3 CbcHeuristicDiveGuided heuristicDiveGuided(*cbcModel);\n"); 43 CbcHeuristic::generateCpp(fp, "heuristicDiveGuided"); 44 fprintf(fp, "3 cbcModel>addHeuristic(&heuristicDiveGuided);\n"); 45 45 } 46 46 47 // Copy constructor 47 // Copy constructor 48 48 CbcHeuristicDiveGuided::CbcHeuristicDiveGuided(const CbcHeuristicDiveGuided & rhs) 49 :50 CbcHeuristicDive(rhs)49 : 50 CbcHeuristicDive(rhs) 51 51 { 52 52 } 53 53 54 // Assignment operator 55 CbcHeuristicDiveGuided & 56 CbcHeuristicDiveGuided::operator=( const CbcHeuristicDiveGuided & rhs)54 // Assignment operator 55 CbcHeuristicDiveGuided & 56 CbcHeuristicDiveGuided::operator=( const CbcHeuristicDiveGuided & rhs) 57 57 { 58 if (this!=&rhs) {59 CbcHeuristicDive::operator=(rhs);60 }61 return *this;58 if (this != &rhs) { 59 CbcHeuristicDive::operator=(rhs); 60 } 61 return *this; 62 62 } 63 63 … … 65 65 CbcHeuristicDiveGuided::canHeuristicRun() 66 66 { 67 double* bestIntegerSolution = model_>bestSolution();68 if(bestIntegerSolution == NULL)69 return false; // no integer solution available. Switch off heuristic67 double* bestIntegerSolution = model_>bestSolution(); 68 if (bestIntegerSolution == NULL) 69 return false; // no integer solution available. Switch off heuristic 70 70 71 return CbcHeuristicDive::canHeuristicRun();71 return CbcHeuristicDive::canHeuristicRun(); 72 72 } 73 73 74 74 bool 75 75 CbcHeuristicDiveGuided::selectVariableToBranch(OsiSolverInterface* solver, 76 77 78 76 const double* newSolution, 77 int& bestColumn, 78 int& bestRound) 79 79 { 80 double* bestIntegerSolution = model_>bestSolution();80 double* bestIntegerSolution = model_>bestSolution(); 81 81 82 int numberIntegers = model_>numberIntegers();83 const int * integerVariable = model_>integerVariable();84 double integerTolerance = model_>getDblParam(CbcModel::CbcIntegerTolerance);82 int numberIntegers = model_>numberIntegers(); 83 const int * integerVariable = model_>integerVariable(); 84 double integerTolerance = model_>getDblParam(CbcModel::CbcIntegerTolerance); 85 85 86 bestColumn = 1;87 bestRound = 1; // 1 rounds down, +1 rounds up88 double bestFraction = DBL_MAX;89 bool allTriviallyRoundableSoFar = true;90 for (int i=0; i<numberIntegers; i++) {91 int iColumn = integerVariable[i];92 double value=newSolution[iColumn];93 double fraction=valuefloor(value);94 int round = 0;95 if (fabs(floor(value+0.5)value)>integerTolerance) {96 if(allTriviallyRoundableSoFar(downLocks_[i]>0&&upLocks_[i]>0)) {86 bestColumn = 1; 87 bestRound = 1; // 1 rounds down, +1 rounds up 88 double bestFraction = DBL_MAX; 89 bool allTriviallyRoundableSoFar = true; 90 for (int i = 0; i < numberIntegers; i++) { 91 int iColumn = integerVariable[i]; 92 double value = newSolution[iColumn]; 93 double fraction = value  floor(value); 94 int round = 0; 95 if (fabs(floor(value + 0.5)  value) > integerTolerance) { 96 if (allTriviallyRoundableSoFar  (downLocks_[i] > 0 && upLocks_[i] > 0)) { 97 97 98 if(allTriviallyRoundableSoFar&&downLocks_[i]>0&&upLocks_[i]>0) {99 100 101 98 if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) { 99 allTriviallyRoundableSoFar = false; 100 bestFraction = DBL_MAX; 101 } 102 102 103 if(value >= bestIntegerSolution[iColumn]) 104 round = 1; 105 else { 106 round = 1; 107 fraction = 1.0  fraction; 108 } 109 110 // if variable is not binary, penalize it 111 if(!solver>isBinary(iColumn)) 112 fraction *= 1000.0; 103 if (value >= bestIntegerSolution[iColumn]) 104 round = 1; 105 else { 106 round = 1; 107 fraction = 1.0  fraction; 108 } 113 109 114 if(fraction < bestFraction) { 115 bestColumn = iColumn; 116 bestFraction = fraction; 117 bestRound = round; 118 } 119 } 110 // if variable is not binary, penalize it 111 if (!solver>isBinary(iColumn)) 112 fraction *= 1000.0; 113 114 if (fraction < bestFraction) { 115 bestColumn = iColumn; 116 bestFraction = fraction; 117 bestRound = round; 118 } 119 } 120 } 120 121 } 121 } 122 return allTriviallyRoundableSoFar; 122 return allTriviallyRoundableSoFar; 123 123 }
Note: See TracChangeset
for help on using the changeset viewer.