-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuct_engine.py
More file actions
170 lines (127 loc) · 5.67 KB
/
Copy pathuct_engine.py
File metadata and controls
170 lines (127 loc) · 5.67 KB
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
import sys
import gomoku
from engine import Engine
from error import GTPError
from mcts import UCTConfig, Searcher, SearchResult
class UCTEngine(Engine):
def __init__(self):
super().__init__("UCT Engine", "1.0")
self.__searcher = Searcher(UCTConfig())
self.__simulation_count = 100000
self.__time_limit_ms = 10**8
self.__num_moves_to_print = 5 # ログに上位何手までを表示するか
self.__original_commands ={
"set_simulations": self.__exec_set_simulations_command,
"set_time_limit_per_move": self.__exec_set_time_limit_per_move_command,
"set_num_moves_to_print": self.__exec_set_num_moves_to_print_command,
}
self.__init_root()
def clear_board(self):
super().clear_board()
self.__searcher.set_root_pos(self._pos)
def play(self, color, coord):
if not super().play(color, coord):
return False
if self.__searcher.try_update_root_pos(coord):
return True
self.__init_root()
return True
def undo(self):
if not super().undo():
return False
self.__init_root()
def set_board_size(self, size):
if not super().set_board_size(size):
return False
self.__init_root()
return True
def get_original_commands(self) -> list[str]:
return list(self.__original_commands.keys())
def exec_original_command(self, cmd, args):
if cmd in self.__original_commands:
return self.__original_commands[cmd](args)
else:
raise GTPError(f"Unknown command: {cmd}")
def gen_move(self) -> int:
if self._pos.winner != gomoku.IntersectionState.EMPTY or self._pos.empty_count == 0:
raise GTPError("Game is already over.")
if self._pos.empty_count == 1:
return self._pos.enumerate_empties().__next__()
result = self.__searcher.search(self.__simulation_count, self.__time_limit_ms)
# GTPの通信と競合するので,探索結果は標準エラー出力に出力する.
print(self.__search_result_to_str(result), file=sys.stderr)
move_evals = result.move_evals
return max(move_evals, key=lambda x: x.effort).move
def __init_root(self):
if self._pos.empty_count == 0:
return
move_history = list(filter(lambda x: x[1] != gomoku.PASS_COORD, self._move_history))
if len(move_history) == 0:
self.__searcher.set_root_pos(self._pos)
elif len(move_history) == 1:
self.__searcher.set_root_pos(self._pos, move_history[0][1])
else:
self.__searcher.set_root_pos(self._pos, move_history[-1][1], move_history[-2][1])
def __exec_set_simulations_command(self, args: list[str]) -> str:
if len(args) != 1:
raise GTPError("Invalid number of arguments for set_simulations command.")
try:
self.__simulation_count = int(args[0])
except ValueError:
raise GTPError("Invalid argument for set_simulations command. Must be an integer.")
return ""
def __exec_set_time_limit_per_move_command(self, args: list[str]) -> str:
if len(args) != 1:
raise GTPError("Invalid number of arguments for set_time_limit_per_move command.")
try:
self.__time_limit_ms = int(args[0])
except ValueError:
raise GTPError("Invalid argument for set_time_limit_per_move command. Must be an integer.")
return ""
def __exec_set_num_moves_to_print_command(self, args: list[str]) -> str:
if len(args) != 1:
raise GTPError("Invalid number of arguments for set_num_moves_to_print command.")
try:
self.__num_moves_to_print = int(args[0])
except ValueError:
raise GTPError("Invalid argument for set_num_moves_to_print command. Must be an integer.")
return ""
def __search_result_to_str(self, res: SearchResult) -> str:
s = []
s.append(f"elapsed={self.__searcher.elapsed_ms}[ms]\t{self.__searcher.simulation_count}[simulations]\t{self.__searcher.pps}[pps]\n")
s.append(f"win_rate={res.root_value.value * 100:.2f}%\n")
s.append("|move|effort|playouts|win_rate|depth|pv\n")
for eval in sorted(res.move_evals, key=lambda x: x.effort, reverse=True)[:min(self.__num_moves_to_print, len(res.move_evals))]:
s.append(f"|{self.__coord_to_str(eval.move).rjust(4)}|")
s.append(f"{eval.effort * 100.0:.2f}%".rjust(6))
s.append('|')
s.append(f"{eval.simulation_count:>8}|")
s.append(f"{eval.value * 100:.2f}%".rjust(8))
s.append('|')
s.append(f"{len(eval.pv):>5}")
s.append('|')
s.append(" ".join(self.__coord_to_str(c) for c in eval.pv))
s.append('\n')
return "".join(s)
def __coord_to_str(self, coord: int) -> str:
if coord == gomoku.PASS_COORD:
return "pass"
x = coord % self.board_size
y = coord // self.board_size
x_chr = chr(ord('A') + x)
if x_chr >= 'I':
x_chr = chr(ord(x_chr) + 1)
return f"{x_chr}{self.board_size - y}"
if __name__ == "__main__":
from gtp import GTP
from server_client import ServerClient
protocol = "gtp"
if len(sys.argv) > 1:
protocol = sys.argv[1]
engine = UCTEngine()
if protocol == "gtp":
gtp = GTP(engine)
gtp.mainloop("gtp.log")
elif protocol == "server":
server_client = ServerClient(engine)
server_client.mainloop("server.log")