source: stable/2.4/Cbc/src/CbcHeuristicDiveLineSearch.cpp @ 1271

Last change on this file since 1271 was 1271, checked in by forrest, 10 years ago

Creating new stable branch 2.4 from trunk (rev 1270)

File size: 3.2 KB
Line 
1/* $Id: CbcHeuristicDiveLineSearch.cpp 1173 2009-06-04 09:44:10Z forrest $ */
2// Copyright (C) 2008, International Business Machines
3// Corporation and others.  All Rights Reserved.
4#if defined(_MSC_VER)
5// Turn off compiler warning about long names
6#  pragma warning(disable:4786)
7#endif
8
9#include "CbcHeuristicDiveLineSearch.hpp"
10#include "CbcStrategy.hpp"
11
12// Default Constructor
13CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch() 
14  :CbcHeuristicDive()
15{
16}
17
18// Constructor from model
19CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch(CbcModel & model)
20  :CbcHeuristicDive(model)
21{
22}
23
24// Destructor
25CbcHeuristicDiveLineSearch::~CbcHeuristicDiveLineSearch ()
26{
27}
28
29// Clone
30CbcHeuristicDiveLineSearch *
31CbcHeuristicDiveLineSearch::clone() const
32{
33  return new CbcHeuristicDiveLineSearch(*this);
34}
35
36// Create C++ lines to get to current state
37void 
38CbcHeuristicDiveLineSearch::generateCpp( FILE * fp) 
39{
40  CbcHeuristicDiveLineSearch other;
41  fprintf(fp,"0#include \"CbcHeuristicDiveLineSearch.hpp\"\n");
42  fprintf(fp,"3  CbcHeuristicDiveLineSearch heuristicDiveLineSearch(*cbcModel);\n");
43  CbcHeuristic::generateCpp(fp,"heuristicDiveLineSearch");
44  fprintf(fp,"3  cbcModel->addHeuristic(&heuristicDiveLineSearch);\n");
45}
46
47// Copy constructor
48CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch(const CbcHeuristicDiveLineSearch & rhs)
49:
50  CbcHeuristicDive(rhs)
51{
52}
53
54// Assignment operator
55CbcHeuristicDiveLineSearch & 
56CbcHeuristicDiveLineSearch::operator=( const CbcHeuristicDiveLineSearch& rhs)
57{
58  if (this!=&rhs) {
59    CbcHeuristicDive::operator=(rhs);
60  }
61  return *this;
62}
63
64bool
65CbcHeuristicDiveLineSearch::selectVariableToBranch(OsiSolverInterface* solver,
66                                                   const double* newSolution,
67                                                   int& bestColumn,
68                                                   int& bestRound)
69{
70  int numberIntegers = model_->numberIntegers();
71  const int * integerVariable = model_->integerVariable();
72  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
73
74  // get the LP relaxation solution at the root node
75  double * rootNodeLPSol = model_->continuousSolution();
76
77  bestColumn = -1;
78  bestRound = -1; // -1 rounds down, +1 rounds up
79  double bestRelDistance = DBL_MAX;
80  bool allTriviallyRoundableSoFar = true;
81  for (int i=0; i<numberIntegers; i++) {
82    int iColumn = integerVariable[i];
83    double rootValue=rootNodeLPSol[iColumn];
84    double value=newSolution[iColumn];
85    double fraction=value-floor(value);
86    int round = 0;
87    if (fabs(floor(value+0.5)-value)>integerTolerance) {
88      if (allTriviallyRoundableSoFar||(downLocks_[i]>0&&upLocks_[i]>0)) {
89
90        if (allTriviallyRoundableSoFar&&downLocks_[i]>0&&upLocks_[i]>0) {
91          allTriviallyRoundableSoFar = false;
92          bestRelDistance = DBL_MAX;
93        }
94
95        double relDistance;
96        if(value < rootValue) {
97          round = -1;
98          relDistance = fraction / (rootValue - value);
99        }
100        else if(value > rootValue) {
101          round = 1;
102          relDistance = (1.0 - fraction) / (value - rootValue);
103        }
104        else {
105          round = -1;
106          relDistance = DBL_MAX;
107        }
108         
109        // if variable is not binary, penalize it
110        if(!solver->isBinary(iColumn))
111          relDistance *= 1000.0;
112       
113        if(relDistance < bestRelDistance) {
114          bestColumn = iColumn;
115          bestRelDistance = relDistance;
116          bestRound = round;
117        }
118      }
119    }
120  }
121  return allTriviallyRoundableSoFar;
122}
Note: See TracBrowser for help on using the repository browser.