Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

HexUctState.hpp

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file HexUctState.hpp
00003  */
00004 //----------------------------------------------------------------------------
00005 
00006 #ifndef HEXUCTSTATE_HPP
00007 #define HEXUCTSTATE_HPP
00008 
00009 #include "SgBlackWhite.h"
00010 #include "SgPoint.h"
00011 #include "SgUctSearch.h"
00012 
00013 #include "HashMap.hpp"
00014 #include "HexBoard.hpp"
00015 #include "HexUctKnowledge.hpp"
00016 #include "Move.hpp"
00017 #include "VC.hpp"
00018 
00019 #include <boost/scoped_ptr.hpp>
00020 
00021 _BEGIN_BENZENE_NAMESPACE_
00022 
00023 class HexUctSearch;
00024 
00025 //----------------------------------------------------------------------------
00026 
00027 /** Black and white stones. */
00028 struct HexUctStoneData
00029 {
00030     bitset_t black;
00031 
00032     bitset_t white;
00033 
00034     bitset_t played;
00035 
00036     /** Creates empty stone set. */
00037     HexUctStoneData();
00038 
00039     /** Copies stones from board. */
00040     HexUctStoneData(const StoneBoard& brd);
00041 
00042     /** Returns true only if all three sets are equal. */
00043     bool operator==(const HexUctStoneData& other) const;
00044 };
00045 
00046 inline HexUctStoneData::HexUctStoneData()
00047 {
00048 }
00049 
00050 inline HexUctStoneData::HexUctStoneData(const StoneBoard& brd)
00051     : black(brd.GetBlack()),
00052       white(brd.GetWhite()),
00053       played(brd.GetPlayed())
00054 {
00055 }
00056 
00057 inline bool HexUctStoneData::operator==(const HexUctStoneData& other) const
00058 {
00059     return black == other.black 
00060         && white == other.white
00061         && played == other.played;
00062 }
00063 
00064 //----------------------------------------------------------------------------
00065 
00066 /** Data shared among all threads. */
00067 struct HexUctSharedData
00068 {
00069     /** Width of board used in last search. */
00070     int board_width;
00071 
00072     /** Height of board used in last search. */ 
00073     int board_height;
00074 
00075     /** Stones in root position. */
00076     HexUctStoneData root_stones;
00077 
00078     /** Moves from begining of game leading to this position. */
00079     MoveSequence game_sequence;
00080 
00081     /** Color to play. */
00082     HexColor root_to_play;
00083 
00084     /** Move played that led to this state.
00085         @todo Remove and use game_sequence to get this info?
00086     */
00087     HexPoint root_last_move_played;
00088 
00089     /** Set of moves to consider from the root. */
00090     bitset_t root_consider;
00091 
00092     /** Stores fillin information for states in the tree. */
00093     HashMap<HexUctStoneData> stones;
00094 
00095     HexUctSharedData();
00096 };
00097 
00098 inline HexUctSharedData::HexUctSharedData()
00099     : board_width(-1), 
00100       board_height(-1), 
00101       stones(16)
00102 { 
00103 }
00104 
00105 //----------------------------------------------------------------------------
00106 
00107 /** Interface for policies controlling move generation in the random
00108     play-out phase of HexUctSearch. */
00109 class HexUctSearchPolicy
00110 {
00111 public:
00112     virtual ~HexUctSearchPolicy() { };
00113 
00114     /** Generate a move in the random play-out phase of
00115         HexUctSearch. */
00116     virtual HexPoint GenerateMove(PatternState& pastate, HexColor color, 
00117                                   HexPoint lastMove) = 0;
00118 
00119     virtual void InitializeForRollout(const StoneBoard& brd) = 0;
00120 
00121     virtual void InitializeForSearch() = 0;
00122 };
00123 
00124 //----------------------------------------------------------------------------
00125 
00126 /** Thread state for HexUctSearch. */
00127 class HexUctState : public SgUctThreadState
00128 {
00129 public: 
00130     /** Constructor.
00131         @param threadId The number of the thread. Needed for passing to
00132         constructor of SgUctThreadState.
00133         @param sch Parent Search object.
00134         @param treeUpdateRadius Pattern matching radius in tree.
00135         @param playoutUpdateRadius Pattern matching radius in playouts.
00136     */
00137     HexUctState(const unsigned int threadId, HexUctSearch& sch,
00138                 int treeUpdateRadius, int playoutUpdateRadius);
00139 
00140     virtual ~HexUctState();
00141 
00142     //-----------------------------------------------------------------------
00143 
00144     /** @name Pure virtual functions of SgUctThreadState */
00145     // @{
00146 
00147     SgUctValue Evaluate();
00148     
00149     void Execute(SgMove move);
00150 
00151     void ExecutePlayout(SgMove move);
00152    
00153     bool GenerateAllMoves(SgUctValue count, std::vector<SgMoveInfo>& moves,
00154                           SgProvenNodeType& provenType);
00155 
00156     SgMove GeneratePlayoutMove(bool& skipRaveUpdate);
00157     
00158     void StartSearch();
00159 
00160     void TakeBackInTree(std::size_t nuMoves);
00161 
00162     void TakeBackPlayout(std::size_t nuMoves);
00163 
00164     SgBlackWhite ToPlay() const;
00165 
00166     // @} // @name
00167 
00168     //-----------------------------------------------------------------------
00169 
00170     /** @name Virtual functions of SgUctThreadState */
00171     // @{
00172 
00173     void GameStart();
00174 
00175     void StartPlayouts();
00176 
00177     virtual void StartPlayout();
00178 
00179     virtual void EndPlayout();
00180 
00181     // @} // @name
00182 
00183     //-----------------------------------------------------------------------
00184 
00185     const StoneBoard& Board() const;
00186 
00187     const PatternState& GetPatternState() const;
00188 
00189     HexUctSearchPolicy* Policy();
00190 
00191     bool IsInPlayout() const;
00192 
00193     std::string Dump() const;
00194 
00195     /** Sets policy (takes control of pointer) and deletes the old
00196         one, if it existed. */
00197     void SetPolicy(HexUctSearchPolicy* policy);
00198     
00199     HexPoint GetLastMovePlayed() const;
00200 
00201     HexColor GetColorToPlay() const;
00202 
00203 private:
00204 
00205     /** Assertion handler to dump the state of a HexUctState. */
00206     class AssertionHandler
00207         : public SgAssertionHandler
00208     {
00209     public:
00210         AssertionHandler(const HexUctState& state);
00211 
00212         void Run();
00213 
00214     private:
00215         const HexUctState& m_state;
00216     };
00217 
00218     AssertionHandler m_assertionHandler;
00219 
00220     boost::scoped_ptr<StoneBoard> m_bd;
00221 
00222     /** Board used during search. */
00223     boost::scoped_ptr<PatternState> m_pastate;
00224 
00225     /** Board used to compute knowledge. */
00226     boost::scoped_ptr<HexBoard> m_vc_brd;
00227 
00228     /** Playout policy. */
00229     boost::scoped_ptr<HexUctSearchPolicy> m_policy;
00230 
00231     /** Data shared between threads. */
00232     HexUctSharedData* m_shared_data;
00233 
00234     HexUctKnowledge m_knowledge;
00235 
00236     /** Parent search object. */
00237     HexUctSearch& m_search;
00238    
00239     /** Color to play next. */
00240     HexColor m_toPlay;
00241 
00242     /** See HexUctSearch::TreeUpdateRadius() */
00243     int m_treeUpdateRadius;
00244 
00245     /** See HexUctSearch::PlayoutUpdateRadius() */
00246     int m_playoutUpdateRadius;
00247 
00248     /** True if in playout phase. */
00249     bool m_isInPlayout;
00250 
00251     /** Moves played the game plus moves in tree. */
00252     MoveSequence m_game_sequence;
00253 
00254     /** Keeps track of last playout move made.
00255     Used for pattern-generated rollouts when call
00256     HexUctSearchPolicy. */
00257     HexPoint m_lastMovePlayed;
00258 
00259     /** True at the start of a game until the first move is played. */
00260     bool m_new_game;
00261 
00262     //----------------------------------------------------------------------
00263 
00264     bitset_t ComputeKnowledge(SgProvenNodeType& provenType);
00265 
00266     void ExecuteTreeMove(HexPoint move);
00267 
00268     void ExecuteRolloutMove(HexPoint move);
00269 
00270     void ExecutePlainMove(HexPoint cell, int updateRadius);
00271 };
00272 
00273 inline const StoneBoard& HexUctState::Board() const
00274 {
00275     return *m_bd;
00276 }
00277 
00278 inline const PatternState& HexUctState::GetPatternState() const
00279 {
00280     return *m_pastate;
00281 }
00282 
00283 inline HexUctSearchPolicy* HexUctState::Policy()
00284 {
00285     return m_policy.get();
00286 }
00287 
00288 inline bool HexUctState::IsInPlayout() const
00289 {
00290     return m_isInPlayout;
00291 }
00292 
00293 inline HexPoint HexUctState::GetLastMovePlayed() const
00294 {
00295     return m_lastMovePlayed;
00296 }
00297 
00298 inline HexColor HexUctState::GetColorToPlay() const
00299 {
00300     return m_toPlay;
00301 }
00302 
00303 //----------------------------------------------------------------------------
00304 
00305 _END_BENZENE_NAMESPACE_
00306 
00307 #endif // HEXUCTSTATE_HPP


6 Jan 2011 Doxygen 1.6.3