source: trunk/Cbc/src/CbcCompareBase.hpp @ 862

Last change on this file since 862 was 838, checked in by forrest, 12 years ago

for deterministic parallel

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1// Copyright (C) 2002, International Business Machines
2// Corporation and others.  All Rights Reserved.
3#ifndef CbcCompareBase_H
4#define CbcCompareBase_H
5
6
7//#############################################################################
8/*  These are alternative strategies for node traversal. 
9    They can take data etc for fine tuning
10
11    At present the node list is stored as a heap and the "test"
12    comparison function returns true if node y is better than node x.
13
14    This is rather inflexible so if the comparison functions wants
15    it can signal to use alternative criterion on a complete pass
16    throgh tree.
17
18*/
19#include "CbcNode.hpp"
20#include "CbcConfig.h"
21
22class CbcModel;
23class CbcTree;
24class CbcCompareBase {
25public:
26  // Default Constructor
27  CbcCompareBase () {test_=NULL;threaded_=false;}
28
29  // This allows any method to change behavior as it is called
30  // after each solution
31  virtual void newSolution(CbcModel * model) {}
32
33  // This Also allows any method to change behavior as it is called
34  // after each solution
35  virtual void newSolution(CbcModel * model,
36                           double objectiveAtContinuous,
37                           int numberInfeasibilitiesAtContinuous) {}
38
39  // This allows any method to change behavior as it is called
40  // after every 1000 nodes.
41  // Return true if want tree re-sorted
42  virtual bool every1000Nodes(CbcModel * model,int numberNodes) {return false;}
43
44  /** Returns true if wants code to do scan with alternate criterion
45      NOTE - this is temporarily disabled
46  */
47  virtual bool fullScan() const { return false;}
48
49  virtual ~CbcCompareBase() {}
50  /// Create C++ lines to get to current state
51  virtual void generateCpp( FILE * fp) {}
52
53  // Copy constructor
54  CbcCompareBase ( const CbcCompareBase & rhs)
55  {test_=rhs.test_;threaded_=rhs.threaded_;}
56   
57  // Assignment operator
58  CbcCompareBase & operator=( const CbcCompareBase& rhs)
59  {  if (this!=&rhs) {test_=rhs.test_;threaded_=rhs.threaded_;}
60  return *this;
61  }
62
63  /// Clone
64  virtual CbcCompareBase * clone() const
65  { abort(); return NULL;}
66
67  /// This is test function
68  virtual bool test (CbcNode * x, CbcNode * y) {return true;}
69
70  /// This is alternate test function
71  virtual bool alternateTest (CbcNode * x, CbcNode * y) {return test(x,y);}
72
73  bool operator() (CbcNode * x, CbcNode * y) {
74    return test(x,y);
75  }
76  /// Further test if everything else equal
77  inline bool equalityTest (CbcNode * x, CbcNode * y) const
78  {
79    assert (x);
80    assert (y);
81    if (!threaded_) {
82      CbcNodeInfo * infoX = x->nodeInfo();
83      assert (infoX);
84      int nodeNumberX = infoX->nodeNumber();
85      CbcNodeInfo * infoY = y->nodeInfo();
86      assert (infoY);
87      int nodeNumberY = infoY->nodeNumber();
88      assert (nodeNumberX!=nodeNumberY);
89      return (nodeNumberX>nodeNumberY);
90    } else {
91      assert (x->nodeNumber()!=y->nodeNumber());
92      return (x->nodeNumber()>y->nodeNumber());
93    }
94  }
95  /// Say threaded
96  inline void sayThreaded()
97  { threaded_ = true;}
98protected:
99  CbcCompareBase * test_;
100  // If not threaded we can use better way to break ties
101  bool threaded_;
102};
103class CbcCompare {
104public:
105  CbcCompareBase * test_;
106  // Default Constructor
107  CbcCompare () {test_=NULL;}
108
109  virtual ~CbcCompare() {}
110
111  bool operator() (CbcNode * x, CbcNode * y) {
112    return test_->test(x,y);
113  }
114  bool compareNodes (CbcNode * x, CbcNode * y) {
115    return test_->test(x,y);
116  }
117  /// This is alternate test function
118  inline bool alternateTest (CbcNode * x, CbcNode * y) {return test_->alternateTest(x,y);}
119
120  /// return comparison object
121  inline CbcCompareBase * comparisonObject() const
122  { return test_;}
123};
124//#############################################################################
125/*  These can be alternative strategies for choosing variables
126    Any descendant can be passed in by setVariableChoice
127*/
128
129class CbcChooseVariable {
130public:
131  // Default Constructor
132  CbcChooseVariable () {}
133
134  virtual ~CbcChooseVariable() {}
135  /** If strong branching, then only those passed in (and movement is that length)
136      .  If not strong
137      branching then all passed in and ignore movement.
138      Returns which one chosen (or -1 if none).  way should be +1
139      if branching up, -1 if down */
140  virtual int chosen (const CbcModel * model,int numberToLookAt,
141                      const int * which, const double * downMovement,
142                      const double * upMovement, const double * solution,
143                      int & way, double & value)=0;
144
145};
146#endif
Note: See TracBrowser for help on using the repository browser.