Ejemplo n.º 1
0
def play_round(cpu_agent, test_agent, num_matches):
    win_count = dict()
    timeout_count = dict()
    forfeit_count = dict()
    game_data = list()
    win_count = defaultdict(int)
    timeout_count = defaultdict(int)
    forfeit_count = defaultdict(int)

    for _ in range(num_matches):

        games = [
            Board(cpu_agent.player, test_agent.player, record_game=True),
            Board(test_agent.player, cpu_agent.player, record_game=True)
        ]
        # initialize all games with a random move and response
        for _ in range(2):
            move = random.choice(games[0].get_legal_moves())
            for game in games:
                game.apply_move(move)

        # play all games and tally the results
        for game in games:
            winner, data, termination = game.play(time_limit=TIME_LIMIT)
            win_count[winner] += 1
            game_data.append(data)

            if termination == "timeout":
                timeout_count[winner] += 1
            elif termination == "forfeit":
                forfeit_count[winner] += 1

    return game_data, win_count, forfeit_count, timeout_count
Ejemplo n.º 2
0
def play_round(cpu_agent, test_agents, win_counts, num_matches):
    """Compare the test agents to the cpu agent in "fair" matches.

    "Fair" matches use random starting locations and force the agents to
    play as both first and second player to control for advantages resulting
    from choosing better opening moves or having first initiative to move.
    """
    timeout_count = 0
    forfeit_count = 0
    for _ in range(num_matches):

        games = sum([[
            Board(cpu_agent.player, agent.player),
            Board(agent.player, cpu_agent.player)
        ] for agent in test_agents], [])

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

        # play all games and tally the results
        for game in games:
            winner, _, termination = game.play(time_limit=TIME_LIMIT)
            win_counts[winner] += 1

        if termination == "timeout":
            timeout_count += 1
        elif winner not in test_agents and termination == "forfeit":
            forfeit_count += 1

    return timeout_count, forfeit_count
Ejemplo n.º 3
0
def mymain():
    visualizing = False
    #     mm_null_reg_agent = Agent(CustomPlayer(score_fn=null_score, method='minimax', search_depth=3, iterative=False), "mm_null_reg_agent")
    #     mm_open_reg_agent = Agent(CustomPlayer(score_fn=open_move_score, method='minimax', search_depth=3, iterative=False), "mm_open_reg_agent")
    #     mm_impr_reg_agent = Agent(CustomPlayer(score_fn=improved_score, method='minimax', search_depth=3, iterative=False), "mm_impr_reg_agent")
    #     mm_cstm_reg_agent = Agent(CustomPlayer(score_fn=custom_score, method='minimax', search_depth=3, iterative=False), "mm_cstm_reg_agent")
    #     ab_null_reg_agent = Agent(CustomPlayer(score_fn=null_score, method='alphabeta', search_depth=5, iterative=False), "ab_null_reg_agent")
    #     ab_open_reg_agent = Agent(CustomPlayer(score_fn=open_move_score, method='alphabeta', search_depth=3, iterative=False), "ab_open_reg_agent")
    #     ab_impr_reg_agent = Agent(CustomPlayer(score_fn=improved_score, method='alphabeta', search_depth=3, iterative=False), "ab_impr_reg_agent")
    #     ab_cstm_reg_agent = Agent(CustomPlayer(score_fn=custom_score, method='alphabeta', search_depth=3, iterative=False), "ab_cstm_reg_agent")
    #     ab_null_id_agent = Agent(CustomPlayer(score_fn=null_score, method='alphabeta', search_depth=3, iterative=True), "ab_null_id_agent")
    #     ab_open_id_agent = Agent(CustomPlayer(score_fn=open_move_score, method='alphabeta', search_depth=3, iterative=True), "ab_open_id_agent")
    #     ab_impr_id_agent = Agent(CustomPlayer(score_fn=improved_score, method='alphabeta', search_depth=3, iterative=True), "ab_impr_id_agent")
    #     ab_cstm_id_agent = Agent(CustomPlayer(score_fn=custom_score, method='alphabeta', search_depth=3, iterative=True), "ab_cstm_id_agent")

    player1 = Agent(
        CustomPlayer(score_fn=net_mobility_score,
                     method='alphabeta',
                     search_depth=3,
                     iterative=True), "Custom")
    player2 = Agent(
        CustomPlayer(score_fn=improved_score,
                     method='alphabeta',
                     search_depth=3,
                     iterative=True), "Improved")

    # Play a few games:
    for i in range(0, 5):
        game1 = Board(player1.player, player2.player)
        game2 = Board(player2.player, player1.player)

        # Initial location:
        move = random.choice(game1.get_legal_moves())
        game1.apply_move(move)
        game2.apply_move(move)
        move = random.choice(game1.get_legal_moves())
        game1.apply_move(move)
        game2.apply_move(move)

        winner1, moves1, reason1 = game1.play()
        winner1 = 1 if player1.player == winner1 else 2
        winner2, moves2, reason2 = game2.play()
        winner2 = 1 if player1.player == winner2 else 2
        print("Player {} won game 1. Reason: {}".format(winner1, reason1))
        print("Player {} won game 2. Reason: {}".format(winner2, reason2))

        if visualizing:
            print("Replaying moves for game 1...")
            print(moves1)
            visualizer = Visualizer(player1.name, player2.name, moves1)
            visualizer.play()
            visualizer.quit()
            print("Replaying moves for game 2...")
            print(moves2)
            visualizer = Visualizer(player2.name, player1.name, moves2)
            visualizer.play()
            visualizer.quit()

        print("Done")
Ejemplo n.º 4
0
def play_match(player1, player2):
    """
    Play a "fair" set of matches between two agents by playing two games
    between the players, forcing each agent to play from randomly selected
    positions. This should control for differences in outcome resulting from
    advantage due to starting position on the board.
    """
    num_wins = {player1: 0, player2: 0}
    num_timeouts = {player1: 0, player2: 0}
    num_invalid_moves = {player1: 0, player2: 0}
    games = [
        Board(player1, player2, 7, 7, USE_HUMAN),
        Board(player2, player1, 7, 7, USE_HUMAN)
    ]

    # initialize both games with a random move and response
    moves = []
    for i in range(2):
        move = random.choice(games[0].get_legal_moves())
        if i == 0:
            moves.append([move])
        else:
            moves[-1].append(move)
        games[0].apply_move(move)
        games[1].apply_move(move)

    # play both games and tally the results
    for game in games:
        winner, h, termination = game.play(time_limit=TIME_LIMIT)
        h = moves + h
        import json
        j = json.dumps(h)

        if not SUPRESS_MESSAGES:
            print('Winner {0}'.format(winner.get_name()))
            #print('\nFirst_player_won: {0}. Game: {1}'.format(winner.get_name() == game.__player_1__.get_name(), j))
            #input("Press Enter to continue...")

        if player1 == winner:
            num_wins[player1] += 1

            if termination == "timeout":
                num_timeouts[player2] += 1
            else:
                num_invalid_moves[player2] += 1

        elif player2 == winner:
            num_wins[player2] += 1

            if termination == "timeout":
                num_timeouts[player1] += 1
            else:
                num_invalid_moves[player1] += 1

    if sum(num_timeouts.values()) != 0:
        warnings.warn(TIMEOUT_WARNING)

    return num_wins[player1], num_wins[player2]
Ejemplo n.º 5
0
def play_match(player1, player2):
    """
    Play a "fair" set of matches between two agents by playing two games
    between the players, forcing each agent to play from randomly selected
    positions. This should control for differences in outcome resulting from
    advantage due to starting position on the board.
    """
    num_wins = {player1: 0, player2: 0}
    num_timeouts = {player1: 0, player2: 0}
    num_invalid_moves = {player1: 0, player2: 0}
    games = [Board(player1, player2), Board(player2, player1)]

    # 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)

    games[0].apply_move((4, 5))
    games[0].apply_move((2, 4))

    games[1].apply_move((2, 4))
    games[1].apply_move((4, 5))

    # play both games and tally the results
    for game in games:
        winner, _, termination = game.play(time_limit=TIME_LIMIT)

        if player1 == winner:
            num_wins[player1] += 1

            if termination == "timeout":
                num_timeouts[player2] += 1
            else:
                num_invalid_moves[player2] += 1

        elif player2 == winner:

            num_wins[player2] += 1

            if termination == "timeout":
                num_timeouts[player1] += 1
            else:
                num_invalid_moves[player1] += 1

    if sum(num_timeouts.values()) != 0:
        warnings.warn(TIMEOUT_WARNING)

    return num_wins[player1], num_wins[player2]
def single_test_agent(myAgent, baselineAgent):
    initial_board = None
    p1 = myAgent()
    p2 = baselineAgent()
    games_won = 0
    games_lost = 0
    game_historiesp1, game_historiesp2 = [], []
    for game_no in range(1):
        initial_board, move_history_rand = random_board(simulate_till=2)
        print()
        new_board = Board(p1, p2)
        new_board.set_state(initial_board.get_state(), p1_turn=True)
        new_board
        winner, move_history, reason = new_board.play_isolation(
            time_limit=1000, print_moves=True)
        move_history = move_history_rand + move_history

        print("Game#%d: %s, %s" % (game_no + 1, winner, reason))
        if "CustomPlayer - " not in winner:
            games_lost += 1
            game_historiesp2.append(move_history)
        else:
            games_won += 1
            game_historiesp1.append(move_history)
    print("Win percent: %f" % (games_won / (games_lost + games_won)))
def random_board(simulate_till=4):
    def time_left():
        return 100000

    randomAgent1 = RandomPlayer()
    randomAgent2 = RandomPlayer()

    game = Board(randomAgent1, randomAgent2)
    move_history = []
    for move_idx in range(simulate_till):
        if move_idx == 0:
            curr_move = (3, 3, False)
        elif move_idx == 1:  # Non mirrorable moves
            curr_move = random.choice(((1, 2, False), (1, 4, False), (2, 5, False), (4, 5, False), \
                                       (5, 4, False), (5, 2, False), (4, 1, False), (2, 1, False)))
        else:
            curr_move = game.__active_player__.move(game, time_left)
            curr_move = (curr_move[0], curr_move[1], bool(curr_move[2]))

        if curr_move not in game.get_active_moves():
            raise Exception("Illegal move played")

        # Append new move to game history
        if game.__active_player__ == game.__player_1__:
            move_history.append([curr_move])
        else:
            move_history[-1].append(curr_move)

        is_over, winner = game.__apply_move__(curr_move)

        if is_over:
            raise ("Game over while simulating board")

    return game, move_history
Ejemplo n.º 8
0
def _run(*args):
    idx, p1, p2, moves = args[0]
    game = Board(p1, p2)
    for m in moves:
        game.apply_move(m)
    winner, move_history, termination = game.play(time_limit=TIME_LIMIT)
    return (idx, winner == p1, len(move_history)), termination
Ejemplo n.º 9
0
def start_game():
    # create an isolation board (by default 7x7)
    player1 = RandomPlayer()
    player2 = AlphaBetaPlayer(score_fn=custom_score_3)
    game = Board(player1, player2)
    assert (player1 == game.active_player)

    player_position = choice(game.get_legal_moves())
    game.apply_move(player_position)

    ai_position = choice(game.get_legal_moves())
    game.apply_move(ai_position)

    move_history = [player_position, ai_position]

    response = jsonify({
        'legal_moves': game.get_legal_moves(),
        'player_position': list(player_position),
        'ai_position': list(ai_position),
        'move_history': move_history
    })

    # place player 1 on the board at row 2, column 3, then place player 2 on
    # the board at row 0, column 5; display the resulting board state.  Note
    # that the .apply_move() method changes the calling object in-place.

    return response
Ejemplo n.º 10
0
    def setUp(self):
        self.board = Board()

        pg.display.set_mode = Mock(wraps=pg.Surface)
        pg.display.set_caption = Mock()
        pg.display.update = Mock()
        self.ui = GUI()
Ejemplo n.º 11
0
def testMiniMax():
    try:
        """Example test to make sure
        your minimax works, using the
        #computer_player_moves - opponent_moves evaluation function."""
        # create dummy 3x3 board

        p1 = RandomPlayer()
        p2 = CustomPlayerAB(search_depth=3)
        #p2 = HumanPlayer()
        b = Board(p1, p2, 5, 5)
        b.__board_state__ = [[0, 21, 0, 0, 0], [0, 0, 11, 0, 0],
                             [0, 0, 12, 0, 0], [0, 0, 0, 0, 0],
                             [0, 22, 0, 0, 0]]
        b.__last_queen_move__["queen11"] = (1, 2)
        b.__last_queen_move__["queen21"] = (0, 1)
        b.__last_queen_move__["queen12"] = (2, 2)
        b.__last_queen_move__["queen22"] = (4, 1)

        b.move_count = 4

        output_b = b.copy()
        winner, move_history, queen_history, termination = b.play_isolation(
            1000, True)
        print 'Minimax Test: Runs Successfully'
        # Uncomment to see example game
        print game_as_text(winner, move_history, queen_history,
                           b.output_history, termination, output_b)
    except NotImplementedError:
        print 'Minimax Test: Not Implemented'
    except:
        print 'Minimax Test: ERROR OCCURRED'
        print traceback.format_exc()
