def py_get_liberty(matrix): black_liberty = np.zeros((19, 19, 8), dtype=np.uint8) white_liberty = np.zeros((19, 19, 8), dtype=np.uint8) visited = {} for i in range(19): for j in range(19): if matrix[i][j] == 1 and (i, j) not in visited: groups = Board.get_group(i, j, matrix, visited=visited) num_liberty = Board.check_liberty(groups, matrix, cnt=True) if num_liberty > 8: num_liberty = 8 for stone in groups: black_liberty[stone[0]][stone[1]][num_liberty-1] = 1 if matrix[i][j] == 2 and (i, j) not in visited: groups = Board.get_group(i, j, matrix, visited=visited) num_liberty = Board.check_liberty(groups, matrix, cnt=True) if num_liberty > 8: num_liberty = 8 for stone in groups: white_liberty[stone[0]][stone[1]][num_liberty-1] = 1 stones = np.concatenate((black_liberty, white_liberty), axis=2) return stones
def py_get_liberty(matrix): black_liberty = np.zeros((19, 19, 8), dtype=np.uint8) white_liberty = np.zeros((19, 19, 8), dtype=np.uint8) visited = {} for i in range(19): for j in range(19): if matrix[i][j] == 1 and (i, j) not in visited: groups = Board.get_group(i, j, matrix, visited=visited) num_liberty = Board.check_liberty(groups, matrix, cnt=True) if num_liberty > 8: num_liberty = 8 for stone in groups: black_liberty[stone[0]][stone[1]][num_liberty - 1] = 1 if matrix[i][j] == 2 and (i, j) not in visited: groups = Board.get_group(i, j, matrix, visited=visited) num_liberty = Board.check_liberty(groups, matrix, cnt=True) if num_liberty > 8: num_liberty = 8 for stone in groups: white_liberty[stone[0]][stone[1]][num_liberty - 1] = 1 stones = np.concatenate((black_liberty, white_liberty), axis=2) return stones
def preprocess_board(board_mtx, y, random=True, contain_liberty=False): if random: rand = np.random.randint(0, 8) if rand <= 3: board_mtx = board_mtx.T y['current_move'] = (y['current_move'][1], y['current_move'][0]) y['next_move'] = (y['next_move'][1], y['next_move'][0]) i = rand % 4 if i == 1: board_mtx = np.rot90(board_mtx) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) # print(a[2-idx[1]][idx[0]]) if i == 2: board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) if i == 3: board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) black_stones = np.zeros((19, 19, 1), dtype=np.uint8) black_stones[board_mtx == 1] = 1 white_stones = np.zeros((19, 19, 1), dtype=np.uint8) white_stones[board_mtx == 2] = 1 if contain_liberty: black_liberty = np.zeros((19, 19, 8), dtype=np.uint8) white_liberty = np.zeros((19, 19, 8), dtype=np.uint8) visited = {} for i in range(19): for j in range(19): if board_mtx[i][j] == 1 and (i, j) not in visited: groups = Board.get_group(i, j, board_mtx, visited=visited) num_liberty = Board.check_liberty(groups, board_mtx, cnt=True) if num_liberty > 8: num_liberty = 8 for stone in groups: black_liberty[stone[0]][stone[1]][num_liberty - 1] = 1 if board_mtx[i][j] == 2 and (i, j) not in visited: groups = Board.get_group(i, j, board_mtx, visited=visited) num_liberty = Board.check_liberty(groups, board_mtx, cnt=True) if num_liberty > 8: num_liberty = 8 for stone in groups: white_liberty[stone[0]][stone[1]][num_liberty - 1] = 1 black_stones = np.concatenate((black_stones, black_liberty), axis=2) white_stones = np.concatenate((white_stones, white_liberty), axis=2) stones = np.concatenate((black_stones, white_stones), axis=2) ones = np.ones((19, 19, 1), dtype=np.uint8) last_move = np.zeros((19, 19, 1), dtype=np.uint8) if not y['ko_state:']: last_move[y['current_move'][0]][y['current_move'][1]] = 1 else: last_move[y['current_move'][0]][y['current_move'][1]] = -1 is_black_next = np.ones((19, 19, 1), dtype=np.uint8) if y['next_to_play'] == 2: is_black_next -= 1 feat = np.concatenate((stones, last_move, is_black_next, ones), axis=2) return feat
def preprocess_board(board_mtx, y, random=True, contain_liberty=False): if random: rand = np.random.randint(0, 8) if rand <= 3: board_mtx = board_mtx.T y['current_move'] = (y['current_move'][1], y['current_move'][0]) y['next_move'] = (y['next_move'][1], y['next_move'][0]) i = rand % 4 if i == 1: board_mtx = np.rot90(board_mtx) y['current_move'] = (18-y['current_move'][1], y['current_move'][0]) y['next_move'] = (18-y['next_move'][1], y['next_move'][0]) # print(a[2-idx[1]][idx[0]]) if i == 2: board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) y['current_move'] = (18-y['current_move'][1], y['current_move'][0]) y['next_move'] = (18-y['next_move'][1], y['next_move'][0]) y['current_move'] = (18-y['current_move'][1], y['current_move'][0]) y['next_move'] = (18-y['next_move'][1], y['next_move'][0]) if i == 3: board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) y['current_move'] = (18-y['current_move'][1], y['current_move'][0]) y['next_move'] = (18-y['next_move'][1], y['next_move'][0]) y['current_move'] = (18-y['current_move'][1], y['current_move'][0]) y['next_move'] = (18-y['next_move'][1], y['next_move'][0]) y['current_move'] = (18-y['current_move'][1], y['current_move'][0]) y['next_move'] = (18-y['next_move'][1], y['next_move'][0]) black_stones = np.zeros((19, 19, 1), dtype=np.uint8) black_stones[board_mtx == 1] = 1 white_stones = np.zeros((19, 19, 1), dtype=np.uint8) white_stones[board_mtx == 2] = 1 if contain_liberty: black_liberty = np.zeros((19, 19, 8), dtype=np.uint8) white_liberty = np.zeros((19, 19, 8), dtype=np.uint8) visited = {} for i in range(19): for j in range(19): if board_mtx[i][j] == 1 and (i, j) not in visited: groups = Board.get_group(i, j, board_mtx, visited=visited) num_liberty = Board.check_liberty(groups, board_mtx, cnt=True) if num_liberty > 8: num_liberty = 8 for stone in groups: black_liberty[stone[0]][stone[1]][num_liberty-1] = 1 if board_mtx[i][j] == 2 and (i, j) not in visited: groups = Board.get_group(i, j, board_mtx, visited=visited) num_liberty = Board.check_liberty(groups, board_mtx, cnt=True) if num_liberty > 8: num_liberty = 8 for stone in groups: white_liberty[stone[0]][stone[1]][num_liberty-1] = 1 black_stones = np.concatenate((black_stones, black_liberty), axis=2) white_stones = np.concatenate((white_stones, white_liberty), axis=2) stones = np.concatenate((black_stones, white_stones), axis=2) ones = np.ones((19, 19, 1), dtype=np.uint8) last_move = np.zeros((19, 19, 1), dtype=np.uint8) if not y['ko_state:']: last_move[y['current_move'][0]][y['current_move'][1]] = 1 else: last_move[y['current_move'][0]][y['current_move'][1]] = -1 is_black_next = np.ones((19, 19, 1), dtype=np.uint8) if y['next_to_play'] == 2: is_black_next -= 1 feat = np.concatenate((stones, last_move, is_black_next, ones), axis=2) return feat
def process_board(board_mtx, y, random=True, contain_liberty=False): # 训练时 将数据进行旋转对称 防止过拟合 减少数据相关性 if random: rand = np.random.randint(0, 8) if rand <= 3: board_mtx = board_mtx.T y['current_move'] = (y['current_move'][1], y['current_move'][0]) y['next_move'] = (y['next_move'][1], y['next_move'][0]) i = rand % 4 if i == 1: board_mtx = np.rot90(board_mtx) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) # print(a[2-idx[1]][idx[0]]) if i == 2: board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) if i == 3: board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) board_mtx = np.rot90(board_mtx) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) y['current_move'] = (18 - y['current_move'][1], y['current_move'][0]) y['next_move'] = (18 - y['next_move'][1], y['next_move'][0]) # 生成传入的19 * 19 * 21 维度的数据 black_board = np.zeros((19, 19, 1), np.uint8) black_board[board_mtx == 1] = 1 white_board = np.zeros((19, 19, 1), np.uint8) white_board[board_mtx == 2] = 1 # 将棋盘上每个落子点气的数目记录下来 当做输出数据喂进网络 if contain_liberty: black_liberty = np.zeros((19, 19, 8), dtype=np.uint8) white_liberty = np.zeros((19, 19, 8), dtype=np.uint8) visited = {} for i in range(19): for j in range(19): if board_mtx[i][j] == 1 and (i, j) not in visited: group = Board.get_group(i, j, board_mtx, visited) num_liberty = Board.check_liberty(group, board_mtx, cnt=True) if num_liberty > 8: num_liberty = 8 for pos in group: black_liberty[pos[0]][pos[1]][num_liberty - 1] = 1 if board_mtx[i][j] == 2 and (i, j) not in visited: group = Board.get_group(i, j, board_mtx, visited) num_liberty = Board.check_liberty(group, board_mtx, cnt=True) if num_liberty > 8: num_liberty = 8 for pos in group: white_liberty[pos[0]][pos[1]][num_liberty - 1] = 1 black_board = np.concatenate((black_board, black_liberty), axis=2) white_board = np.concatenate((white_board, white_liberty), axis=2) board = np.concatenate((black_board, white_board), axis=2) ones = np.ones((19, 19, 1), dtype=np.uint8) last_move = np.zeros((19, 19, 1), dtype=np.uint8) if not y['ko_state:']: last_move[y['current_move'][0]][y['current_move'][1]] = 1 else: last_move[y['current_move'][0]][y['current_move'][1]] = -1 is_black_next = np.ones((19, 19, 1), dtype=np.uint8) if y['next_to_play'] == 2: is_black_next -= 1 feat = np.concatenate((board, last_move, is_black_next, ones), axis=2) return feat