Spaces:
Sleeping
Sleeping
TheYoBots commited on
Commit ·
192c2bf
1
Parent(s): 5a81538
Color Refactor
Browse fileshttps://github.com/WandererXII/lishogi-bot/commit/7976eac1096f478fd5c7046bd24893b5f4cca22e
- engine_ctrl.py +1 -1
- engine_wrapper.py +7 -7
- lishogi-bot.py +24 -24
- model.py +9 -9
- util.py +1 -44
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,
|
| 114 |
if position != "startpos":
|
| 115 |
position = "sfen " + position
|
| 116 |
self.send("position %s moves %s" % (position, " ".join(moves)))
|
|
|
|
| 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)))
|
engine_wrapper.py
CHANGED
|
@@ -36,7 +36,7 @@ class EngineWrapper:
|
|
| 36 |
def first_search(self, board, movetime):
|
| 37 |
pass
|
| 38 |
|
| 39 |
-
def search(self, game, board,
|
| 40 |
pass
|
| 41 |
|
| 42 |
def print_stats(self):
|
|
@@ -84,7 +84,7 @@ class USIEngine(EngineWrapper):
|
|
| 84 |
best_move, _ = self.engine.go(board.sfen(), "", movetime=movetime)
|
| 85 |
return best_move
|
| 86 |
|
| 87 |
-
def search_with_ponder(self, game, board,
|
| 88 |
moves = [m.usi() for m in list(board.move_stack)]
|
| 89 |
cmds = self.go_commands
|
| 90 |
if len(cmds) > 0:
|
|
@@ -100,25 +100,25 @@ class USIEngine(EngineWrapper):
|
|
| 100 |
best_move, ponder_move = self.engine.go(
|
| 101 |
game.initial_fen,
|
| 102 |
moves,
|
| 103 |
-
wtime=wtime,
|
| 104 |
btime=btime,
|
| 105 |
-
|
| 106 |
binc=binc,
|
|
|
|
| 107 |
byo=byo,
|
| 108 |
#ponder=ponder
|
| 109 |
)
|
| 110 |
return (best_move, ponder_move)
|
| 111 |
|
| 112 |
-
def search(self, game, board,
|
| 113 |
cmds = self.go_commands
|
| 114 |
moves = [m.usi() for m in list(board.move_stack)]
|
| 115 |
best_move, _ = self.engine.go(
|
| 116 |
game.initial_fen,
|
| 117 |
moves,
|
| 118 |
-
wtime=wtime,
|
| 119 |
btime=btime,
|
| 120 |
-
|
| 121 |
binc=binc,
|
|
|
|
| 122 |
depth=cmds.get("depth"),
|
| 123 |
nodes=cmds.get("nodes"),
|
| 124 |
movetime=cmds.get("movetime")
|
|
|
|
| 36 |
def first_search(self, board, movetime):
|
| 37 |
pass
|
| 38 |
|
| 39 |
+
def search(self, game, board, btime, wtime, binc, winc):
|
| 40 |
pass
|
| 41 |
|
| 42 |
def print_stats(self):
|
|
|
|
| 84 |
best_move, _ = self.engine.go(board.sfen(), "", movetime=movetime)
|
| 85 |
return best_move
|
| 86 |
|
| 87 |
+
def search_with_ponder(self, game, board, btime, wtime, binc, winc, byo, ponder=False):
|
| 88 |
moves = [m.usi() for m in list(board.move_stack)]
|
| 89 |
cmds = self.go_commands
|
| 90 |
if len(cmds) > 0:
|
|
|
|
| 100 |
best_move, ponder_move = self.engine.go(
|
| 101 |
game.initial_fen,
|
| 102 |
moves,
|
|
|
|
| 103 |
btime=btime,
|
| 104 |
+
wtime=wtime,
|
| 105 |
binc=binc,
|
| 106 |
+
winc=winc,
|
| 107 |
byo=byo,
|
| 108 |
#ponder=ponder
|
| 109 |
)
|
| 110 |
return (best_move, ponder_move)
|
| 111 |
|
| 112 |
+
def search(self, game, board, btime, wtime, binc, winc):
|
| 113 |
cmds = self.go_commands
|
| 114 |
moves = [m.usi() for m in list(board.move_stack)]
|
| 115 |
best_move, _ = self.engine.go(
|
| 116 |
game.initial_fen,
|
| 117 |
moves,
|
|
|
|
| 118 |
btime=btime,
|
| 119 |
+
wtime=wtime,
|
| 120 |
binc=binc,
|
| 121 |
+
winc=winc,
|
| 122 |
depth=cmds.get("depth"),
|
| 123 |
nodes=cmds.get("nodes"),
|
| 124 |
movetime=cmds.get("movetime")
|
lishogi-bot.py
CHANGED
|
@@ -196,9 +196,9 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 196 |
|
| 197 |
ponder_usi = None
|
| 198 |
|
| 199 |
-
def ponder_thread_func(game, engine, board,
|
| 200 |
global ponder_results
|
| 201 |
-
best_move, ponder_move = engine.search_with_ponder(game, board,
|
| 202 |
ponder_results[game.id] = ( best_move, ponder_move )
|
| 203 |
|
| 204 |
engine.set_time_control(game)
|
|
@@ -218,16 +218,16 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 218 |
if not is_game_over(game) and is_engine_move(game, moves):
|
| 219 |
best_move = None
|
| 220 |
ponder_move = None
|
| 221 |
-
wtime = game.state["wtime"]
|
| 222 |
btime = game.state["btime"]
|
|
|
|
| 223 |
start_time = time.perf_counter_ns()
|
| 224 |
|
| 225 |
if board.turn == shogi.BLACK:
|
| 226 |
-
wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
|
| 227 |
-
else:
|
| 228 |
btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
| 231 |
engine.print_stats()
|
| 232 |
|
| 233 |
if is_usi_ponder and not ( ponder_move is None ):
|
|
@@ -237,11 +237,11 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 237 |
ponder_usi = ponder_move
|
| 238 |
|
| 239 |
if board.turn == shogi.BLACK:
|
| 240 |
-
wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + game.state["winc"])
|
| 241 |
-
else:
|
| 242 |
btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + game.state["binc"])
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
| 245 |
ponder_thread.start()
|
| 246 |
li.make_move(game.id, best_move)
|
| 247 |
time.sleep(delay_seconds)
|
|
@@ -270,18 +270,18 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 270 |
best_move = None
|
| 271 |
ponder_move = None
|
| 272 |
|
| 273 |
-
wtime = upd["wtime"]
|
| 274 |
btime = upd["btime"]
|
|
|
|
| 275 |
start_time = time.perf_counter_ns()
|
| 276 |
|
| 277 |
if not deferredFirstMove:
|
| 278 |
if best_move == None:
|
| 279 |
if board.turn == shogi.BLACK:
|
| 280 |
-
wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
|
| 281 |
-
else:
|
| 282 |
btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
|
| 283 |
-
|
| 284 |
-
|
|
|
|
|
|
|
| 285 |
engine.print_stats()
|
| 286 |
|
| 287 |
if is_usi_ponder and not ( ponder_move is None ):
|
|
@@ -290,11 +290,11 @@ def play_game(li, game_id, control_queue, engine_factory, user_profile, config,
|
|
| 290 |
ponder_board.push(shogi.Move.from_usi(ponder_move))
|
| 291 |
ponder_usi = ponder_move
|
| 292 |
if board.turn == shogi.BLACK:
|
| 293 |
-
wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + upd["winc"])
|
| 294 |
-
else:
|
| 295 |
btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + upd["binc"])
|
| 296 |
-
|
| 297 |
-
|
|
|
|
|
|
|
| 298 |
ponder_thread.start()
|
| 299 |
li.make_move(game.id, best_move)
|
| 300 |
else:
|
|
@@ -362,7 +362,7 @@ def print_move_number(board):
|
|
| 362 |
|
| 363 |
def setup_board(game):
|
| 364 |
if game.variant_name == "From Position":
|
| 365 |
-
board = shogi.Board(
|
| 366 |
else:
|
| 367 |
board = shogi.Board() # Standard
|
| 368 |
moves = game.state["moves"].split()
|
|
@@ -372,12 +372,12 @@ def setup_board(game):
|
|
| 372 |
return board
|
| 373 |
|
| 374 |
|
| 375 |
-
def
|
| 376 |
-
return len(moves) % 2 == (0 if game.
|
| 377 |
|
| 378 |
|
| 379 |
def is_engine_move(game, moves):
|
| 380 |
-
return game.
|
| 381 |
|
| 382 |
|
| 383 |
def is_game_over(game):
|
|
|
|
| 196 |
|
| 197 |
ponder_usi = None
|
| 198 |
|
| 199 |
+
def ponder_thread_func(game, engine, board, btime, wtime, binc, winc, byo):
|
| 200 |
global ponder_results
|
| 201 |
+
best_move, ponder_move = engine.search_with_ponder(game, board, btime, wtime, binc, winc, byo, True)
|
| 202 |
ponder_results[game.id] = ( best_move, ponder_move )
|
| 203 |
|
| 204 |
engine.set_time_control(game)
|
|
|
|
| 218 |
if not is_game_over(game) and is_engine_move(game, moves):
|
| 219 |
best_move = None
|
| 220 |
ponder_move = None
|
|
|
|
| 221 |
btime = game.state["btime"]
|
| 222 |
+
wtime = game.state["wtime"]
|
| 223 |
start_time = time.perf_counter_ns()
|
| 224 |
|
| 225 |
if board.turn == shogi.BLACK:
|
|
|
|
|
|
|
| 226 |
btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
|
| 227 |
+
else:
|
| 228 |
+
wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
|
| 229 |
+
logger.info(" Searching for btime {} wtime {}".format(btime, wtime))
|
| 230 |
+
best_move, ponder_move = engine.search_with_ponder(game, board, btime, wtime, game.state["binc"], game.state["winc"], game.state["byo"])
|
| 231 |
engine.print_stats()
|
| 232 |
|
| 233 |
if is_usi_ponder and not ( ponder_move is None ):
|
|
|
|
| 237 |
ponder_usi = ponder_move
|
| 238 |
|
| 239 |
if board.turn == shogi.BLACK:
|
|
|
|
|
|
|
| 240 |
btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + game.state["binc"])
|
| 241 |
+
else:
|
| 242 |
+
wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + game.state["winc"])
|
| 243 |
+
logger.info(" Pondering for btime {} wtime {}".format(btime, wtime))
|
| 244 |
+
ponder_thread = threading.Thread(target=ponder_thread_func, args=(game, engine, ponder_board, btime, wtime, game.state["binc"], game.state["winc"], game.state["byo"]))
|
| 245 |
ponder_thread.start()
|
| 246 |
li.make_move(game.id, best_move)
|
| 247 |
time.sleep(delay_seconds)
|
|
|
|
| 270 |
best_move = None
|
| 271 |
ponder_move = None
|
| 272 |
|
|
|
|
| 273 |
btime = upd["btime"]
|
| 274 |
+
wtime = upd["wtime"]
|
| 275 |
start_time = time.perf_counter_ns()
|
| 276 |
|
| 277 |
if not deferredFirstMove:
|
| 278 |
if best_move == None:
|
| 279 |
if board.turn == shogi.BLACK:
|
|
|
|
|
|
|
| 280 |
btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
|
| 281 |
+
else:
|
| 282 |
+
wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000))
|
| 283 |
+
logger.info(" Searching for btime {} wtime {}".format(btime, wtime))
|
| 284 |
+
best_move, ponder_move = engine.search_with_ponder(game, board, btime, wtime, upd["binc"], upd["winc"], upd["byo"])
|
| 285 |
engine.print_stats()
|
| 286 |
|
| 287 |
if is_usi_ponder and not ( ponder_move is None ):
|
|
|
|
| 290 |
ponder_board.push(shogi.Move.from_usi(ponder_move))
|
| 291 |
ponder_usi = ponder_move
|
| 292 |
if board.turn == shogi.BLACK:
|
|
|
|
|
|
|
| 293 |
btime = max(0, btime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + upd["binc"])
|
| 294 |
+
else:
|
| 295 |
+
wtime = max(0, wtime - move_overhead - int((time.perf_counter_ns() - start_time) / 1000000) + upd["winc"])
|
| 296 |
+
logger.info(" Pondering for btime {} wtime {}".format(btime, wtime))
|
| 297 |
+
ponder_thread = threading.Thread(target=ponder_thread_func, args=(game, engine, ponder_board, btime, wtime, upd["binc"], upd["winc"], upd["byo"]))
|
| 298 |
ponder_thread.start()
|
| 299 |
li.make_move(game.id, best_move)
|
| 300 |
else:
|
|
|
|
| 362 |
|
| 363 |
def setup_board(game):
|
| 364 |
if game.variant_name == "From Position":
|
| 365 |
+
board = shogi.Board(game.initial_fen)
|
| 366 |
else:
|
| 367 |
board = shogi.Board() # Standard
|
| 368 |
moves = game.state["moves"].split()
|
|
|
|
| 372 |
return board
|
| 373 |
|
| 374 |
|
| 375 |
+
def is_sente_to_move(game, moves):
|
| 376 |
+
return len(moves) % 2 == (0 if game.sente_starts else 1)
|
| 377 |
|
| 378 |
|
| 379 |
def is_engine_move(game, moves):
|
| 380 |
+
return game.is_sente == is_sente_to_move(game, moves)
|
| 381 |
|
| 382 |
|
| 383 |
def is_game_over(game):
|
model.py
CHANGED
|
@@ -75,17 +75,17 @@ class Game:
|
|
| 75 |
self.clock_increment = clock.get("increment", 0)
|
| 76 |
self.perf_name = json.get("perf").get("name") if json.get("perf") else "{perf?}"
|
| 77 |
self.variant_name = json.get("variant")["name"]
|
| 78 |
-
self.
|
| 79 |
-
self.
|
| 80 |
self.initial_fen = json.get("initialFen")
|
| 81 |
self.state = json.get("state")
|
| 82 |
-
self.
|
| 83 |
-
self.my_color = "
|
| 84 |
-
self.opponent_color = "
|
| 85 |
-
self.me = self.
|
| 86 |
-
self.opponent = self.
|
| 87 |
self.base_url = base_url
|
| 88 |
-
self.
|
| 89 |
self.abort_at = time.time() + abort_time
|
| 90 |
self.terminate_at = time.time() + (self.clock_initial + self.clock_increment) / 1000 + abort_time + 60
|
| 91 |
|
|
@@ -107,7 +107,7 @@ class Game:
|
|
| 107 |
return time.time() > self.terminate_at
|
| 108 |
|
| 109 |
def my_remaining_seconds(self):
|
| 110 |
-
return (self.state["
|
| 111 |
|
| 112 |
def __str__(self):
|
| 113 |
return "{} {} vs {}".format(self.url(), self.perf_name, self.opponent.__str__())
|
|
|
|
| 75 |
self.clock_increment = clock.get("increment", 0)
|
| 76 |
self.perf_name = json.get("perf").get("name") if json.get("perf") else "{perf?}"
|
| 77 |
self.variant_name = json.get("variant")["name"]
|
| 78 |
+
self.sente = Player(json.get("sente"))
|
| 79 |
+
self.gote = Player(json.get("gote"))
|
| 80 |
self.initial_fen = json.get("initialFen")
|
| 81 |
self.state = json.get("state")
|
| 82 |
+
self.is_sente = bool(self.sente.name and self.sente.name.lower() == username.lower())
|
| 83 |
+
self.my_color = "sente" if self.is_sente else "gote"
|
| 84 |
+
self.opponent_color = "gote" if self.is_sente else "sente"
|
| 85 |
+
self.me = self.sente if self.is_sente else self.gote
|
| 86 |
+
self.opponent = self.gote if self.is_sente else self.sente
|
| 87 |
self.base_url = base_url
|
| 88 |
+
self.sente_starts = self.initial_fen == "startpos" or self.initial_fen.split()[1] == "b"
|
| 89 |
self.abort_at = time.time() + abort_time
|
| 90 |
self.terminate_at = time.time() + (self.clock_initial + self.clock_increment) / 1000 + abort_time + 60
|
| 91 |
|
|
|
|
| 107 |
return time.time() > self.terminate_at
|
| 108 |
|
| 109 |
def my_remaining_seconds(self):
|
| 110 |
+
return (self.state["btime"] if self.is_sente else self.state["wtime"]) / 1000
|
| 111 |
|
| 112 |
def __str__(self):
|
| 113 |
return "{} {} vs {}".format(self.url(), self.perf_name, self.opponent.__str__())
|
util.py
CHANGED
|
@@ -26,47 +26,4 @@ def makeuci(move) -> str:
|
|
| 26 |
if(isusi(move)):
|
| 27 |
return switchusiuci(move)
|
| 28 |
else:
|
| 29 |
-
return move
|
| 30 |
-
|
| 31 |
-
# sfen, fen
|
| 32 |
-
|
| 33 |
-
# replaces color, black goes first in shogi, but chesslike backend still assumes white goes first
|
| 34 |
-
def fixColor(fen) -> str:
|
| 35 |
-
splitted = fen.split(' ')
|
| 36 |
-
if len(splitted) > 1:
|
| 37 |
-
color = splitted[1]
|
| 38 |
-
if color == "b":
|
| 39 |
-
splitted[1] = "w"
|
| 40 |
-
else:
|
| 41 |
-
splitted[1] = "b"
|
| 42 |
-
return " ".join(splitted)
|
| 43 |
-
|
| 44 |
-
# Changing backend representation of pieces to normal representation
|
| 45 |
-
def fixPosition(position) -> str:
|
| 46 |
-
position = position.replace("U", "+L").replace("M", "+N").replace("A", "+S").replace("D", "+R").replace("H", "+B").replace("T", "+P").replace("u", "+l").replace("m", "+n").replace("a", "+s").replace("d", "+r").replace("h", "+b").replace("t", "+p")
|
| 47 |
-
return fixPocket(position)
|
| 48 |
-
|
| 49 |
-
# Orders pocket properly (pppr -> r3p)
|
| 50 |
-
def fixPocket(sfen) -> str:
|
| 51 |
-
splitted = sfen.split(' ')
|
| 52 |
-
if(len(splitted) > 2 and splitted[2] != '-'):
|
| 53 |
-
# rook, bishop, gold, silver, knight, lance, pawn
|
| 54 |
-
pieceOrder = "RBGSNLPrbgsnlp"
|
| 55 |
-
pocket = ""
|
| 56 |
-
for i in "RBGSNLPrbgsnlp":
|
| 57 |
-
c = splitted[2].count(i)
|
| 58 |
-
if c == 0:
|
| 59 |
-
pass
|
| 60 |
-
elif c == 1:
|
| 61 |
-
pocket += i
|
| 62 |
-
else:
|
| 63 |
-
pocket += str(c) + i
|
| 64 |
-
|
| 65 |
-
splitted[2] = pocket
|
| 66 |
-
return ' '.join(splitted)
|
| 67 |
-
|
| 68 |
-
# if called on sfen, it will switch starting color
|
| 69 |
-
def makesfenfromfen(fen) -> str:
|
| 70 |
-
if(fen == "startpos"):
|
| 71 |
-
return fen
|
| 72 |
-
return fixColor(fixPocket(fixPosition(fen)))
|
|
|
|
| 26 |
if(isusi(move)):
|
| 27 |
return switchusiuci(move)
|
| 28 |
else:
|
| 29 |
+
return move
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|