Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

GoUctSearch.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file GoUctSearch.h
00003 */
00004 //----------------------------------------------------------------------------
00005 
00006 #ifndef GOUCT_SEARCH_H
00007 #define GOUCT_SEARCH_H
00008 
00009 #include <iosfwd>
00010 #include "GoBoard.h"
00011 #include "GoBoardHistory.h"
00012 #include "GoBoardSynchronizer.h"
00013 #include "GoUctBoard.h"
00014 #include "SgUctSearch.h"
00015 #include "SgBlackWhite.h"
00016 #include "SgStatistics.h"
00017 
00018 class SgNode;
00019 
00020 //----------------------------------------------------------------------------
00021 
00022 /** Thread state for GoUctSearch.
00023 */
00024 class GoUctState
00025     : public SgUctThreadState
00026 {
00027 public:
00028     /** Constructor.
00029         @param threadId The number of the thread. Needed for passing to
00030         constructor of SgUctThreadState.
00031         @param bd The board with the current position. The state has is own
00032         board that will be synchronized with the currently searched position
00033         in StartSearch()
00034     */
00035     GoUctState(std::size_t threadId, const GoBoard& bd);
00036 
00037     /** @name Pure virtual functions of SgUctThreadState */
00038     // @{
00039 
00040     void StartSearch();
00041 
00042     /** Implementation of SgUctSearch::Execute */
00043     void Execute(SgMove move);
00044 
00045     /** Implementation of SgUctSearch::ExecutePlayout */
00046     void ExecutePlayout(SgMove move);
00047 
00048     void TakeBackInTree(std::size_t nuMoves);
00049 
00050     void TakeBackPlayout(std::size_t nuMoves);
00051 
00052     // @} // @name
00053 
00054 
00055     /** @name Virtual functions of SgUctThreadState */
00056     // @{
00057 
00058     void GameStart();
00059 
00060     void StartPlayout();
00061 
00062     void StartPlayouts();
00063 
00064     // @} // @name
00065 
00066     /** Board used during in-tree phase. */
00067     const GoBoard& Board() const;
00068 
00069     /** Board used during playout phase. */
00070     const GoUctBoard& UctBoard() const;
00071 
00072     bool IsInPlayout() const;
00073 
00074     /** Length of the current game from the root position of the search. */
00075     int GameLength() const;
00076 
00077     void Dump(std::ostream& out) const;
00078 
00079 private:
00080     /** Assertion handler to dump the state of a GoUctState. */
00081     class AssertionHandler
00082         : public SgAssertionHandler
00083     {
00084     public:
00085         AssertionHandler(const GoUctState& state);
00086 
00087         void Run();
00088 
00089     private:
00090         const GoUctState& m_state;
00091     };
00092 
00093     AssertionHandler m_assertionHandler;
00094 
00095     /** Board used for in-tree phase. */
00096     GoBoard m_bd;
00097 
00098     /** Board used for playout phase. */
00099     GoUctBoard m_uctBd;
00100 
00101     GoBoardSynchronizer m_synchronizer;
00102 
00103     bool m_isInPlayout;
00104 
00105     /** See GameLength() */
00106     int m_gameLength;
00107 };
00108 
00109 inline const GoBoard& GoUctState::Board() const
00110 {
00111     return m_bd;
00112 }
00113 
00114 inline int GoUctState::GameLength() const
00115 {
00116     return m_gameLength;
00117 }
00118 
00119 inline bool GoUctState::IsInPlayout() const
00120 {
00121     return m_isInPlayout;
00122 }
00123 
00124 inline const GoUctBoard& GoUctState::UctBoard() const
00125 {
00126     return m_uctBd;
00127 }
00128 
00129 //----------------------------------------------------------------------------
00130 
00131 /** Live-gfx mode used in GoUctSearch.
00132     @see GoUctSearch::LiveGfx.
00133 */
00134 enum GoUctLiveGfx
00135 {
00136     /** Don't print live-gfx information. */
00137     GOUCT_LIVEGFX_NONE,
00138 
00139     /** Print values and counts as in GoUctUtil::GoGuiGfx() */
00140     GOUCT_LIVEGFX_COUNTS,
00141 
00142     /** Show GoGui live graphics with the main variation.
00143         Shows the main variation on the board instead of the counts and values
00144         (main variation and counts cannot be shown at the same time, because
00145         the sequence numbers conflict with the counts).
00146         The status line shows the same information as in GoGuiGfx()
00147     */
00148     GOUCT_LIVEGFX_SEQUENCE
00149 };
00150 
00151 //----------------------------------------------------------------------------
00152 
00153 /** Base class for UCT searches in Go. */
00154 class GoUctSearch
00155     : public SgUctSearch
00156 {
00157 public:
00158     /** Constructor.
00159         @param bd The board
00160         @param factory
00161     */
00162     GoUctSearch(GoBoard& bd, SgUctThreadStateFactory* factory);
00163 
00164     ~GoUctSearch();
00165 
00166 
00167     /** @name Pure virtual functions of SgUctSearch */
00168     // @{
00169 
00170     std::string MoveString(SgMove move) const;
00171 
00172     /** Color to play at the root node of the last search. */
00173     SgBlackWhite ToPlay() const;
00174 
00175     // @} // @name
00176 
00177 
00178     /** @name Virtual functions of SgUctSearch */
00179     // @{
00180 
00181     void OnSearchIteration(std::size_t gameNumber, int threadId,
00182                            const SgUctGameInfo& info);
00183 
00184     void OnStartSearch();
00185 
00186     // @} // @name
00187 
00188 
00189     /** @name Go-specific functions */
00190     // @{
00191 
00192     GoBoard& Board();
00193 
00194     const GoBoard& Board() const;
00195 
00196     /** See SetKeepGames()
00197         @throws SgException if KeepGames() was false at last invocation of
00198         StartSearch()
00199     */
00200     void SaveGames(const std::string& fileName) const;
00201 
00202     /** See GoUctUtil::SaveTree() */
00203     void SaveTree(std::ostream& out, int maxDepth = -1) const;
00204 
00205     /** Set initial color to play. */
00206     void SetToPlay(SgBlackWhite toPlay);
00207 
00208     /** Identifier for the position the last search was performed on. */
00209     const GoBoardHistory& BoardHistory() const;
00210 
00211     // @} // @name
00212 
00213 
00214     /** @name Go-specific parameters */
00215     // @{
00216 
00217     /** Keep a SGF tree of all games.
00218         This is reset in OnStartSearch() and can be saved with SaveGames().
00219     */
00220     bool KeepGames() const;
00221 
00222     /** See KeepGames() */
00223     void SetKeepGames(bool enable);
00224 
00225     /** Output live graphics commands for GoGui.
00226         If enabled, LiveGfx commands for GoGui are outputted to the debug
00227         stream every n games. Note that GoUctUtil::GoGuiGfx() outputs values
00228         as influence data and assumes values in [0:1].
00229         @see GoUctLiveGfxMode, LiveGfxInterval()
00230     */
00231     GoUctLiveGfx LiveGfx() const;
00232 
00233     /** See LiveGfx() */
00234     void SetLiveGfx(GoUctLiveGfx mode);
00235 
00236     /** Interval for outputting of live graphics commands for GoGui.
00237         Default is every 5000 games.
00238         @see LiveGfx()
00239     */
00240     int LiveGfxInterval() const;
00241 
00242     /** See LiveGfxInterval() */
00243     void SetLiveGfxInterval(int interval);
00244 
00245     // @} // @name
00246 
00247 private:
00248     /** See SetKeepGames() */
00249     bool m_keepGames;
00250 
00251     /** See SetLiveGfxInterval() */
00252     int m_liveGfxInterval;
00253 
00254     /** Color to play.
00255         Does not use GoBoard::ToPlay(), because the color to play at the
00256         root node of the search could be needed after the board has
00257         changed (e.g. when saving the tree after a move was generated and
00258         played on the board).
00259     */
00260     SgBlackWhite m_toPlay;
00261 
00262     /** Stones of position that the current search tree belongs to.
00263         Needed such that the tree can be saved even if the board has
00264         changed (e.g. after a move was generated and played).
00265     */
00266     SgBWSet m_stones;
00267 
00268     GoBoard& m_bd;
00269 
00270     /** See SetKeepGames() */
00271     SgNode* m_root;
00272 
00273     GoUctLiveGfx m_liveGfx;
00274 
00275     GoBoardHistory m_boardHistory;
00276 
00277     /** Not implemented */
00278     GoUctSearch(const GoUctSearch& search);
00279 
00280     /** Not implemented */
00281     GoUctSearch& operator=(const GoUctSearch& search);
00282 };
00283 
00284 inline GoBoard& GoUctSearch::Board()
00285 {
00286     return m_bd;
00287 }
00288 
00289 inline const GoBoard& GoUctSearch::Board() const
00290 {
00291     return m_bd;
00292 }
00293 
00294 inline const GoBoardHistory& GoUctSearch::BoardHistory() const
00295 {
00296     return m_boardHistory;
00297 }
00298 
00299 inline bool GoUctSearch::KeepGames() const
00300 {
00301     return m_keepGames;
00302 }
00303 
00304 inline GoUctLiveGfx GoUctSearch::LiveGfx() const
00305 {
00306     return m_liveGfx;
00307 }
00308 
00309 inline int GoUctSearch::LiveGfxInterval() const
00310 {
00311     return m_liveGfxInterval;
00312 }
00313 
00314 inline void GoUctSearch::SetKeepGames(bool enable)
00315 {
00316     m_keepGames = enable;
00317 }
00318 
00319 inline void GoUctSearch::SetLiveGfx(GoUctLiveGfx mode)
00320 {
00321     m_liveGfx = mode;
00322 }
00323 
00324 inline void GoUctSearch::SetLiveGfxInterval(int interval)
00325 {
00326     SG_ASSERT(interval > 0);
00327     m_liveGfxInterval = interval;
00328 }
00329 
00330 inline void GoUctSearch::SetToPlay(SgBlackWhite toPlay)
00331 {
00332     m_toPlay = toPlay;
00333     m_bd.SetToPlay(toPlay);
00334 }
00335 
00336 //----------------------------------------------------------------------------
00337 
00338 /** Utility functions for users of GoUctSearch. */
00339 namespace GoUctSearchUtil
00340 {
00341     /** Checks if move is a bad pass move, if Tromp-Taylor rules are used,
00342         and tries to fix it.
00343         The rationale for this function is, that a UCT search sometimes plays
00344         an early pass move, which allows the opponent to end the game by also
00345         playing pass, and might lose the game, because all stones on the board
00346         are considered alive under Tromp-Taylor rules. This happens mostly
00347         if the UCT search uses RAVE or prior knowledge and the pass move by
00348         the opponent will not be explored for a while. Fortunately, this can
00349         be fixed after the search: if the Tromp-Taylor score of the current
00350         position is worse than the value of the root node of the search, then
00351         we extract the second best move from the search tree.
00352         @param move The move returned by the search
00353         @param search The search, containing the tree with the last search
00354         and other needed information (like board, rules,
00355         SgUctSearch::ToPlay(), SgUctSearch::UseCount())
00356         @return The second best move, if the above applies and such a move
00357         exists.
00358     */
00359     SgPoint TrompTaylorPassCheck(SgPoint move, const GoUctSearch& search);
00360 }
00361 
00362 //----------------------------------------------------------------------------
00363 
00364 #endif // GOUCT_SEARCH_H


17 Jun 2010 Doxygen 1.4.7