BonHeuristicDiveVectorLength.cpp
Go to the documentation of this file.
1 // Copyright (C) 2007, International Business Machines Corporation and others.
2 // All Rights Reserved.
3 // This code is published under the Eclipse Public License.
4 //
5 // Authors :
6 // Joao P. Goncalves, International Business Machines Corporation
7 //
8 // Date : November 12, 2007
9 
10 #include "CoinPragma.hpp"
12 #include "CbcModel.hpp"
13 
14 namespace Bonmin
15 {
17  :
18  HeuristicDive(),
19  columnLength_(NULL)
20  {}
21 
23  :
24  HeuristicDive(setup),
25  columnLength_(NULL)
26  {
27  Initialize(setup->options());
28  }
29 
31  :
32  HeuristicDive(copy),
33  columnLength_(NULL)
34  {
35  }
36 
39  {
40  if (this!=&rhs) {
42  delete [] columnLength_;
43  columnLength_ = NULL;
44  }
45  return *this;
46  }
47 
48  CbcHeuristic *
50  {
51  return new HeuristicDiveVectorLength(*this);
52  }
53 
54  void
56  {
57  delete [] columnLength_;
58 
59  int numberColumns;
60  int numberRows;
61  int nnz_jac_g;
62  int nnz_h_lag;
63  Ipopt::TNLP::IndexStyleEnum index_style;
64  minlp->get_nlp_info(numberColumns, numberRows, nnz_jac_g,
65  nnz_h_lag, index_style);
66 
67  const double* x_sol = minlp->x_sol();
68 
69  // Get the indicies of the jacobian
70  // This is also a way of knowing which variables are
71  // used in each row
72  int* indexRow = new int[nnz_jac_g];
73  int* indexCol = new int[nnz_jac_g];
74  minlp->eval_jac_g(numberColumns, x_sol, false,
75  numberRows, nnz_jac_g,
76  indexRow, indexCol, 0);
77  columnLength_ = new int[numberColumns];
78  int indexCorrection = (index_style == Ipopt::TNLP::C_STYLE) ? 0 : 1;
79  int iniCol = -1;
80  for(int i=0; i<nnz_jac_g; i++) {
81  int thisIndexCol = indexCol[i]-indexCorrection;
82  if(indexCol[i] != iniCol) {
83  iniCol = indexCol[i];
84  columnLength_[thisIndexCol] = 1;
85  }
86  else {
87  columnLength_[thisIndexCol]++;
88  }
89  }
90 
91  }
92 
93  void
95  const vector<int> & integerColumns,
96  const double* newSolution,
97  int& bestColumn,
98  int& bestRound)
99  {
100  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
101 
102  const double* x_l = minlp->x_l();
103  const double* x_u = minlp->x_u();
104 
105  int numberColumns;
106  int numberRows;
107  int nnz_jac_g;
108  int nnz_h_lag;
109  Ipopt::TNLP::IndexStyleEnum index_style;
110  minlp->get_nlp_info(numberColumns, numberRows, nnz_jac_g,
111  nnz_h_lag, index_style);
112 
113  double* gradient_f = new double[numberColumns];
114 
115  double bestScore = COIN_DBL_MAX;
116  bestColumn = -1;
117  bestRound = -1; // -1 rounds down, +1 rounds up
118  minlp->eval_grad_f(numberColumns,newSolution,true,gradient_f);
119  for(int iIntCol=0; iIntCol<(int)integerColumns.size(); iIntCol++) {
120  int iColumn = integerColumns[iIntCol];
121  double value=newSolution[iColumn];
122  if (fabs(floor(value+0.5)-value)>integerTolerance) {
123  double below = floor(value);
124  double downFraction = COIN_DBL_MAX;
125  //double downCost = COIN_DBL_MAX;
126  double gradient = gradient_f[iColumn];
127  if(below >= x_l[iColumn])
128  downFraction = value-below;
129  double above = ceil(value);
130  double upFraction = COIN_DBL_MAX;
131  if(above <= x_u[iColumn])
132  upFraction = ceil(value)-value;
133  double objdelta = 0;
134  int round = 0;
135  if(gradient>=0.0 && upFraction < COIN_DBL_MAX) {
136  objdelta = gradient*upFraction;
137  round = 1;
138  } else if(gradient<0.0 && downFraction < COIN_DBL_MAX) {
139  objdelta = gradient*downFraction;
140  round = -1;
141  } else if(upFraction < COIN_DBL_MAX) {
142  objdelta = gradient*upFraction;
143  round = 1;
144  } else {
145  objdelta = gradient*downFraction;
146  round = -1;
147  }
148  double score = (objdelta + 1e-6)/((double)columnLength_[iColumn]+1.0);
149  if(score<bestScore) {
150  bestScore = score;
151  bestColumn = iColumn;
152  bestRound = round;
153  }
154  }
155  }
156 
157  delete [] gradient_f;
158 
159  }
160 
161  void
163  roptions->SetRegisteringCategory("Primal Heuristics", RegisteredOptions::BonminCategory);
164  roptions->AddStringOption2(
165  "heuristic_dive_vectorLength",
166  "if yes runs the Dive VectorLength heuristic",
167  "no",
168  "no", "",
169  "yes", "",
170  "");
171  roptions->setOptionExtraInfo("heuristic_dive_vectorLength", 63);
172  }
173 
174  void
176  }
177 
178 }
int * columnLength_
the number of nonzero elements in each column
virtual CbcHeuristic * clone() const
Clone.
HeuristicDive & operator=(const HeuristicDive &rhs)
Assignment operator.
virtual void selectVariableToBranch(TMINLP2TNLP *minlp, const vector< int > &integerColumns, const double *newSolution, int &bestColumn, int &bestRound)
Selects the next variable to branch on.
void fint fint fint real fint real real real real real real real real real * e
const Ipopt::Number * x_l()
Get the current values for the lower bounds.
static void registerOptions(Ipopt::SmartPtr< Bonmin::RegisteredOptions > roptions)
Register the options common to all local search based heuristics.
HeuristicDiveVectorLength & operator=(const HeuristicDiveVectorLength &rhs)
Assignment operator.
void Initialize(Ipopt::SmartPtr< Ipopt::OptionsList > options)
Initiaize using passed options.
const Ipopt::Number * x_u()
Get the current values for the upper bounds.
virtual bool eval_grad_f(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Number *grad_f)
Returns the vector of the gradient of the objective w.r.t.
static int
Definition: OSdtoa.cpp:2173
virtual void setInternalVariables(TMINLP2TNLP *minlp)
sets internal variables
const Ipopt::Number * x_sol() const
get the solution values
Ipopt::SmartPtr< Ipopt::OptionsList > options()
Acces list of Options.
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.
virtual bool eval_jac_g(Ipopt::Index n, const Ipopt::Number *x, bool new_x, Ipopt::Index m, Ipopt::Index nele_jac, Ipopt::Index *iRow, Ipopt::Index *jCol, Ipopt::Number *values)
Returns the jacobian of the constraints.