Skip to content

Commit c461118

Browse files
committed
Add sf16 WDL model
1 parent 67aa18e commit c461118

2 files changed

Lines changed: 16 additions & 3 deletions

File tree

chess/engine.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444

4545
try:
4646
from typing import Literal
47-
_WdlModel = Literal["sf", "sf15.1", "sf15", "sf14", "sf12", "lichess"]
47+
_WdlModel = Literal["sf", "sf16", "sf15.1", "sf15", "sf14", "sf12", "lichess"]
4848
except ImportError:
4949
# Before Python 3.8.
5050
_WdlModel = str # type: ignore
@@ -637,11 +637,20 @@ def __ge__(self, other: object) -> bool:
637637
else:
638638
return NotImplemented
639639

640+
def _sf16_wins(cp: int, *, ply: int) -> int:
641+
# https://github.com/official-stockfish/Stockfish/blob/sf_16/src/uci.h#L38
642+
NormalizeToPawnValue = 328
643+
# https://github.com/official-stockfish/Stockfish/blob/sf_16/src/uci.cpp#L200-L224
644+
m = min(240, max(ply, 0)) / 64
645+
a = (((0.38036525 * m + -2.82015070) * m + 23.17882135) * m) + 307.36768407
646+
b = (((-2.29434733 * m + 13.27689788) * m + -14.26828904) * m) + 63.45318330
647+
x = min(4000, max(cp * NormalizeToPawnValue / 100, -4000))
648+
return int(0.5 + 1000 / (1 + math.exp((a - x) / b)))
640649

641650
def _sf15_1_wins(cp: int, *, ply: int) -> int:
642-
# https://github.com/official-stockfish/Stockfish/blob/sf_15.1/src/uci.cpp#L200-L224
643651
# https://github.com/official-stockfish/Stockfish/blob/sf_15.1/src/uci.h#L38
644652
NormalizeToPawnValue = 361
653+
# https://github.com/official-stockfish/Stockfish/blob/sf_15.1/src/uci.cpp#L200-L224
645654
m = min(240, max(ply, 0)) / 64
646655
a = (((-0.58270499 * m + 2.68512549) * m + 15.24638015) * m) + 344.49745382
647656
b = (((-2.65734562 * m + 15.96509799) * m + -20.69040836) * m) + 73.61029937
@@ -703,9 +712,12 @@ def wdl(self, *, model: _WdlModel = "sf", ply: int = 30) -> Wdl:
703712
elif model == "sf15":
704713
wins = _sf15_wins(self.cp, ply=ply)
705714
losses = _sf15_wins(-self.cp, ply=ply)
706-
else:
715+
elif model == "sf15.1":
707716
wins = _sf15_1_wins(self.cp, ply=ply)
708717
losses = _sf15_1_wins(-self.cp, ply=ply)
718+
else:
719+
wins = _sf16_wins(self.cp, ply=ply)
720+
losses = _sf16_wins(-self.cp, ply=ply)
709721
draws = 1000 - wins - losses
710722
return Wdl(wins, draws, losses)
711723

test.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,6 +3002,7 @@ def test_wdl_model(self):
30023002
self.assertEqual(chess.engine.Cp(146).wdl(model="sf14", ply=25), chess.engine.Wdl(601, 398, 1))
30033003
self.assertEqual(chess.engine.Cp(40).wdl(model="sf15", ply=25), chess.engine.Wdl(58, 937, 5))
30043004
self.assertEqual(chess.engine.Cp(100).wdl(model="sf15.1", ply=64), chess.engine.Wdl(497, 503, 0))
3005+
self.assertEqual(chess.engine.Cp(-52).wdl(model="sf16", ply=63), chess.engine.Wdl(0, 932, 68))
30053006

30063007
@catchAndSkip(FileNotFoundError, "need stockfish")
30073008
def test_sf_forced_mates(self):

0 commit comments

Comments
 (0)