How does this engine handle search, focus on move prioritization and when it goes to what depth especially?
Assistant
Ethereal's search logic prioritizes moves and explores depths using a combination of iterative deepening, aspiration windows, and a sophisticated move picker. Here's a breakdown:
Iterative Deepening:
The engine uses iterative deepening, starting its search at depth 1 and incrementing until a time limit, depth limit, or other search condition is met (iterativeDeepening function in search.c). This allows the engine to use information from shallower searches to guide deeper searches, improving efficiency. Transposition table entries from previous depths also inform the search.
Aspiration Windows:
At each depth, Ethereal uses aspiration windows (aspirationWindow function in search.c). This technique searches with a narrow alpha-beta window around the expected score (based on the previous iteration's score). If the score falls within the window, it's accepted. If the score falls outside (fail-high or fail-low), the window is widened and the search is repeated. This often saves time compared to a full alpha-beta search with wide initial bounds.
Move Picker:
The move picker (movepicker.c) plays a crucial role in move prioritization. It proceeds through several stages:
TT Move (STAGE_TABLE): The move suggested by the transposition table is tried first. This leverages information from previous searches.
Good Noisy Moves (STAGE_GENERATE_NOISY, STAGE_GOOD_NOISY): Captures and promotions are generated and ordered. Crucially, a static exchange evaluation (SEE) is performed on these moves, and only moves that pass the SEE threshold are considered "good" and searched first. This prioritizes tactically beneficial captures.
Killer Moves (STAGE_KILLER_1, STAGE_KILLER_2): If the search isn't in a "noisy" phase (skipping quiets), the two "killer" moves are tried. These are moves that caused beta cutoffs in other parts of the search tree at the same depth and are considered promising even if they aren't captures.
Counter Move (STAGE_COUNTER_MOVE): A counter-move from the history heuristic is tried if it hasn't already been played. This move represents a strong reply to the previous move in the game's history.
Quiet Moves (STAGE_GENERATE_QUIET, STAGE_QUIET): If not skipping quiets, all remaining moves (mostly quiet moves) are generated and ordered based on history heuristics.
Bad Noisy Moves (STAGE_BAD_NOISY): Finally, captures and promotions that failed the SEE are searched. These are less promising but still need to be considered.
User
Explain the history heuristics.
Assistant
Ethereal uses history heuristics to order moves within the move picker, improving search efficiency by trying more promising moves earlier. It maintains two history tables:
QuietHistory: This table stores bonuses for moves (non-captures, non-promotions). Entries are indexed by the moving piece type and the destination square.
User
And this is the default prioritization after cached and killer moves?
Assistant
Yes, after the transposition table move (cached move) and killer moves, the default prioritization in Ethereal's move picker is as follows:
Counter Move: A move suggested by the history heuristic that counters the opponent's previous move. This is tried before other quiet moves.
Remaining Quiet Moves (History Heuristic): Quiet moves are ordered based on their history heuristic scores. Moves with higher scores, indicating they've led to cutoffs in previous searches, are tried first.
Bad Captures (SEE Failing Captures): Captures that failed the static exchange evaluation are tried last. These are considered less promising than other captures and quiet moves backed by strong history scores.
So, to clarify, the complete move ordering in the main search is:
Transposition Table Move: The move suggested by the transposition table.
User
Give more code quotes on this point, 5-20 lines each.
Assistant
Here are some code snippets from Ethereal that illustrate the move ordering after cached (TT) and killer moves:
1. select_next() in movepicker.c (handling the stages):
c
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Normal Search: Iterative deepening controls the overall depth progression. Extensions and reductions within the search are performed based on various factors like checks, passed pawns, and singular replies.
Quiescence Search: When the search reaches a leaf node (depth 0) or a relatively quiet position (no king attackers), a quiescence search (qsearch in search.c) is performed. This search only considers captures, promotions, and check evasions to ensure a more stable evaluation. The QSDeltaMargin helps decide whether to extend the quiescence search further.
Late Move Reductions (LMR): For later moves in a given ply, the search depth is reduced. The LMRTable dynamically calculates the reduction based on the current depth and the number of moves already played in that ply. This optimization exploits the fact that later moves are often less influential.
Other Pruning Mechanisms: Various pruning techniques (null move pruning, futility pruning, probcut) influence the effective search depth by selectively skipping branches of the search tree deemed unlikely to improve the score.
In summary, Ethereal prioritizes moves by first considering transposition table suggestions, then good captures, followed by killer moves, counter-moves, quiet moves, and finally bad captures. The depth explored is primarily managed by iterative deepening, with adjustments made based on tactical situations, LMR, and other pruning techniques. This approach allows for efficient and selective exploration of the search tree, focusing resources on the most promising lines of play.
quiet
CaptureHistory: This table stores bonuses for captures. Entries are indexed by the attacking piece type and the destination square (same as QuietHistory).
How the tables are used:
Move Ordering: When generating quiet or capture moves, the move picker consults the appropriate history table. Moves with higher history scores are prioritized and tried earlier in the search. This improves the chances of finding a good move early, leading to better alpha-beta cutoffs.
MovePicker Stages: History heuristics are used in the STAGE_GOOD_NOISY (captures), STAGE_QUIET (quiet moves), and STAGE_BAD_NOISY (remaining captures) stages of the move picker.
Updating the tables:
The history tables are updated during the search whenever a cutoff occurs:
search() function in search.c: When a beta cutoff occurs, the history score for the move that caused the cutoff is increased. The bonus added to the history table is scaled based on the depth at which the cutoff occurred; deeper cutoffs result in larger bonuses. This reinforces moves that have proven effective in similar positions.
History heuristics are an important part of Ethereal's search, helping to guide the move ordering.
They learn from successful searches by rewarding moves that cause cutoffs.
The bonuses are scaled with depth, giving more weight to cutoffs that occur deeper in the search tree.
Separate tables are maintained for quiet and capture moves. This allows for more specialized move ordering within each category.
Good Captures (SEE Passing Captures): Captures that pass the static exchange evaluation, ordered by their history heuristic scores.
Killer Moves: Two killer moves stored for each ply.
Counter Move: A counter move suggested by the history heuristic.
Remaining Quiet Moves (History Heuristic): Quiet moves ordered by history heuristic scores.
Bad Captures (SEE Failing Captures): Captures that failed the SEE.
This order ensures that the most promising moves, based on a combination of past search experience (transposition table, killer moves, history heuristic) and tactical considerations (SEE), are tried first.
switch (mp->stage) {
// ... (TT move and good captures)
case STAGE_KILLER_1: // ...
case STAGE_KILLER_2: // ...
case STAGE_COUNTER_MOVE: // Counter move before quiet moves
mp->stage = STAGE_GENERATE_QUIET;
if (/* ... conditions ... */)
return mp->counter;
case STAGE_GENERATE_QUIET: // Generate and score quiet moves
// ...
case STAGE_QUIET: // Prioritize quiet moves by history heuristic
while (/* ... */) {
best = best_index(mp, mp->split, mp->split + mp->quiet_size) - mp->split; // Best by history
// ...
}
case STAGE_BAD_NOISY: // SEE failing captures last
while (/* ... */) {
// ...
}
// ...
}
This shows the order of the stages in the move picker: counter move, then quiet moves by history, and finally bad captures.
2. get_capture_histories() and get_quiet_histories() in movepicker.c (using history heuristics):
c
void get_capture_histories(Thread *thread, uint16_t *moves, int *values, int start, int end) {
for (int i = start; i < end; i++) {
int from = MoveFrom(moves[i]);
int to = MoveTo(moves[i]);
values[i] = CaptureHistory[thread->board.turn][pieceType(thread->board.squares[from])][to];
// ... (SEE adjustments)
}
}
void get_quiet_histories(Thread *thread, uint16_t *moves, int *values, int start, int end) {
for (int i = start; i < end; i++) {
int from = MoveFrom(moves[i]);
int to = MoveTo(moves[i]);
values[i] = QuietHistory[thread->board.turn][pieceType(thread->board.squares[from])][to];
}
}
These functions demonstrate how history scores are directly used to assign values to moves, which then influence the move ordering. Higher history scores lead to higher values, and thus earlier selection within the move picker.