HexUctState.hpp
Go to the documentation of this file.00001
00002
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
00028 struct HexUctStoneData
00029 {
00030 bitset_t black;
00031
00032 bitset_t white;
00033
00034 bitset_t played;
00035
00036
00037 HexUctStoneData();
00038
00039
00040 HexUctStoneData(const StoneBoard& brd);
00041
00042
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
00067 struct HexUctSharedData
00068 {
00069
00070 int board_width;
00071
00072
00073 int board_height;
00074
00075
00076 HexUctStoneData root_stones;
00077
00078
00079 MoveSequence game_sequence;
00080
00081
00082 HexColor root_to_play;
00083
00084
00085
00086
00087 HexPoint root_last_move_played;
00088
00089
00090 bitset_t root_consider;
00091
00092
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
00108
00109 class HexUctSearchPolicy
00110 {
00111 public:
00112 virtual ~HexUctSearchPolicy() { };
00113
00114
00115
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
00127 class HexUctState : public SgUctThreadState
00128 {
00129 public:
00130
00131
00132
00133
00134
00135
00136
00137 HexUctState(const unsigned int threadId, HexUctSearch& sch,
00138 int treeUpdateRadius, int playoutUpdateRadius);
00139
00140 virtual ~HexUctState();
00141
00142
00143
00144
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
00167
00168
00169
00170
00171
00172
00173 void GameStart();
00174
00175 void StartPlayouts();
00176
00177 virtual void StartPlayout();
00178
00179 virtual void EndPlayout();
00180
00181
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
00196
00197 void SetPolicy(HexUctSearchPolicy* policy);
00198
00199 HexPoint GetLastMovePlayed() const;
00200
00201 HexColor GetColorToPlay() const;
00202
00203 private:
00204
00205
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
00223 boost::scoped_ptr<PatternState> m_pastate;
00224
00225
00226 boost::scoped_ptr<HexBoard> m_vc_brd;
00227
00228
00229 boost::scoped_ptr<HexUctSearchPolicy> m_policy;
00230
00231
00232 HexUctSharedData* m_shared_data;
00233
00234 HexUctKnowledge m_knowledge;
00235
00236
00237 HexUctSearch& m_search;
00238
00239
00240 HexColor m_toPlay;
00241
00242
00243 int m_treeUpdateRadius;
00244
00245
00246 int m_playoutUpdateRadius;
00247
00248
00249 bool m_isInPlayout;
00250
00251
00252 MoveSequence m_game_sequence;
00253
00254
00255
00256
00257 HexPoint m_lastMovePlayed;
00258
00259
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