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

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

towards common use with other solvers

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.9 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))||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 testX = x->depth();
233      int testY = y->depth();
234      if (testX!=testY)
235        return testX < testY;
236      else
237        return equalityTest(x,y); // so ties will be broken in consistent manner
238    }
239  } else {
240    // after solution
241    double weight = CoinMax(weight_,0.0);
242    double testX =  x->objectiveValue()+ weight*x->numberUnsatisfied();
243    double testY = y->objectiveValue() + weight*y->numberUnsatisfied();
244    if (testX!=testY)
245      return testX > testY;
246    else
247      return equalityTest(x,y); // so ties will be broken in consistent manner
248  }
249#endif
250}
251// This allows method to change behavior as it is called
252// after each solution
253void 
254CbcCompareDefault::newSolution(CbcModel * model,
255                               double objectiveAtContinuous,
256                               int numberInfeasibilitiesAtContinuous) 
257{
258  if (model->getSolutionCount()==model->getNumberHeuristicSolutions()&&
259      model->getSolutionCount()<5&&model->getNodeCount()<500)
260    return; // solution was got by rounding
261  // set to get close to this solution
262  double costPerInteger = 
263    (model->getObjValue()-objectiveAtContinuous)/
264    ((double) numberInfeasibilitiesAtContinuous);
265  weight_ = 0.95*costPerInteger;
266  saveWeight_ = 0.95*weight_;
267  numberSolutions_++;
268  if (numberSolutions_>5)
269    weight_ =0.0; // this searches on objective
270}
271// This allows method to change behavior
272bool 
273CbcCompareDefault::every1000Nodes(CbcModel * model, int numberNodes)
274{
275#if 0
276  // was
277  if (numberNodes>10000)
278    weight_ =0.0; // this searches on objective
279  // get size of tree
280  treeSize_ = model->tree()->size();
281#else
282  double saveWeight=weight_;
283  int numberNodes1000 = numberNodes/1000;
284  if (numberNodes>10000) {
285    weight_ =0.0; // this searches on objective
286    // but try a bit of other stuff
287    if ((numberNodes1000%4)==1)
288      weight_=saveWeight_;
289  } else if (numberNodes==1000&&weight_==-2.0) {
290    weight_=-1.0; // Go to depth first
291  }
292  // get size of tree
293  treeSize_ = model->tree()->size();
294  if (treeSize_>10000) {
295    int n1 = model->solver()->getNumRows()+model->solver()->getNumCols();
296    int n2 = model->numberObjects();
297    double size = n1*0.1 + n2*2.0;
298    // set weight to reduce size most of time
299    if (treeSize_*size>5.0e7)
300      weight_=-1.0;
301    else if ((numberNodes1000%4)==0&&treeSize_*size>1.0e6)
302      weight_=-1.0;
303    else if ((numberNodes1000%4)==1)
304      weight_=0.0;
305    else
306      weight_=saveWeight_;
307  }
308#endif
309  //return numberNodes==11000; // resort if first time
310  return (weight_!=saveWeight);
311}
312
313// Create C++ lines to get to current state
314void
315CbcCompareDefault::generateCpp( FILE * fp) 
316{
317  CbcCompareDefault other;
318  fprintf(fp,"0#include \"CbcCompareActual.hpp\"\n");
319  fprintf(fp,"3  CbcCompareDefault compare;\n");
320  if (weight_!=other.weight_)
321    fprintf(fp,"3  compare.setWeight(%g);\n",weight_);
322  fprintf(fp,"3  cbcModel->setNodeComparison(compare);\n");
323}
324
325/** Default Constructor
326
327*/
328CbcCompareEstimate::CbcCompareEstimate ()
329  : CbcCompareBase()
330{
331  test_=this;
332}
333
334// Copy constructor
335CbcCompareEstimate::CbcCompareEstimate ( const CbcCompareEstimate & rhs)
336  :CbcCompareBase(rhs)
337
338{
339}
340
341// Clone
342CbcCompareBase *
343CbcCompareEstimate::clone() const
344{
345  return new CbcCompareEstimate(*this);
346}
347
348// Assignment operator
349CbcCompareEstimate & 
350CbcCompareEstimate::operator=( const CbcCompareEstimate& rhs)
351{
352  if (this!=&rhs) {
353    CbcCompareBase::operator=(rhs);
354  }
355  return *this;
356}
357
358// Destructor
359CbcCompareEstimate::~CbcCompareEstimate ()
360{
361}
362
363// Returns true if y better than x
364bool 
365CbcCompareEstimate::test (CbcNode * x, CbcNode * y)
366{
367  double testX = x->guessedObjectiveValue();
368  double testY = y->guessedObjectiveValue();
369  if (testX!=testY)
370    return testX > testY;
371  else
372    return equalityTest(x,y); // so ties will be broken in consistent manner
373}
374
375// Create C++ lines to get to current state
376void
377CbcCompareEstimate::generateCpp( FILE * fp) 
378{
379  fprintf(fp,"0#include \"CbcCompareActual.hpp\"\n");
380  fprintf(fp,"3  CbcCompareEstimate compare;\n");
381  fprintf(fp,"3  cbcModel->setNodeComparison(compare);\n");
382}
Note: See TracBrowser for help on using the repository browser.