Changeset 854 for trunk/Cbc/src/CbcHeuristic.cpp
- Timestamp:
- Jan 11, 2008 5:05:20 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Cbc/src/CbcHeuristic.cpp
r838 r854 388 388 model.setParentModel(*model_); 389 389 model.setOriginalColumns(process.originalColumns()); 390 model.branchAndBound(); 390 if (model.getNumCols()) { 391 setCutAndHeuristicOptions(model); 392 model.branchAndBound(); 393 } else { 394 // empty model 395 model.setMinimizationObjValue(model.solver()->getObjSense()*model.solver()->getObjValue()); 396 } 391 397 if (logLevel>1) 392 398 model_->messageHandler()->message(CBC_END_SUB,model_->messages()) … … 395 401 if (model.getMinimizationObjValue()<CoinMin(cutoff,1.0e30)) { 396 402 // solution 397 returnCode=model.isProvenOptimal() ? 3 : 1; 403 if (model.getNumCols()) 404 returnCode=model.isProvenOptimal() ? 3 : 1; 405 else 406 returnCode=3; 398 407 // post process 399 408 #ifdef COIN_HAS_CLP … … 1376 1385 1377 1386 // Default Constructor 1387 CbcHeuristicPartial::CbcHeuristicPartial() 1388 :CbcHeuristic() 1389 { 1390 fixPriority_ = 10000; 1391 } 1392 1393 // Constructor from model 1394 CbcHeuristicPartial::CbcHeuristicPartial(CbcModel & model, int fixPriority, int numberNodes) 1395 :CbcHeuristic(model) 1396 { 1397 fixPriority_ = fixPriority; 1398 setNumberNodes(numberNodes); 1399 validate(); 1400 } 1401 1402 // Destructor 1403 CbcHeuristicPartial::~CbcHeuristicPartial () 1404 { 1405 } 1406 1407 // Clone 1408 CbcHeuristic * 1409 CbcHeuristicPartial::clone() const 1410 { 1411 return new CbcHeuristicPartial(*this); 1412 } 1413 // Create C++ lines to get to current state 1414 void 1415 CbcHeuristicPartial::generateCpp( FILE * fp) 1416 { 1417 CbcHeuristicPartial other; 1418 fprintf(fp,"0#include \"CbcHeuristic.hpp\"\n"); 1419 fprintf(fp,"3 CbcHeuristicPartial partial(*cbcModel);\n"); 1420 CbcHeuristic::generateCpp(fp,"partial"); 1421 if (fixPriority_!=other.fixPriority_) 1422 fprintf(fp,"3 partial.setFixPriority(%d);\n",fixPriority_); 1423 else 1424 fprintf(fp,"4 partial.setFixPriority(%d);\n",fixPriority_); 1425 fprintf(fp,"3 cbcModel->addHeuristic(&partial);\n"); 1426 } 1427 //#define NEW_PARTIAL 1428 // Copy constructor 1429 CbcHeuristicPartial::CbcHeuristicPartial(const CbcHeuristicPartial & rhs) 1430 : 1431 CbcHeuristic(rhs), 1432 fixPriority_(rhs.fixPriority_) 1433 { 1434 } 1435 1436 // Assignment operator 1437 CbcHeuristicPartial & 1438 CbcHeuristicPartial::operator=( const CbcHeuristicPartial& rhs) 1439 { 1440 if (this!=&rhs) { 1441 CbcHeuristic::operator=(rhs); 1442 fixPriority_ = rhs.fixPriority_; 1443 } 1444 return *this; 1445 } 1446 1447 // Resets stuff if model changes 1448 void 1449 CbcHeuristicPartial::resetModel(CbcModel * model) 1450 { 1451 model_=model; 1452 // Get a copy of original matrix (and by row for partial); 1453 assert(model_->solver()); 1454 validate(); 1455 } 1456 // See if partial will give solution 1457 // Sets value of solution 1458 // Assumes rhs for original matrix still okay 1459 // At present only works with integers 1460 // Fix values if asked for 1461 // Returns 1 if solution, 0 if not 1462 int 1463 CbcHeuristicPartial::solution(double & solutionValue, 1464 double * betterSolution) 1465 { 1466 // Return if already done 1467 if (fixPriority_<0) 1468 return 0; // switched off 1469 const double * hotstartSolution = model_->hotstartSolution(); 1470 const int * hotstartPriorities = model_->hotstartPriorities(); 1471 if (!hotstartSolution) 1472 return 0; 1473 OsiSolverInterface * solver = model_->solver(); 1474 1475 int numberIntegers = model_->numberIntegers(); 1476 const int * integerVariable = model_->integerVariable(); 1477 1478 OsiSolverInterface * newSolver = model_->continuousSolver()->clone(); 1479 const double * colLower = newSolver->getColLower(); 1480 const double * colUpper = newSolver->getColUpper(); 1481 1482 double primalTolerance; 1483 solver->getDblParam(OsiPrimalTolerance,primalTolerance); 1484 1485 int i; 1486 int numberFixed=0; 1487 int returnCode=0; 1488 1489 for (i=0;i<numberIntegers;i++) { 1490 int iColumn=integerVariable[i]; 1491 if (abs(hotstartPriorities[iColumn])<=fixPriority_) { 1492 double value = hotstartSolution[iColumn]; 1493 double lower = colLower[iColumn]; 1494 double upper = colUpper[iColumn]; 1495 value = CoinMax(value,lower); 1496 value = CoinMin(value,upper); 1497 if (fabs(value-floor(value+0.5))<1.0e-8) { 1498 value = floor(value+0.5); 1499 newSolver->setColLower(iColumn,value); 1500 newSolver->setColUpper(iColumn,value); 1501 numberFixed++; 1502 } 1503 } 1504 } 1505 if (numberFixed>numberIntegers/5-100000000) { 1506 #ifdef COIN_DEVELOP 1507 printf("%d integers fixed\n",numberFixed); 1508 #endif 1509 returnCode = smallBranchAndBound(newSolver,numberNodes_,betterSolution,solutionValue, 1510 model_->getCutoff(),"CbcHeuristicPartial"); 1511 if (returnCode<0) 1512 returnCode=0; // returned on size 1513 //printf("return code %d",returnCode); 1514 if ((returnCode&2)!=0) { 1515 // could add cut 1516 returnCode &= ~2; 1517 //printf("could add cut with %d elements (if all 0-1)\n",nFix); 1518 } else { 1519 //printf("\n"); 1520 } 1521 } 1522 fixPriority_=-1; // switch off 1523 1524 delete newSolver; 1525 return returnCode; 1526 } 1527 // update model 1528 void CbcHeuristicPartial::setModel(CbcModel * model) 1529 { 1530 model_ = model; 1531 assert(model_->solver()); 1532 // make sure model okay for heuristic 1533 validate(); 1534 } 1535 // Validate model i.e. sets when_ to 0 if necessary (may be NULL) 1536 void 1537 CbcHeuristicPartial::validate() 1538 { 1539 if (model_&&when()<10) { 1540 if (model_->numberIntegers()!= 1541 model_->numberObjects()) 1542 setWhen(0); 1543 } 1544 } 1545 1546 // Default Constructor 1378 1547 CbcSerendipity::CbcSerendipity() 1379 1548 :CbcHeuristic()
Note: See TracChangeset
for help on using the changeset viewer.