source: trunk/Cbc/src/CbcHeuristicDiveVectorLength.cpp @ 1869

Last change on this file since 1869 was 1854, checked in by stefan, 7 years ago

fix svn keywords property

  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1/* $Id: CbcHeuristicDiveVectorLength.cpp 1854 2013-01-28 00:02:55Z stefan $ */
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    for (int i = 0; i < numberIntegers; i++) {
85        int iColumn = integerVariable[i];
86        double value = newSolution[iColumn];
87        double fraction = value - floor(value);
88        int round = 0;
89        if (fabs(floor(value + 0.5) - value) > integerTolerance) {
90            if (allTriviallyRoundableSoFar || (downLocks_[i] > 0 && upLocks_[i] > 0)) {
91
92                if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) {
93                    allTriviallyRoundableSoFar = false;
94                    bestScore = COIN_DBL_MAX;
95                }
96
97                // the variable cannot be rounded
98                double obj = direction * objective[iColumn];
99                if (obj >= 0.0)
100                    round = 1; // round up
101                else
102                    round = -1; // round down
103                double objDelta;
104                if (round == 1)
105                    objDelta = (1.0 - fraction) * obj;
106                else
107                    objDelta = - fraction * obj;
108
109                // we want the smaller score
110                double score = objDelta / (static_cast<double> (columnLength[iColumn]) + 1.0);
111
112                // if variable is not binary, penalize it
113                if (!solver->isBinary(iColumn))
114                    score *= 1000.0;
115
116                if (score < bestScore) {
117                    bestColumn = iColumn;
118                    bestScore = score;
119                    bestRound = round;
120                }
121            }
122        }
123    }
124    return allTriviallyRoundableSoFar;
125}
126
Note: See TracBrowser for help on using the repository browser.