Esempio n. 1
0
def main():

    HEURISTICS = [("Null", null_score),
                  ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [Agent(CustomPlayer(score_fn=h, **MM_ARGS),
                       "MM_" + name) for name, h in HEURISTICS]
    ab_agents = [Agent(CustomPlayer(score_fn=h, **AB_ARGS),
                       "AB_" + name) for name, h in HEURISTICS]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    test_agents = [Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"),
                   Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")]

    print(DESCRIPTION)
    for agentUT in test_agents:
        print("")
        print("*************************")
        print("{:^25}".format("Evaluating: " + agentUT.name))
        print("*************************")

        agents = random_agents + mm_agents + ab_agents + [agentUT]
        win_ratio = play_round(agents, NUM_MATCHES)

        print("\n\nResults:")
        print("----------")
        print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))

        if agentUT.name == 'ID_Improved' or agentUT.name == 'Student':
            searches = len(agentUT.player.average_depths)
            total_depth = sum(agentUT.player.average_depths)
            avg = total_depth / searches
            print('{} Avg Depth: {}'.format(agentUT.name, avg))
Esempio n. 2
0
def worker():

    # create an isolation board (by default 7x7)
    player1 = RandomPlayer()
    player2 = GreedyPlayer()
    game = Board(player1, player2)
    game.apply_move((2, 3))
    q.put(game)
    sleep(2)
    game.apply_move((0, 5))
    q.put(game)
    sleep(2)

    # play the remainder of the game automatically -- outcome can be "illegal
    # move" or "timeout"; it should _always_ be "illegal move" in this example
    winner, history, outcome = game.play(q)
    print("\nWinner: {}\nOutcome: {}".format(winner, outcome))
Esempio n. 3
0
def main():

    # Define two agents to compare -- these agents will play from the same
    # starting position against the same adversaries in the tournament
    test_agents = [
        Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved"),
        Agent(AlphaBetaPlayer(score_fn=custom_score), "AB_Custom"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_2), "AB_Custom_2"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_3), "AB_Custom_3"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_4), "AB_Custom_4"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_5), "AB_Custom_5"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_6), "AB_Custom_6"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_7), "AB_Custom_7")
    ]

    # Define a collection of agents to compete against the test agents
    cpu_agents = [
        Agent(RandomPlayer(), "Random"),
        Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open"),
        Agent(MinimaxPlayer(score_fn=center_score), "MM_Center"),
        Agent(MinimaxPlayer(score_fn=improved_score), "MM_Improved"),
        Agent(AlphaBetaPlayer(score_fn=open_move_score), "AB_Open"),
        Agent(AlphaBetaPlayer(score_fn=center_score), "AB_Center"),
        Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved")
    ]

    # test_agents = [
    #     Agent(AlphaBetaPlayer(score_fn=custom_score), "AB_Custom"),
    #     Agent(AlphaBetaPlayer(score_fn=custom_score_2), "AB_Custom_2"),
    #     Agent(AlphaBetaPlayer(score_fn=custom_score_3), "AB_Custom_3"),
    #     Agent(AlphaBetaPlayer(score_fn=custom_score_4), "AB_Custom_4"),
    #     Agent(AlphaBetaPlayer(score_fn=custom_score_5), "AB_Custom_5"),
    #     Agent(AlphaBetaPlayer(score_fn=custom_score_6), "AB_Custom_6"),
    #     Agent(AlphaBetaPlayer(score_fn=custom_score_7), "AB_Custom_7")
    # ]

    # cpu_agents = [
    #     Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved")
    # ]

    print(DESCRIPTION)
    print("{:^74}".format("*************************"))
    print("{:^74}".format("Playing Matches"))
    print("{:^74}".format("*************************"))
    play_matches(cpu_agents, test_agents, NUM_MATCHES)
Esempio n. 4
0
def main():

    # Define two agents to compare -- these agents will play from the same
    # starting position against the same adversaries in the tournament
    test_agents = [
        Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved"),
        Agent(AlphaBetaPlayer(score_fn=my_moves_heuristic),
              "my_moves_heuristic"),
        Agent(AlphaBetaPlayer(score_fn=maximise_player_moves),
              "maximise_player_moves"),
        Agent(AlphaBetaPlayer(score_fn=minimise_opponent_moves),
              "minimise_opponent_moves"),
        Agent(
            AlphaBetaPlayer(
                score_fn=maximise_ratio_of_player_to_opponent_moves),
            "maximise_ratio_of_player_to_opponent_moves"),
        Agent(
            AlphaBetaPlayer(
                score_fn=minimise_ratio_of_player_to_opponent_moves),
            "minimise_ratio_of_player_to_opponent_moves")
    ]

    # Define a collection of agents to compete against the test agents
    cpu_agents = [
        Agent(RandomPlayer(), "Random"),
        Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open"),
        Agent(MinimaxPlayer(score_fn=center_score), "MM_Center"),
        Agent(MinimaxPlayer(score_fn=improved_score), "MM_Improved"),
        Agent(AlphaBetaPlayer(score_fn=open_move_score), "AB_Open"),
        Agent(AlphaBetaPlayer(score_fn=center_score), "AB_Center"),
        Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved")
    ]

    print(DESCRIPTION)
    print("{:^74}".format("*************************"))
    print("{:^74}".format("Playing Matches"))
    print("{:^74}".format("*************************"))
    print()
    play_matches(cpu_agents, test_agents, NUM_MATCHES)
