1 | // Copyright (C) 2004, 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 | //#define CBC_DEBUG |
---|
11 | |
---|
12 | #include "CbcMessage.hpp" |
---|
13 | #include "CbcModel.hpp" |
---|
14 | #include "CbcTree.hpp" |
---|
15 | #include "CbcCompareActual.hpp" |
---|
16 | #include "CoinError.hpp" |
---|
17 | |
---|
18 | |
---|
19 | /** Default Constructor |
---|
20 | |
---|
21 | */ |
---|
22 | CbcCompareDepth::CbcCompareDepth () |
---|
23 | : CbcCompareBase() |
---|
24 | { |
---|
25 | test_=this; |
---|
26 | } |
---|
27 | |
---|
28 | // Copy constructor |
---|
29 | CbcCompareDepth::CbcCompareDepth ( const CbcCompareDepth & rhs) |
---|
30 | :CbcCompareBase(rhs) |
---|
31 | |
---|
32 | { |
---|
33 | } |
---|
34 | |
---|
35 | // Clone |
---|
36 | CbcCompareBase * |
---|
37 | CbcCompareDepth::clone() const |
---|
38 | { |
---|
39 | return new CbcCompareDepth(*this); |
---|
40 | } |
---|
41 | |
---|
42 | // Assignment operator |
---|
43 | CbcCompareDepth & |
---|
44 | CbcCompareDepth::operator=( const CbcCompareDepth& rhs) |
---|
45 | { |
---|
46 | if (this!=&rhs) { |
---|
47 | CbcCompareBase::operator=(rhs); |
---|
48 | } |
---|
49 | return *this; |
---|
50 | } |
---|
51 | |
---|
52 | // Destructor |
---|
53 | CbcCompareDepth::~CbcCompareDepth () |
---|
54 | { |
---|
55 | } |
---|
56 | |
---|
57 | // Returns true if y better than x |
---|
58 | bool |
---|
59 | CbcCompareDepth::test (CbcNode * x, CbcNode * y) |
---|
60 | { |
---|
61 | int testX = x->depth(); |
---|
62 | int testY = y->depth(); |
---|
63 | if (testX!=testY) |
---|
64 | return testX < testY; |
---|
65 | else |
---|
66 | return equalityTest(x,y); // so ties will be broken in consistent manner |
---|
67 | } |
---|
68 | // Create C++ lines to get to current state |
---|
69 | void |
---|
70 | CbcCompareDepth::generateCpp( FILE * fp) |
---|
71 | { |
---|
72 | fprintf(fp,"0#include \"CbcCompareActual.hpp\"\n"); |
---|
73 | fprintf(fp,"3 CbcCompareDepth compare;\n"); |
---|
74 | fprintf(fp,"3 cbcModel->setNodeComparison(compare);\n"); |
---|
75 | } |
---|
76 | |
---|
77 | /** Default Constructor |
---|
78 | |
---|
79 | */ |
---|
80 | CbcCompareObjective::CbcCompareObjective () |
---|
81 | : CbcCompareBase() |
---|
82 | { |
---|
83 | test_=this; |
---|
84 | } |
---|
85 | |
---|
86 | // Copy constructor |
---|
87 | CbcCompareObjective::CbcCompareObjective ( const CbcCompareObjective & rhs) |
---|
88 | :CbcCompareBase(rhs) |
---|
89 | |
---|
90 | { |
---|
91 | } |
---|
92 | |
---|
93 | // Clone |
---|
94 | CbcCompareBase * |
---|
95 | CbcCompareObjective::clone() const |
---|
96 | { |
---|
97 | return new CbcCompareObjective(*this); |
---|
98 | } |
---|
99 | |
---|
100 | // Assignment operator |
---|
101 | CbcCompareObjective & |
---|
102 | CbcCompareObjective::operator=( const CbcCompareObjective& rhs) |
---|
103 | { |
---|
104 | if (this!=&rhs) { |
---|
105 | CbcCompareBase::operator=(rhs); |
---|
106 | } |
---|
107 | return *this; |
---|
108 | } |
---|
109 | |
---|
110 | // Destructor |
---|
111 | CbcCompareObjective::~CbcCompareObjective () |
---|
112 | { |
---|
113 | } |
---|
114 | |
---|
115 | // Returns true if y better than x |
---|
116 | bool |
---|
117 | CbcCompareObjective::test (CbcNode * x, CbcNode * y) |
---|
118 | { |
---|
119 | double testX = x->objectiveValue(); |
---|
120 | double testY = y->objectiveValue(); |
---|
121 | if (testX!=testY) |
---|
122 | return testX > testY; |
---|
123 | else |
---|
124 | return equalityTest(x,y); // so ties will be broken in consistent manner |
---|
125 | } |
---|
126 | // Create C++ lines to get to current state |
---|
127 | void |
---|
128 | CbcCompareObjective::generateCpp( FILE * fp) |
---|
129 | { |
---|
130 | fprintf(fp,"0#include \"CbcCompareActual.hpp\"\n"); |
---|
131 | fprintf(fp,"3 CbcCompareObjective compare;\n"); |
---|
132 | fprintf(fp,"3 cbcModel->setNodeComparison(compare);\n"); |
---|
133 | } |
---|
134 | |
---|
135 | |
---|
136 | /** Default Constructor |
---|
137 | |
---|
138 | */ |
---|
139 | CbcCompareDefault::CbcCompareDefault () |
---|
140 | : CbcCompareBase(), |
---|
141 | weight_(-1.0), |
---|
142 | saveWeight_(0.0), |
---|
143 | numberSolutions_(0), |
---|
144 | treeSize_(0) |
---|
145 | { |
---|
146 | test_=this; |
---|
147 | } |
---|
148 | |
---|
149 | // Constructor with weight |
---|
150 | CbcCompareDefault::CbcCompareDefault (double weight) |
---|
151 | : CbcCompareBase(), |
---|
152 | weight_(weight) , |
---|
153 | saveWeight_(0.0), |
---|
154 | numberSolutions_(0), |
---|
155 | treeSize_(0) |
---|
156 | { |
---|
157 | test_=this; |
---|
158 | } |
---|
159 | |
---|
160 | |
---|
161 | // Copy constructor |
---|
162 | CbcCompareDefault::CbcCompareDefault ( const CbcCompareDefault & rhs) |
---|
163 | :CbcCompareBase(rhs) |
---|
164 | |
---|
165 | { |
---|
166 | weight_=rhs.weight_; |
---|
167 | saveWeight_ = rhs.saveWeight_; |
---|
168 | numberSolutions_=rhs.numberSolutions_; |
---|
169 | treeSize_ = rhs.treeSize_; |
---|
170 | } |
---|
171 | |
---|
172 | // Clone |
---|
173 | CbcCompareBase * |
---|
174 | CbcCompareDefault::clone() const |
---|
175 | { |
---|
176 | return new CbcCompareDefault(*this); |
---|
177 | } |
---|
178 | |
---|
179 | // Assignment operator |
---|
180 | CbcCompareDefault & |
---|
181 | CbcCompareDefault::operator=( const CbcCompareDefault& rhs) |
---|
182 | { |
---|
183 | if (this!=&rhs) { |
---|
184 | CbcCompareBase::operator=(rhs); |
---|
185 | weight_=rhs.weight_; |
---|
186 | saveWeight_ = rhs.saveWeight_; |
---|
187 | numberSolutions_=rhs.numberSolutions_; |
---|
188 | treeSize_ = rhs.treeSize_; |
---|
189 | } |
---|
190 | return *this; |
---|
191 | } |
---|
192 | |
---|
193 | // Destructor |
---|
194 | CbcCompareDefault::~CbcCompareDefault () |
---|
195 | { |
---|
196 | } |
---|
197 | |
---|
198 | // Returns true if y better than x |
---|
199 | bool |
---|
200 | CbcCompareDefault::test (CbcNode * x, CbcNode * y) |
---|
201 | { |
---|
202 | #if 0 |
---|
203 | // was |
---|
204 | if (weight_<0.0||treeSize_>100000) { |
---|
205 | // before solution |
---|
206 | /* printf("x %d %d %g, y %d %d %g\n", |
---|
207 | x->numberUnsatisfied(),x->depth(),x->objectiveValue(), |
---|
208 | y->numberUnsatisfied(),y->depth(),y->objectiveValue()); */ |
---|
209 | if (x->numberUnsatisfied() > y->numberUnsatisfied()) |
---|
210 | return true; |
---|
211 | else if (x->numberUnsatisfied() < y->numberUnsatisfied()) |
---|
212 | return false; |
---|
213 | else |
---|
214 | return x->depth() < y->depth(); |
---|
215 | } else { |
---|
216 | // after solution |
---|
217 | return x->objectiveValue()+ weight_*x->numberUnsatisfied() > |
---|
218 | y->objectiveValue() + weight_*y->numberUnsatisfied(); |
---|
219 | } |
---|
220 | #else |
---|
221 | if ((weight_==-1.0&&(y->depth()>7||x->depth()>7))||weight_==-3.0) { |
---|
222 | int adjust = (weight_==-3.0) ? 10000 : 0; |
---|
223 | // before solution |
---|
224 | /*printf("x %d %d %g, y %d %d %g\n", |
---|
225 | x->numberUnsatisfied(),x->depth(),x->objectiveValue(), |
---|
226 | y->numberUnsatisfied(),y->depth(),y->objectiveValue()); */ |
---|
227 | if (x->numberUnsatisfied() > y->numberUnsatisfied()+adjust) { |
---|
228 | return true; |
---|
229 | } else if (x->numberUnsatisfied() < y->numberUnsatisfied()-adjust) { |
---|
230 | return false; |
---|
231 | } else { |
---|
232 | int depthX = x->depth(); |
---|
233 | int depthY = y->depth(); |
---|
234 | if (depthX!=depthY) |
---|
235 | return depthX < depthY; |
---|
236 | else |
---|
237 | return equalityTest(x,y); // so ties will be broken in consistent manner |
---|
238 | } |
---|
239 | } else { |
---|
240 | // always choose *smallest* depth if <= 7 |
---|
241 | int depthX = x->depth(); |
---|
242 | int depthY = y->depth(); |
---|
243 | if (depthX<=7||depthY<=7) { |
---|
244 | if (depthX!=depthY) { |
---|
245 | return depthX < depthY; |
---|
246 | } |
---|
247 | } |
---|
248 | // after solution |
---|
249 | double weight = CoinMax(weight_,0.0); |
---|
250 | double testX = x->objectiveValue()+ weight*x->numberUnsatisfied(); |
---|
251 | double testY = y->objectiveValue() + weight*y->numberUnsatisfied(); |
---|
252 | if (testX!=testY) |
---|
253 | return testX > testY; |
---|
254 | else |
---|
255 | return equalityTest(x,y); // so ties will be broken in consistent manner |
---|
256 | } |
---|
257 | #endif |
---|
258 | } |
---|
259 | // This allows method to change behavior as it is called |
---|
260 | // after each solution |
---|
261 | void |
---|
262 | CbcCompareDefault::newSolution(CbcModel * model, |
---|
263 | double objectiveAtContinuous, |
---|
264 | int numberInfeasibilitiesAtContinuous) |
---|
265 | { |
---|
266 | if (model->getSolutionCount()==model->getNumberHeuristicSolutions()&& |
---|
267 | model->getSolutionCount()<5&&model->getNodeCount()<500) |
---|
268 | return; // solution was got by rounding |
---|
269 | // set to get close to this solution |
---|
270 | double costPerInteger = |
---|
271 | (model->getObjValue()-objectiveAtContinuous)/ |
---|
272 | ((double) numberInfeasibilitiesAtContinuous); |
---|
273 | weight_ = 0.95*costPerInteger; |
---|
274 | saveWeight_ = 0.95*weight_; |
---|
275 | numberSolutions_++; |
---|
276 | if (numberSolutions_>5) |
---|
277 | weight_ =0.0; // this searches on objective |
---|
278 | } |
---|
279 | // This allows method to change behavior |
---|
280 | bool |
---|
281 | CbcCompareDefault::every1000Nodes(CbcModel * model, int numberNodes) |
---|
282 | { |
---|
283 | #if 0 |
---|
284 | // was |
---|
285 | if (numberNodes>10000) |
---|
286 | weight_ =0.0; // this searches on objective |
---|
287 | // get size of tree |
---|
288 | treeSize_ = model->tree()->size(); |
---|
289 | #else |
---|
290 | double saveWeight=weight_; |
---|
291 | int numberNodes1000 = numberNodes/1000; |
---|
292 | if (numberNodes>10000) { |
---|
293 | weight_ =0.0; // this searches on objective |
---|
294 | // but try a bit of other stuff |
---|
295 | if ((numberNodes1000%4)==1) |
---|
296 | weight_=saveWeight_; |
---|
297 | } else if (numberNodes==1000&&weight_==-2.0) { |
---|
298 | weight_=-1.0; // Go to depth first |
---|
299 | } |
---|
300 | // get size of tree |
---|
301 | treeSize_ = model->tree()->size(); |
---|
302 | if (treeSize_>10000) { |
---|
303 | int n1 = model->solver()->getNumRows()+model->solver()->getNumCols(); |
---|
304 | int n2 = model->numberObjects(); |
---|
305 | double size = n1*0.1 + n2*2.0; |
---|
306 | // set weight to reduce size most of time |
---|
307 | if (treeSize_*size>5.0e7) |
---|
308 | weight_=-1.0; |
---|
309 | else if ((numberNodes1000%4)==0&&treeSize_*size>1.0e6) |
---|
310 | weight_=-1.0; |
---|
311 | else if ((numberNodes1000%4)==1) |
---|
312 | weight_=0.0; |
---|
313 | else |
---|
314 | weight_=saveWeight_; |
---|
315 | } |
---|
316 | #endif |
---|
317 | //return numberNodes==11000; // resort if first time |
---|
318 | return (weight_!=saveWeight); |
---|
319 | } |
---|
320 | |
---|
321 | // Create C++ lines to get to current state |
---|
322 | void |
---|
323 | CbcCompareDefault::generateCpp( FILE * fp) |
---|
324 | { |
---|
325 | CbcCompareDefault other; |
---|
326 | fprintf(fp,"0#include \"CbcCompareActual.hpp\"\n"); |
---|
327 | fprintf(fp,"3 CbcCompareDefault compare;\n"); |
---|
328 | if (weight_!=other.weight_) |
---|
329 | fprintf(fp,"3 compare.setWeight(%g);\n",weight_); |
---|
330 | fprintf(fp,"3 cbcModel->setNodeComparison(compare);\n"); |
---|
331 | } |
---|
332 | |
---|
333 | /** Default Constructor |
---|
334 | |
---|
335 | */ |
---|
336 | CbcCompareEstimate::CbcCompareEstimate () |
---|
337 | : CbcCompareBase() |
---|
338 | { |
---|
339 | test_=this; |
---|
340 | } |
---|
341 | |
---|
342 | // Copy constructor |
---|
343 | CbcCompareEstimate::CbcCompareEstimate ( const CbcCompareEstimate & rhs) |
---|
344 | :CbcCompareBase(rhs) |
---|
345 | |
---|
346 | { |
---|
347 | } |
---|
348 | |
---|
349 | // Clone |
---|
350 | CbcCompareBase * |
---|
351 | CbcCompareEstimate::clone() const |
---|
352 | { |
---|
353 | return new CbcCompareEstimate(*this); |
---|
354 | } |
---|
355 | |
---|
356 | // Assignment operator |
---|
357 | CbcCompareEstimate & |
---|
358 | CbcCompareEstimate::operator=( const CbcCompareEstimate& rhs) |
---|
359 | { |
---|
360 | if (this!=&rhs) { |
---|
361 | CbcCompareBase::operator=(rhs); |
---|
362 | } |
---|
363 | return *this; |
---|
364 | } |
---|
365 | |
---|
366 | // Destructor |
---|
367 | CbcCompareEstimate::~CbcCompareEstimate () |
---|
368 | { |
---|
369 | } |
---|
370 | |
---|
371 | // Returns true if y better than x |
---|
372 | bool |
---|
373 | CbcCompareEstimate::test (CbcNode * x, CbcNode * y) |
---|
374 | { |
---|
375 | double testX = x->guessedObjectiveValue(); |
---|
376 | double testY = y->guessedObjectiveValue(); |
---|
377 | if (testX!=testY) |
---|
378 | return testX > testY; |
---|
379 | else |
---|
380 | return equalityTest(x,y); // so ties will be broken in consistent manner |
---|
381 | } |
---|
382 | |
---|
383 | // Create C++ lines to get to current state |
---|
384 | void |
---|
385 | CbcCompareEstimate::generateCpp( FILE * fp) |
---|
386 | { |
---|
387 | fprintf(fp,"0#include \"CbcCompareActual.hpp\"\n"); |
---|
388 | fprintf(fp,"3 CbcCompareEstimate compare;\n"); |
---|
389 | fprintf(fp,"3 cbcModel->setNodeComparison(compare);\n"); |
---|
390 | } |
---|