|
io-chess
UCI chess engine
|
Implementation of the main Alpha-Beta search algorithm. More...
#include <Negamax.h>


Public Member Functions | |
| Negamax (IEvaluator &eval, TranspositionTable &table, std::shared_ptr< SearchSharedData > shared, bool isMainThread, int threadId=0) | |
| Constructs a new Negamax search instance. | |
| Move | startSearch (Board &root, const SearchParams ¶ms) override |
| Starts the search process on the given root board. | |
| void | stop () override |
| Asynchronously signals the search to stop immediately. | |
| bool | isSearching () const override |
| Checks if the search algorithm is currently running. | |
| void | setInfoCallback (InfoCallback callback) override |
| Sets the callback function for periodic information updates. | |
| uint64_t | getNodes () const override |
| Retrieves the total number of nodes evaluated by the search algorithm. | |
| Public Member Functions inherited from ISearch | |
| virtual | ~ISearch ()=default |
Private Member Functions | |
| uint64_t | getPawnKey (const Board &board) const |
| Computes a hash key based on the pawn structure. | |
| uint64_t | getNonPawnKey (const Board &board, Color c) const |
| Computes a hash key based on non-pawn pieces of a given color. | |
| int | alphaBeta (Board &board, int depth, int alpha, int beta, int ply, bool allowNull, int extensions, bool prevWasCapture=false, bool cutnode=false, Move excludedMove=Move(0)) |
| The core recursive Alpha-Beta search function. | |
| int | quiescence (Board &board, int alpha, int beta, int ply) |
| Quiescence search to resolve tactical sequences before static evaluation. | |
| bool | canLazySkip (const Board &board, int depth, int alpha, int beta, int ply, int fastScore, Bound &outBound, bool prevWasCapture, bool pvNode) |
| Determines if the position can bypass a full neural network evaluation. | |
| int | scoreMoveFast (const Move &move, const Board &board, Move ttMove, int ply, bool isCapture) |
| Scores a move rapidly for sorting during move generation. | |
| void | updateKillers (Move move, int ply) |
| Updates killer move heuristics when a quiet move causes a beta cutoff. | |
| void | updateHistory (const Board &board, Move move, int bonus, int ply) |
| Updates history and continuation history heuristics based on search results. | |
| int | see (const Board &board, Move move) const |
| Static Exchange Evaluation (SEE) for a move. | |
| int | pieceValue (PieceType pt) const |
| Returns the base material value of a piece type for SEE calculations. | |
| int | applyCorrHist (int rawEval, const Board &board, int ply) const |
| Applies historical corrections to the raw neural network evaluation. | |
| bool | isPassedPawn (const Board &board, Square sq, Color side) const |
| Determines if the pawn on the given square is a passed pawn. | |
| bool | isPawnPush7th (const Board &board, Move move) const |
| Determines if the move pushes a pawn to the 7th rank (or 2nd for black). | |
| void | syncTimeToShared () |
| Syncs the thread-local elapsed time to the shared data for the main thread. | |
| bool | shouldStop () const |
| Checks if the search should be aborted due to time limits or external stop signals. | |
| int64_t | elapsedMs () const |
| Calculates the elapsed time since the search started. | |
| bool | shouldStopIteration (int depth, int score, Move bestMove) const |
| Decides whether to stop the search early at the end of an Iterative Deepening iteration. | |
| bool | isCapture (const Board &board, Move move) const |
| Returns true if the move is a capture. | |
| bool | isDraw (const Board &board) const |
| Returns true if the position is a theoretical draw (e.g. 50-move rule, 3-fold repetition, insufficient material). | |
| int | mateScore (int ply) const |
| Returns the mate score adjusted for the current ply (so closer mates score higher). | |
| bool | isMateScore (int score) const |
| Checks if the given score is within the checkmate score range. | |
| void | clearState () |
| Resets transient state arrays and variables before starting a new search. | |
| void | flushLocalSearchStats () |
| Flushes local statistics (nodes, hits) to the global shared atomic counters. | |
Private Attributes | |
| IEvaluator & | evalCtx_ |
| Reference to the thread-local evaluator. | |
| TranspositionTable & | tt_ |
| Reference to the global transposition table. | |
| std::shared_ptr< SearchSharedData > | shared_ |
| Shared state for thread coordination. | |
| bool | isMainThread_ |
| True if this is the primary search thread. | |
| int | threadId_ |
| Unique ID for this thread (used for move ordering diversity). | |
| uint64_t | localNodes_ = 0 |
| Nodes searched by this thread. | |
| uint64_t | localTbHits_ = 0 |
| Tablebase hits by this thread. | |
| uint64_t | localTtHits_ = 0 |
| TT hits by this thread. | |
| int | localSelDepth_ = 0 |
| Maximum selective depth reached by this thread. | |
| TimeManager | timeManager_ |
| Manages time limits and soft/hard stops. | |
| TimeManager::TimeAllocation | timeAlloc_ {} |
| Pre-calculated time allocation constraints. | |
| float | lastEvalCp_ = 0.0f |
| Last evaluation score in centipawns (used for time decisions). | |
| Move | lastBestMove_ |
| Best move found in the previous iterative deepening iteration. | |
| int | lastScore_ = 0 |
| Score returned in the previous iteration. | |
| int | bestMoveChanges_ = 0 |
| Count of how many times the best move changed at root. | |
| int | scoreDrops_ = 0 |
| Count of significant evaluation drops at root. | |
| std::array< uint64_t, 4096 > | rootNodeCounts_ {} |
| Number of nodes spent evaluating each root move. | |
| uint64_t | searchNodes_ = 0 |
| Total nodes searched during the current search session. | |
| std::unique_ptr< SearchHeuristics > | h_ |
| Heap-allocated structure containing large arrays for search heuristics. | |
| std::array< Move, SearchConstants::MAX_PLY > | prevMove_ |
| Records the move made at each ply to reach the current state. | |
| std::array< Piece, SearchConstants::MAX_PLY > | prevPiece_ {} |
| Records the piece that moved at each ply. | |
| std::array< int, SearchConstants::MAX_PLY > | pvLength_ |
| Tracks the length of the principal variation at each ply. | |
| std::array< int, SearchConstants::MAX_PLY > | evalHistory_ {} |
| Tracks static evaluation history for pruning thresholds. | |
| int | currentIter_ = 0 |
| The current depth iteration in Iterative Deepening. | |
| std::vector< Move > | searchMoves_ |
| Restricts the search to only these root moves (used for 'searchmoves' command). | |
| InfoCallback | infoCallback_ |
| Callback function to output search progress to the UCI protocol. | |
| SimpleEvalContext | simpleEval_ |
| A fast, simple evaluator used for pre-NN filtering or lazy evaluation. | |
Static Private Attributes | |
| static constexpr uint64_t | NODE_FLUSH_INTERVAL = 1024 |
| How often to flush local stats to the shared global counters. | |
Friends | |
| class | MovePicker |
| MovePicker requires access to scoreMoveFast and heuristics. | |
Implementation of the main Alpha-Beta search algorithm.
This class encapsulates the search state for a single thread, including local transposition table statistics, move ordering heuristics, and time management. It coordinates with other search threads through SearchSharedData.
| Negamax::Negamax | ( | IEvaluator & | eval, |
| TranspositionTable & | table, | ||
| std::shared_ptr< SearchSharedData > | shared, | ||
| bool | isMainThread, | ||
| int | threadId = 0 ) |
Constructs a new Negamax search instance.
| eval | Thread-local evaluator instance. |
| table | Global transposition table. |
| shared | Shared state for Lazy SMP coordination. |
| isMainThread | True if this instance is the primary search thread. |
| threadId | Unique identifier for this thread. |

