| 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" |
| 11 |
#include "BonHeuristicDiveVectorLength.hpp" |
| 12 |
#include "CbcModel.hpp" |
| 13 |
|
| 14 |
namespace Bonmin |
| 15 |
{ |
| 16 |
HeuristicDiveVectorLength::HeuristicDiveVectorLength() |
| 17 |
: |
| 18 |
HeuristicDive(), |
| 19 |
columnLength_(NULL) |
| 20 |
{} |
| 21 |
|
| 22 |
HeuristicDiveVectorLength::HeuristicDiveVectorLength(BonminSetup * setup) |
| 23 |
: |
| 24 |
HeuristicDive(setup), |
| 25 |
columnLength_(NULL) |
| 26 |
{ |
| 27 |
Initialize(setup->options()); |
| 28 |
} |
| 29 |
|
| 30 |
HeuristicDiveVectorLength::HeuristicDiveVectorLength(const HeuristicDiveVectorLength ©) |
| 31 |
: |
| 32 |
HeuristicDive(copy), |
| 33 |
columnLength_(NULL) |
| 34 |
{ |
| 35 |
} |
| 36 |
|
| 37 |
HeuristicDiveVectorLength & |
| 38 |
HeuristicDiveVectorLength::operator=( const HeuristicDiveVectorLength& rhs) |
| 39 |
{ |
| 40 |
if (this!=&rhs) { |
| 41 |
HeuristicDive::operator=(rhs); |
| 42 |
delete [] columnLength_; |
| 43 |
columnLength_ = NULL; |
| 44 |
} |
| 45 |
return *this; |
| 46 |
} |
| 47 |
|
| 48 |
CbcHeuristic * |
| 49 |
HeuristicDiveVectorLength::clone() const |
| 50 |
{ |
| 51 |
return new HeuristicDiveVectorLength(*this); |
| 52 |
} |
| 53 |
|
| 54 |
void |
| 55 |
HeuristicDiveVectorLength::setInternalVariables(TMINLP2TNLP* minlp) |
| 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 |
| 94 |
HeuristicDiveVectorLength::selectVariableToBranch(TMINLP2TNLP* minlp, |
| 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 |
| 162 |
HeuristicDiveVectorLength::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions){ |
| 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 |
| 175 |
HeuristicDiveVectorLength::Initialize(Ipopt::SmartPtr<Ipopt::OptionsList> options){ |
| 176 |
} |
| 177 |
|
| 178 |
} |