-
Notifications
You must be signed in to change notification settings - Fork 16
/
gtp.py
executable file
·122 lines (97 loc) · 3.49 KB
/
gtp.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
#!/usr/bin/env python
import sys
from conf import conf
from play import game_init
from engine import ModelEngine, COLOR_TO_PLAYER
from model import load_best_model
import string
import os
from __init__ import __version__
SIZE = conf['SIZE']
class Engine(object):
def __init__(self, model, logfile):
self.board, self.player = game_init()
self.start_engine(model)
self.logfile = logfile
def start_engine(self, model):
self.engine = ModelEngine(model, conf['MCTS_SIMULATIONS'], self.board)
def name(self):
return "AlphaGoZero Python - {} - {} simulations".format(self.engine.model.name, conf['MCTS_SIMULATIONS'])
def version(self):
return __version__
def protocol_version(self):
return "2"
def list_commands(self):
return ""
def boardsize(self, size):
size = int(size)
if size != SIZE:
raise Exception("The board size in configuration is {0}x{0} but GTP asked to play {1}x{1}".format(SIZE, size))
return ""
def komi(self, komi):
# Don't check komi in GTP engine. The algorithm has learned with a specific
# komi that we don't have any way to influence after learning.
return ""
def parse_move(self, move):
if move.lower() == 'pass':
x, y = 0, SIZE
return x, y
else:
letter = move[0]
number = move[1:]
x = string.ascii_uppercase.index(letter)
if x >= 9:
x -= 1 # I is a skipped letter
y = int(number) - 1
x, y = x, SIZE - y - 1
return x, y
def print_move(self, x, y):
x, y = x, SIZE - y - 1
if x >= 8:
x += 1 # I is a skipped letter
move = string.ascii_uppercase[x] + str(y + 1)
return move
def play(self, color, move):
announced_player = COLOR_TO_PLAYER[color]
assert announced_player == self.player
x, y = self.parse_move(move)
self.board, self.player = self.engine.play(color, x, y)
return ""
def genmove(self, color):
announced_player = COLOR_TO_PLAYER[color]
assert announced_player == self.player
x, y, policy_target, value, self.board, self.player, policy = self.engine.genmove(color)
self.player = self.board[0, 0, 0, -1] # engine updates self.board already
with open(self.logfile, 'a') as f:
f.write("PLAYER" + str(self.player) + '\n')
move_string = self.print_move(x, y)
result = move_string
return result
def clear_board(self):
self.board, self.player = game_init()
return ""
def parse_command(self, line):
tokens = line.strip().split(" ")
command = tokens[0]
args = tokens[1:]
method = getattr(self, command)
result = method(*args)
if not result.strip():
return "=\n\n"
return "= " + result + "\n\n"
def main():
model = load_best_model()
gtp_log = os.path.join(conf['ROOT_DIR'], conf['LOG_DIR'], conf['GTP_LOGFILE'])
engine = Engine(model, gtp_log)
with open(gtp_log, 'w') as f:
for line in sys.stdin:
f.write("<<<" + line)
result = engine.parse_command(line)
if result.strip():
sys.stdout.write(result)
sys.stdout.flush()
f.write("'''" + str(engine.player) + '\n')
f.write(">>>" + result)
f.flush()
if __name__ == "__main__":
main()