Skip to content

Commit 14f56f9

Browse files
committed
Add WDL model for Stockfish development version
1 parent 5a0e0da commit 14f56f9

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

chess/engine.py

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

4545
try:
4646
from typing import Literal
47-
_WdlModel = Literal["sf12", "lichess"]
47+
_WdlModel = Literal["sf14", "sf12", "lichess"]
4848
except ImportError:
4949
# Before Python 3.8.
5050
_WdlModel = str # type: ignore
@@ -559,6 +559,8 @@ def wdl(self, *, model: _WdlModel = "sf12", ply: int = 30) -> Wdl:
559559
0.015...
560560
561561
:param model:
562+
* ``sf14``, the WDL model used by the current development version
563+
of Stockfish. Still subject to change or removal.
562564
* ``sf12``, the WDL model used by Stockfish 12.
563565
* ``lichess``, the win rate model used by Lichess.
564566
Does not use *ply*, and does not consider drawing chances.
@@ -620,6 +622,15 @@ def __ge__(self, other: object) -> bool:
620622
return NotImplemented
621623

622624

625+
def _sf14_wins(cp: int, *, ply: int) -> int:
626+
# TODO: When released, add permalink and update documentation.
627+
m = min(240, max(ply, 0)) / 64
628+
a = (((-3.68389304 * m + 30.07065921) * m + -60.52878723) * m) + 149.53378557
629+
b = (((-2.01818570 * m + 15.85685038) * m + -29.83452023) * m) + 47.59078827
630+
pawn_value_eg = 208
631+
x = min(2000, max(cp * 100 / pawn_value_eg, -2000))
632+
return int(0.5 + 1000 / (1 + math.exp((a - x) / b)))
633+
623634
def _sf12_wins(cp: int, *, ply: int) -> int:
624635
# https://github.com/official-stockfish/Stockfish/blob/sf_12/src/uci.cpp#L198-L218
625636
m = min(240, max(ply, 0)) / 64
@@ -649,6 +660,9 @@ def wdl(self, *, model: _WdlModel = "sf12", ply: int = 30) -> Wdl:
649660
if model == "lichess":
650661
wins = _lichess_raw_wins(max(-1000, min(self.cp, 1000)))
651662
losses = 1000 - wins
663+
elif model == "sf14":
664+
wins = _sf14_wins(self.cp, ply=ply)
665+
losses = _sf14_wins(-self.cp, ply=ply)
652666
else:
653667
wins = _sf12_wins(self.cp, ply=ply)
654668
losses = _sf12_wins(-self.cp, ply=ply)

0 commit comments

Comments
 (0)