示例#1
0
    def process_zip(self, zip_file_name, data_file_name, game_list):
        tar_file = self.unzip_data(zip_file_name)
        zip_file = tarfile.open(self.data_dir + '/' + tar_file)
        name_list = zip_file.getnames()
        # Xac dinh tong so luong nuoc di trong tat ca games trong file zip
        total_examples = self.num_total_examples(zip_file, game_list,
                                                 name_list)

        shape = self.encoder.shape()
        feature_shape = np.insert(shape, 0, np.asarray([total_examples]))
        features = np.zeros(feature_shape)
        labels = np.zeros((total_examples, ))

        counter = 0
        for index in game_list:
            name = name_list[index + 1]
            if not name.endswith('.sgf'):
                raise ValueError(name + ' is not a valid sgf')
            sgf_content = zip_file.extractfile(name).read()
            # Doc noi dung SGF duoi dang chuoi ,sau khi giai nen tep zip
            sgf = Sgf_game.from_string(sgf_content)
            # Suy ra trang thai tro choi ban dau bang cach ap dung tat ca cac vien da handicap
            game_state, first_move_done = self.get_handicap(sgf)
            # Lap lai tat ca cac di chuyen trong tep SGF
            for item in sgf.main_sequence_iter():
                color, move_tuple = item.get_move()
                point = None
                if color is not None:
                    # Doc toa do cua hon da duoc choi
                    if move_tuple is not None:
                        row, col = move_tuple
                        point = Point(row + 1, col + 1)
                        move = Move.play(point)
                    else:
                        move = Move.pass_turn()
                    if first_move_done and point is not None:
                        # Ma hoa trang thai tro choi duoi dang feature
                        features[counter] = self.encoder.encode(game_state)
                        # Ma hoa nuoc di nhu nhan cua feature
                        labels[counter] = self.encoder.encode_point(point)
                        counter += 1
# Sau do, nuoc di duoc ap dung cho ban co de tien hanh nuoc di tiep theo
                    game_state = game_state.apply_move(move)
                    first_move_done = True

        feature_file_base = self.data_dir + '/' + data_file_name + '_features_%d'
        label_file_base = self.data_dir + '/' + data_file_name + '_labels_%d'

        chunk = 0  # Due to files with large content, split up after chunksize
        chunksize = 1024
        while features.shape[0] >= chunksize:
            feature_file = feature_file_base % chunk
            label_file = label_file_base % chunk
            chunk += 1
            current_features, features = features[:chunksize], features[
                chunksize:]
            current_labels, labels = labels[:chunksize], labels[chunksize:]
            np.save(feature_file, current_features)
            np.save(label_file, current_labels)
示例#2
0
    def process_zip(self, zip_file_name, data_file_name, game_list):
        tar_file = self.unzip_data(zip_file_name)
        zip_file = tarfile.open(self.data_dir + '/' + tar_file)
        name_list = zip_file.getnames()
        #print(name_list)
        total_examples = self.num_total_examples(zip_file, game_list,
                                                 name_list)
        #print(zip_file_name, game_list, total_examples)
        shape = self.encoder.shape()
        feature_shape = np.insert(shape, 0, np.asarray([total_examples]))
        features = np.zeros(feature_shape)
        labels = np.zeros((total_examples, ))

        counter = 0
        for index in game_list:
            name = name_list[index + 1]
            if not name.endswith('.sgf'):
                raise ValueError(name + ' is not valid sgf')

            sgf_content = zip_file.extractfile(name).read()
            sgf = Sgf_game.from_string(sgf_content)
            #print(sgf)
            game_state, first_move_done = self.get_handicap(sgf)
            #print(game_state)
            for item in sgf.main_sequence_iter():
                color, move_tuple = item.get_move()
                point = None
                if color is not None:
                    if move_tuple is not None:
                        row, col = move_tuple
                        point = Point(row + 1, col + 1)
                        move = Move.play(point)
                    else:
                        move = Move.pass_turn()

                    if first_move_done and point is not None:
                        features[counter] = self.encoder.encode(game_state)
                        labels[counter] = self.encoder.encode_point(point)
                        counter += 1
                    game_state = game_state.apply_move(move)
                    first_move_done = True

        print('features and labels size is ', counter)
        feature_file_base = self.data_dir + '/' + data_file_name + '_features_%d'
        label_file_base = self.data_dir + '/' + data_file_name + '_labels_%d'
        chunk = 0
        chunksize = 1024
        zip_file.close()
        print(features.shape)
        while features.shape[0] >= chunksize:
            feature_file = feature_file_base % chunk
            label_file = label_file_base % chunk
            chunk += 1
            current_features, features = features[:chunksize], features[
                chunksize:]
            current_labels, labels = labels[:chunksize], labels[chunksize:]
            np.save(feature_file, current_features)
            np.save(label_file, current_labels)
