forked from jmgilmer/GoCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
110 lines (93 loc) · 3.51 KB
/
server.py
File metadata and controls
110 lines (93 loc) · 3.51 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
# -*- coding: utf-8 -*-
import xlrd
import time
import pandas
import gomill.sgf
import numpy as np
from visualization.go_driver import GoDriver
from board_evaluation.pachi_player import Pachi
# set default board size.
BOARD_SIZE = 9
pachi = Pachi(pachi_path="thirdparty/pachi-pachi-12.10-jowa/pachi")
godriver = GoDriver("data/working/cnn_5layer_64filter", board_size=BOARD_SIZE)
def parse_sgf_file(sgf_content):
while 'AB' in sgf_content:
pos_b = sgf_content.find('AB')
sgf_content = to_sequence(sgf_content, pos_b, ';B')
while 'AW' in sgf_content:
pos_w = sgf_content.find('AW')
sgf_content = to_sequence(sgf_content, pos_w, ';W')
return sgf_content
def to_sequence(sgf_content, pos, prefix):
pos += 1
start_pos = pos + 1
while pos + 4 < len(sgf_content) and sgf_content[pos + 4] == ']':
pos += 4
end_pos = pos + 1
sgf_string = sgf_content[start_pos: end_pos]
res = ""
for i in range(0, len(sgf_string), 4):
res += prefix + sgf_string[i: i + 4]
res = sgf_content[: start_pos - 2] + res + sgf_content[end_pos:]
return res
def board_eval(sgf_content):
# reset go board
# skip komi, we will handle this later
godriver.reset_board()
sgf_content = parse_sgf_file(sgf_content)
try:
sgf = gomill.sgf.Sgf_game.from_string(sgf_content)
except ValueError:
print('WARNING: no SGF data found')
# if this is not a sgf file, we return blank command
return np.zeros((BOARD_SIZE, BOARD_SIZE))
sgf_iterator = sgf.main_sequence_iter()
while True:
try:
it = sgf_iterator.next()
color, move = it.get_move()
if color is None:
it = sgf_iterator.next()
color, move = it.get_move()
except StopIteration:
break
if move is not None:
godriver.play(color, move)
# scale the value range to [-1, 1]
nn_matrix = godriver.evaluate_current_board() * 2 - 1
# pachi player is used to reinforcement the neural network
pachi_matrix, score = pachi.get_final_score_matrix(sgf_content)
if pachi_matrix is None:
pachi_matrix = nn_matrix
else:
assert len(pachi_matrix) == BOARD_SIZE ** 2
pachi_matrix = np.array(pachi_matrix).reshape(BOARD_SIZE, BOARD_SIZE)
final_matrix = 0.1 * nn_matrix + 0.9 * pachi_matrix
# (0, 0) -> A1, (0, 8) ->I1, (8, 8)->I9
return final_matrix
if __name__ == '__main__':
# readbook = xlrd.open_workbook("./data/kifu_test/test9x9.xlsx")
# sheet = readbook.sheet_by_index(0)
# start_time = time.time()
# for i in range(sheet.nrows):
# sgf_content = sheet.cell(i, 3).value.encode('utf-8')
# try:
# score = board_eval(sgf_content)
# print("sgf id {} score {}".format(i, np.sum(score)))
# except Exception as e:
# print("sgf id {} exception {}".format(i, e))
# if i % 100 == 0:
# print("time used: {}s".format(time.time() - start_time))
sql = pandas.read_csv("./data/kifu_test/test_sql.csv")
sgfs = sql["Sgf"]
print("sgf nums {}".format(sgfs.size))
start_time = time.time()
for i, f in enumerate(sgfs):
sgf_content = f.encode('utf-8')
try:
score = board_eval(sgf_content)
print("sgf id {} score {}".format(i, np.sum(score)))
except Exception as e:
print("sgf id {} exception {}".format(i, e))
if i % 100 == 0:
print("time used: {}s".format(time.time() - start_time))