Answers.Body: <p>It is not too difficult to do this with a python script:
  You basically start the engine as a subprocess and communicate via "uci".
  I'll post some not very pretty code snippets and try to explain what you have to do:</p>
  
  <pre><code>engine=subprocess.Popen('./stockfish-5-        64',universal_newlines=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,)
  
  def put(command):
      print command
      engine.stdin.write(command+'\n')
  
      if command!='quit':
          engine.stdin.write('isready\n')
          while(True):
              text=engine.stdout.readline().strip()   
              print
              print text
              print
              if text == 'readyok':
                  return
  
  def get():
      engine.stdin.write('isready\n')
      while(True):
          text=engine.stdout.readline().strip()
          print text
          if text == 'readyok':
              break
          if text !='':
              print text
  
  def getResult(depth):
      Result=[]
      while(True):
          text=engine.stdout.readline().strip()   
          liste=text.split()  
          if text[:22]=="info depth "+str(depth)+" seldepth" and liste[15]=='1':
              Result=[]
          if text[:22]=="info depth "+str(depth)+" seldepth":
              Result.append(text)
  
  
          if text[:8] == 'bestmove':
              break
  
      Scores=[]
      for line in Result:
          liste=line.split()
          Scores.append(liste[7])     
  
      return Scores
  
  def Game_Analysis(game):
      scorestring=''
      moves=''
      for x in range(len(game)-1):
          moves+=game[x]+' '
          command='position startpos moves '+moves
          put(command)    
          get()
          put('go depth 15')
          Scores=getResult(15)
          for score in Scores:
              scorestring+=score+' '
          scorestring+='\n'
      return scorestring
  
  
  get()
  put('uci')
  get()
  put('setoption name MultiPV value 20')      
  get()
  
  print Game_Analysis(game)
  
  put('stop')
  put('quit')
  </code></pre>
  
  <p>So, get() and put() are for communicating with the engine. </p>
  
  <p>put('uci') starts the dialog.</p>
  
  <p>put('setoption name MultiPV value 20') tells the engine to return the best twenty moves. (In your case you really only need 2)</p>
  
  <p>command='position startpos moves '+moves<br>
  put(command)  This sets up the position, unfortunately the moves aren't pgn but in uci-format. So you'll probably have to transcribe or use FEN.</p>
  
  <p>In GetResult() I extract the 20 best scores of the position. Then you only need to look for a big difference between move one and move two.</p>
  
  <p>Game_Analysis(game) extracts the 20 best scores for every position in a game, here given as a list of uci-formated moves.</p>
  
  <p>To deal with the uci-format and really understand these commands you should have a look at the uci=Universal Chess Interface.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-16T16:37:09.577Z