示例#3
0
 def select_move(self, game_state):
     candidates = []
     for r in range(1, game_state.board.num_rows + 1):
         for c in range(1, game_state.board.num_cols + 1):
             candidate = Point(row=r, col=c)
             if game_state.is_valid_move(Move.play(candidate)) and \
                     not is_point_an_eye(game_state.board, candidate, game_state.next_player):
                 candidates.append(candidate)
     if not candidates:
         return Move.pass_turn()
     return Move.play(random.choice(candidates))
示例#4
0
def guess_ladder_stones(game_state, move, escape_player):
    adjacent_strings = [game_state.board.get_go_string(nb) for nb in move.neighbors() if game_state.board.get_go_string(nb)]
    if adjacent_strings:
        string = adjacent_strings[0]
        neighbors = []
        for string in adjacent_strings:
            stones = string.stones
            for stone in stones:
                neighbors.append(stone)
        return [Move(nb) for nb in neighbors if is_candidate(game_state, Move(nb), escape_player)]
    else:
        return []
示例#5
0
    def legal_moves(self):
        moves = []
        for row in range(1, self.board.num_rows + 1):
            for col in range(1, self.board.num_cols + 1):
                move = Move.play(Point(row, col))
                if self.is_valid_move(move):
                    moves.append(move)
        # These two moves are always legal.
        moves.append(Move.pass_turn())
        moves.append(Move.resign())

        return moves
示例#6
0
    def process_zip(self, zip_file_name, data_file_name, game_list):
        tar_file = self.unzip_data(zip_file_name)
        zip_file = tarfile.open(self.data_dir + '/' + tar_file)
        name_list = zip_file.getnames()
        # Determine the total number of moves in all games in this zip file.
        total_examples = self.num_total_examples(zip_file, game_list, name_list)

        shape = self.encoder.shape()  # Infer the shape of features and labels from the encoder we use
        feature_shape = np.insert(shape, 0, np.asarray([total_examples]))
        features = np.zeros(feature_shape)
        labels = np.zeros((total_examples,))

        counter = 0
        for index in game_list:
            name = name_list[index + 1]
            if not name.endswith('.sgf'):
                raise ValueError(name + ' is not a valid sgf')
            sgf_content = zip_file.extractfile(name).read()
            sgf = Sgf_game.from_string(sgf_content)  # Read the SGF content as string, after extracting the zip file

            game_state, first_move_done = self.get_handicap(sgf)  # Infer the initial game state by applying all handicap stones

            for item in sgf.main_sequence_iter():  # Iterate over all moves in the SGF file
                color, move_tuple = item.get_move()
                point = None
                if color is not None:
                    if move_tuple is not None:  # Read the coordinates of the stone to be played...
                        row, col = move_tuple
                        point = Point(row + 1, col + 1)
                        move = Move.play(point)
                    else:
                        move = Move.pass_turn()  # ... or pass, if there is none.
                    if first_move_done and point is not None:
                        features[counter] = self.encoder.encode(game_state)  # We encode the current game state as features...
                        labels[counter] = self.encoder.encode_point(point)  # ... and the next move as label for the features
                        counter += 1
                    game_state = game_state.apply_move(move)  # Afterwards the move is applied to the board and we proceed with the next one
                    first_move_done = True

        feature_file_base = self.data_dir + '/' + data_file_name + '_features_%d'
        label_file_base = self.data_dir + '/' + data_file_name + '_labels_%d'

        chunk = 0  # Due to files with large content, split up after chunksize
        chunksize = 1024
        while features.shape[0] >= chunksize:  # We process features and labels in chunks of size 1024.
            feature_file = feature_file_base % chunk
            label_file = label_file_base % chunk
            chunk += 1
            current_features, features = features[:chunksize], features[chunksize:]
            current_labels, labels = labels[:chunksize], labels[chunksize:]  # The current chunk is cut off from features and labels...
            np.save(feature_file, current_features)
            np.save(label_file, current_labels)  # and then stored in a separate file.
