BonHeuristicLocalBranching.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 
12 #include "CbcModel.hpp"
13 #include "OsiBranchingObject.hpp"
14 
15 namespace Bonmin {
16 
20  howOften_(100),
21  numberSolutions_(0)
22  {}
26  howOften_(100),
27  numberSolutions_(0)
28  {}
29 
32  (const HeuristicLocalBranching &other):
34  howOften_(other.howOften_),
35  numberSolutions_(other.numberSolutions_)
36  {}
37 
39  }
40 
41  void
43  {
44  model_=model;
45  // assert(model_->solver());
46  validate();
47  }
48 
49  void
51  {
52  assert(setup_ != NULL);
53  OsiTMINLPInterface * nlp = dynamic_cast<OsiTMINLPInterface *>
55  TMINLP2TNLP* minlp = nlp->problem();
56  int numberColumns;
57  int numberRows;
58  int nnz_jac_g;
59  int nnz_h_lag;
60  Ipopt::TNLP::IndexStyleEnum index_style;
61  minlp->get_nlp_info(numberColumns, numberRows, nnz_jac_g,
62  nnz_h_lag, index_style);
63  const Bonmin::TMINLP::VariableType* variableType = minlp->var_types();
64  const double* x_l = minlp->x_l();
65  const double* x_u = minlp->x_u();
66 
67  for(int i=0; i<numberColumns; i++) {
68  if ((variableType[i] != Bonmin::TMINLP::CONTINUOUS) &&
69  (x_l[i] != 0.0 || x_u[i] != 1.0)) {
70  setWhen(0);
71  return;
72  }
73  }
74  }
75 
77  int
78  HeuristicLocalBranching::solution(double & objectiveValue,
79  double * newSolution)
80  {
81  // if(!when() || model_->getNodeCount() || model_->getCurrentPassNumber() > 1) return 0;
82  if (numberSolutions_>=model_->getSolutionCount())
83  return 0;
84  else
85  numberSolutions_=model_->getSolutionCount();
86 
87  const double * bestSolution = model_->bestSolution();
88  if (!bestSolution)
89  return 0; // No solution found yet
90 
91  OsiTMINLPInterface * nlp = dynamic_cast<OsiTMINLPInterface *>
93 
94 
95  int numberIntegers = model_->numberIntegers();
96  const int * integerVariable = model_->integerVariable();
97 
98  double* vals = new double[numberIntegers];
99  int* inds = new int[numberIntegers];
100 
101  for (int i=0; i<numberIntegers; i++) {
102  int iColumn = integerVariable[i];
103  vals[i] = bestSolution[iColumn];
104  inds[i] = iColumn;
105  }
106 
107  double rhs_local_branching_constraint = numberIntegers / 2; //stefan: this should be the same as floor(numInt/2) since numInt and 2 are int's
108  nlp->switchToFeasibilityProblem(numberIntegers, vals, inds, rhs_local_branching_constraint);
109 
110  int r_val = 0;
111  r_val = doLocalSearch(nlp, newSolution, objectiveValue, model_->getCutoff());
112 
113  delete [] vals;
114  delete [] inds;
115 
116  if(r_val > 0) numberSolutions_ = model_->getSolutionCount() + 1;
117 
118  return r_val;
119  }
120 
121  void
123  roptions->SetRegisteringCategory("Primal Heuristics (undocumented)", RegisteredOptions::UndocumentedCategory);
124  roptions->AddStringOption2(
125  "heuristic_local_branching",
126  "if yes runs the LocalBranching heuristic",
127  "no",
128  "no", "",
129  "yes", "",
130  "");
131  roptions->setOptionExtraInfo("heuristic_local_branching", 63);
132  }
133 
135  void
137  }
138 }/* ends bonmin namespace*/
int howOften_
How often to do (code can change)
virtual void validate()
Validate model i.e. sets when_ to 0 if necessary.
const TMINLP2TNLP * problem() const
get pointer to the TMINLP2TNLP adapter
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.
void Initialize(Ipopt::SmartPtr< Ipopt::OptionsList > options)
Initiaize using passed options.
virtual void setModel(CbcModel *model)
Update model.
int solution(double &objectiveValue, double *newSolution)
Runs heuristic.
const Ipopt::Number * x_l()
Get the current values for the lower bounds.
const Ipopt::Number * x_u()
Get the current values for the upper bounds.
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.
static void registerOptions(Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions)
Register the options common to all local search based heuristics.
OsiTMINLPInterface * nonlinearSolver()
Pointer to the non-linear solver used.
This is an adapter class that converts a TMINLP to a TNLP to be solved by Ipopt.
virtual bool get_nlp_info(Ipopt::Index &n, Ipopt::Index &m, Ipopt::Index &nnz_jac_g, Ipopt::Index &nnz_h_lag, TNLP::IndexStyleEnum &index_style)
This call is just passed onto the TMINLP object.
VariableType
Type of the variables.
Definition: BonTMINLP.hpp:192
BonminSetup * setup_
Setup to use for local searches (will make copies).
const TMINLP::VariableType * var_types()
Get the variable types.