Ejemplo n.º 12
0
def play_matches():
    g1p1 = a1.constructor()
    g1p2 = a2.constructor()
    g2p1 = a1.constructor()
    g2p2 = a2.constructor()

    stats = []
    for _ in range(NUM_MATCHES):
        games = [Board(g1p1, g1p2), Board(g2p2, g2p1)]
        for _ in range(2):
            move = random.choice(games[0].get_legal_moves())
            for game in games:
                game.apply_move(move)
        stats.append(play_out_with_stats(games[0], g1p1, g1p2))
        stats.append(play_out_with_stats(games[1], g2p1, g2p2))
    return stats
Ejemplo n.º 13
0
def agentvsagentloop(agent1, agent2):
    """
    Pit two agents against eachother
    """

    print("")
    agent1_wins = 0
    agent2_wins = 0
    for i in range(10):
        try:
            r = agent1()
            p = agent2()
            game = Board(r, p, 7, 7)
            output_b = game.copy()
            winner, move_history, termination = game.play_isolation(
                time_limit=1000, print_moves=False)
            print("\n", winner, " has won. Reason: ", termination)
            if winner == "CustomPlayerTest - Q1":
                agent1_wins += 1
            else:
                agent2_wins += 1
            # Uncomment to see game
            # print game_as_text(winner, move_history, termination, output_b)
        except NotImplementedError:
            print('CustomPlayer Test: Not Implemented')
        except:
            print('CustomPlayer Test: ERROR OCCURRED')
            print(traceback.format_exc())

    print("agent 2 win ration: ", agent2_wins / 10)
    print()
def correctOpenEvalFn(yourOpenEvalFn):
    print()
    try:
        sample_board = Board(RandomPlayer(), RandomPlayer())
        # setting up the board as though we've been playing
        board_state = [
            ["Q1", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", "Q2", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "],
            [" ", " ", " ", " ", " ", " ", " "]
        ]
        sample_board.set_state(board_state, True)
        #test = sample_board.get_legal_moves()
        h = yourOpenEvalFn()
        print('OpenMoveEvalFn Test: This board has a score of %s.' % (h.score(sample_board, sample_board.get_active_player())))
    except NotImplementedError:
        print('OpenMoveEvalFn Test: Not implemented')
    except:
        print('OpenMoveEvalFn Test: ERROR OCCURRED')
        print(traceback.format_exc())

    print()
def test1():
	player1 = MinimaxPlayer()
	player2 = MinimaxPlayer()
	game = Board(player1, player2)
	# game.apply_move((1,1))
	# game.apply_move((2,2))
	# print (game._board_state)
	# print(game.to_string())
	# print(len(game._board_state))

	explored = [9,11,12,14,15,16,17,20,24,25,29,30,33,38,39,44]
	for i in explored:
		game._board_state[i] = 1
	game._board_state[-1] = 17
	game._board_state[-2] = 9


	# game.apply_move((5, 3)) # player 1
	# game.apply_move((4, 2)) # player 2

	print (game.to_string())
	moves = game.get_legal_moves()
	print(moves)
	for m in moves:
		fm = game.forecast_move(m).get_legal_moves()
		print (str(m) + " -->" + str(fm))

	# player 1
	for m in moves:
		print (str(m) + " --> " + str(player1.score(game.forecast_move(m), player1)))
	print (player1.get_move(game, 6))
