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") ] # 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"), Agent(GreedyPlayer(), "AB_Greedy") ] print(DESCRIPTION) print("{:^74}".format("*************************")) print("{:^74}".format("Playing Matches")) print("{:^74}".format("*************************")) play_matches(cpu_agents, test_agents, NUM_MATCHES)
def play_Greedy(first): print( "*************************** *************************** *************************** ", "\n") print(" --------------- ", first, " versus improved-Greedy ---------------- ", "\n") print("\n") print("---------------- testing alpha beta player(", num_matches, ") -------------------------", "\n") print( "*************************** *************************** *************************** ", "\n") self.player1 = game_agent.AlphaBetaPlayer(first) self.player2 = GreedyPlayer() self.game = isolation.Board(self.player1, self.player2, height, width) time_millis = lambda: 1000 * timeit.default_timer() if Verbose or Boxes: print(self.game.print_board()) for i in range(800): move_start = time_millis() time_left = lambda: TIME_LIMIT_MILLIS - (time_millis() - move_start) move = self.game.active_player.get_move(self.game, time_left) if self.game.is_loser(self.game.active_player): if self.game.active_player == self.player1: lost[0] += 1 else: lost[1] += 1 print("test_game.. lost:", lost) # if move == (-1, -1): must be the same as "is_loser" print( "*************************** *************************** ", "\n") print(" dead-end reached ", "\n") print( "*************************** *************************** ", "\n") break else: self.game.apply_move(move) if Verbose or Boxes: print("i:", i, "_", move, "\n") print(self.game.print_board(), "\n") for x in range(2): print("lost:", lost[x], "\n") print( "*************************** *************************** *************************** " ) print("player_lost:", player_lost) print( "*************************** *************************** *************************** ", "\n")
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"), Agent(GreedyPlayer(), "Greedy") ] # 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(GreedyPlayer(score_fn=open_move_score), "Greedy"), # Agent(CustomPlayer(score_fn=open_move_score, **CUSTOM_ARGS), "Open Move"), # Agent(CustomPlayer(score_fn=improved_score, **CUSTOM_ARGS), "ID_Improved"), Agent(CustomPlayer(score_fn=aggressive_score, **CUSTOM_ARGS), "Student Aggressive"), Agent(CustomPlayer(score_fn=balanced_score, **CUSTOM_ARGS), "Student Balanced"), Agent(CustomPlayer(score_fn=mcs_score, **CUSTOM_ARGS), "Student MCS"), ] 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))
def testBoard(self): AB = AlphaBetaPlayer() MM = MinimaxPlayer() greedy = GreedyPlayer() rand = RandomPlayer() game = Board(AB, MM) game.apply_move((1, 5)) game.applymove((2, 3)) self.assertTrue(game.check_legal_move((4, 5))) self.assertEqual((2, 3), game.get_player_location(game._active_player))
def endgames(score_fns=[center_score, open_move_score, improved_score]): starting_location = (3, 3) adversary_location = (1, 1) boards = { fn.__name__: CounterBoard(game_agent.MinimaxPlayer(score_fn=fn), GreedyPlayer(), 7, 7) for fn in score_fns } for b in boards.values(): b.apply_move(starting_location) b.apply_move(adversary_location) results = {k: v.play() for k, v in boards.items()} return boards, results
def game_lengths(score_fns=[center_score, open_move_score, improved_score]): boards = { fn.__name__: CounterBoard(game_agent.MinimaxPlayer(score_fn=fn), GreedyPlayer(), 7, 7) for fn in score_fns } import random for b in boards.values(): legal_moves = b.get_legal_moves() b.apply_move(legal_moves[random.randrange(len(legal_moves))]) legal_moves = b.get_legal_moves() b.apply_move(legal_moves[random.randrange(len(legal_moves))]) results = {k: v.play() for k, v in boards.items()} return [len(v[1]) for v in results.values()]
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))
def opening_scores(score_fns=[center_score, open_move_score, improved_score]): starting_location = (3, 3) adversary_location = (1, 1) boards = { fn.__name__: CounterBoard(game_agent.MinimaxPlayer(score_fn=fn), GreedyPlayer(), 7, 7) for fn in score_fns } for b in boards.values(): b.apply_move(starting_location) b.apply_move(adversary_location) plys = { k: [v.forecast_move((i, j)) for i in range(0, 7) for j in range(0, 7)] for k, v in boards.items() } for k, v in plys.items(): scoring_heatmap(boards[k], v, k)
class IsolationTest(unittest.TestCase): """Unit tests for isolation agents""" def setUp(self): reload(game_agent_test) self.player1 = MinimaxPlayer(IsolationPlayer()) self.player2 = GreedyPlayer() self.game = isolation.Board(self.player1, self.player2) def test(self): for i in range(0,50): p1Move = self.player1.minimax(self.game,3) if p1Move == (-1,-1): print("exasa") break self.game.apply_move(p1Move) p2Move = self.player2.get_move(self.game,1) if p2Move == (-1,-1): print("nikisa") break self.game.apply_move(p2Move)
import time from isolation import Board from sample_players import GreedyPlayer from game_agent import CustomPlayer player_1 = CustomPlayer() player_2 = GreedyPlayer() #player_2 = RandomPlayer() print(player_1, player_2) test_game = Board(player_1, player_2) start = time.time() winner, moves, reason = test_game.play() end = time.time() #print (winner) if reason == "timeout": print("Forfeit due to timeout.") for move in moves: print(move) print( 'Play Summary : Time taken = {0}, number of move = {1}, winner= {2}, Reason ={3}' .format(end - start, len(moves), winner, reason))
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))
from isolation import Board from sample_players import GreedyPlayer from game_agent import MinimaxPlayer from game_agent import AlphaBetaPlayer p1 = AlphaBetaPlayer() #p2 = MinimaxPlayer() p2 = GreedyPlayer() game = Board(p1, p2) game.apply_move((3, 3)) game.apply_move((0, 5)) print(game.get_legal_moves()) winner, history, outcome = game.play() print("\nWinner: {}, Outcome: {}".format(winner, outcome)) print(game.to_string()) print("History:\n{!s}".format(history))
def test1(): player1 = AlphaBetaPlayer(search_depth=20, name='p1', score_fn=open_move_score) player2 = GreedyPlayer() game = isolation.Board(player1, player2, height=9, width=9) print(game.play(time_limit=500))
def setUp(self): reload(game_agent_test) self.player1 = MinimaxPlayer(IsolationPlayer()) self.player2 = GreedyPlayer() self.game = isolation.Board(self.player1, self.player2)
def setUp(self): reload(game_agent) self.player1 = AlphaBetaPlayer() self.player2 = GreedyPlayer() self.game = isolation.Board(self.player1, self.player2)
class IsolationTest(unittest.TestCase): """Unit tests for isolation agents""" def setUp(self): reload(game_agent) self.player1 = "Player1" self.player2 = "Player2" self.game = isolation.Board(self.player1, self.player2) #test minimax player1 = GreedyPlayer() player2 = MinimaxPlayer() game = Board(player1, player2, 5, 5) # 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)) print(game.to_string()) print(game.hash()) print(game.board_to_matrix()) # 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()) # player1.minimax_score(game, 1) # 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)) #test alpha_beta player1 = GreedyPlayer() player2 = AlphaBetaPlayer() game = Board(player1, player2, 5, 5) # 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)) print(game.to_string()) print(game.hash()) print(game.board_to_matrix()) # 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()) # player1.minimax_score(game, 1) # 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))
from isolation import Board from sample_players import GreedyPlayer from sample_players import RandomPlayer from game_agent import CustomPlayer from sample_players import null_score player1 = CustomPlayer(3, null_score, True, 'minimax') player2 = GreedyPlayer() game = Board(player1, player2) game.apply_move((2, 3)) game.apply_move((0, 5)) winner, history, outcome = game.play() print( 'student agent with 3 depths, null_score, iterative and minimax VS GreedyPlayer' ) print("\nWinner: {}\nOutcome: {}".format(winner, outcome)) print(game.to_string()) print("Move history:\n{!s}".format(history))
def run(): import logging from logging.config import dictConfig logging_config = dict( version=1, formatters={ 'f': { 'format': '%(asctime)s %(name)-12s %(levelname)-8s %(message)s' } }, handlers={ 'h': { 'class': 'logging.StreamHandler', 'formatter': 'f', 'level': logging.DEBUG } }, root={ 'handlers': ['h'], 'level': logging.DEBUG, }, # filename='example.log', filemode='w', level=logging.DEBUG) ) dictConfig(logging_config) # logger = logging.getLogger() logging.getLogger(__name__).addHandler(logging.NullHandler()) # try: # Copy of minimax Unit Test for debugging only # import isolation from sample_players import GreedyPlayer, null_score, open_move_score, improved_score p1 = game_agent.AlphaBetaPlayer(score_fn=open_move_score) p2 = GreedyPlayer() test_depth = 1 heuristic = open_move_score #, iterative_search, search_method) # agentUT.time_left = lambda: 99 # ignore timeout for fixed-depth search board = CounterBoard(p1, p2, 7, 7) starting_location = (2, 3) adversary_location = (0, 0) # top left corner # board.apply_move(starting_location) # board.apply_move(adversary_location) # disable search timeout by returning a constant value p1.time_left = lambda: 1e3 try: #legal_moves=board.get_legal_moves() #move=p1.minimax(board,test_depth) #print(move) #move = p1.get_move(board, legal_moves, lambda: 99) # assert move in 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." return p1, p2, board except SystemExit: logging.exception('SystemExit occurred') except: logging.exception('Unknown exception occurred.')