Spaces:
Sleeping
Sleeping
Thibault Duplessis commited on
Commit ·
f07b408
1
Parent(s): de0b127
fix polyglot config (and some weirdness)
Browse files- config.py +1 -12
- config.yml.default +6 -5
- lichess-bot.py +19 -21
config.py
CHANGED
|
@@ -24,12 +24,7 @@ def load_config(config_file):
|
|
| 24 |
raise Exception(section[2])
|
| 25 |
|
| 26 |
engine_sections = [["dir", str, "´dir´ must be a string wrapped in quotes."],
|
| 27 |
-
["name", str, "´name´ must be a string wrapped in quotes."]
|
| 28 |
-
["polyglot", bool, "´polyglot´ must be a boolean type: true or false."],
|
| 29 |
-
["polyglot_book", str, "´polyglot_book´ must be a string wrapped in quotes."],
|
| 30 |
-
["polyglot_max_depth", int, "`polyglot_max_depth` must be an integer number without quotes."],
|
| 31 |
-
["polyglot_min_weight", int, "`polyglot_min_weight` must be an integer number without quotes."],
|
| 32 |
-
["polyglot_random", bool, "`polyglot_random` must be a boolean type: true or false."]]
|
| 33 |
for subsection in engine_sections:
|
| 34 |
if subsection[0] not in CONFIG["engine"]:
|
| 35 |
raise Exception("Your config.yml does not have required `engine` subsection `{}`.".format(subsection))
|
|
@@ -50,10 +45,4 @@ def load_config(config_file):
|
|
| 50 |
if not os.access(engine, os.X_OK):
|
| 51 |
raise Exception("The engine %s doesn't have execute (x) permission. Try: chmod +x %s" % (engine, engine))
|
| 52 |
|
| 53 |
-
if (CONFIG["engine"]["polyglot"] == True):
|
| 54 |
-
book_dir = os.path.join(CONFIG["engine"]["dir"], CONFIG["engine"]["polyglot_book"])
|
| 55 |
-
if not os.path.isfile(book_dir):
|
| 56 |
-
raise Exception("The polyglot book %s file does not exist." % book_dir)
|
| 57 |
-
CONFIG["engine"]["polyglot_book"] = book_dir
|
| 58 |
-
|
| 59 |
return CONFIG
|
|
|
|
| 24 |
raise Exception(section[2])
|
| 25 |
|
| 26 |
engine_sections = [["dir", str, "´dir´ must be a string wrapped in quotes."],
|
| 27 |
+
["name", str, "´name´ must be a string wrapped in quotes."]]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
for subsection in engine_sections:
|
| 29 |
if subsection[0] not in CONFIG["engine"]:
|
| 30 |
raise Exception("Your config.yml does not have required `engine` subsection `{}`.".format(subsection))
|
|
|
|
| 45 |
if not os.access(engine, os.X_OK):
|
| 46 |
raise Exception("The engine %s doesn't have execute (x) permission. Try: chmod +x %s" % (engine, engine))
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
return CONFIG
|
config.yml.default
CHANGED
|
@@ -8,11 +8,12 @@ engine: # engine settings
|
|
| 8 |
# weights: "latest.txt" # only if the engine is lczero
|
| 9 |
# threads: 1 # only if the engine is lczero
|
| 10 |
# gpu: 0 # only if the engine is lczero
|
| 11 |
-
polyglot:
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
silence_stderr: false # some engines (yes you, leela) are very noisy
|
| 17 |
|
| 18 |
ucioptions: # arbitrary UCI options passed to the engine
|
|
|
|
| 8 |
# weights: "latest.txt" # only if the engine is lczero
|
| 9 |
# threads: 1 # only if the engine is lczero
|
| 10 |
# gpu: 0 # only if the engine is lczero
|
| 11 |
+
polyglot:
|
| 12 |
+
enabled: false # activate polyglot book
|
| 13 |
+
book: "book.bin" # book file path
|
| 14 |
+
max_depth: 8 # half move max depth
|
| 15 |
+
min_weight: 1 # best move weight
|
| 16 |
+
random: true # select move randomly otherwise select by min weight
|
| 17 |
silence_stderr: false # some engines (yes you, leela) are very noisy
|
| 18 |
|
| 19 |
ucioptions: # arbitrary UCI options passed to the engine
|
lichess-bot.py
CHANGED
|
@@ -110,11 +110,11 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config):
|
|
| 110 |
print("+++ {}".format(game))
|
| 111 |
|
| 112 |
engine_cfg = config["engine"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
-
if (engine_cfg["polyglot"] == True):
|
| 115 |
-
board = play_first_book_move(game, engine, board, li, engine_cfg)
|
| 116 |
-
else:
|
| 117 |
-
board = play_first_move(game, engine, board, li)
|
| 118 |
engine.set_time_control(game)
|
| 119 |
|
| 120 |
try:
|
|
@@ -129,8 +129,8 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config):
|
|
| 129 |
board = update_board(board, moves[-1])
|
| 130 |
if is_engine_move(game, moves):
|
| 131 |
best_move = None
|
| 132 |
-
if (
|
| 133 |
-
best_move = get_book_move(board,
|
| 134 |
if best_move == None:
|
| 135 |
best_move = engine.search(board, upd["wtime"], upd["btime"], upd["winc"], upd["binc"])
|
| 136 |
li.make_move(game.id, best_move)
|
|
@@ -156,33 +156,31 @@ def play_first_move(game, engine, board, li):
|
|
| 156 |
# need to hardcode first movetime since Lichess has 30 sec limit.
|
| 157 |
best_move = engine.first_search(board, 10000)
|
| 158 |
li.make_move(game.id, best_move)
|
| 159 |
-
|
|
|
|
| 160 |
|
| 161 |
|
| 162 |
def play_first_book_move(game, engine, board, li, config):
|
| 163 |
moves = game.state["moves"].split()
|
| 164 |
if is_engine_move(game, moves):
|
| 165 |
book_move = get_book_move(board, config)
|
| 166 |
-
if
|
| 167 |
li.make_move(game.id, book_move)
|
|
|
|
| 168 |
else:
|
| 169 |
return play_first_move(game, engine, board, li)
|
|
|
|
| 170 |
|
| 171 |
-
return board
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
book_move = reader.choice(board).move()
|
| 179 |
else:
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
pass
|
| 184 |
-
|
| 185 |
-
return None
|
| 186 |
|
| 187 |
|
| 188 |
def setup_board(game):
|
|
|
|
| 110 |
print("+++ {}".format(game))
|
| 111 |
|
| 112 |
engine_cfg = config["engine"]
|
| 113 |
+
polyglot_cfg = engine_cfg.get("polyglot", {})
|
| 114 |
+
|
| 115 |
+
if not polyglot_cfg.get("enabled") or not play_first_book_move(game, engine, board, li, polyglot_cfg):
|
| 116 |
+
play_first_move(game, engine, board, li)
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
engine.set_time_control(game)
|
| 119 |
|
| 120 |
try:
|
|
|
|
| 129 |
board = update_board(board, moves[-1])
|
| 130 |
if is_engine_move(game, moves):
|
| 131 |
best_move = None
|
| 132 |
+
if polyglot_cfg.get("enabled") and len(moves) <= polyglot_cfg.get("max_depth", 8) * 2 - 1:
|
| 133 |
+
best_move = get_book_move(board, polyglot_cfg)
|
| 134 |
if best_move == None:
|
| 135 |
best_move = engine.search(board, upd["wtime"], upd["btime"], upd["winc"], upd["binc"])
|
| 136 |
li.make_move(game.id, best_move)
|
|
|
|
| 156 |
# need to hardcode first movetime since Lichess has 30 sec limit.
|
| 157 |
best_move = engine.first_search(board, 10000)
|
| 158 |
li.make_move(game.id, best_move)
|
| 159 |
+
return True
|
| 160 |
+
return False
|
| 161 |
|
| 162 |
|
| 163 |
def play_first_book_move(game, engine, board, li, config):
|
| 164 |
moves = game.state["moves"].split()
|
| 165 |
if is_engine_move(game, moves):
|
| 166 |
book_move = get_book_move(board, config)
|
| 167 |
+
if book_move:
|
| 168 |
li.make_move(game.id, book_move)
|
| 169 |
+
return True
|
| 170 |
else:
|
| 171 |
return play_first_move(game, engine, board, li)
|
| 172 |
+
return False
|
| 173 |
|
|
|
|
| 174 |
|
| 175 |
+
def get_book_move(board, config):
|
| 176 |
+
with chess.polyglot.open_reader(config["book"]) as reader:
|
| 177 |
+
try: # python-chess can raise "IndexError"
|
| 178 |
+
if config.get("random"):
|
| 179 |
+
return reader.choice(board).move()
|
|
|
|
| 180 |
else:
|
| 181 |
+
return reader.find(board, config.get("min_weight", 1)).move()
|
| 182 |
+
except:
|
| 183 |
+
pass
|
|
|
|
|
|
|
|
|
|
| 184 |
|
| 185 |
|
| 186 |
def setup_board(game):
|