Esempio n. 5
0
def main():

    # Define two agents to compare -- these agents will play from the same
    # starting position against the same adversaries in the tournament
    test_agents = [
        Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved"),
        Agent(AlphaBetaPlayer(score_fn=custom_score), "AB_Custom"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_2), "AB_Custom_2"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_3), "AB_Custom_3")
    ]

    """
    Uncomment to add grid search function agents to test_agents
    """
    # print("{} grid search functions".format(len(gs_funcs)))
    # for name, func in gs_funcs.items():
    #     test_agents.append(Agent(AlphaBetaPlayer(score_fn=func), "AB_{}".format(name)))

    # print("{} grid search2 functions".format(len(gs2_funcs)))
    # for name, func in gs2_funcs.items():
    #     test_agents.append(Agent(AlphaBetaPlayer(score_fn=func), "AB_GS2_{}".format(name)))

    # Define a collection of agents to compete against the test agents
    cpu_agents = [
        Agent(RandomPlayer(), "Random"),
        Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open"),
        Agent(MinimaxPlayer(score_fn=center_score), "MM_Center"),
        Agent(MinimaxPlayer(score_fn=improved_score), "MM_Improved"),
        Agent(AlphaBetaPlayer(score_fn=open_move_score), "AB_Open"),
        Agent(AlphaBetaPlayer(score_fn=center_score), "AB_Center"),
        Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved")
    ]

    print(DESCRIPTION)
    print("{:^74}".format("*************************"))
    print("{:^74}".format("Playing Matches"))
    print("{:^74}".format("*************************"))
    play_matches(cpu_agents, test_agents, NUM_MATCHES)
Esempio n. 6
0
def prepare_default_players():
    # Define two agents to compare -- these agents will play from the same
    # starting position against the same adversaries in the tournament
    test_agents = [
        Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved"),
        Agent(AdvancedAlphaBetaNegamaxPlayer(score_fn=custom_score_2),
              "TranspositionTable"),
        Agent(AdvancedAlphaBetaNegamaxPlayer_V2(score_fn=custom_score_2),
              "OrderedMoves"),
        Agent(AlphaBetaPlayer(score_fn=custom_score_3), "AB_Custom_3")
    ]

    # # Define a collection of agents to compete against the test agents
    cpu_agents = [
        Agent(RandomPlayer(), "Random"),
        Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open"),
        Agent(MinimaxPlayer(score_fn=center_score), "MM_Center"),
        Agent(MinimaxPlayer(score_fn=improved_score), "MM_Improved"),
        Agent(AlphaBetaPlayer(score_fn=open_move_score), "AB_Open"),
        Agent(AlphaBetaPlayer(score_fn=center_score), "AB_Center"),
        Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved")
    ]

    return test_agents, cpu_agents
