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 | return x->depth() < y->depth(); |
---|
62 | } |
---|
63 | |
---|
64 | /** Default Constructor |
---|
65 | |
---|
66 | */ |
---|
67 | CbcCompareObjective::CbcCompareObjective () |
---|
68 | : CbcCompareBase() |
---|
69 | { |
---|
70 | test_=this; |
---|
71 | } |
---|
72 | |
---|
73 | // Copy constructor |
---|
74 | CbcCompareObjective::CbcCompareObjective ( const CbcCompareObjective & rhs) |
---|
75 | :CbcCompareBase(rhs) |
---|
76 | |
---|
77 | { |
---|
78 | } |
---|
79 | |
---|
80 | // Clone |
---|
81 | CbcCompareBase * |
---|
82 | CbcCompareObjective::clone() const |
---|
83 | { |
---|
84 | return new CbcCompareObjective(*this); |
---|
85 | } |
---|
86 | |
---|
87 | // Assignment operator |
---|
88 | CbcCompareObjective & |
---|
89 | CbcCompareObjective::operator=( const CbcCompareObjective& rhs) |
---|
90 | { |
---|
91 | if (this!=&rhs) { |
---|
92 | CbcCompareBase::operator=(rhs); |
---|
93 | } |
---|
94 | return *this; |
---|
95 | } |
---|
96 | |
---|
97 | // Destructor |
---|
98 | CbcCompareObjective::~CbcCompareObjective () |
---|
99 | { |
---|
100 | } |
---|
101 | |
---|
102 | // Returns true if y better than x |
---|
103 | bool |
---|
104 | CbcCompareObjective::test (CbcNode * x, CbcNode * y) |
---|
105 | { |
---|
106 | return x->objectiveValue() > y->objectiveValue(); |
---|
107 | } |
---|
108 | |
---|
109 | /** Default Constructor |
---|
110 | |
---|
111 | */ |
---|
112 | CbcCompareDefault::CbcCompareDefault () |
---|
113 | : CbcCompareBase(), |
---|
114 | weight_(-1.0), |
---|
115 | saveWeight_(0.0), |
---|
116 | numberSolutions_(0), |
---|
117 | treeSize_(0) |
---|
118 | { |
---|
119 | test_=this; |
---|
120 | } |
---|
121 | |
---|
122 | // Constructor with weight |
---|
123 | CbcCompareDefault::CbcCompareDefault (double weight) |
---|
124 | : CbcCompareBase(), |
---|
125 | weight_(weight) , |
---|
126 | saveWeight_(0.0), |
---|
127 | numberSolutions_(0), |
---|
128 | treeSize_(0) |
---|
129 | { |
---|
130 | test_=this; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | // Copy constructor |
---|
135 | CbcCompareDefault::CbcCompareDefault ( const CbcCompareDefault & rhs) |
---|
136 | :CbcCompareBase(rhs) |
---|
137 | |
---|
138 | { |
---|
139 | weight_=rhs.weight_; |
---|
140 | saveWeight_ = rhs.saveWeight_; |
---|
141 | numberSolutions_=rhs.numberSolutions_; |
---|
142 | treeSize_ = rhs.treeSize_; |
---|
143 | } |
---|
144 | |
---|
145 | // Clone |
---|
146 | CbcCompareBase * |
---|
147 | CbcCompareDefault::clone() const |
---|
148 | { |
---|
149 | return new CbcCompareDefault(*this); |
---|
150 | } |
---|
151 | |
---|
152 | // Assignment operator |
---|
153 | CbcCompareDefault & |
---|
154 | CbcCompareDefault::operator=( const CbcCompareDefault& rhs) |
---|
155 | { |
---|
156 | if (this!=&rhs) { |
---|
157 | CbcCompareBase::operator=(rhs); |
---|
158 | weight_=rhs.weight_; |
---|
159 | saveWeight_ = rhs.saveWeight_; |
---|
160 | numberSolutions_=rhs.numberSolutions_; |
---|
161 | treeSize_ = rhs.treeSize_; |
---|
162 | } |
---|
163 | return *this; |
---|
164 | } |
---|
165 | |
---|
166 | // Destructor |
---|
167 | CbcCompareDefault::~CbcCompareDefault () |
---|
168 | { |
---|
169 | } |
---|
170 | |
---|
171 | // Returns true if y better than x |
---|
172 | bool |
---|
173 | CbcCompareDefault::test (CbcNode * x, CbcNode * y) |
---|
174 | { |
---|
175 | #if 0 |
---|
176 | // was |
---|
177 | if (weight_<0.0||treeSize_>100000) { |
---|
178 | // before solution |
---|
179 | /* printf("x %d %d %g, y %d %d %g\n", |
---|
180 | x->numberUnsatisfied(),x->depth(),x->objectiveValue(), |
---|
181 | y->numberUnsatisfied(),y->depth(),y->objectiveValue()); */ |
---|
182 | if (x->numberUnsatisfied() > y->numberUnsatisfied()) |
---|
183 | return true; |
---|
184 | else if (x->numberUnsatisfied() < y->numberUnsatisfied()) |
---|
185 | return false; |
---|
186 | else |
---|
187 | return x->depth() < y->depth(); |
---|
188 | } else { |
---|
189 | // after solution |
---|
190 | return x->objectiveValue()+ weight_*x->numberUnsatisfied() > |
---|
191 | y->objectiveValue() + weight_*y->numberUnsatisfied(); |
---|
192 | } |
---|
193 | #else |
---|
194 | if (weight_==-1.0) { |
---|
195 | // before solution |
---|
196 | /* printf("x %d %d %g, y %d %d %g\n", |
---|
197 | x->numberUnsatisfied(),x->depth(),x->objectiveValue(), |
---|
198 | y->numberUnsatisfied(),y->depth(),y->objectiveValue()); */ |
---|
199 | if (x->numberUnsatisfied() > y->numberUnsatisfied()) |
---|
200 | return true; |
---|
201 | else if (x->numberUnsatisfied() < y->numberUnsatisfied()) |
---|
202 | return false; |
---|
203 | else |
---|
204 | return x->depth() < y->depth(); |
---|
205 | } else { |
---|
206 | // after solution |
---|
207 | double weight = CoinMax(weight_,0.0); |
---|
208 | return x->objectiveValue()+ weight*x->numberUnsatisfied() > |
---|
209 | y->objectiveValue() + weight*y->numberUnsatisfied(); |
---|
210 | } |
---|
211 | #endif |
---|
212 | } |
---|
213 | // This allows method to change behavior as it is called |
---|
214 | // after each solution |
---|
215 | void |
---|
216 | CbcCompareDefault::newSolution(CbcModel * model, |
---|
217 | double objectiveAtContinuous, |
---|
218 | int numberInfeasibilitiesAtContinuous) |
---|
219 | { |
---|
220 | if (model->getSolutionCount()==model->getNumberHeuristicSolutions()) |
---|
221 | return; // solution was got by rounding |
---|
222 | // set to get close to this solution |
---|
223 | double costPerInteger = |
---|
224 | (model->getObjValue()-objectiveAtContinuous)/ |
---|
225 | ((double) numberInfeasibilitiesAtContinuous); |
---|
226 | weight_ = 0.98*costPerInteger; |
---|
227 | numberSolutions_++; |
---|
228 | if (numberSolutions_>5) |
---|
229 | weight_ =0.0; // this searches on objective |
---|
230 | } |
---|
231 | // This allows method to change behavior |
---|
232 | bool |
---|
233 | CbcCompareDefault::every1000Nodes(CbcModel * model, int numberNodes) |
---|
234 | { |
---|
235 | #if 0 |
---|
236 | // was |
---|
237 | if (numberNodes>10000) |
---|
238 | weight_ =0.0; // this searches on objective |
---|
239 | // get size of tree |
---|
240 | treeSize_ = model->tree()->size(); |
---|
241 | #else |
---|
242 | if (numberNodes>10000) |
---|
243 | weight_ =0.0; // this searches on objective |
---|
244 | else if (numberNodes==1000&&weight_==-2.0) |
---|
245 | weight_=-1.0; // Go to depth first |
---|
246 | // get size of tree |
---|
247 | treeSize_ = model->tree()->size(); |
---|
248 | if (treeSize_>10000) { |
---|
249 | // set weight to reduce size most of time |
---|
250 | if (treeSize_>20000) |
---|
251 | weight_=-1.0; |
---|
252 | else if ((numberNodes%4000)!=0) |
---|
253 | weight_=-1.0; |
---|
254 | else |
---|
255 | weight_=saveWeight_; |
---|
256 | } |
---|
257 | #endif |
---|
258 | return numberNodes==11000; // resort if first time |
---|
259 | } |
---|
260 | |
---|
261 | /** Default Constructor |
---|
262 | |
---|
263 | */ |
---|
264 | CbcCompareEstimate::CbcCompareEstimate () |
---|
265 | : CbcCompareBase() |
---|
266 | { |
---|
267 | test_=this; |
---|
268 | } |
---|
269 | |
---|
270 | // Copy constructor |
---|
271 | CbcCompareEstimate::CbcCompareEstimate ( const CbcCompareEstimate & rhs) |
---|
272 | :CbcCompareBase(rhs) |
---|
273 | |
---|
274 | { |
---|
275 | } |
---|
276 | |
---|
277 | // Clone |
---|
278 | CbcCompareBase * |
---|
279 | CbcCompareEstimate::clone() const |
---|
280 | { |
---|
281 | return new CbcCompareEstimate(*this); |
---|
282 | } |
---|
283 | |
---|
284 | // Assignment operator |
---|
285 | CbcCompareEstimate & |
---|
286 | CbcCompareEstimate::operator=( const CbcCompareEstimate& rhs) |
---|
287 | { |
---|
288 | if (this!=&rhs) { |
---|
289 | CbcCompareBase::operator=(rhs); |
---|
290 | } |
---|
291 | return *this; |
---|
292 | } |
---|
293 | |
---|
294 | // Destructor |
---|
295 | CbcCompareEstimate::~CbcCompareEstimate () |
---|
296 | { |
---|
297 | } |
---|
298 | |
---|
299 | // Returns true if y better than x |
---|
300 | bool |
---|
301 | CbcCompareEstimate::test (CbcNode * x, CbcNode * y) |
---|
302 | { |
---|
303 | return x->guessedObjectiveValue() > y->guessedObjectiveValue() ; |
---|
304 | } |
---|
305 | |
---|