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