Changeset 1092
 Timestamp:
 Oct 24, 2008 4:43:52 PM (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/src/CbcHeuristicRandRound.cpp
r1077 r1092 31 31 32 32 // Constructor with model  assumed before cuts 33 34 33 CbcHeuristicRandRound::CbcHeuristicRandRound(CbcModel & model) 35 34 :CbcHeuristic(model) 36 35 { 36 model_=&model; 37 setWhen(1); 37 38 } 38 39 … … 48 49 return new CbcHeuristicRandRound(*this); 49 50 } 51 50 52 // Create C++ lines to get to current state 51 53 void … … 82 84 //CbcHeuristic::resetModel(model); 83 85 } 86 84 87 /* 85 88 Randomized Rounding Heuristic 86 Returns 1 if solution, 0 if not */ 89 Returns 1 if solution, 0 if not 90 */ 87 91 int 88 92 CbcHeuristicRandRound::solution(double & solutionValue, 89 93 double * betterSolution) 90 94 { 91 std::cout << "Lucky you! You're in the Randomized Rounding Heuristic" << std::endl; 92 // The struct should be moved to member data 93 95 // rlh: Todo: Memory Cleanup 96 97 setWhen(1); // setWhen(1) didn't have the effect I expected (e.g., run once). 98 99 // Run only once. 100 // 101 // See if at root node 102 bool atRoot = model_>getNodeCount()==0; 103 int passNumber = model_>getCurrentPassNumber(); 104 // Just do once 105 if (!atRootpassNumber!=1) 106 return 0; 107 108 std::cout << "Entering the Randomized Rounding Heuristic" << std::endl; 109 94 110 typedef struct { 95 111 int numberSolutions; … … 101 117 102 118 double start = CoinCpuTime(); 103 numCouldRun_++; // Todo: Ask JJHF what this for. 104 105 OsiClpSolverInterface * clpSolver 106 = dynamic_cast<OsiClpSolverInterface *> (model_>solver()); 119 numCouldRun_++; // 120 // Todo: Ask JJHF what "number of times 121 // the heuristic could run" means. 122 123 OsiSolverInterface * solver= model_>solver()>clone(); 124 OsiClpSolverInterface * clpSolver = dynamic_cast<OsiClpSolverInterface *> (solver); 107 125 assert (clpSolver); 108 126 ClpSimplex * simplex = clpSolver>getModelPtr(); 109 127 110 // Initialize the structure holding the solutions 128 // Initialize the structure holding the solutions for the Simplex iterations 111 129 clpSolution solutions; 112 // Set typeStruct field of ClpTrustedData struct to one.113 // This tells Clp it's "Mahdi!"130 // Set typeStruct field of ClpTrustedData struct to 1 to indicate 131 // desired behavior for RandRound heuristic (which is what?) 114 132 ClpTrustedData trustedSolutions; 115 133 trustedSolutions.typeStruct = 1; … … 120 138 solutions.solution=NULL; 121 139 solutions.numberUnsatisfied=NULL; 122 simplex>setTrustedUserPointer(&trustedSolutions); // rlh: old was123 // "userPointer" 124 140 simplex>setTrustedUserPointer(&trustedSolutions); 141 142 // Solve from all slack to get some points 125 143 simplex>allSlackBasis(); 144 145 // Calling primal invalidates pointers to some rim vectors, 146 // like...row sense (!) 126 147 simplex>primal(); 127 148 128 // rlh: Mahdi's code 129 //  130 // Get the problem information 131 132 //  Get the number of cols and rows 149 // 1. Okay  so a workaround would be to copy the data I want BEFORE 150 // calling primal. 151 // 2. Another approach is to ask the simplex solvers NOT to mess up my 152 // rims. 153 // 3. See freeCachedResults() for what is getting 154 // deleted. Everything else points into the structure. 155 // ...or use collower and colupper rather than rowsense. 156 // ..store address of where one of these 157 158 // Store the basic problem information 159 // Get the number of columns, rows and rhs vector 133 160 int numCols = clpSolver>getNumCols(); 134 161 int numRows = clpSolver>getNumRows(); 135 162 136 //  Get the right hand side of the rows 137 const double * rhs = clpSolver>getRightHandSide(); 138 139 // Find the integer variables 140 // rlh: consider using columnType 141 // One if not continuous (i.e., bin or gen int) 163 // Find the integer variables (use columnType(?)) 164 // One if not continuous, that is binary or general integer) 142 165 bool * varClassInt = new bool[numCols]; 143 166 for(int i=0; i<numCols; i++) … … 154 177 155 178 // Get the objective coefficients 156 const double *objCoefficients = clpSolver>getObjCoefficients(); 157 double *originalObjCoeff = new double [numCols]; 158 for(int i=0; i<numCols;i++) 159 originalObjCoeff[i] = objCoefficients[i]; 179 double *originalObjCoeff = CoinCopyOfArray(clpSolver>getObjCoefficients(), numCols); 160 180 161 181 // Get the matrix of the problem 162 double ** matrix = new double * [numRows]; 182 // rlh: look at using sparse representation 183 double ** matrix = new double * [numRows]; 163 184 for(int i = 0; i < numRows; i++) 164 185 { … … 200 221 201 222 // Start finding corner points by iteratively doing the following: 202 //  findrandomly tilted objective223 //  contruct a randomly tilted objective 203 224 //  solve 204 225 for(int i=0; i<numRows; i++) … … 212 233 // for row i and column j vary the coefficient "a bit" 213 234 if(randNum == 1) 214 // if the element is zero, then round the coefficient down to 0.1 235 // if the element is zero, then set the new obj 236 // coefficient to 0.1 (i.e., round up) 215 237 if(fabs(matrix[index[i]][j]) < 1e6) 216 238 newObj[j] = 0.1; 217 239 else 218 // if the element is nonzero, then increase it "a bit" 240 // if the element is nonzero, then increase the new obj 241 // coefficient "a bit" 219 242 newObj[j] = matrix[index[i]][j] * 1.1; 220 243 else 221 244 // if randnum is 2, then 222 // if the element is zero, then round the coefficient down223 // to NEGATIVE 0.1 245 // if the element is zero, then set the new obj coeffient 246 // to NEGATIVE 0.1 (i.e., round down) 224 247 if(fabs(matrix[index[i]][j]) < 1e6) 225 248 newObj[j] = 0.1; 226 249 else 227 // if the element is nonzero, then DEcrease it "a bit"250 // if the element is nonzero, then DEcrease the new obj coeffienct "a bit" 228 251 newObj[j] = matrix[index[i]][j] * 0.9; 229 252 } … … 238 261 239 262 // Solve with primal simplex 240 clpSolver>getModelPtr()>primal(1); 263 simplex>primal(1); 264 // rlh+ll: This was the original code. But we already have the 265 // model pointer (it's in simplex). And, calling getModelPtr() 266 // invalidates the cached data in the OsiClpSolverInterface 267 // object, which means our precious rowsens is lost. So let's 268 // not use the line below... 269 /******* clpSolver>getModelPtr()>primal(1); */ 241 270 printf(" %d\n", i); 242 271 } … … 260 289 261 290 bool feasibility = 1; 291 // rlh: use some COIN max instead of 1e30 (?) 262 292 double bestObj = 1e30; 263 293 std::vector< std::vector <double> > feasibles; … … 267 297 int numCornerPoints = numberSolutions; 268 298 269 rhs = clpSolver>getRightHandSide(); 299 const double * rhs = clpSolver>getRightHandSide(); 300 // rlh: row sense hasn't changed. why a fresh copy? 301 // Delete next line. 270 302 rowSense = clpSolver>getRowSense(); 271 303 … … 279 311 if(objValue < bestObj) 280 312 { 313 // check integer feasibility 281 314 feasibility = 1; 282 315 for(int j=0; j<numCols; j++) … … 292 325 } 293 326 } 294 if(feasibility) 327 // check all constraints satisfied 328 if (feasibility) 295 329 { 296 330 for(int irow = 0; irow < numRows; irow++) … … 393 427 394 428 //SOFT ROUNDING 395 // Look at original files for the "how to" on soft rounding 396 397 398 // check the feasibility of the rounded random point429 // Look at original files for the "how to" on soft rounding; 430 // Soft rounding omitted here. 431 432 //Check the feasibility of the rounded random point 399 433 // Check the feasibility 400 434 // Get the rows sense … … 460 494 betterSolution[k] = feasibles[numFeasibles1][k]; 461 495 } 462 std::cout << " See you soon! You're leaving the Randomized Rounding Heuristic" << std::endl;496 std::cout << "Leaving the Randomized Rounding Heuristic" << std::endl; 463 497 return 1; 464 498
Note: See TracChangeset
for help on using the changeset viewer.