Xiangiqgame
AI engine for Xiangqi
Loading...
Searching...
No Matches
zobrist_for_concepts.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <atomic>
6#include <chrono>
11#include <memory>
13#include <mutex>
14#include <optional>
15#include <random>
16#include <shared_mutex>
17#include <thread>
18#include <vector>
20
21namespace boardstate {
22
26template <SingleBoardStateProviderConcept C, size_t N>
28
29 std::shared_ptr<C> primary_calculator_;
30 std::array<std::shared_ptr<C>, N> confirmation_calculators_;
31 uint32_t prng_seed_;
32
33public:
34 using KeyType = typename C::KeyType;
35 static size_t constexpr NumConfKeys = N;
36
37public:
38 static std::shared_ptr<ZobristComponentForConcepts<C, N>> Create(
39 uint32_t prng_seed = std::random_device{}()
40 ) {
41 std::mt19937 prng{prng_seed};
42 auto primary_calculator =
44 std::array<std::shared_ptr<C>, N> confirmation_calculators;
45 for (auto idx = 0; idx < N; ++idx) {
46 confirmation_calculators[idx] =
48 }
49
50 return std::shared_ptr<ZobristComponentForConcepts<C, N>>(
51 new ZobristComponentForConcepts<C, N>(
52 primary_calculator,
53 confirmation_calculators,
55 )
56 );
57 }
58
59 static std::shared_ptr<ZobristComponentForConcepts<C, N>> Create(
60 std::shared_ptr<C> primary_calculator,
61 std::array<std::shared_ptr<C>, N> confirmation_calculators,
62 std::uint32_t prng_seed = 0
63 ) {
64 return std::shared_ptr<ZobristComponentForConcepts<C, N>>(
66 primary_calculator,
67 confirmation_calculators,
69 )
70 );
71 }
72
73 // Getters
74 typename C::KeyType primary_board_state() { return primary_calculator_->board_state(); }
75 std::array<typename C::KeyType, N> confirmation_board_states() {
77 }
78 typename C::KeyType primary_calculator_seed() { return primary_calculator_->seed(); }
79 std::array<uint32_t, N> confirmation_calculator_seeds() const {
81 }
82 std::string primary_board_state_hex_str() const {
84 }
85 uint32_t prng_seed() { return prng_seed_; }
86
87private:
91 std::shared_ptr<C> primary_calculator,
92 std::array<std::shared_ptr<C>, N> confirmation_calculators,
93 uint32_t prng_seed = 0
94 )
95 : primary_calculator_{primary_calculator}
96 , confirmation_calculators_{confirmation_calculators}
98
99 // Internal getters
100 std::array<typename C::KeyType, N> confirmation_board_states_internal() {
101 std::array<typename C::KeyType, N> confirmation_states;
102 for (auto i = 0; i < N; ++i) {
103 confirmation_states[i] = confirmation_calculators_[i]->board_state();
104 }
105 return confirmation_states;
106 }
107
108 std::array<uint32_t, N> confirmation_calculator_seeds_internal() const {
109 std::array<uint32_t, N> seeds;
110 for (auto i = 0; i < N; ++i) {
111 seeds[i] = confirmation_calculators_[i].seed();
112 }
113 }
114};
115
120template <typename KeyType, size_t NumConfKeys>
122private:
124 std::array<KeyType, NumConfKeys> confirmation_keys_;
126
127public:
130 std::array<KeyType, NumConfKeys> confirmation_keys,
132 )
136
138
139 std::array<KeyType, NumConfKeys> confirmation_keys() { return confirmation_keys_; }
140
142
145 }
146
147 bool ConfirmationKeysMatchExpected(std::array<KeyType, NumConfKeys> expected_keys) {
148 for (auto i = 0; i < NumConfKeys; ++i) {
149 if (expected_keys[i] != confirmation_keys_[i]) {
150 return false;
151 }
152 }
153 return true;
154 }
155
158 }
159};
160
165template <typename KeyType, size_t NumConfKeys>
167 std::unordered_map<KeyType, TranspositionTableEntryForConcepts<KeyType, NumConfKeys>>
170
171public:
173
175 KeyType primary_board_state,
176 DepthType remaining_search_depth,
177 std::array<KeyType, NumConfKeys> expected_keys
178 ) {
180 auto tr_table_entry_it = data_.find(primary_board_state);
181 if (tr_table_entry_it != data_.end()) {
182 auto tr_table_entry = tr_table_entry_it->second;
183 if (tr_table_entry.remaining_search_depth() >= remaining_search_depth) {
184 tr_table_entry.set_last_access_index(move_counter_);
185 result.set_found(true);
186 result.set_minimax_calc_result(tr_table_entry.minimax_calc_result());
187 }
188 if (result.found() and
189 !tr_table_entry.ConfirmationKeysMatchExpected(expected_keys)) {
190 result.set_known_collision(true);
191 }
192 }
193 return result;
194 }
195
197 KeyType primary_board_state,
198 DepthType search_depth,
200 moveselection::EqualScoreMoves &similar_moves,
201 const std::array<KeyType, NumConfKeys> &confirmation_keys
202 ) {
203 data_.insert_or_assign(
204 primary_board_state,
206 moveselection::MinimaxCalcResult{search_depth, result_type, similar_moves},
207 confirmation_keys,
209 }
210 );
211 }
212
213 size_t size() { return data_.size(); }
214
216
217 MoveCountType NumIdleMovesAt(KeyType board_state) {
218 auto tr_table_entry_it = data_.at(board_state);
219 return NumMovesSinceLastUseOf(tr_table_entry_it->second);
220 }
221
222private:
225 ) {
226 return move_counter_ - tr_table_entry.last_access_index();
227 }
228};
229
233 mutable std::mutex tr_table_mutex_;
234
235public:
238 delete;
240 &) = delete;
241
242 std::unique_lock<std::mutex> GetExclusiveLock() {
243 return std::unique_lock<std::mutex>(tr_table_mutex_);
244 }
245};
246
249template <typename KeyType, size_t NumConfKeys>
253 std::thread pruning_thread_;
254 std::atomic<bool> keep_running_;
255
256public:
258 delete;
261
265 )
266 : tr_table_{tr_table}
267 , tr_table_guard_{tr_table_guard}
269 , keep_running_{true} {}
270
272 if (pruning_thread_.joinable()) {
273 pruning_thread_.join();
274 }
275 }
276
277 void Start() {
280 }
281
282 void Stop() { keep_running_ = false; }
283
284 // void IncrementMoveCounter() { move_count_++; }
285
286private:
288 // TODO: Implement me!
289 std::cout << "Pruning!" << std::endl;
290 }
291
293 std::this_thread::sleep_for(std::chrono::microseconds(200));
294 auto lock = tr_table_guard_.GetExclusiveLock();
296 }
297
299 while (keep_running_) {
301 }
302 }
303};
304
305template <MultiBoardStateProviderConcept M>
307public:
308 using KeyType = typename M::KeyType;
309 static size_t constexpr NumConfKeys = M::NumConfKeys;
310
311private:
312 std::shared_ptr<M> zobrist_component_;
316
317public:
318
319 static std::shared_ptr<ZobristCoordinatorForConcepts<M>> Create(
320 std::shared_ptr<M> zobrist_component
321 ) {
322 return std::shared_ptr<ZobristCoordinatorForConcepts<M>>(
323 new ZobristCoordinatorForConcepts<M>(zobrist_component)
324 );
325 }
326
327 KeyType GetState() { return zobrist_component_->primary_board_state(); }
329 DepthType search_depth,
331 moveselection::EqualScoreMoves &similar_moves,
332 MoveCountType access_index
333 ) {
335 zobrist_component_->primary_board_state(),
336 search_depth,
337 result_type,
338 similar_moves,
339 zobrist_component_->confirmation_board_states()
340 );
341 }
343 DepthType search_depth,
344 MoveCountType access_index
345 ) {
346 return tr_table_.GetDataAt(
347 zobrist_component_->primary_board_state(),
348 search_depth,
349 zobrist_component_->confirmation_board_states()
350 );
351 }
352 size_t GetTrTableSize() { return tr_table_.size(); }
353
355 // tr_table_pruner_.IncrementMoveCounter();
357 }
358
359 const std::string board_state_hex_str() {
360 return IntToHexString(zobrist_component_->primary_board_state());
361 }
362
363 uint32_t zkeys_seed() { return zobrist_component_->prng_seed(); }
364
365private:
366 ZobristCoordinatorForConcepts(std::shared_ptr<M> zobrist_component)
367 : zobrist_component_{zobrist_component}
368 , tr_table_{}
371 } {
372 // tr_table_pruner_.Start();
373 }
374};
375
376} // namespace boardstate
Constants, typedefs, and simple structs used by gameboard::GameBoard.
Data structure to hold calculation results that get entered into a boardstate::TranspositionTableForC...
TranspositionTableEntryForConcepts(moveselection::MinimaxCalcResult minimax_calc_result, std::array< KeyType, NumConfKeys > confirmation_keys, MoveCountType last_access_index)
std::array< KeyType, NumConfKeys > confirmation_keys()
bool ConfirmationKeysMatchExpected(std::array< KeyType, NumConfKeys > expected_keys)
moveselection::MinimaxCalcResult minimax_calc_result_
void set_last_access_index(MoveCountType last_access_index)
std::array< KeyType, NumConfKeys > confirmation_keys_
moveselection::MinimaxCalcResult minimax_calc_result()
Stores and manages key-value pairs consisting of a board_state (from a boardstate::ZobristComponent) ...
MoveCountType NumIdleMovesAt(KeyType board_state)
void RecordData(KeyType primary_board_state, DepthType search_depth, moveselection::MinimaxResultType result_type, moveselection::EqualScoreMoves &similar_moves, const std::array< KeyType, NumConfKeys > &confirmation_keys)
moveselection::TranspositionTableSearchResult GetDataAt(KeyType primary_board_state, DepthType remaining_search_depth, std::array< KeyType, NumConfKeys > expected_keys)
MoveCountType NumMovesSinceLastUseOf(const TranspositionTableEntryForConcepts< KeyType, NumConfKeys > &tr_table_entry)
std::unordered_map< KeyType, TranspositionTableEntryForConcepts< KeyType, NumConfKeys > > data_
Contains std::mutex that other classes lock before accessing TranspositionTableForConcepts.
TranspositionTableGuardForConcepts(const TranspositionTableGuardForConcepts &)=delete
TranspositionTableGuardForConcepts & operator=(const TranspositionTableGuardForConcepts &)=delete
Removes old entries from TranspositionTableForConcepts to prevent excessive memory use.
TranspositionTableGuardForConcepts & tr_table_guard_
TranspositionTablePrunerForConcepts(const TranspositionTablePrunerForConcepts &)=delete
TranspositionTablePrunerForConcepts & operator=(const TranspositionTablePrunerForConcepts &)=delete
TranspositionTablePrunerForConcepts(TranspositionTableForConcepts< KeyType, NumConfKeys > &tr_table, TranspositionTableGuardForConcepts &tr_table_guard)
TranspositionTableForConcepts< KeyType, NumConfKeys > & tr_table_
static std::shared_ptr< ZobristCalculatorForConcepts< K > > Create(uint32_t seed=std::random_device{}())
Container for one or more boardstate::ZobristCalculatorForConcepts objects.
std::array< std::shared_ptr< C >, N > confirmation_calculators_
std::array< uint32_t, N > confirmation_calculator_seeds() const
std::array< uint32_t, N > confirmation_calculator_seeds_internal() const
std::array< typename C::KeyType, N > confirmation_board_states()
static std::shared_ptr< ZobristComponentForConcepts< C, N > > Create(uint32_t prng_seed=std::random_device{}())
std::array< typename C::KeyType, N > confirmation_board_states_internal()
ZobristComponentForConcepts(std::shared_ptr< C > primary_calculator, std::array< std::shared_ptr< C >, N > confirmation_calculators, uint32_t prng_seed=0)
Constructs ZobristComponentForConcepts from existing ZobristCalculatorForConcepts objects.
static std::shared_ptr< ZobristComponentForConcepts< C, N > > Create(std::shared_ptr< C > primary_calculator, std::array< std::shared_ptr< C >, N > confirmation_calculators, std::uint32_t prng_seed=0)
void RecordTrData(DepthType search_depth, moveselection::MinimaxResultType result_type, moveselection::EqualScoreMoves &similar_moves, MoveCountType access_index)
TranspositionTableGuardForConcepts tr_table_guard_
moveselection::TranspositionTableSearchResult GetTrData(DepthType search_depth, MoveCountType access_index)
ZobristCoordinatorForConcepts(std::shared_ptr< M > zobrist_component)
static std::shared_ptr< ZobristCoordinatorForConcepts< M > > Create(std::shared_ptr< M > zobrist_component)
TranspositionTablePrunerForConcepts< KeyType, M::NumConfKeys > tr_table_pruner_
TranspositionTableForConcepts< KeyType, M::NumConfKeys > tr_table_
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...
Container for storing a moveselection::MinimaxCalcResult retrieved by a call to boardstate::SingleZob...
Data structs used by moveselection::MinimaxEvaluator.
uint16_t DepthType
uint16_t MoveCountType
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)