| 1 |
// (C) Copyright CNRS and International Business Machines Corporation |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, LIF Université de la Méditérannée-CNRS |
| 7 |
// Joao Goncalves, International Business Machines Corporation |
| 8 |
// |
| 9 |
// Date : 06/18/2008 |
| 10 |
|
| 11 |
#include "BonHeuristicRINS.hpp" |
| 12 |
#include "CbcModel.hpp" |
| 13 |
#include "OsiBranchingObject.hpp" |
| 14 |
|
| 15 |
//#define DEBUG_BON_HEURISTIC_RINS |
| 16 |
|
| 17 |
namespace Bonmin { |
| 18 |
|
| 19 |
/** Default constructor*/ |
| 20 |
HeuristicRINS::HeuristicRINS(): |
| 21 |
LocalSolverBasedHeuristic(), |
| 22 |
howOften_(10), |
| 23 |
numberSolutions_(0) |
| 24 |
{} |
| 25 |
/** Constructor with setup.*/ |
| 26 |
HeuristicRINS::HeuristicRINS(BonminSetup * setup): |
| 27 |
LocalSolverBasedHeuristic(setup), |
| 28 |
howOften_(10), |
| 29 |
numberSolutions_(0){ |
| 30 |
} |
| 31 |
|
| 32 |
/** Copy constructor.*/ |
| 33 |
HeuristicRINS::HeuristicRINS |
| 34 |
(const HeuristicRINS &other): |
| 35 |
LocalSolverBasedHeuristic(other), |
| 36 |
howOften_(other.howOften_), |
| 37 |
numberSolutions_(other.numberSolutions_) |
| 38 |
{} |
| 39 |
|
| 40 |
HeuristicRINS::~HeuristicRINS(){ |
| 41 |
} |
| 42 |
|
| 43 |
/** Runs heuristic*/ |
| 44 |
int |
| 45 |
HeuristicRINS::solution(double & objectiveValue, |
| 46 |
double * newSolution) |
| 47 |
{ |
| 48 |
if(!howOften_ || model_->getNodeCount() % howOften_ != 0) return 0; |
| 49 |
numberSolutions_=model_->getSolutionCount(); |
| 50 |
|
| 51 |
const double * bestSolution = model_->bestSolution(); |
| 52 |
if (!bestSolution){ |
| 53 |
#ifdef DEBUG_BON_HEURISTIC_RINS |
| 54 |
std::cout<<"exited RINS b"<<std::endl; |
| 55 |
#endif |
| 56 |
return 0; // No solution found yet |
| 57 |
} |
| 58 |
OsiTMINLPInterface * nlp = dynamic_cast<OsiTMINLPInterface *> |
| 59 |
(model_->solver()); |
| 60 |
if(nlp == NULL){ |
| 61 |
nlp = dynamic_cast<OsiTMINLPInterface *> |
| 62 |
(setup_->nonlinearSolver()->clone()); |
| 63 |
} |
| 64 |
else { |
| 65 |
nlp = dynamic_cast<OsiTMINLPInterface *>(nlp->clone()); |
| 66 |
} |
| 67 |
|
| 68 |
int numberIntegers = model_->numberIntegers(); |
| 69 |
const int * integerVariable = model_->integerVariable(); |
| 70 |
|
| 71 |
const double * currentSolution = model_->getColSolution(); |
| 72 |
|
| 73 |
double primalTolerance; |
| 74 |
nlp->getDblParam(OsiPrimalTolerance,primalTolerance); |
| 75 |
|
| 76 |
int nFix=0; |
| 77 |
for (int i=0; i<numberIntegers; i++) { |
| 78 |
int iColumn=integerVariable[i]; |
| 79 |
const OsiObject * object = model_->object(i); |
| 80 |
// get original bounds |
| 81 |
double originalLower; |
| 82 |
double originalUpper; |
| 83 |
getIntegerInformation(object, originalLower, originalUpper); |
| 84 |
double valueInt=bestSolution[iColumn]; |
| 85 |
if (valueInt<originalLower) { |
| 86 |
valueInt=originalLower; |
| 87 |
} else if (valueInt>originalUpper) { |
| 88 |
valueInt=originalUpper; |
| 89 |
} |
| 90 |
if (fabs(currentSolution[iColumn]-valueInt)<10.0*primalTolerance) { |
| 91 |
double nearest=floor(valueInt+0.5); |
| 92 |
nlp->setColLower(iColumn,nearest); |
| 93 |
nlp->setColUpper(iColumn,nearest); |
| 94 |
nFix++; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
int r_val = 0; |
| 99 |
if(nFix > numberIntegers/10) { |
| 100 |
#ifdef DEBUG_BON_HEURISTIC_RINS |
| 101 |
std::cout<<"cutoff = "<<model_->getCutoff()<<std::endl; |
| 102 |
#endif |
| 103 |
r_val = doLocalSearch(nlp, newSolution, objectiveValue, model_->getCutoff(), "rins."); |
| 104 |
#ifdef DEBUG_BON_HEURISTIC_RINS |
| 105 |
std::cout<<"executed RINS "<<r_val<<std::endl; |
| 106 |
#endif |
| 107 |
} |
| 108 |
else {numberSolutions_ --;} |
| 109 |
|
| 110 |
if(r_val > 0) { |
| 111 |
numberSolutions_ = model_->getSolutionCount() + 1; |
| 112 |
howOften_ = std::max(10, howOften_ / 2); |
| 113 |
} |
| 114 |
else |
| 115 |
howOften_ = std::min(10000,2*howOften_); |
| 116 |
return r_val; |
| 117 |
} |
| 118 |
|
| 119 |
void |
| 120 |
HeuristicRINS::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions){ |
| 121 |
roptions->SetRegisteringCategory("Primal Heuristics", RegisteredOptions::BonminCategory); |
| 122 |
roptions->AddStringOption2( |
| 123 |
"heuristic_RINS", |
| 124 |
"if yes runs the RINS heuristic", |
| 125 |
"no", |
| 126 |
"no", "", |
| 127 |
"yes", "", |
| 128 |
""); |
| 129 |
roptions->setOptionExtraInfo("heuristic_RINS", 63); |
| 130 |
} |
| 131 |
|
| 132 |
/** Initiaize using passed options.*/ |
| 133 |
void |
| 134 |
HeuristicRINS::Initialize(Ipopt::SmartPtr<Ipopt::OptionsList> options){ |
| 135 |
} |
| 136 |
}/* ends bonmin namespace*/ |