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") ] print(DESCRIPTION) print("{:^74}".format("*************************")) print("{:^74}".format("Playing Matches")) print("{:^74}".format("*************************")) play_matches(cpu_agents, test_agents, NUM_MATCHES)
def custom_test(): # 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=custom_score), "AB_Custom"), ] # Define a collection of agents to compete against the test agents cpu_agents = [ Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved"), ] print(DESCRIPTION) print("{:^74}".format("*************************")) print("{:^74}".format("Playing Matches")) print("{:^74}".format("*************************")) NUM_MATCHES = 50 play_matches(cpu_agents, test_agents, NUM_MATCHES)
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__())
def fitness(a, b): score_func = generate_score_function(a, b) test_agent = Agent(AlphaBetaPlayer(score_fn=score_func), "AB_Custom") results = play_matches(cpu_agents, [test_agent], NUM_MATCHES) win_ratio = results[test_agent] error = 100. / win_ratio return error
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 test6(): player1 = AlphaBetaPlayer(search_depth=1, score_fn=custom_score) player2 = AlphaBetaPlayer(search_depth=1, score_fn=custom_score_3) game = isolation.Board(player1, player2, height=5, width=5) game.apply_move((0, 3)) print(game.to_string()) game.apply_move((4, 4)) print(game.to_string()) game.apply_move((3, 2)) print(game.to_string()) game.apply_move((1, 1)) game.apply_move((2, 0)) game.apply_move((3, 0)) game.apply_move((1, 2)) print(game.to_string()) print(custom_score(game, game.active_player)) print(custom_score(game, game.inactive_player)) print(custom_score_2(game, game.active_player)) print(custom_score_2(game, game.inactive_player)) print(custom_score_3(game, game.active_player)) print(custom_score_3(game, game.inactive_player)) print(improved_score(game, game.active_player)) print(improved_score(game, game.inactive_player))
def optimize(): 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") ] def fitness(a, b): score_func = generate_score_function(a, b) test_agent = Agent(AlphaBetaPlayer(score_fn=score_func), "AB_Custom") results = play_matches(cpu_agents, [test_agent], NUM_MATCHES) win_ratio = results[test_agent] error = 100. / win_ratio return error param_stack = twiddle(fitness) _, best_params1 = param_stack.pop() _, best_params2 = param_stack.pop() _, best_params3 = param_stack.pop() print(best_params1) print(best_params2) print(best_params3) score_func1 = generate_score_function(*best_params1) score_func2 = generate_score_function(*best_params2) score_func3 = generate_score_function(*best_params3) test_agents = [ Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved"), Agent(AlphaBetaPlayer(score_fn=score_func1), "AB_Custom_1"), Agent(AlphaBetaPlayer(score_fn=score_func2), "AB_Custom_2"), Agent(AlphaBetaPlayer(score_fn=score_func3), "AB_Custom_3"), ] print("{:^74}".format("*************************")) print("{:^74}".format("Playing Matches")) print("{:^74}".format("*************************")) tournament_play_matches(cpu_agents, test_agents, NUM_MATCHES)
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") ] print(DESCRIPTION) print("{:^74}".format("*************************")) print("{:^74}".format("Playing Matches")) print("{:^74}".format("*************************")) results = [] for i in range(5): print("{:^74}".format("*************************")) print("{:^74}".format("Playing Tournament N° {}".format(i + 1))) print("{:^74}".format("*************************")) tournament_results = play_matches(cpu_agents, test_agents, NUM_MATCHES) results.append(tournament_results) print("{:^74}".format("*************************")) print("{:^74}".format("Final results")) print("{:^74}".format("*************************")) print(results) with open(RESULTS, "w") as output: count = 1 for result in results: print("Tournament N° {}: {}".format(count, result)) output.write("Tournament N° {}".format(count) + str(result) + "\n") count += 1
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) def test_minimax(self): 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, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 57 ] self.player1 = AlphaBetaPlayer(2, score_fn=open_move_score) self.player2 = AlphaBetaPlayer(2, score_fn=open_move_score) move = self.player1.alphabeta(self.game, 2)
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(MinimaxPlayer(score_fn=custom_score), "MM_Custom"), # Agent(MinimaxPlayer(score_fn=custom_score_2), "MM_Custom_2"), # Agent(MinimaxPlayer(score_fn=custom_score_3), "MM_Custom_3"), 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"), Agent(AlphaBetaPlayer(score_fn=custom_score_4), "AB_Custom_4"), Agent(AlphaBetaPlayer(score_fn=custom_score_5), "AB_Custom_5"), # Agent(AlphaBetaPlayer(score_fn=custom_score_6), "AB_Custom_6"), # 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(RandomPlayer(), "Random") ] # 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") ] print(DESCRIPTION) print("{:^74}".format("*************************")) print("{:^74}".format("Playing Matches")) print("{:^74}".format("*************************")) test_scores = defaultdict(list) for i in range(NUM_REPEATS): print(" " * 74) print("{:>37}{:d}".format("Sample ", i + 1)) print("-" * 74) wins = play_matches(cpu_agents, test_agents, NUM_MATCHES) for a in test_agents: test_scores[a.name].append(wins[a.player] / NUM_MATCHES) return wins, test_scores
def main(): # Define two agents to compare -- these agents will play from the same # starting position against the same adversaries in the tournament test_agent = \ Agent(AlphaBetaPlayer(score_fn=custom_score_2), "AB_Custom_2") # Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved"), # Agent(AlphaBetaPlayer(score_fn=custom_score), "AB_Custom") # Agent(AlphaBetaPlayer(score_fn=custom_score_3), "AB_Custom_3") # Define a collection of agents to compete against the test agents cpu_agent = \ Agent(MinimaxPlayer(score_fn=center_score), "MM_Center") # Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open") # Agent(RandomPlayer(), "Random"), # 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") play_round(cpu_agent, test_agent)
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") ] """ Uncomment to add grid search function agents to test_agents """ # print("{} grid search functions".format(len(gs_funcs))) # for name, func in gs_funcs.items(): # test_agents.append(Agent(AlphaBetaPlayer(score_fn=func), "AB_{}".format(name))) # print("{} grid search2 functions".format(len(gs2_funcs))) # for name, func in gs2_funcs.items(): # test_agents.append(Agent(AlphaBetaPlayer(score_fn=func), "AB_GS2_{}".format(name))) # 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") ] print(DESCRIPTION) print("{:^74}".format("*************************")) print("{:^74}".format("Playing Matches")) print("{:^74}".format("*************************")) play_matches(cpu_agents, test_agents, NUM_MATCHES)
from isolation import Board from sample_players import RandomPlayer, HumanPlayer, GreedyPlayer, open_move_score, center_score, improved_score, null_score from game_agent import MinimaxPlayer, AlphaBetaPlayer player1 = MinimaxPlayer(search_depth=2, score_fn=improved_score) player2 = AlphaBetaPlayer(search_depth=3, score_fn=improved_score) game = Board(player1, player2) winner, history, outcome = game.play(time_limit=9999999) print("\nWinner: {}\nOutcome: {}".format( "Player 1" if winner == player1 else "Player 2", outcome)) print(game.to_string()) print("Move history:\n{!s}".format(history))
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_1"), # moves count delta Agent(AlphaBetaPlayer(score_fn=custom_score_2), "AB_CUSTOM_2"), # unique moves Agent(AlphaBetaPlayer(score_fn=custom_score_3), "AB_CUSTOM_3"), # weighted moves count delta Agent(AlphaBetaPlayer(score_fn=moves_delta_unique), "DELTA_UNIQ"), Agent(AlphaBetaPlayer(score_fn=moves_delta_unique_exp), "DELTA_UNIQ_EXP"), Agent(AlphaBetaPlayer(score_fn=moves_delta_exp), "DELTA_EXP"), Agent(AlphaBetaPlayer(score_fn=moves_delta_walls), "DELTA_WALLS") ] # 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") ] print(DESCRIPTION) print("{:^74}".format("*************************")) print("{:^74}".format("Playing Matches")) print("{:^74}".format("*************************")) play_matches(cpu_agents, test_agents, NUM_MATCHES)
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))
def setUp(self): reload(game_agent) self.player1 = MinimaxPlayer() self.player2 = AlphaBetaPlayer(search_depth=2)
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))
valid_choice = 0 <= index < len(legal_moves) if not valid_choice: print('Illegal move! Try again.') except ValueError: print('Invalid index! Try again.') return legal_moves[index] if __name__ == "__main__": from isolation import Board # create an isolation board (by default 7x7) player1 = AlphaBetaPlayer() player2 = HumanPlayer() game = Board(player1, player2) movetimelimit = 30000 # # 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()) # # players take turns moving on the board, so player1 should be next to move # assert(player1 == game.active_player)
def setUp(self): reload(game_agent) self.player1 = AlphaBetaPlayer() self.player2 = RandomPlayer() self.game = isolation.Board(self.player1, self.player2)
# -*- coding: utf-8 -*- """ Created on Tue Oct 3 11:28:01 2017 @author: sapereira """ #from game_agent import MinimaxPlayer from game_agent import AlphaBetaPlayer from isolation import Board #import isolation #import game_agent player1 = AlphaBetaPlayer() player2 = AlphaBetaPlayer() game = Board(player1, player2) game.apply_move((2, 3)) game.apply_move((0, 5)) print(game.to_string()) assert (player1 == game.active_player) print(game.get_legal_moves()) player1.terminal_test(game) game.move_count game.play() player1.alphabeta(game, 1)
def setUp(self): reload(game_agent) self.AB_custom_score = AlphaBetaPlayer(score_fn=custom_score) self.AB_custom_score_2 = AlphaBetaPlayer(score_fn=custom_score_2) self.AB_custom_score_3 = AlphaBetaPlayer(score_fn=custom_score_3) self.AB_Improved = AlphaBetaPlayer(score_fn=improved_score)
from isolation import Board from sample_players import (RandomPlayer, open_move_score, improved_score, center_score) from game_agent import (MinimaxPlayer, AlphaBetaPlayer, custom_score, custom_score_2, custom_score_3) from tournament import play_matches, play_round, update from time import time import timeit from copy import copy NUM_MATCHES = 5 # number of matches against each opponent TIME_LIMIT = 150 # number of milliseconds before timeout Agent = namedtuple("Agent", ["player", "name"]) Agent1 = Agent(AlphaBetaPlayer(score_fn=open_move_score), "MM_Open") #Agent1 = Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open") #Agent2 = Agent(RandomPlayer(), "Random") Agent2 = Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved") game = Board(Agent1.player, Agent2.player, 9, 9) #game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 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, 14, 51] #game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24, 32] #game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 51] 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, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 47, 51 ]
@author: Kelvin """ from game_agent import (MinimaxPlayer, AlphaBetaPlayer, custom_score, custom_score_2, custom_score_3, custom_score_4, custom_score_5) from sample_players import (RandomPlayer, open_move_score, improved_score, center_score) from isolation import Board import random import timeit player1 = RandomPlayer() player2 = AlphaBetaPlayer(score_fn=improved_score) player3 = AlphaBetaPlayer(score_fn=custom_score) player4 = AlphaBetaPlayer(score_fn=custom_score_2) player5 = AlphaBetaPlayer(score_fn=custom_score_3) player6 = AlphaBetaPlayer(score_fn=custom_score_4) player7 = AlphaBetaPlayer(score_fn=custom_score_5) time_limit = 150 time_millis = lambda: 1000 * timeit.default_timer() players = [player2, player3, player4, player5, player6, player7] i = 0 for p in players:
# reload(game_agent) # self.player1 = sample_players.GreedyPlayer # self.player2 = game_agent.AlphaBetaPlayer # self.game = isolation.Board(self.player1, self.player2) # game_agent.IsolationPlayer.__init__(3,game_agent.custom_score,10); if __name__ == "__main__": from isolation import Board # reload(game_agent) player1_win = 0; player2_win = 0; # create an isolation board (by default 7x7) while(player1_win+player2_win)<1: player2 = AlphaBetaPlayer(score_fn = improved_score); player1 = MinimaxPlayer(score_fn = improved_score); game = Board(player1, player2); # place player 1 on the board at row 3, 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((3, 3)) # game.apply_move((2, 3)) # print(game.to_string()) # 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())
except ValueError: print('Invalid index! Try again.') return legal_moves[index] if __name__ == "__main__": from isolation import Board # create an isolation board (by default 7x7) # player1 = RandomPlayer() player2 = GreedyPlayer() # player2 = HumanPlayer() # player1 = MinimaxPlayer(search_depth=3) player1 = AlphaBetaPlayer(search_depth=3) game = 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)) print(game.to_string()) # 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())
from sample_players import (RandomPlayer, open_move_score, improved_score, center_score) from game_agent import (MinimaxPlayer, AlphaBetaPlayer, custom_score, custom_score_2, custom_score_3) from tournament import play_matches,play_round,update from time import time NUM_MATCHES = 5 # number of matches against each opponent TIME_LIMIT = 150 # number of milliseconds before timeout Agent = namedtuple("Agent", ["player", "name"]) NUM_MATCHES = 1 t0 = time() for i in range(NUM_MATCHES): #Agent1 = Agent(AlphaBetaPlayer(score_fn=open_move_score), "MM_Open") Agent1 = Agent(MinimaxPlayer(score_fn=open_move_score), "MM_Open") #Agent2 = Agent(RandomPlayer(), "Random") Agent2 = Agent(AlphaBetaPlayer(score_fn=improved_score), "AB_Improved") game = Board(Agent1.player, Agent2.player) # initialize all games with a random move and response for _ in range(2): move = random.choice(game.get_legal_moves()) game.apply_move(move) #print(game.to_string()) # play all games and tally the results winner, log, termination = game.play() # real thing happens here print(game.to_string()) print("winner:",winner, "opponet failed due to", termination) print("total time: ", time()-t0)
import timeit from isolation import Board from sample_players import (RandomPlayer, open_move_score, improved_score, center_score) from game_agent import (MinimaxPlayer, AlphaBetaPlayer, custom_score, custom_score_2, custom_score_3) # Create the test players player1 = AlphaBetaPlayer(score_fn=improved_score) player2 = RandomPlayer() # Create an isolation board (by default 7x7) game = Board(player1, player2, 8, 8) forfeited_match = [[2, 3], [4, 4], [0, 4], [5, 6], [2, 5], [6, 4], [1, 3], [4, 5], [0, 1], [2, 6], [2, 2]] for move in forfeited_match: game.apply_move(move) # print(game.get_legal_moves()) # print(len(game.get_legal_moves())) score = custom_score_2(game, player1) time_millis = lambda: 1000 * timeit.default_timer() move_start = time_millis() time_left = lambda: 100 - (time_millis() - move_start)
return legal_moves[index] if __name__ == "__main__": from isolation import Board win = False win_count = 0 game_count = 0 while True: # weights = [0.34395085588117713, 0.031064823225326332] # if not win: # weights = np.random.rand(2).tolist() # create an isolation board (by default 7x7) player1 = AlphaBetaPlayer(score_fn=improved_score) player2 = AlphaBetaPlayer(score_fn=custom_score) game = 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. p1_start = choice(game.get_legal_moves()) # p1_start = (5, 2) print(p1_start) game.apply_move(p1_start) p2_start = choice(game.get_legal_moves()) # p2_start = (0, 5) print(p2_start) game.apply_move(p2_start) print(game.to_string())
if __name__ == "__main__": from isolation import Board win = False win_count = 0 game_count = 0 while True: # weights = [0.34395085588117713, 0.031064823225326332] # if not win: # weights = np.random.rand(2).tolist() # create an isolation board (by default 7x7) #player1 = AlphaBetaPlayer(score_fn=improved_score) #player1 = GreedyPlayer() player1 = AlphaBetaPlayer(score_fn=custom_score_2) player2 = AlphaBetaPlayer(score_fn=custom_score_2) game = 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. p1_start = choice(game.get_legal_moves()) # p1_start = (5, 2) #print(p1_start) game.apply_move(p1_start) p2_start = choice(game.get_legal_moves()) # p2_start = (0, 5) #print(p2_start) game.apply_move(p2_start) #print(game.to_string())
if not valid_choice: print('Illegal move! Try again.') except ValueError: print('Invalid index! Try again.') return legal_moves[index] if __name__ == "__main__": from isolation import Board # create an isolation board (by default 7x7) player1 = MinimaxPlayer() player2 = AlphaBetaPlayer() game = 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)) print(game.to_string()) # 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())