source: branches/devel/Bonmin/src/CbcBonmin/BonCbcNlpStrategy.cpp @ 62

Last change on this file since 62 was 62, checked in by pbonami, 13 years ago

astyled the devel branch

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