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