示例#1
0
文件: Engine.py 项目: akshay0190/EJGo
    def generate_move(self, color, cleanup=False):
        self.push_state()
        if channel_from_color(color) == gvg.player_channel:
            self.board = go_board.switch_player_perspec(self.board)
        move = self.pick_move(color)
        if move.is_play():
            go_board.make_move(self.board, [move.x, move.y], gvg.bot_channel,
                               gvg.player_channel)

        go_board.show_board(self.board)
        return Move(move.x, move.y)
示例#2
0
文件: utils.py 项目: akshay0190/EJGo
def games_to_states(game_data):
    train_boards = []
    train_next_moves = []
    for game_index in range(len(game_data)):
        board = go_board.setup_board(game_data[game_index])
        for node in game_data[game_index].get_main_sequence():
            board = go_board.switch_player_perspec(
                board
            )  # Changes player perspective, black becomes white and vice versa

            node_move = node.get_move()[1]
            if node_move is not None:
                train_boards.append(np.copy(board))
                next_move = np.zeros(gvg.board_size * gvg.board_size).reshape(
                    gvg.board_size, gvg.board_size)
                next_move[node_move[0], node_move[
                    1]] = gvg.filled  # y = an array in the form [board_x_position, board_y_position]
                train_next_moves.append(
                    next_move.reshape(gvg.board_size * gvg.board_size))

                board = go_board.make_move(
                    board, node_move, gvg.bot_channel,
                    gvg.player_channel)  # Update board with new move
                if board is None:
                    print("ERROR! Illegal move, {}, while training".format(
                        node_move))

    return train_boards, train_next_moves
示例#3
0
文件: Engine.py 项目: akshay0190/EJGo
 def stone_played(self, x, y, color):
     self.push_state()
     if channel_from_color(color) == gvg.bot_channel:
         if x > -1 and y > -1:  # As long as we don't pass, make the move on the board
             go_board.make_move(self.board, [x, y], gvg.bot_channel,
                                gvg.player_channel)
     elif channel_from_color(color) == gvg.player_channel:
         if x > -1 and y > -1:  # If enemy doesn't pass
             go_board.make_move(self.board, [x, y], gvg.player_channel,
                                gvg.bot_channel)
             self.opponent_passed = False
         elif x == -1 and y == -1:  # If enemy passes
             self.opponent_passed = True
     else:
         print("ERROR!", channel_from_color(color),
               "is not a valid channel")
     go_board.show_board(self.board)
示例#4
0
def take_turn(player_move, set_stones=None):
    player_move[0] -= 1 # Change from 1-19 to 0-18
    player_move[1] -= 1
    if len(player_move) != 2:
        print("Please enter 2 numbers")
    elif player_move[0] < 0 or player_move[0] > 18 or player_move[1] < 0 or player_move[1] > 18:
        print("That move was out of range")
    elif board[player_move[0], player_move[1], gvg.player_channel] == gvg.filled:
        print("That spot is already occupied by the player")
    elif board[player_move[0], player_move[1], gvg.bot_channel] == gvg.filled:
        print("That spot is already occupied by the bot")
    else:
        go_board.make_move(board, player_move, gvg.player_channel, gvg.bot_channel)
        bot_move = np.asarray(go_learn.predict_move(board, model))
        if board[bot_move[0]][bot_move[1]][gvg.player_channel] == gvg.filled:
            print("Bot tried to move on player space")
        else:
            go_board.make_move(board, bot_move, gvg.bot_channel, gvg.player_channel)
            print("Bot move: ", bot_move + 1)
        if set_stones != None:
            set_stones(board)
示例#5
0
def predict_move(orig_board, model, level=0, prob_board=None):
    if prob_board is None:
        prob_board = np.array(
            model.predict(go_board.get_encoded_board(orig_board))).reshape(
                gvg.board_size, gvg.board_size)

    found_move = False
    while found_move == False:
        move = nanargmax(prob_board)
        if go_board.make_move(orig_board,
                              move,
                              gvg.bot_channel,
                              gvg.player_channel,
                              result_only=True) == False:
            prob_board[move[0]][move[1]] = -999999.0
        else:
            found_move = True

    return move
