00001 /* $Id: CbcCompareBase.hpp 1506 2010-09-27 16:55:11Z lou $ */ 00002 // Copyright (C) 2002, International Business Machines 00003 // Corporation and others. All Rights Reserved. 00004 #ifndef CbcCompareBase_H 00005 #define CbcCompareBase_H 00006 00007 00008 //############################################################################# 00009 /* These are alternative strategies for node traversal. 00010 They can take data etc for fine tuning 00011 00012 At present the node list is stored as a heap and the "test" 00013 comparison function returns true if node y is better than node x. 00014 00015 This is rather inflexible so if the comparison functions wants 00016 it can signal to use alternative criterion on a complete pass 00017 throgh tree. 00018 00019 */ 00020 #include "CbcNode.hpp" 00021 #include "CbcConfig.h" 00022 00023 class CbcModel; 00024 class CbcTree; 00025 class CbcCompareBase { 00026 public: 00027 // Default Constructor 00028 CbcCompareBase () { 00029 test_ = NULL; 00030 threaded_ = false; 00031 } 00032 00043 virtual bool newSolution(CbcModel * ) { return (false) ; } 00044 00055 virtual bool newSolution(CbcModel * , 00056 double , 00057 int ) { return (false) ; } 00058 00059 // This allows any method to change behavior as it is called 00060 // after every 1000 nodes. 00061 // Return true if want tree re-sorted 00062 virtual bool every1000Nodes(CbcModel * , int ) { 00063 return false; 00064 } 00065 00069 virtual bool fullScan() const { 00070 return false; 00071 } 00072 00073 virtual ~CbcCompareBase() {} 00075 virtual void generateCpp( FILE * ) {} 00076 00077 // Copy constructor 00078 CbcCompareBase ( const CbcCompareBase & rhs) { 00079 test_ = rhs.test_; 00080 threaded_ = rhs.threaded_; 00081 } 00082 00083 // Assignment operator 00084 CbcCompareBase & operator=( const CbcCompareBase& rhs) { 00085 if (this != &rhs) { 00086 test_ = rhs.test_; 00087 threaded_ = rhs.threaded_; 00088 } 00089 return *this; 00090 } 00091 00093 virtual CbcCompareBase * clone() const { 00094 abort(); 00095 return NULL; 00096 } 00097 00099 virtual bool test (CbcNode * , CbcNode * ) { 00100 return true; 00101 } 00102 00104 virtual bool alternateTest (CbcNode * x, CbcNode * y) { 00105 return test(x, y); 00106 } 00107 00108 bool operator() (CbcNode * x, CbcNode * y) { 00109 return test(x, y); 00110 } 00112 inline bool equalityTest (CbcNode * x, CbcNode * y) const { 00113 assert (x); 00114 assert (y); 00115 if (!threaded_) { 00116 CbcNodeInfo * infoX = x->nodeInfo(); 00117 assert (infoX); 00118 int nodeNumberX = infoX->nodeNumber(); 00119 CbcNodeInfo * infoY = y->nodeInfo(); 00120 assert (infoY); 00121 int nodeNumberY = infoY->nodeNumber(); 00122 assert (nodeNumberX != nodeNumberY); 00123 return (nodeNumberX > nodeNumberY); 00124 } else { 00125 assert (x->nodeNumber() != y->nodeNumber()); 00126 return (x->nodeNumber() > y->nodeNumber()); 00127 } 00128 } 00130 inline void sayThreaded() { 00131 threaded_ = true; 00132 } 00133 protected: 00134 CbcCompareBase * test_; 00135 // If not threaded we can use better way to break ties 00136 bool threaded_; 00137 }; 00138 00139 #endif 00140