source: branches/devel/Cbc/src/CbcCompareActual.cpp @ 426

Last change on this file since 426 was 424, checked in by forrest, 13 years ago

many changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
Line 
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*/
22CbcCompareDepth::CbcCompareDepth ()
23  : CbcCompareBase()
24{
25  test_=this;
26}
27
28// Copy constructor
29CbcCompareDepth::CbcCompareDepth ( const CbcCompareDepth & rhs)
30  :CbcCompareBase(rhs)
31
32{
33}
34
35// Clone
36CbcCompareBase *
37CbcCompareDepth::clone() const
38{
39  return new CbcCompareDepth(*this);
40}
41
42// Assignment operator
43CbcCompareDepth & 
44CbcCompareDepth::operator=( const CbcCompareDepth& rhs)
45{
46  if (this!=&rhs) {
47    CbcCompareBase::operator=(rhs);
48  }
49  return *this;
50}
51
52// Destructor
53CbcCompareDepth::~CbcCompareDepth ()
54{
55}
56
57// Returns true if y better than x
58bool 
59CbcCompareDepth::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
69void
70CbcCompareDepth::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*/
80CbcCompareObjective::CbcCompareObjective ()
81  : CbcCompareBase()
82{
83  test_=this;
84}
85
86// Copy constructor
87CbcCompareObjective::CbcCompareObjective ( const CbcCompareObjective & rhs)
88  :CbcCompareBase(rhs)
89
90{
91}
92
93// Clone
94CbcCompareBase *
95CbcCompareObjective::clone() const
96{
97  return new CbcCompareObjective(*this);
98}
99
100// Assignment operator
101CbcCompareObjective & 
102CbcCompareObjective::operator=( const CbcCompareObjective& rhs)
103{
104  if (this!=&rhs) {
105    CbcCompareBase::operator=(rhs);
106  }
107  return *this;
108}
109
110// Destructor
111CbcCompareObjective::~CbcCompareObjective ()
112{
113}
114
115// Returns true if y better than x
116bool 
117CbcCompareObjective::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
127void
128CbcCompareObjective::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*/
139CbcCompareDefault::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
150CbcCompareDefault::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
162CbcCompareDefault::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
173CbcCompareBase *
174CbcCompareDefault::clone() const
175{
176  return new CbcCompareDefault(*this);
177}
178
179// Assignment operator
180CbcCompareDefault & 
181CbcCompareDefault::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
194CbcCompareDefault::~CbcCompareDefault ()
195{
196}
197
198// Returns true if y better than x
199bool 
200CbcCompareDefault::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)) {
222    // before solution
223    /* printf("x %d %d %g, y %d %d %g\n",
224       x->numberUnsatisfied(),x->depth(),x->objectiveValue(),
225       y->numberUnsatisfied(),y->depth(),y->objectiveValue()); */
226    if (x->numberUnsatisfied() > y->numberUnsatisfied()) {
227      return true;
228    } else if (x->numberUnsatisfied() < y->numberUnsatisfied()) {
229      return false;
230    } else {
231      int testX = x->depth();
232      int testY = y->depth();
233      if (testX!=testY)
234        return testX < testY;
235      else
236        return equalityTest(x,y); // so ties will be broken in consistent manner
237    }
238  } else {
239    // after solution
240    double weight = CoinMax(weight_,0.0);
241    double testX =  x->objectiveValue()+ weight*x->numberUnsatisfied();
242    double testY = y->objectiveValue() + weight*y->numberUnsatisfied();
243    if (testX!=testY)
244      return testX > testY;
245    else
246      return equalityTest(x,y); // so ties will be broken in consistent manner
247  }
248#endif
249}
250// This allows method to change behavior as it is called
251// after each solution
252void 
253CbcCompareDefault::newSolution(CbcModel * model,
254                               double objectiveAtContinuous,
255                               int numberInfeasibilitiesAtContinuous) 
256{
257  if (model->getSolutionCount()==model->getNumberHeuristicSolutions()&&
258      model->getSolutionCount()<5&&model->getNodeCount()<500)
259    return; // solution was got by rounding
260  // set to get close to this solution
261  double costPerInteger = 
262    (model->getObjValue()-objectiveAtContinuous)/
263    ((double) numberInfeasibilitiesAtContinuous);
264  weight_ = 0.95*costPerInteger;
265  saveWeight_ = 0.95*weight_;
266  numberSolutions_++;
267  if (numberSolutions_>5)
268    weight_ =0.0; // this searches on objective
269}
270// This allows method to change behavior
271bool 
272CbcCompareDefault::every1000Nodes(CbcModel * model, int numberNodes)
273{
274#if 0
275  // was
276  if (numberNodes>10000)
277    weight_ =0.0; // this searches on objective
278  // get size of tree
279  treeSize_ = model->tree()->size();
280#else
281  double saveWeight=weight_;
282  int numberNodes1000 = numberNodes/1000;
283  if (numberNodes>10000) {
284    weight_ =0.0; // this searches on objective
285    // but try a bit of other stuff
286    if ((numberNodes1000%4)==1)
287      weight_=saveWeight_;
288  } else if (numberNodes==1000&&weight_==-2.0) {
289    weight_=-1.0; // Go to depth first
290  }
291  // get size of tree
292  treeSize_ = model->tree()->size();
293  if (treeSize_>10000) {
294    int n1 = model->solver()->getNumRows()+model->solver()->getNumCols();
295    int n2 = model->numberObjects();
296    double size = n1*0.1 + n2*2.0;
297    // set weight to reduce size most of time
298    if (treeSize_*size>5.0e7)
299      weight_=-1.0;
300    else if ((numberNodes1000%4)==0&&treeSize_*size>1.0e6)
301      weight_=-1.0;
302    else if ((numberNodes1000%4)==1)
303      weight_=0.0;
304    else
305      weight_=saveWeight_;
306  }
307#endif
308  //return numberNodes==11000; // resort if first time
309  return (weight_!=saveWeight);
310}
311
312// Create C++ lines to get to current state
313void
314CbcCompareDefault::generateCpp( FILE * fp) 
315{
316  CbcCompareDefault other;
317  fprintf(fp,"0#include \"CbcCompareActual.hpp\"\n");
318  fprintf(fp,"3  CbcCompareDefault compare;\n");
319  if (weight_!=other.weight_)
320    fprintf(fp,"3  compare.setWeight(%g);\n",weight_);
321  fprintf(fp,"3  cbcModel->setNodeComparison(compare);\n");
322}
323
324/** Default Constructor
325
326*/
327CbcCompareEstimate::CbcCompareEstimate ()
328  : CbcCompareBase()
329{
330  test_=this;
331}
332
333// Copy constructor
334CbcCompareEstimate::CbcCompareEstimate ( const CbcCompareEstimate & rhs)
335  :CbcCompareBase(rhs)
336
337{
338}
339
340// Clone
341CbcCompareBase *
342CbcCompareEstimate::clone() const
343{
344  return new CbcCompareEstimate(*this);
345}
346
347// Assignment operator
348CbcCompareEstimate & 
349CbcCompareEstimate::operator=( const CbcCompareEstimate& rhs)
350{
351  if (this!=&rhs) {
352    CbcCompareBase::operator=(rhs);
353  }
354  return *this;
355}
356
357// Destructor
358CbcCompareEstimate::~CbcCompareEstimate ()
359{
360}
361
362// Returns true if y better than x
363bool 
364CbcCompareEstimate::test (CbcNode * x, CbcNode * y)
365{
366  double testX = x->guessedObjectiveValue();
367  double testY = y->guessedObjectiveValue();
368  if (testX!=testY)
369    return testX > testY;
370  else
371    return equalityTest(x,y); // so ties will be broken in consistent manner
372}
373
374// Create C++ lines to get to current state
375void
376CbcCompareEstimate::generateCpp( FILE * fp) 
377{
378  fprintf(fp,"0#include \"CbcCompareActual.hpp\"\n");
379  fprintf(fp,"3  CbcCompareEstimate compare;\n");
380  fprintf(fp,"3  cbcModel->setNodeComparison(compare);\n");
381}
Note: See TracBrowser for help on using the repository browser.