| 1 |
// (C) Copyright CNRS and others 2010 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Pierre Bonami, Université de la Méditérannée |
| 7 |
// |
| 8 |
// Date : 05/22/2010 |
| 9 |
|
| 10 |
#include <climits> |
| 11 |
|
| 12 |
#include "BonminConfig.h" |
| 13 |
#include "BonStrongBranchingSolver.hpp" |
| 14 |
|
| 15 |
#include "RobotSetup.hpp" |
| 16 |
#include "BonNWayObject.hpp" |
| 17 |
#include "BonNWayChoose.hpp" |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace Bonmin |
| 22 |
{ |
| 23 |
RobotSetup::RobotSetup(const CoinMessageHandler * handler):BonminSetup(handler) |
| 24 |
{} |
| 25 |
|
| 26 |
RobotSetup::RobotSetup(const RobotSetup &other):BonminSetup(other) |
| 27 |
{} |
| 28 |
|
| 29 |
RobotSetup::RobotSetup(const RobotSetup &other, |
| 30 |
OsiTMINLPInterface &nlp): |
| 31 |
BonminSetup(other, nlp) |
| 32 |
{ |
| 33 |
} |
| 34 |
|
| 35 |
RobotSetup::RobotSetup(const RobotSetup &other, |
| 36 |
OsiTMINLPInterface &nlp, |
| 37 |
const std::string &prefix): |
| 38 |
BonminSetup(other, nlp, prefix) |
| 39 |
{ |
| 40 |
initializeRobot(); |
| 41 |
} |
| 42 |
|
| 43 |
void RobotSetup::registerAllOptions(Ipopt::SmartPtr<Bonmin::RegisteredOptions> roptions) |
| 44 |
{ |
| 45 |
BonminSetup::registerAllOptions(roptions); |
| 46 |
BonNWayChoose::registerOptions(roptions); |
| 47 |
|
| 48 |
|
| 49 |
roptions->AddLowerBoundedIntegerOption("branch_on_frac_only", |
| 50 |
"Starting at given depth branch on the subset of fractional variables (and set the last branch that one of them is 1)", |
| 51 |
0,INT_MAX,""); |
| 52 |
|
| 53 |
roptions->AddStringOption2("do_a_quick_one", |
| 54 |
"Do we try our luck?", |
| 55 |
"no", |
| 56 |
"no", "Don't (of course).", |
| 57 |
"yes", "Be crazy", |
| 58 |
""); |
| 59 |
|
| 60 |
} |
| 61 |
|
| 62 |
/** Register all the Bonmin options.*/ |
| 63 |
void |
| 64 |
RobotSetup::registerOptions() |
| 65 |
{ |
| 66 |
registerAllOptions(roptions_); |
| 67 |
} |
| 68 |
|
| 69 |
/** Initialize, read options and create appropriate bonmin setup using initialized tminlp.*/ |
| 70 |
void |
| 71 |
RobotSetup::initialize(Ipopt::SmartPtr<TMINLP> tminlp, bool createContinuousSolver /*= false*/) |
| 72 |
{ |
| 73 |
BonminSetup::initialize(tminlp,createContinuousSolver); |
| 74 |
initializeRobot(); |
| 75 |
} |
| 76 |
|
| 77 |
/** Initialize, read options and create appropriate bonmin setup using initialized tminlp.*/ |
| 78 |
void |
| 79 |
RobotSetup::initialize(const OsiTMINLPInterface &nlpSi, bool createContinuousSolver /*= false*/) |
| 80 |
{ |
| 81 |
BonminSetup::initialize(nlpSi,createContinuousSolver); |
| 82 |
initializeRobot(); |
| 83 |
} |
| 84 |
|
| 85 |
void RobotSetup::initializeRobot() |
| 86 |
{ |
| 87 |
assert(continuousSolver_ == nonlinearSolver_); |
| 88 |
|
| 89 |
delete branchingMethod_; |
| 90 |
|
| 91 |
continuousSolver_->deleteObjects(); |
| 92 |
continuousSolver_->findIntegersAndSOS(false); |
| 93 |
setPriorities(); |
| 94 |
addNWays(); |
| 95 |
Ipopt::SmartPtr<StrongBranchingSolver> strong_solver = NULL; |
| 96 |
nonlinearSolver_->SetStrongBrachingSolver(strong_solver); |
| 97 |
BonNWayChoose* chooseVariable = new BonNWayChoose(*this, |
| 98 |
nonlinearSolver_); |
| 99 |
branchingMethod_ = chooseVariable; |
| 100 |
branchingMethod_->setNumberStrong(intParam_[NumberStrong]); |
| 101 |
|
| 102 |
} |
| 103 |
|
| 104 |
void |
| 105 |
RobotSetup::addNWays() |
| 106 |
{ |
| 107 |
|
| 108 |
int do_quick; |
| 109 |
options()->GetEnumValue("do_a_quick_one", do_quick, prefix()); |
| 110 |
int depth_frac; |
| 111 |
options()->GetIntegerValue("branch_on_frac_only", depth_frac, prefix()); |
| 112 |
|
| 113 |
// pass user set Sos constraints (code inspired from CoinSolve.cpp) |
| 114 |
const TMINLP::SosInfo * sos = nonlinearSolver()->model()->sosConstraints(); |
| 115 |
if (!getIntParameter(BabSetupBase::DisableSos) && sos && sos->num > 0) //we have some sos constraints |
| 116 |
{ |
| 117 |
const int & numSos = sos->num; |
| 118 |
OsiObject ** objects = new OsiObject*[numSos]; |
| 119 |
const int * starts = sos->starts; |
| 120 |
const int * indices = sos->indices; |
| 121 |
//const char * types = sos->types; |
| 122 |
const double * weights = sos->weights; |
| 123 |
bool hasPriorities = false; |
| 124 |
const int * varPriorities = nonlinearSolver()->getPriorities(); |
| 125 |
int numberObjects = nonlinearSolver()->numberObjects(); |
| 126 |
if (varPriorities) |
| 127 |
{ |
| 128 |
for (int i = 0 ; i < numberObjects ; i++) { |
| 129 |
if (varPriorities[i]) { |
| 130 |
hasPriorities = true; |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
} |
| 135 |
const int * sosPriorities = sos->priorities; |
| 136 |
if (sosPriorities) |
| 137 |
{ |
| 138 |
for (int i = 0 ; i < numSos ; i++) { |
| 139 |
if (sosPriorities[i]) { |
| 140 |
hasPriorities = true; |
| 141 |
break; |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
std::vector<std::list<int> > groups(numSos + 1); |
| 147 |
|
| 148 |
for (int i = 0 ; i < numSos ; i++) |
| 149 |
{ |
| 150 |
int start = starts[i]; |
| 151 |
int length = starts[i + 1] - start; |
| 152 |
for(int j = 0 ; j < length ; j++){ |
| 153 |
groups[(size_t) weights[j]].push_back(indices[start+j]); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
for (int i = 0 ; i < numSos ; i++) |
| 158 |
{ |
| 159 |
int start = starts[i]; |
| 160 |
int length = starts[i + 1] - start; |
| 161 |
BonNWayObject * nway = new BonNWayObject(length, &indices[start],i); |
| 162 |
nway->setPriority(1); |
| 163 |
for(int j = 0 ; j < length ; j++){//Setup consequences |
| 164 |
n_way_consequences cons; |
| 165 |
std::vector<int>& ids = cons.indices; |
| 166 |
int idx = (int) weights[j]; |
| 167 |
const std::list<int> &to_add = groups[idx]; |
| 168 |
for(std::list<int>::const_iterator k = to_add.begin() ; |
| 169 |
k != to_add.end() ; k++){ |
| 170 |
if(*k != indices[start+j]) ids.push_back(*k); |
| 171 |
} |
| 172 |
nway->setConsequence(j, cons); |
| 173 |
} |
| 174 |
objects[i] = nway; |
| 175 |
|
| 176 |
if(do_quick) |
| 177 |
nway->make_quick(); |
| 178 |
nway->set_only_frac_branches(depth_frac); |
| 179 |
if (hasPriorities && sosPriorities && sosPriorities[i]) { |
| 180 |
objects[i]->setPriority(sosPriorities[i]); |
| 181 |
} |
| 182 |
} |
| 183 |
nonlinearSolver()->addObjects(numSos, objects); |
| 184 |
for (int i = 0 ; i < numSos ; i++) |
| 185 |
delete objects[i]; |
| 186 |
delete [] objects; |
| 187 |
} |
| 188 |
} |
| 189 |
}/* end namespace Bonmin*/ |
| 190 |
|