| 1 |
// (C) Copyright International Business Machines (IBM) 2006 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// P. Bonami, International Business Machines |
| 7 |
// |
| 8 |
// Date : 12/07/2006 |
| 9 |
#ifndef BonOaDecBase_HPP |
| 10 |
#define BonOaDecBase_HPP |
| 11 |
#include "BonSubMipSolver.hpp" |
| 12 |
#include "CglCutGenerator.hpp" |
| 13 |
#include "BonBabSetupBase.hpp" |
| 14 |
#include "BonOAMessages.hpp" |
| 15 |
#include "CbcModel.hpp" |
| 16 |
|
| 17 |
#include "CbcStrategy.hpp" |
| 18 |
|
| 19 |
#include "CoinTime.hpp" |
| 20 |
#include "OsiAuxInfo.hpp" |
| 21 |
#include "OsiBranchingObject.hpp" |
| 22 |
#include <iostream> |
| 23 |
#include "BonBabInfos.hpp" |
| 24 |
namespace Bonmin |
| 25 |
{ |
| 26 |
/** Base class for OA algorithms.*/ |
| 27 |
class OaDecompositionBase : public CglCutGenerator |
| 28 |
{ |
| 29 |
public: |
| 30 |
|
| 31 |
|
| 32 |
/** Small class to manipulatee various things in an OsiSolverInterface and restore them. |
| 33 |
The OsiSolverInterface manipulated may already exist or may be cloned from another one.*/ |
| 34 |
class solverManip |
| 35 |
{ |
| 36 |
public: |
| 37 |
/** Constructor. */ |
| 38 |
solverManip(OsiSolverInterface *si , bool saveNumRows=true, |
| 39 |
bool saveBasis=true, bool saveBounds=false, |
| 40 |
bool saveCutoff = false, bool resolve=true); |
| 41 |
|
| 42 |
/** Constructor which clone an other interface. */ |
| 43 |
solverManip(const OsiSolverInterface & si); |
| 44 |
/** Destructor. */ |
| 45 |
~solverManip(); |
| 46 |
/** Restore solver. */ |
| 47 |
void restore(); |
| 48 |
|
| 49 |
/** Get pointer to solver interface. */ |
| 50 |
OsiSolverInterface * si() |
| 51 |
{ |
| 52 |
return si_; |
| 53 |
} |
| 54 |
|
| 55 |
/** Set objects.*/ |
| 56 |
void setObjects(OsiObject ** objects, int nObjects) |
| 57 |
{ |
| 58 |
objects_ = objects; |
| 59 |
nObjects_ = nObjects; |
| 60 |
} |
| 61 |
|
| 62 |
private: |
| 63 |
/** Interface saved. */ |
| 64 |
OsiSolverInterface * si_; |
| 65 |
/** Initial number of rows (-1 if don't save). */ |
| 66 |
int initialNumberRows_; |
| 67 |
|
| 68 |
/** Initial lower bounds. */ |
| 69 |
double * colLower_; |
| 70 |
|
| 71 |
/** Initial Upper bounds.*/ |
| 72 |
double * colUpper_; |
| 73 |
|
| 74 |
/** Inital basis. */ |
| 75 |
CoinWarmStart * warm_; |
| 76 |
|
| 77 |
/** Initial cutoff. */ |
| 78 |
double cutoff_; |
| 79 |
|
| 80 |
/** delete si_ ? */ |
| 81 |
bool deleteSolver_; |
| 82 |
|
| 83 |
/// Some objects the feasiblitiy of which to verify. |
| 84 |
OsiObject * * objects_; |
| 85 |
/// Number of objects.*/ |
| 86 |
int nObjects_; |
| 87 |
/** \name Cached info from solver interface.*/ |
| 88 |
/** @{ */ |
| 89 |
/** Number of columns. */ |
| 90 |
int numcols_; |
| 91 |
/** Number of rows. */ |
| 92 |
int numrows_; |
| 93 |
/** Lower bounds on variables.*/ |
| 94 |
const double * siColLower_; |
| 95 |
/** Upper bounds on variables. */ |
| 96 |
const double * siColUpper_; |
| 97 |
|
| 98 |
void getCached(); |
| 99 |
/** @} */ |
| 100 |
}; |
| 101 |
|
| 102 |
/// New usefull constructor |
| 103 |
OaDecompositionBase(BabSetupBase &b, bool leaveSiUnchanged, |
| 104 |
bool reassignLpsolver); |
| 105 |
|
| 106 |
/// Copy constructor |
| 107 |
OaDecompositionBase(const OaDecompositionBase & copy); |
| 108 |
|
| 109 |
|
| 110 |
/// Destructor |
| 111 |
virtual ~OaDecompositionBase(); |
| 112 |
|
| 113 |
/** Standard cut generation methods. */ |
| 114 |
virtual void generateCuts(const OsiSolverInterface &si, OsiCuts & cs, |
| 115 |
const CglTreeInfo info = CglTreeInfo()); |
| 116 |
|
| 117 |
/// Assign an OsiTMINLPInterface |
| 118 |
void assignNlpInterface(OsiTMINLPInterface * nlp) |
| 119 |
{ |
| 120 |
nlp_ = nlp; |
| 121 |
} |
| 122 |
|
| 123 |
/// Assign an OsiTMINLPInterface |
| 124 |
void assignLpInterface(OsiSolverInterface * si) |
| 125 |
{ |
| 126 |
lp_ = si; |
| 127 |
} |
| 128 |
|
| 129 |
bool reassignLpsolver() |
| 130 |
{ |
| 131 |
return reassignLpsolver_; |
| 132 |
} |
| 133 |
/** Set objects.*/ |
| 134 |
void setObjects(OsiObject ** objects, int nObjects) |
| 135 |
{ |
| 136 |
objects_ = objects; |
| 137 |
nObjects_ = nObjects; |
| 138 |
} |
| 139 |
/// Set whether to leave the solverinterface unchanged |
| 140 |
inline void setLeaveSiUnchanged(bool yesno) |
| 141 |
{ |
| 142 |
leaveSiUnchanged_ = yesno; |
| 143 |
} |
| 144 |
|
| 145 |
/** Parameters for algorithm. */ |
| 146 |
struct Parameters |
| 147 |
{ |
| 148 |
/// Add cuts as global |
| 149 |
bool global_; |
| 150 |
/// Add only violated OA inequalities |
| 151 |
bool addOnlyViolated_; |
| 152 |
/// cutoff min increase (has to be intialized trhough Cbc) |
| 153 |
double cbcCutoffIncrement_; |
| 154 |
/// integer tolerance (has to be the same as Cbc's) |
| 155 |
double cbcIntegerTolerance_; |
| 156 |
/** setting for gap tolerance.*/ |
| 157 |
double gap_tol_; |
| 158 |
///Total max number of local searches |
| 159 |
int maxLocalSearch_; |
| 160 |
/// maximum time for local searches |
| 161 |
double maxLocalSearchTime_; |
| 162 |
/** sub milp log level.*/ |
| 163 |
int subMilpLogLevel_; |
| 164 |
/** maximum number of solutions*/ |
| 165 |
int maxSols_; |
| 166 |
/** Frequency of log. */ |
| 167 |
double logFrequency_; |
| 168 |
|
| 169 |
|
| 170 |
/** Constructor with default values */ |
| 171 |
Parameters(); |
| 172 |
|
| 173 |
/** Copy constructor */ |
| 174 |
Parameters(const Parameters & other); |
| 175 |
|
| 176 |
/** Destructor */ |
| 177 |
~Parameters() |
| 178 |
{ |
| 179 |
if (strategy_) delete strategy_; |
| 180 |
} |
| 181 |
|
| 182 |
/** Strategy to apply when using Cbc as MILP sub-solver.*/ |
| 183 |
void setStrategy(const CbcStrategy & strategy) |
| 184 |
{ |
| 185 |
if (strategy_) delete strategy_; |
| 186 |
strategy_ = strategy.clone(); |
| 187 |
} |
| 188 |
|
| 189 |
const CbcStrategy * strategy() const |
| 190 |
{ |
| 191 |
return strategy_; |
| 192 |
} |
| 193 |
|
| 194 |
private: |
| 195 |
/** Strategy to apply when using Cbc as MILP sub-solver.*/ |
| 196 |
CbcStrategy * strategy_; |
| 197 |
|
| 198 |
}; |
| 199 |
|
| 200 |
Parameters& parameter() |
| 201 |
{ |
| 202 |
return parameters_; |
| 203 |
} |
| 204 |
|
| 205 |
const Parameters& parameter()const |
| 206 |
{ |
| 207 |
return parameters_; |
| 208 |
} |
| 209 |
|
| 210 |
void setLogLevel(int level) |
| 211 |
{ |
| 212 |
handler_->setLogLevel(level); |
| 213 |
} |
| 214 |
|
| 215 |
void setReassignLpSolver(bool v){ |
| 216 |
reassignLpsolver_ = v; |
| 217 |
} |
| 218 |
void passInMessageHandler(CoinMessageHandler * handler); |
| 219 |
protected: |
| 220 |
void setupMipSolver(BabSetupBase &b, const std::string &prefix); |
| 221 |
/// \name Protected helper functions |
| 222 |
/**@{ */ |
| 223 |
|
| 224 |
/** Solve the nlp and do output. |
| 225 |
\return true if feasible*/ |
| 226 |
bool post_nlp_solve(BabInfo * babInfo, double cutoff) const; |
| 227 |
/** @} */ |
| 228 |
|
| 229 |
/// virtual method which performs the OA algorithm by modifying lp and nlp. |
| 230 |
virtual double performOa(OsiCuts &cs, solverManip &lpManip, |
| 231 |
BabInfo * babInfo, double &, const CglTreeInfo & info) const = 0; |
| 232 |
/// virutal method to decide if local search is performed |
| 233 |
virtual bool doLocalSearch(BabInfo * babInfo) const = 0; |
| 234 |
|
| 235 |
/// \name Protected members |
| 236 |
/** @{ */ |
| 237 |
/// Pointer to nlp interface |
| 238 |
mutable OsiTMINLPInterface * nlp_; |
| 239 |
/// Pointer to setup |
| 240 |
BabSetupBase * s_; |
| 241 |
///Number of nlp solved done |
| 242 |
mutable int nSolve_; |
| 243 |
/// A linear solver |
| 244 |
mutable OsiSolverInterface * lp_; |
| 245 |
/// Some objects the feasiblitiy of which to verify. |
| 246 |
OsiObject * * objects_; |
| 247 |
/// Number of objects.*/ |
| 248 |
int nObjects_; |
| 249 |
///number of local searches performed |
| 250 |
mutable int nLocalSearch_; |
| 251 |
/** messages handler. */ |
| 252 |
CoinMessageHandler * handler_; |
| 253 |
/** Messages for OA */ |
| 254 |
CoinMessages messages_; |
| 255 |
/** Wether or not we should remove cuts at the end of the procedure */ |
| 256 |
bool leaveSiUnchanged_; |
| 257 |
/** Do we need to reassign the lp solver with Cbc.*/ |
| 258 |
bool reassignLpsolver_; |
| 259 |
/** time of construction*/ |
| 260 |
double timeBegin_; |
| 261 |
/** number of solutions found by OA_decomposition.*/ |
| 262 |
mutable int numSols_; |
| 263 |
|
| 264 |
/** Parameters.*/ |
| 265 |
Parameters parameters_; |
| 266 |
|
| 267 |
/** Saved cuts: in some cases when using OA to check feasible solution algorithm may loop because Cbc removes inactive cuts. |
| 268 |
To overcome this we can impose that no OA cut can be discarded by Cbc but this consumes too much memory in some cases. |
| 269 |
Here we do it another way: cuts generated at current node are saved if algorithm seems to enter a loop we impose the needed cuts to be kept.*/ |
| 270 |
mutable OsiCuts savedCuts_; |
| 271 |
/** Store the current node number.*/ |
| 272 |
mutable int currentNodeNumber_; |
| 273 |
/** @} */ |
| 274 |
|
| 275 |
#ifdef OA_DEBUG |
| 276 |
class OaDebug |
| 277 |
{ |
| 278 |
public: |
| 279 |
bool checkInteger(const OsiSolverInterface&nlp, std::ostream & os) const; |
| 280 |
|
| 281 |
void printEndOfProcedureDebugMessage(const OsiCuts &cs, |
| 282 |
bool foundSolution, |
| 283 |
double solValue, |
| 284 |
double milpBound, |
| 285 |
bool isInteger, |
| 286 |
bool feasible, |
| 287 |
std::ostream & os) const; |
| 288 |
}; |
| 289 |
|
| 290 |
/** debug object. */ |
| 291 |
OaDebug debug_; |
| 292 |
|
| 293 |
#endif |
| 294 |
}; |
| 295 |
} |
| 296 |
#endif |
| 297 |
|