source: trunk/Cbc/src/CbcHeuristicDiveVectorLength.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: 4.5 KB
Line 
1/* $Id: CbcHeuristicDiveVectorLength.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 "CbcHeuristicDiveVectorLength.hpp"
12#include "CbcStrategy.hpp"
13
14// Default Constructor
15CbcHeuristicDiveVectorLength::CbcHeuristicDiveVectorLength()
16        : CbcHeuristicDive()
17{
18}
19
20// Constructor from model
21CbcHeuristicDiveVectorLength::CbcHeuristicDiveVectorLength(CbcModel & model)
22        : CbcHeuristicDive(model)
23{
24}
25
26// Destructor
27CbcHeuristicDiveVectorLength::~CbcHeuristicDiveVectorLength ()
28{
29}
30
31// Clone
32CbcHeuristicDiveVectorLength *
33CbcHeuristicDiveVectorLength::clone() const
34{
35    return new CbcHeuristicDiveVectorLength(*this);
36}
37
38// Create C++ lines to get to current state
39void
40CbcHeuristicDiveVectorLength::generateCpp( FILE * fp)
41{
42    CbcHeuristicDiveVectorLength other;
43    fprintf(fp, "0#include \"CbcHeuristicDiveVectorLength.hpp\"\n");
44    fprintf(fp, "3  CbcHeuristicDiveVectorLength heuristicDiveVectorLength(*cbcModel);\n");
45    CbcHeuristic::generateCpp(fp, "heuristicDiveVectorLength");
46    fprintf(fp, "3  cbcModel->addHeuristic(&heuristicDiveVectorLength);\n");
47}
48
49// Copy constructor
50CbcHeuristicDiveVectorLength::CbcHeuristicDiveVectorLength(const CbcHeuristicDiveVectorLength & rhs)
51        :
52        CbcHeuristicDive(rhs)
53{
54}
55
56// Assignment operator
57CbcHeuristicDiveVectorLength &
58CbcHeuristicDiveVectorLength::operator=( const CbcHeuristicDiveVectorLength & rhs)
59{
60    if (this != &rhs) {
61        CbcHeuristicDive::operator=(rhs);
62    }
63    return *this;
64}
65
66bool
67CbcHeuristicDiveVectorLength::selectVariableToBranch(OsiSolverInterface* solver,
68        const double* newSolution,
69        int& bestColumn,
70        int& bestRound)
71{
72    const double * objective = solver->getObjCoefficients();
73    double direction = solver->getObjSense(); // 1 for min, -1 for max
74
75    const int * columnLength = matrix_.getVectorLengths();
76    int numberIntegers = model_->numberIntegers();
77    const int * integerVariable = model_->integerVariable();
78    double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
79
80    bestColumn = -1;
81    bestRound = -1; // -1 rounds down, +1 rounds up
82    double bestScore = COIN_DBL_MAX;
83    bool allTriviallyRoundableSoFar = true;
84    int bestPriority = COIN_INT_MAX;
85    for (int i = 0; i < numberIntegers; i++) {
86        int iColumn = integerVariable[i];
87        double value = newSolution[iColumn];
88        double fraction = value - floor(value);
89        int round = 0;
90        if (fabs(floor(value + 0.5) - value) > integerTolerance) {
91            if (allTriviallyRoundableSoFar || (downLocks_[i] > 0 && upLocks_[i] > 0)) {
92
93                if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) {
94                    allTriviallyRoundableSoFar = false;
95                    bestScore = COIN_DBL_MAX;
96                }
97
98                // the variable cannot be rounded
99                double obj = direction * objective[iColumn];
100                if (obj > smallObjective_) {
101                    round = 1; // round up
102                } else if (obj < -smallObjective_) {
103                    round = -1; // round down
104                } else {
105                  if (fraction<0.4)
106                    round = -1;
107                  else
108                    round = 1;
109                }
110                double objDelta;
111                if (round == 1)
112                    objDelta = (1.0 - fraction) * CoinMax(obj,smallObjective_);
113                else
114                    objDelta = - fraction * CoinMin(obj,-smallObjective_);
115
116                // we want the smaller score
117                double score = objDelta / (static_cast<double> (columnLength[iColumn]) + 1.0);
118
119                // if variable is not binary, penalize it
120                if (!solver->isBinary(iColumn))
121                    score *= 1000.0;
122
123                // if priorities then use
124                if (priority_) {
125                  int thisRound=static_cast<int>(priority_[i].direction);
126                  if ((thisRound&1)!=0) 
127                    round = ((thisRound&2)==0) ? -1 : +1;
128                  if (priority_[i].priority>bestPriority) {
129                    score=COIN_DBL_MAX;
130                  } else if (priority_[i].priority<bestPriority) {
131                    bestPriority=static_cast<int>(priority_[i].priority);
132                    bestScore=COIN_DBL_MAX;
133                  }
134                }
135                if (score < bestScore) {
136                    bestColumn = iColumn;
137                    bestScore = score;
138                    bestRound = round;
139                }
140            }
141        }
142    }
143    return allTriviallyRoundableSoFar;
144}
145
Note: See TracBrowser for help on using the repository browser.