|
private |
The core recursive Alpha-Beta search function.
| board | The current board state. |
| depth | The remaining search depth. |
| alpha | The lower bound of the current search window. |
| beta | The upper bound of the current search window. |
| ply | The current distance from the root. |
| allowNull | True if Null Move Pruning is permitted at this node. |
| extensions | The number of depth extensions applied so far along this path. |
| prevWasCapture | True if the move that led to this node was a capture. |
| cutnode | True if we expect this node to fail high (used for LMR scaling). |
| excludedMove | A move to ignore during search (used for Singular Extensions). |


|
private |
Applies historical corrections to the raw neural network evaluation.
Corrects systematic evaluation errors based on pawn structures and piece configurations.
| rawEval | The raw static evaluation. |
| board | The board state. |
| ply | The distance from root. |


|
private |
Determines if the position can bypass a full neural network evaluation.
| board | The board state. |
| depth | Remaining depth. |
| alpha | Alpha bound. |
| beta | Beta bound. |
| ply | Distance from root. |
| fastScore | Score from the fast/simple evaluator. |
| outBound | Output bound (Upper or Lower) if skipping is permitted. |
| prevWasCapture | True if previous move was a capture. |
| pvNode | True if this is a Principal Variation node. |

|
private |
Resets transient state arrays and variables before starting a new search.

|
private |
Calculates the elapsed time since the search started.

|
private |
Flushes local statistics (nodes, hits) to the global shared atomic counters.

