Spaces:
Sleeping
Sleeping
AttackingOrDefending commited on
Commit ·
7e681ca
1
Parent(s): 7de9da3
Support Ponder (#22)
Browse files- config.yml.default +3 -2
- engine_ctrl.py +19 -3
- engine_wrapper.py +2 -2
- lishogi-bot.py +19 -1
config.yml.default
CHANGED
|
@@ -5,7 +5,7 @@ engine: # Engine Settings.
|
|
| 5 |
dir: "./engines/" # Directory containing engines, relative to this project.
|
| 6 |
name: "engine_name" # Binary name of the engine to use. Make sure the engine you use is running under the USI protocol.
|
| 7 |
protocol: "usi" # Protocol that engine is run under. Only "usi" is supported currently.
|
| 8 |
-
ponder: false #
|
| 9 |
# engine_options: # Any custom command line params to pass to the engine.
|
| 10 |
# cpuct: 3.1
|
| 11 |
usi_options: # Arbitrary USI options passed to the engine.
|
|
@@ -21,12 +21,13 @@ engine: # Engine Settings.
|
|
| 21 |
abort_time: 30 # Time in seconds after which the engine will abort the game if there is no activity.
|
| 22 |
fake_think_time: false # Artificially slow down the bot to pretend like it's thinking.
|
| 23 |
rate_limiting_delay: 0 # Time (in ms) to delay after sending a move to prevent "Too Many Requests" errors.
|
| 24 |
-
move_overhead:
|
| 25 |
|
| 26 |
correspondence:
|
| 27 |
move_time: 60 # Time in seconds to search in correspondence games.
|
| 28 |
checkin_period: 600 # How often to check for opponent moves in correspondence games after disconnecting.
|
| 29 |
disconnect_time: 300 # Time before disconnecting from a correspondence game.
|
|
|
|
| 30 |
|
| 31 |
challenge: # Incoming challenges.
|
| 32 |
concurrency: 1 # Number of games to play simultaneously.
|
|
|
|
| 5 |
dir: "./engines/" # Directory containing engines, relative to this project.
|
| 6 |
name: "engine_name" # Binary name of the engine to use. Make sure the engine you use is running under the USI protocol.
|
| 7 |
protocol: "usi" # Protocol that engine is run under. Only "usi" is supported currently.
|
| 8 |
+
ponder: false # Think on opponent's time.
|
| 9 |
# engine_options: # Any custom command line params to pass to the engine.
|
| 10 |
# cpuct: 3.1
|
| 11 |
usi_options: # Arbitrary USI options passed to the engine.
|
|
|
|
| 21 |
abort_time: 30 # Time in seconds after which the engine will abort the game if there is no activity.
|
| 22 |
fake_think_time: false # Artificially slow down the bot to pretend like it's thinking.
|
| 23 |
rate_limiting_delay: 0 # Time (in ms) to delay after sending a move to prevent "Too Many Requests" errors.
|
| 24 |
+
move_overhead: 1900 # Increase if your bot flags games too often.
|
| 25 |
|
| 26 |
correspondence:
|
| 27 |
move_time: 60 # Time in seconds to search in correspondence games.
|
| 28 |
checkin_period: 600 # How often to check for opponent moves in correspondence games after disconnecting.
|
| 29 |
disconnect_time: 300 # Time before disconnecting from a correspondence game.
|
| 30 |
+
ponder: false # ponder in correspondence games the bot is connected to
|
| 31 |
|
| 32 |
challenge: # Incoming challenges.
|
| 33 |
concurrency: 1 # Number of games to play simultaneously.
|
engine_ctrl.py
CHANGED
|
@@ -110,7 +110,7 @@ class Engine:
|
|
| 110 |
|
| 111 |
self.send("setoption name %s value %s" % (name, value))
|
| 112 |
|
| 113 |
-
def go(self, position, moves, movetime=None, btime=None, wtime=None, binc=None, winc=None, byo=None, depth=None, nodes=None):
|
| 114 |
if position != "startpos":
|
| 115 |
position = "sfen " + position
|
| 116 |
self.send("position %s moves %s" % (position, " ".join(moves)))
|
|
@@ -118,6 +118,8 @@ class Engine:
|
|
| 118 |
|
| 119 |
builder = []
|
| 120 |
builder.append("go")
|
|
|
|
|
|
|
| 121 |
if movetime is not None:
|
| 122 |
builder.append("movetime")
|
| 123 |
builder.append(str(movetime))
|
|
@@ -149,15 +151,22 @@ class Engine:
|
|
| 149 |
|
| 150 |
info = {}
|
| 151 |
info["bestmove"] = None
|
|
|
|
| 152 |
|
| 153 |
while True:
|
| 154 |
command, arg = self.recv_usi()
|
| 155 |
|
| 156 |
if command == "bestmove":
|
| 157 |
-
|
|
|
|
| 158 |
if bestmove and bestmove != "(none)":
|
| 159 |
info["bestmove"] = bestmove
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
elif command == "info":
|
| 163 |
arg = arg or ""
|
|
@@ -218,3 +227,10 @@ class Engine:
|
|
| 218 |
self.info = info
|
| 219 |
else:
|
| 220 |
logger.error("Unexpected engine response to go: %s %s" % (command, arg))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
self.send("setoption name %s value %s" % (name, value))
|
| 112 |
|
| 113 |
+
def go(self, position, moves, movetime=None, btime=None, wtime=None, binc=None, winc=None, byo=None, depth=None, nodes=None, ponder=False):
|
| 114 |
if position != "startpos":
|
| 115 |
position = "sfen " + position
|
| 116 |
self.send("position %s moves %s" % (position, " ".join(moves)))
|
|
|
|
| 118 |
|
| 119 |
builder = []
|
| 120 |
builder.append("go")
|
| 121 |
+
if ponder:
|
| 122 |
+
builder.append("ponder")
|
| 123 |
if movetime is not None:
|
| 124 |
builder.append("movetime")
|
| 125 |
builder.append(str(movetime))
|
|
|
|
| 151 |
|
| 152 |
info = {}
|
| 153 |
info["bestmove"] = None
|
| 154 |
+
info["pondermove"] = None
|
| 155 |
|
| 156 |
while True:
|
| 157 |
command, arg = self.recv_usi()
|
| 158 |
|
| 159 |
if command == "bestmove":
|
| 160 |
+
arg_split = arg.split()
|
| 161 |
+
bestmove = arg_split[0]
|
| 162 |
if bestmove and bestmove != "(none)":
|
| 163 |
info["bestmove"] = bestmove
|
| 164 |
+
if len(arg_split) == 3:
|
| 165 |
+
if arg_split[1] == 'ponder':
|
| 166 |
+
ponder_move = arg_split[2]
|
| 167 |
+
if ponder_move and ponder_move != "(none)":
|
| 168 |
+
info["pondermove"] = ponder_move
|
| 169 |
+
return (info["bestmove"], info["pondermove"])
|
| 170 |
|
| 171 |
elif command == "info":
|
| 172 |
arg = arg or ""
|
|
|
|
| 227 |
self.info = info
|
| 228 |
else:
|
| 229 |
logger.error("Unexpected engine response to go: %s %s" % (command, arg))
|
| 230 |
+
|
| 231 |
+
def stop(self):
|
| 232 |
+
self.send("stop")
|
| 233 |
+
|
| 234 |
+
def ponderhit(self):
|
| 235 |
+
self.send("ponderhit")
|
| 236 |
+
logger.info("ponderhit")
|
engine_wrapper.py
CHANGED
|
@@ -88,7 +88,7 @@ class USIEngine(EngineWrapper):
|
|
| 88 |
nodes=cmds.get("nodes"),
|
| 89 |
depth=cmds.get("depth"),
|
| 90 |
movetime=cmds.get("movetime"),
|
| 91 |
-
|
| 92 |
)
|
| 93 |
else:
|
| 94 |
best_move, ponder_move = self.engine.go(
|
|
@@ -99,7 +99,7 @@ class USIEngine(EngineWrapper):
|
|
| 99 |
binc=binc,
|
| 100 |
winc=winc,
|
| 101 |
byo=byo,
|
| 102 |
-
|
| 103 |
)
|
| 104 |
return (best_move, ponder_move)
|
| 105 |
|
|
|
|
| 88 |
nodes=cmds.get("nodes"),
|
| 89 |
depth=cmds.get("depth"),
|
| 90 |
movetime=cmds.get("movetime"),
|
| 91 |
+
ponder=ponder
|
| 92 |
)
|
| 93 |
else:
|
| 94 |
best_move, ponder_move = self.engine.go(
|
|
|
|
| 99 |
binc=binc,
|
| 100 |
winc=winc,
|
| 101 |
byo=byo,
|
| 102 |
+
ponder=ponder
|
| 103 |
)
|
| 104 |
return (best_move, ponder_move)
|
| 105 |
|
lishogi-bot.py
CHANGED
|
@@ -198,6 +198,9 @@ def start(li, user_profile, engine_factory, config, logging_level, log_filename,
|
|
| 198 |
logging_listener.join()
|
| 199 |
|
| 200 |
|
|
|
|
|
|
|
|
|
|
| 201 |
@backoff.on_exception(backoff.expo, BaseException, max_time=600, giveup=is_final)
|
| 202 |
def play_game(li, game_id, control_queue, engine_factory, user_profile, config, challenge_queue, correspondence_queue, logging_queue, logging_configurer, logging_level):
|
| 203 |
logging_configurer(logging_queue, logging_level)
|
|
@@ -222,7 +225,8 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 222 |
|
| 223 |
engine_cfg = config["engine"]
|
| 224 |
is_usi = engine_cfg["protocol"] == "usi"
|
| 225 |
-
|
|
|
|
| 226 |
move_overhead = config.get("move_overhead", 1000)
|
| 227 |
delay_seconds = config.get("rate_limiting_delay", 0)/1000
|
| 228 |
polyglot_cfg = engine_cfg.get("polyglot", {})
|
|
@@ -309,6 +313,20 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 309 |
|
| 310 |
best_move = None
|
| 311 |
ponder_move = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
|
| 313 |
btime = upd["btime"]
|
| 314 |
wtime = upd["wtime"]
|
|
|
|
| 198 |
logging_listener.join()
|
| 199 |
|
| 200 |
|
| 201 |
+
ponder_results = {}
|
| 202 |
+
|
| 203 |
+
|
| 204 |
@backoff.on_exception(backoff.expo, BaseException, max_time=600, giveup=is_final)
|
| 205 |
def play_game(li, game_id, control_queue, engine_factory, user_profile, config, challenge_queue, correspondence_queue, logging_queue, logging_configurer, logging_level):
|
| 206 |
logging_configurer(logging_queue, logging_level)
|
|
|
|
| 225 |
|
| 226 |
engine_cfg = config["engine"]
|
| 227 |
is_usi = engine_cfg["protocol"] == "usi"
|
| 228 |
+
ponder_cfg = correspondence_cfg if is_correspondence else engine_cfg
|
| 229 |
+
is_usi_ponder = is_usi and ponder_cfg.get("ponder", False)
|
| 230 |
move_overhead = config.get("move_overhead", 1000)
|
| 231 |
delay_seconds = config.get("rate_limiting_delay", 0)/1000
|
| 232 |
polyglot_cfg = engine_cfg.get("polyglot", {})
|
|
|
|
| 313 |
|
| 314 |
best_move = None
|
| 315 |
ponder_move = None
|
| 316 |
+
|
| 317 |
+
if ponder_thread is not None:
|
| 318 |
+
move_uci = makeusi(moves[-1])
|
| 319 |
+
if ponder_usi == move_uci:
|
| 320 |
+
engine.engine.ponderhit()
|
| 321 |
+
ponder_thread.join()
|
| 322 |
+
ponder_thread = None
|
| 323 |
+
best_move, ponder_move = ponder_results[game.id]
|
| 324 |
+
engine.print_stats()
|
| 325 |
+
else:
|
| 326 |
+
engine.engine.stop()
|
| 327 |
+
ponder_thread.join()
|
| 328 |
+
ponder_thread = None
|
| 329 |
+
ponder_usi = None
|
| 330 |
|
| 331 |
btime = upd["btime"]
|
| 332 |
wtime = upd["wtime"]
|