Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.06 KB | None | 0 0
  1. # ----------------------------------------------------------------------
  2. # Name:     adversarial_search
  3. # Purpose:  Homework 6 - Implement adversarial search algorithms
  4. #
  5. # Author:
  6. #
  7. # ----------------------------------------------------------------------
  8. """
  9. Adversarial search algorithms implementation
  10.  
  11. Your task for homework 6 is to implement:
  12. 1.  minimax
  13. 2.  alphabeta
  14. 3.  abdl (alpha beta depth limited)
  15. """
  16. import random
  17. import math  # You can use math.inf to initialize to infinity
  18.  
  19.  
  20. def rand(game_state):
  21.     """
  22.    Generate a random move.
  23.    :param game_state: GameState object
  24.    :return:  a tuple representing the row column of the random move
  25.    """
  26.     done = False
  27.     while not done:
  28.         row = random.randint(0, game_state.size - 1)
  29.         col = random.randint(0, game_state.size - 1)
  30.         if game_state.available(row, col):
  31.             done = True
  32.     return row, col
  33.  
  34.  
  35. def minimax(game_state):
  36.     """
  37.    Find the best move for our AI agent using the minimax algorithm.
  38.    (searching the entire tree from the current game state)
  39.    :param game_state: GameState object
  40.    :return:  a tuple representing the row column of the best move
  41.    """
  42.     best_move = ()
  43.     for move in game_state.possible_moves():
  44.         if value(game_state.successor(move, 'AI'), 'AI') == 1:
  45.             return move
  46.         if value(game_state.successor(move, 'AI'), 'AI') == 0:
  47.             best_move = move
  48.         elif not best_move:
  49.             best_move = move
  50.     return best_move
  51.  
  52.  
  53. def value(game_state, agent):
  54.     """
  55.    Calculate the minimax value for any state under the given agent's
  56.    control.
  57.    :param game_state: GameState object - state may be terminal or
  58.    non-terminal
  59.    :param agent: (string) 'user' or 'AI' - AI is max
  60.    :return: (integer) value of that state -1, 0 or 1
  61.    """
  62.     if game_state.is_win('AI'):
  63.         return 1
  64.     elif game_state.is_tie():
  65.         return 0
  66.     elif game_state.is_win('user'):
  67.         return -1
  68.  
  69.     if agent == 'AI':
  70.         return max_value(game_state)
  71.     else:
  72.         return min_value(game_state)
  73.  
  74.  
  75. def max_value(game_state):
  76.     """
  77.    Calculate the minimax value for a non-terminal state under Max's
  78.    control (AI agent)
  79.    :param game_state: non-terminal GameState object
  80.    :return: (integer) value of that state -1, 0 or 1
  81.    """
  82.  
  83.     v = max(value(game_state.successor(move, 'user'), 'user') for move in game_state.possible_moves())
  84.     return v
  85.  
  86.  
  87. def min_value(game_state):
  88.     """
  89.    Calculate the minimax value for a non-terminal state under Min's
  90.    control (user)
  91.    :param game_state: non-terminal GameState object
  92.    :return: (integer) value of that state -1, 0 or 1
  93.    """
  94.     v = min(value(game_state.successor(move, 'AI'), 'AI') for move in game_state.possible_moves())
  95.     return v
  96.  
  97.  
  98. def alphabeta(game_state):
  99.     """
  100.    Find the best move for our AI agent using the minimax algorithm
  101.    with alpha beta pruning.
  102.    :param game_state: GameState object
  103.    :return:  a tuple representing the row column of the best move
  104.    """
  105.     # Enter your code here and remove the raise statement below
  106.     raise NotImplementedError
  107.  
  108.  
  109. def ab_value(game_state, agent, alpha, beta):
  110.     """
  111.    Calculate the minimax value for any state under the given agent's
  112.    control using alpha beta pruning
  113.    :param game_state: GameState object - state may be terminal or
  114.    non-terminal.
  115.    :param agent: (string) 'user' or 'AI' - AI is max
  116.    :return: (integer) value of that state -1, 0 or 1
  117.    """
  118.     # Enter your code here and remove the pass statement below
  119.     pass
  120.  
  121.  
  122. def abmax_value(game_state, alpha, beta):
  123.     """
  124.    Calculate the minimax value for a non-terminal state under Max's
  125.    control (AI agent) using alpha beta pruning
  126.    :param game_state: non-terminal GameState object
  127.    :return: (integer) value of that state -1, 0 or 1
  128.    """
  129.     # Enter your code here and remove the pass statement below
  130.     pass
  131.  
  132.  
  133. def abmin_value(game_state, alpha, beta):
  134.     """
  135.    Calculate the minimax value for a non-terminal state under Min's
  136.    control (user) using alpha beta pruning
  137.    :param game_state: non-terminal GameState object
  138.    :return: (integer) value of that state -1, 0 or 1
  139.    """
  140.     # Enter your code here and remove the pass statement below
  141.     pass
  142.  
  143.  
  144. def abdl(game_state, depth):
  145.     """
  146.    Find the best move for our AI agent by limiting the alpha beta
  147.    search the given depth and using the evaluation function
  148.    game_state.eval()
  149.    :param game_state: GameState object
  150.    :return:  a tuple representing the row column of the best move
  151.    """
  152.     # Enter your code here and remove the raise statement below
  153.     raise NotImplementedError
  154.  
  155.  
  156. def abdl_value(game_state, agent, alpha, beta, depth):
  157.     """
  158.    Calculate the utility for any state under the given agent's control
  159.    using depth limited alpha beta pruning and the evaluation
  160.    function game_state.eval()
  161.    :param game_state: GameState object - state may be terminal or
  162.    non-terminal
  163.    :param agent: (string) 'user' or 'AI' - AI is max
  164.    :return: (integer) utility of that state
  165.    """
  166.     # Enter your code here and remove the pass statement below
  167.     pass
  168.  
  169.  
  170. def abdlmax_value(game_state, alpha, beta, depth):
  171.     """
  172.    Calculate the utility for a non-terminal state under Max's control
  173.    using depth limited alpha beta pruning and the evaluation
  174.    function game_state.eval()
  175.    :param game_state: non-terminal GameState object
  176.    :return: (integer) utility (evaluation function) of that state
  177.    """
  178.     # Enter your code here and remove the pass statement below
  179.     pass
  180.  
  181.  
  182. def abdlmin_value(game_state, alpha, beta, depth):
  183.     """
  184.    Calculate the utility for a non-terminal state under Min's control
  185.    using depth limited alpha beta pruning and the evaluation
  186.    function game_state.eval()
  187.    :param game_state: non-terminal GameState object
  188.    :return: (integer) utility (evaluation function) of that state
  189.    """
  190.     # Enter your code here and remove the pass statement below
  191.     pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement