예제 #1
0
def analyze_game_first_move_working_version_for_heatmap_animation(file_name):
    a = np.zeros((3, 3))
    games = UT.read_games(file_name)
    first_move_stats_dict = {}
    T = 0

    m = 0
    fig = plt.figure()
    for each_game in games:
        if each_game.get("winner") != "D":
            r, c = each_game.get("sequence")[0].get("xy")
            rc_move = (r, c)
            first_move_stats_dict[rc_move] = first_move_stats_dict.get(
                rc_move, 0) + 1
            a[r][c] += 1
            T += 1
            if m % 10 == 0:
                draw_heat_map(a, T)
                plt.draw()
                plt.pause(0.01)
                plt.clf()

    print("Total Count: %d" % T)
    plt.show()
    # plt.imshow(a, cmap='hot', interpolation='nearest')

    return first_move_stats_dict, a
예제 #2
0
def analyze_game(file_name):
    games = UT.read_games(file_name)
    winning_count = {}
    average_num_moves_per_game = {}
    first_move_stats = {}
    for i in range(len(games)):
        each_game = games[i]
        a_player = each_game.get("winner")
        num_moves = len(each_game.get("sequence"))
        if a_player in average_num_moves_per_game:
            average_num_moves_per_game[a_player].append(num_moves)
        else:
            average_num_moves_per_game[a_player] = [num_moves]
        winning_count[a_player] = winning_count.get(a_player, 0) + 1
        if a_player != "Draw":
            first_move_info = each_game.get("sequence")[0]
            first_move = tuple(first_move_info.get("xy"))
            first_move_stats[first_move] = first_move_stats.get(first_move,
                                                                0) + 1

    move_tree = build_session_second_degree(games)
    first_move_stats_sorted = sorted(first_move_stats.items(),
                                     key=lambda kv: kv[1],
                                     reverse=True)
    return winning_count, move_tree, average_num_moves_per_game, first_move_stats_sorted
예제 #3
0
def parse_data_example(filename):
    # games is a list, an array, where each cell in the array holds a dictionary format for each game per line from the filename
    games = UT.read_games(filename)
    winning_count = {}
    x_wins = 0
    o_wins = 0
    ties = 0

    winning_count_dict = {}

    for i in range(len(games)):
        ## each i holds a game log, which is represented in a dictionary format
        each_game = games[i]
        # once you have a dictionary obj, you can retrieve a value for a key by
        # adict.get("mykey") <-- adict is a dictionary obj; "mykey" is a key name000
        player_winner = each_game.get("winner")

        winning_count_dict[player_winner] = winning_count_dict.get(
            player_winner, 0) + 1

        if player_winner == "X":
            x_wins += 1
        elif player_winner == "O":
            o_wins += 1
        elif player_winner == "D":
            ties += 1

        game_sequence_in_list = each_game.get("sequence")
        play_modes = each_game.get("play_modes")

        print("Winner is: %s. First move: %s." %
              (player_winner, game_sequence_in_list[0].get("turn")))
    print("X win-rate:", winning_count_dict.get("X") / len(games))
    print("O win-rate:", o_wins / len(games))
    print("Draw rate:", ties / len(games))
예제 #4
0
def animation_heatmap(file_name):

    games = UT.read_games(file_name)
    games_win_by_O = []
    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
    T = len(games)
    focused_result = "O"
    fig = plt.figure()


    skip_by = 1
    c_ = 0
    board_size = 5
    
    first_move_winingstat = np.zeros((board_size, board_size))

    for each_game in games:
        if each_game.get("winner") == focused_result:  # and c_ % skip_by == 0 :
            games_win_by_O.append(each_game)
            if c_ == 0:
                board_size = each_game.get("board_size", 5)
            
            r, c = each_game.get("sequence")[0].get("xy")
            first_move_winingstat[r][c]  += 1
            
            c_ += 1

    #print(np.array(first_move_winingstat)/c_)
    T = len(games_win_by_O)
    a = np.zeros((board_size, board_size))


    index_to_keep_processed ={}
    def animate(i):
        global C
        each_game_local = games_win_by_O[i]  # games[i]
        
        # somehow this callback function cals the first element (e.g. at index 0) twice;
        if i in index_to_keep_processed:
            return
        else:
            index_to_keep_processed[i] = 1

        #print("focused player: %s, this_game_player: %s" % (focused_result, each_game.get("winner")))

        plt.clf()
        #print("HHH")

        if each_game_local.get("winner") == focused_result:
            r, c = each_game_local.get("sequence")[0].get("xy")
            a[r][c] += 1
            C += 1
            draw_heat_map(a, C)

    anim = animation.FuncAnimation(fig, animate, frames=T, interval=10)
    anim.save('first_move_winningRate_animation.mp4', writer=writer)
    print("Total Win: %d" % len(games_win_by_O))
    print(np.divide(a, len(games_win_by_O)))
