source: trunk/Samples/CbcCompareUser.cpp @ 180

Last change on this file since 180 was 180, checked in by forrest, 15 years ago

mods

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.2 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    count_(0),
28    treeSize_(0)
29{
30  test_=this;
31}
32
33// Constructor with weight
34CbcCompareUser::CbcCompareUser (double weight) 
35  : CbcCompareBase(),
36    weight_(weight) ,
37    saveWeight_(0.0),
38    numberSolutions_(0),
39    count_(0),
40    treeSize_(0)
41{
42  test_=this;
43}
44
45
46// Copy constructor
47CbcCompareUser::CbcCompareUser ( const CbcCompareUser & rhs)
48  :CbcCompareBase(rhs)
49
50{
51  weight_=rhs.weight_;
52  saveWeight_ = rhs.saveWeight_;
53  numberSolutions_=rhs.numberSolutions_;
54  count_ = rhs.count_;
55  treeSize_ = rhs.treeSize_;
56}
57
58// Clone
59CbcCompareBase *
60CbcCompareUser::clone() const
61{
62  return new CbcCompareUser(*this);
63}
64
65// Assignment operator
66CbcCompareUser & 
67CbcCompareUser::operator=( const CbcCompareUser& rhs)
68{
69  if (this!=&rhs) {
70    CbcCompareBase::operator=(rhs);
71    weight_=rhs.weight_;
72    saveWeight_ = rhs.saveWeight_;
73    numberSolutions_=rhs.numberSolutions_;
74    count_ = rhs.count_;
75    treeSize_ = rhs.treeSize_;
76  }
77  return *this;
78}
79
80// Destructor
81CbcCompareUser::~CbcCompareUser ()
82{
83}
84
85// Returns true if y better than x
86bool 
87CbcCompareUser::test (CbcNode * x, CbcNode * y)
88{
89  if (x) {
90    if (y) {
91      if (weight_==-1.0) {
92        // before solution
93        /* printf("x %d %d %g, y %d %d %g\n",
94           x->numberUnsatisfied(),x->depth(),x->objectiveValue(),
95           y->numberUnsatisfied(),y->depth(),y->objectiveValue()); */
96        if (x->numberUnsatisfied() > y->numberUnsatisfied())
97          return true;
98        else if (x->numberUnsatisfied() < y->numberUnsatisfied())
99          return false;
100        else
101          return x->depth() < y->depth();
102      } else {
103        // after solution
104        double weight = CoinMax(weight_,0.0);
105        return x->objectiveValue()+ weight*x->numberUnsatisfied() > 
106          y->objectiveValue() + weight*y->numberUnsatisfied();
107        //return x->guessedObjectiveValue()>y->guessedObjectiveValue();
108      }
109    } else {
110      return false;
111    }
112  } else {
113    return true;
114  }
115}
116// This allows method to change behavior as it is called
117// after each solution
118void 
119CbcCompareUser::newSolution(CbcModel * model,
120                               double objectiveAtContinuous,
121                               int numberInfeasibilitiesAtContinuous) 
122{
123  // set to get close to this solution
124  double costPerInteger = 
125    (model->getObjValue()-objectiveAtContinuous)/
126    ((double) numberInfeasibilitiesAtContinuous);
127  weight_ = 0.95*costPerInteger;
128  saveWeight_=weight_;
129  if (model->getSolutionCount()==model->getNumberHeuristicSolutions())
130    return; // solution was got by rounding
131  numberSolutions_++;
132  if (numberSolutions_>5)
133    weight_ =0.0; // this searches on objective
134}
135// This allows method to change behavior
136bool 
137CbcCompareUser::every1000Nodes(CbcModel * model, int numberNodes)
138{
139  if (numberNodes>10000)
140    weight_ =0.0; // this searches on objective
141  else if (numberNodes==1000&&weight_==-2.0)
142    weight_=-1.0; // Go to depth first
143  // get size of tree
144  treeSize_ = model->tree()->size();
145  if (treeSize_>10000) {
146    // set weight to reduce size most of time
147    if (treeSize_>20000)
148      weight_=-1.0;
149    else if ((numberNodes%4000)!=0)
150      weight_=-1.0;
151    else
152      weight_=saveWeight_;
153  }
154  return numberNodes==11000; // resort if first time
155}
156// Returns true if wants code to do scan with alternate criterion
157bool 
158CbcCompareUser::fullScan() const
159{
160  count_++;
161  if (weight_)
162    return (count_%10)==0;
163  else
164    return false;
165}
166// This is alternate test function
167bool 
168CbcCompareUser::alternateTest (CbcNode * x, CbcNode * y)
169{
170  if (x) {
171    if (y) {
172      return x->objectiveValue() > 
173        y->objectiveValue() ;
174    } else {
175      return false;
176    }
177  } else {
178    return true;
179  }
180}
Note: See TracBrowser for help on using the repository browser.