Expands a Book using the given player to evaluate game positions. More...
#include <BookBuilder.hpp>
Classes | |
class | Worker |
Copyable worker. More... | |
Public Member Functions | |
BookBuilder (PLAYER &player) | |
Constructor. | |
~BookBuilder () | |
Destructor. | |
void | SetState (Book &book, const HexState &state) |
Sets the state to start work from. | |
void | SetWorkBoard (HexBoard &brd) |
Sets the board the book builder can use to perform work. | |
bool | UseICE () const |
Whether to prune out inferior cells from the book or not. | |
void | SetUseICE (bool flag) |
See UseICE(). | |
std::size_t | NumThreads () const |
Number of players to use during leaf expansion. | |
void | SetNumThreads (std::size_t num) |
See NumThreads(). | |
Protected Member Functions | |
std::string | MoveString (SgMove move) const |
void | PrintMessage (std::string msg) |
float | InverseEval (float eval) const |
bool | IsLoss (float eval) const |
void | PlayMove (SgMove move) |
void | UndoMove (SgMove move) |
bool | GetNode (SgBookNode &node) const |
float | Value (const SgBookNode &node) const |
void | WriteNode (const SgBookNode &node) |
void | FlushBook () |
void | EnsureRootExists () |
Creates root node if necessary. | |
bool | GenerateMoves (std::vector< SgMove > &moves, float &value) |
Computes an ordered set of moves to consider. | |
void | GetAllLegalMoves (std::vector< SgMove > &moves) |
void | EvaluateChildren (const std::vector< SgMove > &childrenToDo, std::vector< std::pair< SgMove, float > > &scores) |
void | Init () |
void | StartIteration (int iteration) |
void | EndIteration () |
void | BeforeEvaluateChildren () |
void | AfterEvaluateChildren () |
void | Fini () |
void | ClearAllVisited () |
void | MarkAsVisited () |
bool | HasBeenVisited () |
Private Member Functions | |
void | CreateWorkers () |
Copies the player and board and creates the threads. | |
void | DestroyWorkers () |
Destroys copied players, boards, and threads. | |
Private Attributes | |
Book * | m_book |
Book this builder is expanding. | |
PLAYER & | m_orig_player |
Player passed to constructor. | |
HexBoard * | m_brd |
HexState | m_state |
std::set< hash_t > | m_visited |
bool | m_use_ice |
See UseICE(). | |
std::size_t | m_num_threads |
See NumberThreads(). | |
std::size_t | m_num_evals |
std::size_t | m_num_widenings |
std::size_t | m_value_updates |
std::size_t | m_priority_updates |
std::size_t | m_internal_nodes |
std::size_t | m_leaf_nodes |
std::size_t | m_terminal_nodes |
std::vector< HexBoard * > | m_boards |
Boards for each thread. | |
std::vector< BenzenePlayer * > | m_players |
Players for each thread. | |
std::vector< Worker > | m_workers |
SgThreadedWorker< SgMove, float, Worker > * | m_threadedWorker |
Expands a Book using the given player to evaluate game positions.
Supports multithreaded evaluation of states.
We do not include the swap rule as a move, since this would lead to redundant evaluation computations (such as a2-f6 and a2-swap-f6). We do handle swap implicitly, however. States in which swap is a valid move are scored taking it into account.
Definition at line 38 of file BookBuilder.hpp.
BookBuilder< PLAYER >::BookBuilder | ( | PLAYER & | player | ) | [inline] |
Constructor.
Takes a reference to the player that will evaluate states, which must be a reference to a BenzenePlayer.
Definition at line 194 of file BookBuilder.hpp.
BookBuilder< PLAYER >::~BookBuilder | ( | ) | [inline] |
Destructor.
Definition at line 205 of file BookBuilder.hpp.
void BookBuilder< PLAYER >::AfterEvaluateChildren | ( | ) | [inline, protected] |
Definition at line 493 of file BookBuilder.hpp.
void BookBuilder< PLAYER >::BeforeEvaluateChildren | ( | ) | [inline, protected] |
Definition at line 478 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_state, BookBuilder< PLAYER >::m_workers, and BookBuilder< PLAYER >::SetState().
void BookBuilder< PLAYER >::ClearAllVisited | ( | ) | [inline, protected] |
Definition at line 510 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_visited.
void BookBuilder< PLAYER >::CreateWorkers | ( | ) | [inline, private] |
Copies the player and board and creates the threads.
Definition at line 239 of file BookBuilder.hpp.
References LogInfo(), BookBuilder< PLAYER >::m_boards, BookBuilder< PLAYER >::m_brd, BookBuilder< PLAYER >::m_num_threads, BookBuilder< PLAYER >::m_orig_player, BookBuilder< PLAYER >::m_players, BookBuilder< PLAYER >::m_threadedWorker, and BookBuilder< PLAYER >::m_workers.
Referenced by BookBuilder< PLAYER >::Init().
void BookBuilder< PLAYER >::DestroyWorkers | ( | ) | [inline, private] |
Destroys copied players, boards, and threads.
Definition at line 262 of file BookBuilder.hpp.
References LogInfo(), BookBuilder< PLAYER >::m_boards, BookBuilder< PLAYER >::m_num_threads, BookBuilder< PLAYER >::m_players, BookBuilder< PLAYER >::m_threadedWorker, and BookBuilder< PLAYER >::m_workers.
Referenced by BookBuilder< PLAYER >::Fini().
void BookBuilder< PLAYER >::EndIteration | ( | ) | [inline, protected] |
Definition at line 504 of file BookBuilder.hpp.
void BookBuilder< PLAYER >::EnsureRootExists | ( | ) | [inline, protected] |
Creates root node if necessary.
Definition at line 417 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::GetNode(), BookBuilder< PLAYER >::m_state, BookBuilder< PLAYER >::m_workers, and BookBuilder< PLAYER >::WriteNode().
void BookBuilder< PLAYER >::EvaluateChildren | ( | const std::vector< SgMove > & | childrenToDo, | |
std::vector< std::pair< SgMove, float > > & | scores | |||
) | [inline, protected] |
Definition at line 486 of file BookBuilder.hpp.
void BookBuilder< PLAYER >::Fini | ( | ) | [inline, protected] |
Definition at line 283 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::DestroyWorkers().
void BookBuilder< PLAYER >::FlushBook | ( | ) | [inline, protected] |
Definition at line 396 of file BookBuilder.hpp.
References StateDB< T >::Flush(), LogInfo(), and BookBuilder< PLAYER >::m_book.
bool BookBuilder< PLAYER >::GenerateMoves | ( | std::vector< SgMove > & | moves, | |
float & | value | |||
) | [inline, protected] |
Computes an ordered set of moves to consider.
Returns true if state is determined, with the value set in value and moves untouched. Returns false otherwise, in which case moves will contain the sorted moves and value will be untouched.
Definition at line 433 of file BookBuilder.hpp.
References benzene_bitset< _Nb >::any(), HexBoard::ComputeAll(), HexBoard::GetPosition(), HexAssert, HexEval, EndgameUtils::IsDeterminedState(), BookBuilder< PLAYER >::m_brd, BookBuilder< PLAYER >::m_state, BookBuilder< PLAYER >::m_use_ice, EndgameUtils::MovesToConsider(), HexState::Position(), StoneBoard::SetPosition(), HexBoard::SetUseICE(), HexState::ToPlay(), and HexBoard::UseICE().
void BookBuilder< PLAYER >::GetAllLegalMoves | ( | std::vector< SgMove > & | moves | ) | [inline, protected] |
Definition at line 409 of file BookBuilder.hpp.
References StoneBoard::GetEmpty(), BookBuilder< PLAYER >::m_state, and HexState::Position().
bool BookBuilder< PLAYER >::GetNode | ( | SgBookNode & | node | ) | const [inline, protected] |
Definition at line 377 of file BookBuilder.hpp.
References StateDB< T >::Get(), BookBuilder< PLAYER >::m_book, and BookBuilder< PLAYER >::m_state.
Referenced by BookBuilder< PLAYER >::EnsureRootExists().
bool BookBuilder< PLAYER >::HasBeenVisited | ( | ) | [inline, protected] |
Definition at line 522 of file BookBuilder.hpp.
References HexState::Hash(), BookBuilder< PLAYER >::m_state, and BookBuilder< PLAYER >::m_visited.
void BookBuilder< PLAYER >::Init | ( | ) | [inline, protected] |
Definition at line 277 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::CreateWorkers().
float BookBuilder< PLAYER >::InverseEval | ( | float | eval | ) | const [inline, protected] |
Definition at line 353 of file BookBuilder.hpp.
bool BookBuilder< PLAYER >::IsLoss | ( | float | eval | ) | const [inline, protected] |
Definition at line 359 of file BookBuilder.hpp.
void BookBuilder< PLAYER >::MarkAsVisited | ( | ) | [inline, protected] |
Definition at line 516 of file BookBuilder.hpp.
References HexState::Hash(), BookBuilder< PLAYER >::m_state, and BookBuilder< PLAYER >::m_visited.
std::string BookBuilder< PLAYER >::MoveString | ( | SgMove | move | ) | const [inline, protected] |
Definition at line 341 of file BookBuilder.hpp.
References HexPointUtil::ToString().
std::size_t BookBuilder< PLAYER >::NumThreads | ( | ) | const [inline] |
Number of players to use during leaf expansion.
Each player may use a multi-threaded search. Should speed up the expansion of leaf states by a factor of (very close to) NumThreads().
Definition at line 224 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_num_threads.
void BookBuilder< PLAYER >::PlayMove | ( | SgMove | move | ) | [inline, protected] |
Definition at line 365 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_state, and HexState::PlayMove().
void BookBuilder< PLAYER >::PrintMessage | ( | std::string | msg | ) | [inline, protected] |
Definition at line 347 of file BookBuilder.hpp.
References LogInfo().
void BookBuilder< PLAYER >::SetNumThreads | ( | std::size_t | num | ) | [inline] |
See NumThreads().
Definition at line 230 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_num_threads.
void BookBuilder< PLAYER >::SetState | ( | Book & | book, | |
const HexState & | state | |||
) | [inline] |
Sets the state to start work from.
Definition at line 328 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_book, and BookBuilder< PLAYER >::m_state.
Referenced by BookBuilder< PLAYER >::BeforeEvaluateChildren().
void BookBuilder< PLAYER >::SetUseICE | ( | bool | flag | ) | [inline] |
See UseICE().
Definition at line 218 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_use_ice.
void BookBuilder< PLAYER >::SetWorkBoard | ( | HexBoard & | brd | ) | [inline] |
Sets the board the book builder can use to perform work.
Definition at line 335 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_brd.
void BookBuilder< PLAYER >::StartIteration | ( | int | iteration | ) | [inline, protected] |
Definition at line 498 of file BookBuilder.hpp.
References LogInfo().
void BookBuilder< PLAYER >::UndoMove | ( | SgMove | move | ) | [inline, protected] |
Definition at line 371 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_state, and HexState::UndoMove().
bool BookBuilder< PLAYER >::UseICE | ( | ) | const [inline] |
Whether to prune out inferior cells from the book or not.
Definition at line 212 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_use_ice.
float BookBuilder< PLAYER >::Value | ( | const SgBookNode & | node | ) | const [inline, protected] |
Definition at line 403 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_state.
void BookBuilder< PLAYER >::WriteNode | ( | const SgBookNode & | node | ) | [inline, protected] |
Definition at line 389 of file BookBuilder.hpp.
References BookBuilder< PLAYER >::m_book, BookBuilder< PLAYER >::m_state, and StateDB< T >::Put().
Referenced by BookBuilder< PLAYER >::EnsureRootExists().
std::vector<HexBoard*> BookBuilder< PLAYER >::m_boards [private] |
Boards for each thread.
Definition at line 177 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::CreateWorkers(), and BookBuilder< PLAYER >::DestroyWorkers().
Book* BookBuilder< PLAYER >::m_book [private] |
Book this builder is expanding.
Definition at line 145 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::FlushBook(), BookBuilder< PLAYER >::GetNode(), BookBuilder< PLAYER >::SetState(), and BookBuilder< PLAYER >::WriteNode().
HexBoard* BookBuilder< PLAYER >::m_brd [private] |
Definition at line 150 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::CreateWorkers(), BookBuilder< PLAYER >::GenerateMoves(), and BookBuilder< PLAYER >::SetWorkBoard().
std::size_t BookBuilder< PLAYER >::m_internal_nodes [private] |
Definition at line 170 of file BookBuilder.hpp.
std::size_t BookBuilder< PLAYER >::m_leaf_nodes [private] |
Definition at line 172 of file BookBuilder.hpp.
std::size_t BookBuilder< PLAYER >::m_num_evals [private] |
Definition at line 162 of file BookBuilder.hpp.
std::size_t BookBuilder< PLAYER >::m_num_threads [private] |
See NumberThreads().
Definition at line 160 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::CreateWorkers(), BookBuilder< PLAYER >::DestroyWorkers(), BookBuilder< PLAYER >::NumThreads(), and BookBuilder< PLAYER >::SetNumThreads().
std::size_t BookBuilder< PLAYER >::m_num_widenings [private] |
Definition at line 164 of file BookBuilder.hpp.
PLAYER& BookBuilder< PLAYER >::m_orig_player [private] |
Player passed to constructor.
Definition at line 148 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::CreateWorkers().
std::vector<BenzenePlayer*> BookBuilder< PLAYER >::m_players [private] |
Players for each thread.
Definition at line 180 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::CreateWorkers(), and BookBuilder< PLAYER >::DestroyWorkers().
std::size_t BookBuilder< PLAYER >::m_priority_updates [private] |
Definition at line 168 of file BookBuilder.hpp.
HexState BookBuilder< PLAYER >::m_state [private] |
Definition at line 152 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::BeforeEvaluateChildren(), BookBuilder< PLAYER >::EnsureRootExists(), BookBuilder< PLAYER >::GenerateMoves(), BookBuilder< PLAYER >::GetAllLegalMoves(), BookBuilder< PLAYER >::GetNode(), BookBuilder< PLAYER >::HasBeenVisited(), BookBuilder< PLAYER >::MarkAsVisited(), BookBuilder< PLAYER >::PlayMove(), BookBuilder< PLAYER >::SetState(), BookBuilder< PLAYER >::UndoMove(), BookBuilder< PLAYER >::Value(), and BookBuilder< PLAYER >::WriteNode().
std::size_t BookBuilder< PLAYER >::m_terminal_nodes [private] |
Definition at line 174 of file BookBuilder.hpp.
SgThreadedWorker<SgMove,float,Worker>* BookBuilder< PLAYER >::m_threadedWorker [private] |
Definition at line 184 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::CreateWorkers(), and BookBuilder< PLAYER >::DestroyWorkers().
bool BookBuilder< PLAYER >::m_use_ice [private] |
See UseICE().
Definition at line 157 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::GenerateMoves(), BookBuilder< PLAYER >::SetUseICE(), and BookBuilder< PLAYER >::UseICE().
std::size_t BookBuilder< PLAYER >::m_value_updates [private] |
Definition at line 166 of file BookBuilder.hpp.
std::set<hash_t> BookBuilder< PLAYER >::m_visited [private] |
Definition at line 154 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::ClearAllVisited(), BookBuilder< PLAYER >::HasBeenVisited(), and BookBuilder< PLAYER >::MarkAsVisited().
std::vector<Worker> BookBuilder< PLAYER >::m_workers [private] |
Definition at line 182 of file BookBuilder.hpp.
Referenced by BookBuilder< PLAYER >::BeforeEvaluateChildren(), BookBuilder< PLAYER >::CreateWorkers(), BookBuilder< PLAYER >::DestroyWorkers(), and BookBuilder< PLAYER >::EnsureRootExists().