00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "BonHeuristicRINS.hpp"
00012 #include "CbcModel.hpp"
00013 #include "OsiBranchingObject.hpp"
00014
00015
00016
00017 namespace Bonmin {
00018
00020 HeuristicRINS::HeuristicRINS():
00021 LocalSolverBasedHeuristic(),
00022 howOften_(10),
00023 numberSolutions_(0)
00024 {}
00026 HeuristicRINS::HeuristicRINS(BonminSetup * setup):
00027 LocalSolverBasedHeuristic(setup),
00028 howOften_(10),
00029 numberSolutions_(0){
00030 }
00031
00033 HeuristicRINS::HeuristicRINS
00034 (const HeuristicRINS &other):
00035 LocalSolverBasedHeuristic(other),
00036 howOften_(other.howOften_),
00037 numberSolutions_(other.numberSolutions_)
00038 {}
00039
00040 HeuristicRINS::~HeuristicRINS(){
00041 }
00042
00044 int
00045 HeuristicRINS::solution(double & objectiveValue,
00046 double * newSolution)
00047 {
00048 #ifdef DEBUG_BON_HEURISTIC_RINS
00049 std::cout<<"entered RINS"<<std::endl;
00050 #endif
00051 if(!howOften_ || model_->getNodeCount() % howOften_ != 0) return 0;
00052
00053 if (0 && numberSolutions_>=model_->getSolutionCount()){
00054 #ifdef DEBUG_BON_HEURISTIC_RINS
00055 std::cout<<"exited RINS a"<<std::endl;
00056 printf("numberSolutions_ %i model_->getSolutionCount() %i\n", numberSolutions_, model_->getSolutionCount());
00057 #endif
00058 return 0;
00059 }
00060 else
00061 numberSolutions_=model_->getSolutionCount();
00062
00063 const double * bestSolution = model_->bestSolution();
00064 if (!bestSolution){
00065 #ifdef DEBUG_BON_HEURISTIC_RINS
00066 std::cout<<"exited RINS b"<<std::endl;
00067 #endif
00068 return 0;
00069 }
00070 OsiTMINLPInterface * nlp = dynamic_cast<OsiTMINLPInterface *>
00071 (model_->solver());
00072 if(nlp == NULL){
00073 nlp = dynamic_cast<OsiTMINLPInterface *>
00074 (setup_->nonlinearSolver()->clone());
00075 }
00076 else {
00077 nlp = dynamic_cast<OsiTMINLPInterface *>(nlp->clone());
00078 }
00079
00080 int numberIntegers = model_->numberIntegers();
00081 const int * integerVariable = model_->integerVariable();
00082
00083 const double * currentSolution = model_->getColSolution();
00084
00085 double primalTolerance;
00086 nlp->getDblParam(OsiPrimalTolerance,primalTolerance);
00087
00088 int nFix=0;
00089 for (int i=0; i<numberIntegers; i++) {
00090 int iColumn=integerVariable[i];
00091 const OsiObject * object = model_->object(i);
00092
00093 double originalLower;
00094 double originalUpper;
00095 getIntegerInformation(object, originalLower, originalUpper);
00096 double valueInt=bestSolution[iColumn];
00097 if (valueInt<originalLower) {
00098 valueInt=originalLower;
00099 } else if (valueInt>originalUpper) {
00100 valueInt=originalUpper;
00101 }
00102 if (fabs(currentSolution[iColumn]-valueInt)<10.0*primalTolerance) {
00103 double nearest=floor(valueInt+0.5);
00104 nlp->setColLower(iColumn,nearest);
00105 nlp->setColUpper(iColumn,nearest);
00106 nFix++;
00107 }
00108 }
00109
00110 int r_val = 0;
00111 if(nFix > numberIntegers/10) {
00112 #ifdef DEBUG_BON_HEURISTIC_RINS
00113 std::cout<<"cutoff = "<<model_->getCutoff()<<std::endl;
00114 #endif
00115 r_val = doLocalSearch(nlp, newSolution, objectiveValue, model_->getCutoff());
00116 #ifdef DEBUG_BON_HEURISTIC_RINS
00117 std::cout<<"executed RINS "<<r_val<<std::endl;
00118 #endif
00119 }
00120 else {numberSolutions_ --;}
00121
00122 if(r_val > 0) {
00123 numberSolutions_ = model_->getSolutionCount() + 1;
00124 howOften_ = std::max(10, howOften_ / 2);
00125 }
00126 else
00127 howOften_ = std::min(10000,2*howOften_);
00128 return r_val;
00129 }
00130
00131 void
00132 HeuristicRINS::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions){
00133 roptions->SetRegisteringCategory("MINLP Heuristics", RegisteredOptions::BonminCategory);
00134 roptions->AddStringOption2(
00135 "heuristic_RINS",
00136 "if yes runs the RINS heuristic",
00137 "no",
00138 "no", "don't run it",
00139 "yes", "runs the heuristic",
00140 "");
00141 roptions->setOptionExtraInfo("heuristic_RINS", 63);
00142 }
00143
00145 void
00146 HeuristicRINS::Initialize(Ipopt::SmartPtr<Bonmin::OptionsList> options){
00147 }
00148 }