|
inlineoverridevirtual |
Retrieves the total number of nodes evaluated by the search algorithm.
Implements ISearch.
|
private |
Computes a hash key based on non-pawn pieces of a given color.
| board | The current board state. |
| c | The color of the pieces to hash. |

|
private |
Computes a hash key based on the pawn structure.
| board | The current board state. |

Returns true if the move is a capture.

|
private |
Returns true if the position is a theoretical draw (e.g. 50-move rule, 3-fold repetition, insufficient material).

|
inlineprivate |
Checks if the given score is within the checkmate score range.

Determines if the pawn on the given square is a passed pawn.
| board | The current board state. |
| sq | The square of the pawn. |
| side | The color of the pawn. |
Determines if the move pushes a pawn to the 7th rank (or 2nd for black).
| board | The current board state. |
| move | The move to evaluate. |
|
inlineoverridevirtual |
Checks if the search algorithm is currently running.
Implements ISearch.
|
private |
Returns the mate score adjusted for the current ply (so closer mates score higher).

|
private |
Returns the base material value of a piece type for SEE calculations.
|
private |
Quiescence search to resolve tactical sequences before static evaluation.
Searches only captures and promotions to ensure the evaluation is stable.
| board | The current board state. |
| alpha | The lower bound. |
| beta | The upper bound. |
| ply | The distance from the root. |


|
private |
Scores a move rapidly for sorting during move generation.
| move | The move to score. |
| board | The current board state. |
| ttMove | The best move retrieved from the Transposition Table. |
| ply | The current distance from the root. |
| isCapture | True if the move is a capture. |

Static Exchange Evaluation (SEE) for a move.
Evaluates the material consequence of a sequence of captures on a single square.
| board | The board state. |
| move | The capture move to evaluate. |


|
inlineoverridevirtual |
Sets the callback function for periodic information updates.
| callback | The function to call with depth, score, node count, NPS, and PV. |
Implements ISearch.
|
private |
Checks if the search should be aborted due to time limits or external stop signals.


|
private |
Decides whether to stop the search early at the end of an Iterative Deepening iteration.
Triggers soft time limits early if the best move seems stable and no score drops have occurred.
| depth | The completed search depth. |
| score | The best score achieved at this depth. |
| bestMove | The best move found. |


|
overridevirtual |
Starts the search process on the given root board.
This is a blocking call that will execute the search algorithm until the stopping conditions in params (or stop()) are met.
| root | The starting board position. |
| params | The time and depth constraints for the search. |
Implements ISearch.


|
inlineoverridevirtual |
Asynchronously signals the search to stop immediately.
This method must be thread-safe.
Implements ISearch.
|
private |
Syncs the thread-local elapsed time to the shared data for the main thread.

Updates history and continuation history heuristics based on search results.
| board | The board state before the move. |
| move | The move executed. |
| bonus | The bonus/penalty to apply (based on search depth). |
| ply | The current distance from root. |

|
private |
Updates killer move heuristics when a quiet move causes a beta cutoff.
| move | The move that caused the cutoff. |
| ply | The distance from root where the cutoff occurred. |

|
friend |
MovePicker requires access to scoreMoveFast and heuristics.
|
private |
Count of how many times the best move changed at root.
|
private |
The current depth iteration in Iterative Deepening.
|
private |
Reference to the thread-local evaluator.
|
private |
Tracks static evaluation history for pruning thresholds.
|
private |
Heap-allocated structure containing large arrays for search heuristics.
|
private |
Callback function to output search progress to the UCI protocol.
|
private |
True if this is the primary search thread.
|
private |
Best move found in the previous iterative deepening iteration.
|
private |
Last evaluation score in centipawns (used for time decisions).
|
private |
Score returned in the previous iteration.
|
private |
Nodes searched by this thread.
|
private |
Maximum selective depth reached by this thread.
|
private |
Tablebase hits by this thread.
|
private |
TT hits by this thread.
|
staticconstexprprivate |
How often to flush local stats to the shared global counters.
|
private |
Records the move made at each ply to reach the current state.
|
private |
Records the piece that moved at each ply.
|
private |
Tracks the length of the principal variation at each ply.
|
private |
Number of nodes spent evaluating each root move.
|
private |
Count of significant evaluation drops at root.
|
private |
Restricts the search to only these root moves (used for 'searchmoves' command).
|
private |
Total nodes searched during the current search session.
|
private |
Shared state for thread coordination.
|
private |
A fast, simple evaluator used for pre-NN filtering or lazy evaluation.
|
private |
Unique ID for this thread (used for move ordering diversity).
|
private |
Pre-calculated time allocation constraints.
|
private |
Manages time limits and soft/hard stops.
|
private |
Reference to the global transposition table.