| 1 |
// (C) Copyright International Business Machines Corporation 2007 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, International Business Machines Corporation |
| 7 |
// |
| 8 |
// Date : 09/01/2007 |
| 9 |
|
| 10 |
#include "BonDiver.hpp" |
| 11 |
#include "CoinFinite.hpp" |
| 12 |
#include "CbcModel.hpp" |
| 13 |
#include "CbcConfig.h" |
| 14 |
#include "BonBabSetupBase.hpp" |
| 15 |
//#define DIVE_DEBUG |
| 16 |
namespace Bonmin |
| 17 |
{ |
| 18 |
|
| 19 |
/************************************************************************/ |
| 20 |
/* CbcDiver methods */ |
| 21 |
/************************************************************************/ |
| 22 |
|
| 23 |
/// Default constructor. |
| 24 |
CbcDiver::CbcDiver(): CbcTree(), |
| 25 |
treeCleaning_(false), |
| 26 |
nextOnBranch_(NULL), |
| 27 |
stop_diving_on_cutoff_(false) |
| 28 |
{} |
| 29 |
|
| 30 |
///Copy constructor. |
| 31 |
CbcDiver::CbcDiver(const CbcDiver &rhs):CbcTree(rhs), |
| 32 |
treeCleaning_(rhs.treeCleaning_), |
| 33 |
nextOnBranch_(rhs.nextOnBranch_), |
| 34 |
stop_diving_on_cutoff_(rhs.stop_diving_on_cutoff_) |
| 35 |
{} |
| 36 |
|
| 37 |
/// Assignment operator. |
| 38 |
CbcDiver & |
| 39 |
CbcDiver::operator=(const CbcDiver &rhs) |
| 40 |
{ |
| 41 |
if (this != &rhs) { |
| 42 |
CbcTree::operator=(rhs); |
| 43 |
treeCleaning_ = rhs.treeCleaning_; |
| 44 |
nextOnBranch_ = rhs.nextOnBranch_; |
| 45 |
stop_diving_on_cutoff_ = rhs.stop_diving_on_cutoff_; |
| 46 |
} |
| 47 |
return *this; |
| 48 |
} |
| 49 |
|
| 50 |
/// Destructor. |
| 51 |
CbcDiver::~CbcDiver() |
| 52 |
{} |
| 53 |
|
| 54 |
///copy constructor. |
| 55 |
CbcTree * |
| 56 |
CbcDiver::clone() const |
| 57 |
{ |
| 58 |
return new CbcDiver(*this); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
///Return top node (next node to process.*/ |
| 63 |
CbcNode * |
| 64 |
CbcDiver::top() const |
| 65 |
{ |
| 66 |
#ifdef DIVE_DEBUG |
| 67 |
std::cout<<"CbcDiver::top"<<std::endl; |
| 68 |
#endif |
| 69 |
if (nextOnBranch_ != NULL && !treeCleaning_) { |
| 70 |
return nextOnBranch_; |
| 71 |
} |
| 72 |
else return CbcTree::top(); |
| 73 |
} |
| 74 |
|
| 75 |
/// Add node to the heap. |
| 76 |
void |
| 77 |
CbcDiver::push(CbcNode * x) |
| 78 |
{ |
| 79 |
#ifdef DIVE_DEBUG |
| 80 |
std::cout<<"CbcDiver::push"<<std::endl; |
| 81 |
#endif |
| 82 |
if (treeCleaning_) return CbcTree::push(x); |
| 83 |
|
| 84 |
if (x->branchingObject()->numberBranchesLeft() == x->branchingObject()->numberBranches()) {//Not Backtracking |
| 85 |
assert(nextOnBranch_==NULL);//Should not happen twice in a row |
| 86 |
nextOnBranch_ = x; |
| 87 |
} |
| 88 |
else |
| 89 |
CbcTree::push(x); |
| 90 |
} |
| 91 |
|
| 92 |
/// Remove the top node of the heap. |
| 93 |
void |
| 94 |
CbcDiver::pop() |
| 95 |
{ |
| 96 |
#ifdef DIVE_DEBUG |
| 97 |
std::cout<<"CbcDiver::pop"<<std::endl; |
| 98 |
#endif |
| 99 |
if (nextOnBranch_ != NULL && !treeCleaning_) { |
| 100 |
nextOnBranch_ = NULL; |
| 101 |
} |
| 102 |
else |
| 103 |
CbcTree::pop(); |
| 104 |
} |
| 105 |
/// Remove the best node from the heap and return it |
| 106 |
CbcNode * |
| 107 |
CbcDiver::bestNode(double cutoff) |
| 108 |
{ |
| 109 |
#ifdef DIVE_DEBUG |
| 110 |
std::cout<<"CbcDiver::bestNode"<<std::endl; |
| 111 |
#endif |
| 112 |
if (nextOnBranch_ != NULL && !treeCleaning_) { |
| 113 |
if (nextOnBranch_->objectiveValue() < cutoff) { |
| 114 |
if (stop_diving_on_cutoff_ && nextOnBranch_->guessedObjectiveValue() >= cutoff) { |
| 115 |
//printf("Stopping dive %g %g\n", nextOnBranch_->objectiveValue(), nextOnBranch_->guessedObjectiveValue()); |
| 116 |
CbcTree::push(nextOnBranch_); |
| 117 |
nextOnBranch_ = NULL; |
| 118 |
return CbcTree::bestNode(cutoff); |
| 119 |
} |
| 120 |
//printf("Diving!! %g %g\n", nextOnBranch_->objectiveValue(), nextOnBranch_->guessedObjectiveValue()); |
| 121 |
CbcNode * ret_val = nextOnBranch_; |
| 122 |
nextOnBranch_ = NULL; |
| 123 |
return ret_val; |
| 124 |
} |
| 125 |
assert(true && "We did not think we get here."); |
| 126 |
CbcTree::push(nextOnBranch_);//For safety |
| 127 |
nextOnBranch_ = NULL; |
| 128 |
} |
| 129 |
return CbcTree::bestNode(cutoff); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
/** Test if empty. */ |
| 134 |
bool CbcDiver::empty() |
| 135 |
{ |
| 136 |
return (CbcTree::empty() && (nextOnBranch_ == NULL) ); |
| 137 |
} |
| 138 |
|
| 139 |
/*! \brief Prune the tree using an objective function cutoff |
| 140 |
if nextOnBranch_ exists we push it on the heap and call CbcTree function |
| 141 |
*/ |
| 142 |
void |
| 143 |
CbcDiver::cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective) |
| 144 |
{ |
| 145 |
if (nextOnBranch_ != NULL) |
| 146 |
CbcTree::push(nextOnBranch_); |
| 147 |
nextOnBranch_=NULL; |
| 148 |
treeCleaning_ = true; |
| 149 |
CbcTree::cleanTree(model,cutoff, bestPossibleObjective); |
| 150 |
treeCleaning_ = false; |
| 151 |
} |
| 152 |
|
| 153 |
/// Get best possible objective function in the tree |
| 154 |
double |
| 155 |
CbcDiver::getBestPossibleObjective() |
| 156 |
{ |
| 157 |
double bestPossibleObjective = (nextOnBranch_ != NULL) ? nextOnBranch_->objectiveValue() : 1e100; |
| 158 |
for (unsigned int i = 0 ; i < nodes_.size() ; i++) { |
| 159 |
if (nodes_[i] == NULL) continue; |
| 160 |
const double & obj = nodes_[i]->objectiveValue(); |
| 161 |
if (obj < bestPossibleObjective) { |
| 162 |
bestPossibleObjective = obj; |
| 163 |
} |
| 164 |
} |
| 165 |
return bestPossibleObjective; |
| 166 |
} |
| 167 |
|
| 168 |
void |
| 169 |
CbcDiver::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions) |
| 170 |
{ |
| 171 |
roptions->SetRegisteringCategory("Diving options", RegisteredOptions::UndocumentedCategory); |
| 172 |
roptions->AddStringOption2( |
| 173 |
"stop_diving_on_cutoff", |
| 174 |
"Flag indicating whether we stop diving based on guessed feasible objective and the current cutoff", |
| 175 |
"no", |
| 176 |
"no", "", |
| 177 |
"yes", ""); |
| 178 |
roptions->setOptionExtraInfo("stop_diving_on_cutoff", 63); |
| 179 |
|
| 180 |
} |
| 181 |
|
| 182 |
/// Initialize the method (get options) |
| 183 |
void |
| 184 |
CbcDiver::initialize(BabSetupBase &b) |
| 185 |
{ |
| 186 |
b.options()->GetBoolValue("stop_diving_on_cutoff", stop_diving_on_cutoff_, |
| 187 |
b.prefix()); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/************************************************************************/ |
| 192 |
/* CbcProbedDiver methods */ |
| 193 |
/************************************************************************/ |
| 194 |
|
| 195 |
/// Default constructor. |
| 196 |
CbcProbedDiver::CbcProbedDiver(): CbcTree(), |
| 197 |
treeCleaning_(false), |
| 198 |
nextOnBranch_(NULL), |
| 199 |
candidateChild_(NULL), |
| 200 |
stop_diving_on_cutoff_(false) |
| 201 |
{} |
| 202 |
|
| 203 |
///Copy constructor. |
| 204 |
CbcProbedDiver::CbcProbedDiver(const CbcProbedDiver &rhs):CbcTree(rhs), |
| 205 |
treeCleaning_(rhs.treeCleaning_), |
| 206 |
nextOnBranch_(rhs.nextOnBranch_), |
| 207 |
candidateChild_(rhs.candidateChild_), |
| 208 |
stop_diving_on_cutoff_(rhs.stop_diving_on_cutoff_) |
| 209 |
{} |
| 210 |
|
| 211 |
/// Assignment operator. |
| 212 |
CbcProbedDiver & |
| 213 |
CbcProbedDiver::operator=(const CbcProbedDiver &rhs) |
| 214 |
{ |
| 215 |
if (this != &rhs) { |
| 216 |
CbcTree::operator=(rhs); |
| 217 |
treeCleaning_ = rhs.treeCleaning_; |
| 218 |
nextOnBranch_ = rhs.nextOnBranch_; |
| 219 |
candidateChild_ = rhs.candidateChild_; |
| 220 |
stop_diving_on_cutoff_ = rhs.stop_diving_on_cutoff_; |
| 221 |
} |
| 222 |
return *this; |
| 223 |
} |
| 224 |
|
| 225 |
/// Destructor. |
| 226 |
CbcProbedDiver::~CbcProbedDiver() |
| 227 |
{} |
| 228 |
|
| 229 |
///copy constructor. |
| 230 |
CbcTree * |
| 231 |
CbcProbedDiver::clone() const |
| 232 |
{ |
| 233 |
return new CbcProbedDiver(*this); |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
///Return top node (next node to process.*/ |
| 238 |
CbcNode * |
| 239 |
CbcProbedDiver::top() const |
| 240 |
{ |
| 241 |
#ifdef DIVE_DEBUG |
| 242 |
std::cout<<"CbcProbedDiver::top"<<std::endl; |
| 243 |
#endif |
| 244 |
if (nextOnBranch_ != NULL && !treeCleaning_) { |
| 245 |
return nextOnBranch_; |
| 246 |
} |
| 247 |
else if(candidateChild_ != NULL && !treeCleaning_){ |
| 248 |
return candidateChild_; |
| 249 |
} |
| 250 |
else return CbcTree::top(); |
| 251 |
} |
| 252 |
|
| 253 |
/// Add node to the heap. |
| 254 |
void |
| 255 |
CbcProbedDiver::push(CbcNode * x) |
| 256 |
{ |
| 257 |
#ifdef DIVE_DEBUG |
| 258 |
std::cout<<"CbcProbedDiver::push"<<std::endl; |
| 259 |
#endif |
| 260 |
if (treeCleaning_) return CbcTree::push(x); |
| 261 |
|
| 262 |
if (x->branchingObject()->numberBranchesLeft() == x->branchingObject()->numberBranches()) { |
| 263 |
//Not Backtracking |
| 264 |
if(nextOnBranch_ == NULL && candidateChild_ == NULL){ |
| 265 |
#ifdef DIVE_DEBUG |
| 266 |
printf("Putting parent %i. objective value %g\n", x, x->objectiveValue()); |
| 267 |
#endif |
| 268 |
nextOnBranch_ = x; |
| 269 |
return; |
| 270 |
} |
| 271 |
if(candidateChild_ == NULL){ |
| 272 |
#ifdef DIVE_DEBUG |
| 273 |
printf("Putting first kid %i. objective value %g\n", x, x->objectiveValue()); |
| 274 |
#endif |
| 275 |
candidateChild_ = x; |
| 276 |
return;} |
| 277 |
#ifdef DIVE_DEBUG |
| 278 |
printf("Putting second kid %i. objective value %g\n", x, x->objectiveValue()); |
| 279 |
#endif |
| 280 |
// If we get here we have two nodes follow on the best one and put the other on the tree |
| 281 |
if(comparison_.compareNodes(x,candidateChild_)){// Follow on candidateChild_ |
| 282 |
#ifdef DIVE_DEBUG |
| 283 |
printf("first child %i is found better.\n", x); |
| 284 |
#endif |
| 285 |
nextOnBranch_ = candidateChild_; |
| 286 |
CbcTree::push(x); |
| 287 |
candidateChild_ = NULL; |
| 288 |
return; |
| 289 |
} |
| 290 |
else {// Follow on x |
| 291 |
#ifdef DIVE_DEBUG |
| 292 |
printf("second child %i is found better\n", x); |
| 293 |
#endif |
| 294 |
nextOnBranch_ = x; |
| 295 |
CbcTree::push(candidateChild_); |
| 296 |
candidateChild_ = NULL; |
| 297 |
return; |
| 298 |
} |
| 299 |
} |
| 300 |
else { |
| 301 |
#ifdef DIVE_DEBUG |
| 302 |
printf("Putting back parent %i.\n",x); |
| 303 |
#endif |
| 304 |
if(nextOnBranch_ != NULL){ |
| 305 |
#ifdef DIVE_DEBUG |
| 306 |
printf("Putting nextOnBranch_ in candidateChild_.\n"); |
| 307 |
#endif |
| 308 |
assert(candidateChild_ == NULL); |
| 309 |
candidateChild_ = nextOnBranch_; |
| 310 |
} |
| 311 |
nextOnBranch_ = x; |
| 312 |
return; |
| 313 |
//CbcTree::push(x); |
| 314 |
} |
| 315 |
} |
| 316 |
/// Remove the top node of the heap. |
| 317 |
void |
| 318 |
CbcProbedDiver::pop() |
| 319 |
{ |
| 320 |
#ifdef DIVE_DEBUG |
| 321 |
std::cout<<"CbcProbedDiver::pop"<<std::endl; |
| 322 |
#endif |
| 323 |
if (nextOnBranch_ != NULL && !treeCleaning_) { |
| 324 |
nextOnBranch_ = NULL; |
| 325 |
} |
| 326 |
else if(candidateChild_ != NULL && !treeCleaning_){ |
| 327 |
candidateChild_ = NULL; |
| 328 |
} |
| 329 |
else |
| 330 |
CbcTree::pop(); |
| 331 |
} |
| 332 |
/// Remove the best node from the heap and return it |
| 333 |
CbcNode * |
| 334 |
CbcProbedDiver::bestNode(double cutoff) |
| 335 |
{ |
| 336 |
#ifdef DIVE_DEBUG |
| 337 |
std::cout<<"CbcProbedDiver::bestNode"<<std::endl; |
| 338 |
#endif |
| 339 |
if (nextOnBranch_ != NULL && !treeCleaning_) { |
| 340 |
if (nextOnBranch_->objectiveValue() < cutoff) { |
| 341 |
if (stop_diving_on_cutoff_ && nextOnBranch_->guessedObjectiveValue() >= cutoff) { |
| 342 |
#ifdef DIVE_DEBUG |
| 343 |
printf("Stopping dive %g %g\n", nextOnBranch_->objectiveValue(), nextOnBranch_->guessedObjectiveValue()); |
| 344 |
#endif |
| 345 |
CbcTree::push(nextOnBranch_); |
| 346 |
nextOnBranch_ = NULL; |
| 347 |
//Also have to cleanup candidateChild_ |
| 348 |
CbcTree::push(candidateChild_); |
| 349 |
candidateChild_ = NULL; |
| 350 |
return CbcTree::bestNode(cutoff); |
| 351 |
} |
| 352 |
#ifdef DIVE_DEBUG |
| 353 |
printf("Diving on %i. obj=%g guesses=%g\n", nextOnBranch_, |
| 354 |
nextOnBranch_->objectiveValue(), |
| 355 |
nextOnBranch_->guessedObjectiveValue()); |
| 356 |
#endif |
| 357 |
CbcNode * ret_val = nextOnBranch_; |
| 358 |
nextOnBranch_ = NULL; |
| 359 |
return ret_val; |
| 360 |
} |
| 361 |
assert(true && "Should not get here."); |
| 362 |
CbcTree::push(nextOnBranch_);//For safety |
| 363 |
nextOnBranch_ = NULL; |
| 364 |
} |
| 365 |
else if(candidateChild_ != NULL && ! treeCleaning_){ |
| 366 |
#ifdef DIVE_DEBUG |
| 367 |
printf("brother was infeasible!!\n``"); |
| 368 |
#endif |
| 369 |
if(candidateChild_->objectiveValue() < cutoff) { |
| 370 |
if(stop_diving_on_cutoff_ && candidateChild_->guessedObjectiveValue() >= cutoff) { |
| 371 |
#ifdef DIVE_DEBUG |
| 372 |
printf("Stopping dive %g %g\n", candidateChild_->objectiveValue(), candidateChild_->guessedObjectiveValue()); |
| 373 |
#endif |
| 374 |
CbcTree::push(candidateChild_); |
| 375 |
candidateChild_ = NULL; |
| 376 |
return CbcTree::bestNode(cutoff); |
| 377 |
} |
| 378 |
#ifdef DIVE_DEBUG |
| 379 |
printf("Diving on %i. obj=%g guessed=%g\n", |
| 380 |
candidateChild_, |
| 381 |
candidateChild_->objectiveValue(), |
| 382 |
candidateChild_->guessedObjectiveValue()); |
| 383 |
#endif |
| 384 |
CbcNode * ret_val = candidateChild_; |
| 385 |
candidateChild_ = NULL; |
| 386 |
return ret_val; |
| 387 |
} |
| 388 |
assert(true && "Should not get here."); |
| 389 |
} |
| 390 |
CbcNode * ret_val = CbcTree::bestNode(cutoff); |
| 391 |
#ifdef DIVE_DEBUG |
| 392 |
printf("Picking top of the heap node %i", ret_val); |
| 393 |
#endif |
| 394 |
return ret_val; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
/** Test if empty. */ |
| 399 |
bool CbcProbedDiver::empty() |
| 400 |
{ |
| 401 |
return (CbcTree::empty() && (nextOnBranch_ == NULL) && (candidateChild_ == NULL) ); |
| 402 |
} |
| 403 |
|
| 404 |
/*! \brief Prune the tree using an objective function cutoff |
| 405 |
if nextOnBranch_ exists we push it on the heap and call CbcTree function |
| 406 |
*/ |
| 407 |
void |
| 408 |
CbcProbedDiver::cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective) |
| 409 |
{ |
| 410 |
if (nextOnBranch_ != NULL) |
| 411 |
CbcTree::push(nextOnBranch_); |
| 412 |
if (candidateChild_ != NULL) |
| 413 |
CbcTree::push(candidateChild_); |
| 414 |
nextOnBranch_=NULL; |
| 415 |
candidateChild_ = NULL; |
| 416 |
treeCleaning_ = true; |
| 417 |
CbcTree::cleanTree(model,cutoff, bestPossibleObjective); |
| 418 |
treeCleaning_ = false; |
| 419 |
} |
| 420 |
|
| 421 |
/// Get best possible objective function in the tree |
| 422 |
double |
| 423 |
CbcProbedDiver::getBestPossibleObjective() |
| 424 |
{ |
| 425 |
double bestPossibleObjective = (nextOnBranch_ != NULL) ? nextOnBranch_->objectiveValue() : 1e100; |
| 426 |
if(candidateChild_ != NULL && candidateChild_->objectiveValue() < bestPossibleObjective ) |
| 427 |
bestPossibleObjective = candidateChild_->objectiveValue(); |
| 428 |
for (unsigned int i = 0 ; i < nodes_.size() ; i++) { |
| 429 |
if (nodes_[i] == NULL) continue; |
| 430 |
const double & obj = nodes_[i]->objectiveValue(); |
| 431 |
if (obj < bestPossibleObjective) { |
| 432 |
bestPossibleObjective = obj; |
| 433 |
} |
| 434 |
} |
| 435 |
return bestPossibleObjective; |
| 436 |
} |
| 437 |
|
| 438 |
/// Initialize the method (get options) |
| 439 |
void |
| 440 |
CbcProbedDiver::initialize(BabSetupBase &b) |
| 441 |
{ |
| 442 |
b.options()->GetBoolValue("stop_diving_on_cutoff", stop_diving_on_cutoff_, |
| 443 |
b.prefix()); |
| 444 |
} |
| 445 |
|
| 446 |
/************************************************************************/ |
| 447 |
/* CbcDfsDiver methods */ |
| 448 |
/************************************************************************/ |
| 449 |
/// Default constructor. |
| 450 |
CbcDfsDiver::CbcDfsDiver(): CbcTree(), |
| 451 |
treeCleaning_(false), |
| 452 |
dive_(), |
| 453 |
diveListSize_(0), |
| 454 |
divingBoardDepth_(-1), |
| 455 |
cutoff_(1e100), |
| 456 |
nBacktracks_(0), |
| 457 |
maxDepthBFS_(4), |
| 458 |
maxDiveBacktracks_(2), |
| 459 |
maxDiveDepth_(COIN_INT_MAX), |
| 460 |
mode_(Enlarge) |
| 461 |
{} |
| 462 |
|
| 463 |
///Copy constructor. |
| 464 |
CbcDfsDiver::CbcDfsDiver(const CbcDfsDiver &rhs):CbcTree(rhs), |
| 465 |
treeCleaning_(rhs.treeCleaning_), |
| 466 |
dive_(rhs.dive_), |
| 467 |
diveListSize_(rhs.diveListSize_), |
| 468 |
divingBoardDepth_(rhs.divingBoardDepth_), |
| 469 |
cutoff_(rhs.cutoff_), |
| 470 |
nBacktracks_(rhs.nBacktracks_), |
| 471 |
maxDepthBFS_(rhs.maxDepthBFS_), |
| 472 |
maxDiveBacktracks_(rhs.maxDiveBacktracks_), |
| 473 |
maxDiveDepth_(rhs.maxDiveDepth_), |
| 474 |
mode_(rhs.mode_) |
| 475 |
{} |
| 476 |
/// Assignment operator. |
| 477 |
CbcDfsDiver & |
| 478 |
CbcDfsDiver::operator=(const CbcDfsDiver &rhs) |
| 479 |
{ |
| 480 |
if (this != &rhs) { |
| 481 |
CbcTree::operator=(rhs); |
| 482 |
treeCleaning_ = rhs.treeCleaning_; |
| 483 |
dive_ = rhs.dive_; |
| 484 |
diveListSize_ = rhs.diveListSize_; |
| 485 |
divingBoardDepth_ = rhs.divingBoardDepth_; |
| 486 |
cutoff_ = rhs.cutoff_; |
| 487 |
nBacktracks_ = rhs.nBacktracks_; |
| 488 |
maxDepthBFS_ = rhs.maxDepthBFS_; |
| 489 |
maxDiveBacktracks_ = rhs.maxDiveBacktracks_; |
| 490 |
maxDiveDepth_ = maxDiveDepth_; |
| 491 |
mode_ = rhs.mode_; |
| 492 |
} |
| 493 |
return *this; |
| 494 |
} |
| 495 |
|
| 496 |
/// Destructor. |
| 497 |
CbcDfsDiver::~CbcDfsDiver() |
| 498 |
{} |
| 499 |
|
| 500 |
///copy constructor. |
| 501 |
CbcTree * |
| 502 |
CbcDfsDiver::clone() const |
| 503 |
{ |
| 504 |
return new CbcDfsDiver(*this); |
| 505 |
} |
| 506 |
|
| 507 |
///Return top node (next node to process.*/ |
| 508 |
CbcNode * |
| 509 |
CbcDfsDiver::top() const |
| 510 |
{ |
| 511 |
if (treeCleaning_) return CbcTree::top(); |
| 512 |
#ifdef DIVE_DEBUG |
| 513 |
std::cout<<"CbcDfsDiver::top"<<std::endl; |
| 514 |
#endif |
| 515 |
if (mode_ != CbcDfsDiver::FindSolutions) { |
| 516 |
assert(dive_.empty()); |
| 517 |
CbcTree::top(); |
| 518 |
} |
| 519 |
if (diveListSize_) { |
| 520 |
return dive_.front(); |
| 521 |
} |
| 522 |
else return CbcTree::top(); |
| 523 |
} |
| 524 |
|
| 525 |
/// Add node to the heap. |
| 526 |
void |
| 527 |
CbcDfsDiver::push(CbcNode * x) |
| 528 |
{ |
| 529 |
if (treeCleaning_) return CbcTree::push(x); |
| 530 |
#ifdef DIVE_DEBUG |
| 531 |
std::cout<<"CbcDfsDiver::push"<<std::endl; |
| 532 |
#endif |
| 533 |
if (mode_ > CbcDfsDiver::FindSolutions) { |
| 534 |
CbcTree::push(x); |
| 535 |
assert(dive_.empty()); |
| 536 |
return; |
| 537 |
} |
| 538 |
//Always push on dive; |
| 539 |
dive_.push_front(x); |
| 540 |
diveListSize_++; |
| 541 |
#ifdef DIVE_DEBUG |
| 542 |
printf("diveListSize_ = %i == %u = dive_.size()\n",diveListSize_, dive_.size()); |
| 543 |
assert(diveListSize_ == dive_.size()); |
| 544 |
#endif |
| 545 |
} |
| 546 |
|
| 547 |
/// Remove the top node of the heap. |
| 548 |
void |
| 549 |
CbcDfsDiver::pop() |
| 550 |
{ |
| 551 |
if (treeCleaning_) return CbcTree::pop(); |
| 552 |
#ifdef DIVE_DEBUG |
| 553 |
std::cout<<"CbcDfsDiver::pop"<<std::endl; |
| 554 |
#endif |
| 555 |
if (mode_ > CbcDfsDiver::FindSolutions) { |
| 556 |
assert(dive_.empty()); |
| 557 |
} |
| 558 |
if (!dive_.empty()) { |
| 559 |
dive_.pop_front(); |
| 560 |
diveListSize_--; |
| 561 |
} |
| 562 |
else |
| 563 |
CbcTree::pop(); |
| 564 |
} |
| 565 |
|
| 566 |
/// Remove the best node from the heap and return it |
| 567 |
CbcNode * |
| 568 |
CbcDfsDiver::bestNode(double cutoff) |
| 569 |
{ |
| 570 |
#ifdef DIVE_DEBUG |
| 571 |
std::cout<<"CbcDfsDiver::bestNode"<<std::endl; |
| 572 |
#endif |
| 573 |
if (treeCleaning_) return CbcTree::bestNode(cutoff); |
| 574 |
#ifdef DIVE_DEBUG |
| 575 |
for (unsigned int i = 0 ; i < nodes_.size() ; i++) { |
| 576 |
if (nodes_[i]->objectiveValue() >= cutoff) |
| 577 |
std::cerr<<"CbcDfsDiver::bestNode"<<std::endl |
| 578 |
<<nodes_[i]->objectiveValue()<<", "<<cutoff<<std::endl; |
| 579 |
assert(nodes_[i]->objectiveValue() < cutoff); |
| 580 |
} |
| 581 |
#endif |
| 582 |
if (mode_ == CbcDfsDiver::Enlarge) { |
| 583 |
if (diveListSize_ == 0) |
| 584 |
mode_ = CbcDfsDiver::FindSolutions; |
| 585 |
else { |
| 586 |
CbcNode * node = dive_.back(); |
| 587 |
assert(node != NULL); |
| 588 |
if (node->depth() > maxDepthBFS_) { |
| 589 |
//Switch mode to Diving |
| 590 |
setComparisonMode(FindSolutions); |
| 591 |
} |
| 592 |
else { |
| 593 |
//pop and return node; |
| 594 |
dive_.pop_back(); |
| 595 |
diveListSize_ --; |
| 596 |
return node; |
| 597 |
} |
| 598 |
} |
| 599 |
} |
| 600 |
if (mode_ != CbcDfsDiver::FindSolutions) { |
| 601 |
assert(dive_.empty()); |
| 602 |
CbcTree::bestNode(cutoff); |
| 603 |
} |
| 604 |
assert(nBacktracks_ < maxDiveBacktracks_); |
| 605 |
CbcNode * node = NULL; |
| 606 |
while (diveListSize_ > 0) { |
| 607 |
#ifdef DIVE_DEBUG |
| 608 |
std::cerr<<"CbcDfsDiver::bestNode" |
| 609 |
<<", examining node"<<std::endl; |
| 610 |
#endif |
| 611 |
assert(!dive_.empty()); |
| 612 |
node = dive_.front(); |
| 613 |
dive_.pop_front(); |
| 614 |
diveListSize_ --; |
| 615 |
assert(node); |
| 616 |
assert((node->depth() - divingBoardDepth_) <= maxDiveDepth_); |
| 617 |
if (node->objectiveValue() > cutoff) {//throw away node for now just put it on the heap as deleting a node is |
| 618 |
//more complicated than that (has to delete nodeInfo, cuts...) |
| 619 |
#ifdef DIVE_DEBUG |
| 620 |
std::cout<<"CbcDfsDiver::bestNode" |
| 621 |
<<", node above cutoff"<<std::endl; |
| 622 |
#endif |
| 623 |
CbcTree::push(node); |
| 624 |
node = NULL; |
| 625 |
nBacktracks_++; |
| 626 |
} |
| 627 |
else if (0 && node->guessedObjectiveValue() > cutoff) {//Put it on the real heap |
| 628 |
#ifdef DIVE_DEBUG |
| 629 |
std::cout<<"CbcDfsDiver::bestNode" |
| 630 |
<<", node estimates "<<node->guessedObjectiveValue()<<"above cutoff" |
| 631 |
<<cutoff<<std::endl; |
| 632 |
#endif |
| 633 |
CbcTree::push(node); |
| 634 |
nBacktracks_++; |
| 635 |
node = NULL; |
| 636 |
} |
| 637 |
else if ((node->depth() - divingBoardDepth_) > maxDiveDepth_) {//Put it on the real heap |
| 638 |
#ifdef DIVE_DEBUG |
| 639 |
std::cout<<"CbcDfsDiver::bestNode" |
| 640 |
<<", node too deep"<<std::endl; |
| 641 |
#endif |
| 642 |
CbcTree::push(node); |
| 643 |
nBacktracks_++; |
| 644 |
node = NULL; |
| 645 |
} |
| 646 |
else if (node->branchingObject()->numberBranchesLeft() < node->branchingObject()->numberBranches()) {//Backtracking |
| 647 |
nBacktracks_++; |
| 648 |
#ifdef DIVE_DEBUG |
| 649 |
std::cout<<"CbcDfsDiver::bestNode" |
| 650 |
<<", backtracking"<<std::endl; |
| 651 |
#endif |
| 652 |
} |
| 653 |
if (nBacktracks_ >= maxDiveBacktracks_) {//Push all the node in dive_ onto nodes_ |
| 654 |
#ifdef DIVE_DEBUG |
| 655 |
std::cout<<"CbcDfsDiver::bestNode" |
| 656 |
<<", maximum number of backtracks attained emptying dive_"<<std::endl; |
| 657 |
#endif |
| 658 |
pushDiveOntoHeap(-COIN_DBL_MAX); |
| 659 |
if (node != NULL) CbcTree::push(node); |
| 660 |
node = NULL; |
| 661 |
} |
| 662 |
if (node != NULL) |
| 663 |
return node; |
| 664 |
} |
| 665 |
assert(node == NULL); |
| 666 |
assert(dive_.empty()); |
| 667 |
assert(diveListSize_ == 0); |
| 668 |
node = CbcTree::bestNode(cutoff); |
| 669 |
divingBoardDepth_ = node->depth(); |
| 670 |
nBacktracks_ = 0; |
| 671 |
return node; |
| 672 |
} |
| 673 |
|
| 674 |
|
| 675 |
void CbcDfsDiver::pushDiveOntoHeap(double cutoff) |
| 676 |
{ |
| 677 |
while (!dive_.empty() ){//&& dive_.front()->objectiveValue() >= cutoff) { |
| 678 |
CbcTree::push(dive_.front()); |
| 679 |
dive_.pop_front(); |
| 680 |
diveListSize_--; |
| 681 |
} |
| 682 |
for (std::list<CbcNode *>::iterator i = dive_.begin() ; i != dive_.end(); |
| 683 |
i++) { |
| 684 |
assert(*i != NULL); |
| 685 |
} |
| 686 |
} |
| 687 |
/** Test if empty. */ |
| 688 |
bool CbcDfsDiver::empty() |
| 689 |
{ |
| 690 |
return (CbcTree::empty() && dive_.empty()); |
| 691 |
} |
| 692 |
|
| 693 |
/*! \brief Prune the tree using an objective function cutoff |
| 694 |
if nextOnBranch_ exists we push it on the heap and call CbcTree function |
| 695 |
*/ |
| 696 |
void |
| 697 |
CbcDfsDiver::cleanTree(CbcModel * model, double cutoff, double & bestPossibleObjective) |
| 698 |
{ |
| 699 |
#ifdef DIVE_DEBUG |
| 700 |
std::cout<<"CbcDfsDiver::cleanTree"<<std::endl; |
| 701 |
std::cout<<"cutoff: "<<cutoff<<std::endl; |
| 702 |
#endif |
| 703 |
pushDiveOntoHeap(cutoff); |
| 704 |
treeCleaning_ = true; |
| 705 |
CbcTree::cleanTree(model,cutoff, bestPossibleObjective); |
| 706 |
treeCleaning_ = false; |
| 707 |
} |
| 708 |
|
| 709 |
/// Get best possible objective function in the tree |
| 710 |
double |
| 711 |
CbcDfsDiver::getBestPossibleObjective() |
| 712 |
{ |
| 713 |
#ifdef DIVE_DEBUG |
| 714 |
std::cout<<"CbcDfsDiver::getBestPossibleObjective"<<std::endl; |
| 715 |
#endif |
| 716 |
double bestPossibleObjective = CbcTree::empty() ? COIN_DBL_MAX : CbcTree::getBestPossibleObjective(); |
| 717 |
for (std::list<CbcNode *>::iterator i = dive_.begin() ; i != dive_.end() ; i++) { |
| 718 |
if (*i == NULL) continue; |
| 719 |
const double & obj = (*i)->objectiveValue(); |
| 720 |
if (obj < bestPossibleObjective) { |
| 721 |
bestPossibleObjective = obj; |
| 722 |
} |
| 723 |
} |
| 724 |
return bestPossibleObjective; |
| 725 |
} |
| 726 |
//#ifdef COIN_HAS_BONMIN |
| 727 |
///Register the options of the method. |
| 728 |
void |
| 729 |
CbcDfsDiver::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions) |
| 730 |
{ |
| 731 |
roptions->SetRegisteringCategory("Diving options", RegisteredOptions::UndocumentedCategory); |
| 732 |
roptions->AddLowerBoundedIntegerOption("max_backtracks_in_dive", |
| 733 |
"Set the number of backtracks in a dive when using dfs-dive tree search" |
| 734 |
"strategy.", |
| 735 |
0,5, |
| 736 |
""); |
| 737 |
roptions->setOptionExtraInfo("max_backtracks_in_dive",27); |
| 738 |
|
| 739 |
roptions->AddLowerBoundedIntegerOption("max_dive_depth", |
| 740 |
"When using dfs-dive search. Maximum depth to go to from the diving " |
| 741 |
"board (node where the diving started.", |
| 742 |
0,COIN_INT_MAX, |
| 743 |
""); |
| 744 |
roptions->setOptionExtraInfo("max_dive_depth",27); |
| 745 |
|
| 746 |
} |
| 747 |
|
| 748 |
/// Initialize the method (get options) |
| 749 |
void |
| 750 |
CbcDfsDiver::initialize(BabSetupBase &b) |
| 751 |
{ |
| 752 |
b.options()->GetIntegerValue("max_dive_depth", maxDiveDepth_,b.prefix()); |
| 753 |
b.options()->GetIntegerValue("max_backtracks_in_dive", maxDiveBacktracks_,b.prefix()); |
| 754 |
} |
| 755 |
//#endif |
| 756 |
|
| 757 |
/** Changes the mode of comparison of the tree for "safety reasons" if the mode really changes we always |
| 758 |
finish the current dive and put all the node back onto the heap.*/ |
| 759 |
void |
| 760 |
CbcDfsDiver::setComparisonMode(ComparisonModes newMode) |
| 761 |
{ |
| 762 |
if (newMode != mode_) { |
| 763 |
mode_ = newMode; |
| 764 |
//Empty heap |
| 765 |
pushDiveOntoHeap(-COIN_DBL_MAX); |
| 766 |
nBacktracks_ = maxDiveBacktracks_ -1;//Force to start a new dive |
| 767 |
#ifdef DIVE_DEBUG |
| 768 |
std::cout<<"CbcDfsDiver::setComparisonMode" |
| 769 |
<<std::endl; |
| 770 |
switch (mode_) { |
| 771 |
case Enlarge: |
| 772 |
std::cout<<"Enlarge"<<std::endl; |
| 773 |
break; |
| 774 |
case FindSolutions: |
| 775 |
std::cout<<"FindSolutions"<<std::endl; |
| 776 |
break; |
| 777 |
case CloseBound: |
| 778 |
std::cout<<"CloseBound"<<std::endl; |
| 779 |
break; |
| 780 |
case LimitTreeSize: |
| 781 |
std::cout<<"LimitTreeSize"<<std::endl; |
| 782 |
break; |
| 783 |
} |
| 784 |
#endif |
| 785 |
CbcTree::setComparison(*comparison_.test_); |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
// This allows any method to change behavior as it is called |
| 792 |
// after each solution |
| 793 |
bool |
| 794 |
DiverCompare::newSolution(CbcModel * model) |
| 795 |
{ |
| 796 |
assert(diver_); |
| 797 |
#ifdef DIVE_DEBUG |
| 798 |
std::cout<<"CbcDfsDiver::newSolution" |
| 799 |
<<std::endl; |
| 800 |
std::cout<<"Found "<<model->getSolutionCount()<<" solutions"<<std::endl; |
| 801 |
#endif |
| 802 |
bool r_value = false; |
| 803 |
if (diver_->getComparisonMode() == CbcDfsDiver::Enlarge){ |
| 804 |
diver_->setComparisonMode(CbcDfsDiver::FindSolutions); |
| 805 |
r_value = true;} |
| 806 |
if (model->getSolutionCount() >= numberSolToStopDive_ && diver_->getComparisonMode() == CbcDfsDiver::FindSolutions) { |
| 807 |
diver_->setComparisonMode(CbcDfsDiver::CloseBound); |
| 808 |
r_value = true; |
| 809 |
} |
| 810 |
return r_value; |
| 811 |
} |
| 812 |
|
| 813 |
/// This is test function |
| 814 |
bool |
| 815 |
DiverCompare::test (CbcNode * x, CbcNode * y) |
| 816 |
{ |
| 817 |
assert(diver_); |
| 818 |
assert(comparisonDive_); |
| 819 |
assert(comparisonBound_); |
| 820 |
CbcDfsDiver::ComparisonModes mode = diver_->getComparisonMode(); |
| 821 |
if (mode == CbcDfsDiver::FindSolutions) { |
| 822 |
return comparisonDive_->test(x,y); |
| 823 |
} |
| 824 |
else if (mode == CbcDfsDiver::CloseBound) { |
| 825 |
return comparisonBound_->test(x,y); |
| 826 |
} |
| 827 |
else if (mode == CbcDfsDiver::LimitTreeSize) { |
| 828 |
return comparisonDepth_.test(x,y); |
| 829 |
} |
| 830 |
else { |
| 831 |
throw CoinError("DiverCompare","test"," Unknown mode for comparison."); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
|
| 836 |
// This Also allows any method to change behavior as it is called |
| 837 |
// after each solution |
| 838 |
bool |
| 839 |
DiverCompare::newSolution(CbcModel * model, |
| 840 |
double objectiveAtContinuous, |
| 841 |
int numberInfeasibilitiesAtContinuous) |
| 842 |
{ return false; } |
| 843 |
|
| 844 |
// This allows any method to change behavior as it is called |
| 845 |
// after every 1000 nodes. |
| 846 |
// Return true if want tree re-sorted |
| 847 |
bool |
| 848 |
DiverCompare::every1000Nodes(CbcModel * model,int numberNodes) |
| 849 |
{ |
| 850 |
assert(diver_); |
| 851 |
if (numberNodes > numberNodesToLimitTreeSize_ && diver_->getComparisonMode() != CbcDfsDiver::LimitTreeSize) { |
| 852 |
diver_->setComparisonMode(CbcDfsDiver::LimitTreeSize); |
| 853 |
return true; |
| 854 |
} |
| 855 |
return false; |
| 856 |
} |
| 857 |
|
| 858 |
}/* Ends namespace Bonmin.*/ |
| 859 |
|