Ejemplo n.º 16
0
def test_case1():
    player1 = MinimaxPlayer(search_depth=1, score_fn=open_move_score)
    player2 = GreedyPlayer()
    game = Board(player1, player2, 9, 9)
    game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, \
                         0, 0, 0, 0, 0, 0, 0, 0, 0, \
                         0, 0, 1, 1, 0, 0, 1, 0, 0, \
                         0, 0, 0, 0, 1, 1, 0, 0, 0, \
                         0, 1, 0, 1, 1, 1, 0, 0, 0, \
                         0, 0, 1, 0, 1, 0, 0, 0, 0, \
                         0, 0, 1, 1, 1, 0, 0, 0, 0, \
                         0, 0, 0, 0, 0, 0, 0, 0, 0, \
                         0, 0, 0, 0, 0, 0, 0, 0, 0, \
                         0, 37, 57]
    print('Current game')
    print(game.to_string())
    print('Active player {}'.format(game.active_player))
    print('Legal moves\n\t{}'.format(game.get_legal_moves()))

    time_limit  = 10
    time_millis = lambda: 1000 * timeit.default_timer()
    move_start = time_millis()
    time_left = lambda: 20
    game_copy = game.copy()
    print('Next move: \n\t{}'.format(game.active_player.get_move(game_copy, time_left)))
    print('Expected move is (5,5), please verify!!!')
Ejemplo n.º 17
0
def testUtility():
    try:
        sample_board = Board(RandomPlayer(), RandomPlayer())
        # setting up the board as though we've been playing
        sample_board.move_count = 4
        sample_board.__board_state__ = [[11, 0, 0, 0, 21, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 22, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 12, 0],
                                        [0, 0, 0, 0, 0, 0, 0],
                                        [0, 0, 0, 0, 0, 0, 0]]
        sample_board.__last_queen_move__ = {
            sample_board.queen_11: (0, 0),
            sample_board.queen_12: (4, 5),
            sample_board.queen_21: (0, 4),
            sample_board.queen_22: (2, 2)
        }
        test = sample_board.get_legal_moves()
        h = OpenMoveEvalFn()
        print 'OpenMoveEvalFn Test: This board has a score of %s.' % (
            h.score(sample_board))
        sample_board.print_board()
    except NotImplementedError:
        print 'OpenMoveEvalFn Test: Not implemented'
    except:
        print 'OpenMoveEvalFn Test: ERROR OCCURRED'
        print traceback.format_exc()
Ejemplo n.º 18
0
 def test_remove(self):
     b = Board()
     x, y = b.get_ann_pos()
     self.assertTrue(b.remove_cell(0, 0))
     self.assertFalse(b.remove_cell(0, 0))
     self.assertFalse(b.remove_cell(x, y))
     self.assertFalse(b.remove_cell(100, 100))
Ejemplo n.º 19
0
def play_game():
    content = request.get_json(silent=True)
    move_history = content['move_history']

    # create an isolation board (by default 7x7)
    player1 = RandomPlayer()
    player2 = AlphaBetaPlayer(score_fn=custom_score_3)
    game = Board(player1, player2)

    assert (player1 == game.active_player)

    for move in move_history:
        game.apply_move(move)

    isWinner = ''

    if game.is_winner(player1):
        isWinner = 'White Knight'

    if game.is_winner(player2):
        isWinner = 'Black Knight'

    if isWinner:
        response = jsonify({
            'legal_moves': game.get_legal_moves(),
            'player_position': list(player_position),
            'ai_position': list(ai_position),
            'move_history': move_history,
            'isWinner': isWinner
        })

    player2.time_left = time_left

    ai_move = player2.alphabeta(game, 6)
    game.apply_move(ai_move)

    player_position = game.get_player_location(player1)
    move_history.append(ai_move)
    ai_position = game.get_player_location(player2)

    if game.is_winner(player1):
        isWinner = 'White Knight'

    if game.is_winner(player2):
        isWinner = 'Black Knight'

    response = jsonify({
        'legal_moves': game.get_legal_moves(),
        'player_position': list(player_position),
        'ai_position': list(ai_position),
        'move_history': move_history,
        'isWinner': isWinner
    })

    # place player 1 on the board at row 2, column 3, then place player 2 on
    # the board at row 0, column 5; display the resulting board state.  Note
    # that the .apply_move() method changes the calling object in-place.

    return response
def play_round(cpu_agent, test_agents, win_counts, num_matches):
    """Compare the test agents to the cpu agent in "fair" matches.

    "Fair" matches use random starting locations and force the agents to
    play as both first and second player to control for advantages resulting
    from choosing better opening moves or having first initiative to move.
    """
    timeout_count = 0
    forfeit_count = 0
    queue = JoinableQueue()

    for _ in range(num_matches):

        workers = []

        games = sum([[
            Board(cpu_agent.player, agent.player),
            Board(agent.player, cpu_agent.player)
        ] for agent in test_agents], [])

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

        # play all games and tally the results
        for game in games:
            p = Process(target=game_play_process, args=(
                queue,
                game,
            ))
            workers.append(p)
            p.start()

        for _ in range(len(workers)):
            (winner, t_c, f_c) = queue.get()
            timeout_count += t_c
            forfeit_count += f_c
            for key, value in win_counts.items():
                if hash(key) == winner:
                    win_counts[key] += 1

        queue.join()

    return timeout_count, forfeit_count
