Answers.Body: <p>In the world of chess engines, what you're looking for is called <strong>multi-PV</strong> (multiple principal variations): telling the engine to look for and return up to <em>n</em> best moves and the associated principal variations for each. ("Up to" because sometimes there's only 1 legal move in a position.)</p>
  
  <p>In python-chess, this is part of the <a href="https://python-chess.readthedocs.io/en/latest/engine.html" rel="nofollow noreferrer">engine protocols</a>, as one of the optional parameters when calling <code>engine.analyse()</code>. From the documentation:</p>
  
  <blockquote>
    <p><code>class chess.engine.EngineProtocol</code></p>
    
    <p><code>coroutine analyse( [...] )</code></p>
    
    <ul>
    <li>multipv – Optional. Analyse multiple root moves. Will return a list of at most multipv dictionaries rather than just a single info dictionary.</li>
    </ul>
  </blockquote>
  
  <p>The <code>.analyse()</code> method tells the engine to analyse the position, returning a <a href="https://python-chess.readthedocs.io/en/latest/engine.html#chess.engine.PlayResult.info" rel="nofollow noreferrer">dictionary of information</a>, which includes among other things the moves of the principal variation. Specifying for instance <code>.analyse(multipv = 3)</code> will tell the engine to return a list of (up to) 3 such dictionaries -- the first moves in the <code>pv</code> value of each dictionary should be what you are looking for.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-04T18:45:04.750Z
Answers.LastActivityDate: 2020-03-04T18:45:04.750Z
Answers.UserDisplayName: Remellion
Body: <p>Say that I currently have a board, and then on that board I move my e2 pawn to e4. I'm looking for a way to get the engine to predict the X amount of best moves for black.</p>
  
  <p>I know how to predict the best move given a position:</p>
  
  <pre><code>result = engine.play(board, chess.engine.Limit(time=0.1))
  board.push(result.move)
  </code></pre>
  
  <p>But that only predicts the number 1 best move. I want a list containing the second, third ... X best moves.</p>
  
  <p>How would I do that?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2020-03-04T05:32:54.220Z
Comments.Text: What you are asking for is known as multiPV. In the documentation for engine.analyse(), read about the multipv optional parameter. I would have posted this as an answer -- to the closers, note that asking an engine for the X best moves is **very different** from all moves, they are not even accomplished with the same technique, so please don't close so hastily questions that only *seem* similar.
Comments.UserDisplayName: Remellion
Tags: <engines><software><programming><python>
Title: PythonChess - Get engine to predict a list of the best moves in a position
ThreadText: # PythonChess - Get engine to predict a list of the best moves in a position
  
  (Asked by: username_0 on 2020-03-03)
  
  Say that I currently have a board, and then on that board I move my e2 pawn to e4. I'm looking for a way to get the engine to predict the X amount of best moves for black.
  I know how to predict the best move given a position:
  result = engine.play(board, chess.engine.Limit(time=0.1))
  board.push(result.move)
  
  But that only predicts the number 1 best move. I want a list containing the second, third ... X best moves.
  How would I do that?
  
  **Tags:** engines, software, programming, python
  
  > **Comments:**
  > **username_1**: What you are asking for is known as multiPV. In the documentation for engine.analyse(), read about the multipv optional parameter. I would have posted this as an answer -- to the closers, note that asking an engine for the X best moves is **very different** from all moves, they are not even accomplished with the same technique, so please don't close so hastily questions that only *seem* similar.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2020-03-04. Score: 1 | Accepted answer
  
  In the world of chess engines, what you're looking for is called **multi-PV** (multiple principal variations): telling the engine to look for and return up to *n* best moves and the associated principal variations for each. ("Up to" because sometimes there's only 1 legal move in a position.)
  
  In python-chess, this is part of the [engine protocols](https://python-chess.readthedocs.io/en/latest/engine.html), as one of the optional parameters when calling `engine.analyse()`. From the documentation:
  
  > `class chess.engine.EngineProtocol`
  >
  > `coroutine analyse( [...] )`
  >
  > * multipv – Optional. Analyse multiple root moves. Will return a list of at most multipv dictionaries rather than just a single info dictionary.
  
  The `.analyse()` method tells the engine to analyse the position, returning a [dictionary of information](https://python-chess.readthedocs.io/en/latest/engine.html#chess.engine.PlayResult.info), which includes among other things the moves of the principal variation. Specifying for instance `.analyse(multipv = 3)` will tell the engine to return a list of (up to) 3 such dictionaries -- the first moves in the `pv` value of each dictionary should be what you are looking for.
  
  
  ---
  
  