Python-Puzzle-Creator
Python-Puzzle-Creator copied to clipboard
Use MultiPV:2 instead of evaluating all legal moves
While adapting this project to work locally, I came across a solution to speed up the process all together.
When investigating a puzzle position, evaluate_legals() made a list of all legal moves, played them on the board, and got the engine analysis of that position. It then sorted all the analyzed positions by evaluation, and checked for ambiguity within moves (only one good move should be in a puzzle). However, only the first two moves in the sorted analysed_legals were used to check ambiguity in the puzzle. Meaning many more moves were evaluated at 6 million nodes, at each step during the puzzle investigation. Furthermore, those moves were played onto the board for the engine to evaluate, taking up more computation time and space.
Using a MultiPV value of 2, Stockfish is able to provide us with it's top two picks for moves using only one go command. Since this evaluation is of the current position before the move, as opposed to the move being made and the position after being evaluated, all of the inequality signs had to be flipped, and numbers negated in ambiguous(). With some testing I found out simply multiple the nodes in evaluate_legals() by 2 was enough to make sure both PVs were analyzed by stockfish with the same depth as individually. (On higher MultiPVs the nodes are split amongst the PVs, so without increasing nodes the analysis would be less accurate)
I've tested these changes on my locally adapted version (found on the master branch of https://github.com/forhavu/Python-Puzzle-Creator/) and found around a 400% speed increase on my machine, and the same puzzles were generated from the same pgn as before, so the result is equivalent.
I don't know if this program will even be used any more, but I thought I'd share the speed increase in the case it does ever get used again by lichess :smile: