def test_custom_player(self): """ CustomPlayer successfully completes a game against itself """ agents = (Agent(CustomPlayer, "Player 1"), Agent(CustomPlayer, "Player 2")) initial_state = Isolation() winner, game_history, _ = play( (agents, initial_state, self.time_limit, 0)) state = initial_state moves = deque(game_history) while moves: state = state.result(moves.popleft()) if not state.terminal_test(): print( "Your agent with id:{state.player()} was not able to make a move in state:" ) print(state.player()) debug_state = DebugState.from_state(state) print(debug_state) raise Exception("Your agent did not play until a terminal state.") debug_state = DebugState.from_state(state) print(debug_state) print("Winner is: " + str(winner) + "!")
def get_action(self, state): """ Employ an adversarial search technique to choose an action available in the current state calls self.queue.put(ACTION) at least This method must call self.queue.put(ACTION) at least once, and may call it as many times as you want; the caller will be responsible for cutting off the function after the search time limit has expired. See RandomPlayer and GreedyPlayer in sample_players for more examples. ********************************************************************** NOTE: - The caller is responsible for cutting off search, so calling get_action() from your own code will create an infinite loop! Refer to (and use!) the Isolation.play() function to run games. ********************************************************************** """ # TODO: Replace the example implementation below with your own search # method by combining techniques from lecture # # EXAMPLE: choose a random move without any search--this function MUST # call self.queue.put(ACTION) at least once before time expires # (the timer is automatically managed for you) #print(self.load_q()) choice = self.decision(state) debug_state = DebugState() debug_board = debug_state.from_state(state) #sys.stdout.write( str(debug_board)) #sys.stdout.flush() #print(debug_board) self.queue.put(choice)
def max_v(self, state, depth, alpha, beta, player): if state.terminal_test(): #print(state.utility(player), "aa") return state.utility(player) elif depth == 0: #print(len(state.liberties(state.locs[player])),"da") i = 0 if player == 0: i = 1 p1 = state.liberties(state.locs[player]) p2 = state.liberties(state.locs[i]) print(state.locs) print(self.walldist(state.locs[0]), "wallsdist") print('In get_action(), state received:') debug_board = DebugState.from_state(state) print(debug_board) nex1 = sum(len(state.liberties(A)) for A in p1) nex2 = sum(len(state.liberties(B)) for B in p2) if nex1 == 0: return float("-inf") elif nex2 == 0: return float("inf") return nex1**2 - nex2 #return len(p1)**2 - len(p2) v = float("-inf") for a in state.actions(): v = max( v, self.min_v(state.result(a), depth - 1, alpha, beta, player)) if v >= beta: #print(v,"WOOOOW") return v alpha = max(alpha, v) #print(v,"ja") return v
def verbose_callback(game_state, action, active_player, active_idx, match_id, time_taken): if game_state.ply_count % 2 == 0 or game_state.terminal_test(): # print every other move, plus endgame summary = "\nmatch: {} | move: {} | {:.2f}s | {}({}) => {}".format( match_id, game_state.ply_count, time_taken, active_player.__class__.__name__, active_idx, DebugState.ind2xy(action) ) board = str(DebugState.from_state(game_state)) print(summary); logger.info(summary) print(board); logger.info(board)
def get_action(self, state): TIME_LIMIT = 150 # milliseconds if state.terminal_test() or state.ply_count < 2: debug_board = DebugState.from_state(state) print(debug_board) self.queue.put(random.choice(state.actions())) else: debug_board = DebugState.from_state(state) print(debug_board) minimaxAB = MinimaxAlphaBetaSearch(state, depth=6) res = minimaxAB.minimax_alphaBeta(TIME_LIMIT) if res: self.queue.put(res) elif state.actions(): self.queue.put(random.choice(state.actions())) else: self.queue.put(None)
def build_tree(state, book, depth=4): if (depth == 0): debug_board = DebugState.from_state(state) print(debug_board) #using Open Move Score instead of raw wins if depth <= 0 or state.terminal_test(): return -simulate(state) action = alpha_beta_search(state, state.player(), 4) reward = build_tree(state.result(action), book, depth - 1) book[state.board][action] += reward return -reward
def get_action(self, state): TIME_LIMIT = 150 # milliseconds if state.terminal_test() or state.ply_count < 2: debug_board = DebugState.from_state(state) print(debug_board) self.queue.put(random.choice(state.actions())) else: debug_board = DebugState.from_state(state) print(debug_board) mcts = MonteCarloTreeSearch(state) res = mcts.best_action(TIME_LIMIT) if res: self.queue.put(res) elif state.actions(): self.queue.put(random.choice(state.actions())) else: self.queue.put(None)
def maximize_distance_to_opponent_heuristic(self, game_state): own_loc = game_state.locs[self.player_id] opp_loc = game_state.locs[1 - self.player_id] debug_state = DebugState.from_state(game_state) own_xy_position = debug_state.ind2xy(own_loc) opp_xy_position = debug_state.ind2xy(opp_loc) distance = CustomPlayer.xy_distance(own_xy_position, opp_xy_position) # max distance is 18, so calculate a score which is positive for bigger distance and negative for lower one return distance - 9
def get_action(self, state): """ Employ an adversarial search technique to choose an action available in the current state calls self.queue.put(ACTION) at least This method must call self.queue.put(ACTION) at least once, and may call it as many times as you want; the caller will be responsible for cutting off the function after the search time limit has expired. See RandomPlayer and GreedyPlayer in sample_players for more examples. ********************************************************************** NOTE: - The caller is responsible for cutting off search, so calling get_action() from your own code will create an infinite loop! Refer to (and use!) the Isolation.play() function to run games. ********************************************************************** """ # TODO: Replace the example implementation below with your own search # method by combining techniques from lecture # # EXAMPLE: choose a random move without any search--this function MUST # call self.queue.put(ACTION) at least once before time expires # (the timer is automatically managed for you) import random if DEBUG_MODE: import time from isolation import DebugState print("\nTurn " + str(state.ply_count) + " - Player " + str(self.player_id + 1) + " goes:") print("Available actions: " + ','.join(map(str, state.actions()))) debug_board = DebugState.from_state(state) print(debug_board) start = time.process_time() if state.ply_count <= 4: if self.data is not None and state in self.data and self.data[state] in state.actions(): action = self.data[state] if DEBUG_MODE: print("Pick action from opening book: " + str(action)) self.queue.put(action) else: self.queue.put(random.choice(state.actions())) else: depth_limit = 100 for depth in range(1, depth_limit + 1): action = self.alpha_beta_search(state, depth) if action is None: action = random.choice(state.actions()) if DEBUG_MODE: print("Calculate best action from minimax (depth=" + str(depth) + ") in " + str(time.process_time() - start) + " seconds: " + str(action)) self.queue.put(action)
def _for_print(matches, results): for winner_agent, game_history, match_id in results: if match_id < 6: match = matches[match_id] state_opening_moves = Isolation().result(game_history[0]).result( game_history[1]) print("Match Id: {}.".format(match_id), end=' ') print("Players One: {}.".format(match.players[0].name), end=' ') print("Players Two: {}.".format(match.players[1].name)) print("State after opening moves: {}".format(state_opening_moves)) print("Winner: {}".format(winner_agent.name)) print(DebugState.from_state(state_opening_moves)) print() else: break
def get_action(self, state): """ Employ an adversarial search technique to choose an action available in the current state calls self.queue.put(ACTION) at least This method must call self.queue.put(ACTION) at least once, and may call it as many times as you want; the caller will be responsible for cutting off the function after the search time limit has expired. See RandomPlayer and GreedyPlayer in sample_players for more examples. ********************************************************************** NOTE: - The caller is responsible for cutting off search, so calling get_action() from your own code will create an infinite loop! Refer to (and use!) the Isolation.play() function to run games. ********************************************************************** """ # TODO: Replace the example implementation below with your own search # method by combining techniques from lecture # # EXAMPLE: choose a random move without any search--this function MUST # call self.queue.put(ACTION) at least once before time expires # (the timer is automatically managed for you) # # For Debugging print('In get_action(), state received:') debug_board = DebugState.from_state(state) print(debug_board) # With iterative deepening # for depth in range(1, self.depth_limit + 1): # self.queue.put(self.minimax(state, depth)) # self.start_time = time.time() # With iterative deepening & Alpha-Beta Pruning for depth in range(1, self.depth_limit + 1): # if self.timertest(): # print("\t", depth) # writeToCsv( # 'Player Weighted Self' + # "," + str(depth)+ # '\n') if len(state.actions()) > 0: self.queue.put(self.alpha_beta_search(state, depth))
def get_action(self, state): """ Employ an adversarial search technique to choose an action available in the current state calls self.queue.put(ACTION) at least This method must call self.queue.put(ACTION) at least once, and may call it as many times as you want; the caller will be responsible for cutting off the function after the search time limit has expired. See RandomPlayer and GreedyPlayer in sample_players for more examples. ********************************************************************** NOTE: - The caller is responsible for cutting off search, so calling get_action() from your own code will create an infinite loop! Refer to (and use!) the Isolation.play() function to run games. ********************************************************************** """ # TODO: Replace the example implementation below with your own search # method by combining techniques from lecture # # EXAMPLE: choose a random move without any search--this function MUST # call self.queue.put(ACTION) at least once before time expires # (the timer is automatically managed for you) #import random #self.queue.put(random.choice(state.actions())) # randomly select a move as player 1 or 2 on an empty board, otherwise # return the optimal minimax move at a fixed search depth of 3 plies print('In get_action(), state received:') debug_board = DebugState.from_state(state) print(debug_board) if state.ply_count < 2: self.queue.put(random.choice(state.actions())) else: #self.queue.put(self.minimax(state, depth=3)) self.queue.put(self.monte_carlo_tree_search(state))
def biggest_quadrant_heuristic(position, game_state): debug_state = DebugState.from_state(game_state) xy_position = debug_state.ind2xy(position) q1_xy_modifiers = CustomPlayer.count_modifiers_for_q1(xy_position) q1_empty_fields = CustomPlayer.count_empty_fields( position, game_state, q1_xy_modifiers) q2_xy_modifiers = CustomPlayer.count_modifiers_for_q2(xy_position) q2_empty_fields = CustomPlayer.count_empty_fields( position, game_state, q2_xy_modifiers) q3_xy_modifiers = CustomPlayer.count_modifiers_for_q3(xy_position) q3_empty_fields = CustomPlayer.count_empty_fields( position, game_state, q3_xy_modifiers) q4_xy_modifiers = CustomPlayer.count_modifiers_for_q4(xy_position) q4_empty_fields = CustomPlayer.count_empty_fields( position, game_state, q4_xy_modifiers) # max empty fields is nearly 100, so calculate a score which is positive for bigger distance and negative for lower one return max(q1_empty_fields, q2_empty_fields, q3_empty_fields, q4_empty_fields) - 40
def show_board(self, state): dbstate = DebugState.from_state(state) self.feedback(dbstate)
def center_field_heuristic(position, game_state): xy_position = DebugState.from_state(game_state).ind2xy(position) return CustomPlayer.xy_distance(xy_position, _CENTER)
def debug_print_state(state): dbstate = DebugState.from_state(state) print(dbstate)
def get_num_blank_spaces(self, state): """Return number of locations that are still available on the board. """ debug_board = DebugState.from_state(state) return sum([1 for s in debug_board.bitboard_string if s == '1'])
from collections import defaultdict, Counter from isolation import DebugState import pickle f = open("data.pickle", 'rb') book = pickle.load(f) from isolation import Isolation state = Isolation() if state in book: print("empty state is in data.pickle") # first move action = book[state] print("The best action for an empty board is ", action) state = state.result(action) debug_board = DebugState.from_state(state) print("Board after first move") print(debug_board) # best response action = book[state] print("The best response for it from the opponent is ", action) state = state.result(action) debug_board = DebugState.from_state(state) print("Board after the response") print(debug_board) else: print("empty state is NOT found in data.pickle")
from isolation import Isolation, Agent, DebugState from my_custom_player import CustomPlayer import train # main code if __name__ == '__main__': board = DebugState() debug_board = board.from_state(board) test_agent = TEST_AGENTS['MINIMAX']¬ custom_agent = Agent(CustomPlayer, "Custom Agent")¬ wins, num_games = play_matches(custom_agent, test_agent, args) print(debug_board)