File size: 10,099 Bytes
bfceb5b
 
 
 
 
 
 
 
 
 
faf38e6
bfceb5b
 
 
 
 
42398ba
bfceb5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdae82e
42398ba
 
 
 
 
 
98409a9
 
 
 
3d8e222
cdae82e
98409a9
 
 
3d8e222
cdae82e
bfceb5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba6b37a
bfceb5b
 
ba6b37a
 
bfceb5b
 
ba6b37a
bfceb5b
 
ba6b37a
bfceb5b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
faf38e6
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import threading
import subprocess
import os
import signal
import logging

logger = logging.getLogger(__name__)


class Engine:
    def __init__(self, command, cwd=None):
        self.info = {}
        self.id = {}
        cwd = cwd or os.path.realpath(os.path.expanduser("."))
        self.proccess = self.open_process(command, cwd)
        self.go_commands = None
        self.current_variant = None 

    def set_go_commands(self, go_comm):
        self.go_commands = go_comm
        logger.info(self.go_commands)

    def open_process(self, command, cwd=None, shell=True, _popen_lock=threading.Lock()):
        kwargs = {
            "shell": shell,
            "stdout": subprocess.PIPE,
            "stderr": subprocess.STDOUT,
            "stdin": subprocess.PIPE,
            "bufsize": 1,  # Line buffered
            "universal_newlines": True,
        }

        if cwd is not None:
            kwargs["cwd"] = cwd

        # Prevent signal propagation from parent process
        try:
            # Windows
            kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP
        except AttributeError:
            # Unix
            kwargs["preexec_fn"] = os.setpgrp

        with _popen_lock:  # Work around Python 2 Popen race condition
            return subprocess.Popen(command, **kwargs)

    def kill_process(self):
        try:
            # Windows
            self.proccess.send_signal(signal.CTRL_BREAK_EVENT)
        except AttributeError:
            # Unix
            os.killpg(self.proccess.pid, signal.SIGKILL)

    def send(self, line):
        logger.debug(f"<< {line}")
        assert self.proccess.stdin is not None
        self.proccess.stdin.write(line + "\n")
        self.proccess.stdin.flush()

    def recv(self):
        while True:
            assert self.proccess.stdout is not None
            line = self.proccess.stdout.readline()
            if line == "":
                raise EOFError()

            line = line.rstrip()

            logger.debug(f">> {line}")

            if line:
                return line

    def recv_usi(self):
        command_and_args = self.recv().split(None, 1)
        if len(command_and_args) == 1:
            return command_and_args[0], ""
        elif len(command_and_args) == 2:
            return command_and_args

    def usi(self):
        self.send("usi")

        engine_info = {}

        while True:
            command, arg = self.recv_usi()

            if command == "usiok":
                return engine_info
            elif command == "id":
                name_and_value = arg.split(None, 1)
                if len(name_and_value) == 2:
                    engine_info[name_and_value[0]] = name_and_value[1]
            elif command == "option":
                pass
            else:
                logger.warning("Unexpected engine response to usi: %s %s" % (command, arg))
            self.id = engine_info

    def isready(self):
        self.send("isready")
        while True:
            command, arg = self.recv_usi()
            if command == "readyok":
                break
            elif command == "info" and arg.startswith("string Error! "):
                logger.error("Unexpected engine response to isready: %s %s" % (command, arg))
            elif command == "info" and arg.startswith("string "):
                pass
            else:
                logger.warning("Unexpected engine response to isready: %s %s" % (command, arg))

    def setoption(self, name, value):
        if value is True:
            value = "true"
        elif value is False:
            value = "false"
        elif value is None:
            value = "none"

        self.send("setoption name %s value %s" % (name, value))

    def set_variant_options(self, variant):
        # Some engines may unnecessarily reset board when selecting a variant
        if self.current_variant == variant: 
            return 
        
        self.current_variant = variant 

        if "fairy-stockfish" in self.id.get("name", "").lower():
            if variant in ["standard"]:
                self.setoption("UCI_Variant", "shogi")
            else:
                self.setoption("UCI_Variant", variant.replace(" ", ""))
        else:
            if variant in ["standard"]:
                self.setoption("USI_Variant", "shogi")
            else:
                self.setoption("USI_Variant", variant.replace(" ", ""))

    def go(self, position, moves, movetime=None, btime=None, wtime=None, binc=None, winc=None, byo=None, depth=None, nodes=None, ponder=False):
        self.position(position, moves)

        builder = []
        builder.append("go")
        if ponder:
            builder.append("ponder")
        if movetime is not None:
            builder.append("movetime")
            builder.append(str(movetime))
        if nodes is not None:
            builder.append("nodes")
            builder.append(str(nodes))
        if depth is not None:
            builder.append("depth")
            builder.append(str(depth))
        # In Shogi and USI, black is the player to move first
        if btime is not None:
            builder.append("btime")
            builder.append(str(max(btime - byo - binc, 0)))
        if wtime is not None:
            builder.append("wtime")
            builder.append(str(max(wtime - byo - winc, 0)))
        if binc is not None and binc > 0:
            builder.append("binc")
            builder.append(str(binc))
        if winc is not None and winc > 0:
            builder.append("winc")
            builder.append(str(winc))
        if byo is not None and byo > 0:
            builder.append("byoyomi")
            builder.append(str(byo))

        self.send(" ".join(builder))
        logger.info(" ".join(builder))

        info = {}
        info["bestmove"] = None
        info["pondermove"] = None

        while True:
            command, arg = self.recv_usi()

            if command == "bestmove":
                arg_split = arg.split()
                bestmove = arg_split[0]
                if bestmove and bestmove != "(none)":
                    info["bestmove"] = bestmove
                if len(arg_split) == 3:
                    if arg_split[1] == "ponder":
                        ponder_move = arg_split[2]
                        if ponder_move and ponder_move != "(none)":
                            info["pondermove"] = ponder_move
                return (info["bestmove"], info["pondermove"])

            elif command == "info":
                arg = arg or ""

                # Parse all other parameters
                score_kind, score_value, lowerbound, upperbound = None, None, False, False
                current_parameter = None
                for token in arg.split(" "):
                    if current_parameter == "string":
                        # Everything until the end of line is a string
                        if "string" in info:
                            info["string"] += " " + token
                        else:
                            info["string"] = token
                    elif token == "score":
                        current_parameter = "score"
                    elif token == "pv":
                        current_parameter = "pv"
                        if info.get("multipv", 1) == 1:
                            info.pop("pv", None)
                    elif token in ["depth", "seldepth", "time", "nodes", "multipv",
                                "currmove", "currmovenumber",
                                "hashfull", "nps", "tbhits", "cpuload",
                                "refutation", "currline", "string"]:
                        current_parameter = token
                        info.pop(current_parameter, None)
                    elif current_parameter in ["depth", "seldepth", "time",
                                            "nodes", "currmovenumber",
                                            "hashfull", "nps", "tbhits",
                                            "cpuload", "multipv"]:
                        # Integer parameters
                        info[current_parameter] = int(token)
                    elif current_parameter == "score":
                        # Score
                        if token in ["cp", "mate"]:
                            score_kind = token
                            score_value = None
                        elif token == "lowerbound":
                            lowerbound = True
                        elif token == "upperbound":
                            upperbound = True
                        else:
                            score_value = int(token)
                    elif current_parameter != "pv" or info.get("multipv", 1) == 1:
                        # Strings
                        if current_parameter in info:
                            info[current_parameter] += " " + token
                        else:
                            info[current_parameter] = token

                # Set score. Prefer scores that are not just a bound
                if score_kind and score_value is not None and (not (lowerbound or upperbound) or "score" not in info or info["score"].get("lowerbound") or info["score"].get("upperbound")):
                    info["score"] = {score_kind: score_value}
                    if lowerbound:
                        info["score"]["lowerbound"] = lowerbound
                    if upperbound:
                        info["score"]["upperbound"] = upperbound
                self.info = info
            else:
                logger.error("Unexpected engine response to go: %s %s" % (command, arg))

    def position(self, position, moves):
        if position != "startpos":
            position = "sfen " + position
        self.send("position %s moves %s" % (position, " ".join(moves)))
        logger.debug("position %s moves %s" % (position, " ".join(moves)))

    def stop(self):
        self.send("stop")

    def ponderhit(self):
        self.send("ponderhit")
        logger.info("ponderhit")

    def quit(self):
        self.send("quit")