示例#7
0
 def select_move(self, game_state):
   """Choose a random valid move that perserves our own eyes"""
   candidates = [] # keeps track of all valid moves
   for r in range(1, game_state.board.num_rows + 1):
     for c in range(1, game_state.board.num_cols + 1):
       candidate = Point(row=r, col=c)
       if game_state.is_valid_move(Move.play(candidate)) and \
                       not is_point_an_eye(game_state.board,
                                           candidate,
                                           game_state.next_player):
                   candidates.append(candidate)
   if not candidates:
     return Move.pass_turn() # if there are no valid moves left, we pass
   return Move.play(random.choice(candidates)) # choose a random move that is valid
示例#8
0
 def select_move(self, game_state):
     """Choose a random valid move that preserves our own eyes.
     自分の眼を維持するランダムな有効な着手を選択する"""
     candidates = []  # 候補
     for r in range(1, game_state.board.num_rows + 1):
         for c in range(1, game_state.board.num_cols + 1):
             candidate = Point(row=r, col=c)
             if game_state.is_valid_move( Move.play( candidate ) ) and \
                not is_point_an_eye( game_state.board,            # <1>
                                     candidate,
                                     game_state.next_player ):
                 candidates.append(candidate)  # <2>
     if not candidates:  # <3>
         return Move.pass_turn()
     return Move.play(random.choice(candidates))  # <4>
示例#9
0
    def select_move(self, game_state):
        """Choose a random valid move that preserves our own eyes."""
        candidates = []
        for r in range(1, game_state.board.num_rows + 1):
            for c in range(1, game_state.board.num_cols + 1):
                candidate = Point(row=r, col=c)
                if game_state.is_valid_move(Move.play(candidate)) and \
                        not is_point_an_eye(game_state.board,
                                            candidate,
                                            game_state.next_player):
                    candidates.append(candidate)
        if not candidates:
            return Move.pass_turn()
        return Move.play(random.choice(candidates))
# end::random_bot[]
示例#10
0
    def test_new_game(self):
        start = GameState.new_game(19)
        next_state = start.apply_move(Move.play(Point(16, 16)))

        self.assertEqual(start, next_state.previous_state)
        self.assertEqual(Player.white, next_state.next_player)
        self.assertEqual(Player.black, next_state.board.get(Point(16, 16)))
示例#11
0
    def select_move(self, game_state):
        # Chon random 1 nuoc di hop le
        dim = (game_state.board.num_rows, game_state.board.num_cols)
        if dim != self.dim:
            self._update_cache(dim)

        idx = np.arange(len(self.point_cache))
        np.random.shuffle(idx)
        for i in idx:
            p = self.point_cache[i]
            if game_state.is_valid_move(Move.play(p)) and \
                    not is_point_an_eye(game_state.board,
                                        p,
                                        game_state.next_player):
                return Move.play(p)
        return Move.pass_turn()
示例#12
0
    def select_move(self, game_state):
        """Choose a random valid move that preserves our own eyes."""
        dim = (game_state.board.num_rows, game_state.board.num_cols)
        if dim != self.dim:
            self._update_cache(dim)

        idx = np.arange(len(self.point_cache))
        np.random.shuffle(idx)
        for i in idx:
            p = self.point_cache[i]
            if game_state.is_valid_move(Move.play(p)) and \
                    not is_point_an_eye(game_state.board,
                                        p,
                                        game_state.next_player):
                return Move.play(p)
        return Move.pass_turn()