def analyze_game(file_name):
    games = UT.read_games(file_name)
    winning_count = {}
    for i in range(len(games)):
        each_game = games[i]
        a_player = each_game.get("winner")
        winning_count[a_player] = winning_count.get(a_player, 0) + 1
    move_tree = build_session_second_degree(games)
    return winning_count, move_tree
예제 #6
0
def animation_heatmap(file_name):
    a = np.zeros((3, 3))
    games = UT.read_games(file_name)
    games_win_by_O = []
    Writer = animation.writers['ffmpeg']
    writer = Writer(fps=15, metadata=dict(artist='Me'), bitrate=1800)
    T = len(games)
    focused_result = "O"
    fig = plt.figure()

    skip_by = 1
    c_ = 0
    for each_game in games:
        if each_game.get(
                "winner") == focused_result:  # and c_ % skip_by == 0 :
            games_win_by_O.append(each_game)
            c_ += 1

    T = len(games_win_by_O)

    def animate(i):
        global C
        each_game = games_win_by_O[i]  # games[i]
        #print("focused player: %s, this_game_player: %s" % (focused_result, each_game.get("winner")))

        plt.clf()

        if each_game.get("winner") == focused_result:
            r, c = each_game.get("sequence")[0].get("xy")
            a[r][c] += 1
            C += 1
            draw_heat_map(a, C)

    anim = animation.FuncAnimation(fig, animate, frames=T, interval=10)
    anim.save('first_move_winningRate_animation.mp4', writer=writer)
    print("Total Win: %d" % C)
예제 #7
0
def parse_jsons_example(filename):
    # games is a list, an array, where each cell in the array holds a dictionary format for each game per line from the filename
    games_in_jsons = UT.read_games(filename)
    list_of_dic = []
    training_samples = []
    weight_of_lost_samples_from_x = 10
    weight_of_winning_samples_from_o = 20
    moves_to_prevent_losing = []
    moves_to_enhance_winning = []
    for i in range(len(games_in_jsons)):
        # TO-DO
        # 1) create an-empty list for init-game
        # 2) create list of list

        ## each i holds a game log, which is represented in a dictionary format
        each_game_in_a_json = games_in_jsons[i]
        # once you have a dictionary obj, you can retrieve a value for a key by
        # adict.get("mykey") <-- adict is a dictionary obj; "mykey" is a key name000

        dict_move_set = {}
        dict_move_set["move_sequence"] = None
        dict_move_set["player_winner"] = None
        board_size = each_game_in_a_json.get("board_size", 5)
        individual_sequence = ["_"] * (board_size**2)
        list_of_individual_sequence = []

        player_winner = each_game_in_a_json.get("winner")
        moves_in_sequence_per_game = each_game_in_a_json.get("sequence")

        copy_list = individual_sequence[:]
        list_of_individual_sequence.append(updated_list_parse(copy_list))

        for each_move_dict in range(len(moves_in_sequence_per_game)):

            #list_of_individual_sequence.append(individual_sequence)
            turn_for_this_move = moves_in_sequence_per_game[
                each_move_dict].get("turn")
            move_made_for_this_move = moves_in_sequence_per_game[
                each_move_dict].get("position")
            # for this game and movements so far, a list is created... and then
            # need to be appended to the list of list
            individual_sequence[move_made_for_this_move -
                                1] = turn_for_this_move
            copy_list = individual_sequence[:]
            current_state = updated_list_parse(copy_list)
            list_of_individual_sequence.append(current_state)

            # for moves from each game, we can generate training data for player O
            # every even index from the sequence is for player O, except 0
            if player_winner == "O":
                if each_move_dict > 0 and each_move_dict % 2 == 1:
                    # then each_move_dict-1 is x; and each_move_dict is y indices
                    x = list_of_individual_sequence[-2]
                    y = list_of_individual_sequence[-1]
                    multi_y = np.subtract(y, x)
                    a_max = np.amax(multi_y)
                    multi_y = np.true_divide(multi_y, a_max).astype(int)
                    training_samples.append((x, y, multi_y))

        # Now we can collect another data by replacing a winning position made by X with O
        # that is, final third position becomes x; and the final postion made by X becomes Y
        # we can work on the "list_of_individual_sequence", not above for loop
        if player_winner == "X":
            x = list_of_individual_sequence[-3]
            ## y_1 will contains 2 and 1 -- 2 for O and 1 for X;
            ## so we only need to find a position of 1 for y
            y_1 = list_of_individual_sequence[-1]
            multi_y = np.subtract(y_1, x)
            final_position_made_by_x = np.where(multi_y == 1)[0][0]
            y = x[:]
            y[final_position_made_by_x] = 2
            multi_y = get_multi_class_format(x, y)
            moves_to_prevent_losing.append((x, y, multi_y))
            for a_weight in range(weight_of_lost_samples_from_x):
                training_samples.append((x, y, multi_y))

        # Now I can add more weight to winner of "O" for the last two movements
        # to let the game know the winning position explicitly
        if player_winner == "O":
            x = list_of_individual_sequence[-2]
            y = list_of_individual_sequence[-1]
            multi_y = get_multi_class_format(x, y)
            moves_to_enhance_winning.append((x, y, multi_y))
            for a_weight in range(weight_of_winning_samples_from_o):
                training_samples.append((x, y, multi_y))

        dict_move_set["player_winner"] = player_winner
        dict_move_set["move_sequence"] = list_of_individual_sequence
        #(list_of_individual_sequence)
        list_of_dic.append(dict_move_set)
    focused_samples = [moves_to_enhance_winning, moves_to_prevent_losing]
    return list_of_dic, training_samples, focused_samples