Ejemplo n.º 21
0
def compute_exitability(width, height):
    game = Board("p1", "p2", width, height)
    grid = CostGrid(width, height)
    for x in range(game.width):
        for y in range(game.height):
            #print(y,x,len(game.get_moves((y,x))))
            grid.set((y, x), len(game.get_moves((y, x))))
    return grid
Ejemplo n.º 22
0
 def setUp(self):
     reload(game_agent)
     self.player1 = AlphaBetaPlayer(search_depth=2,
                                    score_fn=null_score,
                                    timeout=10.)
     self.player2 = AlphaBetaPlayer(search_depth=2,
                                    score_fn=null_score,
                                    timeout=10.)
     self.game = Board(self.player1, self.player2)
Ejemplo n.º 23
0
 def setUp(self):
     reload(game_agent)
     self.player1 = MinimaxPlayer(search_depth=1,
                                  score_fn=improved_score,
                                  timeout=10.)
     self.player2 = MinimaxPlayer(search_depth=1,
                                  score_fn=improved_score,
                                  timeout=10.)
     self.game = Board(self.player1, self.player2)
Ejemplo n.º 24
0
 def test_alphabeta_7(self):
     player1 = game_agent.AlphaBetaPlayer()
     player2 = game_agent.AlphaBetaPlayer()
     game = Board(player1, player2)
     game.apply_move(game.get_legal_moves(player1)[0])
     winner, history, outcome = game.play()
     print("\nWinner: {}\nOutcome: {}".format(winner, outcome))
     print(game.to_string())
     print("Move history:\n{!s}".format(history))
Ejemplo n.º 25
0
def evolve(pop, gamesFactor=2, retain=0.2, random_select=0.05, mutate=0.01):
    # Determine the parents to breed from the population
    agent_score = {}
    numGames = len(pop) * gamesFactor
    bar = progressbar.ProgressBar()

    for game in bar(range(numGames)):
        competitors = random.sample(pop, 2)
        game = Board(competitors[0], competitors[1])
        winner, history, outcome = game.play()
        competitors.remove(winner)
        loser = competitors[0]
        if winner not in agent_score.keys():
            agent_score[winner] = 1
        else:
            agent_score[winner] += 1
        if loser not in agent_score.keys():
            agent_score[winner] = -1
        else:
            agent_score[loser] -= 1

    top_performers_size = int(retain * len(pop))
    bottom_performers_size = len(pop) - top_performers_size
    rand_select_size = int(len(pop) * random_select)
    top_perfomers = heapq.nlargest(top_performers_size,
                                   agent_score,
                                   key=agent_score.get)
    bottom_performers = heapq.nsmallest(bottom_performers_size,
                                        agent_score,
                                        key=agent_score.get)
    parents = top_perfomers + random.sample(bottom_performers,
                                            rand_select_size)
    random.shuffle(parents)

    # Create children
    numChildren = len(pop) - len(parents)

    children = []
    for i in range(numChildren):
        par = random.sample(parents, 2)
        father = par[0]
        mother = par[1]
        child = breed(mother, father)
        children.append(child)

    new_pop = parents + children

    mutated_pop = []
    # Randomly mutate some of the new population
    for agent in new_pop:
        if mutate > random.uniform(0, 1):
            print('Mutate')
            mutated_agent = mutate_agent(agent)
            mutated_pop.append(mutated_agent)
        else:
            mutated_pop.append(agent)
    return mutated_pop
Ejemplo n.º 26
0
    def test_hash_different(self):
        board = Board(AlphaBetaPlayer(), RandomPlayer())

        board.apply_move(random.choice(board.get_legal_moves()))
        board.apply_move(random.choice(board.get_legal_moves()))

        b1 = board.forecast_move(board.get_legal_moves()[0])
        b2 = board.forecast_move(board.get_legal_moves()[1])

        self.assertNotEqual(b1.__hash__(), b2.__hash__())
