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

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

Incorporated changes from branches/heur

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