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_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_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_dict = {'count':0} def get_heuristic(heuristic_dict): def heuristic(g, p): if heuristic_dict['count'] == 9: #stop at depth 3 raise game_agent.Timeout() heuristic_dict['count'] += 1 return 0. return heuristic # create a player agent & a game board agentUT = game_agent.CustomPlayer( test_depth, get_heuristic(heuristic_dict), 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) legal_moves = board.get_legal_moves() move = agentUT.get_move(board, legal_moves, lambda: 99) self.assertEqual(heuristic_dict['count'],9,'Wrong number of nodes visited')
def initAUT(self, depth, eval_fn, iterative=False, method="minimax", loc1=(3, 3), loc2=(0, 0), w=7, h=7): """Generate and initialize player and board objects to be used for testing. """ reload(game_agent) agentUT = game_agent.CustomPlayer(depth, eval_fn, iterative, method) board = CounterBoard(agentUT, 'null_agent', w, h) board.apply_move(loc1) board.apply_move(loc2) return agentUT, board
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 initAUT(self, depth, eval_fn, iterative=False, method="minimax", loc1=(3, 3), loc2=(0, 0), w=7, h=7): reload(game_agent) agentUT = game_agent.CustomPlayer(depth, eval_fn, iterative, method) board = CounterBoard(agentUT, 'null_agent', w, h) board.apply_move(loc1) board.apply_move(loc2) return agentUT, board
def test_toe_stepper(self): """ | 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.toe_stepper(board, board.active_player) self.assertEqual(score, 74)
def test_common_sense_3(self): """ - Player O has 2 movements, Player P has 4 movement: - 4 - 2 = 2 * 10 = 20 | | | | | | | | | | | | | | | | | | | | | p | | p | | | | | p | | o | | | | | | | o | P | | | | | | p | | | O | """ agent_ut = game_agent.CustomPlayer() board = isolation.Board(agent_ut, 'null_agent') starting_location = (5, 5) adversary_location = (6, 6) board.apply_move(starting_location) board.apply_move(adversary_location) score = scoring.common_sense(board, board.active_player) self.assertEqual(score, 20)