BonCbcNlpStrategy.cpp
Go to the documentation of this file.
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 
22 namespace Bonmin
23 {
24 // Default Constructor
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
40  {}
41 
42 // Clone
43  CbcStrategy *
45  {
46  return new CbcNlpStrategy(*this);
47  }
48 
49 // Copy constructor
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);
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
159  {}
160 
161  void
163  {}
164 
165  void
166  CbcNlpStrategy::setupPrinting(CbcModel &model, int toto)
167  {}
168 
169  void
170  CbcNlpStrategy::setupOther(CbcModel &model)
171  {}
172 }
virtual void setupCutGenerators(CbcModel &model)
Setup cut generators.
int getSequenceOfUnsolvedSize()
Number of consecutive unsolved parents only recorded if node is infeasible.
Definition: BonCbcNode.hpp:117
TNLPSolver::UnsolvedError * newUnsolvedError(int num, Ipopt::SmartPtr< TMINLP2TNLP > problem, std::string name)
Holds information for recreating a subproblem by incremental change from the parent for...
Definition: BonCbcNode.hpp:85
const TMINLP2TNLP * problem() const
get pointer to the TMINLP2TNLP adapter
This is class provides an Osi interface for a Mixed Integer Linear Program expressed as a TMINLP (so ...
void forceBranchable()
Force current solution to be branched on (make it fractionnal with small objective) ...
virtual CbcNodeInfo * fullNodeInfo(CbcModel *model, int numberRowsAtContinuous) const
Return a new Full node information pointer (descendant of CbcFullNodeInfo)
int maxInfeasible_
maximum number of consecutive infeasible nodes before giving up
bool getStrParam(OsiStrParam key, std::string &value) const
virtual int status(CbcModel *model, CbcNodeInfo *parent, int whereFrom)
After a CbcModel::resolve this can return a status -1 no effect 0 treat as optimal 1 as 0 but do not ...
void forceInfeasible()
Force current solution to be infeasible.
virtual void setupPrinting(CbcModel &model, int modelLogLevel)
Do printing stuff.
int pretendFailIsInfeasible_
If yes when a problem is not solved (failed to be solved) will pretend that it is infeasible...
int getSequenceOfInfeasiblesSize()
Number of consecutive infeasible parents only recorded if node is infeasible.
Definition: BonCbcNode.hpp:112
virtual CbcStrategy * clone() const
Clone.
bool hasFailed_
did we fail?
virtual void setupOther(CbcModel &model)
Other stuff e.g. strong branching and preprocessing.
virtual CbcNodeInfo * partialNodeInfo(CbcModel *model, CbcNodeInfo *parent, CbcNode *owner, int numberChangedBounds, const int *variables, const double *boundChanges, const CoinWarmStartDiff *basisDiff) const
Return a new Partial node information pointer (descendant of CbcPartialNodeInfo)
int maxFailure_
maximum number of consecutive failures in a branch before giving up
virtual void setupHeuristics(CbcModel &model)
Setup heuristics.
CbcNlpStrategy(int maxFailures, int maxInfeasibles, int pretendFailIsInfeasible)