Advertisement
Guest User

Untitled

a guest
May 28th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import argparse, subprocess, sys
  2.  
  3. import chess
  4.  
  5. parser = argparse.ArgumentParser()
  6. parser.add_argument("-E", "--engine", help="Name of Engine", required=True, default="")
  7. parser.add_argument("-S", "--suite", help="Name of Suite", required=True, default="")
  8. parser.add_argument("-T", "--time", help="Time per Position in seconds", required=True, default="")
  9. parser.add_argument("-C", "--cores", help="Cores for the Engine", required=False, default=1)
  10. parser.add_argument("-H", "--hash", help="Table Size in MB", required=False, default=256)
  11. argument = parser.parse_args()
  12.  
  13. logfile = "{0}-{1}-{2}-{3}-{4}.log".format(
  14. argument.engine,
  15. argument.suite,
  16. argument.time,
  17. argument.cores,
  18. argument.hash
  19. )
  20.  
  21. with open(logfile, "w") as fout:
  22. fout.write("ENGINE : {0}\n".format(argument.engine))
  23. fout.write("SUITE : {0}\n".format(argument.suite ))
  24. fout.write("TIME : {0}\n".format(argument.time ))
  25. fout.write("CORES : {0}\n".format(argument.cores ))
  26. fout.write("HASH : {0}\n".format(argument.hash ))
  27.  
  28. engine = subprocess.Popen(
  29. argument.engine,
  30. stderr=subprocess.STDOUT,
  31. stdin=subprocess.PIPE,
  32. stdout=subprocess.PIPE,
  33. universal_newlines=True
  34. )
  35.  
  36. engine.stdin.write("setoption name Threads value {0}\n".format(argument.cores))
  37. engine.stdin.write("setoption name Hash value {0}\n".format(argument.hash))
  38. engine.stdin.flush()
  39.  
  40. with open(argument.suite, "r") as fin:
  41. data = fin.readlines()
  42. data = [f.split(";")[0] for f in data]
  43.  
  44. score = 0
  45. for i, position in enumerate(data):
  46.  
  47. if "am" in position:
  48. fen = position.split("am")[0]
  49. move = position.split("am ")[1].split(" ")[0].strip()
  50. good = False
  51.  
  52. else:
  53. fen = position.split("bm")[0]
  54. move = position.split("bm ")[1].split(" ")[0].strip()
  55. good = True
  56.  
  57. move = chess.Board(fen + "0 0").parse_san(move)
  58.  
  59. engine.stdin.write("ucinewgame\n")
  60. engine.stdin.write("position fen {0}\n".format(fen + "0 0"))
  61. engine.stdin.write("go movetime {0}\n".format(float(argument.time) * 1000))
  62. engine.stdin.flush()
  63.  
  64. outputs = []
  65. while True:
  66. line = engine.stdout.readline().strip()
  67. outputs.append(line)
  68. if "bestmove" in line:
  69. best = line.split(" ")[1]
  70. best = chess.Board(fen + "0 0").parse_uci(best)
  71. break
  72.  
  73. with open(logfile, "a") as fout:
  74. fout.write("Position #%d FEN : %s\n" % (i, fen))
  75. for line in outputs:
  76. fout.write(line + "\n")
  77.  
  78. scoreLine = None
  79. for line in outputs:
  80. if "score mate" in line or "score cp" in line:
  81. scoreLine = line
  82.  
  83. if "mate" in scoreLine: _eval = "M" + scoreLine.split("score mate ")[1].split(" ")[0]
  84. elif "cp" in scoreLine: _eval = scoreLine.split("score cp ")[1].split(" ")[0]
  85. else : _eval = "NONE"
  86.  
  87. if good:
  88. print("{0:>3} : FEN={1:>70} BEST={2} GIVEN={3} EVAL={4:>5}".format(i, fen, move, best, _eval))
  89. score += move == best
  90. else:
  91. print("{0:>3} : FEN={1:>70} ANTI={2} GIVEN={3} EVAL={4:>5}".format(i, fen, move, best, _eval))
  92. score += move != best
  93.  
  94. sys.stdout.flush()
  95.  
  96. engine.stdin.write("quit\n")
  97. engine.stdin.flush()
  98. engine.wait()
  99.  
  100. with open(logfile, "a") as fout:
  101. fout.write("Score [%3d / %3d]" % (score, len(data)))
  102. print ("Score [%3d / %3d]" % (score, len(data)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement