forked from tensorflow/minigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtp_wrapper.py
132 lines (105 loc) · 3.9 KB
/
gtp_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
# Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import gtp
import gtp_extensions
import coords
import datetime
import go
import random
import sys
import os
from dual_net import DualNetwork
from strategies import MCTSPlayerMixin, CGOSPlayerMixin
def translate_gtp_colors(gtp_color):
if gtp_color == gtp.BLACK:
return go.BLACK
elif gtp_color == gtp.WHITE:
return go.WHITE
else:
return go.EMPTY
class GtpInterface(object):
def __init__(self):
self.size = 9
self.position = None
self.komi = 6.5
def set_size(self, n):
if n != go.N:
raise ValueError(("Can't handle boardsize {n}!"
"Restart with env var BOARD_SIZE={n}").format(n=n))
def set_komi(self, komi):
self.komi = komi
self.position.komi = komi
def clear(self):
if self.position and len(self.position.recent) > 1:
try:
sgf = self.to_sgf()
with open(datetime.datetime.now().strftime("%Y-%m-%d-%H:%M.sgf"), 'w') as f:
f.write(sgf)
except NotImplementedError:
pass
except:
print("Error saving sgf", file=sys.stderr, flush=True)
self.position = go.Position(komi=self.komi)
self.initialize_game(self.position)
def accomodate_out_of_turn(self, color):
if not translate_gtp_colors(color) == self.position.to_play:
self.position.flip_playerturn(mutate=True)
def make_move(self, color, vertex):
c = coords.from_pygtp(vertex)
# let's assume this never happens for now.
# self.accomodate_out_of_turn(color)
return self.play_move(c)
def get_move(self, color):
self.accomodate_out_of_turn(color)
move = self.suggest_move(self.position)
if self.should_resign():
return gtp.RESIGN
return coords.to_pygtp(move)
def final_score(self):
return self.position.result_string()
def showboard(self):
print('\n\n' + str(self.position) + '\n\n', file=sys.stderr)
return True
def should_resign(self):
raise NotImplementedError
def get_score(self):
return self.position.result_string()
def suggest_move(self, position):
raise NotImplementedError
def play_move(self, c):
raise NotImplementedError
def initialize_game(self):
raise NotImplementedError
def chat(self, msg_type, sender, text):
raise NotImplementedError
def to_sgf(self):
raise NotImplementedError
class MCTSPlayer(MCTSPlayerMixin, GtpInterface):
pass
class CGOSPlayer(CGOSPlayerMixin, GtpInterface):
pass
def make_gtp_instance(read_file, readouts_per_move=100, verbosity=1, cgos_mode=False):
n = DualNetwork(read_file)
instance = MCTSPlayer(n, simulations_per_move=readouts_per_move,
verbosity=verbosity, two_player_mode=True)
gtp_engine = gtp.Engine(instance)
if cgos_mode:
instance = CGOSPlayer(n, seconds_per_move=5,
verbosity=verbosity, two_player_mode=True)
else:
instance = MCTSPlayer(n, simulations_per_move=readouts_per_move,
verbosity=verbosity, two_player_mode=True)
name = "Somebot-" + os.path.basename(read_file)
gtp_engine = gtp_extensions.GTPDeluxe(instance, name=name)
return gtp_engine