source: stable/2.4/Cbc/src/CbcHeuristicDiveCoefficient.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.3 KB
Line 
1/* $Id: CbcHeuristicDiveCoefficient.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//#define PRINT_DEBUG
10
11#include "CbcHeuristicDiveCoefficient.hpp"
12#include "CbcStrategy.hpp"
13
14// Default Constructor
15CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient() 
16  :CbcHeuristicDive()
17{
18}
19
20// Constructor from model
21CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(CbcModel & model)
22  :CbcHeuristicDive(model)
23{
24}
25
26// Destructor
27CbcHeuristicDiveCoefficient::~CbcHeuristicDiveCoefficient ()
28{
29}
30
31// Clone
32CbcHeuristicDiveCoefficient *
33CbcHeuristicDiveCoefficient::clone() const
34{
35  return new CbcHeuristicDiveCoefficient(*this);
36}
37
38// Create C++ lines to get to current state
39void 
40CbcHeuristicDiveCoefficient::generateCpp( FILE * fp) 
41{
42  CbcHeuristicDiveCoefficient other;
43  fprintf(fp,"0#include \"CbcHeuristicDiveCoefficient.hpp\"\n");
44  fprintf(fp,"3  CbcHeuristicDiveCoefficient heuristicDiveCoefficient(*cbcModel);\n");
45  CbcHeuristic::generateCpp(fp,"heuristicDiveCoefficient");
46  fprintf(fp,"3  cbcModel->addHeuristic(&heuristicDiveCoefficient);\n");
47}
48
49// Copy constructor
50CbcHeuristicDiveCoefficient::CbcHeuristicDiveCoefficient(const CbcHeuristicDiveCoefficient & rhs)
51:
52  CbcHeuristicDive(rhs)
53{
54}
55
56// Assignment operator
57CbcHeuristicDiveCoefficient & 
58CbcHeuristicDiveCoefficient::operator=( const CbcHeuristicDiveCoefficient& rhs)
59{
60  if (this!=&rhs) {
61    CbcHeuristicDive::operator=(rhs);
62  }
63  return *this;
64}
65
66bool
67CbcHeuristicDiveCoefficient::selectVariableToBranch(OsiSolverInterface* solver,
68                                                    const double* newSolution,
69                                                    int& bestColumn,
70                                                    int& bestRound)
71{
72  int numberIntegers = model_->numberIntegers();
73  const int * integerVariable = model_->integerVariable();
74  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
75
76  bestColumn = -1;
77  bestRound = -1; // -1 rounds down, +1 rounds up
78  double bestFraction = DBL_MAX;
79  int bestLocks = COIN_INT_MAX;
80  bool allTriviallyRoundableSoFar = true;
81  for (int i=0; i<numberIntegers; i++) {
82    int iColumn = integerVariable[i];
83    double value=newSolution[iColumn];
84    double fraction=value-floor(value);
85    int round = 0;
86    if (fabs(floor(value+0.5)-value)>integerTolerance) {
87      int nDownLocks = downLocks_[i];
88      int nUpLocks = upLocks_[i];
89      if(allTriviallyRoundableSoFar||(nDownLocks>0 && nUpLocks>0)) {
90
91        if(allTriviallyRoundableSoFar&&nDownLocks>0 && nUpLocks>0) {
92          allTriviallyRoundableSoFar = false;
93          bestFraction = DBL_MAX;
94          bestLocks = COIN_INT_MAX;
95        }
96
97        // the variable cannot be rounded
98        int nLocks = nDownLocks;
99        if(nDownLocks<nUpLocks)
100          round = -1;
101        else if(nDownLocks>nUpLocks) {
102          round = 1;
103          fraction = 1.0 - fraction;
104          nLocks = nUpLocks;
105        }
106        else if(fraction < 0.5)
107          round = -1;
108        else {
109          round = 1;
110          fraction = 1.0 - fraction;
111          nLocks = nUpLocks;
112        }
113       
114        // if variable is not binary, penalize it
115        if(!solver->isBinary(iColumn))
116          fraction *= 1000.0;
117       
118        if(nLocks < bestLocks || (nLocks == bestLocks &&
119                                  fraction < bestFraction)) {
120          bestColumn = iColumn;
121          bestLocks = nLocks;
122          bestFraction = fraction;
123          bestRound = round;
124        }
125      }
126    }
127  }
128  return allTriviallyRoundableSoFar;
129}
Note: See TracBrowser for help on using the repository browser.