Add UCI time management with iterative deepening#57
Open
Conversation
BenchmarksThe following benchmarks are available for this PR:
Post a comment with the command to trigger a benchmark run. |
Greptile SummaryThis PR adds UCI-compliant time management to the Moonfish chess engine through iterative deepening search. Major changes:
Implementation approach: Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| moonfish/engines/alpha_beta.py | Added iterative deepening with time management; clean implementation with proper state reset and fallback handling |
| moonfish/mode/uci.py | Implemented UCI time parameter parsing and time allocation logic with appropriate safety margins |
Flowchart
flowchart TD
A[UCI 'go' command received] --> B{Parse parameters}
B --> C{movetime specified?}
C -->|Yes| D[time_limit = movetime * 0.95]
C -->|No| E{wtime/btime specified?}
E -->|Yes| F[Calculate: remaining/movestogo + inc*0.8]
F --> G[Cap at 25% of remaining time]
G --> H[Apply 50ms safety margin]
E -->|No| I[Use fixed depth search]
D --> J[search_move_timed]
H --> J
J --> K[Initialize: nodes=0, cache=empty]
K --> L[Start iterative deepening: depth=1]
L --> M[Call negamax with current depth]
M --> N{Every 1024 nodes}
N -->|Check| O{Time expired?}
O -->|Yes| P[Set _time_abort=True]
P --> Q[Return 0, None immediately]
O -->|No| R[Continue search]
N -->|Skip| R
R --> S{Search complete?}
S -->|Aborted| T[Discard partial result]
S -->|Complete| U[Save best_move]
U --> V{Remaining < elapsed*3?}
V -->|Yes| W[Stop iterating]
V -->|No| X[depth += 1]
T --> W
X --> M
W --> Y{best_move found?}
Y -->|Yes| Z[Return best_move]
Y -->|No| AA[Return random_move]
I --> AB[search_move with fixed depth]
Last reviewed commit: a1c3b6b
a1c3b6b to
0eb5350
Compare
The engine previously searched to a fixed depth (default 3) regardless of available time, wasting most of the allocated thinking time in timed games. With 60s per move, the engine would finish in <1s. Add search_move_timed() to AlphaBeta that uses iterative deepening under a time constraint: searches depth 1, 2, 3, ... until time runs out, keeping the best move from the last completed depth. Time is checked every 512 nodes to minimize overshoot. Update the UCI handler to parse go parameters (wtime, btime, winc, binc, movetime, movestogo, depth) and calculate appropriate time allocation per move with 1-second safety margins for Python overhead.
0eb5350 to
13f83ec
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
search_move_timed()to AlphaBeta engine: iterative deepening search (depth 1, 2, 3, ...) under a time constraint, keeping the best move from the last completed depthgoparameters:wtime,btime,winc,binc,movetime,movestogo,depthBefore: Engine searched to fixed depth 3 in <1s regardless of available time (e.g., 60s per move)
After: Engine uses nearly all available time, searching to much deeper depths
Stockfish benchmark (vs skill 3, 10s/move, 20 games)
All 8 losses were time forfeitures at the tight 10s/move time control. With the CI benchmark's 60s/move, time margins are 6x larger and timing issues should be minimal.
Local Stockfish Benchmark
Settings: 20 games, Stockfish skill 3, 10s/move, no opening book.
Note: 10 of the 12 wins were checkmate wins, and 0 of the 8 losses were chess losses -- all 8 losses were time forfeitures. This PR uses iterative deepening with time management, so the engine plays stronger chess but is sensitive to tight time controls. With longer time controls (e.g., 60s/move in CI), time forfeitures should be minimal.
Use
/run-stockfish-benchmarkfor CI validation with opening book and longer time control.Test plan
go movetime 5000→ engine uses ~4.8s ✓go wtime 60000 btime 60000→ correct time allocation ✓go movetime 60000→ engine uses ~50s ✓/run-stockfish-benchmarkfor CI validation