2017-12-05 23:17:20 +08:00
|
|
|
import os, sys
|
|
|
|
|
2017-12-01 01:37:55 +08:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir))
|
2017-11-26 13:36:52 +08:00
|
|
|
import numpy as np
|
|
|
|
import utils
|
2017-11-28 17:00:10 +08:00
|
|
|
import time
|
2017-12-05 23:17:20 +08:00
|
|
|
import copy
|
2017-12-04 11:01:49 +08:00
|
|
|
import network_small
|
2017-11-28 17:00:10 +08:00
|
|
|
import tensorflow as tf
|
2017-11-26 13:36:52 +08:00
|
|
|
from collections import deque
|
|
|
|
from tianshou.core.mcts.mcts import MCTS
|
|
|
|
|
2017-12-05 23:17:20 +08:00
|
|
|
DELTA = [[1, 0], [-1, 0], [0, -1], [0, 1]]
|
2017-12-07 21:05:29 +08:00
|
|
|
CORNER_OFFSET = [[-1, -1], [-1, 1], [1, 1], [1, -1]]
|
2017-12-05 23:17:20 +08:00
|
|
|
|
2017-11-26 13:36:52 +08:00
|
|
|
class GoEnv:
|
2017-12-16 23:29:11 +08:00
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self.game = kwargs['game']
|
|
|
|
self.board = [utils.EMPTY] * (self.game.size * self.game.size)
|
|
|
|
self.latest_boards = deque(maxlen=8)
|
2017-11-26 13:36:52 +08:00
|
|
|
|
|
|
|
def _flatten(self, vertex):
|
|
|
|
x, y = vertex
|
2017-12-16 23:29:11 +08:00
|
|
|
return (x - 1) * self.game.size + (y - 1)
|
2017-12-05 23:17:20 +08:00
|
|
|
|
2017-12-03 19:16:21 +08:00
|
|
|
def _bfs(self, vertex, color, block, status, alive_break):
|
|
|
|
block.append(vertex)
|
|
|
|
status[self._flatten(vertex)] = True
|
|
|
|
nei = self._neighbor(vertex)
|
|
|
|
for n in nei:
|
|
|
|
if not status[self._flatten(n)]:
|
|
|
|
if self.board[self._flatten(n)] == color:
|
|
|
|
self._bfs(n, color, block, status, alive_break)
|
|
|
|
|
|
|
|
def _find_block(self, vertex, alive_break=False):
|
|
|
|
block = []
|
2017-12-16 23:29:11 +08:00
|
|
|
status = [False] * (self.game.size * self.game.size)
|
2017-12-03 19:16:21 +08:00
|
|
|
color = self.board[self._flatten(vertex)]
|
|
|
|
self._bfs(vertex, color, block, status, alive_break)
|
|
|
|
|
|
|
|
for b in block:
|
|
|
|
for n in self._neighbor(b):
|
|
|
|
if self.board[self._flatten(n)] == utils.EMPTY:
|
|
|
|
return False, block
|
|
|
|
return True, block
|
|
|
|
|
|
|
|
def _is_qi(self, color, vertex):
|
|
|
|
nei = self._neighbor(vertex)
|
|
|
|
for n in nei:
|
|
|
|
if self.board[self._flatten(n)] == utils.EMPTY:
|
|
|
|
return True
|
|
|
|
|
|
|
|
self.board[self._flatten(vertex)] = color
|
|
|
|
for n in nei:
|
|
|
|
if self.board[self._flatten(n)] == utils.another_color(color):
|
|
|
|
can_kill, block = self._find_block(n)
|
|
|
|
if can_kill:
|
|
|
|
self.board[self._flatten(vertex)] = utils.EMPTY
|
|
|
|
return True
|
|
|
|
|
2017-12-07 21:05:29 +08:00
|
|
|
### avoid suicide
|
2017-12-03 19:16:21 +08:00
|
|
|
can_kill, block = self._find_block(vertex)
|
|
|
|
if can_kill:
|
|
|
|
self.board[self._flatten(vertex)] = utils.EMPTY
|
|
|
|
return False
|
|
|
|
|
|
|
|
self.board[self._flatten(vertex)] = utils.EMPTY
|
|
|
|
return True
|
|
|
|
|
|
|
|
def _check_global_isomorphous(self, color, vertex):
|
|
|
|
##backup
|
|
|
|
_board = copy.copy(self.board)
|
|
|
|
self.board[self._flatten(vertex)] = color
|
|
|
|
self._process_board(color, vertex)
|
2017-12-16 23:29:11 +08:00
|
|
|
if self.board in self.latest_boards:
|
2017-12-03 19:16:21 +08:00
|
|
|
res = True
|
|
|
|
else:
|
|
|
|
res = False
|
|
|
|
|
|
|
|
self.board = _board
|
|
|
|
return res
|
|
|
|
|
|
|
|
def _in_board(self, vertex):
|
|
|
|
x, y = vertex
|
2017-12-16 23:29:11 +08:00
|
|
|
if x < 1 or x > self.game.size: return False
|
|
|
|
if y < 1 or y > self.game.size: return False
|
2017-12-03 19:16:21 +08:00
|
|
|
return True
|
|
|
|
|
|
|
|
def _neighbor(self, vertex):
|
|
|
|
x, y = vertex
|
|
|
|
nei = []
|
|
|
|
for d in DELTA:
|
|
|
|
_x = x + d[0]
|
|
|
|
_y = y + d[1]
|
|
|
|
if self._in_board((_x, _y)):
|
|
|
|
nei.append((_x, _y))
|
|
|
|
return nei
|
|
|
|
|
2017-12-07 21:05:29 +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-03 19:16:21 +08:00
|
|
|
def _process_board(self, color, vertex):
|
|
|
|
nei = self._neighbor(vertex)
|
|
|
|
for n in nei:
|
|
|
|
if self.board[self._flatten(n)] == utils.another_color(color):
|
|
|
|
can_kill, block = self._find_block(n, alive_break=True)
|
|
|
|
if can_kill:
|
|
|
|
for b in block:
|
|
|
|
self.board[self._flatten(b)] = utils.EMPTY
|
|
|
|
|
2017-12-07 21:05:29 +08:00
|
|
|
def _find_group(self, start):
|
|
|
|
color = self.board[self._flatten(start)]
|
|
|
|
# print ("color : ", color)
|
|
|
|
chain = set()
|
|
|
|
frontier = [start]
|
|
|
|
while frontier:
|
|
|
|
current = frontier.pop()
|
|
|
|
# print ("current : ", current)
|
|
|
|
chain.add(current)
|
|
|
|
for n in self._neighbor(current):
|
|
|
|
# print n, self._flatten(n), self.board[self._flatten(n)],
|
|
|
|
if self.board[self._flatten(n)] == color and not n in chain:
|
|
|
|
frontier.append(n)
|
|
|
|
return chain
|
|
|
|
|
|
|
|
def _is_eye(self, color, vertex):
|
|
|
|
nei = self._neighbor(vertex)
|
|
|
|
cor = self._corner(vertex)
|
|
|
|
ncolor = {color == self.board[self._flatten(n)] for n in nei}
|
|
|
|
if False in ncolor:
|
|
|
|
# print "not all neighbors are in same color with us"
|
|
|
|
return False
|
|
|
|
if set(nei) < self._find_group(nei[0]):
|
|
|
|
# print "all neighbors are in same group and same color with us"
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
opponent_number = [self.board[self._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
|
|
|
|
|
2017-12-16 23:29:11 +08:00
|
|
|
def knowledge_prunning(self, 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(color, vertex):
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
|
|
|
|
def simulate_is_valid(self, state, action):
|
2017-12-07 21:05:29 +08:00
|
|
|
# state is the play board, the shape is [1, 9, 9, 17]
|
2017-12-16 23:29:11 +08:00
|
|
|
if action == self.game.size * self.game.size:
|
2017-12-05 23:17:20 +08:00
|
|
|
vertex = (0, 0)
|
|
|
|
else:
|
2017-12-16 23:29:11 +08:00
|
|
|
vertex = (action / self.game.size + 1, action % self.game.size + 1)
|
2017-12-07 21:05:29 +08:00
|
|
|
if state[0, 0, 0, -1] == utils.BLACK:
|
|
|
|
color = utils.BLACK
|
2017-12-05 23:17:20 +08:00
|
|
|
else:
|
2017-12-07 21:05:29 +08:00
|
|
|
color = utils.WHITE
|
2017-12-16 23:29:11 +08:00
|
|
|
self.latest_boards.clear()
|
2017-12-05 23:17:20 +08:00
|
|
|
for i in range(8):
|
2017-12-16 23:29:11 +08:00
|
|
|
self.latest_boards.append((state[:, :, :, i] - state[:, :, :, i + 8]).reshape(-1).tolist())
|
|
|
|
self.board = copy.copy(self.latest_boards[-1])
|
|
|
|
|
2017-12-03 19:16:21 +08:00
|
|
|
### in board
|
|
|
|
if not self._in_board(vertex):
|
|
|
|
return False
|
|
|
|
|
|
|
|
### already have stone
|
|
|
|
if not self.board[self._flatten(vertex)] == utils.EMPTY:
|
2017-12-05 23:17:20 +08:00
|
|
|
# print(np.array(self.board).reshape(9, 9))
|
|
|
|
# print(vertex)
|
2017-12-03 19:16:21 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
### check if it is qi
|
|
|
|
if not self._is_qi(color, vertex):
|
|
|
|
return False
|
|
|
|
|
2017-12-16 23:29:11 +08:00
|
|
|
### forbid global isomorphous
|
|
|
|
if self._check_global_isomorphous(color, vertex):
|
2017-12-07 21:05:29 +08:00
|
|
|
return False
|
|
|
|
|
2017-12-16 23:29:11 +08:00
|
|
|
if not self.knowledge_prunning(color, vertex):
|
2017-12-03 19:16:21 +08:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
2017-11-26 13:36:52 +08:00
|
|
|
|
|
|
|
def do_move(self, color, vertex):
|
|
|
|
if vertex == utils.PASS:
|
|
|
|
return True
|
|
|
|
|
|
|
|
id_ = self._flatten(vertex)
|
|
|
|
if self.board[id_] == utils.EMPTY:
|
|
|
|
self.board[id_] = color
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def step_forward(self, state, action):
|
|
|
|
if state[0, 0, 0, -1] == 1:
|
2017-12-08 17:05:33 +08:00
|
|
|
color = utils.BLACK
|
2017-11-26 13:36:52 +08:00
|
|
|
else:
|
2017-12-08 17:05:33 +08:00
|
|
|
color = utils.WHITE
|
2017-12-16 23:29:11 +08:00
|
|
|
if action == self.game.size ** 2:
|
2017-12-08 17:05:33 +08:00
|
|
|
vertex = utils.PASS
|
2017-11-26 13:36:52 +08:00
|
|
|
else:
|
2017-12-16 23:29:11 +08:00
|
|
|
vertex = (action % self.game.size + 1, action / self.game.size + 1)
|
2017-12-05 23:17:20 +08:00
|
|
|
# print(vertex)
|
|
|
|
# print(self.board)
|
|
|
|
self.board = (state[:, :, :, 7] - state[:, :, :, 15]).reshape(-1).tolist()
|
2017-11-26 13:36:52 +08:00
|
|
|
self.do_move(color, vertex)
|
|
|
|
new_state = np.concatenate(
|
2017-12-16 23:29:11 +08:00
|
|
|
[state[:, :, :, 1:8], (np.array(self.board) == utils.BLACK).reshape(1, self.game.size, self.game.size, 1),
|
|
|
|
state[:, :, :, 9:16], (np.array(self.board) == utils.WHITE).reshape(1, self.game.size, self.game.size, 1),
|
|
|
|
np.array(1 - state[:, :, :, -1]).reshape(1, self.game.size, self.game.size, 1)],
|
2017-11-26 13:36:52 +08:00
|
|
|
axis=3)
|
|
|
|
return new_state, 0
|