19#include <shared_mutex>
29template <
typename KeyType>
34 using TeamZarray_t = array<PieceZarray_t, gameboard::kNumPieceTypeVals>;
81 for (
auto color_idx = 0; color_idx < 2; color_idx++) {
83 for (
auto rank = 0; rank <
kNumRanks; rank++) {
84 for (
auto file = 0; file <
kNumFiles; file++) {
85 game_zarray[color_idx][piece_id][rank][file] = key_generator.
GenerateKey();
99 if (board_map[rank][file].piece_color != 0) {
101 board_map[rank][file].piece_color,
102 board_map[rank][file].piece_type,
141template <
typename KeyType,
size_t NumConfKeys>
192 for (
auto i = 0; i < NumConfKeys; i++) {
199 std::array<KeyType, NumConfKeys> confirmation_states;
200 for (
auto i = 0; i < NumConfKeys; ++i) {
203 return confirmation_states;
207 std::array<uint32_t, NumConfKeys> seeds;
208 for (
auto i = 0; i < NumConfKeys; ++i) {
217 calculator.UpdateBoardState(executed_move);
224 calculator.FullBoardStateCalc(board_map);
233template <
typename KeyType,
size_t NumConfKeys>
261 for (
auto i = 0; i < NumConfKeys; ++i) {
278template <
typename KeyType,
size_t NumConfKeys>
280 std::unordered_map<KeyType, TranspositionTableEntry<KeyType, NumConfKeys>>
data_;
287 KeyType primary_board_state,
289 std::array<KeyType, NumConfKeys> expected_keys
292 auto tr_table_entry_it =
data_.find(primary_board_state);
293 if (tr_table_entry_it !=
data_.end()) {
294 auto tr_table_entry = tr_table_entry_it->second;
295 if (tr_table_entry.remaining_search_depth() >= remaining_search_depth) {
297 result.set_found(
true);
298 result.set_minimax_calc_result(tr_table_entry.minimax_calc_result());
300 if (result.found() and
301 !tr_table_entry.ConfirmationKeysMatchExpected(expected_keys)) {
302 result.set_known_collision(
true);
309 KeyType primary_board_state,
313 const std::array<KeyType, NumConfKeys> &confirmation_keys
315 data_.insert_or_assign(
330 auto tr_table_entry_it =
data_.at(board_state);
357template <
typename KeyType,
size_t NumConfKeys>
394 std::cout <<
"Pruning!" << std::endl;
398 std::this_thread::sleep_for(std::chrono::microseconds(200));
414template <
typename KeyType,
size_t NumConfKeys>
435 uint32_t primary_seed,
436 std::array<uint32_t, NumConfKeys> confirmation_seeds
Constants, typedefs, and simple structs used by gameboard::GameBoard.
Definition of BoardStateCoordinator.
CRTP Interface with methods to calculate / read / update hash values representing a board state; and ...
Generates pseudorandom integers.
IntType GenerateKey()
Generates a pseudorandom IntType value using mt19937.
Data structure to hold calculation results that get entered into a boardstate::TranspositionTable.
std::array< KeyType, NumConfKeys > confirmation_keys()
MoveCountType last_access_index()
TranspositionTableEntry(moveselection::MinimaxCalcResult minimax_calc_result, std::array< KeyType, NumConfKeys > confirmation_keys, MoveCountType last_access_index)
void set_last_access_index(MoveCountType last_access_index)
moveselection::MinimaxCalcResult minimax_calc_result_
moveselection::MinimaxCalcResult minimax_calc_result()
MoveCountType last_access_index_
std::array< KeyType, NumConfKeys > confirmation_keys_
bool ConfirmationKeysMatchExpected(std::array< KeyType, NumConfKeys > expected_keys)
DepthType remaining_search_depth()
Contains std::mutex that other classes lock before accessing TranspositionTable.
std::mutex tr_table_mutex_
TranspositionTableGuard & operator=(const TranspositionTableGuard &)=delete
std::unique_lock< std::mutex > GetExclusiveLock()
TranspositionTableGuard(const TranspositionTableGuard &)=delete
TranspositionTableGuard()=default
Removes old entries from TranspositionTable to prevent excessive memory use.
std::thread pruning_thread_
TranspositionTablePruner(const TranspositionTablePruner &)=delete
void ThreadSafePruneEntry()
~TranspositionTablePruner()
TranspositionTablePruner & operator=(const TranspositionTablePruner &)=delete
TranspositionTablePruner(TranspositionTable< KeyType, NumConfKeys > &tr_table, TranspositionTableGuard &tr_table_guard)
std::atomic< bool > keep_running_
TranspositionTable< KeyType, NumConfKeys > & tr_table_
TranspositionTableGuard & tr_table_guard_
Stores and manages key-value pairs consisting of a board_state (from a boardstate::ZobristComponent) ...
void IncrementMoveCounter()
void RecordData(KeyType primary_board_state, DepthType search_depth, moveselection::MinimaxResultType result_type, moveselection::EqualScoreMoves &similar_moves, const std::array< KeyType, NumConfKeys > &confirmation_keys)
TranspositionTable()=default
MoveCountType NumMovesSinceLastUseOf(const TranspositionTableEntry< KeyType, NumConfKeys > &tr_table_entry)
std::unordered_map< KeyType, TranspositionTableEntry< KeyType, NumConfKeys > > data_
moveselection::TranspositionTableSearchResult GetDataAt(KeyType primary_board_state, DepthType remaining_search_depth, std::array< KeyType, NumConfKeys > expected_keys)
MoveCountType move_counter_
MoveCountType NumIdleMovesAt(KeyType board_state)
Uses Zobrist hashing to calculate a "reasonably unique" integer value for each board configuration en...
ZobristCalculator(uint32_t seed=std::random_device{}())
Constructs a ZobristCalculator.
void UpdateBoardStateInternal(const gameboard::ExecutedMove &executed_move)
void UpdateBoardState(const gameboard::ExecutedMove &executed_move)
void FullBoardStateCalc(const gameboard::BoardMap_t &board_map)
array< array< KeyType, gameboard::kNumFiles >, gameboard::kNumRanks > PieceZarray_t
void FullBoardStateCalInternal(const gameboard::BoardMap_t &board_map)
array< TeamZarray_t, 2 > GameZarray_t
static const GameZarray_t CreateGameZarray(PseudoRandomKeyGenerator< KeyType > &key_generator)
Static helper method for building 4-D array of Zobrist keys in constuctor.
KeyType board_state() const
GameZarray_t zarray() const
KeyType GetHashValueAt(PieceColor color, PieceType piece_type, BoardSpace space)
array< PieceZarray_t, gameboard::kNumPieceTypeVals > TeamZarray_t
Container for one or more boardstate::ZobristCalculator objects.
ZobristComponent(uint32_t prng_seed=std::random_device{}())
Constructs ZobristComponent using 32-bit unsigned in as a PRNG seed.
std::optional< uint32_t > prng_seed_
std::array< ZobristCalculator< KeyType >, NumConfKeys > confirmation_calculators_
std::array< uint32_t, NumConfKeys > confirmation_calculator_seeds() const
std::string primary_board_state_hex_str() const
ZobristComponent(std::mt19937 prng)
Constructs ZobristComponent from a std::mt19937 pseudorandom number generator.
void FullBoardStateCalcInternal(const BoardMap_t &board_map)
void UpdateBoardStates(const ExecutedMove &executed_move)
std::array< KeyType, NumConfKeys > confirmation_board_states_internal()
void FullBoardStateCalc(const BoardMap_t &board_map)
KeyType primary_calculator_seed()
KeyType primary_board_state()
std::array< KeyType, NumConfKeys > confirmation_board_states()
std::array< uint32_t, NumConfKeys > confirmation_calculator_seeds_internal() const
ZobristCalculator< KeyType > primary_calculator_
ZobristComponent(const ZobristCalculator< KeyType > primary_calculator, const std::array< ZobristCalculator< KeyType >, NumConfKeys > &confirmation_calculators)
Constructs ZobristComponent from existing ZobristCalculator objects.
void UpdateBoardStatesInternal(const ExecutedMove &executed_move)
Implements the BoardStateCoordinator interface, providing a moveselection::MinimaxMoveEvaluator with ...
void ImplementUpdateMoveCounter()
void ImplementFullBoardStateCalc(const BoardMap_t &board_map)
ZobristCoordinator(uint32_t primary_seed, std::array< uint32_t, NumConfKeys > confirmation_seeds)
TranspositionTable< KeyType, NumConfKeys > tr_table_
void ImplementRecordTrData(DepthType search_depth, moveselection::MinimaxResultType result_type, moveselection::EqualScoreMoves &similar_moves, MoveCountType access_index)
moveselection::TranspositionTableSearchResult ImplementGetTrData(DepthType search_depth, MoveCountType access_index)
TranspositionTablePruner< KeyType, NumConfKeys > tr_table_pruner_
ZobristComponent< KeyType, NumConfKeys > zobrist_component_
ZobristCoordinator(uint32_t prng_seed=(uint32_t) std::random_device{}())
ZobristCoordinator & operator=(const ZobristCoordinator &)=delete
KeyType ImplementGetState()
size_t ImplementGetTrTableSize()
ZobristCoordinator(ZobristComponent< KeyType, NumConfKeys > zobrist_component)
const std::string board_state_hex_str()
void ImplementUpdateBoardState(const ExecutedMove &executed_move)
ZobristCoordinator(const ZobristCoordinator &)=delete
TranspositionTableGuard tr_table_guard_
Holds a gameboard::MoveCollection in which all gameboard::Move have the same value (as perceived by a...
Data structure that holds a moveselection::EqualScoreMoves and other search-related info obtained fro...
DepthType remaining_search_depth()
Container for storing a moveselection::MinimaxCalcResult retrieved by a call to boardstate::SingleZob...
Data structs used by moveselection::MinimaxEvaluator.
Declaration of boardstate::KeyGenerator and implementation of its template methods.
Definitions and implementations of gameboard::Move and other move-related structs.
Calculate / manage board state and associate Minimax results.
std::string IntToHexString(IntType num)
size_t GetZColorIndexOf(PieceColor color)
const BoardIndexType kNumFiles
const BoardIndexType kNumRanks
array< array< GamePiece, kNumFiles >, kNumRanks > BoardMap_t
2-D array of gameboard::GamePiece objects.
const int kNumPieceTypeVals
A pair of coordinate (rank, and file) with properties determined by comparison with values of gameboa...
A change in the state of a gameboard::GameBoard represented by a gameboard::Move, and each of the gam...
gameboard::GamePiece moving_piece
gameboard::GamePiece destination_piece
gameboard::BoardSpace end
gameboard::BoardSpace start