source: trunk/Cbc/src/CbcHeuristicDiveFractional.cpp

Last change on this file was 2467, checked in by unxusr, 11 months ago

spaces after angles

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/* $Id: CbcHeuristicDiveFractional.cpp 2467 2019-01-03 21:26:29Z unxusr $ */
2// Copyright (C) 2008, International Business Machines
3// Corporation and others.  All Rights Reserved.
4// This code is licensed under the terms of the Eclipse Public License (EPL).
5
6#if defined(_MSC_VER)
7// Turn off compiler warning about long names
8#pragma warning(disable : 4786)
9#endif
10
11#include "CbcHeuristicDiveFractional.hpp"
12#include "CbcStrategy.hpp"
13
14// Default Constructor
15CbcHeuristicDiveFractional::CbcHeuristicDiveFractional()
16  : CbcHeuristicDive()
17{
18}
19
20// Constructor from model
21CbcHeuristicDiveFractional::CbcHeuristicDiveFractional(CbcModel &model)
22  : CbcHeuristicDive(model)
23{
24}
25
26// Destructor
27CbcHeuristicDiveFractional::~CbcHeuristicDiveFractional()
28{
29}
30
31// Clone
32CbcHeuristicDiveFractional *
33CbcHeuristicDiveFractional::clone() const
34{
35  return new CbcHeuristicDiveFractional(*this);
36}
37
38// Create C++ lines to get to current state
39void CbcHeuristicDiveFractional::generateCpp(FILE *fp)
40{
41  CbcHeuristicDiveFractional other;
42  fprintf(fp, "0#include \"CbcHeuristicDiveFractional.hpp\"\n");
43  fprintf(fp, "3  CbcHeuristicDiveFractional heuristicDiveFractional(*cbcModel);\n");
44  CbcHeuristic::generateCpp(fp, "heuristicDiveFractional");
45  fprintf(fp, "3  cbcModel->addHeuristic(&heuristicDiveFractional);\n");
46}
47
48// Copy constructor
49CbcHeuristicDiveFractional::CbcHeuristicDiveFractional(const CbcHeuristicDiveFractional &rhs)
50  : CbcHeuristicDive(rhs)
51{
52}
53
54// Assignment operator
55CbcHeuristicDiveFractional &
56CbcHeuristicDiveFractional::operator=(const CbcHeuristicDiveFractional &rhs)
57{
58  if (this != &rhs) {
59    CbcHeuristicDive::operator=(rhs);
60  }
61  return *this;
62}
63
64bool CbcHeuristicDiveFractional::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 = COIN_DBL_MAX;
76  bool allTriviallyRoundableSoFar = true;
77  int bestPriority = COIN_INT_MAX;
78  for (int i = 0; i < numberIntegers; i++) {
79    int iColumn = integerVariable[i];
80    if (!isHeuristicInteger(solver, iColumn))
81      continue;
82    double value = newSolution[iColumn];
83    double fraction = value - floor(value);
84    int round = 0;
85    if (fabs(floor(value + 0.5) - value) > integerTolerance) {
86      if (allTriviallyRoundableSoFar || (downLocks_[i] > 0 && upLocks_[i] > 0)) {
87
88        if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) {
89          allTriviallyRoundableSoFar = false;
90          bestFraction = COIN_DBL_MAX;
91        }
92
93        // the variable cannot be rounded
94        if (fraction < 0.5)
95          round = -1;
96        else {
97          round = 1;
98          fraction = 1.0 - fraction;
99        }
100
101        // if variable is not binary, penalize it
102        if (!solver->isBinary(iColumn))
103          fraction *= 1000.0;
104
105        // if priorities then use
106        if (priority_) {
107          int thisRound = static_cast< int >(priority_[i].direction);
108          if ((thisRound & 1) != 0)
109            round = ((thisRound & 2) == 0) ? -1 : +1;
110          if (priority_[i].priority > bestPriority) {
111            fraction = COIN_DBL_MAX;
112          } else if (priority_[i].priority < bestPriority) {
113            bestPriority = static_cast< int >(priority_[i].priority);
114            bestFraction = COIN_DBL_MAX;
115          }
116        }
117        if (fraction < bestFraction) {
118          bestColumn = iColumn;
119          bestFraction = fraction;
120          bestRound = round;
121        }
122      }
123    }
124  }
125  return allTriviallyRoundableSoFar;
126}
127
128/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
129*/
Note: See TracBrowser for help on using the repository browser.