source: trunk/Samples/CbcCompareUser.cpp @ 86

Last change on this file since 86 was 86, checked in by forrest, 16 years ago

inline with doc

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.4 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 "CbcCompareUser.hpp"
16#include "CoinError.hpp"
17#include "CoinHelperFunctions.hpp"
18
19/** Default Constructor
20
21*/
22CbcCompareUser::CbcCompareUser ()
23  : CbcCompareBase(),
24    weight_(-1.0),
25    saveWeight_(0.0),
26    numberSolutions_(0),
27    treeSize_(0)
28{
29  test_=this;
30}
31
32// Constructor with weight
33CbcCompareUser::CbcCompareUser (double weight) 
34  : CbcCompareBase(),
35    weight_(weight) ,
36    saveWeight_(0.0),
37    numberSolutions_(0),
38    treeSize_(0)
39{
40  test_=this;
41}
42
43
44// Copy constructor
45CbcCompareUser::CbcCompareUser ( const CbcCompareUser & rhs)
46  :CbcCompareBase(rhs)
47
48{
49  weight_=rhs.weight_;
50  saveWeight_ = rhs.saveWeight_;
51  numberSolutions_=rhs.numberSolutions_;
52  treeSize_ = rhs.treeSize_;
53}
54
55// Clone
56CbcCompareBase *
57CbcCompareUser::clone() const
58{
59  return new CbcCompareUser(*this);
60}
61
62// Assignment operator
63CbcCompareUser & 
64CbcCompareUser::operator=( const CbcCompareUser& rhs)
65{
66  if (this!=&rhs) {
67    CbcCompareBase::operator=(rhs);
68    weight_=rhs.weight_;
69    saveWeight_ = rhs.saveWeight_;
70    numberSolutions_=rhs.numberSolutions_;
71    treeSize_ = rhs.treeSize_;
72  }
73  return *this;
74}
75
76// Destructor
77CbcCompareUser::~CbcCompareUser ()
78{
79}
80
81// Returns true if y better than x
82bool 
83CbcCompareUser::test (CbcNode * x, CbcNode * y)
84{
85  if (weight_==-1.0) {
86    // before solution
87    /* printf("x %d %d %g, y %d %d %g\n",
88       x->numberUnsatisfied(),x->depth(),x->objectiveValue(),
89       y->numberUnsatisfied(),y->depth(),y->objectiveValue()); */
90    if (x->numberUnsatisfied() > y->numberUnsatisfied())
91      return true;
92    else if (x->numberUnsatisfied() < y->numberUnsatisfied())
93      return false;
94    else
95      return x->depth() < y->depth();
96  } else {
97    // after solution
98    double weight = CoinMax(weight_,0.0);
99    return x->objectiveValue()+ weight*x->numberUnsatisfied() > 
100      y->objectiveValue() + weight*y->numberUnsatisfied();
101  }
102}
103// This allows method to change behavior as it is called
104// after each solution
105void 
106CbcCompareUser::newSolution(CbcModel * model,
107                               double objectiveAtContinuous,
108                               int numberInfeasibilitiesAtContinuous) 
109{
110  if (model->getSolutionCount()==model->getNumberHeuristicSolutions())
111    return; // solution was got by rounding
112  // set to get close to this solution
113  double costPerInteger = 
114    (model->getObjValue()-objectiveAtContinuous)/
115    ((double) numberInfeasibilitiesAtContinuous);
116  weight_ = 0.98*costPerInteger;
117  saveWeight_=weight_;
118  numberSolutions_++;
119  if (numberSolutions_>5)
120    weight_ =0.0; // this searches on objective
121}
122// This allows method to change behavior
123bool 
124CbcCompareUser::every1000Nodes(CbcModel * model, int numberNodes)
125{
126  if (numberNodes>10000)
127    weight_ =0.0; // this searches on objective
128  else if (numberNodes==1000&&weight_==-2.0)
129    weight_=-1.0; // Go to depth first
130  // get size of tree
131  treeSize_ = model->tree()->size();
132  if (treeSize_>10000) {
133    // set weight to reduce size most of time
134    if (treeSize_>20000)
135      weight_=-1.0;
136    else if ((numberNodes%4000)!=0)
137      weight_=-1.0;
138    else
139      weight_=saveWeight_;
140  }
141  return numberNodes==11000; // resort if first time
142}
Note: See TracBrowser for help on using the repository browser.