| 1 |
// (C) Copyright International Business Machines Corporation and Carnegie Mellon University 2006 |
| 2 |
// All Rights Reserved. |
| 3 |
// This code is published under the Eclipse Public License. |
| 4 |
// |
| 5 |
// Authors : |
| 6 |
// John J. Forrest, International Business Machines Corporation |
| 7 |
// P. Bonami, Carnegie Mellon University, |
| 8 |
// |
| 9 |
// Date : 03/15/2006 |
| 10 |
|
| 11 |
#include <cassert> |
| 12 |
#include <cmath> |
| 13 |
#include <cfloat> |
| 14 |
|
| 15 |
#include "CoinPragma.hpp" |
| 16 |
#include "OsiSolverInterface.hpp" |
| 17 |
#include "CbcModel.hpp" |
| 18 |
#include "BonCbcNlpStrategy.hpp" |
| 19 |
#include "BonCbcNode.hpp" |
| 20 |
|
| 21 |
#include "BonOsiTMINLPInterface.hpp" |
| 22 |
namespace Bonmin |
| 23 |
{ |
| 24 |
// Default Constructor |
| 25 |
CbcNlpStrategy::CbcNlpStrategy(int maxFailures, |
| 26 |
int maxInfeasibles, |
| 27 |
int pretendFailIsInfeasible) |
| 28 |
: |
| 29 |
hasFailed_(false), |
| 30 |
maxFailure_(maxFailures), |
| 31 |
maxInfeasible_(maxInfeasibles), |
| 32 |
pretendFailIsInfeasible_(pretendFailIsInfeasible) |
| 33 |
{ |
| 34 |
setPreProcessState(0); |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
// Destructor |
| 39 |
CbcNlpStrategy::~CbcNlpStrategy () |
| 40 |
{} |
| 41 |
|
| 42 |
// Clone |
| 43 |
CbcStrategy * |
| 44 |
CbcNlpStrategy::clone() const |
| 45 |
{ |
| 46 |
return new CbcNlpStrategy(*this); |
| 47 |
} |
| 48 |
|
| 49 |
// Copy constructor |
| 50 |
CbcNlpStrategy::CbcNlpStrategy(const CbcNlpStrategy & rhs) |
| 51 |
: |
| 52 |
hasFailed_(false), |
| 53 |
maxFailure_(rhs.maxFailure_), |
| 54 |
maxInfeasible_(rhs.maxInfeasible_), |
| 55 |
pretendFailIsInfeasible_(rhs.pretendFailIsInfeasible_) |
| 56 |
{} |
| 57 |
// Return a new Full node information pointer (descendant of CbcFullNodeInfo) |
| 58 |
CbcNodeInfo * |
| 59 |
CbcNlpStrategy::fullNodeInfo(CbcModel * model,int numberRowsAtContinuous) const |
| 60 |
{ |
| 61 |
return new CbcFullNodeInfo(model,numberRowsAtContinuous); |
| 62 |
} |
| 63 |
// Return a new Partial node information pointer (descendant of CbcPartialNodeInfo) |
| 64 |
CbcNodeInfo * |
| 65 |
CbcNlpStrategy::partialNodeInfo(CbcModel * model, CbcNodeInfo * parent, CbcNode * owner, |
| 66 |
int numberChangedBounds,const int * variables, |
| 67 |
const double * boundChanges, |
| 68 |
const CoinWarmStartDiff *basisDiff) const |
| 69 |
{ |
| 70 |
return new BonCbcPartialNodeInfo(model,parent, owner, numberChangedBounds, variables, |
| 71 |
boundChanges,basisDiff); |
| 72 |
} |
| 73 |
/* After a CbcModel::resolve this can return a status |
| 74 |
-1 no effect |
| 75 |
0 treat as optimal |
| 76 |
1 as 0 but do not do any more resolves (i.e. no more cuts) |
| 77 |
2 treat as infeasible |
| 78 |
*/ |
| 79 |
int |
| 80 |
CbcNlpStrategy::status(CbcModel * model, CbcNodeInfo * parent,int whereFrom) |
| 81 |
{ |
| 82 |
|
| 83 |
OsiSolverInterface * solver = model->solver();//get solver |
| 84 |
int feasible = 1; |
| 85 |
bool solved = true; |
| 86 |
int returnStatus = -1; |
| 87 |
BonCbcPartialNodeInfo * bmNodeInfo = dynamic_cast<BonCbcPartialNodeInfo *>(parent); |
| 88 |
if (!bmNodeInfo) return -1; |
| 89 |
|
| 90 |
int seqOfInfeasiblesSize = bmNodeInfo->getSequenceOfInfeasiblesSize(); |
| 91 |
int seqOfUnsolvedSize = bmNodeInfo->getSequenceOfUnsolvedSize(); |
| 92 |
|
| 93 |
|
| 94 |
if (solver->isAbandoned()) { |
| 95 |
solved = false; |
| 96 |
seqOfUnsolvedSize++; |
| 97 |
; |
| 98 |
} |
| 99 |
else if (solver->isProvenPrimalInfeasible()) { |
| 100 |
feasible = 0; |
| 101 |
seqOfInfeasiblesSize++; |
| 102 |
} |
| 103 |
|
| 104 |
if (((seqOfUnsolvedSize==0) || (maxFailure_ == 0)) && |
| 105 |
((maxInfeasible_== 0) || (seqOfInfeasiblesSize==0))) |
| 106 |
|
| 107 |
if (feasible && seqOfInfeasiblesSize > 1) { |
| 108 |
(*model->messageHandler())<<"Feasible node while father was infeasible." |
| 109 |
<<CoinMessageEol; |
| 110 |
} |
| 111 |
|
| 112 |
if (solved && seqOfUnsolvedSize > 1) { |
| 113 |
(*model->messageHandler())<<"Solved node while father was unsolved." |
| 114 |
<<CoinMessageEol; |
| 115 |
} |
| 116 |
|
| 117 |
if (seqOfInfeasiblesSize < maxInfeasible_ && |
| 118 |
solved && !feasible) { |
| 119 |
(*model->messageHandler())<<"Branching on infeasible node, sequence of infeasible size " |
| 120 |
<<seqOfInfeasiblesSize<<CoinMessageEol; |
| 121 |
// Have to make sure that we will branch |
| 122 |
OsiTMINLPInterface * ipopt = dynamic_cast<OsiTMINLPInterface *>(solver); |
| 123 |
ipopt->forceBranchable(); |
| 124 |
//change objective value |
| 125 |
returnStatus = 0; |
| 126 |
|
| 127 |
} |
| 128 |
|
| 129 |
if (!solved && parent != NULL && |
| 130 |
seqOfUnsolvedSize <= maxFailure_) { |
| 131 |
(*model->messageHandler())<<"Branching on unsolved node, sequence of unsolved size "<<seqOfUnsolvedSize<<CoinMessageEol; |
| 132 |
// Have to make sure that we will branch |
| 133 |
OsiTMINLPInterface * osiMinlp = dynamic_cast<OsiTMINLPInterface *>(solver); |
| 134 |
osiMinlp->forceBranchable(); // feasible=1; |
| 135 |
returnStatus = 0; |
| 136 |
} |
| 137 |
|
| 138 |
if (solver->isAbandoned() && parent != NULL && |
| 139 |
seqOfUnsolvedSize > maxFailure_) { |
| 140 |
hasFailed_ = true; |
| 141 |
OsiTMINLPInterface * osiMinlp = |
| 142 |
dynamic_cast<OsiTMINLPInterface *>(solver); |
| 143 |
if (pretendFailIsInfeasible_) { |
| 144 |
//force infeasible |
| 145 |
osiMinlp->forceInfeasible(); |
| 146 |
returnStatus = 2; |
| 147 |
} |
| 148 |
else { |
| 149 |
std::string probName; |
| 150 |
osiMinlp->getStrParam(OsiProbName,probName); |
| 151 |
throw osiMinlp->newUnsolvedError(0, osiMinlp->problem(), probName); |
| 152 |
} |
| 153 |
} |
| 154 |
return returnStatus; |
| 155 |
} |
| 156 |
|
| 157 |
void |
| 158 |
CbcNlpStrategy::setupCutGenerators(CbcModel &model) |
| 159 |
{} |
| 160 |
|
| 161 |
void |
| 162 |
CbcNlpStrategy::setupHeuristics(CbcModel &model) |
| 163 |
{} |
| 164 |
|
| 165 |
void |
| 166 |
CbcNlpStrategy::setupPrinting(CbcModel &model, int toto) |
| 167 |
{} |
| 168 |
|
| 169 |
void |
| 170 |
CbcNlpStrategy::setupOther(CbcModel &model) |
| 171 |
{} |
| 172 |
} |