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 attack(gameState, playerId):
    # Goal: Chase around the opponent, always minimizing the distance between the player and the opponent
    # Use ecl

    from isolation import DebugState  #steal the dbug functions to make math easier

    #Ideally these would be imported somehow
    WIDTH = 11
    HEIGHT = 9

    player_loc = gameState.locs[playerId]
    opp_loc = gameState.locs[1 - playerId]

    #Get locations
    player_xy = DebugState.ind2xy(player_loc)
    opp_xy = DebugState.ind2xy(opp_loc)

    #Calculate the distance
    distance = (player_xy[0] - opp_xy[0])**2 + (player_xy[1] - opp_xy[1])**2

    #Compute a normalization factor
    max_dist = (WIDTH)**2 + (HEIGHT)**2

    #For attack we want to maximize score when distance is minimized
    return ((max_dist - distance) / max_dist
            )  # returns 1 when we are as close as possible to opponent
Exemple #3
0
    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 distance(self, state):
     if None in state.locs: return 0
     own_xy = DebugState.ind2xy(state.locs[self.player_id])
     opp_xy = DebugState.ind2xy(state.locs[1 - self.player_id])
     manhattan_distance = abs(own_xy[0] - opp_xy[0]) + abs(own_xy[1] -
                                                           opp_xy[1])
     euclidean_distance = math.sqrt((own_xy[0] - opp_xy[0])**2 +
                                    (own_xy[1] - opp_xy[1])**2)
     return euclidean_distance
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)
Exemple #6
0
 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 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 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 centeredness(gameState, playerId):
    # Goal: Maximize self-centeredness while preferring opponents at the edge
    # Reasoning: In the middle of the board you are more flexible then at the edges
    from isolation import DebugState  #steal the dbug functions to make math easier

    player_loc = gameState.locs[playerId]
    opp_loc = gameState.locs[1 - playerId]

    #Process player location
    player_xy = DebugState.ind2xy(player_loc)
    x_centeredness = 5 - abs(5 - player_xy[0])
    y_centeredness = 4 - abs(4 - player_xy[1])
    player_centeredness = x_centeredness + y_centeredness

    #Process opponent location
    opp_xy = DebugState.ind2xy(opp_loc)
    opp_x_cent = 5 - abs(5 - opp_xy[0])
    opp_y_cent = 4 - abs(4 - opp_xy[1])
    opp_centeredness = opp_x_cent + opp_y_cent

    return player_centeredness - opp_centeredness
    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
Exemple #12
0
    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 coward(gameState, playerId):
    from isolation import DebugState  #steal the dbug functions to make math easier

    #Ideally these would be imported somehow
    WIDTH = 11
    HEIGHT = 9

    player_loc = gameState.locs[playerId]
    opp_loc = gameState.locs[1 - playerId]

    #Get locations
    player_xy = DebugState.ind2xy(player_loc)
    opp_xy = DebugState.ind2xy(opp_loc)

    #Calculate the distance
    distance = (player_xy[0] - opp_xy[0])**2 + (player_xy[1] - opp_xy[1])**2

    #Compute a normalization factor
    max_dist = (WIDTH)**2 + (HEIGHT)**2

    #For attack we want to maximize score when distance is minimized
    return ((distance) / max_dist
            )  # returns 1 when we are as far as possible to opponent
Exemple #14
0
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 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'])
Exemple #19
0
    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)
        methods = ["RANDOM", "MINIMAX", "ALPHABETA_Iterative", "MCTS", "NEGASCOUT", "PVS", "PVS_Iterative", "PVS_ZWS"]
        method = "MCTS"

        printDebugMsg(DebugState(state.board))

        ply_count_threshold = 2
        # If fewer than ply_count_threshold applied on board, then choose random move.
        if state.ply_count < ply_count_threshold:
            if state.actions():
                self.queue.put(random.choice(state.actions()))
            else:
                self.queue.put(None)
        else:
            if method == "RANDOM":
                self.queue.put(random.choice(state.actions()))
            elif method == "MINIMAX":
                # Code from sample_players.py file.
                # return the optimal minimax move at a fixed search depth of 3 plies
                self.queue.put(self.minimax(state, depth=3))
            elif method == "ALPHABETA_Iterative":  # Win ~= 62.5%
                # Alpha-Beta with iterative deepening
                depth_limit = 3
                best_move = None
                for depth in range(1, depth_limit + 1):
                    best_move = self.alpha_beta_search(state, depth)
                printDebugMsg("final best_move = {}".format(best_move))
                # print("Alpha Beta Node Count = {}".format(AB_Baseline_NodeCount))
                self.queue.put(best_move)
            elif method == "MCTS":  # Win ~=
                # Use Monte Carlo Tree Search
                mcts = MCTS_Search(computational_budget=100)
                # mcts = MCTS_Search()
                action = mcts.uctSearch(state)
                # Handle case where no action was returned.
                if action:
                    self.queue.put(action)
                elif state.actions():
                    self.queue.put(random.choice(state.actions()))
                else:
                    self.queue.put(None)
            elif method == "NEGASCOUT":  # Win ~= 18%
                # Use NegaScout
                self.queue.put(self.negaScout(state, depth=5))
            elif method == "PVS":  # Win ~= 11.5%
                # Use Principal Variation Search
                self.queue.put(self.principal_variation_search(state, depth=3))
            elif method == "PVS_Iterative":  # Win ~=
                # Use principal variation search with iterative deepening.
                depth_limit = 5
                best_move = None
                for depth in range(1, depth_limit + 1):
                    best_move = self.principal_variation_search(state, depth)
                self.queue.put(best_move)
            elif method == "PVS_ZWS":  # Win ~=
                # Use Principal Variation Search
                self.queue.put(self.principal_variation_search_zws(state, depth=5))
            else:
                import sys
                sys.exit("Unknown method")
        printDebugMsg("self.queue = {}".format(self.queue))
Exemple #20
0
 def show_board(self, state):
     dbstate = DebugState.from_state(state)
     self.feedback(dbstate) 
Exemple #21
0
def debug_print_state(state):
    dbstate = DebugState.from_state(state)
    print(dbstate)
			def custom_heuristic(state):
				x1, y1 = DebugState.ind2xy(state.locs[self.player_id])
				#x2, y2 = DebugState.ind2xy(state.locs[1-self.player_id])
				x2, y2 = DebugState.ind2xy(57)
				return -((x2-x1)**2 + (y2-y1)**2)**(1/2)
    def center_field_heuristic(position, game_state):
        xy_position = DebugState.from_state(game_state).ind2xy(position)

        return CustomPlayer.xy_distance(xy_position, _CENTER)
Exemple #24
0
from isolation import Isolation, DebugState

bd = Isolation()
#board = board.result(67)
db = DebugState(bd)
print(db)
Exemple #25
0
import pickle
import queue
from isolation import Isolation, DebugState

# Loading the opening book form a pickle file
with open("data.pickle", "rb") as f:
    book = pickle.load(f)

# Creating the initial state (a blank board)
initial_statate = Isolation()

# Displaying the initial move taken by the first player
first_move_state = initial_statate.result(book[initial_statate])
print('OPENING MOVE FOR PLAYER 1')
print(DebugState().from_state(first_move_state))

# Displaying the reply taken by the second player
response_move_state = first_move_state.result(book[first_move_state])
print('BEST REPLY BY PLAYER 2')
print(DebugState().from_state(response_move_state))
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")
Exemple #27
0
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)