source: branches/sandbox/Cbc/src/CbcNWayBranchingObject.cpp @ 1293

Last change on this file since 1293 was 1293, checked in by EdwinStraver, 10 years ago
File size: 5.0 KB
Line 
1// Edwin 11/10/2009-- carved out of CbcBranchActual
2#if defined(_MSC_VER)
3// Turn off compiler warning about long names
4#  pragma warning(disable:4786)
5#endif
6#include <cassert>
7#include <cstdlib>
8#include <cmath>
9#include <cfloat>
10//#define CBC_DEBUG
11
12#include "CoinTypes.hpp"
13#include "OsiSolverInterface.hpp"
14#include "OsiSolverBranch.hpp"
15#include "CbcModel.hpp"
16#include "CbcMessage.hpp"
17#include "CbcNWayBranchingObject.hpp"
18#include "CbcBranchActual.hpp"
19#include "CoinSort.hpp"
20#include "CoinError.hpp"
21
22// Default Constructor
23CbcNWayBranchingObject::CbcNWayBranchingObject()
24        : CbcBranchingObject()
25{
26    order_ = NULL;
27    object_ = NULL;
28    numberInSet_ = 0;
29    way_ = 0;
30}
31
32// Useful constructor
33CbcNWayBranchingObject::CbcNWayBranchingObject (CbcModel * model,
34        const CbcNWay * nway,
35        int number, const int * order)
36        : CbcBranchingObject(model, nway->id(), -1, 0.5)
37{
38    numberBranches_ = number;
39    order_ = new int [number];
40    object_ = nway;
41    numberInSet_ = number;
42    memcpy(order_, order, number*sizeof(int));
43}
44
45// Copy constructor
46CbcNWayBranchingObject::CbcNWayBranchingObject ( const CbcNWayBranchingObject & rhs) : CbcBranchingObject(rhs)
47{
48    numberInSet_ = rhs.numberInSet_;
49    object_ = rhs.object_;
50    if (numberInSet_) {
51        order_ = new int [numberInSet_];
52        memcpy(order_, rhs.order_, numberInSet_*sizeof(int));
53    } else {
54        order_ = NULL;
55    }
56}
57
58// Assignment operator
59CbcNWayBranchingObject &
60CbcNWayBranchingObject::operator=( const CbcNWayBranchingObject & rhs)
61{
62    if (this != &rhs) {
63        CbcBranchingObject::operator=(rhs);
64        object_ = rhs.object_;
65        delete [] order_;
66        numberInSet_ = rhs.numberInSet_;
67        if (numberInSet_) {
68            order_ = new int [numberInSet_];
69            memcpy(order_, rhs.order_, numberInSet_*sizeof(int));
70        } else {
71            order_ = NULL;
72        }
73    }
74    return *this;
75}
76CbcBranchingObject *
77CbcNWayBranchingObject::clone() const
78{
79    return (new CbcNWayBranchingObject(*this));
80}
81
82
83// Destructor
84CbcNWayBranchingObject::~CbcNWayBranchingObject ()
85{
86    delete [] order_;
87}
88double
89CbcNWayBranchingObject::branch()
90{
91    int which = branchIndex_;
92    branchIndex_++;
93    assert (numberBranchesLeft() >= 0);
94    if (which == 0) {
95        // first branch so way_ may mean something
96        assert (way_ == -1 || way_ == 1);
97        if (way_ == -1)
98            which++;
99    } else if (which == 1) {
100        // second branch so way_ may mean something
101        assert (way_ == -1 || way_ == 1);
102        if (way_ == -1)
103            which--;
104        // switch way off
105        way_ = 0;
106    }
107    const double * lower = model_->solver()->getColLower();
108    const double * upper = model_->solver()->getColUpper();
109    const int * members = object_->members();
110    for (int j = 0; j < numberInSet_; j++) {
111        int iSequence = order_[j];
112        int iColumn = members[iSequence];
113        if (j != which) {
114            model_->solver()->setColUpper(iColumn, lower[iColumn]);
115            //model_->solver()->setColLower(iColumn,lower[iColumn]);
116            assert (lower[iColumn] > -1.0e20);
117            // apply any consequences
118            object_->applyConsequence(iSequence, -9999);
119        } else {
120            model_->solver()->setColLower(iColumn, upper[iColumn]);
121            //model_->solver()->setColUpper(iColumn,upper[iColumn]);
122#ifdef FULL_PRINT
123            printf("Up Fix %d to %g\n", iColumn, upper[iColumn]);
124#endif
125            assert (upper[iColumn] < 1.0e20);
126            // apply any consequences
127            object_->applyConsequence(iSequence, 9999);
128        }
129    }
130    return 0.0;
131}
132void
133CbcNWayBranchingObject::print()
134{
135    printf("NWay - Up Fix ");
136    const int * members = object_->members();
137    for (int j = 0; j < way_; j++) {
138        int iColumn = members[order_[j]];
139        printf("%d ", iColumn);
140    }
141    printf("\n");
142}
143
144/** Compare the original object of \c this with the original object of \c
145    brObj. Assumes that there is an ordering of the original objects.
146    This method should be invoked only if \c this and brObj are of the same
147    type.
148    Return negative/0/positive depending on whether \c this is
149    smaller/same/larger than the argument.
150*/
151int
152CbcNWayBranchingObject::compareOriginalObject
153(const CbcBranchingObject* /*brObj*/) const
154{
155    throw("must implement");
156}
157
158/** Compare the \c this with \c brObj. \c this and \c brObj must be os the
159    same type and must have the same original object, but they may have
160    different feasible regions.
161    Return the appropriate CbcRangeCompare value (first argument being the
162    sub/superset if that's the case). In case of overlap (and if \c
163    replaceIfOverlap is true) replace the current branching object with one
164    whose feasible region is the overlap.
165*/
166CbcRangeCompare
167CbcNWayBranchingObject::compareBranchingObject
168(const CbcBranchingObject* /*brObj*/, const bool /*replaceIfOverlap*/)
169{
170    throw("must implement");
171}
Note: See TracBrowser for help on using the repository browser.