def get_MCTS_winrate():
    '''
    Returns plot of winning percentage for different itermax sizes against a
    random player.
    '''
    winrate = []
    # number of games played in total=
    num_games = 50
    for itermax in range(start, stop, increment):
        wins = 0
        for game in range(num_games):
            Board = connect4.Board()

            winner = start_game(Board, ["MCTS", "Random"], itermax=itermax, print_winner=False)
            if winner == 0:
                wins += 1
        winrate.append(wins/num_games * 100)
    #plot results
    plt.plot(range(start, stop, increment), winrate, '.')
    plt.xlabel("Itermax Size")
    plt.ylabel("Win Percentage (%)")
    plt.title("MCTS Win Rate Against Random by Itermax Value")
    fig = plt.gcf()
    fig.savefig("MCTS_winrate.png")
    plt.show()
def get_MCTS_runtime(start, stop, increment):
    '''
    Returns plot of runtime for different itermax sizes.
    '''
    runtimes = []

    for itermax in range(start, stop, increment):
        board = connect4.Board()
        node = Node(board=board)
        turn = len(board.history)
        node.turn = turn % 2
        node.piece = node.pieces[node.turn]

        # node, move = MCTS(board, itermax, node)
        t = timeit.Timer('MCTS(board, itermax, node)', setup='from __main__ import MCTS', globals=locals())

        runtime = t.timeit(1)
        print("Runtime added")
        runtimes.append(runtime)

    #plot results
    plt.plot(range(start, stop, increment), runtimes, '.')
    plt.xlabel("Itermax Size")
    plt.ylabel("Runtime (seconds)")
    plt.title("MCTS Move Time by Itermax Value")
    fig = plt.gcf()
    fig.savefig("turn_MCTS_runtimes.png")
    plt.show()
def train_MCTS(games_to_play, itermax):
    games = []
    winrate = []
    node = Node(board=connect4.Board())
    wins = 0
    for game in range(games_to_play):
        Board = connect4.Board()
        winner = start_game(Board, ["MCTS", "Random"], node=node, itermax=itermax, timeout=3600, print_winner=False)
        if winner == 0:
            wins += 1
        if game % 100 == 0:
            games.append(game+1)
            winrate.append(wins/(game+1))

    plt.plot(games, winrate, '.')
    plt.xlabel("Games Played")
    plt.ylabel("Win Percentage (%)")
    plt.title("MCTS Win Rate (Itermax {})".format(itermax))
    fig = plt.gcf()
    fig.savefig("MCTS_winrate_build.png")
    plt.show()
Exemplo n.º 4
0
def MCTS(state):
    """ Performs MCTS by sampling games and calling the appropriate functions to construct the game tree.

    Args:
        board:  The game setup.

    Returns:    The action to be taken.

    """
    board = connect4.Board()
    winning_moves = board.get_winning_moves(state)
    if winning_moves:
        print("E4-4U goes: " + str(winning_moves[0][0]))
        return winning_moves[0][0]
        
    root_node = MCTSNode(   state = deepcopy(state),
                            turn = player,
                            parent=None,
                            parent_action=None,
                            untried_actions=board.get_valid_moves(state))

    for _ in range(num_nodes):
        node = root_node

        leaf_node = traverse_nodes(node)
        if leaf_node.untried_actions:
            leaf_node = expand_leaf(leaf_node, board)
        winner = rollout(board, leaf_node)
        won = True if winner == player else False
        backpropagate(leaf_node,won)
        # board.print_board(leaf_node.state)


    action = get_child_by_winrate(root_node).parent_action
    # print(root_node.child_nodes)
    print("E4-4U goes: " + str(action))

    return action
Exemplo n.º 5
0
from MCTS import MCTS
import connect4
from random import choice
import time

player = connect4.player
enemy = connect4.enemy

b = connect4.Board()
state = b.state
j = 0

while not b.is_ended(b.state)[0]:
    if j % 2 == 0:
        start = time.time()
        nextmove = MCTS(b.state)
        end = time.time()
        print("Total time: " + str(end - start))
        state = b.do_move(state, player, nextmove)
    else:
        # b.print_board(state)
        # print("0 1 2 3 4 5 6")
        # col = int(input())
        # row = int(input())
        # move = (col,row)
        # state = b.do_move(state,enemy,move)
        state = b.do_move(state, enemy, choice(b.get_valid_moves(state)))
    b.print_board(state)
    j += 1

print(b.is_ended(b.state))
Exemplo n.º 6
0
import connect4
from tensorflow.keras.models import model_from_json

with open("saved_model.json", "r") as file:
    loaded_model = model_from_json(file.read())
    game = connect4.Board(connect4.WIDTH, connect4.HEIGHT,
                          connect4.AI(1, loaded_model),
                          connect4.Human_Player(2))
    game.run_board()
    print("Switching sides...")
    game = connect4.Board(connect4.WIDTH, connect4.HEIGHT,
                          connect4.Human_Player(1),
                          connect4.AI(2, loaded_model))
    game.run_board()