Skip to content

Commit a2db1ac

Browse files
authored
Allow the user to optionally specify a movetime for get_wdl_stats. (zhelyabuzhsky#139)
* Allow the user to optionally specify a movetime for `get_wdl_stats`. * Explain the arguments below the example.
1 parent 7e5cdba commit a2db1ac

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,13 +265,17 @@ Before calling this function, it is recommended that you first check if your ver
265265
use the "does_current_engine_version_have_wdl_option()" function below.
266266

267267
```python
268-
stockfish.get_wdl_stats() # include the argument get_as_tuple=True if you'd like to have a tuple returned instead of a list.
268+
stockfish.get_wdl_stats()
269269
```
270270

271271
```text
272272
[87, 894, 19]
273273
```
274274

275+
Optional arguments:
276+
- `get_as_tuple`: if you'd like to have a tuple returned instead of a list.
277+
- `time`: if you'd like to constrain the search by a duration rather than the current depth.
278+
275279
### Find if your version of Stockfish is recent enough to display WDL stats
276280

277281
```python

stockfish/models.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ def get_best_move(
542542
self, wtime: int | None = None, btime: int | None = None
543543
) -> str | None:
544544
"""Returns the best move in the current position on the board.
545-
`wtime` and `btime` arguments influence the search only if provided.
545+
If both `wtime` and `btime` aren't provided, the current depth is used for the search.
546546
547547
Args:
548548
wtime:
@@ -686,13 +686,16 @@ def is_move_correct(self, move_value: str) -> bool:
686686
return move_value in self.get_perft(1)[1]
687687

688688
def get_wdl_stats(
689-
self, get_as_tuple: bool = False
689+
self, get_as_tuple: bool = False, time: int | None = None
690690
) -> list[int] | tuple[int, int, int] | None:
691691
"""Returns Stockfish's win/draw/loss stats for the side to move.
692692
693693
Args:
694694
get_as_tuple:
695695
Option to return the wdl stats as a tuple instead of a list. Default is `False`.
696+
time:
697+
Time for Stockfish to search (milliseconds). If provided, will be used instead of the
698+
current depth.
696699
697700
Returns:
698701
A list or tuple of three integers, unless the game is over (in which case
@@ -709,18 +712,17 @@ def get_wdl_stats(
709712
+ """ get_wdl_stats will still return full strength Stockfish's wdl stats of the position."""
710713
)
711714

712-
self._go()
715+
if time is None:
716+
self._go()
717+
else:
718+
self._go_time(time)
713719
lines = self._get_sf_go_command_output()
714720
if lines[-1].startswith("bestmove (none)"):
715721
return None
716722
split_line = [line.split(" ") for line in lines if " multipv 1 " in line][-1]
717723
wdl_index = split_line.index("wdl")
718-
719724
wdl_stats = [int(split_line[i]) for i in range(wdl_index + 1, wdl_index + 4)]
720-
721-
if get_as_tuple:
722-
return (wdl_stats[0], wdl_stats[1], wdl_stats[2])
723-
return wdl_stats
725+
return (wdl_stats[0], wdl_stats[1], wdl_stats[2]) if get_as_tuple else wdl_stats
724726

725727
def does_current_engine_version_have_wdl_option(self) -> bool:
726728
"""Returns whether the user's version of Stockfish has the option to display WDL stats."""

tests/stockfish/test_models.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def test_get_best_move_first_move(self, stockfish: Stockfish):
5555
best_move = stockfish.get_best_move()
5656
assert best_move in ("e2e3", "e2e4", "g1f3", "b1c3", "d2d4")
5757

58+
@pytest.mark.slow
5859
def test_get_best_move_time_first_move(self, stockfish: Stockfish):
5960
best_move = stockfish.get_best_move_time(1000)
6061
assert best_move in ("e2e3", "e2e4", "g1f3", "b1c3", "d2d4")
@@ -83,7 +84,8 @@ def test_info_raises_error_by_default(self, stockfish: Stockfish):
8384
stockfish.get_evaluation()
8485
stockfish.get_perft(1)
8586
stockfish.get_top_moves(1)
86-
stockfish.get_wdl_stats()
87+
if stockfish.does_current_engine_version_have_wdl_option():
88+
stockfish.get_wdl_stats()
8789
with pytest.raises(RuntimeError):
8890
stockfish.info()
8991
stockfish.get_best_move_time(1)
@@ -94,6 +96,7 @@ def test_get_best_move_not_first_move(self, stockfish: Stockfish):
9496
best_move = stockfish.get_best_move()
9597
assert best_move in ("d2d4", "g1f3")
9698

99+
@pytest.mark.slow
97100
def test_get_best_move_time_not_first_move(self, stockfish: Stockfish):
98101
stockfish.make_moves_from_start(["e2e4", "e7e6"])
99102
best_move = stockfish.get_best_move_time(1000)
@@ -935,6 +938,15 @@ def test_get_wdl_stats(self, stockfish: Stockfish):
935938
with pytest.raises(RuntimeError):
936939
stockfish.get_wdl_stats()
937940

941+
@pytest.mark.slow
942+
def test_get_wdl_stats_movetime(self, stockfish: Stockfish):
943+
if not stockfish.does_current_engine_version_have_wdl_option():
944+
return
945+
start = time.time()
946+
wdl_stats = stockfish.get_wdl_stats(get_as_tuple=True, time=1000)
947+
assert 0.5 < time.time() - start < 1.5
948+
assert wdl_stats and wdl_stats[1] > wdl_stats[0] > wdl_stats[2]
949+
938950
def test_does_current_engine_version_have_wdl_option(self, stockfish: Stockfish):
939951
if stockfish.get_stockfish_major_version() <= 11:
940952
assert not stockfish.does_current_engine_version_have_wdl_option()

0 commit comments

Comments
 (0)