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 "CbcBranchUser.hpp" |
---|
15 | #include "CoinSort.hpp" |
---|
16 | |
---|
17 | // Default Constructor // Default Constructor |
---|
18 | CbcBranchUserDecision::CbcBranchUserDecision() |
---|
19 | :CbcBranchDecision() |
---|
20 | { |
---|
21 | } |
---|
22 | |
---|
23 | // Copy constructor |
---|
24 | CbcBranchUserDecision::CbcBranchUserDecision ( |
---|
25 | const CbcBranchUserDecision & rhs) |
---|
26 | :CbcBranchDecision(rhs) |
---|
27 | { |
---|
28 | } |
---|
29 | |
---|
30 | CbcBranchUserDecision::~CbcBranchUserDecision() |
---|
31 | { |
---|
32 | } |
---|
33 | |
---|
34 | // Clone |
---|
35 | CbcBranchDecision * |
---|
36 | CbcBranchUserDecision::clone() const |
---|
37 | { |
---|
38 | return new CbcBranchUserDecision(*this); |
---|
39 | } |
---|
40 | |
---|
41 | // Initialize i.e. before start of choosing at a node |
---|
42 | void |
---|
43 | CbcBranchUserDecision::initialize(CbcModel * model) |
---|
44 | { |
---|
45 | } |
---|
46 | |
---|
47 | |
---|
48 | /* Returns nonzero if branching on first object is "better" than on |
---|
49 | second (if second NULL first wins). User can play with decision object. |
---|
50 | This is only used after strong branching. The initial selection |
---|
51 | is done by infeasibility() for each CbcObject |
---|
52 | return code +1 for up branch preferred, -1 for down |
---|
53 | |
---|
54 | */ |
---|
55 | int |
---|
56 | CbcBranchUserDecision::betterBranch(CbcBranchingObject * thisOne, |
---|
57 | CbcBranchingObject * bestSoFar, |
---|
58 | double changeUp, int numberInfeasibilitiesUp, |
---|
59 | double changeDown, int numberInfeasibilitiesDown) |
---|
60 | { |
---|
61 | printf("Now obsolete CbcBranchUserDecision::betterBranch\n"); |
---|
62 | abort(); |
---|
63 | return 0; |
---|
64 | } |
---|
65 | /* Compare N branching objects. Return index of best |
---|
66 | and sets way of branching in chosen object. |
---|
67 | |
---|
68 | This routine is used only after strong branching. |
---|
69 | This is reccommended version as it can be more sophisticated |
---|
70 | */ |
---|
71 | |
---|
72 | int |
---|
73 | CbcBranchUserDecision::bestBranch (CbcBranchingObject ** objects, int numberObjects, |
---|
74 | int numberUnsatisfied, |
---|
75 | double * changeUp, int * numberInfeasibilitiesUp, |
---|
76 | double * changeDown, int * numberInfeasibilitiesDown, |
---|
77 | double objectiveValue) |
---|
78 | { |
---|
79 | |
---|
80 | int bestWay=0; |
---|
81 | int whichObject = -1; |
---|
82 | if (numberObjects) { |
---|
83 | CbcModel * model = objects[0]->model(); |
---|
84 | // at continuous |
---|
85 | //double continuousObjective = model->getContinuousObjective(); |
---|
86 | //int continuousInfeasibilities = model->getContinuousInfeasibilities(); |
---|
87 | |
---|
88 | // average cost to get rid of infeasibility |
---|
89 | //double averageCostPerInfeasibility = |
---|
90 | //(objectiveValue-continuousObjective)/ |
---|
91 | //(double) (abs(continuousInfeasibilities-numberUnsatisfied)+1); |
---|
92 | /* beforeSolution is : |
---|
93 | 0 - before any solution |
---|
94 | n - n heuristic solutions but no branched one |
---|
95 | -1 - branched solution found |
---|
96 | */ |
---|
97 | int numberSolutions = model->getSolutionCount(); |
---|
98 | double cutoff = model->getCutoff(); |
---|
99 | int method=0; |
---|
100 | int i; |
---|
101 | if (numberSolutions) { |
---|
102 | int numberHeuristic = model->getNumberHeuristicSolutions(); |
---|
103 | if (numberHeuristic<numberSolutions) { |
---|
104 | method = 1; |
---|
105 | } else { |
---|
106 | method = 2; |
---|
107 | // look further |
---|
108 | for ( i = 0 ; i < numberObjects ; i++) { |
---|
109 | int numberNext = numberInfeasibilitiesUp[i]; |
---|
110 | |
---|
111 | if (numberNext<numberUnsatisfied) { |
---|
112 | int numberUp = numberUnsatisfied - numberInfeasibilitiesUp[i]; |
---|
113 | double perUnsatisfied = changeUp[i]/(double) numberUp; |
---|
114 | double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied; |
---|
115 | if (estimatedObjective<cutoff) |
---|
116 | method=3; |
---|
117 | } |
---|
118 | numberNext = numberInfeasibilitiesDown[i]; |
---|
119 | if (numberNext<numberUnsatisfied) { |
---|
120 | int numberDown = numberUnsatisfied - numberInfeasibilitiesDown[i]; |
---|
121 | double perUnsatisfied = changeDown[i]/(double) numberDown; |
---|
122 | double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied; |
---|
123 | if (estimatedObjective<cutoff) |
---|
124 | method=3; |
---|
125 | } |
---|
126 | } |
---|
127 | } |
---|
128 | method=2; |
---|
129 | } else { |
---|
130 | method = 0; |
---|
131 | } |
---|
132 | // Uncomment next to force method 4 |
---|
133 | //method=4; |
---|
134 | /* Methods : |
---|
135 | 0 - fewest infeasibilities |
---|
136 | 1 - largest min change in objective |
---|
137 | 2 - as 1 but use sum of changes if min close |
---|
138 | 3 - predicted best solution |
---|
139 | 4 - take cheapest up branch if infeasibilities same |
---|
140 | */ |
---|
141 | int bestNumber=INT_MAX; |
---|
142 | double bestCriterion=-1.0e50; |
---|
143 | double alternativeCriterion = -1.0; |
---|
144 | double bestEstimate = 1.0e100; |
---|
145 | switch (method) { |
---|
146 | case 0: |
---|
147 | // could add in depth as well |
---|
148 | for ( i = 0 ; i < numberObjects ; i++) { |
---|
149 | int thisNumber = min(numberInfeasibilitiesUp[i],numberInfeasibilitiesDown[i]); |
---|
150 | if (thisNumber<=bestNumber) { |
---|
151 | int betterWay=0; |
---|
152 | if (numberInfeasibilitiesUp[i]<numberInfeasibilitiesDown[i]) { |
---|
153 | if (numberInfeasibilitiesUp[i]<bestNumber) { |
---|
154 | betterWay = 1; |
---|
155 | } else { |
---|
156 | if (changeUp[i]<bestCriterion) |
---|
157 | betterWay=1; |
---|
158 | } |
---|
159 | } else if (numberInfeasibilitiesUp[i]>numberInfeasibilitiesDown[i]) { |
---|
160 | if (numberInfeasibilitiesDown[i]<bestNumber) { |
---|
161 | betterWay = -1; |
---|
162 | } else { |
---|
163 | if (changeDown[i]<bestCriterion) |
---|
164 | betterWay=-1; |
---|
165 | } |
---|
166 | } else { |
---|
167 | // up and down have same number |
---|
168 | bool better=false; |
---|
169 | if (numberInfeasibilitiesUp[i]<bestNumber) { |
---|
170 | better=true; |
---|
171 | } else if (numberInfeasibilitiesUp[i]==bestNumber) { |
---|
172 | if (min(changeUp[i],changeDown[i])<bestCriterion) |
---|
173 | better=true;; |
---|
174 | } |
---|
175 | if (better) { |
---|
176 | // see which way |
---|
177 | if (changeUp[i]<=changeDown[i]) |
---|
178 | betterWay=1; |
---|
179 | else |
---|
180 | betterWay=-1; |
---|
181 | } |
---|
182 | } |
---|
183 | if (betterWay) { |
---|
184 | bestCriterion = min(changeUp[i],changeDown[i]); |
---|
185 | bestNumber = thisNumber; |
---|
186 | whichObject = i; |
---|
187 | bestWay = betterWay; |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | break; |
---|
192 | case 1: |
---|
193 | for ( i = 0 ; i < numberObjects ; i++) { |
---|
194 | int betterWay=0; |
---|
195 | if (changeUp[i]<=changeDown[i]) { |
---|
196 | if (changeUp[i]>bestCriterion) |
---|
197 | betterWay=1; |
---|
198 | } else { |
---|
199 | if (changeDown[i]>bestCriterion) |
---|
200 | betterWay=-1; |
---|
201 | } |
---|
202 | if (betterWay) { |
---|
203 | bestCriterion = min(changeUp[i],changeDown[i]); |
---|
204 | whichObject = i; |
---|
205 | bestWay = betterWay; |
---|
206 | } |
---|
207 | } |
---|
208 | break; |
---|
209 | case 2: |
---|
210 | for ( i = 0 ; i < numberObjects ; i++) { |
---|
211 | double change = min(changeUp[i],changeDown[i]); |
---|
212 | double sum = changeUp[i] + changeDown[i]; |
---|
213 | bool take=false; |
---|
214 | if (change>1.1*bestCriterion) |
---|
215 | take=true; |
---|
216 | else if (change>0.9*bestCriterion&&sum+change>bestCriterion+alternativeCriterion) |
---|
217 | take=true; |
---|
218 | if (take) { |
---|
219 | if (changeUp[i]<=changeDown[i]) { |
---|
220 | if (changeUp[i]>bestCriterion) |
---|
221 | bestWay=1; |
---|
222 | } else { |
---|
223 | if (changeDown[i]>bestCriterion) |
---|
224 | bestWay=-1; |
---|
225 | } |
---|
226 | bestCriterion = change; |
---|
227 | alternativeCriterion = sum; |
---|
228 | whichObject = i; |
---|
229 | } |
---|
230 | } |
---|
231 | break; |
---|
232 | case 3: |
---|
233 | for ( i = 0 ; i < numberObjects ; i++) { |
---|
234 | int numberNext = numberInfeasibilitiesUp[i]; |
---|
235 | |
---|
236 | if (numberNext<numberUnsatisfied) { |
---|
237 | int numberUp = numberUnsatisfied - numberInfeasibilitiesUp[i]; |
---|
238 | double perUnsatisfied = changeUp[i]/(double) numberUp; |
---|
239 | double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied; |
---|
240 | if (estimatedObjective<bestEstimate) { |
---|
241 | bestEstimate = estimatedObjective; |
---|
242 | bestWay=1; |
---|
243 | whichObject=i; |
---|
244 | } |
---|
245 | } |
---|
246 | numberNext = numberInfeasibilitiesDown[i]; |
---|
247 | if (numberNext<numberUnsatisfied) { |
---|
248 | int numberDown = numberUnsatisfied - numberInfeasibilitiesDown[i]; |
---|
249 | double perUnsatisfied = changeDown[i]/(double) numberDown; |
---|
250 | double estimatedObjective = objectiveValue + numberUnsatisfied * perUnsatisfied; |
---|
251 | if (estimatedObjective<bestEstimate) { |
---|
252 | bestEstimate = estimatedObjective; |
---|
253 | bestWay=-1; |
---|
254 | whichObject=i; |
---|
255 | } |
---|
256 | } |
---|
257 | } |
---|
258 | break; |
---|
259 | case 4: |
---|
260 | // if number infeas same then cheapest up |
---|
261 | // first get best number or when going down |
---|
262 | // now choose smallest change up amongst equal number infeas |
---|
263 | for ( i = 0 ; i < numberObjects ; i++) { |
---|
264 | int thisNumber = min(numberInfeasibilitiesUp[i],numberInfeasibilitiesDown[i]); |
---|
265 | if (thisNumber<=bestNumber) { |
---|
266 | int betterWay=0; |
---|
267 | if (numberInfeasibilitiesUp[i]<numberInfeasibilitiesDown[i]) { |
---|
268 | if (numberInfeasibilitiesUp[i]<bestNumber) { |
---|
269 | betterWay = 1; |
---|
270 | } else { |
---|
271 | if (changeUp[i]<bestCriterion) |
---|
272 | betterWay=1; |
---|
273 | } |
---|
274 | } else if (numberInfeasibilitiesUp[i]>numberInfeasibilitiesDown[i]) { |
---|
275 | if (numberInfeasibilitiesDown[i]<bestNumber) { |
---|
276 | betterWay = -1; |
---|
277 | } else { |
---|
278 | if (changeDown[i]<bestCriterion) |
---|
279 | betterWay=-1; |
---|
280 | } |
---|
281 | } else { |
---|
282 | // up and down have same number |
---|
283 | bool better=false; |
---|
284 | if (numberInfeasibilitiesUp[i]<bestNumber) { |
---|
285 | better=true; |
---|
286 | } else if (numberInfeasibilitiesUp[i]==bestNumber) { |
---|
287 | if (min(changeUp[i],changeDown[i])<bestCriterion) |
---|
288 | better=true;; |
---|
289 | } |
---|
290 | if (better) { |
---|
291 | // see which way |
---|
292 | if (changeUp[i]<=changeDown[i]) |
---|
293 | betterWay=1; |
---|
294 | else |
---|
295 | betterWay=-1; |
---|
296 | } |
---|
297 | } |
---|
298 | if (betterWay) { |
---|
299 | bestCriterion = min(changeUp[i],changeDown[i]); |
---|
300 | bestNumber = thisNumber; |
---|
301 | whichObject = i; |
---|
302 | bestWay = betterWay; |
---|
303 | } |
---|
304 | } |
---|
305 | } |
---|
306 | bestCriterion=1.0e50; |
---|
307 | for ( i = 0 ; i < numberObjects ; i++) { |
---|
308 | int thisNumber = numberInfeasibilitiesUp[i]; |
---|
309 | if (thisNumber==bestNumber&&changeUp) { |
---|
310 | if (changeUp[i]<bestCriterion) { |
---|
311 | bestCriterion = changeUp[i]; |
---|
312 | whichObject = i; |
---|
313 | bestWay = 1; |
---|
314 | } |
---|
315 | } |
---|
316 | } |
---|
317 | break; |
---|
318 | } |
---|
319 | // set way in best |
---|
320 | if (whichObject>=0) |
---|
321 | objects[whichObject]->way(bestWay); |
---|
322 | } |
---|
323 | return whichObject; |
---|
324 | } |
---|