Changeset 2280 for trunk/Cbc/src/CbcHeuristicFPump.cpp
 Timestamp:
 Jun 14, 2016 10:39:54 AM (6 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/Cbc/src/CbcHeuristicFPump.cpp
r2146 r2280 216 216 // Returns 1 if solution, 0 if not 217 217 int 218 CbcHeuristicFPump::solution (double & solutionValue,218 CbcHeuristicFPump::solutionInternal(double & solutionValue, 219 219 double * betterSolution) 220 220 { … … 286 286 double cutoff; 287 287 model_>solver()>getDblParam(OsiDualObjectiveLimit, cutoff); 288 double realCutoff=cutoff; 289 bool secondMajorPass=false; 288 290 double direction = model_>solver()>getObjSense(); 289 291 cutoff *= direction; … … 336 338 assert(integerObject  integerObject2); 337 339 #endif 340 #ifdef COIN_HAS_CLP 341 if (!isHeuristicInteger(model_>solver(),iColumn)) 342 continue; 343 #endif 338 344 double value = initialSolution[iColumn]; 339 345 double nearest = floor(value + 0.5); … … 370 376 for (i = 0; i < numberIntegers; i++) { 371 377 int iColumn = integerVariableOrig[i]; 378 #ifdef COIN_HAS_CLP 379 if (!isHeuristicInteger(model_>solver(),iColumn)) 380 continue; 381 #endif 372 382 if (upper[iColumn]  lower[iColumn] < 1.000001) 373 383 integerVariable[j++] = iColumn; … … 476 486 double artificialFactor = 0.00001; 477 487 // also try rounding! 478 double * roundingSolution = new double[ numberColumns];479 double roundingObjective = solutionValue;488 double * roundingSolution = new double[2*numberColumns]; 489 double roundingObjective = realCutoff; 480 490 CbcRounding roundingHeuristic(*model_); 481 491 int dualPass = 0; … … 807 817 randomNumberGenerator_.setSeed(987654321); 808 818 } 819 #ifdef COIN_HAS_CLP 820 { 821 OsiClpSolverInterface * clpSolver 822 = dynamic_cast<OsiClpSolverInterface *> (clonedSolver); 823 //printf("real cutoff %g fake %g  second pass %c\n",realCutoff,cutoff, 824 // secondMajorPass ? 'Y' : 'N'); 825 if (clpSolver && (((accumulate_&16) != 0)  826 ((accumulate_&8)!=0 && secondMajorPass))) { 827 // try rounding heuristic 828 OsiSolverInterface * saveSolver = model_>swapSolver(clonedSolver); 829 ClpSimplex * simplex = clpSolver>getModelPtr(); 830 double * solverSolution = simplex>primalColumnSolution(); 831 memcpy(solverSolution,solution,numberColumns*sizeof(double)); 832 // Compute using dot product 833 double newSolutionValue = saveOffset; 834 for ( i = 0 ; i < numberColumns ; i++ ) 835 newSolutionValue += saveObjective[i] * solution[i]; 836 simplex>setObjectiveValue(newSolutionValue); 837 clpSolver>setObjective(saveObjective); 838 CbcRounding heuristic1(*model_); 839 heuristic1.setHeuristicName("rounding in feaspump!"); 840 heuristic1.setWhen(1); 841 newSolutionValue=realCutoff; 842 int ifSolR = heuristic1.solution(newSolutionValue, 843 roundingSolution+numberColumns); 844 model_>swapSolver(saveSolver); 845 if (ifSolR&&newSolutionValue<roundingObjective) { 846 roundingObjective=newSolutionValue; 847 //printf("rounding obj of %g?\n", roundingObjective); 848 memcpy(roundingSolution,roundingSolution+numberColumns, 849 numberColumns*sizeof(double)); 850 } 851 } 852 } 853 #endif 809 854 returnCode = rounds(solver, newSolution,/*saveObjective,*/ 810 855 numberIntegers, integerVariable, … … 817 862 } 818 863 numberPasses++; 864 if (roundingObjective<realCutoff) { 865 if (returnCode) { 866 newSolutionValue = saveOffset; 867 for ( i = 0 ; i < numberColumns ; i++ ) 868 newSolutionValue += saveObjective[i] * newSolution[i]; 869 } else { 870 newSolutionValue = COIN_DBL_MAX; 871 } 872 if (roundingObjective<newSolutionValue&&false) { 873 returnCode=1; 874 memcpy(newSolution,roundingSolution, 875 numberColumns*sizeof(double)); 876 } 877 } 819 878 if (returnCode) { 820 879 // SOLUTION IS INTEGER … … 841 900 for (i = 0; i < numberIntegersOrig; i++) { 842 901 int iColumn = integerVariableOrig[i]; 902 #ifdef COIN_HAS_CLP 903 if (!isHeuristicInteger(solver,iColumn)) 904 continue; 905 #endif 843 906 double value = floor(newSolution[iColumn] + 0.5); 844 907 if (solver>isBinary(iColumn)) { … … 932 995 } 933 996 newSolutionValue += objective[iColumn] * newSolution[iColumn]; 934 if ( solver>isInteger(iColumn)) {997 if (isHeuristicInteger(solver,iColumn)) { 935 998 double intValue = floor(value+0.5); 936 999 if (fabs(valueintValue)>largestAway) { … … 993 1056 model_>setBestSolution(saveOldSolution, numberColumns, saveObjectiveValue); 994 1057 delete [] saveOldSolution; 1058 realCutoff = model_>getMinimizationObjValue(); 995 1059 } 996 1060 if (action == 0  model_>maximumSecondsReached()) { … … 1156 1220 continue; 1157 1221 // deal with fixed variables (i.e., upper=lower) 1158 if (fabs(lower[iColumn]  upper[iColumn]) < primalTolerance  ! solver>isInteger(iColumn)) {1222 if (fabs(lower[iColumn]  upper[iColumn]) < primalTolerance  !isHeuristicInteger(solver,iColumn)) { 1159 1223 //if (lower[iColumn] > 1.  primalTolerance) solver>setObjCoeff(iColumn,costValue); 1160 1224 //else solver>setObjCoeff(iColumn,costValue); … … 1318 1382 for (i = 0; i < numberIntegersOrig; i++) { 1319 1383 int iColumn = integerVariableOrig[i]; 1384 #ifdef COIN_HAS_CLP 1385 if (!isHeuristicInteger(solver,iColumn)) 1386 continue; 1387 #endif 1320 1388 #ifdef RAND_RAND 1321 1389 if (nChanged > numberChanged) … … 1341 1409 for (i = 0; i < numberIntegersOrig; i++) { 1342 1410 int iColumn = integerVariableOrig[i]; 1411 #ifdef COIN_HAS_CLP 1412 if (!isHeuristicInteger(solver,iColumn)) 1413 continue; 1414 #endif 1343 1415 if (objective[iColumn] > 0.0) { 1344 1416 if (basis>getStructStatus(iColumn) == … … 1395 1467 const double * newSolution = solver>getColSolution(); 1396 1468 for (int iColumn = 0 ; iColumn < numberColumns ; iColumn++ ) { 1397 if ( solver>isInteger(iColumn)) {1469 if (isHeuristicInteger(solver,iColumn)) { 1398 1470 double value = newSolution[iColumn]; 1399 1471 double nearest = floor(value + 0.5); … … 1459 1531 for ( i = 0 ; i < numberColumns ; i++ ) { 1460 1532 double value = newSolution[i]; 1461 if ( solver>isInteger(i)) {1533 if (isHeuristicInteger(solver,i)) { 1462 1534 double nearest = floor(value + 0.5); 1463 1535 if (fabs(value  nearest) > 1.0e6) { … … 1672 1744 const double * newSolution = solver>getColSolution(); 1673 1745 for ( i = 0 ; i < numberColumns ; i++ ) { 1674 if ( solver>isInteger(i)) {1746 if (isHeuristicInteger(solver,i)) { 1675 1747 double value = newSolution[i]; 1676 1748 double nearest = floor(value + 0.5); … … 1740 1812 for (i = 0; i < numberIntegersOrig; i++) { 1741 1813 int iColumn = integerVariableOrig[i]; 1814 #ifdef COIN_HAS_CLP 1815 if (!isHeuristicInteger(solver,iColumn)) 1816 continue; 1817 #endif 1742 1818 if (objective[iColumn] > 0.0) 1743 1819 closestSolution[i] = 0; … … 1857 1933 solutionValue = roundingObjective; 1858 1934 newSolutionValue = solutionValue; 1935 realCutoff=solutionValue1.0e5; 1859 1936 memcpy(betterSolution, roundingSolution, numberColumns*sizeof(double)); 1860 1937 solutionFound = true; … … 1903 1980 for (i = 0; i < numberIntegersOrig; i++) { 1904 1981 int iColumn = integerVariableOrig[i]; 1982 #ifdef COIN_HAS_CLP 1983 if (!isHeuristicInteger(newSolver,iColumn)) 1984 continue; 1985 #endif 1905 1986 //const OsiObject * object = model_>object(i); 1906 1987 //double originalLower; … … 1934 2015 if (fixContinuous) { 1935 2016 for (int iColumn = 0; iColumn < numberColumns; iColumn++) { 1936 if (! newSolver>isInteger(iColumn) && usedColumn[iColumn] <= allowedPass) {2017 if (!isHeuristicInteger(newSolver,iColumn) && usedColumn[iColumn] <= allowedPass) { 1937 2018 double value = lastSolution[iColumn]; 1938 2019 if (value < colLower[iColumn] + 1.0e8) { … … 2042 2123 const double * obj = newSolver>getObjCoefficients(); 2043 2124 for (int i = 0 ; i < numberColumns ; i++ ) { 2044 if ( newSolver>isInteger(i)) {2125 if (isHeuristicInteger(newSolver,i)) { 2045 2126 double value = newSolution[i]; 2046 2127 double nearest = floor(value + 0.5); … … 2067 2148 for (int iColumn = 0; iColumn < numberColumns; iColumn++) { 2068 2149 double value = newSolution[iColumn]; 2069 if ( newSolver>isInteger(iColumn)) {2150 if (isHeuristicInteger(newSolver,iColumn)) { 2070 2151 value = floor(newSolution[iColumn] + 0.5); 2071 2152 newSolver>setColLower(iColumn, value); … … 2150 2231 const double * solution = newSolver>getColSolution(); 2151 2232 for (int i = 0 ; i < numberColumns ; i++ ) { 2152 if ( newSolver>isInteger(i)) {2233 if (isHeuristicInteger(newSolver,i)) { 2153 2234 double value = solution[i]; 2154 2235 double nearest = floor(value + 0.5); … … 2192 2273 const double * upper = model_>solver()>getColUpper(); 2193 2274 for (int iColumn = 0; iColumn < numberColumns; iColumn++) { 2194 if ( newSolver>isInteger(iColumn)) {2275 if (isHeuristicInteger(newSolver,iColumn)) { 2195 2276 double value = floor(newSolution[iColumn] + 0.5); 2196 2277 newSolver>setColLower(iColumn, value); … … 2253 2334 if (solutionFound) finalReturnCode = 1; 2254 2335 cutoff = CoinMin(cutoff, solutionValue  model_>getCutoffIncrement()); 2336 realCutoff = cutoff; 2255 2337 if (numberTries >= maximumRetries_  !solutionFound  exitAll  cutoff < continuousObjectiveValue + 1.0e7) { 2256 2338 break; … … 2272 2354 break; 2273 2355 sprintf(pumpPrint, "Round again with cutoff of %g", cutoff); 2356 secondMajorPass=true; 2274 2357 model_>messageHandler()>message(CBC_FPUMP1, model_>messages()) 2275 2358 << pumpPrint … … 2301 2384 for (i = 0; i < numberIntegersOrig; i++) { 2302 2385 int iColumn = integerVariableOrig[i]; 2386 #ifdef COIN_HAS_CLP 2387 if (!isHeuristicInteger(newSolver,iColumn)) 2388 continue; 2389 #endif 2303 2390 int direction = closestSolution[i]; 2304 2391 closestSolution[i] = iColumn; … … 2383 2470 2384 2471 /**************************END MAIN PROCEDURE ***********************************/ 2472 /* If general integers then adds variables to turn into binaries round 2473 solution 2474 */ 2475 int 2476 CbcHeuristicFPump::solution(double & objectiveValue, double * newSolution) 2477 { 2478 double * newSolution2=NULL; 2479 double objective2=COIN_DBL_MAX; 2480 int returnCode2=0; 2481 int oddGeneral = (accumulate_&(3264128))>>5; 2482 if (oddGeneral) { 2483 int maxAround=2; 2484 bool fixSatisfied=false; 2485 // clone solver and modify 2486 OsiSolverInterface * solver = cloneBut(2); // wasmodel_>solver()>clone(); 2487 double cutoff; 2488 model_>solver()>getDblParam(OsiDualObjectiveLimit, cutoff); 2489 int numberColumns = model_>getNumCols(); 2490 int numberIntegers = model_>numberIntegers(); 2491 const int * integerVariableOrig = model_>integerVariable(); 2492 int general = 0; 2493 int nAdd=0; 2494 const double * lower = solver>getColLower(); 2495 const double * upper = solver>getColUpper(); 2496 const double * initialSolution = solver>getColSolution(); 2497 // we may be being clever so make sure solver lines up wuth model 2498 for (int i = 0; i < numberColumns; i++) 2499 solver>setContinuous(i); 2500 for (int i = 0; i < numberIntegers; i++) { 2501 int iColumn = integerVariableOrig[i]; 2502 #ifdef COIN_HAS_CLP 2503 if (!isHeuristicInteger(solver,iColumn)) 2504 continue; 2505 #endif 2506 double value = initialSolution[iColumn]; 2507 double nearest = floor(value + 0.5); 2508 if (upper[iColumn]  lower[iColumn] > 1.000001) { 2509 if (fabs(valuenearest)<1.0e6&&fixSatisfied) { 2510 solver>setColLower(iColumn,nearest); 2511 solver>setColUpper(iColumn,nearest); 2512 } else { 2513 general++; 2514 int up=static_cast<int>(upper[iColumn]); 2515 int lo=static_cast<int>(lower[iColumn]); 2516 int near=static_cast<int>(nearest); 2517 up = CoinMin(up,near+maxAround); 2518 lo = CoinMax(lo,nearmaxAround); 2519 solver>setColLower(iColumn,lo); 2520 solver>setColUpper(iColumn,up); 2521 int n=uplo; 2522 // 1  1, 2,3  2, 4567  3 2523 while (n) { 2524 nAdd++; 2525 n = n>>1; 2526 } 2527 } 2528 } else { 2529 solver>setInteger(iColumn); 2530 } 2531 } 2532 if (!general) { 2533 delete solver; 2534 } 2535 if (general) { 2536 CbcModel * saveModel = model_; 2537 int * addStart = new int[nAdd+1]; 2538 memset(addStart,0,(nAdd+1)*sizeof(int)); 2539 int * addIndex = new int[general+nAdd]; 2540 double * addElement = new double[general+nAdd]; 2541 double * addLower = new double[nAdd]; 2542 double * addUpper = new double[nAdd]; 2543 for (int i=0;i<nAdd;i++) { 2544 addLower[i]=0.0; 2545 addUpper[i]=1.0; 2546 } 2547 solver>addCols(nAdd, addStart, NULL, NULL, addLower, addUpper, NULL); 2548 lower = solver>getColLower(); 2549 upper = solver>getColUpper(); 2550 // now rows 2551 nAdd = 0; 2552 int nEl = 0; 2553 int nAddRow = 0; 2554 for (int i = 0; i < numberIntegers; i++) { 2555 int iColumn = integerVariableOrig[i]; 2556 #ifdef COIN_HAS_CLP 2557 if (!isHeuristicInteger(solver,iColumn)) 2558 continue; 2559 #endif 2560 if (upper[iColumn]  lower[iColumn] > 1.000001) { 2561 int up=static_cast<int>(upper[iColumn]); 2562 int lo=static_cast<int>(lower[iColumn]); 2563 addLower[nAddRow] = lo; 2564 addUpper[nAddRow] = lo; 2565 addIndex[nEl] = iColumn; 2566 addElement[nEl++] = 1.0; 2567 int n=uplo; 2568 // 1  1, 2,3  2, 4567  3 2569 int el=1; 2570 while (n) { 2571 addIndex[nEl] = numberColumns+nAdd; 2572 addElement[nEl++] = el; 2573 nAdd++; 2574 n = n>>1; 2575 el = el<<1; 2576 } 2577 nAddRow++; 2578 addStart[nAddRow]=nEl; 2579 } 2580 } 2581 for (int i=0;i<nAdd;i++) 2582 solver>setInteger(i+numberColumns); 2583 solver>addRows(nAddRow, addStart, addIndex, addElement, addLower, addUpper); 2584 delete [] addStart; 2585 delete [] addIndex; 2586 delete [] addElement; 2587 delete [] addLower; 2588 delete [] addUpper; 2589 solver>resolve(); 2590 solver>writeMps("test"); 2591 // new CbcModel 2592 model_=new CbcModel(*solver); 2593 model_>findIntegers(true); 2594 // set cutoff 2595 solver>setDblParam(OsiDualObjectiveLimit, cutoff); 2596 model_>setCutoff(cutoff); 2597 newSolution2 = new double [numberColumns+nAdd]; 2598 objective2=objectiveValue; 2599 returnCode2=solutionInternal(objective2,newSolution2); 2600 delete solver; 2601 delete model_; 2602 model_=saveModel; 2603 } 2604 } 2605 int returnCode=solutionInternal(objectiveValue,newSolution); 2606 if (returnCode2&&false) { 2607 int numberColumns=model_>getNumCols(); 2608 memcpy(newSolution,newSolution2,numberColumns*sizeof(double)); 2609 } 2610 delete [] newSolution2; 2611 return returnCode; 2612 } 2385 2613 2386 2614 // update model … … 2433 2661 for (CoinBigIndex k = rowStart[i]; k < rowStart[i] + rowLength[i]; k++) { 2434 2662 int iColumn = column[k]; 2435 if (elementByRow[k] != 1.0  ! solver>isInteger(iColumn)) {2663 if (elementByRow[k] != 1.0  !isHeuristicInteger(solver,iColumn)) { 2436 2664 cover = false; 2437 2665 break; … … 2479 2707 for (CoinBigIndex k = rowStart[i]; k < rowStart[i] + rowLength[i]; k++) { 2480 2708 int iColumn = column[k]; 2481 if (elementByRow[k] != 1.0  ! solver>isInteger(iColumn)) {2709 if (elementByRow[k] != 1.0  !isHeuristicInteger(solver,iColumn)) { 2482 2710 cover = false; 2483 2711 break; … … 2700 2928 // First improve by moving continuous ones 2701 2929 for (int iColumn = 0; iColumn < numberColumns; iColumn++) { 2702 if (! solver>isInteger(iColumn)) {2930 if (!isHeuristicInteger(solver,iColumn)) { 2703 2931 double solValue = solution[iColumn]; 2704 2932 double thetaUp = columnUpper[iColumn]  solValue;
Note: See TracChangeset
for help on using the changeset viewer.