3

In python-chess you can communicate with the engine on a given position. However the method presented in https://python-chess.readthedocs.io/en/latest/uci.html using the engine.go() function with only a time parameter returns just one continuation and that continuations evaluation.

I know in most chess programs you can see the top few ranked positions and their evaluations. How can I do the same in python chess? None of the parameters in engine.go seemed to let me do that.

Rewan Demontay
  • 17,514
  • 4
  • 67
  • 113
Qwertford
  • 241
  • 2
  • 4

2 Answers2

4

You can do this by sending out MultiPV, something like:

engine = chess.uci.popen_engine(...)
engine.setoption({ "MultiPV" : multiPV})

The module will generate multiple lines for you, as an array.

11684
  • 1,296
  • 11
  • 23
SmallChess
  • 22,476
  • 2
  • 45
  • 82
  • 1
    Short and simple! Gotta say, love how you and Phonon (like this recent stuff) among others are constantly providing code snippets in these answers, to encourage people to do a bit of scripting/programming on their own, and relying less on commercial-closed-source products. – user929304 Apr 11 '19 at 10:37
2

As the version of python-chess I'm running doesn't support chess.uci, here's what worked for me.

engine = chess.engine.SimpleEngine.popen_uci("/path/to/engine.exe")
board = chess.Board()
info = engine.analyse(board, chess.engine.Limit(time=10),multipv=20))

info is a list of InfoDict objects, each with the keys "pv" and "score", the list of moves in the line, and the evaluation respectively.