def test_minimax(self): p1 = game_agent.MinimaxPlayer() p1.search_depth = 1 p2 = game_agent.MinimaxPlayer() p2.search_depth = 1 # Create a smaller board for testing self.game = isolation.Board(p1, p2, 9, 9) self.game._board_state = [ 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, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 41, 21 ] logging.info(self.game.to_string()) # players take turns moving on the board, so player1 should be next to move assert (p1 == self.game.active_player) winner, history, outcome = self.game.play() logging.info("\nWinner: {}\nOutcome: {}".format(winner, outcome)) logging.info(self.game.to_string()) logging.info("Move history:\n{!s}".format(history)) # Check the first selected move expectedResults = [[2, 4]] self.assertIn(history[0], expectedResults)
def test_get_moves1(self): d = 9 self.player1 = game_agent.MinimaxPlayer(search_depth=2) self.player2 = game_agent.MinimaxPlayer(search_depth=2) self.game = isolation.Board(self.player1, self.player2, width=d, height=d) self.game._board_state = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 42 ] self.game.move_count = len( [x for x in self.game._board_state[0:-4] if x == 1]) print(self.game.to_string()) moves = game_agent.get_moves(self.game, [(6, 5)]) print(len(moves)) print(moves) self.assertEqual(len(moves), 6) moves = game_agent.get_moves(self.game, moves) print(len(moves)) print(moves) self.assertEqual(len(moves), 13)
class MinimaxTest(unittest.TestCase): """Unit tests for MinimaxTests""" reload(game_agent) player1 = game_agent.MinimaxPlayer() player1.time_left = tl player2 = game_agent.MinimaxPlayer() # setup a board with no moves to make sure returns correctly def trivial_setup(self): game = isolation.Board(self.player1, self.player2, 2, 2) game.apply_move((0, 1)) game.apply_move((1, 0)) return game def setUp(self, pos1=(0, 1), pos2=(1, 0)): game = isolation.Board(self.player1, self.player2) game.apply_move(pos1) game.apply_move(pos2) return game # Test that minimax returns when no moves left def testTrivial(self): game = self.trivial_setup() ret = self.player1.minimax(game, 3) self.assertTrue(ret == (-1, -1), ret) def testMiniMax(self): game = self.setUp() ret = self.player1.minimax(game, 3)
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 = Agent( game_agent.MinimaxPlayer(score_fn=open_move_score), "MM_Open") self.player2 = Agent(game_agent.MinimaxPlayer(score_fn=improved_score), "MM_Improved") self.game = isolation.Board(self.player1, self.player2) self.game.play()
def setUp(self): reload(game_agent) self.player1 = game_agent.MinimaxPlayer() self.player2 = sample_players.GreedyPlayer() self.player3 = sample_players.RandomPlayer() self.player4 = game_agent.AlphaBetaPlayer() self.player5 = game_agent.AlphaBetaPlayer() self.player6 = game_agent.MinimaxPlayer() self.game = None
def test_empty(self): reload(game_agent) self.player1 = game_agent.MinimaxPlayer() self.player2 = game_agent.MinimaxPlayer() game = isolation.Board(self.player1, self.player2) self.assertEqual(game.get_player_location(self.player1), None) self.assertEqual(game.get_player_location(self.player2), None) game.play(time_limit=100) self.assertNotEqual(game.get_player_location(self.player1), None) self.assertNotEqual(game.get_player_location(self.player2), None)
def setUp(self): reload(game_agent) self.player1 = game_agent.MinimaxPlayer(name="Player 1", search_depth=1) self.player2 = game_agent.MinimaxPlayer(name="Player 2", search_depth=1) self.game = isolation.Board(self.player1, self.player2, width=9, height=9) self.game.set_board_state(self.ud_submit_board_state)
def _test_minimax(self): print("---------- testing min max player ----------") self.player1 = game_agent.MinimaxPlayer() self.player2 = game_agent.MinimaxPlayer() for i in range(75): move = self.game.active_player.get_move(self.game, time_left=lambda: 100000) print(move) self.game.apply_move(move) print(self.game.print_board())
def test1(self): player1 = game_agent.MinimaxPlayer() player2 = game_agent.MinimaxPlayer() game = isolation.Board(player1, player2) game.apply_move((2, 3)) game.apply_move((0, 5)) assert (player1 == game.active_player) winner, history, outcome = game.play()
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 get_next_move(self, board_size, depth, visited_moves): player1 = game_agent.MinimaxPlayer(depth) player2 = game_agent.MinimaxPlayer(depth) self.game = isolation.Board(player1, player2, board_size, board_size) for move in visited_moves: print('Applying move {}'.format(move)) self.game.apply_move(move) print(self.game.print_board()) next_move = player1.get_move(self.game, lambda: 150) self.game.apply_move(next_move) print(self.game.print_board()) return next_move
def reset_boards(self): self.my_player = game_agent.MinimaxPlayer(3,game_agent.custom_score,10) self.opponent = sample_players.GreedyPlayer(sample_players.open_move_score) self.game_board_1 = TestBoard(self.my_player, self.opponent) self.game_board_2 = TestBoard(self.opponent,self.my_player) self.my_player = game_agent.MinimaxPlayer(5,game_agent.custom_score,10) self.game_board_3 = TestBoard(self.my_player, self.opponent) self.my_player = game_agent.MinimaxPlayer(10,game_agent.custom_score,10) self.game_board_4 = TestBoard(self.my_player, self.opponent) self.my_player = game_agent.MinimaxPlayer(3,game_agent.custom_score_2,10) self.game_board_5 = TestBoard(self.my_player, self.opponent) self.my_player = game_agent.MinimaxPlayer(3,game_agent.custom_score_3,10) self.game_board_6 = TestBoard(self.my_player, self.opponent)
def testSecondPlayerShouldWinIn2x3(self): player1 = ga.MinimaxPlayer(search_depth=1, score_fn=sp.open_move_score) player2 = ga.MinimaxPlayer(search_depth=1, score_fn=sp.open_move_score) game = isolation.Board(player1, player2, 2, 3) winner, moves, _ = game.play(100) self.assertEqual(winner, player2, "player2 should have won the match") self.assertEqual(len(moves), 2, "there should have been two moves, due to the clear win") self.assertEqual(moves[0], [2, 1], "player1 should have picked the top left corner since \ it's the first node in the tree") self.assertEqual(moves[1], [0, 0], "player2 should have picked the opposite corner since \ it's the first node that has a direct win")
def test_get_long_path_potential_score(self): d = 9 self.player1 = game_agent.MinimaxPlayer(search_depth=2) self.player2 = game_agent.MinimaxPlayer(search_depth=2) self.game = isolation.Board(self.player1, self.player2, width=d, height=d) self.game._board_state = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 42 ] self.game.move_count = len( [x for x in self.game._board_state[0:-4] if x == 1]) print(self.game.to_string()) start = time.time() score = game_agent.custom_score_2(self.game, self.player1) elapsed = time.time() - start print('generations_weighted_difference {}'.format(score)) print('elapsed {}'.format(elapsed)) start = time.time() score = game_agent.custom_score_3(self.game, self.player1) elapsed = time.time() - start print('get_long_path_potential_score {}'.format(score)) print('elapsed {}'.format(elapsed)) moves = game_agent.get_moves( self.game, [self.game.get_player_location(self.player1)]) start = time.time() max, avg = game_agent.max_avg_path(self.game, moves) elapsed = time.time() - start print('max_avg_path {} {}'.format(max, avg)) print('elapsed {}'.format(elapsed)) start = time.time() max_pos = 100 max = game_agent.dfs(self.game, self.game.get_player_location(self.player1), max_pos) elapsed = time.time() - start print('dfs {} max positions {}'.format(max, max_pos)) print('elapsed {}'.format(elapsed))
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 test_minimax(self): reload(game_agent) self.player1 = game_agent.MinimaxPlayer() self.player2 = sample_players.RandomPlayer() self.game = isolation.Board(self.player1, self.player2) time_left = lambda : 10. print(self.player1.get_move(self.game, time_left))
def test_minimax_depth_5(self): player = game_agent.MinimaxPlayer() player.time_left = self.create_clock(1000) best_move = player.minimax(self.board, 5) self.assertTrue( best_move in [(2, 3), (3, 2), (2, 5), (3, 6), (5, 2), (5, 6), (6, 3), (6, 5)], 'best move: ' + str(best_move))
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 test_minimax_get_move(self): # TODO: All methods must start with "test_" game = self.game player = game_agent.MinimaxPlayer() player.time_left = 10. print(player.minimax(game, 3)) self.assertTrue(0 == 0)
def test_minimax_no_moves(self): self.player1 = game_agent.MinimaxPlayer(1, left_and_uppermost, 0) self.player2 = "Player 2" self.game = isolation.Board(self.player1, self.player2, 1, 1) self.game._board_state[0] = 'X' move = self.player1.get_move(self.game, always_time_left) self.assertEqual(move, (-1, -1))
def test_alpha_beta(self): minimax_player = game_agent.MinimaxPlayer( score_fn=sample_players.open_move_score) random_player = sample_players.RandomPlayer() game = isolation.Board(minimax_player, random_player) result = game.play(time_limit=TIME_LIMIT) pdb.set_trace()
def test_minimax(self): player1 = game_agent.MinimaxPlayer() player2 = game_agent.AlphaBetaPlayer() the_board = isolation.Board(player1, player2) assert (the_board.get_legal_moves() is not None), "Legal moves is not blank"
def test_minimax(self): #Set up board print("Setting up inital board for simple minimax player test....") self.setUp() #Put some moves on board to create different start states # Player 1 move 1 self.game.active_player == self.player1 self.game.apply_move((0, 0)) # Player 2 move 1 self.game.active_player == self.player2 self.game.apply_move((0, 2)) ## # Player 1 move 2 ## self.game.active_player==self.player1 ## self.game.apply_move((2,1)) ## # Player 2 move 2 ## self.game.active_player==self.player2 ## self.game.apply_move((2,0)) #Print inital board print("\nInital state:\n{}".format(self.game.to_string())) #Calculate player 1 best next move using MiniMax best_move = (-1, -1) MinimaxPlayer = game_agent.MinimaxPlayer() best_move = MinimaxPlayer.get_move(self.game, self.timeLimit) print('Best move for Player 1 is', best_move)
def xtest_minimax(self): self.setUp(5, 5) game = self.game minimaxPlayer = game_agent.MinimaxPlayer(1, game_agent.custom_score) #self.game.apply_move((0,2)) #self.game.apply_move((0,4)) print('##################################################') print(game.to_string()) print('##################################################') for i in range(25): moves = game.get_legal_moves() if len(moves) == 0: break self.print_moves(moves) print('active player', game.active_player) print('minimax number of legal moves', len(moves)) move = minimaxPlayer.get_move(game, self.timeLeft) self.print_move(move) game.apply_move((move)) print('##################################################') print(game.to_string()) print('##################################################', i) if game.is_loser(game.active_player): print(game.active_player, "has lost the game") print(game.inactive_player, "has won the game")
def test_single_min_max(self): width, height = 7, 7 #timeout = float("-inf") timeout = 10. self.player1 = game_agent.MinimaxPlayer( search_depth=3, score_fn=game_agent.custom_score, timeout=timeout) self.player2 = sample_players.GreedyPlayer( score_fn=game_agent.custom_score_3) self.game = isolation.Board(self.player1, self.player2, width=width, height=height) #self.game.apply_move((random.randint(0, self.game.width-1), random.randint(0, self.game.height-1))) #self.game.apply_move((random.randint(0, self.game.width-1), random.randint(0, self.game.height-1))) print(self.game.to_string()) # players take turns moving on the board, so player1 should be next to move assert (self.player1 == self.game.active_player) # get a list of the legal moves available to the active player print(self.game.get_legal_moves()) # play the remainder of the game automatically -- outcome can be "illegal # move", "timeout", or "forfeit" winner, history, outcome = self.game.play() print("\nWinner: {}\nOutcome: {}".format( "player 1" if winner == self.player1 else "player 2", outcome)) print("\nstate:\n{}".format(self.game.to_string())) print("\nhistory:\n{}".format(history))
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 test_minimax_init(self): self.setUp_random_vs_minimax() player = game_agent.MinimaxPlayer(search_depth=10, timeout=5.) self.assertTrue(player.search_depth == 10) self.assertTrue(player.TIMER_THRESHOLD == 5.) self.assertTrue(player.score == game_agent.min_value) pass
def setUp(self): reload(game_agent) self.player1 = game_agent.MinimaxPlayer() self.player2 = sample_players.GreedyPlayer( sample_players.improved_score) self.player3 = game_agent.AlphaBetaPlayer() # self.game = isolation.Board(self.player1, self.player2,7,7) self.game = isolation.Board(self.player3, self.player2, 7, 7)
def test_minimax_scores_expected_number_of_nodes(self): self.player1 = game_agent.MinimaxPlayer(score_fn=open_move_score, search_depth=2) self.game = isolation.Board(self.player1, self.player2, width=9, height=9) self.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, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 32] with mock.patch.object(self.player1, 'score', wraps=self.player1.score) as score_fn: self.player1.get_move(self.game, lambda : 15.) self.assertEqual(score_fn.call_count, 35)