示例#6
0
def train_network(game_data, model):
    vs = gvg.validation_split  # What fraction of games are reserved for validation

    hm_batches = int(len(game_data) / batch_size)

    train_boards = []
    train_next_moves = []
    for game_index in range(len(game_data)):
        board = go_board.setup_board(game_data[game_index])
        for node in game_data[game_index].get_main_sequence():
            board = go_board.switch_player_perspec(
                board
            )  # Changes player perspective, black becomes white and vice versa

            node_move = node.get_move()[1]
            if node_move is not None:
                train_boards.append(go_board.get_encoded_board(board))

                next_move = np.zeros(gvg.board_size * gvg.board_size).reshape(
                    gvg.board_size, gvg.board_size)
                next_move[node_move[0], node_move[
                    1]] = gvg.filled  # y = an array in the form [board_x_position, board_y_position]
                train_next_moves.append(
                    next_move.reshape(gvg.board_size * gvg.board_size))

                board = go_board.make_move(
                    board, node_move, gvg.bot_channel,
                    gvg.player_channel)  # Update board with new move
                if board is None:
                    print("ERROR! Illegal move, {}, while training".format(
                        node_move[1]))
            if len(train_boards
                   ) >= batch_size:  # Send chunk to GPU at batch limit
                model.fit(
                    {"input": train_boards[:int(-len(train_boards) * vs)]}, {
                        "target":
                        train_next_moves[:int(-len(train_boards) * vs)]
                    },
                    validation_set=({
                        "input":
                        train_boards[int(-len(train_boards) * vs):]
                    }, {
                        "target":
                        train_next_moves[int(-len(train_boards) * vs):]
                    }),
                    n_epoch=1,
                    batch_size=gvg.train_batch_size,
                    snapshot_step=7500,
                    show_metric=True)
                train_boards = []
                train_next_moves = []

    # Finish of what is remaining in the batch
    model.fit({"input": train_boards[:int(-len(train_boards) * vs)]},
              {"target": train_next_moves[:int(-len(train_boards) * vs)]},
              validation_set=({
                  "input":
                  train_boards[int(-len(train_boards) * vs):]
              }, {
                  "target":
                  train_next_moves[int(-len(train_boards) * vs):]
              }),
              n_epoch=1,
              batch_size=gvg.train_batch_size,
              snapshot_step=7500,
              show_metric=True)
示例#7
0
            board = go_board.switch_player_perspec(
                board
            )  # Changes player perspective, black becomes white and vice versa

            node_move = node.get_move()[1]
            if node_move is not None:
                train_boards.append(go_board.get_encoded_board(board))
                next_move = np.zeros(gvg.board_size * gvg.board_size).reshape(
                    gvg.board_size, gvg.board_size)
                next_move[node_move[0], node_move[
                    1]] = gvg.filled  # y = an array in the form [board_x_position, board_y_position]
                train_next_moves.append(
                    next_move.reshape(gvg.board_size * gvg.board_size))

                board = go_board.make_move(
                    board, node_move, gvg.bot_channel,
                    gvg.player_channel)  # Update board with new move
                if board is None:
                    print("ERROR! Illegal move, {}, while training".format(
                        node_move))
    print("Finished data processing...")

    print("Begin testing...")
    for i in range(len(train_boards)):
        pred = np.asarray(model.predict(train_boards[i].reshape(1, gvg.board_size, gvg.board_size, gvg.enc_board_channels))) \
        .reshape(gvg.board_size * gvg.board_size)
        if pred.argmax() == train_next_moves[i].argmax():
            correct += 1
        total += 1
print("Accuracy: {}".format(correct / total))
print("Finished testing")