Spaces:
Sleeping
Sleeping
Tony commited on
Commit ·
bf4870b
1
Parent(s): 028abf8
Support for polyglot book
Browse files- config.py +16 -4
- config.yml.default +5 -0
- main.py +49 -8
config.py
CHANGED
|
@@ -25,12 +25,19 @@ def load_config():
|
|
| 25 |
raise Exception(section[2])
|
| 26 |
|
| 27 |
|
| 28 |
-
engine_sections = ["dir", "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
for subsection in engine_sections:
|
| 30 |
-
if subsection not in CONFIG["engine"]:
|
| 31 |
raise Exception("Your config.yml does not have required `engine` subsection `{}`.".format(subsection))
|
| 32 |
-
if not isinstance(CONFIG["engine"][subsection],
|
| 33 |
-
raise Exception("
|
| 34 |
|
| 35 |
if CONFIG["token"] == "xxxxxxxxxxxxxxxx":
|
| 36 |
raise Exception("Your config.yml has the default Lichess API token. This is probably wrong.")
|
|
@@ -46,4 +53,9 @@ def load_config():
|
|
| 46 |
if not os.access(engine, os.X_OK):
|
| 47 |
raise Exception("The engine %s doesn't have execute (x) permission. Try: chmod +x %s" % (engine, engine))
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return CONFIG
|
|
|
|
| 25 |
raise Exception(section[2])
|
| 26 |
|
| 27 |
|
| 28 |
+
engine_sections = [["dir", str, "´dir´ must be a string wrapped in quotes."],
|
| 29 |
+
["name", str, "´name´ must be a string wrapped in quotes."],
|
| 30 |
+
["polyglot", bool, "´polyglot´ must be a boolean type: true or false."],
|
| 31 |
+
["polyglot_book", str, "´polyglot_book´ must be a string wrapped in quotes."],
|
| 32 |
+
["polyglot_max_depth", int, "`polyglot_max_depth` must be an integer number without quotes."],
|
| 33 |
+
["polyglot_min_weight", int, "`polyglot_min_weight` must be an integer number without quotes."],
|
| 34 |
+
["polyglot_random", bool, "`polyglot_random` must be a boolean type: true or false."]]
|
| 35 |
+
|
| 36 |
for subsection in engine_sections:
|
| 37 |
+
if subsection[0] not in CONFIG["engine"]:
|
| 38 |
raise Exception("Your config.yml does not have required `engine` subsection `{}`.".format(subsection))
|
| 39 |
+
if not isinstance(CONFIG["engine"][subsection[0]], subsection[1]):
|
| 40 |
+
raise Exception("´engine´ subsection {}".format(subsection[2]))
|
| 41 |
|
| 42 |
if CONFIG["token"] == "xxxxxxxxxxxxxxxx":
|
| 43 |
raise Exception("Your config.yml has the default Lichess API token. This is probably wrong.")
|
|
|
|
| 53 |
if not os.access(engine, os.X_OK):
|
| 54 |
raise Exception("The engine %s doesn't have execute (x) permission. Try: chmod +x %s" % (engine, engine))
|
| 55 |
|
| 56 |
+
if (CONFIG["engine"]["polyglot"] == True):
|
| 57 |
+
book_dir = os.path.join(CONFIG["engine"]["dir"], CONFIG["engine"]["polyglot_book"])
|
| 58 |
+
if not os.path.isfile(book_dir):
|
| 59 |
+
raise Exception("The polyglot book %s file does not exist." % book_dir)
|
| 60 |
+
|
| 61 |
return CONFIG
|
config.yml.default
CHANGED
|
@@ -7,6 +7,11 @@ engine: # engine settings
|
|
| 7 |
protocol: "uci" # "uci" or "xboard"
|
| 8 |
# weights: "latest.txt" # only if the engine is lczero
|
| 9 |
# threads: 1 # only if the engine is lczero
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
ucioptions: # arbitrary UCI options passed to the engine
|
| 12 |
Move Overhead: 100 # increase if your bot flags games too often
|
|
|
|
| 7 |
protocol: "uci" # "uci" or "xboard"
|
| 8 |
# weights: "latest.txt" # only if the engine is lczero
|
| 9 |
# threads: 1 # only if the engine is lczero
|
| 10 |
+
polyglot: false # activate polyglot book
|
| 11 |
+
polyglot_book: "book.bin" # book file name, must be in engine.dir
|
| 12 |
+
polyglot_max_depth: 8 # half move max depth
|
| 13 |
+
polyglot_min_weight: 1 # best move weight
|
| 14 |
+
polyglot_random: true # select move randomly otherwise select by min weight
|
| 15 |
|
| 16 |
ucioptions: # arbitrary UCI options passed to the engine
|
| 17 |
Move Overhead: 100 # increase if your bot flags games too often
|
main.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
import argparse
|
| 2 |
import chess
|
| 3 |
from chess.variant import find_variant
|
|
|
|
| 4 |
import engine_wrapper
|
| 5 |
import model
|
| 6 |
import json
|
|
@@ -36,8 +37,10 @@ def watch_control_stream(control_queue, li):
|
|
| 36 |
else:
|
| 37 |
control_queue.put_nowait({"type": "ping"})
|
| 38 |
|
| 39 |
-
def start(li, user_profile,
|
| 40 |
# init
|
|
|
|
|
|
|
| 41 |
username = user_profile.get("username")
|
| 42 |
print("Welcome {}!".format(username))
|
| 43 |
manager = multiprocessing.Manager()
|
|
@@ -75,7 +78,7 @@ def start(li, user_profile, max_games, max_queued, engine_factory, config):
|
|
| 75 |
else:
|
| 76 |
queued_processes -= 1
|
| 77 |
game_id = event["game"]["id"]
|
| 78 |
-
pool.apply_async(play_game, [li, game_id, control_queue, engine_factory])
|
| 79 |
busy_processes += 1
|
| 80 |
print("--- Process Used. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 81 |
|
|
@@ -96,7 +99,7 @@ def start(li, user_profile, max_games, max_queued, engine_factory, config):
|
|
| 96 |
control_stream.join()
|
| 97 |
|
| 98 |
|
| 99 |
-
def play_game(li, game_id, control_queue, engine_factory):
|
| 100 |
username = li.get_profile()["username"]
|
| 101 |
updates = li.get_game_stream(game_id).iter_lines()
|
| 102 |
|
|
@@ -110,7 +113,12 @@ def play_game(li, game_id, control_queue, engine_factory):
|
|
| 110 |
|
| 111 |
engine.pre_game(game)
|
| 112 |
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
try:
|
| 116 |
for binary_chunk in updates:
|
|
@@ -123,7 +131,15 @@ def play_game(li, game_id, control_queue, engine_factory):
|
|
| 123 |
board = update_board(board, moves[-1])
|
| 124 |
|
| 125 |
if is_engine_move(game.is_white, moves):
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
li.make_move(game.id, best_move)
|
| 128 |
except (RemoteDisconnected, ConnectionError, ProtocolError, HTTPError) as exception:
|
| 129 |
print("Abandoning game due to connection error")
|
|
@@ -150,6 +166,33 @@ def play_first_move(game, engine, board, li):
|
|
| 150 |
return board
|
| 151 |
|
| 152 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
def setup_board(game):
|
| 154 |
if game.variant_name.lower() == "chess960":
|
| 155 |
board = chess.Board(game.initial_fen, chess960=True)
|
|
@@ -204,9 +247,7 @@ if __name__ == "__main__":
|
|
| 204 |
is_bot = upgrade_account(li)
|
| 205 |
|
| 206 |
if is_bot:
|
| 207 |
-
max_games = CONFIG["max_concurrent_games"]
|
| 208 |
-
max_queued = CONFIG["max_queued_challenges"]
|
| 209 |
engine_factory = partial(engine_wrapper.create_engine, CONFIG)
|
| 210 |
-
start(li, user_profile,
|
| 211 |
else:
|
| 212 |
print("{} is not a bot account. Please upgrade your it to a bot account!".format(user_profile["username"]))
|
|
|
|
| 1 |
import argparse
|
| 2 |
import chess
|
| 3 |
from chess.variant import find_variant
|
| 4 |
+
import chess.polyglot
|
| 5 |
import engine_wrapper
|
| 6 |
import model
|
| 7 |
import json
|
|
|
|
| 37 |
else:
|
| 38 |
control_queue.put_nowait({"type": "ping"})
|
| 39 |
|
| 40 |
+
def start(li, user_profile, engine_factory, config):
|
| 41 |
# init
|
| 42 |
+
max_games = config["max_concurrent_games"]
|
| 43 |
+
max_queued = config["max_queued_challenges"]
|
| 44 |
username = user_profile.get("username")
|
| 45 |
print("Welcome {}!".format(username))
|
| 46 |
manager = multiprocessing.Manager()
|
|
|
|
| 78 |
else:
|
| 79 |
queued_processes -= 1
|
| 80 |
game_id = event["game"]["id"]
|
| 81 |
+
pool.apply_async(play_game, [li, game_id, control_queue, engine_factory, config])
|
| 82 |
busy_processes += 1
|
| 83 |
print("--- Process Used. Total Queued: {}. Total Used: {}".format(queued_processes, busy_processes))
|
| 84 |
|
|
|
|
| 99 |
control_stream.join()
|
| 100 |
|
| 101 |
|
| 102 |
+
def play_game(li, game_id, control_queue, engine_factory, config):
|
| 103 |
username = li.get_profile()["username"]
|
| 104 |
updates = li.get_game_stream(game_id).iter_lines()
|
| 105 |
|
|
|
|
| 113 |
|
| 114 |
engine.pre_game(game)
|
| 115 |
|
| 116 |
+
engine_cfg = config["engine"]
|
| 117 |
+
|
| 118 |
+
if (engine_cfg["polyglot"] == True):
|
| 119 |
+
board = play_first_book_move(game, engine, board, li, engine_cfg)
|
| 120 |
+
else:
|
| 121 |
+
board = play_first_move(game, engine, board, li)
|
| 122 |
|
| 123 |
try:
|
| 124 |
for binary_chunk in updates:
|
|
|
|
| 131 |
board = update_board(board, moves[-1])
|
| 132 |
|
| 133 |
if is_engine_move(game.is_white, moves):
|
| 134 |
+
if (engine_cfg["polyglot"] == True and len(moves) <= (engine_cfg["polyglot_max_depth"] * 2) - 1):
|
| 135 |
+
book_move = get_book_move(board, engine_cfg)
|
| 136 |
+
if (book_move != None):
|
| 137 |
+
best_move = book_move
|
| 138 |
+
else:
|
| 139 |
+
best_move = engine.search(board, upd.get("wtime"), upd.get("btime"), upd.get("winc"), upd.get("binc"))
|
| 140 |
+
else:
|
| 141 |
+
best_move = engine.search(board, upd.get("wtime"), upd.get("btime"), upd.get("winc"), upd.get("binc"))
|
| 142 |
+
|
| 143 |
li.make_move(game.id, best_move)
|
| 144 |
except (RemoteDisconnected, ConnectionError, ProtocolError, HTTPError) as exception:
|
| 145 |
print("Abandoning game due to connection error")
|
|
|
|
| 166 |
return board
|
| 167 |
|
| 168 |
|
| 169 |
+
def play_first_book_move(game, engine, board, li, config):
|
| 170 |
+
moves = game.state["moves"].split()
|
| 171 |
+
if is_engine_move(game.is_white, moves):
|
| 172 |
+
book_move = get_book_move(board, config)
|
| 173 |
+
if (book_move != None):
|
| 174 |
+
li.make_move(game.id, book_move)
|
| 175 |
+
else:
|
| 176 |
+
return play_first_move(game, engine, board, li)
|
| 177 |
+
|
| 178 |
+
return board
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
def get_book_move(board, engine_cfg):
|
| 182 |
+
book_dir = os.path.join(engine_cfg["dir"], engine_cfg["polyglot_book"])
|
| 183 |
+
try:
|
| 184 |
+
with chess.polyglot.open_reader(book_dir) as reader:
|
| 185 |
+
if (engine_cfg["polyglot_random"] == True):
|
| 186 |
+
book_move = reader.choice(board).move()
|
| 187 |
+
else:
|
| 188 |
+
book_move = reader.find(board, engine_cfg["polyglot_min_weight"]).move()
|
| 189 |
+
return book_move
|
| 190 |
+
except (FileNotFoundError, IndexError):
|
| 191 |
+
pass
|
| 192 |
+
|
| 193 |
+
return None
|
| 194 |
+
|
| 195 |
+
|
| 196 |
def setup_board(game):
|
| 197 |
if game.variant_name.lower() == "chess960":
|
| 198 |
board = chess.Board(game.initial_fen, chess960=True)
|
|
|
|
| 247 |
is_bot = upgrade_account(li)
|
| 248 |
|
| 249 |
if is_bot:
|
|
|
|
|
|
|
| 250 |
engine_factory = partial(engine_wrapper.create_engine, CONFIG)
|
| 251 |
+
start(li, user_profile, engine_factory, CONFIG)
|
| 252 |
else:
|
| 253 |
print("{} is not a bot account. Please upgrade your it to a bot account!".format(user_profile["username"]))
|