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

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

add $id

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