示例#13
0
    def encode(self, game_state):
        # 空箱
        board_tensor = np.zeros(self.shape())

        # 白は0,1,2黒は3,4,5を使うためにズラすとき使う
        base_plane = {
            game_state.next_player: 0,
            game_state.next_player.other: 3
        }

        # 全ての盤上の点を探索
        for row in range(self.board_height):
            for col in range(self.board_width):

                p = Point(row=row + 1, col=col + 1)
                go_string = game_state.board.get_go_string(p)

                # 石が置かれていない点は劫かどうかだけ調べればよい
                if go_string is None:
                    if game_state.does_move_violate_ko(game_state.next_player,
                                                       Move.play(p)):
                        board_tensor[6][row][col] = 1
                else:
                    # 呼吸点が3以上か,2か,1か
                    liberty_plane = min(3, go_string.num_liberties) - 1
                    liberty_plane += base_plane[go_string.color]
                    board_tensor[liberty_plane][row][col] = 1

        return board_tensor
示例#14
0
文件: utils.py 项目: Nail1959/Code_Go
def is_ladder(try_capture,
              game_state,
              candidate,
              ladder_stones=None,
              recursion_depth=50):
    """Ladders are played out in reversed roles, one player tries to capture,
    the other to escape. We determine the ladder status by recursively calling
    is_ladder in opposite roles, providing suitable capture or escape candidates.
    Arguments:
    try_capture: boolean flag to indicate if you want to capture or escape the ladder
    game_state: current game state, instance of GameState
    candidate: a move that potentially leads to escaping the ladder or capturing it, instance of Move
    ladder_stones: the stones to escape or capture, list of Point. Will be inferred if not provided.
    recursion_depth: when to stop recursively calling this function, integer valued.
    Returns True if game state is a ladder and try_capture is true (the ladder captures)
    or if game state is not a ladder and try_capture is false (you can successfully escape)
    and False otherwise.
    """

    if not game_state.is_valid_move(Move(candidate)) or not recursion_depth:
        return False

    next_player = game_state.next_player
    capture_player = next_player if try_capture else next_player.other
    escape_player = capture_player.other

    if ladder_stones is None:
        ladder_stones = guess_ladder_stones(game_state, candidate,
                                            escape_player)

    for ladder_stone in ladder_stones:
        current_state = game_state.apply_move(candidate)

        if try_capture:
            candidates = determine_escape_candidates(game_state, ladder_stone,
                                                     capture_player)
            attempted_escapes = [  # now try to escape
                is_ladder(False, current_state, escape_candidate, ladder_stone,
                          recursion_depth - 1)
                for escape_candidate in candidates
            ]

            if not any(attempted_escapes):
                return True  # if at least one escape fails, we capture
        else:
            if count_liberties(current_state, ladder_stone) >= 3:
                return True  # successful escape
            if count_liberties(current_state, ladder_stone) == 1:
                continue  # failed escape, others might still do
            candidates = liberties(current_state, ladder_stone)
            attempted_captures = [  # now try to capture
                is_ladder(True, current_state, capture_candidate, ladder_stone,
                          recursion_depth - 1)
                for capture_candidate in candidates
            ]
            if any(attempted_captures):
                continue  # failed escape, try others
            return True  # candidate can't be caught in a ladder, escape.
    return False  # no captures / no escapes
示例#15
0
def main():
    board_size = 9
    game: GameState = GameState.new_game(board_size)
    bot: Agent = RandomAgent()

    move: Move = None
    while not game.is_over():
        print('\033c')  # clear terminal [Linux and OS X]
        if move is not None:
            print_move(game.next_player, move)
        print_board(game.board)
        if game.next_player == gotypes.Player.black:
            print(compute_game_result(game))
            human_move_str: str = input('-- ')
            try:
                point = point_from_coords(human_move_str.strip().upper())
                move = Move.play(point)
            except (ValueError, Exception):
                move = Move.pass_turn()
        else:
            move = bot.select_move(game)
        game = game.apply_move(move)
示例#16
0
 def encode(self, game_state: GameState):
     board_tensor = np.zeroes(self.shape())
     board_plane = {game_state.next_player: 0,
                    game_state.next_player.other: 3}
     for row in range(self.board_height):
         for col in range(self.board_width):
             point = Point(row+1, col+1)
             go_string = game_state.board.get_go_string(point)
             if go_string is None:
                 if game_state.does_move_violate_ko(game_state.next_player, Move(point)):
                     board_tensor[VIOLATE_KO][row][col] = 1
             else:
                 liberty_plane = min(3, go_string.num_liberties)
                 liberty_plane += board_plane[go_string.color]
                 board_tensor[liberty_plane][row][col] = 1
     return board_tensor
