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

Last change on this file since 1569 was 1432, checked in by bjarni, 10 years ago

Added extra return at end of each source file where needed, to remove possible linefeed conflicts (NightlyBuild? errors)

File size: 3.9 KB
Line 
1/* $Id: CbcHeuristicDiveVectorLength.cpp 1173 2009-06-04 09:44:10Z forrest $ */
2// Copyright (C) 2008, International Business Machines
3// Corporation and others.  All Rights Reserved.
4#if defined(_MSC_VER)
5// Turn off compiler warning about long names
6#  pragma warning(disable:4786)
7#endif
8
9#include "CbcHeuristicDiveVectorLength.hpp"
10#include "CbcStrategy.hpp"
11
12// Default Constructor
13CbcHeuristicDiveVectorLength::CbcHeuristicDiveVectorLength()
14        : CbcHeuristicDive()
15{
16}
17
18// Constructor from model
19CbcHeuristicDiveVectorLength::CbcHeuristicDiveVectorLength(CbcModel & model)
20        : CbcHeuristicDive(model)
21{
22}
23
24// Destructor
25CbcHeuristicDiveVectorLength::~CbcHeuristicDiveVectorLength ()
26{
27}
28
29// Clone
30CbcHeuristicDiveVectorLength *
31CbcHeuristicDiveVectorLength::clone() const
32{
33    return new CbcHeuristicDiveVectorLength(*this);
34}
35
36// Create C++ lines to get to current state
37void
38CbcHeuristicDiveVectorLength::generateCpp( FILE * fp)
39{
40    CbcHeuristicDiveVectorLength other;
41    fprintf(fp, "0#include \"CbcHeuristicDiveVectorLength.hpp\"\n");
42    fprintf(fp, "3  CbcHeuristicDiveVectorLength heuristicDiveVectorLength(*cbcModel);\n");
43    CbcHeuristic::generateCpp(fp, "heuristicDiveVectorLength");
44    fprintf(fp, "3  cbcModel->addHeuristic(&heuristicDiveVectorLength);\n");
45}
46
47// Copy constructor
48CbcHeuristicDiveVectorLength::CbcHeuristicDiveVectorLength(const CbcHeuristicDiveVectorLength & rhs)
49        :
50        CbcHeuristicDive(rhs)
51{
52}
53
54// Assignment operator
55CbcHeuristicDiveVectorLength &
56CbcHeuristicDiveVectorLength::operator=( const CbcHeuristicDiveVectorLength & rhs)
57{
58    if (this != &rhs) {
59        CbcHeuristicDive::operator=(rhs);
60    }
61    return *this;
62}
63
64bool
65CbcHeuristicDiveVectorLength::selectVariableToBranch(OsiSolverInterface* solver,
66        const double* newSolution,
67        int& bestColumn,
68        int& bestRound)
69{
70    const double * objective = solver->getObjCoefficients();
71    double direction = solver->getObjSense(); // 1 for min, -1 for max
72
73    const int * columnLength = matrix_.getVectorLengths();
74    int numberIntegers = model_->numberIntegers();
75    const int * integerVariable = model_->integerVariable();
76    double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
77
78    bestColumn = -1;
79    bestRound = -1; // -1 rounds down, +1 rounds up
80    double bestScore = DBL_MAX;
81    bool allTriviallyRoundableSoFar = true;
82    for (int i = 0; i < numberIntegers; i++) {
83        int iColumn = integerVariable[i];
84        double value = newSolution[iColumn];
85        double fraction = value - floor(value);
86        int round = 0;
87        if (fabs(floor(value + 0.5) - value) > integerTolerance) {
88            if (allTriviallyRoundableSoFar || (downLocks_[i] > 0 && upLocks_[i] > 0)) {
89
90                if (allTriviallyRoundableSoFar && downLocks_[i] > 0 && upLocks_[i] > 0) {
91                    allTriviallyRoundableSoFar = false;
92                    bestScore = DBL_MAX;
93                }
94
95                // the variable cannot be rounded
96                double obj = direction * objective[iColumn];
97                if (obj >= 0.0)
98                    round = 1; // round up
99                else
100                    round = -1; // round down
101                double objDelta;
102                if (round == 1)
103                    objDelta = (1.0 - fraction) * obj;
104                else
105                    objDelta = - fraction * obj;
106
107                // we want the smaller score
108                double score = objDelta / (static_cast<double> (columnLength[iColumn]) + 1.0);
109
110                // if variable is not binary, penalize it
111                if (!solver->isBinary(iColumn))
112                    score *= 1000.0;
113
114                if (score < bestScore) {
115                    bestColumn = iColumn;
116                    bestScore = score;
117                    bestRound = round;
118                }
119            }
120        }
121    }
122    return allTriviallyRoundableSoFar;
123}
124
Note: See TracBrowser for help on using the repository browser.