source: trunk/Cbc/src/CbcHeuristicDiveGuided.cpp

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

spaces after angles

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/* $Id: CbcHeuristicDiveGuided.cpp 2467 2019-01-03 21:26:29Z forrest $ */
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 "CbcHeuristicDiveGuided.hpp"
12#include "CbcStrategy.hpp"
13
14// Default Constructor
15CbcHeuristicDiveGuided::CbcHeuristicDiveGuided()
16  : CbcHeuristicDive()
17{
18}
19
20// Constructor from model
21CbcHeuristicDiveGuided::CbcHeuristicDiveGuided(CbcModel &model)
22  : CbcHeuristicDive(model)
23{
24}
25
26// Destructor
27CbcHeuristicDiveGuided::~CbcHeuristicDiveGuided()
28{
29}
30
31// Clone
32CbcHeuristicDiveGuided *
33CbcHeuristicDiveGuided::clone() const
34{
35  return new CbcHeuristicDiveGuided(*this);
36}
37
38// Create C++ lines to get to current state
39void CbcHeuristicDiveGuided::generateCpp(FILE *fp)
40{
41  CbcHeuristicDiveGuided other;
42  fprintf(fp, "0#include \"CbcHeuristicDiveGuided.hpp\"\n");
43  fprintf(fp, "3  CbcHeuristicDiveGuided heuristicDiveGuided(*cbcModel);\n");
44  CbcHeuristic::generateCpp(fp, "heuristicDiveGuided");
45  fprintf(fp, "3  cbcModel->addHeuristic(&heuristicDiveGuided);\n");
46}
47
48// Copy constructor
49CbcHeuristicDiveGuided::CbcHeuristicDiveGuided(const CbcHeuristicDiveGuided &rhs)
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 CbcHeuristicDiveGuided::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
73bool CbcHeuristicDiveGuided::selectVariableToBranch(OsiSolverInterface *solver,
74  const double *newSolution,
75  int &bestColumn,
76  int &bestRound)
77{
78  double *bestIntegerSolution = model_->bestSolution();
79
80  int numberIntegers = model_->numberIntegers();
81  const int *integerVariable = model_->integerVariable();
82  double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
83
84  bestColumn = -1;
85  bestRound = -1; // -1 rounds down, +1 rounds up
86  double bestFraction = COIN_DBL_MAX;
87  bool allTriviallyRoundableSoFar = true;
88  int bestPriority = COIN_INT_MAX;
89  for (int i = 0; i < numberIntegers; i++) {
90    int iColumn = integerVariable[i];
91    if (!isHeuristicInteger(solver, iColumn))
92      continue;
93    double value = newSolution[iColumn];
94    double fraction = value - floor(value);
95    int round = 0;
96    if (fabs(floor(value + 0.5) - value) > integerTolerance) {
97      if (allTriviallyRoundableSoFar || (downLocks_[i] > 0 && upLocks_[i] > 0)) {
98
99        if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) {
100          allTriviallyRoundableSoFar = false;
101          bestFraction = COIN_DBL_MAX;
102        }
103
104        if (value >= bestIntegerSolution[iColumn])
105          round = -1;
106        else {
107          round = 1;
108          fraction = 1.0 - fraction;
109        }
110
111        // if variable is not binary, penalize it
112        if (!solver->isBinary(iColumn))
113          fraction *= 1000.0;
114
115        // if priorities then use
116        if (priority_) {
117          int thisRound = static_cast< int >(priority_[i].direction);
118          if ((thisRound & 1) != 0)
119            round = ((thisRound & 2) == 0) ? -1 : +1;
120          if (priority_[i].priority > bestPriority) {
121            fraction = COIN_DBL_MAX;
122          } else if (priority_[i].priority < bestPriority) {
123            bestPriority = static_cast< int >(priority_[i].priority);
124            bestFraction = COIN_DBL_MAX;
125          }
126        }
127        if (fraction < bestFraction) {
128          bestColumn = iColumn;
129          bestFraction = fraction;
130          bestRound = round;
131        }
132      }
133    }
134  }
135  return allTriviallyRoundableSoFar;
136}
137
138/* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2
139*/
Note: See TracBrowser for help on using the repository browser.