source: trunk/Cbc/src/CbcHeuristicDiveLineSearch.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.2 KB
Line 
1/* $Id: CbcHeuristicDiveLineSearch.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 "CbcHeuristicDiveLineSearch.hpp"
12#include "CbcStrategy.hpp"
13
14// Default Constructor
15CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch()
16        : CbcHeuristicDive()
17{
18}
19
20// Constructor from model
21CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch(CbcModel & model)
22        : CbcHeuristicDive(model)
23{
24}
25
26// Destructor
27CbcHeuristicDiveLineSearch::~CbcHeuristicDiveLineSearch ()
28{
29}
30
31// Clone
32CbcHeuristicDiveLineSearch *
33CbcHeuristicDiveLineSearch::clone() const
34{
35    return new CbcHeuristicDiveLineSearch(*this);
36}
37
38// Create C++ lines to get to current state
39void
40CbcHeuristicDiveLineSearch::generateCpp( FILE * fp)
41{
42    CbcHeuristicDiveLineSearch other;
43    fprintf(fp, "0#include \"CbcHeuristicDiveLineSearch.hpp\"\n");
44    fprintf(fp, "3  CbcHeuristicDiveLineSearch heuristicDiveLineSearch(*cbcModel);\n");
45    CbcHeuristic::generateCpp(fp, "heuristicDiveLineSearch");
46    fprintf(fp, "3  cbcModel->addHeuristic(&heuristicDiveLineSearch);\n");
47}
48
49// Copy constructor
50CbcHeuristicDiveLineSearch::CbcHeuristicDiveLineSearch(const CbcHeuristicDiveLineSearch & rhs)
51        :
52        CbcHeuristicDive(rhs)
53{
54}
55
56// Assignment operator
57CbcHeuristicDiveLineSearch &
58CbcHeuristicDiveLineSearch::operator=( const CbcHeuristicDiveLineSearch & rhs)
59{
60    if (this != &rhs) {
61        CbcHeuristicDive::operator=(rhs);
62    }
63    return *this;
64}
65
66bool
67CbcHeuristicDiveLineSearch::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    // get the LP relaxation solution at the root node
77    double * rootNodeLPSol = model_->continuousSolution();
78
79    bestColumn = -1;
80    bestRound = -1; // -1 rounds down, +1 rounds up
81    double bestRelDistance = COIN_DBL_MAX;
82    bool allTriviallyRoundableSoFar = true;
83    int bestPriority = COIN_INT_MAX;
84    for (int i = 0; i < numberIntegers; i++) {
85        int iColumn = integerVariable[i];
86        double rootValue = rootNodeLPSol[iColumn];
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                    bestRelDistance = COIN_DBL_MAX;
96                }
97
98                double relDistance;
99                if (value < rootValue) {
100                    round = -1;
101                    relDistance = fraction / (rootValue - value);
102                } else if (value > rootValue) {
103                    round = 1;
104                    relDistance = (1.0 - fraction) / (value - rootValue);
105                } else {
106                    round = -1;
107                    relDistance = COIN_DBL_MAX;
108                }
109
110                // if variable is not binary, penalize it
111                if (!solver->isBinary(iColumn))
112                    relDistance *= 1000.0;
113
114                // if priorities then use
115                if (priority_) {
116                  int thisRound=static_cast<int>(priority_[i].direction);
117                  if ((thisRound&1)!=0) 
118                    round = ((thisRound&2)==0) ? -1 : +1;
119                  if (priority_[i].priority>bestPriority) {
120                    relDistance=COIN_DBL_MAX;
121                  } else if (priority_[i].priority<bestPriority) {
122                    bestPriority=static_cast<int>(priority_[i].priority);
123                    bestRelDistance=COIN_DBL_MAX;
124                  }
125                }
126                if (relDistance < bestRelDistance) {
127                    bestColumn = iColumn;
128                    bestRelDistance = relDistance;
129                    bestRound = round;
130                }
131            }
132        }
133    }
134    return allTriviallyRoundableSoFar;
135}
136
Note: See TracBrowser for help on using the repository browser.