Esempio n. 7
0
def main():
    for tour_num in range(NUM_TOURNAMENTS):
        HEURISTICS = [("Null", null_score),
                      ("Open", open_move_score),
                      ("Improved", improved_score)]
        AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
        MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
        CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

        mm_agents = [Agent(CustomPlayer(score_fn=h, **MM_ARGS),
                           "MM_" + name) for name, h in HEURISTICS]
        ab_agents = [Agent(CustomPlayer(score_fn=h, **AB_ARGS),
                           "AB_" + name) for name, h in HEURISTICS]
        random_agents = [Agent(RandomPlayer(), "Random")]

        test_agents = [Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"),
                       Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")]

        for agentUT in test_agents:
            agents = random_agents + mm_agents + ab_agents + [agentUT]
            win_ratio = play_round(agents, NUM_MATCHES)
            print("{:>10.2f}".format(win_ratio))

        print("-----")
Esempio n. 8
0
import timeit

from isolation import Board
from sample_players import (RandomPlayer, open_move_score,
                            improved_score, center_score)

from game_agent import (MinimaxPlayer, AlphaBetaPlayer, custom_score,
                        custom_score_2, custom_score_3)


# Create the test players
player1 = AlphaBetaPlayer(score_fn=improved_score)
player2 = RandomPlayer()

# Create an isolation board (by default 7x7)
game = Board(player1, player2, 8, 8)

forfeited_match = [[2, 3], [4, 4], [0, 4], [5, 6], [2, 5], [6, 4],
                   [1, 3], [4, 5], [0, 1], [2, 6], [2, 2]]

for move in forfeited_match:
   game.apply_move(move)

# print(game.get_legal_moves())
# print(len(game.get_legal_moves()))

score = custom_score_2(game, player1)

time_millis = lambda: 1000 * timeit.default_timer()
move_start = time_millis()
time_left = lambda: 100 - (time_millis() - move_start)
Esempio n. 9
0
 def setUp(self):
     reload(game_agent)
     self.player1 = AlphaBetaPlayer()
     self.player2 = RandomPlayer()
     self.game = isolation.Board(self.player1, self.player2)
Esempio n. 10
0
Created on Sat Jan  6 20:58:45 2018

@author: Kelvin
"""

from game_agent import (MinimaxPlayer, AlphaBetaPlayer, custom_score,
                        custom_score_2, custom_score_3, custom_score_4,
                        custom_score_5)

from sample_players import (RandomPlayer, open_move_score, improved_score,
                            center_score)
from isolation import Board
import random
import timeit

player1 = RandomPlayer()
player2 = AlphaBetaPlayer(score_fn=improved_score)

player3 = AlphaBetaPlayer(score_fn=custom_score)
player4 = AlphaBetaPlayer(score_fn=custom_score_2)
player5 = AlphaBetaPlayer(score_fn=custom_score_3)
player6 = AlphaBetaPlayer(score_fn=custom_score_4)
player7 = AlphaBetaPlayer(score_fn=custom_score_5)

time_limit = 150
time_millis = lambda: 1000 * timeit.default_timer()

players = [player2, player3, player4, player5, player6, player7]

i = 0
for p in players:
Esempio n. 11
0
def main():

    HEURISTICS = [("Null", null_score), ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [
        Agent(CustomPlayer(score_fn=h, **MM_ARGS), "MM_" + name)
        for name, h in HEURISTICS
    ]
    ab_agents = [
        Agent(CustomPlayer(score_fn=h, **AB_ARGS), "AB_" + name)
        for name, h in HEURISTICS
    ]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    test_agents = [
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved"),
        Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")
    ]

    start_time = time.time()
    set_score = list()
    complete_score = list()
    our_win = 0

    for i in range(5):
        print("\nSet n.{} - Results:".format(i + 1))
        print("----------")

        for agentUT in test_agents:
            agents = random_agents + mm_agents + ab_agents + [agentUT]
            win_ratio = play_round(agents, NUM_MATCHES)
            set_score.append(win_ratio)

            print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))

        print("{!s:<15}{:>10.2f}%".format("Ratio",
                                          (set_score[1] - set_score[0])))
        complete_score.append(set_score[1] - set_score[0])
        if set_score[1] >= set_score[0]:
            our_win = our_win + 1
        set_score.clear()
    if our_win >= 3:
        print("\nYOU WIN! \o/ --> {} - {}".format(our_win, 5 - our_win))
    else:
        print("\nYOU LOSE. :( --> {} - {}".format(our_win, 5 - our_win))
    print("--- Match time: {} minutes ---".format(
        (time.time() - start_time) / 60))
Esempio n. 12
0
def main_mine():

    HEURISTICS = [("Null", null_score),
                  ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [Agent(CustomPlayer(score_fn=h, **MM_ARGS),
                       "MM_" + name) for name, h in HEURISTICS]
    ab_agents = [Agent(CustomPlayer(score_fn=h, **AB_ARGS),
                       "AB_" + name) for name, h in HEURISTICS]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.

    print("NEW RUN ******************************************************************")

    from game_agent import custom_score_diff_in_free_percent_of_board
    from game_agent import custom_score_diff_in_mine_and_double_opponent
    from game_agent import custom_score_diff_in_mine_and_double_opponent_chase_incase_of_tie
    from game_agent import custom_score_diff_in_mine_and_double_opponent_run_away_incase_of_tie
    from game_agent import custom_score_divide_own_by_opponent
    from game_agent import custom_score_my_open_moves
    from game_agent import custom_score_simple
    from game_agent import custom_score_diff_in_mine_and_double_opponent_closest_to_center_tie
    from game_agent import custom_score_diff_in_opp_and_double_mine
    test_agents = [
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"),
        Agent(CustomPlayer(score_fn=custom_score_my_open_moves, **CUSTOM_ARGS), "Student"),
        Agent(CustomPlayer(score_fn=custom_score_simple, **CUSTOM_ARGS), "Student"),
        Agent(CustomPlayer(score_fn=custom_score_diff_in_mine_and_double_opponent, **CUSTOM_ARGS), "Student"),
        Agent(CustomPlayer(score_fn=custom_score_diff_in_opp_and_double_mine, **CUSTOM_ARGS), "Student"),
        Agent(CustomPlayer(score_fn=custom_score_diff_in_mine_and_double_opponent_chase_incase_of_tie, **CUSTOM_ARGS), "Student"),
        Agent(CustomPlayer(score_fn=custom_score_diff_in_mine_and_double_opponent_run_away_incase_of_tie, **CUSTOM_ARGS), "Student"),
        Agent(CustomPlayer(score_fn=custom_score_diff_in_mine_and_double_opponent_closest_to_center_tie, **CUSTOM_ARGS), "Student"),
        Agent(CustomPlayer(score_fn=custom_score_divide_own_by_opponent, **CUSTOM_ARGS), "Student"),
        Agent(CustomPlayer(score_fn=custom_score_diff_in_free_percent_of_board, **CUSTOM_ARGS), "Student")
    ]

    print(DESCRIPTION)
    for agentUT in test_agents:
        print("")
        print("*************************")
        print("{:^25}".format("Evaluating: " + agentUT.name))
        print("*************************")

        agents = random_agents + mm_agents + ab_agents + [agentUT]
        win_ratio = play_round(agents, NUM_MATCHES)

        print("\n\nResults:")
        print("----------")
        print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))
Esempio n. 13
0
def main():

    HEURISTICS = [("Null", null_score), ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}
    PVS_ARGS = {"method": 'pvs', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [
        Agent(CustomPlayer(score_fn=h, **MM_ARGS), "MM_" + name)
        for name, h in HEURISTICS
    ]
    ab_agents = [
        Agent(CustomPlayer(score_fn=h, **AB_ARGS), "AB_" + name)
        for name, h in HEURISTICS
    ]
    random_agents = [
        Agent(RandomPlayer(), "random"),
        Agent(GreedyPlayer(), "greedy")
    ]
    THE_agent = [
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved")
    ]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    #test_agents = [Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"),
    #               Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")]

    test_agents = [
        #Agent(CustomPlayer(score_fn=my_little_killer, **CUSTOM_ARGS), "my_little_killer"),
        #Agent(CustomPlayer(score_fn=you_cant_catch_me_adaptive, **CUSTOM_ARGS), "you_cant_catch_me_adaptive"),
        #Agent(CustomPlayer(score_fn=reversed_aggression, **CUSTOM_ARGS), "reversed_aggression"),
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved"),
        #Agent(CustomPlayer(score_fn=as_far_aggression, **CUSTOM_ARGS), "as_far_aggression"),
        #Agent(CustomPlayer(score_fn=super_improve_score, **CUSTOM_ARGS), "super_improve_score"),
        #Agent(CustomPlayer(score_fn=random_1, **CUSTOM_ARGS), "random"),
        #Agent(CustomPlayer(score_fn=block_in_two, **CUSTOM_ARGS), "block_in_two"),
        Agent(CustomPlayer(score_fn=free_in_two, **CUSTOM_ARGS),
              "free_in_two"),
        Agent(CustomPlayer(score_fn=blind_aggression, **CUSTOM_ARGS), "blind"),
        #Agent(CustomPlayer(score_fn=you_cant_catch_me, **CUSTOM_ARGS), "you_cant_catch_me"),
        #Agent(CustomPlayer(score_fn=central_knight, **CUSTOM_ARGS), "central_knight"),
        #Agent(CustomPlayer(score_fn=division, **CUSTOM_ARGS), "division"),
        Agent(CustomPlayer(score_fn=combine, **CUSTOM_ARGS), "The chosen one"),
        #Agent(CustomPlayer(score_fn=killer_combine, **CUSTOM_ARGS), "killer_combine")
    ]

    print(DESCRIPTION)
    for agentUT in test_agents:
        print("")
        print("*************************")
        print("{:^25}".format("Evaluating: " + agentUT.name))
        print("*************************")

        agents = random_agents + mm_agents + ab_agents + [agentUT]
        human = THE_agent + [Agent(HumanPlayer(), "human")]
        if USE_HUMAN:
            win_ratio = play_round(human, NUM_MATCHES)
        else:
            win_ratio = play_round(agents, NUM_MATCHES)

        print("\n\nResults:")
        print("----------")
        print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))
Esempio n. 14
0
        move_cache[str(
            np.fliplr(flipud_game_state))] = (depth, best_fliplr_flipud_move,
                                              best_val)
        # move_cache[str(np.flipud(fliplr_game_state))] = (depth, best_move, best_val)
        move_cache[str(rot_90)] = (depth, best_rot_90_move, best_val)
        move_cache[str(rot_180)] = (depth, best_rot_180_move, best_val)
        move_cache[str(np.flipud(rot_90))] = (depth, best_flipud_rot_90_move,
                                              best_val)
        move_cache[str(np.flipud(rot_180))] = (depth, best_flipud_rot_180_move,
                                               best_val)


from isolation import Board
from sample_players import RandomPlayer
from sample_players import null_score
if __name__ == "__main__":
    counter = 0
    # (self, search_depth = 3, score_fn = custom_score,
    #                                     iterative = True, method = 'minimax', timeout = 10.)

    customPlayer = CustomPlayer(3, null_score)
    randomPlayer = RandomPlayer()

    game = Board(customPlayer, 3, 3)
    # player_1 = CustomPlayer(2)
    num_trials = orginal_num_trials = 5
    while num_trials > 0:
        num_trials = num_trials - 1
        while True:
            game = Board(customPlayer, randomPlayer)
            game.play()
Esempio n. 15
0
def tryall():
    # Setup all the permutations
    search_depths = [3, 5]
    score_functions = [
        null_score, open_move_score, improved_score, custom_score
    ]
    methods = ["minimax", "alphabeta"]
    iteratives = [False, True]

    # Setup the players:
    player1_agents = []
    player2_agents = [Agent(RandomPlayer(), "random_player")]
    for score_function in score_functions:
        for method in methods:
            for iterative in iteratives:
                for search_depth in search_depths:
                    params = {
                        "search_depth": search_depth,
                        "method": method,
                        "iterative": iterative,
                        "score_fn": score_function
                    }
                    name = "{:16s} / {:9s} / DEPTH({:1d}) / ITER({:1b})".format(
                        score_function.__name__, method, search_depth,
                        iterative)
                    if score_function is improved_score or score_function is custom_score:
                        player1_agents.append(
                            Agent(CustomPlayer(**params), name))
                    else:
                        player2_agents.append(
                            Agent(CustomPlayer(**params), name))

    # Launch the matches for each pair:
    counter = 0
    for player1 in player1_agents:
        for player2 in player2_agents:
            counter += 1
            # Play some games:
            wins = 0.
            num_wins = {player1: 0, player2: 0}
            num_timeouts = {player1: 0, player2: 0}
            num_invalid_moves = {player1: 0, player2: 0}
            print("{:2d}: {:50s}\t--VS--\t {:50s}".format(
                counter, player1.name, player2.name),
                  end=' ')
            for _ in range(0, NUM_MATCHES):
                games = [
                    Board(player1.player, player2.player),
                    Board(player2.player, player1.player)
                ]

                # initialize both games with a random move and response
                for _ in range(2):
                    move = random.choice(games[0].get_legal_moves())
                    games[0].apply_move(move)
                    games[1].apply_move(move)

                # play both games and tally the results
                for game in games:
                    winner, moves, termination = game.play(
                        time_limit=TIME_LIMIT)
                    if player1.player == winner:
                        num_wins[player1] += 1
                        if termination == "timeout":
                            num_timeouts[player2] += 1
                        else:
                            num_invalid_moves[player2] += 1
                    elif player2.player == winner:
                        num_wins[player2] += 1
                        if termination == "timeout":
                            num_timeouts[player1] += 1
                        else:
                            num_invalid_moves[player1] += 1
            winratio = 100 * (num_wins[player1] /
                              (num_wins[player1] + num_wins[player2]))
            print("==>: Wins {:3.0f} % {:5s} ({:2d} to {:2d}) / Timeouts ({:2d} to {:2d})".\
                  format(winratio, \
                         "..|  " if winratio <= 50 else "  |..", \
                         int(num_wins[player1]), int(num_wins[player2]), \
                         int(num_timeouts[player1]), int(num_timeouts[player1])))
Esempio n. 16
0
CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

# Create a collection of CPU agents using fixed-depth minimax or alpha beta
# search, or random selection.  The agent names encode the search method
# (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
# Open=open_move_score, Improved=improved_score). For example, MM_Open is
# an agent using minimax search with the open moves heuristic.
mm_agents = [
    Agent(CustomPlayer(score_fn=h, **MM_ARGS), "MM_" + name)
    for name, h in HEURISTICS
]
ab_agents = [
    Agent(CustomPlayer(score_fn=h, **AB_ARGS), "AB_" + name)
    for name, h in HEURISTICS
]
random_agents = [Agent(RandomPlayer(), "Random")]

# ID_Improved agent is used for comparison to the performance of the
# submitted agent for calibration on the performance across different
# systems; i.e., the performance of the student agent is considered
# relative to the performance of the ID_Improved agent to account for
# faster or slower computers.
# test_agents = [Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"),
#               Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")]
#test_agents = [ Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student"),
#                Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved")]

my_agent = Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")
adversary = Agent(RandomPlayer(), "Random")

# game = Board(my_agent.player, adversary.player,7,7)
def main():

    HEURISTICS = [("Null", null_score),
                  ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}
    # w1 = [1]

    # custom_agents = [Agent(CustomPlayer(w2=h, **CUSTOM_ARGS),
    #                    "custom_" + str(h)) for h in w2_list]
    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [Agent(CustomPlayer(score_fn=h, **MM_ARGS),
                       "MM_" + name) for name, h in HEURISTICS]
    ab_agents = [Agent(CustomPlayer(score_fn=h, **AB_ARGS),
                       "AB_" + name) for name, h in HEURISTICS]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    ID_Improved_agent = [Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved")]
    Student_agent = [Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")]

    print("")
    print("*************************")
    print("{:^25}".format("Evaluating: " + ID_Improved_agent[0].name))
    print("*************************")

    agents = random_agents + mm_agents + ab_agents + ID_Improved_agent
    win_ratio = play_round(agents, NUM_MATCHES)

    print("\n\nResults:")
    print("----------")
    print("{!s:<15}{:>10.2f}%".format(ID_Improved_agent[0].name, win_ratio))


    print(DESCRIPTION)
    print("")
    print("*************************")
    print("{:^25}".format("Evaluating: " + Student_agent[0].name ))
    print("*************************")

    # agents = ID_Improved_agent + Student_agent
    # win_ratio = play_round(agents, NUM_MATCHES)
    w2_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]
    w3_list = [0, 0.5, 1, 1.5, 2, 2.5, 3, 3.5, 4]
    for w2_ in w2_list:
        for w3_ in w3_list:
            custom_agent_test = [Agent(CustomPlayerTest(w2=w2_, w3=w3_, **CUSTOM_ARGS), "custom_" +str(w2_) + "_" +str(w3_))]
            agents = random_agents + mm_agents + ab_agents + custom_agent_test
            win_ratio = play_round(agents, NUM_MATCHES)
            print("\n\nResults:")
            print("----------")
            print("{!s:<15}{:>10.2f}%".format(custom_agent_test[0].name, win_ratio))
            print("{!s:<15}{:>10.2f}%".format('likelihood_of_superiority : ',
                                              likelihood_of_superiority(win_ratio, 2 * NUM_MATCHES)))
Esempio n. 18
0
def tournament_schedule(args):
    "args: epochs, matches, self_play_dir start_model, pool_dir, out_pool_dir"

    base_model = args.pool_dir + args.start_model
    policy = torch.load(base_model)
    modelglob = glob.glob(args.pool_dir + '*')

    inactive = [["MM_Improved", MinimaxPlayer(score_fn=improved_score)],
                ["AB_Open", AlphaBetaPlayer(score_fn=open_move_score)],
                ["AB_Center", AlphaBetaPlayer(score_fn=open_move_score)],
                ["AB_Improved", AlphaBetaPlayer(score_fn=improved_score)]]

    agents = {"Random": RandomPlayer(),
              "Random2": RandomPlayer(),
              "MM_Open": MinimaxPlayer(score_fn=open_move_score),
              "MM_center": MinimaxPlayer(score_fn=center_score)}

    def print_round(win_pct, match, opp_name):
        print("win_pct:{}, match:{}, vs:{} ---- base:{}".format(win_pct, match, opp_name, args.start_model))

    defeated = []
    models = modelglob + list(agents.keys())
    best_hist = []
    start_time = time.strftime("%Y-%m-%d-%H:%M")

    if args.q == 0:
        game_fn = play_game_rl
    else:
        game_fn = play_game_questionable

    for match in range(args.matches):

        # load an Opponent
        opp_model_loc = random.choice(models)
        if opp_model_loc in agents:
            opp_name = opp_model_loc
            opponent_player = agents[opp_model_loc]
        else:
            opp_name = os.path.basename(opp_model_loc)
            opponent_player = loadNNPlayer(opp_model_loc)

        # play game
        policy, win_pct = game_fn(policy, opponent_player, opp_name, args, policy_id='best_in')
        print_round(win_pct, match, opp_name)

        if win_pct >= 0.99:
            print("---------------")
            print("{} has been defeated at match {}".format(opp_name, match))
            try:
                if opp_model_loc in agents:
                    if len(inactive) > 0:
                        print("new Search opponent has been created: {}".format(inactive[0][0]))
                        models.append(inactive[0][0])
                        agents[inactive[0][0]] = inactive[0][1]
                        del inactive[0]
                else:
                    add_pool = glob.glob(args.out_pool_dir + '*')
                    for nn in add_pool:
                        if nn not in defeated:
                            models.append(nn)
                            print("new NN opponent has been created: {}".format(nn))
                            break

                models.remove(opp_model_loc)
                defeated.append(opp_model_loc)
            except:
                print("you are an idiot. remember to write a unittest. f**k state")

        if (match + 1) % 50 == 0:
            policy_loc = save_model_fmt(policy, args.out_pool_dir, start_time, '-top-', match)

        best_hist.append([opp_name, win_pct])

    print("---------------Tournament complete------------")
    print("---------------defeated opponents--------------")
    # (print(x) for x in defeated)
    print("---------------FINAL RESULTS------------------")
    # running_total = 0
    for m in best_hist:
        print("win_pct:{}, vs: {}".format(m[1], m[0]))
Esempio n. 19
0
def mainMod():

    HEURISTICS = [("Null", null_score), ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [
        Agent(CustomPlayer(score_fn=h, **MM_ARGS), "MM_" + name)
        for name, h in HEURISTICS
    ]
    ab_agents = [
        Agent(CustomPlayer(score_fn=h, **AB_ARGS), "AB_" + name)
        for name, h in HEURISTICS
    ]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    #    test_agents = [Agent(CustomPlayer(score_fn=custom_score_normalizedByBlankSpaces, **CUSTOM_ARGS), "StudentNormalized"),
    #                   Agent(CustomPlayer(score_fn=custom_score_edgeAndCornerLimiting, **CUSTOM_ARGS), "StudentECLimiting"),
    #                   Agent(CustomPlayer(score_fn=custom_score_distaceWeightedPositions, **CUSTOM_ARGS), "StudentDistWeighted")]
    test_agents = [
        Agent(
            CustomPlayer(score_fn=custom_score_distaceWeightedPositions,
                         **CUSTOM_ARGS), "StudentDistWeighted")
    ]
    idImprovedAgent = [
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved")
    ]
    #all_agents = test_agents+[idImprovedAgent]
    print(DESCRIPTION)
    results = dict()
    for agentUT in test_agents:
        for i in range(50):
            print("")
            print("*************************")
            print("{:^25}".format("Evaluating: " + agentUT.name))
            print("*************************")
            agentName = agentUT.name
            agents = idImprovedAgent + [agentUT]
            opponent = idImprovedAgent[0].name
            key = (opponent + "_" + agentName)
            win_ratio = play_round(agents, NUM_MATCHES)
            if (key in results):
                results.get(key).append(win_ratio)
            else:
                results[key] = [win_ratio]
            print("\n\nResults:")
            print("----------")
            print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))
    with open('HeuristicsResults.csv', 'w') as f:
        [
            f.write('{0},{1}\n'.format(key, value))
            for key, v in results.items() for value in v
        ]
Esempio n. 20
0
def main(argv):

    USAGE = """usage: tournament_mp.py [-m <number of matches>] [-p <pool size>] [-o <outputfile>]
            -m number of matches: optional number of matches (each match has 4 games) - default is 5
            -p pool size: optional pool size - default is 3
            -o output file: optional output file name - default is results.txt"""

    # Assumes 2 x dual-core CPUs able to run 3 processes relatively
    # uninterrupted (interruptions cause get_move to timeout)
    pool_size = 3
    outputfilename = 'results.txt'
    num_matches = NUM_MATCHES
    try:
        opts, args = getopt.getopt(argv, "hm:p:o:",
                                   ["matches=", "poolsize=", "ofile="])
    except getopt.GetoptError as err:
        print(err)
        print(USAGE)
        sys.exit(2)
    for opt, arg in opts:
        if opt in ["-h", "--help"]:
            print(USAGE)
            sys.exit()
        elif opt in ("-m", "--matches"):
            num_matches = int(arg)
        elif opt in ("-p", "--poolsize"):
            pool_size = int(arg)
        elif opt in ("-o", "--ofile"):
            outputfilename = arg

    HEURISTICS = [("Null", null_score), ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_opponents = [
        Agent(CustomPlayer(score_fn=h, **MM_ARGS), "MM_" + name)
        for name, h in HEURISTICS
    ]
    ab_opponents = [
        Agent(CustomPlayer(score_fn=h, **AB_ARGS), "AB_" + name)
        for name, h in HEURISTICS
    ]
    random_opponents = [Agent(RandomPlayer(), "Random")]
    all_opponents = random_opponents + mm_opponents + ab_opponents

    test_agents = []
    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    test_agents.append(
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved"))

    # Create all the parameterized evaluation function objects
    # Then create all the test agents using those eval functions
    params = [(a, b, c, d, e, f) for a in range(1, 3) for b in range(0, 3)
              for c in range(-1, 2) for d in range(1, 3) for e in range(0, 3)
              for f in range(0, 1)]
    #params = [(0,0,0,0,0,0)]
    #params = [(1, 2, -1, 1, 2, 0)]

    for param in params:
        eval_obj = ParameterizedEvaluationFunction(param)
        test_agents.append(
            Agent(CustomPlayer(score_fn=eval_obj.eval_func, **CUSTOM_ARGS),
                  "Student " + str(param)))

    # Put the start time in the output file
    with open(outputfilename, mode='a') as ofile:
        ofile.write(
            '*******************************************************************************************\n'
        )
        ofile.write(
            'Starting Isolation tournament with %d test agents, %d games per round, and %d sub-processes\n'
            % (len(test_agents), num_matches * 4, pool_size))
        ofile.write('Tournament started at %s\n' %
                    (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))

    # Run the tournament!
    with Pool(processes=pool_size) as pool:
        results = []
        for agentUT in test_agents:
            results.append(
                pool.apply_async(play_round,
                                 args=(all_opponents, agentUT, num_matches)))

        # Write the output... flush each time as it takes a long time to run
        with open(outputfilename, mode='a') as ofile:
            for result in results:
                agent, res = result.get()
                ofile.write('%s got %2.2f\n' % (agent, res))
                ofile.flush()
            ofile.write(
                'Tournament complete at: %s\n' %
                (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
            ofile.write(
                '*******************************************************************************************\n\n'
            )
Esempio n. 21
0
def main():

    HEURISTICS = [("Null", null_score), ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [
        Agent(CustomPlayer(score_fn=h, **MM_ARGS), "MM_" + name)
        for name, h in HEURISTICS
    ]
    ab_agents = [
        Agent(CustomPlayer(score_fn=h, **AB_ARGS), "AB_" + name)
        for name, h in HEURISTICS
    ]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    test_agents = [
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved"),
        Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")
    ]
    test_agents = [
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved"),
        Agent(CustomPlayer(score_fn=aggressive_move_heuristic, **CUSTOM_ARGS),
              "AggressiveMovesStudent"),
        Agent(CustomPlayer(score_fn=relaxed_move_heuristic, **CUSTOM_ARGS),
              "RelaxedMovesStudent")
    ]
    test_agents = [
        Agent(
            CustomPlayer(score_fn=relaxed_move_aggressive_distance,
                         **CUSTOM_ARGS), "relaxed_move_aggressive_distance"),
        Agent(
            CustomPlayer(score_fn=relaxed_move_relaxed_distance,
                         **CUSTOM_ARGS), "relaxed_move_relaxed_distance"),
        Agent(
            CustomPlayer(score_fn=relaxed_move_relaxed_distance_norm,
                         **CUSTOM_ARGS), "relaxed_move_relaxed_distance_norm"),
        Agent(
            CustomPlayer(score_fn=relaxed_move_aggressive_distance_norm,
                         **CUSTOM_ARGS),
            "relaxed_move_aggressive_distance_norm"),
        Agent(CustomPlayer(score_fn=aggressive_move_heuristic, **CUSTOM_ARGS),
              "aggressive_move_heuristic"),
        Agent(CustomPlayer(score_fn=relaxed_move_heuristic, **CUSTOM_ARGS),
              "relaxed_move_heuristic"),
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved")
    ]

    res = pd.Series(name='player')
    agent_names = []
    print(DESCRIPTION)
    for agentUT in test_agents:
        print("")
        print("*************************")
        print("{:^25}".format("Evaluating: " + agentUT.name))
        print("*************************")

        agents = random_agents + mm_agents + ab_agents + [agentUT]
        win_ratio = play_round(agents, NUM_MATCHES)

        print("\n\nResults:")
        print("----------")
        print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))
        res[agentUT.name] = win_ratio
        agent_names.append(agentUT.name)
        res.reset_index().to_hdf('data/full_run_{}.h5'.format(
            "_".join(agent_names)),
                                 'test',
                                 mode='w')
Esempio n. 22
0
def main():

    HEURISTICS = [("Null", null_score), ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [
        Agent(CustomPlayer(score_fn=h, **MM_ARGS), "MM_" + name)
        for name, h in HEURISTICS
    ]
    ab_agents = [
        Agent(CustomPlayer(score_fn=h, **AB_ARGS), "AB_" + name)
        for name, h in HEURISTICS
    ]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    test_agents = [
        Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
              "ID_Improved"),
        # Agent(CustomPlayer(score_fn=more_improved_score, **CUSTOM_ARGS), "More_Improved"),
        Agent(
            CustomPlayer(score_fn=linear_ratio_improved_score, **CUSTOM_ARGS),
            "Linear_Improved"),
        Agent(
            CustomPlayer(score_fn=nonlinear_ratio_improved_score,
                         **CUSTOM_ARGS), "Non_Linear_Improved"),
        Agent(CustomPlayer(score_fn=second_moves_score, **CUSTOM_ARGS),
              "Second_Moves"),
        Agent(
            CustomPlayer(score_fn=second_moves_in_middle_game_score,
                         **CUSTOM_ARGS), "Second_Moves_In_Middle_Game"),
        Agent(CustomPlayer(score_fn=all_boxes_can_move_score, **CUSTOM_ARGS),
              "All_Boxes_Can_Move"),
    ]

    print(DESCRIPTION)
    for agentUT in test_agents:
        print("")
        print("*************************")
        print("{:^25}".format("Evaluating: " + agentUT.name))
        print("*************************")

        # agents = random_agents + mm_agents + ab_agents + [agentUT]
        agents = test_agents + [agentUT]
        agents.remove(agentUT)
        win_ratio = play_round(agents, NUM_MATCHES)

        print("\n\nResults:")
        print("----------")
        print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))
Esempio n. 23
0
def main():

    HEURISTICS = [("Null", null_score),
                  ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {"search_depth": 5, "method": 'alphabeta', "iterative": False}
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}
    # For just comparing the correctness of heuristic.. using a shart search depth could help
    #CUSTOM_ARGS = {"method": 'minimax', 'iterative': False, 'search_depth': 3}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [Agent(CustomPlayer(score_fn=h, **MM_ARGS),
                       "MM_" + name) for name, h in HEURISTICS]
    ab_agents = [Agent(CustomPlayer(score_fn=h, **AB_ARGS),
                       "AB_" + name) for name, h in HEURISTICS]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    #test_agents = [Agent(CustomPlayer(score_fn=combined_improved_density_at_end, **CUSTOM_ARGS), "Combined improved"),
    #               Agent(CustomPlayer(score_fn=diff_density, **CUSTOM_ARGS), "Diff density"),
    #               Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved")
        # Agent(CustomPlayer(score_fn=combined_full, **CUSTOM_ARGS), "Combined full"),
    #               ]

    #test_agents = [Agent(CustomPlayer(score_fn=agrressive_first_then_preserving, **CUSTOM_ARGS), "Agressive first then preserving"),
    #               Agent(CustomPlayer(score_fn=custom_score_location_using_hash_table, **CUSTOM_ARGS), "Location using hash"),
    #               Agent(CustomPlayer(score_fn=inverted_to_center, **CUSTOM_ARGS), "Inverted Location using hash"),
    #               Agent(CustomPlayer(score_fn=custom_score_loser, **CUSTOM_ARGS), "loser"),
    #               Agent(CustomPlayer(score_fn=preserving_score_with_self, **CUSTOM_ARGS), "Preserving score with self"),
    #               Agent(CustomPlayer(score_fn=aggressive_score_with_self, **CUSTOM_ARGS), "Agressive score with self"),
    #               Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Custom score using coefficient"),
    #               Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved")]

    test_agents = [Agent(CustomPlayer(score_fn=combined_improved_density_at_end, **CUSTOM_ARGS), "Combined_improved_and_density"),
               Agent(CustomPlayer(score_fn=diff_density, **CUSTOM_ARGS), "Diff_density"),
               Agent(CustomPlayer(score_fn=improved_with_sleep, **CUSTOM_ARGS), "ID_Improved_slow"),
               Agent(CustomPlayer(score_fn=distance_to_center, **CUSTOM_ARGS), "Distance_to_center"),
               Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved")
               ]

    print(DESCRIPTION)
    full_res = {}
    for agentUT in test_agents:
        print("")
        print("*************************")
        print("{:^25}".format("Evaluating: " + agentUT.name))
        print("*************************")
        agents = random_agents + mm_agents + ab_agents + [agentUT]

        win_ratio, res = play_round(agents, NUM_MATCHES)

        print("\n\nResults:")
        print("----------")
        print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))
        full_res[agentUT.name] = res

    with open('out.json', 'w') as f:
        json.dump(full_res, f)
Esempio n. 24
0
def main():

    HEURISTICS = [("Null", null_score), ("Open", open_move_score),
                  ("Improved", improved_score)]
    AB_ARGS = {
        "search_depth": 5,
        "method": 'alphabeta',
        "iterative": False
    }  # 5
    MM_ARGS = {"search_depth": 3, "method": 'minimax', "iterative": False}  # 3
    CUSTOM_ARGS = {"method": 'alphabeta', 'iterative': True}

    # Create a collection of CPU agents using fixed-depth minimax or alpha beta
    # search, or random selection.  The agent names encode the search method
    # (MM=minimax, AB=alpha-beta) and the heuristic function (Null=null_score,
    # Open=open_move_score, Improved=improved_score). For example, MM_Open is
    # an agent using minimax search with the open moves heuristic.
    mm_agents = [
        Agent(CustomPlayer(score_fn=h, **MM_ARGS), "MM_" + name)
        for name, h in HEURISTICS
    ]
    ab_agents = [
        Agent(CustomPlayer(score_fn=h, **AB_ARGS), "AB_" + name)
        for name, h in HEURISTICS
    ]
    random_agents = [Agent(RandomPlayer(), "Random")]

    # ID_Improved agent is used for comparison to the performance of the
    # submitted agent for calibration on the performance across different
    # systems; i.e., the performance of the student agent is considered
    # relative to the performance of the ID_Improved agent to account for
    # faster or slower computers.
    #test_agents = [Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"),
    #               Agent(CustomPlayer(score_fn=custom_score, **CUSTOM_ARGS), "Student")]
    #    test_agents = [Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"),
    #                   Agent(CustomPlayer(score_fn=custom_score_mixed_centrality, **CUSTOM_ARGS), "mixed_centrality")]

    # FIXME

    if True:
        test_agents = [
            Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS),
                  "ID_Improved"),
            Agent(
                CustomPlayer(score_fn=custom_score_legal_moves_left_balance,
                             **CUSTOM_ARGS), "Legal Moves\nLeft Balance"),
            Agent(
                CustomPlayer(score_fn=custom_score_dominating_space,
                             **CUSTOM_ARGS), "Dominating\nSpace"),
            Agent(
                CustomPlayer(score_fn=custom_score_dominating_moves,
                             **CUSTOM_ARGS), "Dominating\nMoves"),
            Agent(
                CustomPlayer(score_fn=custom_score_schadenfreude,
                             **CUSTOM_ARGS), "Schadenfreude"),
            Agent(
                CustomPlayer(score_fn=custom_score_mixed_centrality,
                             **CUSTOM_ARGS), "Mixed \nCentrality")
        ]

    results_prepared = {}

    for run in range(0, 3):
        print(DESCRIPTION)
        print("============================")
        print("Run:", run)
        print("============================")

        results = []
        for agentUT in test_agents:
            print("")
            print("*************************")
            print("{:^25}".format("Evaluating: " + agentUT.name))
            print("*************************")

            agents = random_agents + mm_agents + ab_agents + [agentUT]
            win_ratio = play_round(agents, NUM_MATCHES)
            results.append((win_ratio, agentUT.name))

            print("\n\nResults:")
            print("----------")
            print("{!s:<15}{:>10.2f}%".format(agentUT.name, win_ratio))

        print("All Results:")

        for index, (score, name) in enumerate(results):
            if name == "ID_Improved":
                break

        assert ("ID_Improved" == results[index][1])
        baseline_score = results[index][0]
        print("baseline_score", baseline_score)

        del results[index]

        #results_prepared = [[name, score-baseline_score] for (score, name) in sorted(results)]
        for (score, name) in results:
            results_prepared[name] = results_prepared.get(
                name, []) + [score - baseline_score]

        #labels, ys = zip(*results_prepared)

    import visualize
    import numpy as np
    print("results_prepared", results_prepared)
    visualize.visualize_as_bar(results_prepared, "Absolute Gains to Baseline")

    # Create table
    averaged_results = {}
    for key in results_prepared:
        averaged_results[key] = np.mean(results_prepared[key])
    print("averaged_results", averaged_results)