source: trunk/Cbc/src/CbcHeuristicDiveFractional.cpp @ 912

Last change on this file since 912 was 912, checked in by ladanyi, 12 years ago

Incorporated changes from branches/heur

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