Spaces:
Sleeping
Sleeping
Dan Ravensloft commited on
Commit ·
5637bd2
1
Parent(s): fb07f16
Create EngineWrapper class
Browse files- engine_wrapper.py +94 -0
- main.py +9 -71
engine_wrapper.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chess
|
| 2 |
+
import chess.xboard
|
| 3 |
+
import chess.uci
|
| 4 |
+
|
| 5 |
+
class EngineWrapper:
|
| 6 |
+
|
| 7 |
+
def __init__(self, board, commands):
|
| 8 |
+
pass
|
| 9 |
+
|
| 10 |
+
def pre_game(self, game):
|
| 11 |
+
pass
|
| 12 |
+
|
| 13 |
+
def first_search(self, movetime):
|
| 14 |
+
pass
|
| 15 |
+
|
| 16 |
+
def search(self, board, wtime, btime, winc, binc):
|
| 17 |
+
pass
|
| 18 |
+
|
| 19 |
+
def print_stats(self):
|
| 20 |
+
pass
|
| 21 |
+
|
| 22 |
+
class XBoardEngine(EngineWrapper):
|
| 23 |
+
|
| 24 |
+
def __init__(self, board, commands):
|
| 25 |
+
commands = commands[0] if len(commands) == 1 else commands
|
| 26 |
+
self.engine = chess.xboard.popen_engine(commands)
|
| 27 |
+
|
| 28 |
+
self.engine.xboard()
|
| 29 |
+
self.engine.setboard(board)
|
| 30 |
+
|
| 31 |
+
post_handler = chess.xboard.PostHandler()
|
| 32 |
+
self.engine.post_handlers.append(post_handler)
|
| 33 |
+
|
| 34 |
+
def pre_game(self, game):
|
| 35 |
+
minutes = game.clock_initial / 1000 / 60
|
| 36 |
+
seconds = game.clock_initial / 1000 % 60
|
| 37 |
+
inc = game.clock_increment / 1000
|
| 38 |
+
self.engine.level(0, minutes, seconds, inc)
|
| 39 |
+
|
| 40 |
+
def first_search(self, board, movetime):
|
| 41 |
+
self.engine.setboard(board)
|
| 42 |
+
self.engine.st(movetime / 1000)
|
| 43 |
+
return self.engine.go()
|
| 44 |
+
|
| 45 |
+
def search(self, board, wtime, btime, winc, binc):
|
| 46 |
+
self.engine.setboard(board)
|
| 47 |
+
if board.turn == chess.WHITE:
|
| 48 |
+
self.engine.time(wtime / 10)
|
| 49 |
+
self.engine.otim(btime / 10)
|
| 50 |
+
else:
|
| 51 |
+
self.engine.time(btime / 10)
|
| 52 |
+
self.engine.otim(wtime / 10)
|
| 53 |
+
return self.engine.go()
|
| 54 |
+
|
| 55 |
+
def print_stats(self):
|
| 56 |
+
stats = ["depth", "nodes", "score"]
|
| 57 |
+
for stat in stats:
|
| 58 |
+
post_handler = self.engine.post_handlers[0]
|
| 59 |
+
if stat in post_handler.post:
|
| 60 |
+
print(" {}: {}".format(stat, post_handler.post[stat]))
|
| 61 |
+
|
| 62 |
+
class UCIEngine(EngineWrapper):
|
| 63 |
+
|
| 64 |
+
def __init__(self, board, commands):
|
| 65 |
+
commands = commands[0] if len(commands) == 1 else commands
|
| 66 |
+
self.engine = chess.uci.popen_engine(commands)
|
| 67 |
+
|
| 68 |
+
self.engine.uci()
|
| 69 |
+
self.engine.position(board)
|
| 70 |
+
|
| 71 |
+
info_handler = chess.uci.InfoHandler()
|
| 72 |
+
self.engine.info_handlers.append(info_handler)
|
| 73 |
+
|
| 74 |
+
def first_search(self, board, movetime):
|
| 75 |
+
self.engine.position(board)
|
| 76 |
+
best_move, _ = self.engine.go(movetime=movetime)
|
| 77 |
+
return best_move
|
| 78 |
+
|
| 79 |
+
def search(self, board, wtime, btime, winc, binc):
|
| 80 |
+
self.engine.position(board)
|
| 81 |
+
best_move, _ = self.engine.go(
|
| 82 |
+
wtime=wtime,
|
| 83 |
+
btime=btime,
|
| 84 |
+
winc=winc,
|
| 85 |
+
binc=binc
|
| 86 |
+
)
|
| 87 |
+
return best_move
|
| 88 |
+
|
| 89 |
+
def print_stats(self):
|
| 90 |
+
stats = ["string", "depth", "nps", "nodes", "score"]
|
| 91 |
+
for stat in stats:
|
| 92 |
+
info_handler = self.engine.info_handlers[0]
|
| 93 |
+
if stat in info_handler.info:
|
| 94 |
+
print(" {}: {}".format(stat, info_handler.info[stat]))
|
main.py
CHANGED
|
@@ -1,8 +1,7 @@
|
|
| 1 |
import argparse
|
| 2 |
import chess
|
|
|
|
| 3 |
import model
|
| 4 |
-
import chess.uci
|
| 5 |
-
import chess.xboard
|
| 6 |
import json
|
| 7 |
import lichess
|
| 8 |
import logging
|
|
@@ -81,17 +80,12 @@ def play_game(li, game_id, engine_path, weights, threads, challenge_queue):
|
|
| 81 |
#Initial response of stream will be the full game info. Store it
|
| 82 |
game = model.Game(json.loads(next(updates).decode('utf-8')), username, li.baseUrl)
|
| 83 |
board = setup_board(game.state)
|
| 84 |
-
engine
|
| 85 |
conversation = Conversation(game, engine, li)
|
| 86 |
|
| 87 |
print("+++ {}".format(game.show()))
|
| 88 |
|
| 89 |
-
|
| 90 |
-
minutes = game.clock_initial / 1000 / 60
|
| 91 |
-
seconds = game.clock_initial / 1000 % 60
|
| 92 |
-
inc = game.clock_increment / 1000
|
| 93 |
-
|
| 94 |
-
engine.level(0, minutes, seconds, inc)
|
| 95 |
|
| 96 |
board = play_first_move(game, engine, board, li)
|
| 97 |
|
|
@@ -105,27 +99,10 @@ def play_game(li, game_id, engine_path, weights, threads, challenge_queue):
|
|
| 105 |
board = update_board(board, moves[-1])
|
| 106 |
|
| 107 |
if is_engine_move(game.is_white, moves):
|
| 108 |
-
best_move =
|
| 109 |
-
if CONFIG["engine"]["protocol"] == "xboard":
|
| 110 |
-
engine.setboard(board)
|
| 111 |
-
if game.is_white:
|
| 112 |
-
engine.time(upd.get("wtime") / 10)
|
| 113 |
-
engine.otim(upd.get("btime") / 10)
|
| 114 |
-
else:
|
| 115 |
-
engine.time(upd.get("btime") / 10)
|
| 116 |
-
engine.otim(upd.get("wtime") / 10)
|
| 117 |
-
best_move = engine.go()
|
| 118 |
-
else:
|
| 119 |
-
engine.position(board)
|
| 120 |
-
best_move, ponder = engine.go(
|
| 121 |
-
wtime=upd.get("wtime"),
|
| 122 |
-
btime=upd.get("btime"),
|
| 123 |
-
winc=upd.get("winc"),
|
| 124 |
-
binc=upd.get("binc")
|
| 125 |
-
)
|
| 126 |
li.make_move(game.id, best_move)
|
| 127 |
|
| 128 |
-
|
| 129 |
|
| 130 |
print("--- {} Game over".format(game.url()))
|
| 131 |
accept_next_challenge(challenge_queue, li)
|
|
@@ -139,14 +116,7 @@ def play_first_move(game, engine, board, li):
|
|
| 139 |
moves = game.state["moves"].split()
|
| 140 |
if is_engine_move(game.is_white, moves):
|
| 141 |
# need to hardcode first movetime since Lichess has 30 sec limit.
|
| 142 |
-
|
| 143 |
-
engine.setboard(board)
|
| 144 |
-
engine.st(2)
|
| 145 |
-
best_move = engine.go()
|
| 146 |
-
else:
|
| 147 |
-
engine.position(board)
|
| 148 |
-
best_move, ponder = engine.go(movetime=2000)
|
| 149 |
-
|
| 150 |
li.make_move(game.id, best_move)
|
| 151 |
|
| 152 |
return board
|
|
@@ -171,31 +141,11 @@ def setup_engine(engine_path, board, weights=None, threads=None):
|
|
| 171 |
commands.append("-t")
|
| 172 |
commands.append(threads)
|
| 173 |
|
|
|
|
| 174 |
if CONFIG["engine"]["protocol"] == "xboard":
|
| 175 |
-
|
| 176 |
-
engine = chess.xboard.popen_engine(commands)
|
| 177 |
-
else:
|
| 178 |
-
engine = chess.xboard.popen_engine(engine_path)
|
| 179 |
-
|
| 180 |
-
engine.xboard()
|
| 181 |
-
engine.setboard(board)
|
| 182 |
-
|
| 183 |
-
post_handler = chess.xboard.PostHandler()
|
| 184 |
-
engine.post_handlers.append(post_handler)
|
| 185 |
-
return engine, post_handler
|
| 186 |
|
| 187 |
-
|
| 188 |
-
engine = chess.uci.popen_engine(commands)
|
| 189 |
-
else:
|
| 190 |
-
engine = chess.uci.popen_engine(engine_path)
|
| 191 |
-
|
| 192 |
-
engine.uci()
|
| 193 |
-
engine.position(board)
|
| 194 |
-
|
| 195 |
-
info_handler = chess.uci.InfoHandler()
|
| 196 |
-
engine.info_handlers.append(info_handler)
|
| 197 |
-
|
| 198 |
-
return engine, info_handler
|
| 199 |
|
| 200 |
|
| 201 |
def is_white_to_move(moves):
|
|
@@ -215,18 +165,6 @@ def update_board(board, move):
|
|
| 215 |
return board
|
| 216 |
|
| 217 |
|
| 218 |
-
def get_engine_stats(handler):
|
| 219 |
-
if CONFIG["engine"]["protocol"] == "xboard":
|
| 220 |
-
stats = ["depth", "nodes", "score"]
|
| 221 |
-
for stat in stats:
|
| 222 |
-
if stat in handler.post:
|
| 223 |
-
print(" {}: {}".format(stat, handler.post[stat]))
|
| 224 |
-
else:
|
| 225 |
-
stats = ["string", "depth", "nps", "nodes", "score"]
|
| 226 |
-
for stat in stats:
|
| 227 |
-
if stat in handler.info:
|
| 228 |
-
print(" {}: {}".format(stat, handler.info[stat]))
|
| 229 |
-
|
| 230 |
def load_config():
|
| 231 |
global CONFIG
|
| 232 |
with open("./config.yml", 'r') as stream:
|
|
|
|
| 1 |
import argparse
|
| 2 |
import chess
|
| 3 |
+
import engine_wrapper
|
| 4 |
import model
|
|
|
|
|
|
|
| 5 |
import json
|
| 6 |
import lichess
|
| 7 |
import logging
|
|
|
|
| 80 |
#Initial response of stream will be the full game info. Store it
|
| 81 |
game = model.Game(json.loads(next(updates).decode('utf-8')), username, li.baseUrl)
|
| 82 |
board = setup_board(game.state)
|
| 83 |
+
engine = setup_engine(engine_path, board, weights, threads)
|
| 84 |
conversation = Conversation(game, engine, li)
|
| 85 |
|
| 86 |
print("+++ {}".format(game.show()))
|
| 87 |
|
| 88 |
+
engine.pre_game(game)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
board = play_first_move(game, engine, board, li)
|
| 91 |
|
|
|
|
| 99 |
board = update_board(board, moves[-1])
|
| 100 |
|
| 101 |
if is_engine_move(game.is_white, moves):
|
| 102 |
+
best_move = engine.search(board, upd.get("wtime"), upd.get("btime"), upd.get("winc"), upd.get("binc"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
li.make_move(game.id, best_move)
|
| 104 |
|
| 105 |
+
engine.print_stats()
|
| 106 |
|
| 107 |
print("--- {} Game over".format(game.url()))
|
| 108 |
accept_next_challenge(challenge_queue, li)
|
|
|
|
| 116 |
moves = game.state["moves"].split()
|
| 117 |
if is_engine_move(game.is_white, moves):
|
| 118 |
# need to hardcode first movetime since Lichess has 30 sec limit.
|
| 119 |
+
best_move = engine.first_search(board, 2000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
li.make_move(game.id, best_move)
|
| 121 |
|
| 122 |
return board
|
|
|
|
| 141 |
commands.append("-t")
|
| 142 |
commands.append(threads)
|
| 143 |
|
| 144 |
+
global CONFIG
|
| 145 |
if CONFIG["engine"]["protocol"] == "xboard":
|
| 146 |
+
return engine_wrapper.XBoardEngine(board, commands)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
+
return engine_wrapper.UCIEngine(board, commands)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
|
| 150 |
|
| 151 |
def is_white_to_move(moves):
|
|
|
|
| 165 |
return board
|
| 166 |
|
| 167 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
def load_config():
|
| 169 |
global CONFIG
|
| 170 |
with open("./config.yml", 'r') as stream:
|