Port safe-heuristic bots to Cython with Python reference fallback

Cython implementation in heuristic_cy.pyx achieves ~2.55× speedup on
opponent_act_seconds (200-game eval: 59.20s → 23.24s). Original Python
implementation preserved verbatim in heuristic_py.py as the equivalence
reference. Action-sequence equivalence is verified by
test_safe_heuristic_equivalence.py against seeded game corpora.

Key implementation notes:
- File-local wraparound=True override required for negative discard
  indexing; Cython global wraparound=False would segfault.
- annotation_typing=False preserves verbatim Python semantics.
- _CachedState materializes hands/expeditions/discards/deck once per
  act() call — this is the dominant performance win.

Further C-array optimization of _card_value_for_me /
_card_value_for_opponent / _color_commitment / _bonus_potential is
deferred. The current 2.55× delivers most of the dense-eval future
benefit; further work is gated on actually adopting denser eval
schedules (eval_every=5, games=1000).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-07 20:03:53 +09:00
co-authored by Claude Opus 4.7
parent 4014e49168
commit 05de0e2a81
5 changed files with 2432 additions and 1156 deletions
+4
View File
@@ -26,6 +26,10 @@ extensions = cythonize(
"coolrl_lost_cities.games.classic.deep_cfr.traversal",
["src/coolrl_lost_cities/games/classic/deep_cfr/traversal.pyx"],
),
Extension(
"coolrl_lost_cities.games.classic.bots.heuristic_cy",
["src/coolrl_lost_cities/games/classic/bots/heuristic_cy.pyx"],
),
],
language_level=3,
compiler_directives={