示例#17
0
文件: utils.py 项目: LJQCN101/alphago
def is_ladder(try_capture,
              game_state,
              candidate,
              ladder_stones=None,
              recursion_depth=50):

    if not game_state.is_valid_move(Move(candidate)) or not recursion_depth:
        return False

    next_player = game_state.next_player
    capture_player = next_player if try_capture else next_player.other
    escape_player = capture_player.other

    if ladder_stones is None:
        ladder_stones = guess_ladder_stones(game_state, candidate,
                                            escape_player)

    for ladder_stone in ladder_stones:
        current_state = game_state.apply_move(candidate)

        if try_capture:
            candidates = determine_escape_candidates(game_state, ladder_stone,
                                                     capture_player)
            attempted_escapes = [  # now try to escape
                is_ladder(False, current_state, escape_candidate, ladder_stone,
                          recursion_depth - 1)
                for escape_candidate in candidates
            ]

            if not any(attempted_escapes):
                return True  # if at least one escape fails, we capture
        else:
            if count_liberties(current_state, ladder_stone) >= 3:
                return True  # successful escape
            if count_liberties(current_state, ladder_stone) == 1:
                continue  # failed escape, others might still do
            candidates = liberties(current_state, ladder_stone)
            attempted_captures = [  # now try to capture
                is_ladder(True, current_state, capture_candidate, ladder_stone,
                          recursion_depth - 1)
                for capture_candidate in candidates
            ]
            if any(attempted_captures):
                continue  # failed escape, try others
            return True  # candidate can't be caught in a ladder, escape.
    return False  # no captures / no escapes
示例#18
0
 def encode(self, game_state):
     board_tensor = np.zeros(self.shape())
     base_plane = {
         game_state.next_player: 0,
         game_state.next_player.other: 3
     }
     for row in range(self.board_height):
         for col in range(self.board_width):
             p = Point(row=row + 1, col=col + 1)
             go_string = game_state.board.get_go_string(p)
             if go_string is None:
                 if game_state.does_move_violate_ko(game_state.next_player,
                                                    Move.play(p)):
                     board_tensor[6][row][col] = 1  # <1>
             else:
                 liberty_plane = min(3, go_string.num_liberties) - 1
                 liberty_plane += base_plane[go_string.color]
                 board_tensor[liberty_plane][row][col] = 1  # <2>
     return board_tensor
示例#19
0
 def encode(self, game_state):
     board_tensor = np.zeros(self.shape())
     if game_state.next_player == Player.black:
         board_tensor[8] = 1
     else:
         board_tensor[9] = 1
     for r in range(self.board_height):
         for c in range(self.board_width):
             p = Point(row=r + 1, col=c + 1)
             go_string = game_state.board.get_go_string(p)
             if go_string is None:
                 if game_state.does_move_violate_ko(game_state.next_player,
                                                 Move.play(p)):
                     board_tensor[10][r][c] = 1
             else:
                 liberty_plane = min(4, go_string.num_liberties) - 1
                 if go_string.color == Player.white:
                     liberty_plane += 4
                 board_tensor[liberty_plane][r][c] = 1
     return board_tensor
示例#20
0
from dlgo.goboard import GameState, Move
from dlgo.gotypes import Point
from dlgo.utils import print_board
from dlgo.gosgf.sgf import Sgf_game

sgf_content = "(;GM[1]FF[4]SZ[9];B[ee];W[ef];B[ff];W[df];B[fe];W[fc];B[ec];W[gd];B[fb])"

sgf_game = Sgf_game.from_string(sgf_content)

game_state = GameState.new_game(19)

for item in sgf_game.main_sequence_iter():

    color, move_tuple = item.get_move()
    if color is not None and move_tuple is not None:
        row, col = move_tuple
        point = Point(row + 1, col + 1)
        move = Move.play(point)
        game_state = game_state.apply_move(move)
        print_board(game_state.board)
示例#21
0
def gtp_position_to_coords(gtp_position):
    col_str, row_str = gtp_position[0], gtp_position[1:]
    point = Point(int(row_str), COLS.find(col_str.upper()) + 1)
    return Move(point)