16#include <unordered_map>
22namespace minimaxutils_forconcepts {
26 alpha = max(alpha, cur_eval);
36 bool is_prunable = (beta <= alpha);
48 bool is_prunable = (beta <= alpha);
59template <SpaceInfoProv
iderConcept G, PieceValueProv
iderConcept P>
71 std::shared_ptr<G> game_board,
72 std::shared_ptr<P> game_position_points
81 ->GetValueOfPieceAtPosition(cur_player, piece_type, move.
end);
87 auto position_value_delta = end_points - start_points;
102 return ScoredMove{move, (position_value_delta + capture_val)};
109 vector<ScoredMove> rated_moves;
110 for (
auto cur_move : cur_player_moves.
moves) {
111 auto cur_rated_move =
RateMove(cur_move, cur_player);
112 rated_moves.emplace_back(cur_rated_move);
118 return move_a.score > move_b.score;
148 std::shared_ptr<G> game_board,
149 std::shared_ptr<P> game_position_points,
167 throw std::runtime_error(
"MinimaxMoveEvaluator selected an illegal move");
174 return final_selected_move;
203 return first_search_summary.selected_move();
210 for (
auto space :
game_board_->GetAllSpacesOccupiedBy(color)) {
215 return pre_attack_total;
225 return first_search_summary;
236 return second_search_summary;
268 return EqualScoreMoves{numeric_limits<Points_t>::min(), empty_best_moves};
271 return EqualScoreMoves{numeric_limits<Points_t>::max(), empty_best_moves};
301 (cur_player_points - opponent_points),
302 empty_move_collection
306 (opponent_points - cur_player_points),
307 empty_move_collection
332 return cur_eval > previous_best_eval;
334 return cur_eval < previous_best_eval;
345 if (cur_eval == previous_best_eval) {
347 }
else if (
IsImprovement(cur_eval, previous_best_eval, cur_player)) {
348 previous_best_eval = cur_eval;
349 best_moves.
moves.clear();
400 bool use_transposition_table
402 auto executed_move =
game_board_->ExecuteMove(move);
411 use_transposition_table
420 return numeric_limits<Points_t>::min();
422 return numeric_limits<Points_t>::max();
447 bool use_transposition_table
451 auto ranked_moves =
move_sorter_.GenerateRankedMoveList(cur_player, allowed_moves);
453 for (
auto rated_move : ranked_moves) {
462 use_transposition_table
465 UpdateBestMoves(cur_player, rated_move.move, best_moves, cur_eval, max_eval);
469 if (
IsPrunable(alpha, beta, result_type, cur_player)) {
490 bool use_transposition_table
496 if (use_transposition_table) {
497 auto tr_table_search_result =
499 if (tr_table_search_result.found() &&
500 tr_table_search_result.IsConsistentWith(allowed_moves)) {
504 tr_table_search_result,
511 if (allowed_moves.
moves.size() == 0) {
530 use_transposition_table
537 bool use_transposition_table =
true
539 auto search_start = std::chrono::high_resolution_clock::now();
543 numeric_limits<Points_t>::min(),
544 numeric_limits<Points_t>::max(),
547 use_transposition_table
549 auto search_end = std::chrono::high_resolution_clock::now();
550 std::chrono::duration<double, std::nano> search_time = search_end - search_start;
551 search_summary.
set_time(search_time);
553 return minimax_result;
559 bool use_transposition_table =
true
561 auto minimax_result =
562 RunTimedMinimax(allowed_moves, search_summary, use_transposition_table);
563 auto selected_move = minimax_result.move_collection().SelectRandom();
Constants, typedefs, and simple structs used by gameboard::GameBoard.
Holds a gameboard::MoveCollection in which all gameboard::Move have the same value (as perceived by a...
Complies with MoveEvaluatorConcept, and selects move::Move using Minimax algorithm; uses SpaceInfoPro...
std::shared_ptr< H > hash_calculator_
PieceColor evaluating_player_
EqualScoreMoves EvaluateEndOfGameLeaf(PieceColor cur_player, MinimaxResultType &result_type)
Points_t RecursivelyVisitNodes(Move move, PieceColor cur_player, const MoveCollection &allowed_moves, DepthType search_depth, Points_t alpha, Points_t beta, SearchSummary &search_summary, bool use_transposition_table)
bool IsPrunable(Points_t &alpha, Points_t &beta, MinimaxResultType &result_type, PieceColor cur_player)
std::shared_ptr< G > game_board_
void IncrementNumMoveSelections()
Move SelectValidMove(const MoveCollection &allowed_moves)
EqualScoreMoves RunTimedMinimax(const MoveCollection &allowed_moves, SearchSummary &search_summary, bool use_transposition_table=true)
std::shared_ptr< P > game_position_points_
void UpdateBestMoves(PieceColor cur_player, Move move, MoveCollection &best_moves, Points_t cur_eval, Points_t &previous_best_eval)
MoveCountType num_move_selections_
const uint32_t zkeys_seed()
const H & hash_calculator() const
bool IsImprovement(Points_t cur_eval, Points_t previous_best_eval, PieceColor cur_player)
SearchSummary & RunSecondSearch(const MoveCollection &allowed_moves)
EqualScoreMoves FinalizeNodeResult(MinimaxResultType &result_type, Points_t best_eval, MoveCollection best_moves, DepthType search_depth, SearchSummary &search_summary)
moveselection::SearchSummaries search_summaries_
SearchSummary & RunFirstSearch(const MoveCollection &allowed_moves)
Points_t InitializedBestEval(PieceColor cur_player)
EqualScoreMoves EvaluateNonWinLeaf(PieceColor cur_player, MinimaxResultType &result_type)
MinimaxMoveEvaluatorForConcepts(PieceColor evaluating_player, DepthType search_depth, std::shared_ptr< G > game_board, std::shared_ptr< P > game_position_points, std::shared_ptr< H > hash_calculator)
EqualScoreMoves MinimaxRecursive(const MoveCollection &allowed_moves, DepthType search_depth, Points_t alpha, Points_t beta, PieceColor cur_player, SearchSummary &search_summary, bool use_transposition_table)
typename H::KeyType KeyType
PreSearchMoveSorterForConcepts< G, P > move_sorter_
void UpdatePruningParam(Points_t &alpha, Points_t &beta, Points_t cur_eval, PieceColor cur_player)
const std::optional< moveselection::SearchSummaries > search_summaries() const override
Points_t GetPlayerTotal(PieceColor color)
EqualScoreMoves HandleInternalNode(PieceColor cur_player, const MoveCollection &allowed_moves, DepthType &search_depth, Points_t &alpha, Points_t &beta, MinimaxResultType result_type, SearchSummary &search_summary, bool use_transposition_table)
void GetMinimaxMoveAndStats(const MoveCollection &allowed_moves, SearchSummary &search_summary, bool use_transposition_table=true)
EqualScoreMoves HandleLeaf(PieceColor cur_player, SearchSummary &search_summary, MinimaxResultType &result_type, DepthType search_depth)
Move SelectMove(const MoveCollection &allowed_moves)
std::atomic< bool > game_over_
EqualScoreMoves HandleEndOfGame(PieceColor cur_player, SearchSummary &search_summary, MinimaxResultType &result_type, DepthType search_depth)
EqualScoreMoves HandleTrTableHit(SearchSummary &search_summary, MinimaxResultType &result_type, TranspositionTableSearchResult &tr_table_search_result, DepthType search_depth)
Sorts moves based on points changed induce by single move; used by MinimaxMoveEvaluator for pre-sorti...
std::vector< ScoredMove > GenerateRankedMoveList(PieceColor cur_player, const MoveCollection &cur_player_moves)
std::shared_ptr< P > game_position_points_
ScoredMove RateMove(Move move, PieceColor cur_player)
PreSearchMoveSorterForConcepts(std::shared_ptr< G > game_board, std::shared_ptr< P > game_position_points)
std::shared_ptr< G > game_board_
Stores data collected during a single call to moveselection::MinimaxMoveEvaluator....
void set_time(std::chrono::duration< double, std::nano > search_time)
void RecordNodeInfo(MinimaxResultType result_type, DepthType search_depth, const EqualScoreMoves &equal_score_moves)
void RecordTrTableHit(TranspositionTableSearchResult &tr_table_search_result, DepthType remaining_search_depth)
void set_tr_table_size_final(size_t tr_table_size_final)
void SetSelectedMove(Move selected_move)
Container for storing a moveselection::MinimaxCalcResult retrieved by a call to boardstate::SingleZob...
EqualScoreMoves score_and_moves()
Data structs used by moveselection::MinimaxEvaluator.
Tracking piece positions and determining legal moves.
PieceColor opponent_of(PieceColor color)
bool IsPrunableForEvaluator(Points_t &alpha, Points_t &beta, MinimaxResultType &result_type)
void UpdateAlpha(Points_t &alpha, Points_t cur_eval)
bool IsPrunableForEvaluatorOpponent(Points_t &alpha, Points_t &beta, MinimaxResultType &result_type)
bool ValidateMove(SearchSummary &search_summary, const MoveCollection &allowed_moves)
void UpdateBeta(Points_t &beta, Points_t cur_eval)
Selecting a move to execute.
A container for multiple gameboard::Move objects.
A gameboard::BoardSpace pair (start and end).
gameboard::BoardSpace end
gameboard::BoardSpace start
A gameboard::Move, and an associated score calculated by a MoveEvaluator.
Stores a moveselection::SearchSummary for each moveselection::MinimaxMoveEvaluator....
SearchSummary & NewFirstSearch(DepthType search_depth, size_t tr_table_size_initial)
SearchSummary & NewExtraSearch(DepthType search_depth, MoveCountType search_number, size_t tr_table_size_current)