Ejemplo n.º 27
0
 def test_move(self):
     b = Board()
     x, y = b.get_ann_pos()
     new_pos = (x - 1, y)
     moved = b.move_ann(*new_pos)
     self.assertTrue(moved)
     self.assertTrue(b.get_ann_pos() == new_pos)
     self.assertTrue(b.board[x][y] == Cell.ok)
     self.assertFalse(b.move_ann(*new_pos))
     self.assertFalse(b.move_ann(100, 110))
Ejemplo n.º 28
0
def play_round(cpu_agent, test_agents, win_counts, num_matches):
    """Compare the test agents to the cpu agent in "fair" matches.

    "Fair" matches use random starting locations and force the agents to
    play as both first and second player to control for advantages resulting
    from choosing better opening moves or having first initiative to move.
    """
    timeout_count = 0
    forfeit_count = 0
    for _ in range(num_matches):

        games = []

        for agent in test_agents:
            games.append((Board(cpu_agent.player,
                                agent.player), agent, cpu_agent))
            games.append((Board(agent.player,
                                cpu_agent.player), agent, cpu_agent))

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

        # play all games and tally the results
        for game in games:
            winner, _, termination = game[0].play(time_limit=TIME_LIMIT)
            print(game[1].name, game[2].name,
                  1 if winner is game[1].player else -1)
            win_counts[winner] += 1

        if termination == "timeout":
            timeout_count += 1
        elif winner not in test_agents and termination == "forfeit":
            loser = game[0].get_opponent(winner)
            tree_print = getattr(loser, "printLastDecisionTree", None)
            if callable(tree_print):
                tree_print(10000)
            forfeit_count += 1

    return timeout_count, forfeit_count
Ejemplo n.º 29
0
def play_round(cpu_agent, test_agents, win_counts, num_matches):
    """Compare the test agents to the cpu agent in "fair" matches.

    "Fair" matches use random starting locations and force the agents to
    play as both first and second player to control for advantages resulting
    from choosing better opening moves or having first initiative to move.
    """
    timeout_count = 0
    forfeit_count = 0
    for _ in range(num_matches):

        games = sum([[
            Board(cpu_agent.player, agent.player),
            Board(agent.player, cpu_agent.player)
        ] for agent in test_agents], [])

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

        # play all games and tally the results
        for game in games:
            winner, hist, termination = game.play(time_limit=TIME_LIMIT)
            win_counts[winner] += 1

            if termination == "timeout":
                timeout_count += 1
            elif termination == "forfeit":
                forfeit_count += 1
                # dbg_dict = {'hist'            : hist,
                #             'board'           : game._board_state,
                #             'player1'         : type(game._player_1),
                #             'player2'         : type(game._player_2)}
                # with open('dbg.pkl', 'wb') as f:
                #     pickle.dump(dbg_dict, f)
                #     print('Debug info is dumped to {}'.format('dbg.pkl'))
                #
                # sys.exit(0)

    return timeout_count, forfeit_count
Ejemplo n.º 30
0
    def test_can_move(self):
        b = Board()
        b.n = 2

        b.ann_pos = (0, 0)
        b.board = [
            [Cell.ann, Cell.none],
            [Cell.none, Cell.none],
        ]
        self.assertFalse(b.can_ann_move())

        b.ann_pos = (1, 0)
        b.board = [
            [Cell.none, Cell.none],
            [Cell.ann, Cell.none],
        ]
        self.assertFalse(b.can_ann_move())

        b.ann_pos = (1, 1)
        b.board = [
            [Cell.none, Cell.none],
            [Cell.none, Cell.ann],
        ]
        self.assertFalse(b.can_ann_move())

        b.ann_pos = (0, 1)
        b.board = [
            [Cell.none, Cell.ann],
            [Cell.none, Cell.none],
        ]
        self.assertFalse(b.can_ann_move())

        b.ann_pos = (0, 1)
        b.board = [
            [Cell.bob, Cell.ann],
            [Cell.none, Cell.none],
        ]
        self.assertFalse(b.can_ann_move())

        b.ann_pos = (0, 1)
        b.board = [
            [Cell.bob, Cell.ann],
            [Cell.ok, Cell.none],
        ]
        self.assertTrue(b.can_ann_move())

        b.n = 3
        b.ann_pos = (1, 1)
        b.board = [
            [Cell.none, Cell.none, Cell.none],
            [Cell.bob, Cell.ann, Cell.none],
            [Cell.none, Cell.none, Cell.none],
        ]
        self.assertFalse(b.can_ann_move())