00001 //---------------------------------------------------------------------------- 00002 /** @file SgSearchControl.h 00003 Search control for searchengine. 00004 00005 Provides base class SgSearchControl and several 00006 basic serch control strategies. 00007 An SgSearchControl object is installed into SgSearch or derived engine. 00008 */ 00009 //---------------------------------------------------------------------------- 00010 00011 #ifndef SG_SEARCHCONTROL_H 00012 #define SG_SEARCHCONTROL_H 00013 00014 //---------------------------------------------------------------------------- 00015 00016 /** Resource control used in class SgSearch. */ 00017 class SgSearchControl 00018 { 00019 public: 00020 SgSearchControl(); 00021 00022 virtual ~SgSearchControl(); 00023 00024 /** Check if search should be aborted. 00025 Called at each node. 00026 */ 00027 virtual bool Abort(double elapsedTime, int numNodes) = 0; 00028 00029 /** Check if next iteration should be started. 00030 Called before each iteration. 00031 The default implementation always returns true. 00032 @param depth The depth of the next iteration. 00033 @param elapsedTime The elapsed time in seconds. 00034 @param numNodes The number of nodes visited. 00035 */ 00036 virtual bool StartNextIteration(int depth, double elapsedTime, 00037 int numNodes); 00038 00039 private: 00040 /** Not implemented */ 00041 SgSearchControl(const SgSearchControl&); 00042 00043 /** Not implemented */ 00044 SgSearchControl& operator=(const SgSearchControl&); 00045 }; 00046 00047 inline SgSearchControl::SgSearchControl() 00048 { 00049 } 00050 00051 //---------------------------------------------------------------------------- 00052 00053 /** Example of a simple search abort class: abort when time has expired. */ 00054 class SgTimeSearchControl 00055 : public SgSearchControl 00056 { 00057 public: 00058 SgTimeSearchControl(double maxTime); 00059 00060 virtual ~SgTimeSearchControl(); 00061 00062 virtual bool Abort(double elapsedTime, int ignoreNumNodes); 00063 00064 double GetMaxTime() const; 00065 00066 void SetMaxTime(double maxTime); 00067 00068 private: 00069 double m_maxTime; 00070 00071 /** Not implemented */ 00072 SgTimeSearchControl(const SgTimeSearchControl&); 00073 00074 /** Not implemented */ 00075 SgTimeSearchControl& operator=(const SgTimeSearchControl&); 00076 }; 00077 00078 inline double SgTimeSearchControl::GetMaxTime() const 00079 { 00080 return m_maxTime; 00081 } 00082 00083 inline void SgTimeSearchControl::SetMaxTime(double maxTime) 00084 { 00085 m_maxTime = maxTime; 00086 } 00087 00088 //---------------------------------------------------------------------------- 00089 00090 /** Example of a simple search abort class: abort when node limit 00091 is reached. 00092 */ 00093 class SgNodeSearchControl 00094 : public SgSearchControl 00095 { 00096 public: 00097 SgNodeSearchControl(int maxNumNodes); 00098 00099 virtual ~SgNodeSearchControl(); 00100 00101 virtual bool Abort(double ignoreElapsedTime, int numNodes); 00102 00103 void SetMaxNumNodes(int maxNumNodes); 00104 00105 private: 00106 int m_maxNumNodes; 00107 00108 /** Not implemented */ 00109 SgNodeSearchControl(const SgNodeSearchControl&); 00110 00111 /** Not implemented */ 00112 SgNodeSearchControl& operator=(const SgNodeSearchControl&); 00113 }; 00114 00115 inline void SgNodeSearchControl::SetMaxNumNodes(int maxNumNodes) 00116 { 00117 m_maxNumNodes = maxNumNodes; 00118 } 00119 00120 //---------------------------------------------------------------------------- 00121 00122 /** Abort when either time or node limit is reached. */ 00123 class SgCombinedSearchControl 00124 : public SgSearchControl 00125 { 00126 public: 00127 SgCombinedSearchControl(double maxTime, int maxNumNodes); 00128 00129 virtual ~SgCombinedSearchControl(); 00130 00131 virtual bool Abort(double elapsedTime, int numNodes); 00132 00133 private: 00134 double m_maxTime; 00135 00136 int m_maxNumNodes; 00137 00138 /** Not implemented */ 00139 SgCombinedSearchControl(const SgCombinedSearchControl&); 00140 00141 /** Not implemented */ 00142 SgCombinedSearchControl& operator=(const SgCombinedSearchControl&); 00143 }; 00144 00145 inline SgCombinedSearchControl::SgCombinedSearchControl(double maxTime, 00146 int maxNumNodes) 00147 : m_maxTime(maxTime), 00148 m_maxNumNodes(maxNumNodes) 00149 { 00150 } 00151 00152 //---------------------------------------------------------------------------- 00153 00154 /** Abort when time limit is reached AND a number of nodes were searched. */ 00155 class SgRelaxedSearchControl 00156 : public SgSearchControl 00157 { 00158 public: 00159 static const int MIN_NODES_PER_SECOND = 1000; 00160 00161 SgRelaxedSearchControl(double maxTime); 00162 00163 virtual ~SgRelaxedSearchControl(); 00164 00165 virtual bool Abort(double elapsedTime, int numNodes); 00166 00167 private: 00168 double m_maxTime; 00169 00170 /** Not implemented */ 00171 SgRelaxedSearchControl(const SgRelaxedSearchControl&); 00172 00173 /** Not implemented */ 00174 SgRelaxedSearchControl& operator=(const SgRelaxedSearchControl&); 00175 }; 00176 00177 inline SgRelaxedSearchControl::SgRelaxedSearchControl(double maxTime) 00178 : m_maxTime(maxTime) 00179 { 00180 } 00181 00182 //---------------------------------------------------------------------------- 00183 00184 #endif // SG_SEARCHCONTROL_H