| 1 |
// (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2006, 2007 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// Laszlo Ladanyi, International Business Machines Corporation |
| 7 |
|
| 8 |
#include "BM.hpp" |
| 9 |
#include "BCP_lp.hpp" |
| 10 |
|
| 11 |
#include "BonAmplSetup.hpp" |
| 12 |
|
| 13 |
//############################################################################# |
| 14 |
|
| 15 |
void |
| 16 |
BM_tm::pack_module_data(BCP_buffer& buf, BCP_process_t ptype) |
| 17 |
{ |
| 18 |
// possible process types looked up in BCP_enum_process_t.hpp |
| 19 |
switch (ptype) { |
| 20 |
case BCP_ProcessType_LP: |
| 21 |
par.pack(buf); |
| 22 |
buf.pack(nl_file_content); |
| 23 |
buf.pack(ipopt_file_content); |
| 24 |
break; |
| 25 |
case BCP_ProcessType_TS: |
| 26 |
break; |
| 27 |
default: |
| 28 |
abort(); |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
/****************************************************************************/ |
| 33 |
|
| 34 |
void |
| 35 |
BM_lp::unpack_module_data(BCP_buffer& buf) |
| 36 |
{ |
| 37 |
using namespace Bonmin; |
| 38 |
|
| 39 |
par.unpack(buf); |
| 40 |
buf.unpack(nl_file_content); |
| 41 |
buf.unpack(ipopt_file_content); |
| 42 |
|
| 43 |
char* argv_[3]; |
| 44 |
char** argv = argv_; |
| 45 |
argv[0] = NULL; |
| 46 |
argv[1] = strdup("dont_even_try_to_open_it.nl"); |
| 47 |
argv[2] = NULL; |
| 48 |
std::string ipopt_content(ipopt_file_content.c_str()); |
| 49 |
std::string nl_content(nl_file_content.c_str()); |
| 50 |
|
| 51 |
/* PIERRE create the setup for algorithm, last argument indicates that |
| 52 |
continuous relaxation should not be created.*/ |
| 53 |
bonmin_.initialize(argv, ipopt_content, nl_content, false); |
| 54 |
bonmin_.nonlinearSolver()->setExposeWarmStart(true); |
| 55 |
|
| 56 |
free(argv[1]); |
| 57 |
|
| 58 |
/* synchronize bonmin & BCP parameters */ |
| 59 |
Ipopt::SmartPtr<Ipopt::OptionsList> options = bonmin_.options(); |
| 60 |
|
| 61 |
/** update getting options directly from setup and store them in vars |
| 62 |
local to the BM_lp object */ |
| 63 |
integerTolerance_ = bonmin_.getDoubleParameter(BabSetupBase::IntTol); |
| 64 |
// cutOffDecrement_ could be negative |
| 65 |
double cutOffDecrement = |
| 66 |
bonmin_.getDoubleParameter(BabSetupBase::CutoffDecr); |
| 67 |
|
| 68 |
BCP_lp_prob* bcp_lp = getLpProblemPointer(); |
| 69 |
const double bcp_intTol = bcp_lp->par.entry(BCP_lp_par::IntegerTolerance); |
| 70 |
const double bcp_cutoffIncr = bcp_lp->par.entry(BCP_lp_par::Granularity); |
| 71 |
|
| 72 |
if (fabs(integerTolerance_ - bcp_intTol) > 1e-10) { |
| 73 |
printf("WARNING!\n"); |
| 74 |
printf(" The integrality tolerance parameters are different for\n"); |
| 75 |
printf(" BCP (%f) and bonmin (%f). They should be identical.\n", |
| 76 |
bcp_intTol, integerTolerance_); |
| 77 |
printf(" For now both will be set to that of bonmin.\n"); |
| 78 |
} |
| 79 |
if (fabs(bcp_cutoffIncr - cutOffDecrement) > 1e-10) { |
| 80 |
printf("WARNING!\n"); |
| 81 |
printf(" The granularity (cutoff increment) parameters are different\n"); |
| 82 |
printf(" BCP (%f) and bonmin (%f). They should be identical.\n", |
| 83 |
bcp_cutoffIncr, cutOffDecrement); |
| 84 |
printf(" For now both will be set to that of bonmin.\n"); |
| 85 |
} |
| 86 |
bcp_lp->par.set_entry(BCP_lp_par::IntegerTolerance, integerTolerance_); |
| 87 |
bcp_lp->par.set_entry(BCP_lp_par::Granularity, cutOffDecrement); |
| 88 |
|
| 89 |
/* If pure BB is selected then a number of BCP parameters are changed */ |
| 90 |
if (bonmin_.getAlgorithm() == 0 /* pure B&B */) { |
| 91 |
/* disable strong branching */ |
| 92 |
bcp_lp->par.set_entry(BCP_lp_par::MaxPresolveIter, -1); |
| 93 |
/* disable a bunch of printing, all of which are meaningless, since the |
| 94 |
LP relaxation is meaningless */ |
| 95 |
bcp_lp->par.set_entry(BCP_lp_par::LpVerb_ReportLocalCutPoolSize, false); |
| 96 |
bcp_lp->par.set_entry(BCP_lp_par::LpVerb_ReportLocalVarPoolSize, false); |
| 97 |
bcp_lp->par.set_entry(BCP_lp_par::LpVerb_GeneratedCutCount, false); |
| 98 |
bcp_lp->par.set_entry(BCP_lp_par::LpVerb_GeneratedVarCount, false); |
| 99 |
bcp_lp->par.set_entry(BCP_lp_par::LpVerb_RowEffectivenessCount, false); |
| 100 |
} |
| 101 |
|
| 102 |
/* extract the sos constraints */ |
| 103 |
Bonmin::OsiTMINLPInterface& nlp = *bonmin_.nonlinearSolver(); |
| 104 |
const Bonmin::TMINLP::SosInfo * sos = nlp.model()->sosConstraints(); |
| 105 |
|
| 106 |
int i; |
| 107 |
const int numCols = nlp.getNumCols(); |
| 108 |
const double* clb = nlp.getColLower(); |
| 109 |
const double* cub = nlp.getColUpper(); |
| 110 |
const int* cPrio = nlp.getPriorities(); |
| 111 |
|
| 112 |
/* Find first the integer variables and then the SOS constraints */ |
| 113 |
int nObj = 0; |
| 114 |
OsiObject** osiObj = new OsiObject*[numCols + sos->num]; |
| 115 |
for (i = 0; i < numCols; ++i) { |
| 116 |
if (nlp.isInteger(i)) { |
| 117 |
osiObj[nObj] = new OsiSimpleInteger(i, clb[i], cub[i]); |
| 118 |
if (cPrio) { |
| 119 |
osiObj[nObj]->setPriority(cPrio[i]); |
| 120 |
} |
| 121 |
++nObj; |
| 122 |
} |
| 123 |
} |
| 124 |
#if ! defined(BM_DISREGARD_SOS) |
| 125 |
const int* starts = sos->starts; |
| 126 |
for (i = 0; i < sos->num; ++i) { |
| 127 |
OsiSOS* so = new OsiSOS(NULL, /* FIXME: why does the constr need */ |
| 128 |
starts[i+1] - starts[i], |
| 129 |
sos->indices + starts[i], |
| 130 |
sos->weights + starts[i], |
| 131 |
sos->types[i]); |
| 132 |
so->setPriority(sos->priorities ? sos->priorities[i] : 1); |
| 133 |
osiObj[nObj++] = so; |
| 134 |
} |
| 135 |
#endif |
| 136 |
nlp.addObjects(nObj, osiObj); |
| 137 |
|
| 138 |
objNum_ = nObj; |
| 139 |
/* Allocate the storage arrays */ |
| 140 |
infInd_ = new int[objNum_]; |
| 141 |
infUseful_ = new double[objNum_]; |
| 142 |
feasInd_ = new int[objNum_]; |
| 143 |
feasUseful_ = new double[objNum_]; |
| 144 |
sbResult_ = new BM_SB_result[objNum_]; |
| 145 |
|
| 146 |
/* Sort the objects based on their priority */ |
| 147 |
int* prio = new int[objNum_]; |
| 148 |
objInd_ = new int[objNum_]; |
| 149 |
for (i = 0; i < objNum_; ++i) { |
| 150 |
sbResult_[i].colInd = osiObj[i]->columnNumber(); |
| 151 |
objInd_[i] = i; |
| 152 |
prio[i] = osiObj[i]->priority(); |
| 153 |
} |
| 154 |
CoinSort_2(prio, prio+objNum_, objInd_); |
| 155 |
delete[] prio; |
| 156 |
|
| 157 |
for (i = 0; i < nObj; ++i) { |
| 158 |
delete osiObj[i]; |
| 159 |
} |
| 160 |
delete[] osiObj; |
| 161 |
} |
| 162 |
|
| 163 |
//############################################################################# |
| 164 |
|
| 165 |
void |
| 166 |
BM_pack::pack_user_data(const BCP_user_data* ud, BCP_buffer& buf) |
| 167 |
{ |
| 168 |
const BM_node* data = dynamic_cast<const BM_node*>(ud); |
| 169 |
data->pack(buf); |
| 170 |
BM_tm* tm = dynamic_cast<BM_tm*>(user_class); |
| 171 |
if (tm) { |
| 172 |
tm->pack_pseudo_costs(buf); |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
/*---------------------------------------------------------------------------*/ |
| 177 |
|
| 178 |
BCP_user_data* |
| 179 |
BM_pack::unpack_user_data(BCP_buffer& buf) |
| 180 |
{ |
| 181 |
BM_node* node = new BM_node(buf); |
| 182 |
BM_lp* lp = dynamic_cast<BM_lp*>(user_class); |
| 183 |
if (lp) { |
| 184 |
lp->unpack_pseudo_costs(buf); |
| 185 |
} |
| 186 |
return node; |
| 187 |
} |
| 188 |
|
| 189 |
/*---------------------------------------------------------------------------*/ |
| 190 |
|
| 191 |
void |
| 192 |
BM_pack::pack_cut_algo(const BCP_cut_algo* cut, BCP_buffer& buf) |
| 193 |
{ |
| 194 |
const BB_cut* bb_cut = dynamic_cast<const BB_cut*>(cut); |
| 195 |
if (!bb_cut) |
| 196 |
throw BCP_fatal_error("pack_cut_algo() : unknown cut type!\n"); |
| 197 |
bb_cut->pack(buf); |
| 198 |
} |
| 199 |
|
| 200 |
/*---------------------------------------------------------------------------*/ |
| 201 |
BCP_cut_algo* BM_pack::unpack_cut_algo(BCP_buffer& buf) |
| 202 |
{ |
| 203 |
return new BB_cut(buf); |
| 204 |
} |