1 | // Copyright (C) 2002, International Business Machines |
---|
2 | // Corporation and others. All Rights Reserved. |
---|
3 | #if defined(_MSC_VER) |
---|
4 | // Turn off compiler warning about long names |
---|
5 | # pragma warning(disable:4786) |
---|
6 | #endif |
---|
7 | #include <cassert> |
---|
8 | #include <cmath> |
---|
9 | #include <cfloat> |
---|
10 | |
---|
11 | #include "OsiSolverInterface.hpp" |
---|
12 | #include "CbcModel.hpp" |
---|
13 | #include "CbcMessage.hpp" |
---|
14 | #include "CbcBranchBase.hpp" |
---|
15 | |
---|
16 | |
---|
17 | // Default Constructor |
---|
18 | CbcObject::CbcObject() |
---|
19 | :model_(NULL), |
---|
20 | id_(-1) |
---|
21 | { |
---|
22 | } |
---|
23 | |
---|
24 | // Constructor from model |
---|
25 | CbcObject::CbcObject(CbcModel * model) |
---|
26 | : |
---|
27 | model_(model), |
---|
28 | id_(-1) |
---|
29 | { |
---|
30 | } |
---|
31 | |
---|
32 | |
---|
33 | // Destructor |
---|
34 | CbcObject::~CbcObject () |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | // Copy constructor |
---|
39 | CbcObject::CbcObject ( const CbcObject & rhs) |
---|
40 | { |
---|
41 | model_ = rhs.model_; |
---|
42 | id_ = rhs.id_; |
---|
43 | } |
---|
44 | |
---|
45 | // Assignment operator |
---|
46 | CbcObject & |
---|
47 | CbcObject::operator=( const CbcObject& rhs) |
---|
48 | { |
---|
49 | if (this!=&rhs) { |
---|
50 | model_ = rhs.model_; |
---|
51 | id_ = rhs.id_; |
---|
52 | } |
---|
53 | return *this; |
---|
54 | } |
---|
55 | |
---|
56 | /* Returns floor and ceiling i.e. closest valid points |
---|
57 | */ |
---|
58 | void |
---|
59 | CbcObject::floorCeiling(double & floorValue, double & ceilingValue, double value, |
---|
60 | double tolerance) const |
---|
61 | { |
---|
62 | if (fabs(floor(value+0.5)-value)>tolerance) { |
---|
63 | floorValue = floor(value); |
---|
64 | } else { |
---|
65 | floorValue = floor(value+0.5); |
---|
66 | } |
---|
67 | ceilingValue = floorValue+1.0; |
---|
68 | } |
---|
69 | // Return "up" estimate (default 1.0e-5) |
---|
70 | double |
---|
71 | CbcObject::upEstimate() const |
---|
72 | { |
---|
73 | return 1.0e-5; |
---|
74 | } |
---|
75 | // Return "down" estimate (default 1.0e-5) |
---|
76 | double |
---|
77 | CbcObject::downEstimate() const |
---|
78 | { |
---|
79 | return 1.0e-5; |
---|
80 | } |
---|
81 | // Default Constructor |
---|
82 | CbcBranchingObject::CbcBranchingObject() |
---|
83 | { |
---|
84 | model_=NULL; |
---|
85 | variable_=-1; |
---|
86 | way_=0; |
---|
87 | value_=0.0; |
---|
88 | numberBranchesLeft_=0; |
---|
89 | } |
---|
90 | |
---|
91 | // Useful constructor |
---|
92 | CbcBranchingObject::CbcBranchingObject (CbcModel * model, int variable, int way , double value) |
---|
93 | { |
---|
94 | model_= model; |
---|
95 | variable_=variable; |
---|
96 | way_=way; |
---|
97 | value_=value; |
---|
98 | numberBranchesLeft_=2; |
---|
99 | } |
---|
100 | |
---|
101 | // Copy constructor |
---|
102 | CbcBranchingObject::CbcBranchingObject ( const CbcBranchingObject & rhs) |
---|
103 | { |
---|
104 | model_=rhs.model_; |
---|
105 | variable_=rhs.variable_; |
---|
106 | way_=rhs.way_; |
---|
107 | value_=rhs.value_; |
---|
108 | numberBranchesLeft_=rhs.numberBranchesLeft_; |
---|
109 | } |
---|
110 | |
---|
111 | // Assignment operator |
---|
112 | CbcBranchingObject & |
---|
113 | CbcBranchingObject::operator=( const CbcBranchingObject& rhs) |
---|
114 | { |
---|
115 | if (this != &rhs) { |
---|
116 | model_=rhs.model_; |
---|
117 | variable_=rhs.variable_; |
---|
118 | way_=rhs.way_; |
---|
119 | value_=rhs.value_; |
---|
120 | numberBranchesLeft_=rhs.numberBranchesLeft_; |
---|
121 | } |
---|
122 | return *this; |
---|
123 | } |
---|
124 | |
---|
125 | // Destructor |
---|
126 | CbcBranchingObject::~CbcBranchingObject () |
---|
127 | { |
---|
128 | } |
---|
129 | // Default Constructor |
---|
130 | CbcBranchDecision::CbcBranchDecision () |
---|
131 | { |
---|
132 | } |
---|
133 | |
---|
134 | CbcBranchDecision::~CbcBranchDecision() |
---|
135 | { |
---|
136 | } |
---|
137 | /* Compare N branching objects. Return index of best |
---|
138 | and sets way of branching in chosen object. |
---|
139 | |
---|
140 | This routine is used only after strong branching. |
---|
141 | This is reccommended version as it can be more sophisticated |
---|
142 | */ |
---|
143 | |
---|
144 | int |
---|
145 | CbcBranchDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects, |
---|
146 | int numberUnsatisfied, |
---|
147 | double * changeUp, int * numberInfeasibilitiesUp, |
---|
148 | double * changeDown, int * numberInfeasibilitiesDown, |
---|
149 | double objectiveValue) |
---|
150 | { |
---|
151 | int bestWay=0; |
---|
152 | int whichObject = -1; |
---|
153 | if (numberObjects) { |
---|
154 | initialize(objects[0]->model()); |
---|
155 | CbcBranchingObject * bestObject = NULL; |
---|
156 | for (int i = 0 ; i < numberObjects ; i++) { |
---|
157 | int betterWay = betterBranch(objects[i], |
---|
158 | bestObject, |
---|
159 | changeUp[i], |
---|
160 | numberInfeasibilitiesUp [i], |
---|
161 | changeDown[i], |
---|
162 | numberInfeasibilitiesDown[i] ); |
---|
163 | if (betterWay) { |
---|
164 | bestObject = objects[i]; |
---|
165 | bestWay = betterWay; |
---|
166 | whichObject=i; |
---|
167 | } |
---|
168 | } |
---|
169 | // set way in best |
---|
170 | if (whichObject>=0) |
---|
171 | objects[whichObject]->way(bestWay); |
---|
172 | else |
---|
173 | printf("debug\n"); |
---|
174 | } |
---|
175 | return whichObject; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|