forked from lichess-bot-devs/lichess-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathengine_wrapper.py
135 lines (102 loc) · 3.89 KB
/
engine_wrapper.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import chess
import chess.xboard
import chess.uci
def create_engine(config, board):
# print("Loading Engine!")
cfg = config["engine"]
engine_path = os.path.join(cfg["dir"], cfg["name"])
weights = os.path.join(cfg["dir"], cfg["weights"]) if "weights" in cfg else None
threads = cfg.get("threads")
# TODO: ucioptions should probably be a part of the engine subconfig
ucioptions = config.get("ucioptions")
engine_type = cfg.get("protocol")
commands = [engine_path]
if weights:
commands.append("-w")
commands.append(weights)
if threads:
commands.append("-t")
commands.append(str(threads))
if engine_type == "xboard":
return XBoardEngine(board, commands)
return UCIEngine(board, commands, ucioptions)
class EngineWrapper:
def __init__(self, board, commands):
pass
def pre_game(self, game):
pass
def first_search(self, movetime):
pass
def search(self, board, wtime, btime, winc, binc):
pass
def print_stats(self):
pass
def name(self):
return self.engine.name
def quit(self):
self.engine.quit()
def print_handler_stats(self, info, stats):
for stat in stats:
if stat in info:
print(" {}: {}".format(stat, info[stat]))
class XBoardEngine(EngineWrapper):
def __init__(self, board, commands):
commands = commands[0] if len(commands) == 1 else commands
self.engine = chess.xboard.popen_engine(commands)
self.engine.xboard()
if board.chess960:
self.engine.send_variant("fischerandom")
elif type(board).uci_variant != "chess":
self.engine.send_variant(type(board).uci_variant)
self.engine.setboard(board)
post_handler = chess.xboard.PostHandler()
self.engine.post_handlers.append(post_handler)
def pre_game(self, game):
minutes = game.clock_initial / 1000 / 60
seconds = game.clock_initial / 1000 % 60
inc = game.clock_increment / 1000
self.engine.level(0, minutes, seconds, inc)
def first_search(self, board, movetime):
self.engine.setboard(board)
self.engine.st(movetime / 1000)
return self.engine.go()
def search(self, board, wtime, btime, winc, binc):
self.engine.setboard(board)
if board.turn == chess.WHITE:
self.engine.time(wtime / 10)
self.engine.otim(btime / 10)
else:
self.engine.time(btime / 10)
self.engine.otim(wtime / 10)
return self.engine.go()
def print_stats(self):
self.print_handler_stats(self.engine.post_handlers[0].post, ["depth", "nodes", "score"])
class UCIEngine(EngineWrapper):
def __init__(self, board, commands, options):
commands = commands[0] if len(commands) == 1 else commands
self.engine = chess.uci.popen_engine(commands)
self.engine.uci()
if options:
self.engine.setoption(options)
self.engine.setoption({"UCI_Variant": type(board).uci_variant})
self.engine.position(board)
info_handler = chess.uci.InfoHandler()
self.engine.info_handlers.append(info_handler)
def first_search(self, board, movetime):
self.engine.setoption({"UCI_Variant": type(board).uci_variant})
self.engine.position(board)
best_move, _ = self.engine.go(movetime=movetime)
return best_move
def search(self, board, wtime, btime, winc, binc):
self.engine.setoption({"UCI_Variant": type(board).uci_variant})
self.engine.position(board)
best_move, _ = self.engine.go(
wtime=wtime,
btime=btime,
winc=winc,
binc=binc
)
return best_move
def print_stats(self):
self.print_handler_stats(self.engine.info_handlers[0].info, ["string", "depth", "nps", "nodes", "score"])