| 1 |
// (C) Copyright CNRS 2008 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// P. Bonami, CNRS |
| 7 |
// |
| 8 |
// Date : 02/13/2009 |
| 9 |
|
| 10 |
#include "BonFpForMinlp.hpp" |
| 11 |
#include "BonminConfig.h" |
| 12 |
|
| 13 |
#include "OsiClpSolverInterface.hpp" |
| 14 |
|
| 15 |
#include "CbcModel.hpp" |
| 16 |
#include "BonCbcLpStrategy.hpp" |
| 17 |
#ifdef COIN_HAS_CPX |
| 18 |
#include "OsiCpxSolverInterface.hpp" |
| 19 |
#endif |
| 20 |
#include "OsiAuxInfo.hpp" |
| 21 |
#include "BonSolverHelp.hpp" |
| 22 |
|
| 23 |
#include <climits> |
| 24 |
|
| 25 |
namespace Bonmin |
| 26 |
{ |
| 27 |
static const char * txt_id = "FP for MINLP"; |
| 28 |
/// Constructor with basic setup |
| 29 |
MinlpFeasPump::MinlpFeasPump(BabSetupBase & b): |
| 30 |
OaDecompositionBase(b, true, false), |
| 31 |
subMip_(NULL) |
| 32 |
{ |
| 33 |
std::string bonmin="bonmin."; |
| 34 |
std::string prefix = (b.prefix() == bonmin) ? "" : b.prefix(); |
| 35 |
prefix += "pump_for_minlp."; |
| 36 |
subMip_ = new SubMipSolver(b, prefix); |
| 37 |
double oaTime; |
| 38 |
b.options()->GetNumericValue("time_limit",oaTime,prefix); |
| 39 |
parameter().maxLocalSearch_ = INT_MAX; |
| 40 |
b.options()->GetIntegerValue("solution_limit", parameter().maxSols_,prefix); |
| 41 |
parameter().maxLocalSearchTime_ = |
| 42 |
std::min(b.getDoubleParameter(BabSetupBase::MaxTime), oaTime); |
| 43 |
if(parameter().maxSols_ > b.getIntParameter(BabSetupBase::MaxSolutions)) |
| 44 |
parameter().maxSols_ = b.getIntParameter(BabSetupBase::MaxSolutions); |
| 45 |
b.options()->GetEnumValue("fp_pass_infeasible", passBound_, prefix); |
| 46 |
} |
| 47 |
|
| 48 |
MinlpFeasPump::~MinlpFeasPump() |
| 49 |
{ |
| 50 |
delete subMip_; |
| 51 |
} |
| 52 |
|
| 53 |
/// virutal method to decide if local search is performed |
| 54 |
bool |
| 55 |
MinlpFeasPump::doLocalSearch(BabInfo * babInfo) const |
| 56 |
{ |
| 57 |
return (nLocalSearch_<parameters_.maxLocalSearch_ && |
| 58 |
CoinCpuTime() - timeBegin_ < parameters_.maxLocalSearchTime_ && |
| 59 |
numSols_ < parameters_.maxSols_); |
| 60 |
} |
| 61 |
/// virtual method which performs the OA algorithm by modifying lp and nlp. |
| 62 |
double |
| 63 |
MinlpFeasPump::performOa(OsiCuts &cs, |
| 64 |
solverManip &lpManip, |
| 65 |
BabInfo * babInfo, |
| 66 |
double & cutoff,const CglTreeInfo &info) const |
| 67 |
{ |
| 68 |
|
| 69 |
//bool interuptOnLimit = false; |
| 70 |
//double lastPeriodicLog = CoinCpuTime(); |
| 71 |
|
| 72 |
const int numcols = nlp_->getNumCols(); |
| 73 |
vector<double> savedColLower(nlp_->getNumCols()); |
| 74 |
CoinCopyN(nlp_->getColLower(), nlp_->getNumCols(), savedColLower()); |
| 75 |
vector<double> savedColUpper(nlp_->getNumCols()); |
| 76 |
CoinCopyN(nlp_->getColUpper(), nlp_->getNumCols(), savedColUpper()); |
| 77 |
|
| 78 |
|
| 79 |
subMip_->setLpSolver(lpManip.si()); |
| 80 |
OsiSolverInterface * lp = subMip_->solver(); |
| 81 |
|
| 82 |
assert(lp); |
| 83 |
vector<int> indices; |
| 84 |
for(int i = 0; i < numcols ; i++) { |
| 85 |
lp->setObjCoeff(i,0); |
| 86 |
if(!lp->isInteger(i)) { |
| 87 |
} |
| 88 |
else { indices.push_back(i);} |
| 89 |
} |
| 90 |
|
| 91 |
// If objective is linear need to add to lp constraint for objective |
| 92 |
const double * colsol = NULL; |
| 93 |
lp->resolve(); |
| 94 |
OsiBranchingInformation branch_info(lp, false); |
| 95 |
branch_info.lower_ = savedColLower(); |
| 96 |
branch_info.upper_ = savedColUpper(); |
| 97 |
if(lp->getNumCols() == nlp_->getNumCols()) |
| 98 |
nlp_->addObjectiveFunction(*lp, nlp_->getColSolution()); |
| 99 |
lp->setObjCoeff(numcols,0); |
| 100 |
|
| 101 |
bool milpOptimal = false; |
| 102 |
nlp_->resolve(txt_id); |
| 103 |
set_fp_objective(*lp, nlp_->getColSolution()); |
| 104 |
lp->initialSolve(); |
| 105 |
lp->setColUpper(numcols, cutoff); |
| 106 |
subMip_->solve(DBL_MAX, parameters_.subMilpLogLevel_, |
| 107 |
//subMip_->optimize(DBL_MAX, parameters_.subMilpLogLevel_, |
| 108 |
(parameters_.maxLocalSearchTime_ + timeBegin_ - CoinCpuTime()) /* time limit */); |
| 109 |
|
| 110 |
milpOptimal = subMip_ -> optimal(); |
| 111 |
colsol = subMip_->getLastSolution(); |
| 112 |
nLocalSearch_++; |
| 113 |
if(milpOptimal) |
| 114 |
handler_->message(SOLVED_LOCAL_SEARCH, messages_) |
| 115 |
<<subMip_->nodeCount()<<subMip_->iterationCount()<<CoinMessageEol; |
| 116 |
else |
| 117 |
handler_->message(LOCAL_SEARCH_ABORT, messages_) |
| 118 |
<<subMip_->nodeCount()<<subMip_->iterationCount()<<CoinMessageEol; |
| 119 |
int numberPasses = 0; |
| 120 |
|
| 121 |
#ifdef OA_DEBUG |
| 122 |
bool foundSolution = 0; |
| 123 |
#endif |
| 124 |
double * nlpSol = NULL; |
| 125 |
int major_iteration = 0; |
| 126 |
double ub = cutoff; |
| 127 |
while (colsol) { |
| 128 |
numberPasses++; |
| 129 |
|
| 130 |
//setup the nlp |
| 131 |
int numberCutsBefore = cs.sizeRowCuts(); |
| 132 |
|
| 133 |
//Fix the variable which have to be fixed, after having saved the bounds |
| 134 |
branch_info.solution_ = colsol; |
| 135 |
|
| 136 |
vector<double> x_bar(indices.size()); |
| 137 |
for(unsigned int i = 0 ; i < indices.size() ; i++){ |
| 138 |
assert(fabs(colsol[indices[i]] - floor(colsol[indices[i]] + 0.5)) < 1e-5); |
| 139 |
x_bar[i] = colsol[indices[i]]; |
| 140 |
} |
| 141 |
|
| 142 |
double dist = nlp_->solveFeasibilityProblem(indices.size(), x_bar(), indices(), 1, 0, 2); |
| 143 |
|
| 144 |
handler_->message(FP_DISTANCE, messages_) |
| 145 |
<<dist<<CoinMessageEol; |
| 146 |
|
| 147 |
if(dist < 1e-06){ |
| 148 |
fixIntegers(*nlp_,branch_info, parameters_.cbcIntegerTolerance_, objects_, nObjects_); |
| 149 |
|
| 150 |
nlp_->resolve(txt_id); |
| 151 |
if(!nlp_->isProvenOptimal()){ |
| 152 |
relaxIntegers(*nlp_,branch_info, parameters_.cbcIntegerTolerance_, objects_, nObjects_); |
| 153 |
nlp_->resolve(txt_id); |
| 154 |
} |
| 155 |
bool restart = false; |
| 156 |
if (post_nlp_solve(babInfo, cutoff)) { |
| 157 |
restart = true; |
| 158 |
//nlp is solved and feasible |
| 159 |
// Update the cutoff |
| 160 |
ub = std::min(ub, nlp_->getObjValue()); |
| 161 |
cutoff = ub > 0 ? |
| 162 |
ub * (1 - parameters_.cbcCutoffIncrement_): |
| 163 |
ub * (1 + parameters_.cbcCutoffIncrement_); |
| 164 |
assert(cutoff < ub); |
| 165 |
|
| 166 |
numSols_++; |
| 167 |
} |
| 168 |
else{ |
| 169 |
//nlp_->setColLower(savedColLower()); |
| 170 |
//nlp_->setColUpper(savedColUpper()); |
| 171 |
//dist = nlp_->solveFeasibilityProblem(indices.size(), x_bar(), indices(), 1, 0, 2); |
| 172 |
} |
| 173 |
nlpSol = const_cast<double *>(nlp_->getColSolution()); |
| 174 |
nlp_->getOuterApproximation(cs, nlpSol, 1, NULL, |
| 175 |
parameter().global_); |
| 176 |
//if(restart){ |
| 177 |
nlp_->setColLower(savedColLower()); |
| 178 |
nlp_->setColUpper(savedColUpper()); |
| 179 |
if(restart){ |
| 180 |
major_iteration++; |
| 181 |
handler_->message(FP_MAJOR_ITERATION, messages_) |
| 182 |
<<major_iteration<<cutoff<<CoinMessageEol; |
| 183 |
nlp_->resolve(txt_id); |
| 184 |
} |
| 185 |
|
| 186 |
//} |
| 187 |
} |
| 188 |
else { |
| 189 |
nlpSol = const_cast<double *>(nlp_->getColSolution()); |
| 190 |
nlp_->getOuterApproximation(cs, nlpSol, 1, NULL, |
| 191 |
parameter().global_); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
#if 0 |
| 196 |
handler_->message(FP_MINOR_ITERATION, messages_) |
| 197 |
<<nLocalSearch_<<cutoff<<CoinMessageEol; |
| 198 |
#endif |
| 199 |
|
| 200 |
int numberCuts = cs.sizeRowCuts() - numberCutsBefore; |
| 201 |
assert(numberCuts); |
| 202 |
installCuts(*lp, cs, numberCuts); |
| 203 |
numberCutsBefore = cs.sizeRowCuts(); |
| 204 |
|
| 205 |
//check time |
| 206 |
if (CoinCpuTime() - timeBegin_ > parameters_.maxLocalSearchTime_){ |
| 207 |
colsol = NULL; |
| 208 |
break; |
| 209 |
} |
| 210 |
//do we perform a new local search ? |
| 211 |
if (nLocalSearch_ < parameters_.maxLocalSearch_ && |
| 212 |
numSols_ < parameters_.maxSols_) { |
| 213 |
|
| 214 |
nLocalSearch_++; |
| 215 |
set_fp_objective(*lp, nlp_->getColSolution()); |
| 216 |
|
| 217 |
lp->setColUpper(numcols, cutoff); |
| 218 |
|
| 219 |
subMip_->solve(DBL_MAX, parameters_.subMilpLogLevel_, |
| 220 |
// subMip_->optimize(DBL_MAX, parameters_.subMilpLogLevel_, |
| 221 |
parameters_.maxLocalSearchTime_ + timeBegin_ - CoinCpuTime()); |
| 222 |
milpOptimal = subMip_ -> optimal(); |
| 223 |
colsol = subMip_->getLastSolution(); |
| 224 |
if(milpOptimal) |
| 225 |
handler_->message(SOLVED_LOCAL_SEARCH, messages_)<<subMip_->nodeCount()<<subMip_->iterationCount()<<CoinMessageEol; |
| 226 |
else |
| 227 |
handler_->message(LOCAL_SEARCH_ABORT, messages_)<<subMip_->nodeCount()<<subMip_->iterationCount()<<CoinMessageEol; |
| 228 |
if(colsol) |
| 229 |
handler_->message(FP_MILP_VAL, messages_) |
| 230 |
<<colsol[nlp_->getNumCols()]<<CoinMessageEol; |
| 231 |
|
| 232 |
}/** endif localSearch*/ |
| 233 |
else { |
| 234 |
colsol = NULL; |
| 235 |
} |
| 236 |
} |
| 237 |
//return -DBL_MAX; |
| 238 |
if(!passBound_ || colsol || ! milpOptimal) |
| 239 |
return -DBL_MAX; |
| 240 |
else{ |
| 241 |
handler_->message(OASUCCESS, messages_)<<"FP"<<CoinCpuTime() - timeBegin_ |
| 242 |
<<ub<<CoinMessageEol; |
| 243 |
return DBL_MAX; |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
/** Register OA options.*/ |
| 248 |
void |
| 249 |
MinlpFeasPump::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions) |
| 250 |
{ |
| 251 |
roptions->SetRegisteringCategory("Primal Heuristics", RegisteredOptions::BonminCategory); |
| 252 |
|
| 253 |
roptions->AddStringOption2("fp_pass_infeasible", "Say whether feasibility pump should claim to converge or not", |
| 254 |
"no", |
| 255 |
"no", "When master MILP is infeasible just bail out (don't stop all algorithm). This is the option for using in B-Hyb.", |
| 256 |
"yes", "Claim convergence, numerically dangerous.",""); |
| 257 |
|
| 258 |
roptions->SetRegisteringCategory("Output and Loglevel", RegisteredOptions::BonminCategory); |
| 259 |
roptions->AddBoundedIntegerOption("fp_log_level", |
| 260 |
"specify FP iterations log level.", |
| 261 |
0,2,1, |
| 262 |
"Set the level of output of OA decomposition solver : " |
| 263 |
"0 - none, 1 - normal, 2 - verbose" |
| 264 |
); |
| 265 |
roptions->setOptionExtraInfo("fp_log_level",3); |
| 266 |
|
| 267 |
roptions->AddLowerBoundedNumberOption("fp_log_frequency", |
| 268 |
"display an update on lower and upper bounds in FP every n seconds", |
| 269 |
0.,1.,100., |
| 270 |
""); |
| 271 |
roptions->setOptionExtraInfo("fp_log_frequency",3); |
| 272 |
} |
| 273 |
|
| 274 |
/** Put objective of MIP according to FP scheme. */ |
| 275 |
void |
| 276 |
MinlpFeasPump::set_fp_objective(OsiSolverInterface &si, const double * colsol) const{ |
| 277 |
if (objects_) { |
| 278 |
for (int i = 0 ; i < nObjects_ ; i++) { |
| 279 |
OsiObject * obj = objects_[i]; |
| 280 |
int colnum = obj->columnNumber(); |
| 281 |
if (colnum >= 0) {//Variable branching object |
| 282 |
double round = floor(colsol[colnum] + 0.5); |
| 283 |
double coeff = (colsol[colnum] - round ) < 0; |
| 284 |
si.setObjCoeff(colnum, 1 - 2 * coeff); |
| 285 |
} |
| 286 |
else { |
| 287 |
throw CoinError("OaDecompositionBase::solverManip", |
| 288 |
"setFpObjective", |
| 289 |
"Can not use FP on problem with SOS constraints"); |
| 290 |
} |
| 291 |
} |
| 292 |
} |
| 293 |
else { |
| 294 |
int numcols = nlp_->getNumCols(); |
| 295 |
for (int i = 0; i < numcols ; i++) { |
| 296 |
if (nlp_->isInteger(i)){ |
| 297 |
double round = floor(colsol[i] + 0.5); |
| 298 |
double coeff = (colsol[i] - round ) < 0; |
| 299 |
si.setObjCoeff(i, 1 - 2*coeff); |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
si.initialSolve(); |
| 304 |
} |
| 305 |
|
| 306 |
}/* End namespace Bonmin. */ |