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

Last change on this file since 2093 was 2093, checked in by forrest, 4 years ago

changes for diving heuristic

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.8 KB
Line 
1/* $Id: CbcHeuristicDiveFractional.cpp 2093 2014-11-06 16:17:38Z 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 "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
40CbcHeuristicDiveFractional::generateCpp( FILE * fp)
41{
42    CbcHeuristicDiveFractional other;
43    fprintf(fp, "0#include \"CbcHeuristicDiveFractional.hpp\"\n");
44    fprintf(fp, "3  CbcHeuristicDiveFractional heuristicDiveFractional(*cbcModel);\n");
45    CbcHeuristic::generateCpp(fp, "heuristicDiveFractional");
46    fprintf(fp, "3  cbcModel->addHeuristic(&heuristicDiveFractional);\n");
47}
48
49// Copy constructor
50CbcHeuristicDiveFractional::CbcHeuristicDiveFractional(const CbcHeuristicDiveFractional & rhs)
51        :
52        CbcHeuristicDive(rhs)
53{
54}
55
56// Assignment operator
57CbcHeuristicDiveFractional &
58CbcHeuristicDiveFractional::operator=( const CbcHeuristicDiveFractional & rhs)
59{
60    if (this != &rhs) {
61        CbcHeuristicDive::operator=(rhs);
62    }
63    return *this;
64}
65
66bool
67CbcHeuristicDiveFractional::selectVariableToBranch(OsiSolverInterface* solver,
68        const double* newSolution,
69        int& bestColumn,
70        int& bestRound)
71{
72    int numberIntegers = model_->numberIntegers();
73    const int * integerVariable = model_->integerVariable();
74    double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
75
76    bestColumn = -1;
77    bestRound = -1; // -1 rounds down, +1 rounds up
78    double bestFraction = COIN_DBL_MAX;
79    bool allTriviallyRoundableSoFar = true;
80    int bestPriority = COIN_INT_MAX;
81    for (int i = 0; i < numberIntegers; i++) {
82        int iColumn = integerVariable[i];
83        double value = newSolution[iColumn];
84        double fraction = value - floor(value);
85        int round = 0;
86        if (fabs(floor(value + 0.5) - value) > integerTolerance) {
87            if (allTriviallyRoundableSoFar || (downLocks_[i] > 0 && upLocks_[i] > 0)) {
88
89                if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) {
90                    allTriviallyRoundableSoFar = false;
91                    bestFraction = COIN_DBL_MAX;
92                }
93
94                // the variable cannot be rounded
95                if (fraction < 0.5)
96                    round = -1;
97                else {
98                    round = 1;
99                    fraction = 1.0 - fraction;
100                }
101
102                // if variable is not binary, penalize it
103                if (!solver->isBinary(iColumn))
104                    fraction *= 1000.0;
105
106                // if priorities then use
107                if (priority_) {
108                  int thisRound=static_cast<int>(priority_[i].direction);
109                  if ((thisRound&1)!=0) 
110                    round = ((thisRound&2)==0) ? -1 : +1;
111                  if (priority_[i].priority>bestPriority) {
112                    fraction=COIN_DBL_MAX;
113                  } else if (priority_[i].priority<bestPriority) {
114                    bestPriority=static_cast<int>(priority_[i].priority);
115                    bestFraction=COIN_DBL_MAX;
116                  }
117                }
118                if (fraction < bestFraction) {
119                    bestColumn = iColumn;
120                    bestFraction = fraction;
121                    bestRound = round;
122                }
123            }
124        }
125    }
126    return allTriviallyRoundableSoFar;
127}
128
Note: See TracBrowser for help on using the repository browser.