Skip to content

Add chess.chesstb: pure-Python prober for chesstb endgame tablebases#1194

Open
noobpwnftw wants to merge 1 commit into
niklasf:masterfrom
noobpwnftw:add-chesstb-tablebases
Open

Add chess.chesstb: pure-Python prober for chesstb endgame tablebases#1194
noobpwnftw wants to merge 1 commit into
niklasf:masterfrom
noobpwnftw:add-chesstb-tablebases

Conversation

@noobpwnftw

Copy link
Copy Markdown

Add chess.chesstb: pure-Python prober for chesstb endgame tablebases

What this adds

A new module, chess.chesstb, that probes the chesstb endgame
tablebase format directly from chess.Board positions — in the same spirit as
the existing chess.syzygy and chess.gaviota modules.

chesstb ships three table types per material:

Table Extension Answer
WDL .lzw 50-move-rule-aware win/draw/loss with cursed/blessed classes
DTC .lzdtc distance-to-conversion (plies to the next zeroing move)
DTM50 .lzdtm50 one pack giving both the unbounded DTM and the exact 50MR DTM at any halfmove clock

API

import chess, chess.chesstb

with chess.chesstb.open_tablebase("/path/to/chesstb") as tb:
    board = chess.Board("8/8/8/5k2/8/8/1Q6/K7 w - - 0 1")
    tb.probe_wdl(board)        # 2  (+2 win .. -2 loss, like syzygy)
    tb.probe_dtz(board)        # 19 (signed distance-to-conversion)
    tb.probe_dtm(board)        # 19 (signed distance-to-mate, ignoring 50MR)
    tb.probe_dtm50(board)      # (2, 19): rule-true (wdl, plies) at the board's clock

get_wdl / get_dtz / get_dtm are non-raising variants returning a default
(None) when no table is available. probe(board, rule50=0) returns the full
structured result (all fields at once).

Why pure Python

chess.syzygy is pure Python; this follows suit. The module depends only on
python-chess and the standard library:

  • LZMA (DTC / DTM50 blocks) via stdlib lzma with FORMAT_RAW (the C++
    side uses the LZMA SDK with props appended at each block tail).
  • LZ4 (WDL blocks) via a small bundled pure-Python LZ4-block decoder
    (~30 lines) supporting the optional LZ4 dictionary. No new dependency.

The position index (symmetry canonicalization, king/pawn slice managers, the
binomial piece-group ranking, the radix-composed board index and the
index-permutation layout) and the probe orchestration (dropped-frame one-ply
minimax reconstruction for shrunk files, the en-passant overlay, and the DTM50
halfmove-clock layer selection) are faithful re-implementations of the C++
src/probe library. Square numbering already matches python-chess exactly
(a1=0 … h8=63), so boards are consumed directly.

Validation

Every value is validated bit-for-bit against the reference C++ prober
(tests/probe_fen) by enumerating positions and comparing WDL, DTC/dtz, DTM
and DTM50 in lockstep:

  • All 145 shipped ≤5-man materials, ~72k positions at halfmove clock 0 —
    0 mismatches.
  • Layered DTM50 at halfmove clocks 1, 30, 40, 80, 98, 99 across all
    materials (exercising the CONST/SINGLE/DOUBLE/MULTI changepoint state machine,
    the draw-end hint, and recover_mate_at_hmc) — 0 mismatches.
  • Dropped-frame reconstruction: both the symmetric-mirror path (e.g. KRKR)
    and the asymmetric one-ply-minimax derive (109 of 145 materials ship a dropped
    frame) — 0 mismatches.
  • En-passant overlay and color-mirrored material (stronger side is
    Black) — verified against the oracle.
  • The pure-Python LZ4 decoder is cross-checked block-for-block against the
    reference lz4 C library on every shipped WDL table.

A self-contained test_chesstb.py pins representative expected values (no C++
build required) and skips when no tables are present.

Scope / future work

  • probe_root_* (Fathom-style root move ranking) is not yet ported.
  • The standalone .lzdtm table is intentionally not read: DTM is served from
    the .lzdtm50 pack (layer 0), which supersedes it.
  • Performance is pure-Python (block-cached); a future pass could add mmap and
    a native-accelerated LZ4 path. Correctness, not speed, is the goal here.

chesstb (https://github.com/noobpwnftw/chesstb) ships three table types per
material: 50-move-rule-aware WDL (with cursed/blessed classes), DTC
(distance-to-conversion), and a DTM50 pack that answers both the unbounded DTM
and the exact 50MR DTM at any halfmove clock.

This adds chess/chesstb.py, a faithful pure-Python re-implementation of the
reference C++ prober (no native extension; depends only on the standard
library's lzma plus a small bundled LZ4-block decoder). It covers the position
index (symmetry canonicalization, king/pawn slice managers, binomial
piece-group ranking, index-permutation layout), all three decoders, and the
probe orchestration (dropped-frame one-ply-minimax reconstruction for shrunk
files, the en-passant overlay, and DTM50 halfmove-clock layer selection).

API mirrors chess.syzygy: open_tablebase(); probe_wdl/probe_dtz/probe_dtm
return signed values; probe_dtm50 returns (wdl, plies); get_* are non-raising.

Validated bit-for-bit against the reference C++ prober across all 145 shipped
<=5-man materials and every halfmove-clock layer. A small fixture set
(data/chesstb) drives ChesstbTestCase.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant