00001 //---------------------------------------------------------------------------- 00002 /** @file HexUctSearch.hpp 00003 */ 00004 //---------------------------------------------------------------------------- 00005 00006 #ifndef HEXUCTSEARCH_H 00007 #define HEXUCTSEARCH_H 00008 00009 #include "SgBlackWhite.h" 00010 #include "SgPoint.h" 00011 #include "SgNode.h" 00012 #include "SgUctSearch.h" 00013 00014 #include "HexUctState.hpp" 00015 #include "PatternState.hpp" 00016 00017 _BEGIN_BENZENE_NAMESPACE_ 00018 00019 //---------------------------------------------------------------------------- 00020 00021 class HexUctSharedPolicy; 00022 00023 /** Creates threads. */ 00024 class HexThreadStateFactory : public SgUctThreadStateFactory 00025 { 00026 public: 00027 HexThreadStateFactory(HexUctSharedPolicy* shared_policy); 00028 00029 ~HexThreadStateFactory(); 00030 00031 SgUctThreadState* Create(unsigned int threadId, const SgUctSearch& search); 00032 private: 00033 00034 HexUctSharedPolicy* m_shared_policy; 00035 }; 00036 00037 //---------------------------------------------------------------------------- 00038 00039 /** Monte-Carlo search using UCT for Hex. */ 00040 class HexUctSearch : public SgUctSearch 00041 { 00042 public: 00043 /** Constructor. 00044 @param factory Creates HexUctState instances for each thread. 00045 @param maxMoves Maximum move number. 00046 */ 00047 HexUctSearch(SgUctThreadStateFactory* factory, 00048 int maxMoves = 0); 00049 00050 ~HexUctSearch(); 00051 00052 //----------------------------------------------------------------------- 00053 00054 /** @name Pure virtual functions of SgUctSearch */ 00055 // @{ 00056 00057 std::string MoveString(SgMove move) const; 00058 00059 SgUctValue UnknownEval() const; 00060 00061 SgUctValue InverseEval(SgUctValue eval) const; 00062 00063 // @} 00064 00065 //----------------------------------------------------------------------- 00066 00067 /** @name Virtual functions of SgUctSearch */ 00068 // @{ 00069 00070 void OnSearchIteration(SgUctValue gameNumber, const unsigned int threadId, 00071 const SgUctGameInfo& info); 00072 00073 void OnStartSearch(); 00074 00075 // @} 00076 00077 //----------------------------------------------------------------------- 00078 00079 /** @name Hex-specific functions */ 00080 // @{ 00081 00082 void SetBoard(HexBoard& board); 00083 00084 HexBoard& Board(); 00085 00086 const HexBoard& Board() const; 00087 00088 void SetSharedData(HexUctSharedData& data); 00089 00090 HexUctSharedData& SharedData(); 00091 00092 const HexUctSharedData& SharedData() const; 00093 00094 /** @see SetKeepGames() 00095 @throws SgException if KeepGames() was false at last invocation of 00096 StartSearch() 00097 */ 00098 void SaveGames(const std::string& fileName) const; 00099 00100 void AppendGame(const std::vector<SgMove>& sequence); 00101 00102 /** @see HexUctUtil::SaveTree() */ 00103 void SaveTree(std::ostream& out, int maxDepth) const; 00104 00105 /** Returns the position the previous search was run on. */ 00106 const StoneBoard& LastPositionSearched() const; 00107 00108 // @} 00109 00110 //----------------------------------------------------------------------- 00111 00112 /** @name Hex-specific parameters */ 00113 // @{ 00114 00115 /** Keep a SGF tree of all games. 00116 Games are cleared in each OnStartSearch(). Games can be saved 00117 with SaveGames(). 00118 */ 00119 void SetKeepGames(bool enable); 00120 00121 /** @see SetKeepGames() */ 00122 bool KeepGames() const; 00123 00124 /** Enable output of live graphics commands for HexGui. 00125 @see GoGuiGfx(), SetLiveGfxInterval() 00126 */ 00127 void SetLiveGfx(bool enable); 00128 00129 /** @see SetLiveGfx() */ 00130 bool LiveGfx() const; 00131 00132 /** Set interval for outputting of live graphics commands for HexGui. 00133 @see SetLiveGfx() 00134 */ 00135 void SetLiveGfxInterval(int interval); 00136 00137 /** @see SetLiveGfxInterval() */ 00138 int LiveGfxInterval() const; 00139 00140 /** Pattern-check radius to use during in-tree phase. */ 00141 int TreeUpdateRadius() const; 00142 00143 /** See TreeUpdateRadius() */ 00144 void SetTreeUpdateRadius(int radius); 00145 00146 /** Pattern-check radius to use during playout phase. */ 00147 int PlayoutUpdateRadius() const; 00148 00149 /** See PlayoutUpdateRadius() */ 00150 void SetPlayoutUpdateRadius(int radius); 00151 00152 // @} 00153 00154 protected: 00155 00156 /** @see SetKeepGames() */ 00157 bool m_keepGames; 00158 00159 /** @see SetLiveGfx() */ 00160 bool m_liveGfx; 00161 00162 /** @see SetLiveGfxInterval() */ 00163 int m_liveGfxInterval; 00164 00165 /** @see TreeUpdateRadius() */ 00166 int m_treeUpdateRadius; 00167 00168 /** @see PlayoutUpdateRadius() */ 00169 int m_playoutUpdateRadius; 00170 00171 //---------------------------------------------------------------------- 00172 00173 /** Nothing is done to this board. We do not own this pointer. 00174 Threads will synchronise with this board at the start of the 00175 search. */ 00176 HexBoard* m_brd; 00177 00178 /** Data among threads. */ 00179 HexUctSharedData m_shared_data; 00180 00181 StoneBoard m_lastPositionSearched; 00182 00183 //---------------------------------------------------------------------- 00184 00185 /** @see SetKeepGames(). 00186 Should be non-null only if KeepGames() is true. 00187 */ 00188 SgNode* m_root; 00189 00190 SgUctValue m_nextLiveGfx; 00191 00192 private: 00193 00194 /** Not implemented */ 00195 HexUctSearch(const HexUctSearch& search); 00196 00197 /** Not implemented */ 00198 HexUctSearch& operator=(const HexUctSearch& search); 00199 }; 00200 00201 inline void HexUctSearch::SetBoard(HexBoard& board) 00202 { 00203 m_brd = &board; 00204 } 00205 00206 inline HexBoard& HexUctSearch::Board() 00207 { 00208 return *m_brd; 00209 } 00210 00211 inline const HexBoard& HexUctSearch::Board() const 00212 { 00213 return *m_brd; 00214 } 00215 00216 inline bool HexUctSearch::KeepGames() const 00217 { 00218 return m_keepGames; 00219 } 00220 00221 inline bool HexUctSearch::LiveGfx() const 00222 { 00223 return m_liveGfx; 00224 } 00225 00226 inline int HexUctSearch::LiveGfxInterval() const 00227 { 00228 return m_liveGfxInterval; 00229 } 00230 00231 inline void HexUctSearch::SetKeepGames(bool enable) 00232 { 00233 m_keepGames = enable; 00234 } 00235 00236 inline void HexUctSearch::SetLiveGfx(bool enable) 00237 { 00238 m_liveGfx = enable; 00239 } 00240 00241 inline void HexUctSearch::SetLiveGfxInterval(int interval) 00242 { 00243 SG_ASSERT(interval > 0); 00244 m_liveGfxInterval = interval; 00245 } 00246 00247 inline int HexUctSearch::TreeUpdateRadius() const 00248 { 00249 return m_treeUpdateRadius; 00250 } 00251 00252 inline void HexUctSearch::SetTreeUpdateRadius(int radius) 00253 { 00254 m_treeUpdateRadius = radius; 00255 } 00256 00257 inline int HexUctSearch::PlayoutUpdateRadius() const 00258 { 00259 return m_playoutUpdateRadius; 00260 } 00261 00262 inline void HexUctSearch::SetPlayoutUpdateRadius(int radius) 00263 { 00264 m_playoutUpdateRadius = radius; 00265 } 00266 00267 inline void HexUctSearch::SetSharedData(HexUctSharedData& data) 00268 { 00269 m_shared_data = data; 00270 } 00271 00272 inline HexUctSharedData& HexUctSearch::SharedData() 00273 { 00274 return m_shared_data; 00275 } 00276 00277 inline const HexUctSharedData& HexUctSearch::SharedData() const 00278 { 00279 return m_shared_data; 00280 } 00281 00282 inline const StoneBoard& HexUctSearch::LastPositionSearched() const 00283 { 00284 return m_lastPositionSearched; 00285 } 00286 00287 //---------------------------------------------------------------------------- 00288 00289 _END_BENZENE_NAMESPACE_ 00290 00291 #endif // HEXUCTSEARCH_H