source: trunk/Cbc/src/CbcHeuristicDiveLineSearch.cpp @ 1422

Last change on this file since 1422 was 1286, checked in by EdwinStraver, 10 years ago

Changed formatting using AStyle -A4 -p

File size: 3.7 KB
Line 
1/* $Id: CbcHeuristicDiveLineSearch.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 "CbcHeuristicDiveLineSearch.hpp"
10#include "CbcStrategy.hpp"
11
12// Default Constructor
13CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch()
14        : CbcHeuristicDive()
15{
16}
17
18// Constructor from model
19CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch(CbcModel & model)
20        : CbcHeuristicDive(model)
21{
22}
23
24// Destructor
25CbcHeuristicDiveLineSearch::~CbcHeuristicDiveLineSearch ()
26{
27}
28
29// Clone
30CbcHeuristicDiveLineSearch *
31CbcHeuristicDiveLineSearch::clone() const
32{
33    return new CbcHeuristicDiveLineSearch(*this);
34}
35
36// Create C++ lines to get to current state
37void
38CbcHeuristicDiveLineSearch::generateCpp( FILE * fp)
39{
40    CbcHeuristicDiveLineSearch other;
41    fprintf(fp, "0#include \"CbcHeuristicDiveLineSearch.hpp\"\n");
42    fprintf(fp, "3  CbcHeuristicDiveLineSearch heuristicDiveLineSearch(*cbcModel);\n");
43    CbcHeuristic::generateCpp(fp, "heuristicDiveLineSearch");
44    fprintf(fp, "3  cbcModel->addHeuristic(&heuristicDiveLineSearch);\n");
45}
46
47// Copy constructor
48CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch(const CbcHeuristicDiveLineSearch & rhs)
49        :
50        CbcHeuristicDive(rhs)
51{
52}
53
54// Assignment operator
55CbcHeuristicDiveLineSearch &
56CbcHeuristicDiveLineSearch::operator=( const CbcHeuristicDiveLineSearch & rhs)
57{
58    if (this != &rhs) {
59        CbcHeuristicDive::operator=(rhs);
60    }
61    return *this;
62}
63
64bool
65CbcHeuristicDiveLineSearch::selectVariableToBranch(OsiSolverInterface* solver,
66        const double* newSolution,
67        int& bestColumn,
68        int& bestRound)
69{
70    int numberIntegers = model_->numberIntegers();
71    const int * integerVariable = model_->integerVariable();
72    double integerTolerance = model_->getDblParam(CbcModel::CbcIntegerTolerance);
73
74    // get the LP relaxation solution at the root node
75    double * rootNodeLPSol = model_->continuousSolution();
76
77    bestColumn = -1;
78    bestRound = -1; // -1 rounds down, +1 rounds up
79    double bestRelDistance = DBL_MAX;
80    bool allTriviallyRoundableSoFar = true;
81    for (int i = 0; i < numberIntegers; i++) {
82        int iColumn = integerVariable[i];
83        double rootValue = rootNodeLPSol[iColumn];
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                    bestRelDistance = DBL_MAX;
93                }
94
95                double relDistance;
96                if (value < rootValue) {
97                    round = -1;
98                    relDistance = fraction / (rootValue - value);
99                } else if (value > rootValue) {
100                    round = 1;
101                    relDistance = (1.0 - fraction) / (value - rootValue);
102                } else {
103                    round = -1;
104                    relDistance = DBL_MAX;
105                }
106
107                // if variable is not binary, penalize it
108                if (!solver->isBinary(iColumn))
109                    relDistance *= 1000.0;
110
111                if (relDistance < bestRelDistance) {
112                    bestColumn = iColumn;
113                    bestRelDistance = relDistance;
114                    bestRound = round;
115                }
116            }
117        }
118    }
119    return allTriviallyRoundableSoFar;
120}
Note: See TracBrowser for help on using the repository browser.