source: trunk/Cbc/src/CbcCompareActual.cpp @ 310

Last change on this file since 310 was 310, checked in by andreasw, 13 years ago

first commit for autotools conversion to be able to move more files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.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  return x->depth() < y->depth();
62}
63
64/** Default Constructor
65
66*/
67CbcCompareObjective::CbcCompareObjective ()
68  : CbcCompareBase()
69{
70  test_=this;
71}
72
73// Copy constructor
74CbcCompareObjective::CbcCompareObjective ( const CbcCompareObjective & rhs)
75  :CbcCompareBase(rhs)
76
77{
78}
79
80// Clone
81CbcCompareBase *
82CbcCompareObjective::clone() const
83{
84  return new CbcCompareObjective(*this);
85}
86
87// Assignment operator
88CbcCompareObjective & 
89CbcCompareObjective::operator=( const CbcCompareObjective& rhs)
90{
91  if (this!=&rhs) {
92    CbcCompareBase::operator=(rhs);
93  }
94  return *this;
95}
96
97// Destructor
98CbcCompareObjective::~CbcCompareObjective ()
99{
100}
101
102// Returns true if y better than x
103bool 
104CbcCompareObjective::test (CbcNode * x, CbcNode * y)
105{
106  return x->objectiveValue() > y->objectiveValue();
107}
108
109/** Default Constructor
110
111*/
112CbcCompareDefault::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
123CbcCompareDefault::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
135CbcCompareDefault::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
146CbcCompareBase *
147CbcCompareDefault::clone() const
148{
149  return new CbcCompareDefault(*this);
150}
151
152// Assignment operator
153CbcCompareDefault & 
154CbcCompareDefault::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
167CbcCompareDefault::~CbcCompareDefault ()
168{
169}
170
171// Returns true if y better than x
172bool 
173CbcCompareDefault::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&&(y->depth()>7||x->depth()>7)) {
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
215void 
216CbcCompareDefault::newSolution(CbcModel * model,
217                               double objectiveAtContinuous,
218                               int numberInfeasibilitiesAtContinuous) 
219{
220  if (model->getSolutionCount()==model->getNumberHeuristicSolutions()&&
221      model->getSolutionCount()<5&&model->getNodeCount()<500)
222    return; // solution was got by rounding
223  // set to get close to this solution
224  double costPerInteger = 
225    (model->getObjValue()-objectiveAtContinuous)/
226    ((double) numberInfeasibilitiesAtContinuous);
227  weight_ = 0.95*costPerInteger;
228  saveWeight_ = 0.95*weight_;
229  numberSolutions_++;
230  if (numberSolutions_>5)
231    weight_ =0.0; // this searches on objective
232}
233// This allows method to change behavior
234bool 
235CbcCompareDefault::every1000Nodes(CbcModel * model, int numberNodes)
236{
237#if 0
238  // was
239  if (numberNodes>10000)
240    weight_ =0.0; // this searches on objective
241  // get size of tree
242  treeSize_ = model->tree()->size();
243#else
244  double saveWeight=weight_;
245  int numberNodes1000 = numberNodes/1000;
246  if (numberNodes>10000) {
247    weight_ =0.0; // this searches on objective
248    // but try a bit of other stuff
249    if ((numberNodes1000%4)==1)
250      weight_=saveWeight_;
251  } else if (numberNodes==1000&&weight_==-2.0) {
252    weight_=-1.0; // Go to depth first
253  }
254  // get size of tree
255  treeSize_ = model->tree()->size();
256  if (treeSize_>10000) {
257    int n1 = model->solver()->getNumRows()+model->solver()->getNumCols();
258    int n2 = model->numberObjects();
259    double size = n1*0.1 + n2*2.0;
260    // set weight to reduce size most of time
261    if (treeSize_*size>5.0e7)
262      weight_=-1.0;
263    else if ((numberNodes1000%4)==0&&treeSize_*size>1.0e6)
264      weight_=-1.0;
265    else if ((numberNodes1000%4)==1)
266      weight_=0.0;
267    else
268      weight_=saveWeight_;
269  }
270#endif
271  //return numberNodes==11000; // resort if first time
272  return (weight_!=saveWeight);
273}
274
275/** Default Constructor
276
277*/
278CbcCompareEstimate::CbcCompareEstimate ()
279  : CbcCompareBase()
280{
281  test_=this;
282}
283
284// Copy constructor
285CbcCompareEstimate::CbcCompareEstimate ( const CbcCompareEstimate & rhs)
286  :CbcCompareBase(rhs)
287
288{
289}
290
291// Clone
292CbcCompareBase *
293CbcCompareEstimate::clone() const
294{
295  return new CbcCompareEstimate(*this);
296}
297
298// Assignment operator
299CbcCompareEstimate & 
300CbcCompareEstimate::operator=( const CbcCompareEstimate& rhs)
301{
302  if (this!=&rhs) {
303    CbcCompareBase::operator=(rhs);
304  }
305  return *this;
306}
307
308// Destructor
309CbcCompareEstimate::~CbcCompareEstimate ()
310{
311}
312
313// Returns true if y better than x
314bool 
315CbcCompareEstimate::test (CbcNode * x, CbcNode * y)
316{
317  return x->guessedObjectiveValue() >  y->guessedObjectiveValue() ;
318}
319
Note: See TracBrowser for help on using the repository browser.