BonHeuristicRINS.cpp
Go to the documentation of this file.
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 
22  howOften_(10),
23  numberSolutions_(0)
24  {}
28  howOften_(10),
29  numberSolutions_(0){
30  }
31 
34  (const HeuristicRINS &other):
36  howOften_(other.howOften_),
37  numberSolutions_(other.numberSolutions_)
38  {}
39 
41  }
42 
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 *>
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
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 
133  void
135  }
136 }/* ends bonmin namespace*/
This is class provides an Osi interface for a Mixed Integer Linear Program expressed as a TMINLP (so ...
OsiSolverInterface * clone(bool copyData=true) const
Virtual copy constructor.
virtual const double * getColSolution() const
Get pointer to array[getNumCols()] of primal solution vector.
virtual void setColUpper(int elementIndex, double elementValue)
Set a single column upper bound.
int solution(double &objectiveValue, double *newSolution)
Runs heuristic.
HeuristicRINS()
Default constructor.
void Initialize(Ipopt::SmartPtr< Ipopt::OptionsList > options)
Initiaize using passed options.
int doLocalSearch(OsiTMINLPInterface *solver, double *solution, double &solValue, double cutoff, std::string prefix="local_solver.") const
Do a local search based on setup and passed solver.
int numberSolutions_
Number of solutions so we can do something at solution.
virtual ~HeuristicRINS()
Destructor.
OsiTMINLPInterface * nonlinearSolver()
Pointer to the non-linear solver used.
int howOften_
How often to do (code can change)
static void registerOptions(Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions)
Register the options common to all local search based heuristics.
virtual void setColLower(int elementIndex, double elementValue)
Set a single column lower bound.
BonminSetup * setup_
Setup to use for local searches (will make copies).
bool getDblParam(OsiDblParam key, double &value) const