1 | /* $Id: CbcBranchBase.cpp 1573 2011-01-05 01:12:36Z tkr $ */ |
---|
2 | // Copyright (C) 2002, 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 | #include <cassert> |
---|
11 | #include <cstdlib> |
---|
12 | #include <cmath> |
---|
13 | #include <cfloat> |
---|
14 | |
---|
15 | #include "OsiSolverInterface.hpp" |
---|
16 | #include "OsiSolverBranch.hpp" |
---|
17 | #include "OsiChooseVariable.hpp" |
---|
18 | #include "CbcModel.hpp" |
---|
19 | #include "CbcMessage.hpp" |
---|
20 | #include "CbcBranchBase.hpp" |
---|
21 | |
---|
22 | |
---|
23 | // Default Constructor |
---|
24 | CbcObject::CbcObject() |
---|
25 | : OsiObject(), |
---|
26 | model_(NULL), |
---|
27 | id_(-1), |
---|
28 | position_(-1), |
---|
29 | preferredWay_(0) |
---|
30 | { |
---|
31 | } |
---|
32 | |
---|
33 | // Constructor from model |
---|
34 | CbcObject::CbcObject(CbcModel * model) |
---|
35 | : OsiObject(), |
---|
36 | model_(model), |
---|
37 | id_(-1), |
---|
38 | position_(-1), |
---|
39 | preferredWay_(0) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | // Destructor |
---|
45 | CbcObject::~CbcObject () |
---|
46 | { |
---|
47 | } |
---|
48 | |
---|
49 | // Copy constructor |
---|
50 | CbcObject::CbcObject ( const CbcObject & rhs) |
---|
51 | : OsiObject(rhs) |
---|
52 | { |
---|
53 | model_ = rhs.model_; |
---|
54 | id_ = rhs.id_; |
---|
55 | position_ = rhs.position_; |
---|
56 | preferredWay_ = rhs.preferredWay_; |
---|
57 | } |
---|
58 | |
---|
59 | // Assignment operator |
---|
60 | CbcObject & |
---|
61 | CbcObject::operator=( const CbcObject & rhs) |
---|
62 | { |
---|
63 | if (this != &rhs) { |
---|
64 | OsiObject::operator=(rhs); |
---|
65 | model_ = rhs.model_; |
---|
66 | id_ = rhs.id_; |
---|
67 | position_ = rhs.position_; |
---|
68 | preferredWay_ = rhs.preferredWay_; |
---|
69 | } |
---|
70 | return *this; |
---|
71 | } |
---|
72 | |
---|
73 | /* Returns floor and ceiling i.e. closest valid points |
---|
74 | */ |
---|
75 | void |
---|
76 | CbcObject::floorCeiling(double & floorValue, double & ceilingValue, double value, |
---|
77 | double tolerance) const |
---|
78 | { |
---|
79 | if (fabs(floor(value + 0.5) - value) > tolerance) { |
---|
80 | floorValue = floor(value); |
---|
81 | } else { |
---|
82 | floorValue = floor(value + 0.5); |
---|
83 | } |
---|
84 | ceilingValue = floorValue + 1.0; |
---|
85 | } |
---|
86 | /* For the variable(s) referenced by the object, |
---|
87 | look at the current solution and set bounds to match the solution. |
---|
88 | Returns measure of how much it had to move solution to make feasible |
---|
89 | */ |
---|
90 | double |
---|
91 | CbcObject::feasibleRegion(OsiSolverInterface * /*solver*/) const |
---|
92 | { |
---|
93 | //assert (solver==model_->solver()); |
---|
94 | CbcObject * fudge = const_cast<CbcObject *>(this); |
---|
95 | fudge->feasibleRegion(); |
---|
96 | return 0.0; |
---|
97 | } |
---|
98 | |
---|
99 | /* For the variable(s) referenced by the object, |
---|
100 | look at the current solution and set bounds to match the solution. |
---|
101 | Returns measure of how much it had to move solution to make feasible |
---|
102 | */ |
---|
103 | double |
---|
104 | CbcObject::feasibleRegion(OsiSolverInterface * /*solver*/, |
---|
105 | const OsiBranchingInformation * /*info*/) const |
---|
106 | { |
---|
107 | //assert (solver==model_->solver()); |
---|
108 | CbcObject * fudge = const_cast<CbcObject *>(this); |
---|
109 | fudge->feasibleRegion(); |
---|
110 | return 0.0; |
---|
111 | } |
---|
112 | /* Create a branching object and indicate which way to branch first. |
---|
113 | |
---|
114 | The branching object has to know how to create branches (fix |
---|
115 | variables, etc.) |
---|
116 | */ |
---|
117 | OsiBranchingObject * |
---|
118 | CbcObject::createOsiBranch(OsiSolverInterface * solver, |
---|
119 | const OsiBranchingInformation * info, |
---|
120 | int way) const |
---|
121 | { |
---|
122 | //assert (solver==model_->solver()); |
---|
123 | CbcObject * fudge = const_cast<CbcObject *>(this); |
---|
124 | return fudge->createBranch(solver, info, way); |
---|
125 | } |
---|
126 | /* Create an OsiSolverBranch object |
---|
127 | |
---|
128 | This returns NULL if branch not represented by bound changes |
---|
129 | */ |
---|
130 | OsiSolverBranch * |
---|
131 | CbcObject::solverBranch() const |
---|
132 | { |
---|
133 | return NULL; |
---|
134 | } |
---|
135 | /* Pass in information on branch just done and create CbcObjectUpdateData instance. |
---|
136 | If object does not need data then backward pointer will be NULL. |
---|
137 | Assumes can get information from solver */ |
---|
138 | CbcObjectUpdateData |
---|
139 | CbcObject::createUpdateInformation(const OsiSolverInterface * /*solver*/, |
---|
140 | const CbcNode * /*node*/, |
---|
141 | const CbcBranchingObject * /*branchingObject*/) |
---|
142 | { |
---|
143 | return CbcObjectUpdateData(); |
---|
144 | } |
---|
145 | |
---|
146 | // Default Constructor |
---|
147 | CbcBranchingObject::CbcBranchingObject() |
---|
148 | : OsiBranchingObject() |
---|
149 | { |
---|
150 | model_ = NULL; |
---|
151 | originalCbcObject_ = NULL; |
---|
152 | variable_ = -1; |
---|
153 | way_ = 0; |
---|
154 | } |
---|
155 | |
---|
156 | // Useful constructor |
---|
157 | CbcBranchingObject::CbcBranchingObject (CbcModel * model, int variable, int way , double value) |
---|
158 | : OsiBranchingObject(model->solver(), value) |
---|
159 | { |
---|
160 | model_ = model; |
---|
161 | originalCbcObject_ = NULL; |
---|
162 | variable_ = variable; |
---|
163 | way_ = way; |
---|
164 | } |
---|
165 | |
---|
166 | // Copy constructor |
---|
167 | CbcBranchingObject::CbcBranchingObject ( const CbcBranchingObject & rhs) |
---|
168 | : OsiBranchingObject(rhs) |
---|
169 | { |
---|
170 | model_ = rhs.model_; |
---|
171 | originalCbcObject_ = rhs.originalCbcObject_; |
---|
172 | variable_ = rhs.variable_; |
---|
173 | way_ = rhs.way_; |
---|
174 | value_ = rhs.value_; |
---|
175 | } |
---|
176 | |
---|
177 | // Assignment operator |
---|
178 | CbcBranchingObject & |
---|
179 | CbcBranchingObject::operator=( const CbcBranchingObject & rhs) |
---|
180 | { |
---|
181 | if (this != &rhs) { |
---|
182 | OsiBranchingObject::operator=(rhs); |
---|
183 | model_ = rhs.model_; |
---|
184 | originalCbcObject_ = rhs.originalCbcObject_; |
---|
185 | variable_ = rhs.variable_; |
---|
186 | way_ = rhs.way_; |
---|
187 | } |
---|
188 | return *this; |
---|
189 | } |
---|
190 | |
---|
191 | // Destructor |
---|
192 | CbcBranchingObject::~CbcBranchingObject () |
---|
193 | { |
---|
194 | } |
---|
195 | // Default Constructor |
---|
196 | CbcBranchDecision::CbcBranchDecision () |
---|
197 | : object_(NULL), model_(NULL), chooseMethod_(NULL) |
---|
198 | { |
---|
199 | } |
---|
200 | |
---|
201 | // Copy Constructor |
---|
202 | CbcBranchDecision::CbcBranchDecision (const CbcBranchDecision &rhs) |
---|
203 | : object_(NULL), model_(rhs.model_), chooseMethod_(NULL) |
---|
204 | { |
---|
205 | if (rhs.chooseMethod_) |
---|
206 | chooseMethod_ = rhs.chooseMethod_->clone(); |
---|
207 | } |
---|
208 | |
---|
209 | CbcBranchDecision::~CbcBranchDecision() |
---|
210 | { |
---|
211 | delete object_; |
---|
212 | delete chooseMethod_; |
---|
213 | } |
---|
214 | /* Compare N branching objects. Return index of best |
---|
215 | and sets way of branching in chosen object. |
---|
216 | |
---|
217 | This routine is used only after strong branching. |
---|
218 | This is reccommended version as it can be more sophisticated |
---|
219 | */ |
---|
220 | |
---|
221 | int |
---|
222 | CbcBranchDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects, |
---|
223 | int /*numberUnsatisfied*/, |
---|
224 | double * changeUp, int * numberInfeasibilitiesUp, |
---|
225 | double * changeDown, int * numberInfeasibilitiesDown, |
---|
226 | double /*objectiveValue*/) |
---|
227 | { |
---|
228 | int bestWay = 0; |
---|
229 | int whichObject = -1; |
---|
230 | if (numberObjects) { |
---|
231 | initialize(objects[0]->model()); |
---|
232 | CbcBranchingObject * bestObject = NULL; |
---|
233 | for (int i = 0 ; i < numberObjects ; i++) { |
---|
234 | int betterWay = betterBranch(objects[i], |
---|
235 | bestObject, |
---|
236 | changeUp[i], |
---|
237 | numberInfeasibilitiesUp [i], |
---|
238 | changeDown[i], |
---|
239 | numberInfeasibilitiesDown[i] ); |
---|
240 | if (betterWay) { |
---|
241 | bestObject = objects[i]; |
---|
242 | bestWay = betterWay; |
---|
243 | whichObject = i; |
---|
244 | } |
---|
245 | } |
---|
246 | // set way in best |
---|
247 | if (whichObject >= 0) |
---|
248 | objects[whichObject]->way(bestWay); |
---|
249 | } |
---|
250 | return whichObject; |
---|
251 | } |
---|
252 | // Set (clone) chooseMethod |
---|
253 | void |
---|
254 | CbcBranchDecision::setChooseMethod(const OsiChooseVariable & method) |
---|
255 | { |
---|
256 | delete chooseMethod_; |
---|
257 | chooseMethod_ = method.clone(); |
---|
258 | } |
---|
259 | // Default constructor |
---|
260 | CbcConsequence::CbcConsequence() |
---|
261 | { |
---|
262 | } |
---|
263 | |
---|
264 | |
---|
265 | // Destructor |
---|
266 | CbcConsequence::~CbcConsequence () |
---|
267 | { |
---|
268 | } |
---|
269 | |
---|
270 | // Copy constructor |
---|
271 | CbcConsequence::CbcConsequence ( const CbcConsequence & /*rhs*/) |
---|
272 | { |
---|
273 | } |
---|
274 | |
---|
275 | // Assignment operator |
---|
276 | CbcConsequence & |
---|
277 | CbcConsequence::operator=( const CbcConsequence & rhs) |
---|
278 | { |
---|
279 | if (this != &rhs) { |
---|
280 | } |
---|
281 | return *this; |
---|
282 | } |
---|
283 | // Default constructor |
---|
284 | CbcObjectUpdateData::CbcObjectUpdateData() |
---|
285 | : object_(NULL), |
---|
286 | way_(0), |
---|
287 | objectNumber_(-1), |
---|
288 | change_(0.0), |
---|
289 | status_(0), |
---|
290 | intDecrease_(0), |
---|
291 | branchingValue_(0.0), |
---|
292 | originalObjective_(COIN_DBL_MAX), |
---|
293 | cutoff_(COIN_DBL_MAX) |
---|
294 | { |
---|
295 | } |
---|
296 | |
---|
297 | // Useful constructor |
---|
298 | CbcObjectUpdateData::CbcObjectUpdateData (CbcObject * object, |
---|
299 | int way, |
---|
300 | double change, |
---|
301 | int status, |
---|
302 | int intDecrease, |
---|
303 | double branchingValue) |
---|
304 | : object_(object), |
---|
305 | way_(way), |
---|
306 | objectNumber_(-1), |
---|
307 | change_(change), |
---|
308 | status_(status), |
---|
309 | intDecrease_(intDecrease), |
---|
310 | branchingValue_(branchingValue), |
---|
311 | originalObjective_(COIN_DBL_MAX), |
---|
312 | cutoff_(COIN_DBL_MAX) |
---|
313 | { |
---|
314 | } |
---|
315 | |
---|
316 | // Destructor |
---|
317 | CbcObjectUpdateData::~CbcObjectUpdateData () |
---|
318 | { |
---|
319 | } |
---|
320 | |
---|
321 | // Copy constructor |
---|
322 | CbcObjectUpdateData::CbcObjectUpdateData ( const CbcObjectUpdateData & rhs) |
---|
323 | : object_(rhs.object_), |
---|
324 | way_(rhs.way_), |
---|
325 | objectNumber_(rhs.objectNumber_), |
---|
326 | change_(rhs.change_), |
---|
327 | status_(rhs.status_), |
---|
328 | intDecrease_(rhs.intDecrease_), |
---|
329 | branchingValue_(rhs.branchingValue_), |
---|
330 | originalObjective_(rhs.originalObjective_), |
---|
331 | cutoff_(rhs.cutoff_) |
---|
332 | { |
---|
333 | } |
---|
334 | |
---|
335 | // Assignment operator |
---|
336 | CbcObjectUpdateData & |
---|
337 | CbcObjectUpdateData::operator=( const CbcObjectUpdateData & rhs) |
---|
338 | { |
---|
339 | if (this != &rhs) { |
---|
340 | object_ = rhs.object_; |
---|
341 | way_ = rhs.way_; |
---|
342 | objectNumber_ = rhs.objectNumber_; |
---|
343 | change_ = rhs.change_; |
---|
344 | status_ = rhs.status_; |
---|
345 | intDecrease_ = rhs.intDecrease_; |
---|
346 | branchingValue_ = rhs.branchingValue_; |
---|
347 | originalObjective_ = rhs.originalObjective_; |
---|
348 | cutoff_ = rhs.cutoff_; |
---|
349 | } |
---|
350 | return *this; |
---|
351 | } |
---|
352 | |
---|
353 | |
---|