From 8a5ebdc7f4a243a02cbf6f55c416dcab086592a0 Mon Sep 17 00:00:00 2001 From: knutole Date: Thu, 29 Sep 2022 13:30:31 +0200 Subject: [PATCH 1/3] Add include_info=True flag to .get_top_moves() lint with black add dict type more elaborate typing type type this ignore type fixed Separete moves, info in Tuple if include_info=True lint with black add test revert; add test fix test cleanup --- stockfish/models.py | 64 ++++++++++++++++++++++++++-------- tests/stockfish/test_models.py | 25 +++++++++++++ 2 files changed, 74 insertions(+), 15 deletions(-) diff --git a/stockfish/models.py b/stockfish/models.py index b90a3d55..22e80a4d 100644 --- a/stockfish/models.py +++ b/stockfish/models.py @@ -514,7 +514,9 @@ def get_evaluation(self) -> dict: elif splitted_text[0] == "bestmove": return evaluation - def get_top_moves(self, num_top_moves: int = 5) -> List[dict]: + def get_top_moves( + self, num_top_moves: int = 5, include_info: bool = False + ) -> List[dict]: """Returns info on the top moves in the position. Args: @@ -522,10 +524,18 @@ def get_top_moves(self, num_top_moves: int = 5) -> List[dict]: The number of moves to return info on, assuming there are at least those many legal moves. + include_info: + Option to include the full info from the engine in the returned dictionary, + including seldepth, multipv, time, nodes, nps, and wdl if available. + Boolean. Default is False. + Returns: A list of dictionaries. In each dictionary, there are keys for Move, Centipawn, and Mate; the corresponding value for either the Centipawn or Mate key will be None. If there are no moves in the position, an empty list is returned. + + If include_info is True, the dictionary will also include the keys SelectiveDepth, Time, + Nodes, N/s, MultiPVLine, and WDL (if available). WDL is set from the White player's perspective. """ if num_top_moves <= 0: @@ -562,20 +572,44 @@ def get_top_moves(self, num_top_moves: int = 5) -> List[dict]: raise RuntimeError( "Having a centipawn value and mate value should be mutually exclusive." ) - top_moves.insert( - 0, - { - "Move": current_line[current_line.index("pv") + 1], - "Centipawn": int(current_line[current_line.index("cp") + 1]) - * multiplier - if has_centipawn_value - else None, - "Mate": int(current_line[current_line.index("mate") + 1]) - * multiplier - if has_mate_value - else None, - }, - ) + move_evaluation = { + "Move": current_line[current_line.index("pv") + 1], + "Centipawn": int(current_line[current_line.index("cp") + 1]) + * multiplier + if has_centipawn_value + else None, + "Mate": int(current_line[current_line.index("mate") + 1]) + * multiplier + if has_mate_value + else None, + } + if include_info == True: + move_evaluation.update( + { + "Nodes": current_line[current_line.index("nodes") + 1], + "N/s": current_line[current_line.index("nps") + 1], + "Time": current_line[current_line.index("time") + 1], + "SelectiveDepth": current_line[ + current_line.index("seldepth") + 1 + ], + "MultiPVLine": current_line[ + current_line.index("multipv") + 1 + ], + } + ) + if self.does_current_engine_version_have_wdl_option(): + move_evaluation.update( + { + "WDL": " ".join( + [ + current_line[current_line.index("wdl") + 1], + current_line[current_line.index("wdl") + 2], + current_line[current_line.index("wdl") + 3], + ][::multiplier] + ) + } + ) + top_moves.insert(0, move_evaluation) else: break if old_MultiPV_value != self._parameters["MultiPV"]: diff --git a/tests/stockfish/test_models.py b/tests/stockfish/test_models.py index 45269a44..a77082fc 100644 --- a/tests/stockfish/test_models.py +++ b/tests/stockfish/test_models.py @@ -571,6 +571,31 @@ def test_get_top_moves_mate(self, stockfish): assert stockfish.get_top_moves() == [] assert stockfish.get_parameters()["MultiPV"] == 3 + def test_get_top_moves_with_info(self, stockfish): + stockfish.set_depth(15) + stockfish._set_option("MultiPV", 4) + stockfish.set_fen_position("1rQ1r1k1/5ppp/8/8/1R6/8/2r2PPP/4R1K1 w - - 0 1") + assert stockfish.get_top_moves(2, include_info=False) == [ + {"Move": "e1e8", "Centipawn": None, "Mate": 1}, + {"Move": "c8e8", "Centipawn": None, "Mate": 2}, + ] + moves = stockfish.get_top_moves(2, include_info=True) + assert all( + k in moves[0] + for k in ( + "Move", + "Centipawn", + "Mate", + "MultiPVLine", + "N/s", + "Nodes", + "SelectiveDepth", + "Time", + ) + ) + if stockfish.does_current_engine_version_have_wdl_option(): + assert "WDL" in moves[0] + def test_get_top_moves_raising_error(self, stockfish): stockfish.set_fen_position( "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" From c2014345b99bd2521542be85023751cc9625119c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Ole=20Sj=C3=B8li?= Date: Wed, 5 Apr 2023 00:17:32 +0200 Subject: [PATCH 2/3] Update stockfish/models.py Add suggested cleanup Co-authored-by: kieferro <81773954+kieferro@users.noreply.github.com> --- stockfish/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stockfish/models.py b/stockfish/models.py index 22e80a4d..f2e4e438 100644 --- a/stockfish/models.py +++ b/stockfish/models.py @@ -583,7 +583,7 @@ def get_top_moves( if has_mate_value else None, } - if include_info == True: + if include_info: move_evaluation.update( { "Nodes": current_line[current_line.index("nodes") + 1], From 0906d2e63d12a6ae7e0d3e0aaa7a287511fa7a89 Mon Sep 17 00:00:00 2001 From: knutole Date: Wed, 5 Apr 2023 07:34:02 +0200 Subject: [PATCH 3/3] Update readme with include_info flag --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ef541046..18084bbb 100644 --- a/README.md +++ b/README.md @@ -130,8 +130,9 @@ True ### Get info on the top n moves ```python -stockfish.get_top_moves(3) +stockfish.get_top_moves(3, include_info=False) ``` +Optional parameter `include_info` specifies whether to include the full info from the engine in the returned dictionary, including seldepth, multipv, time, nodes, nps, and wdl if available. Boolean. Default is `False`. ```text [ {'Move': 'f5h7', 'Centipawn': None, 'Mate': 1},