| 1 |
// (C) Copyright CNRS |
| 2 |
// This code is published under the Eclipse Public License. |
| 3 |
// |
| 4 |
// Authors : |
| 5 |
// Pierre Bonami, LIF Université de la Méditérannée-CNRS |
| 6 |
// |
| 7 |
// Date : 06/18/2008 |
| 8 |
|
| 9 |
#include "BonFixAndSolveHeuristic.hpp" |
| 10 |
#include "CbcModel.hpp" |
| 11 |
#include "OsiBranchingObject.hpp" |
| 12 |
|
| 13 |
namespace Bonmin { |
| 14 |
|
| 15 |
/** Default constructor*/ |
| 16 |
FixAndSolveHeuristic::FixAndSolveHeuristic(): |
| 17 |
LocalSolverBasedHeuristic() |
| 18 |
{ |
| 19 |
} |
| 20 |
/** Constructor with setup.*/ |
| 21 |
FixAndSolveHeuristic::FixAndSolveHeuristic(BonminSetup * setup): |
| 22 |
LocalSolverBasedHeuristic(setup) |
| 23 |
{ |
| 24 |
} |
| 25 |
|
| 26 |
/** Copy constructor.*/ |
| 27 |
FixAndSolveHeuristic::FixAndSolveHeuristic |
| 28 |
(const FixAndSolveHeuristic &other): |
| 29 |
LocalSolverBasedHeuristic(other){ |
| 30 |
} |
| 31 |
|
| 32 |
FixAndSolveHeuristic::~FixAndSolveHeuristic(){ |
| 33 |
} |
| 34 |
|
| 35 |
/** Runs heuristic*/ |
| 36 |
int |
| 37 |
FixAndSolveHeuristic::solution(double & objectiveValue, |
| 38 |
double * newSolution){ |
| 39 |
//if(model_->getNodeCount() || model_->getCurrentPassNumber() > 1) return 0; |
| 40 |
if(model_->getSolutionCount() > 0) return 0; |
| 41 |
if(model_->getNodeCount() > 1000) return 0; |
| 42 |
if(model_->getNodeCount() % 100 != 0) return 0; |
| 43 |
int numberObjects = model_->numberObjects(); |
| 44 |
OsiObject ** objects = model_->objects(); |
| 45 |
|
| 46 |
OsiTMINLPInterface * nlp = dynamic_cast<OsiTMINLPInterface *> |
| 47 |
(model_->solver()); |
| 48 |
if(nlp == NULL){ |
| 49 |
nlp = dynamic_cast<OsiTMINLPInterface *> |
| 50 |
(setup_->nonlinearSolver()->clone()); |
| 51 |
} |
| 52 |
else { |
| 53 |
nlp = dynamic_cast<OsiTMINLPInterface *>(nlp->clone()); |
| 54 |
} |
| 55 |
|
| 56 |
OsiBranchingInformation info = model_->usefulInformation(); |
| 57 |
info.solution_ = model_->getColSolution(); |
| 58 |
|
| 59 |
int dummy; |
| 60 |
int nFixed = 0; |
| 61 |
for(int i = 0 ; i < numberObjects; i++){ |
| 62 |
if(objects[i]->infeasibility(&info, dummy) == 0.){ |
| 63 |
objects[i]->feasibleRegion(nlp, &info); |
| 64 |
nFixed ++; |
| 65 |
} |
| 66 |
} |
| 67 |
if(nFixed < numberObjects / 3) return 0; |
| 68 |
double cutoff = info.cutoff_; |
| 69 |
int r_val = doLocalSearch(nlp, newSolution, objectiveValue, cutoff); |
| 70 |
delete nlp; |
| 71 |
return r_val; |
| 72 |
} |
| 73 |
|
| 74 |
void |
| 75 |
FixAndSolveHeuristic::registerOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions){ |
| 76 |
roptions->SetRegisteringCategory("Primal Heuristics (undocumented)", RegisteredOptions::UndocumentedCategory); |
| 77 |
roptions->AddStringOption2( |
| 78 |
"fix_and_solve_heuristic", |
| 79 |
"if yes runs a heuristic at root where fixes all variables integer in the continuous solution", |
| 80 |
"no", |
| 81 |
"no", "don't run it", |
| 82 |
"yes", "runs the heuristic", |
| 83 |
""); |
| 84 |
roptions->setOptionExtraInfo("fix_and_solve_heuristic", 63); |
| 85 |
} |
| 86 |
|
| 87 |
/** Initiaize using passed options.*/ |
| 88 |
void |
| 89 |
FixAndSolveHeuristic::Initialize(Ipopt::SmartPtr<Ipopt::OptionsList> options){ |
| 90 |
} |
| 91 |
}/* ends bonmin namespace*/ |