예제 #8
0
def visualize_historical_games(filename):
    for each_game in UT.read_games(file_name):
        parse_history(each_game)
예제 #9
0
                "Starting simulation of Ramdom_MCTS_bigger_board -- %d times" %
                args.n)
            start_time = time.time()
            for i in range(10):
                #t = threading.Thread(target=RANDOM_vs_MCTS_bigger_board_thread, args=(args.n,))
                #threads.append(t)
                #t.start()
                t = multiprocessing.Process(
                    target=RANDOM_vs_MCTS_bigger_board_thread, args=(args.n, ))
                time.sleep(1)
                procs.append(t)
                t.start()

            print("Elapsed time: %d" % (time.time() - start_time))
            #[proc.join() for proc in threads]
            [proc.join() for proc in procs]

        #run_n_simulations(3, "random_vs_mcts")
        #human_vs_MCTS()
        #MCTS_vs_MODEL(1000)
        #MCTS_vs_MCTS()
        #MCTS_vs_RANDOM(100)
    elif GAME_MODE == LOAD_PLAY:
        all_games = UT.read_games("./game_output.log")
        sampled_games = np.random.choice(all_games, 3)
        c = 0
        print(len(sampled_games))
        for index, each_item in enumerate(sampled_games):
            Game.parse_history(each_item, index)
            print(c)
예제 #10
0
def analyze_game(file_name):
    games = UT.read_games(file_name)
    winning_count = {}
    for each_game in games:
        a_player = each_game.get("winner")
        winning_count[a_player] = winning_count.get(a_player) + 1
예제 #11
0
def parse_jsons_example(filename):
    # games is a list, an array, where each cell in the array holds a dictionary format for each game per line from the filename
    games_in_jsons = UT.read_games(filename)
    list_of_dic = []
    training_samples =[]

    for i  in range(len(games_in_jsons)):
        # TO-DO
        # 1) create an-empty list for init-game
        # 2) create list of list

        dict_move_set = {}
        dict_move_set["move_sequence"] = None
        dict_move_set["player_winner"] = None

        individual_sequence = ["_"] * 9
        list_of_individual_sequence = []

        ## each i holds a game log, which is represented in a dictionary format
        each_game_in_a_json = games_in_jsons[i]
        # once you have a dictionary obj, you can retrieve a value for a key by
        # adict.get("mykey") <-- adict is a dictionary obj; "mykey" is a key name000

        player_winner = each_game_in_a_json.get("winner")
        moves_in_sequence_per_game = each_game_in_a_json.get("sequence")

        copy_list = individual_sequence[:]
        list_of_individual_sequence.append(updated_list_parse(copy_list))

        for each_move_dict in range(len(moves_in_sequence_per_game)):

            #list_of_individual_sequence.append(individual_sequence)
            turn_for_this_move = moves_in_sequence_per_game[each_move_dict].get("turn")
            move_made_for_this_move = moves_in_sequence_per_game[each_move_dict].get("position")
            # for this game and movements so far, a list is created... and then
            # need to be appended to the list of list
            individual_sequence[move_made_for_this_move - 1] = turn_for_this_move
            copy_list = individual_sequence[:]
            current_state = updated_list_parse(copy_list)
            list_of_individual_sequence.append(current_state)

            # for moves from each game, we can generate training data for player O
            # every even index from the sequence is for player O, except 0
            if player_winner == "O":
                if each_move_dict > 0 and each_move_dict % 2 == 1:
                    # then each_move_dict-1 is x; and each_move_dict is y indices
                    x = list_of_individual_sequence[-2]
                    y = list_of_individual_sequence[-1]
                    multi_y = np.subtract(y, x)
                    a_max = np.amax(multi_y)
                    multi_y = np.true_divide(multi_y, a_max).astype(int)
                    training_samples.append((x, y, multi_y))

        dict_move_set["player_winner"] = player_winner
        dict_move_set["move_sequence"] = list_of_individual_sequence

        list_of_dic.append(dict_move_set)



    return list_of_dic, training_samples