File size: 4,482 Bytes
4ab541b
90ee17c
 
 
 
f01cd90
 
 
5637bd2
90ee17c
3d8d542
4fda15b
90ee17c
3ebd8a3
bc0a359
2b66c9f
90ee17c
039909f
bc0a359
039909f
 
 
bc0a359
4fdf090
3ebd8a3
6155768
90ee17c
 
 
 
 
 
d418201
90ee17c
 
 
 
 
192c2bf
90ee17c
 
 
 
 
 
 
 
 
 
 
f01cd90
 
 
90ee17c
 
 
522f652
 
90ee17c
522f652
 
90ee17c
 
 
19325a4
6155768
 
2919001
 
 
 
 
c31cb65
2919001
6a55b6d
90ee17c
 
 
 
 
192c2bf
90ee17c
6155768
 
 
cea9531
6155768
 
 
 
7e681ca
6155768
 
 
cea9531
6155768
 
192c2bf
6155768
192c2bf
286409d
7e681ca
6155768
90ee17c
 
192c2bf
90ee17c
 
 
cea9531
90ee17c
 
192c2bf
90ee17c
192c2bf
90ee17c
 
 
 
 
 
 
 
 
522f652
 
 
 
 
 
 
90ee17c
522f652
 
 
 
 
 
 
 
 
90ee17c
 
 
 
 
 
 
5fba045
f01cd90
6a55b6d
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
import os
import shogi
import backoff
import subprocess
from util import *
import logging

logger = logging.getLogger(__name__)

import engine_ctrl


@backoff.on_exception(backoff.expo, BaseException, max_time=120)
def create_engine(config, board):
    cfg = config["engine"]
    engine_path = os.path.realpath(os.path.join(cfg["dir"], cfg["name"]))
    engine_type = cfg.get("protocol")
    engine_options = cfg.get("engine_options")
    commands = [engine_path]
    if engine_options:
        for k, v in engine_options.items():
            commands.append("--{}={}".format(k, v))

    silence_stderr = cfg.get("silence_stderr", False)

    return USIEngine(board, commands, cfg.get("usi_options", {}), cfg.get("go_commands", {}), silence_stderr)


class EngineWrapper:
    def __init__(self, board, commands, options=None, silence_stderr=False):
        pass

    def search_for(self, board, movetime):
        pass

    def first_search(self, board, movetime):
        pass

    def search(self, game, board, btime, wtime, binc, winc):
        pass

    def print_stats(self):
        pass

    def get_opponent_info(self, game):
        pass

    def name(self):
        return self.engine.name

    def report_game_result(self, game, board):
        pass

    def quit(self):
        self.engine.kill_process()

    def print_handler_stats(self):
        pass

    def get_handler_stats(self):
        pass


class USIEngine(EngineWrapper):
    def __init__(self, board, commands, options, go_commands={}, silence_stderr=False):
        commands = commands[0] if len(commands) == 1 else commands        
        self.go_commands = go_commands

        self.engine = engine_ctrl.Engine(commands)
        self.engine.usi()

        if options:
            for name, value in options.items():
                self.engine.setoption(name, value)
        self.engine.isready()

    def first_search(self, board, movetime):
        best_move, _ = self.engine.go(board.sfen(), "", movetime=movetime)
        return best_move

    def search_with_ponder(self, game, board, btime, wtime, binc, winc, byo, ponder=False):
        moves = [m.usi() for m in list(board.move_stack)]
        cmds = self.go_commands        
        if len(cmds) > 0:
               best_move, ponder_move = self.engine.go(
                   game.initial_fen,
                   moves,
                   nodes=cmds.get("nodes"),
                   depth=cmds.get("depth"),
                   movetime=cmds.get("movetime"),
                   ponder=ponder
               )
        else:
               best_move, ponder_move = self.engine.go(
                   game.initial_fen,
                   moves,
                   btime=btime,
                   wtime=wtime,
                   binc=binc,
                   winc=winc,
                   byo=byo,
                   ponder=ponder
               )
        return (best_move, ponder_move)

    def search(self, game, board, btime, wtime, binc, winc):
        cmds = self.go_commands
        moves = [m.usi() for m in list(board.move_stack)]
        best_move, _ = self.engine.go(
            game.initial_fen,
            moves,
            btime=btime,
            wtime=wtime,
            binc=binc,
            winc=winc,
            depth=cmds.get("depth"),
            nodes=cmds.get("nodes"),
            movetime=cmds.get("movetime")
        )
        return best_move

    def stop(self):
        self.engine.kill_process()

    def print_stats(self, stats=None):
        if stats is None:
            stats = ['score', 'depth', 'nodes', 'nps']
        info = self.engine.info
        for stat in stats:
            if stat in info:
                logger.info("{}: {}".format(stat, info[stat]))

    def get_stats(self, stats=None):
        if stats is None:
            stats = ['score', 'depth', 'nodes', 'nps']
        info = self.engine.info
        stats_str = []
        for stat in stats:
            if stat in info:
                stats_str.append("{}: {}".format(stat, info[stat]))
        return stats_str

    def get_opponent_info(self, game):
        name = game.opponent.name
        if name:
            rating = game.opponent.rating if game.opponent.rating is not None else "none"
            title = game.opponent.title if game.opponent.title else "none"
            player_type = "computer" if title == "BOT" else "human"
    
    def report_game_result(self, game, board):
        self.engine.protocol._position(board)