2017-12-15 22:19:44 +08:00
|
|
|
from __future__ import print_function
|
|
|
|
import utils
|
2017-11-04 01:45:55 +08:00
|
|
|
import copy
|
2017-12-20 01:14:05 +08:00
|
|
|
import numpy as np
|
2017-12-15 22:19:44 +08:00
|
|
|
from collections import deque
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-15 22:19:44 +08:00
|
|
|
'''
|
|
|
|
Settings of the Go game.
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-15 22:19:44 +08:00
|
|
|
(1, 1) is considered as the upper left corner of the board,
|
|
|
|
(size, 1) is the lower left
|
|
|
|
'''
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-15 22:19:44 +08:00
|
|
|
NEIGHBOR_OFFSET = [[1, 0], [-1, 0], [0, -1], [0, 1]]
|
2017-12-20 01:14:05 +08:00
|
|
|
CORNER_OFFSET = [[-1, -1], [-1, 1], [1, 1], [1, -1]]
|
2017-12-15 22:19:44 +08:00
|
|
|
|
|
|
|
class Go:
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.game = kwargs['game']
|
|
|
|
|
|
|
|
def _in_board(self, vertex):
|
|
|
|
x, y = vertex
|
|
|
|
if x < 1 or x > self.game.size: return False
|
|
|
|
if y < 1 or y > self.game.size: return False
|
|
|
|
return True
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-15 22:19:44 +08:00
|
|
|
def _neighbor(self, vertex):
|
|
|
|
x, y = vertex
|
|
|
|
nei = []
|
|
|
|
for d in NEIGHBOR_OFFSET:
|
|
|
|
_x = x + d[0]
|
|
|
|
_y = y + d[1]
|
|
|
|
if self._in_board((_x, _y)):
|
|
|
|
nei.append((_x, _y))
|
|
|
|
return nei
|
|
|
|
|
2017-12-20 01:14:05 +08:00
|
|
|
def _corner(self, vertex):
|
|
|
|
x, y = vertex
|
|
|
|
corner = []
|
|
|
|
for d in CORNER_OFFSET:
|
|
|
|
_x = x + d[0]
|
|
|
|
_y = y + d[1]
|
|
|
|
if self._in_board((_x, _y)):
|
|
|
|
corner.append((_x, _y))
|
|
|
|
return corner
|
|
|
|
|
2017-12-20 00:16:24 +08:00
|
|
|
def _find_group(self, current_board, vertex):
|
|
|
|
color = current_board[self.game._flatten(vertex)]
|
|
|
|
# print ("color : ", color)
|
|
|
|
chain = set()
|
|
|
|
frontier = [vertex]
|
|
|
|
has_liberty = False
|
|
|
|
while frontier:
|
|
|
|
current = frontier.pop()
|
|
|
|
# print ("current : ", current)
|
|
|
|
chain.add(current)
|
|
|
|
for n in self._neighbor(current):
|
|
|
|
if current_board[self.game._flatten(n)] == color and not n in chain:
|
|
|
|
frontier.append(n)
|
|
|
|
if current_board[self.game._flatten(n)] == utils.EMPTY:
|
|
|
|
has_liberty = True
|
|
|
|
return has_liberty, chain
|
|
|
|
|
|
|
|
def _is_suicide(self, current_board, color, vertex):
|
|
|
|
current_board[self.game._flatten(vertex)] = color # assume that we already take this move
|
|
|
|
suicide = False
|
|
|
|
|
|
|
|
has_liberty, group = self._find_group(current_board, vertex)
|
|
|
|
if not has_liberty:
|
|
|
|
suicide = True # no liberty, suicide
|
|
|
|
for n in self._neighbor(vertex):
|
|
|
|
if current_board[self.game._flatten(n)] == utils.another_color(color):
|
|
|
|
opponent_liberty, group = self._find_group(current_board, n)
|
|
|
|
if not opponent_liberty:
|
|
|
|
suicide = False # this move is able to take opponent's stone, not suicide
|
|
|
|
|
|
|
|
current_board[self.game._flatten(vertex)] = utils.EMPTY # undo this move
|
|
|
|
return suicide
|
|
|
|
|
2017-12-19 22:57:38 +08:00
|
|
|
def _process_board(self, current_board, color, vertex):
|
2017-12-15 22:19:44 +08:00
|
|
|
nei = self._neighbor(vertex)
|
|
|
|
for n in nei:
|
2017-12-19 22:57:38 +08:00
|
|
|
if current_board[self.game._flatten(n)] == utils.another_color(color):
|
2017-12-20 00:16:24 +08:00
|
|
|
has_liberty, group = self._find_group(current_board, n)
|
|
|
|
if not has_liberty:
|
|
|
|
for b in group:
|
2017-12-19 22:57:38 +08:00
|
|
|
current_board[self.game._flatten(b)] = utils.EMPTY
|
2017-12-15 22:19:44 +08:00
|
|
|
|
2017-12-20 00:16:24 +08:00
|
|
|
def _check_global_isomorphous(self, history_boards, current_board, color, vertex):
|
|
|
|
repeat = False
|
|
|
|
next_board = copy.copy(current_board)
|
|
|
|
next_board[self.game._flatten(vertex)] = color
|
|
|
|
self._process_board(next_board, color, vertex)
|
|
|
|
if next_board in history_boards:
|
|
|
|
repeat = True
|
|
|
|
return repeat
|
|
|
|
|
2017-12-20 01:14:05 +08:00
|
|
|
def _is_eye(self, current_board, color, vertex):
|
|
|
|
nei = self._neighbor(vertex)
|
|
|
|
cor = self._corner(vertex)
|
|
|
|
ncolor = {color == current_board[self.game._flatten(n)] for n in nei}
|
|
|
|
if False in ncolor:
|
|
|
|
# print "not all neighbors are in same color with us"
|
|
|
|
return False
|
|
|
|
_, group = self._find_group(current_board, nei[0])
|
|
|
|
if set(nei) < group:
|
|
|
|
# print "all neighbors are in same group and same color with us"
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
opponent_number = [current_board[self.game._flatten(c)] for c in cor].count(-color)
|
|
|
|
opponent_propotion = float(opponent_number) / float(len(cor))
|
|
|
|
if opponent_propotion < 0.5:
|
|
|
|
# print "few opponents, real eye"
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
# print "many opponents, fake eye"
|
|
|
|
return False
|
|
|
|
|
|
|
|
def _knowledge_prunning(self, current_board, color, vertex):
|
|
|
|
### check if it is an eye of yourself
|
|
|
|
### assumptions : notice that this judgement requires that the state is an endgame
|
|
|
|
if self._is_eye(current_board, color, vertex):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
2017-12-20 16:43:42 +08:00
|
|
|
def _action2vertex(self, action):
|
2017-12-20 01:14:05 +08:00
|
|
|
if action == self.game.size ** 2:
|
|
|
|
vertex = (0, 0)
|
|
|
|
else:
|
|
|
|
vertex = self.game._deflatten(action)
|
2017-12-20 16:43:42 +08:00
|
|
|
return vertex
|
2017-12-20 01:14:05 +08:00
|
|
|
|
2017-12-20 00:43:31 +08:00
|
|
|
def _is_valid(self, history_boards, current_board, color, vertex):
|
2017-12-15 22:19:44 +08:00
|
|
|
### in board
|
|
|
|
if not self._in_board(vertex):
|
|
|
|
return False
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-15 22:19:44 +08:00
|
|
|
### already have stone
|
2017-12-19 22:57:38 +08:00
|
|
|
if not current_board[self.game._flatten(vertex)] == utils.EMPTY:
|
2017-12-15 22:19:44 +08:00
|
|
|
return False
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-20 00:16:24 +08:00
|
|
|
### check if it is suicide
|
|
|
|
if self._is_suicide(current_board, color, vertex):
|
2017-12-15 22:19:44 +08:00
|
|
|
return False
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-20 01:14:05 +08:00
|
|
|
### forbid global isomorphous
|
2017-12-19 22:57:38 +08:00
|
|
|
if self._check_global_isomorphous(history_boards, current_board, color, vertex):
|
2017-12-15 22:19:44 +08:00
|
|
|
return False
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-15 22:19:44 +08:00
|
|
|
return True
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-20 16:43:42 +08:00
|
|
|
def simulate_is_valid(self, state, action):
|
|
|
|
history_boards, color = state
|
|
|
|
vertex = self._action2vertex(action)
|
|
|
|
current_board = history_boards[-1]
|
2017-12-20 01:14:05 +08:00
|
|
|
|
|
|
|
if not self._is_valid(history_boards, current_board, color, vertex):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if not self._knowledge_prunning(current_board, color, vertex):
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2017-12-20 16:43:42 +08:00
|
|
|
def _do_move(self, board, color, vertex):
|
2017-12-20 01:14:05 +08:00
|
|
|
if vertex == utils.PASS:
|
2017-12-20 16:43:42 +08:00
|
|
|
return board
|
2017-12-20 01:14:05 +08:00
|
|
|
else:
|
2017-12-20 16:43:42 +08:00
|
|
|
id_ = self.game._flatten(vertex)
|
|
|
|
board[id_] = color
|
|
|
|
return board
|
2017-12-20 01:14:05 +08:00
|
|
|
|
|
|
|
def simulate_step_forward(self, state, action):
|
|
|
|
# initialize the simulate_board from state
|
2017-12-20 16:43:42 +08:00
|
|
|
history_boards, color = state
|
|
|
|
vertex = self._action2vertex(action)
|
|
|
|
new_board = self._do_move(copy.copy(history_boards[-1]), color, vertex)
|
|
|
|
history_boards.append(new_board)
|
|
|
|
new_color = -color
|
|
|
|
return [history_boards, new_color], 0
|
2017-12-20 01:14:05 +08:00
|
|
|
|
2017-12-20 00:43:31 +08:00
|
|
|
def executor_do_move(self, color, vertex):
|
|
|
|
if not self._is_valid(self.game.history, self.game.board, color, vertex):
|
2017-12-15 22:19:44 +08:00
|
|
|
return False
|
|
|
|
self.game.board[self.game._flatten(vertex)] = color
|
2017-12-19 22:57:38 +08:00
|
|
|
self._process_board(self.game.board, color, vertex)
|
2017-12-15 22:19:44 +08:00
|
|
|
self.game.history.append(copy.copy(self.game.board))
|
2017-12-16 23:29:11 +08:00
|
|
|
self.game.latest_boards.append(copy.copy(self.game.board))
|
2017-12-15 22:19:44 +08:00
|
|
|
return True
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-15 22:19:44 +08:00
|
|
|
def _find_empty(self):
|
|
|
|
idx = [i for i,x in enumerate(self.game.board) if x == utils.EMPTY ][0]
|
|
|
|
return self.game._deflatten(idx)
|
2017-11-04 01:45:55 +08:00
|
|
|
|
2017-12-20 00:16:24 +08:00
|
|
|
def _find_boarder(self, vertex):
|
|
|
|
_, group = self._find_group(self.game.board, vertex)
|
|
|
|
border = []
|
|
|
|
for b in group:
|
|
|
|
for n in self._neighbor(b):
|
|
|
|
if not (n in group):
|
|
|
|
border.append(n)
|
|
|
|
return border
|
|
|
|
|
2017-12-19 11:16:17 +08:00
|
|
|
def _add_nearby_stones(self, neighbor_vertex_set, start_vertex_x, start_vertex_y, x_diff, y_diff, num_step):
|
|
|
|
'''
|
|
|
|
add the nearby stones around the input vertex
|
|
|
|
:param neighbor_vertex_set: input list
|
|
|
|
:param start_vertex_x: x axis of the input vertex
|
|
|
|
:param start_vertex_y: y axis of the input vertex
|
|
|
|
:param x_diff: add x axis
|
|
|
|
:param y_diff: add y axis
|
|
|
|
:param num_step: number of steps to be added
|
|
|
|
:return:
|
|
|
|
'''
|
|
|
|
for step in xrange(num_step):
|
|
|
|
new_neighbor_vertex = (start_vertex_x, start_vertex_y)
|
|
|
|
if self._in_board(new_neighbor_vertex):
|
|
|
|
neighbor_vertex_set.append((start_vertex_x, start_vertex_y))
|
|
|
|
start_vertex_x += x_diff
|
|
|
|
start_vertex_y += y_diff
|
|
|
|
|
2017-12-20 16:43:42 +08:00
|
|
|
def _predict_from_nearby(self, vertex, neighbor_step=3):
|
2017-12-19 11:16:17 +08:00
|
|
|
'''
|
|
|
|
step: the nearby 3 steps is considered
|
|
|
|
:vertex: position to be estimated
|
|
|
|
:neighbor_step: how many steps nearby
|
|
|
|
:return: the nearby positions of the input position
|
|
|
|
currently the nearby 3*3 grid is returned, altogether 4*8 points involved
|
|
|
|
'''
|
|
|
|
for step in range(1, neighbor_step + 1): # check the stones within the steps in range
|
|
|
|
neighbor_vertex_set = []
|
|
|
|
self._add_nearby_stones(neighbor_vertex_set, vertex[0] - step, vertex[1], 1, 1, neighbor_step)
|
|
|
|
self._add_nearby_stones(neighbor_vertex_set, vertex[0], vertex[1] + step, 1, -1, neighbor_step)
|
|
|
|
self._add_nearby_stones(neighbor_vertex_set, vertex[0] + step, vertex[1], -1, -1, neighbor_step)
|
|
|
|
self._add_nearby_stones(neighbor_vertex_set, vertex[0], vertex[1] - step, -1, 1, neighbor_step)
|
|
|
|
color_estimate = 0
|
|
|
|
for neighbor_vertex in neighbor_vertex_set:
|
|
|
|
color_estimate += self.game.board[self.game._flatten(neighbor_vertex)]
|
|
|
|
if color_estimate > 0:
|
|
|
|
return utils.BLACK
|
|
|
|
elif color_estimate < 0:
|
|
|
|
return utils.WHITE
|
|
|
|
|
2017-12-20 16:43:42 +08:00
|
|
|
def executor_get_score(self, is_unknown_estimation=False):
|
2017-12-15 22:19:44 +08:00
|
|
|
'''
|
|
|
|
is_unknown_estimation: whether use nearby stone to predict the unknown
|
|
|
|
return score from BLACK perspective.
|
|
|
|
'''
|
|
|
|
_board = copy.copy(self.game.board)
|
|
|
|
while utils.EMPTY in self.game.board:
|
|
|
|
vertex = self._find_empty()
|
|
|
|
boarder = self._find_boarder(vertex)
|
|
|
|
boarder_color = set(map(lambda v: self.game.board[self.game._flatten(v)], boarder))
|
|
|
|
if boarder_color == {utils.BLACK}:
|
|
|
|
self.game.board[self.game._flatten(vertex)] = utils.BLACK
|
|
|
|
elif boarder_color == {utils.WHITE}:
|
|
|
|
self.game.board[self.game._flatten(vertex)] = utils.WHITE
|
|
|
|
elif is_unknown_estimation:
|
|
|
|
self.game.board[self.game._flatten(vertex)] = self._predict_from_nearby(vertex)
|
2017-11-04 01:45:55 +08:00
|
|
|
else:
|
2017-12-15 22:19:44 +08:00
|
|
|
self.game.board[self.game._flatten(vertex)] =utils.UNKNOWN
|
|
|
|
score = 0
|
|
|
|
for i in self.game.board:
|
|
|
|
if i == utils.BLACK:
|
|
|
|
score += 1
|
|
|
|
elif i == utils.WHITE:
|
|
|
|
score -= 1
|
|
|
|
score -= self.game.komi
|
|
|
|
|
|
|
|
self.game.board = _board
|
|
|
|
return score
|
|
|
|
|