def test_play_game(self): tested_player = game_agent.AlphaBetaPlayer(score_fn=game_agent.custom_score) tested_player.Name = "Tested Player" reference_player = game_agent.AlphaBetaPlayer(score_fn=sample_players.improved_score) reference_player.Name = "Reference Player" game = isolation.Board(tested_player, reference_player) opposite_game = isolation.Board(reference_player, tested_player) self.play_game(game,tested_player) self.play_game(opposite_game,tested_player)
def setUp(self): reload(game_agent) self.minimax_player1 = game_agent.MinimaxPlayer() self.minimax_player2 = game_agent.MinimaxPlayer() self.minimax_game = isolation.Board(self.minimax_player1, self.minimax_player2) self.alphabeta_player1 = game_agent.AlphaBetaPlayer() self.alphabeta_player2 = game_agent.AlphaBetaPlayer() self.alphabeta_game = isolation.Board(self.alphabeta_player1, self.alphabeta_player2)
def test_get_move_interface(self): """ Test CustomPlayer.get_move interface with simple input """ h, w = 9, 9 # board size test_depth = 1 starting_location = (2, 7) adversary_location = (0, 0) # top left corner iterative_search = False search_method = "minimax" heuristic = lambda g, p: 0. # return 0 everywhere # create a player agent & a game board agentUT = game_agent.CustomPlayer(test_depth, heuristic, iterative_search, search_method) # Test that get_move returns a legal choice on an empty game board board = isolation.Board(agentUT, 'null_agent', w, h) legal_moves = board.get_legal_moves() move = agentUT.get_move(board, legal_moves, lambda: 99) #print('Test1') self.assertIn(move, legal_moves, ("The get_move() function failed as player 1 on an " + "empty board. It should return coordinates on the " + "game board for the location of the agent's next " + "move. The move must be one of the legal moves on " + "the current game board.")) # Test that get_move returns a legal choice for first move as player 2 board = isolation.Board('null_agent', agentUT, w, h) board.apply_move(starting_location) legal_moves = board.get_legal_moves() move = agentUT.get_move(board, legal_moves, lambda: 99) #print('Test2') self.assertIn(move, legal_moves, ("The get_move() function failed making the first " + "move as player 2 on a new board. It should return " + "coordinates on the game board for the location " + "of the agent's next move. The move must be one " + "of the legal moves on the current game board.")) # Test that get_move returns a legal choice after first move board = isolation.Board(agentUT, 'null_agent', w, h) board.apply_move(starting_location) board.apply_move(adversary_location) legal_moves = board.get_legal_moves() move = agentUT.get_move(board, legal_moves, lambda: 99) #print('Test3') self.assertIn(move, legal_moves, ("The get_move() function failed as player 1 on a " + "game in progress. It should return coordinates on" + "the game board for the location of the agent's " + "next move. The move must be one of the legal moves " + "on the current game board."))
def test_alpha_beta(self): """Test Alfa beta agains GreedPlayer""" player1 = game_agent.AlphaBetaPlayer() player2 = sample_players.GreedyPlayer() game = isolation.Board(player1, player2, 3, 5) winner, history, outcome = game.play() self.assertNotEqual(outcome, 'timeout', "AlphaBeta should not lost by timeout") game = isolation.Board(player1, player2) winner, history, outcome = game.play() self.assertNotEqual(outcome, 'timeout', "AlphaBeta should not lost by timeout")
def test_minimax_greedy(self): """ Test the game with the given players """ winners = {} reasons = {} print("Minimax vs. Greedy") for x in range(0, 10): # Set the game board self.game = isolation.Board(self.player1, self.player2) results = self.game.play() # Print the winners if results[0] in winners: winners[results[0]] = winners[results[0]] + 1 else: winners[results[0]] = 1 # Print the reasons for losing if results[2] in reasons: reasons[results[2]] = reasons[results[2]] + 1 else: reasons[results[2]] = 1 print(winners) print(reasons)
def setUp(self): import sample_players reload(game_agent) self.player1 = game_agent.AlphaBetaPlayer() self.player1.time_left = lambda: 10000 self.player2 = sample_players.RandomPlayer() self.game = isolation.Board(self.player1, self.player2)
def test_alphabeta_interface(self): """ Test AlphaBetaPlayer.alphabeta interface with simple input """ h, w = 7, 7 # board size test_depth = 1 starting_location = (5, 3) adversary_location = (0, 0) # top left corner iterative_search = False search_method = "minimax" heuristic = lambda g, p: 0. # return 0 everywhere # create a player agent & a game board agentUT = game_agent.AlphaBetaPlayer(game_agent.IsolationPlayer) agentUT.time_left = lambda: 99 # ignore timeout for fixed-depth search board = isolation.Board(agentUT, 'null_agent', w, h) # place two "players" on the board at arbitrary (but fixed) locations board.apply_move(starting_location) board.apply_move(adversary_location) for move in board.get_legal_moves(): next_state = board.forecast_move(move) op_move = agentUT.alphabeta(next_state, test_depth) print("op_move = ") print(op_move) self.assertTrue( type(op_move) == tuple, ("Minimax function should return a tuple " + "point value approximating the score for the " + "branch being searched."))
def test_minimax(self): self.minimax_player = game_agent.MinimaxPlayer( score_fn=sample_players.center_score, timeout=float("inf")) self.greedy_player = sample_players.GreedyPlayer() self.game = isolation.Board(self.minimax_player, self.greedy_player) # self.game.play(time_limit=float("inf")) self.minimax_player.match_boards(self.game, self.game)
def setUp(self): reload(game_agent) self.player_1 = "Player1" self.player_2 = "Player2" self.board = isolation.Board(self.player_1, self.player_2) self.board.apply_move(self.start_position_player_1) self.board.apply_move(self.start_position_player_2) self.agent = MonteCarloPlayer(EXPLORATION_PARAM=math.sqrt(2)) root = MonteCarloNode(self.board, None) root.score = 11. root.simulations = 21 self.agent.update_root(root) node_1 = self.add_child_pos(root, (3, 2), 7., 10) node_2 = self.add_child_pos(root, (6, 3), 3., 8) node_3 = self.add_child_pos(root, (6, 5), 0, 3) node_11 = self.add_child_pos(node_1, (1, 1), 2., 4) node_12 = self.add_child_pos(node_1, (1, 3), 1., 6) node_21 = self.add_child_pos(node_2, (4, 2), 1., 2) node_22 = self.add_child_pos(node_2, (5, 1), 2., 3) node_23 = self.add_child_pos(node_2, (5, 5), 2., 3) node_121 = self.add_child_pos(node_12, (0, 5), 2., 3) node_122 = self.add_child_pos(node_12, (0, 1), 3., 3) self.agent.frontier = [ node_11, node_121, node_122, node_21, node_22, node_23, node_3 ]
def test_get_move_with_variable_depth(self): """ Test CustomPlayer.get_move interface with simple input """ h, w = 4, 4 # board size test_depth = 2 starting_location = (0, 1) adversary_location = (0, 0) # top left corner iterative_search = True search_method = "minimax" heuristic_pos = list() def get_heuristic(heuristic_pos): def heuristic(g, p): if len(heuristic_pos) == 9: raise game_agent.Timeout() heuristic_pos.append(g.get_player_location(p)) return 0. return heuristic # create a player agent & a game board agentUT = game_agent.CustomPlayer(test_depth, get_heuristic(heuristic_pos), iterative_search, search_method) # Test that get_move returns a legal choice on an empty game board board = isolation.Board(agentUT, 'null_agent', w, h) board.apply_move(starting_location) board.apply_move(adversary_location) agentUT.get_move(board, board.get_legal_moves(), lambda: 99) depth_one_and_two_nodes = [(1, 3), (2, 0), (2, 2), (1, 3), (1, 3), (2, 0), (2, 0), (2, 2), (2, 2)] self.assertEqual(heuristic_pos, depth_one_and_two_nodes, 'Wrong nodes visited')
def test_it_finds_oppossible_reachability(self): player1 = "Player 1" player2 = "Player 2" game = isolation.Board(player1, player2, 4, 4) """ Reach for A 3|A|3|2 -------- 2|3|2|1 -------- 1|2|1|4 -------- B|3|2|3 Reach for B 5|A|3|2 -------- 2|1|4|3 -------- 3|4|1|2 -------- B|3|2|5 Combined 3|A | 3|2 ---------- 2|-1| 2|1 ---------- 1| 2| 1|-2 ---------- B| 3|-1|3 """ reachables = opposed_reachability(game, (0, 1), (3, 0)) self.assertEqual([p[0] for p in reachables.pairs()], [3, 2, 1, 0, 0, -1, 2, 3, 3, 2, 1, 2, 2, 1, -2, 3])
def setUp(self): reload(game_agent) self.player1 = game_agent.MinimaxPlayer(2, sample_players.improved_score) self.player2 = sample_players.GreedyPlayer() self.game = isolation.Board(self.player1, self.player2, 9, 9) self.time_left = lambda: float("inf")
def setUp(self): reload(game_agent) self.player1 = game_agent.AlphaBetaPlayer( score_fn=game_agent.next_moves_score) self.player2 = game_agent.AlphaBetaPlayer( score_fn=game_agent.next_moves_score) self.game = isolation.Board(self.player1, self.player2)
def test_alphabeta_alphabeta(self): """ Test the game with the given players """ winners = {} reasons = {} print("Alpha Beta vs. Alpha Beta") for x in range(0, 10): # Set the game board self.game = isolation.Board(self.player4, self.player5) results = self.game.play() # Print the winners if results[0] in winners: winners[results[0]] = winners[results[0]] + 1 else: winners[results[0]] = 1 # Print the reasons for losing if results[2] in reasons: reasons[results[2]] = reasons[results[2]] + 1 else: reasons[results[2]] = 1 print(winners) print(reasons)
def test_common_sense_1(self): """ - Player 1 has 5 movements, Player 2 has 1 movement: - 5 - 1 = 4 * maximizer = 40 - Player 1's movement stepped on an Player 2's move: - score += 1 * mov_value * maximizer ~ 24 | 2 | | | | x | | | | | | 1 | | | | | | x | | | | x | | | | | x | | x | | | | | | | | | | | | | | | | | | | | """ agent_ut = game_agent.CustomPlayer() board = isolation.Board(agent_ut, 'null_agent') starting_location = (1, 2) adversary_location = (0, 0) board.apply_move(starting_location) board.apply_move(adversary_location) score = scoring.common_sense(board, board.active_player) self.assertEqual(score, 64)
def test_alphabeta2(self): player1 = game_agent.AlphaBetaPlayer(name='Player 1') player2 = game_agent.AlphaBetaPlayer(name='Player 2') #player2 = sample_players.GreedyPlayer() game = isolation.Board(player1, player2, width=9, height=9) game._board_state = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 30, 67 ] print('Game from start') print('Legal moves >{}<'.format(game.get_legal_moves())) print(game.to_string()) # players take turns moving on the board, so player1 should be next to move assert (player1 == game.active_player) # play the remainder of the game automatically -- outcome can be "illegal # move", "timeout", or "forfeit" winner, history, outcome = game.play() print("\nWinner: {}\nOutcome: {}".format(winner, outcome)) print(game.to_string()) print("Move history:\n{!s}".format(history)) print( 'Game tree evaluation order:\n[(2, 6), (2, 8), (6, 6)]\n[(1, 2)]')
def test_minimax(self): player1 = game_agent.MinimaxPlayer(name='Player 1') player2 = game_agent.MinimaxPlayer(name='Player 2') #player2 = sample_players.GreedyPlayer() game = isolation.Board(player1, player2) # 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. game.apply_move((2, 3)) game.apply_move((0, 5)) # players take turns moving on the board, so player1 should be next to move assert (player1 == game.active_player) ''' # get a list of the legal moves available to the active player print(game.get_legal_moves()) # get a successor of the current state by making a copy of the board and # applying a move. Notice that this does NOT change the calling object # (unlike .apply_move()). new_game = game.forecast_move((1, 1)) assert(new_game.to_string() != game.to_string()) print("\nOld state:\n{}".format(game.to_string())) print("\nNew state:\n{}".format(new_game.to_string())) ''' # play the remainder of the game automatically -- outcome can be "illegal # move", "timeout", or "forfeit" winner, history, outcome = game.play() print("\nWinner: {}\nOutcome: {}".format(winner, outcome)) print(game.to_string()) print("Move history:\n{!s}".format(history))
def setUp(self): reload(game_agent) self.player1 = "Player1" self.player2 = "Player2" self.board = isolation.Board(self.player1, self.player2) self.board.apply_move((4, 4)) self.board.apply_move((0, 2))
def play(self, player_1, player_2): game = isolation.Board(player_1, player_2) winner, history, outcome = game.play() print("\nWinner: {}\nOutcome: {}".format( 'Player 1' if winner == player_1 else 'Player 2', outcome)) print(game.to_string()) print("Move history:\n{!s}".format(history))
def test_alphabetaPlayerOMS9x9(self): # One of the 9x9 Scenario as per Udacity self.game = isolation.Board(self.abPlayerOMS1, self.abPlayerOMS2, 9, 9) self.game.apply_move((1, 2)) self.game.apply_move((3, 2)) self.game.apply_move((4, 2)) self.game.apply_move((5, 2)) self.game.apply_move((6, 2)) self.game.apply_move((3, 3)) self.game.apply_move((4, 3)) self.game.apply_move((5, 3)) self.game.apply_move((4, 4)) self.game.apply_move((5, 4)) self.game.apply_move((6, 4)) self.game.apply_move((5, 5)) self.game.apply_move((6, 5)) self.game.apply_move((4, 6)) self.game.apply_move((6, 7)) self.game.apply_move((2, 5)) self.game.apply_move((2, 4)) # Player 1 self.assertTrue(self.abPlayerOMS2 == self.game.active_player) new_game = self.game.forecast_move((6, 3)) new_game.apply_move((0,3)) new_game.apply_move((0,5)) new_game.apply_move((5,1)) self.assertTrue(new_game.to_string() != self.game.to_string()) print("\nOld state:\n{}".format(self.game.to_string())) print("\nNew state:\n{}".format(new_game.to_string())) # Player 1 # Next move next_move = self.abPlayerOMS1.get_move(new_game, self.time_left) self.assertTrue(next_move == (1,3), msg = next_move)
def test_common_sense_2(self): """ - Player O has 8 movements, Player P has 8 movement: - 8 - 8 = 0 - Player P's may block an opponents next move - score += __mov_value * 2 = 24 | | | o | p | o | p | | | | o | | | | o | | | | | p | O | | | p | | | o | | | P | o | | | | | x | | o | | p | | | | | p | | p | | """ agent_ut = game_agent.CustomPlayer() board = isolation.Board(agent_ut, 'null_agent') starting_location = (3, 4) adversary_location = (2, 3) board.apply_move(starting_location) board.apply_move(adversary_location) score = scoring.common_sense(board, board.active_player) self.assertEqual(score, 24)
def setup_board(self): player1 = sample_players.RandomPlayer() player2 = sample_players.RandomPlayer() game = isolation.Board(player1, player2) game.apply_move((3, 2)) #1 game.apply_move((4, 2)) game.apply_move((2, 2)) #1 game.apply_move((5, 2)) game.apply_move((1, 2)) #1 game.apply_move((6, 2)) game.apply_move((0, 2)) #1 game.apply_move((6, 3)) game.apply_move((0, 1)) #1 game.apply_move((6, 4)) ''' 0 1 2 3 4 5 6 0 | | 1 | - | | | | | 1 | | | - | | | | | 2 | | | - | | | | | 3 | | | - | | | | | 4 | | | - | | | | | 5 | | | - | | | | | 6 | | | - | 2 | | | | ''' return game
def test_CustomScore3(self): self.player1 = AlphaBetaPlayer() self.player2 = AlphaBetaPlayer() time_millis = lambda: 1000 * timeit.default_timer() move_start = time_millis() time_left = lambda : 150 - (time_millis() - move_start) self.player1.time_left = time_left self.player2.time_left = time_left self.player1.score = game_agent.custom_score_3 self.player2.score = game_agent.custom_score_3 self.game = isolation.Board(self.player1, self.player2, 9, 9) self.game.apply_move((3,3)) while True: # player2 best_move = self.player2.alphabeta(self.game, 1) if best_move == (-1, -1): break self.game.apply_move(best_move) # player1 best_move = self.player1.alphabeta(self.game, 1) if best_move == (-1, -1): break self.game.apply_move(best_move) assert(len(self.game.get_legal_moves())==0)
def test4(): player1 = MinimaxPlayer(search_depth=1) player2 = MinimaxPlayer(search_depth=0) game = isolation.Board(player1, player2, height=5, width=5) game.apply_move((0,3)) game.apply_move((4,4)) print(game.to_string())
def test_performance(self): # control_group = {"Null Score" : sample_players.null_score, # "Open Move Score" : sample_players.open_move_score, # "Improved Score" : sample_players.improved_score} control_group = {"Improved Score" : sample_players.improved_score} # test_group = {"custom_score": game_agent.custom_score, # "custom_score_2": game_agent.custom_score_2, # "custom_score_3": game_agent.custom_score_3} test_group = {"Custom Score": game_agent.custom_score} total = 10 for j in range(1): scores = dict(zip(test_group,[[0]*len(control_group) for n in range(len(test_group))])) for round in range(total): for k in test_group: for i, c in enumerate(control_group): player1 = game_agent.AlphaBetaPlayer(score_fn=test_group[k]) player2 = game_agent.AlphaBetaPlayer(score_fn=control_group[c]) game = isolation.Board(player1, player2, 7, 7) for _ in range(2): move = random.choice(game.get_legal_moves()) game.apply_move(move) winner, history, outcome = game.play() winner_str = "Player 1" if winner == player1 else "Player 2" print("Player 1:",k,"| Player 2:",c) print("Winner: {}\nOutcome: {}".format(winner_str, outcome)) print("Percentage of blanks spaces left:", len(game.get_blank_spaces())/(game.width*game.height)) print("Move history:\n{!s}\n".format(history)) if winner == player1: scores[k][i] += 1 print(list(control_group)) for k in scores: print("{0} | {1}".format(k, [x/total for x in scores[k]]))
def test_minimax_interface(self): """Test CustomPlayer.minimax interface with simple input """ h, w = 7, 7 # board size test_depth = 1 starting_location = (5, 3) adversary_location = (0, 0) # top left corner heuristic = lambda g, p: 0. # return 0 everywhere # create a player agent & a game board agentUT = game_agent.MinimaxPlayer(test_depth, heuristic) agentUT.time_left = lambda: 99 # ignore timeout for fixed-depth search board = isolation.Board(agentUT, 'null_agent', w, h) # place two "players" on the board at arbitrary (but fixed) locations board.apply_move(starting_location) board.apply_move(adversary_location) for move in board.get_legal_moves(): next_state = board.forecast_move(move) v, _ = agentUT.minimax_with_score(next_state, test_depth, True) self.assertTrue( type(v) == float, ("Minimax function should return a floating " + "point value approximating the score for the " + "branch being searched."))
def vest_it_runs_in_realistic_time(self): player1 = "Player 1" player2 = "Player 2" size_x = 1000 size_y = 100 game = isolation.Board(player1, player2, size_x, size_y) reachables = reachability(game, (0, 1))
def test_get_distances_center(self): """ | | | | | | | | | 2| 3| 2| 3| 2| 3| 2| | | | | | | | | | 3| 4| 1| 2| 1| 4| 3| | | | | | | | | | 2| 1| 2| 3| 2| 1| 2| | | | | 1| | | | =====> | 3| 2| 3| 0| 3| 2| 3| | | | | | | | | | 2| 1| 2| 3| 2| 1| 2| | | | | | | | | | 3| 4| 1| 2| 1| 4| 3| | | | | | | | | | 2| 3| 2| 3| 2| 3| 2| Game State Distances to Player 1 """ reload(game_agent) self.player1 = game_agent.AlphaBetaPlayer() self.player2 = game_agent.AlphaBetaPlayer() self.game = isolation.Board(self.player1, self.player2) self.game.apply_move((3, 3)) distances = game_agent.get_distances(self.game, self.game.get_player_location(self.player1)) assert distances == [2, 3, 2, 3, 2, 3, 2, 3, 4, 1, 2, 1, 4, 3, 2, 1, 2, 3, 2, 1, 2, 3, 2, 3, 0, 3, 2, 3, 2, 1, 2, 3, 2, 1, 2, 3, 4, 1, 2, 1, 4, 3, 2, 3, 2, 3, 2, 3, 2]
def test_alphabeta_interface(self): """ Test CustomPlayer.alphabeta interface with simple input """ h, w = 9, 9 # board size test_depth = 1 starting_location = (2, 7) adversary_location = (0, 0) # top left corner iterative_search = False search_method = "alphabeta" heuristic = lambda g, p: 0. # return 0 everywhere # create a player agent & a game board agentUT = game_agent.CustomPlayer(test_depth, heuristic, iterative_search, search_method) agentUT.time_left = lambda: 99 # ignore timeout for fixed-depth search board = isolation.Board(agentUT, 'null_agent', w, h) # place two "players" on the board at arbitrary (but fixed) locations board.apply_move(starting_location) board.apply_move(adversary_location) for move in board.get_legal_moves(): next_state = board.forecast_move(move) v, _ = agentUT.alphabeta(next_state, test_depth) self.assertTrue( type(v) == float, ("Alpha Beta function should return a floating " + "point value approximating the score for the " + "branch being searched."))
def test_get_max_depth_case1(self): """ | -| -| | -| -| -| | | -| -| 2| -| -| -| 4| | | -| -| -| | -| -| | 1| -| -| -| 3| -| -| | -| -| -| -| -| | -| | -| -| -| -| -| 5| -| | -| 1| -| -| -| -| -| =====> | -| 0| -| -| -| -| -| ===> Max height = 6 | -| -| -| -| -| -| | | -| -| -| -| -| -| 6| | -| -| -| -| -| -| -| | -| -| -| -| -| -| -| | -| -| -| -| -| | -| | -| -| -| -| -| 7| -| Game State Max height from Player 1, cut off at 6 """ self.player1 = game_agent.AlphaBetaPlayer() self.player2 = game_agent.AlphaBetaPlayer() self.game = isolation.Board(self.player1, self.player2) self.game._board_state[0: self.game.height * self.game.width] = [float("inf") for _ in range(self.game.height * self.game.width)] self.game._board_state[1] = 0 #1 self.game._board_state[10] = 0 #0 self.game._board_state[14] = 0 #2 self.game._board_state[29] = 0 #3 self.game._board_state[37] = 0 #5 self.game._board_state[41] = 0 #7 self.game._board_state[42] = 0 #4 self.game._board_state[46] = 0 #6 self.game.apply_move((3,1)) max_depth = game_agent.get_max_depth(self.game, self.game.get_player_location(self.player1)) assert max_depth == 6