Answers.Text: this is fantastic, I will give it a go when I have a chance. to confirm - game (in print Game_Analysis(game)) is a string which is a game representation in UCI format? My input is in pgn, but I might be able to plug-in pgn-extract to get UCI out of it ... http://chess.stackexchange.com/questions/2895/how-to-convert-pgn-moves-to-long-algebraic-notation-in-python
Answers.UserDisplayName: Joe
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-16T18:14:40.583Z
Answers.Text: No, "game" is actually a list of moves. The moves are strings in the uci-format. So "moves+=game[x]+' '" adds another move to the string of moves that are executed from the starting position, to reach the position that is evaluated.
Answers.UserDisplayName: BlindKungFuMaster
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-12-15T10:46:21.210Z
Answers.LastActivityDate: 2014-12-16T10:06:56.530Z
Answers.LastEditDate: 2014-12-16T10:06:56.530Z
Answers.UserDisplayName: BlindKungFuMaster
Body: <p>Let's imagine I have a pgn file with each game starting from a tactical position, and I want to identify the games where the solution was the only one. Basically an engine would have to evaluate several possible moves, and the evaluation margin between the first best move and the second best move would have to be above a certain threshold (e.g. >3.00 pawns). I do not want to load up manually every game and look at the engine evaluation - that's the step I am hoping to eliminate.</p>
  
  <p>I am interested to hear of any GUI based solutions, or even script based (e.g. modifications to this - <a href="http://sourceforge.net/p/scid/mailman/message/29220017/" rel="nofollow">http://sourceforge.net/p/scid/mailman/message/29220017/</a> - I like this script, but can't get it up and running easily because SCID TCL interpreter on Windows seems to come without Expect package ...)</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-27T06:28:25.163Z
Comments.Text: Try contacting the guy who wrote the tactical server http://chesstempo.com through the chesstempo discussion forum. All the tactical puzzles there have been extracted from game databases through an algorithm that recognizes positions that have a tactical blow. It is possible that this solves the problem posed here. The guy is available for discussion or even the forum members may know the technical details.
Comments.UserDisplayName: MichalRyszardWojcik
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-28T08:16:57.430Z
Comments.Text: If you are doing this in order to find tactics exercises, please also note that a position where the best move is +20 and the second best move is +5 is not a very good one.
Comments.UserDisplayName: JiK
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-10-29T14:55:12.997Z
Comments.Text: Yes, finding tactical exercises is my goal. Good point though, so if I could put into a script - I would also cap it so that second best move is no  higher than 2.00 (i.e. also clearly winning).
Comments.UserDisplayName: Joe
Tags: <engines><software><pgn>
Title: What are the options to automatically identify games where the first move played was the best by a margin?
ThreadText: # What are the options to automatically identify games where the first move played was the best by a margin?
  
  (Asked by: username_0 on 2014-10-26)
  
  Let's imagine I have a pgn file with each game starting from a tactical position, and I want to identify the games where the solution was the only one. Basically an engine would have to evaluate several possible moves, and the evaluation margin between the first best move and the second best move would have to be above a certain threshold (e.g. >3.00 pawns). I do not want to load up manually every game and look at the engine evaluation - that's the step I am hoping to eliminate.
  
  I am interested to hear of any GUI based solutions, or even script based (e.g. modifications to this - <http://sourceforge.net/p/scid/mailman/message/29220017/> - I like this script, but can't get it up and running easily because SCID TCL interpreter on Windows seems to come without Expect package ...)
  
  **Tags:** engines, software, pgn
  
  > **Comments:**
  > **username_1**: Try contacting the guy who wrote the tactical server http://chesstempo.com through the chesstempo discussion forum. All the tactical puzzles there have been extracted from game databases through an algorithm that recognizes positions that have a tactical blow. It is possible that this solves the problem posed here. The guy is available for discussion or even the forum members may know the technical details.  
  > **username_2**: If you are doing this in order to find tactics exercises, please also note that a position where the best move is +20 and the second best move is +5 is not a very good one.  
  > **username_0**: Yes, finding tactical exercises is my goal. Good point though, so if I could put into a script - I would also cap it so that second best move is no  higher than 2.00 (i.e. also clearly winning).  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_3 on 2014-12-15. Score: 2 | Accepted answer
  
  It is not too difficult to do this with a python script:
  You basically start the engine as a subprocess and communicate via "uci".
  I'll post some not very pretty code snippets and try to explain what you have to do:
  engine=subprocess.Popen('./stockfish-5-        64',universal_newlines=True,stdin=subprocess.PIPE,stdout=subprocess.PIPE,)
  
  def put(command):
      print command
      engine.stdin.write(command+'\n')
  
      if command!='quit':
          engine.stdin.write('isready\n')
          while(True):
              text=engine.stdout.readline().strip()   
              print
              print text
              print
              if text == 'readyok':
                  return
  
  def get():
      engine.stdin.write('isready\n')
      while(True):
          text=engine.stdout.readline().strip()
          print text
          if text == 'readyok':
              break
          if text !='':
              print text
  
  def getResult(depth):
      Result=[]
      while(True):
          text=engine.stdout.readline().strip()   
          liste=text.split()  
          if text[:22]=="info depth "+str(depth)+" seldepth" and liste[15]=='1':
              Result=[]
          if text[:22]=="info depth "+str(depth)+" seldepth":
              Result.append(text)
  
  
          if text[:8] == 'bestmove':
              break
  
      Scores=[]
      for line in Result:
          liste=line.split()
          Scores.append(liste[7])     
  
      return Scores
  
  def Game_Analysis(game):
      scorestring=''
      moves=''
      for x in range(len(game)-1):
          moves+=game[x]+' '
          command='position startpos moves '+moves
          put(command)    
          get()
          put('go depth 15')
          Scores=getResult(15)
          for score in Scores:
              scorestring+=score+' '
          scorestring+='\n'
      return scorestring
  
  
  get()
  put('uci')
  get()
  put('setoption name MultiPV value 20')      
  get()
  
  print Game_Analysis(game)
  
  put('stop')
  put('quit')
  
  So, get() and put() are for communicating with the engine. 
  put('uci') starts the dialog.
  put('setoption name MultiPV value 20') tells the engine to return the best twenty moves. (In your case you really only need 2)
  command='position startpos moves '+moves
  put(command)  This sets up the position, unfortunately the moves aren't pgn but in uci-format. So you'll probably have to transcribe or use FEN.
  In GetResult() I extract the 20 best scores of the position. Then you only need to look for a big difference between move one and move two.
  Game_Analysis(game) extracts the 20 best scores for every position in a game, here given as a list of uci-formated moves.
  To deal with the uci-format and really understand these commands you should have a look at the uci=Universal Chess Interface.
  
  > **Comments:**
  > **username_0**: this is fantastic, I will give it a go when I have a chance. to confirm - game (in print Game_Analysis(game)) is a string which is a game representation in UCI format? My input is in pgn, but I might be able to plug-in pgn-extract to get UCI out of it ... http://chess.stackexchange.com/questions/2895/how-to-convert-pgn-moves-to-long-algebraic-notation-in-python  
  > **username_3**: No, "game" is actually a list of moves. The moves are strings in the uci-format. So "moves+=game[x]+' '" adds another move to the string of moves that are executed from the starting position, to reach the position that is evaluated.  
  
  ---
  
  