Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ----------------------------------------------------------------------
- # Name: adversarial_search
- # Purpose: Homework 6 - Implement adversarial search algorithms
- #
- # Author:
- #
- # ----------------------------------------------------------------------
- """
- Adversarial search algorithms implementation
- Your task for homework 6 is to implement:
- 1. minimax
- 2. alphabeta
- 3. abdl (alpha beta depth limited)
- """
- import random
- import math # You can use math.inf to initialize to infinity
- def rand(game_state):
- """
- Generate a random move.
- :param game_state: GameState object
- :return: a tuple representing the row column of the random move
- """
- done = False
- while not done:
- row = random.randint(0, game_state.size - 1)
- col = random.randint(0, game_state.size - 1)
- if game_state.available(row, col):
- done = True
- return row, col
- def minimax(game_state):
- """
- Find the best move for our AI agent using the minimax algorithm.
- (searching the entire tree from the current game state)
- :param game_state: GameState object
- :return: a tuple representing the row column of the best move
- """
- best_move = ()
- for move in game_state.possible_moves():
- if value(game_state.successor(move, 'AI'), 'AI') == 1:
- return move
- if value(game_state.successor(move, 'AI'), 'AI') == 0:
- best_move = move
- elif not best_move:
- best_move = move
- return best_move
- def value(game_state, agent):
- """
- Calculate the minimax value for any state under the given agent's
- control.
- :param game_state: GameState object - state may be terminal or
- non-terminal
- :param agent: (string) 'user' or 'AI' - AI is max
- :return: (integer) value of that state -1, 0 or 1
- """
- if game_state.is_win('AI'):
- return 1
- elif game_state.is_tie():
- return 0
- elif game_state.is_win('user'):
- return -1
- if agent == 'AI':
- return max_value(game_state)
- else:
- return min_value(game_state)
- def max_value(game_state):
- """
- Calculate the minimax value for a non-terminal state under Max's
- control (AI agent)
- :param game_state: non-terminal GameState object
- :return: (integer) value of that state -1, 0 or 1
- """
- v = max(value(game_state.successor(move, 'user'), 'user') for move in game_state.possible_moves())
- return v
- def min_value(game_state):
- """
- Calculate the minimax value for a non-terminal state under Min's
- control (user)
- :param game_state: non-terminal GameState object
- :return: (integer) value of that state -1, 0 or 1
- """
- v = min(value(game_state.successor(move, 'AI'), 'AI') for move in game_state.possible_moves())
- return v
- def alphabeta(game_state):
- """
- Find the best move for our AI agent using the minimax algorithm
- with alpha beta pruning.
- :param game_state: GameState object
- :return: a tuple representing the row column of the best move
- """
- # Enter your code here and remove the raise statement below
- raise NotImplementedError
- def ab_value(game_state, agent, alpha, beta):
- """
- Calculate the minimax value for any state under the given agent's
- control using alpha beta pruning
- :param game_state: GameState object - state may be terminal or
- non-terminal.
- :param agent: (string) 'user' or 'AI' - AI is max
- :return: (integer) value of that state -1, 0 or 1
- """
- # Enter your code here and remove the pass statement below
- pass
- def abmax_value(game_state, alpha, beta):
- """
- Calculate the minimax value for a non-terminal state under Max's
- control (AI agent) using alpha beta pruning
- :param game_state: non-terminal GameState object
- :return: (integer) value of that state -1, 0 or 1
- """
- # Enter your code here and remove the pass statement below
- pass
- def abmin_value(game_state, alpha, beta):
- """
- Calculate the minimax value for a non-terminal state under Min's
- control (user) using alpha beta pruning
- :param game_state: non-terminal GameState object
- :return: (integer) value of that state -1, 0 or 1
- """
- # Enter your code here and remove the pass statement below
- pass
- def abdl(game_state, depth):
- """
- Find the best move for our AI agent by limiting the alpha beta
- search the given depth and using the evaluation function
- game_state.eval()
- :param game_state: GameState object
- :return: a tuple representing the row column of the best move
- """
- # Enter your code here and remove the raise statement below
- raise NotImplementedError
- def abdl_value(game_state, agent, alpha, beta, depth):
- """
- Calculate the utility for any state under the given agent's control
- using depth limited alpha beta pruning and the evaluation
- function game_state.eval()
- :param game_state: GameState object - state may be terminal or
- non-terminal
- :param agent: (string) 'user' or 'AI' - AI is max
- :return: (integer) utility of that state
- """
- # Enter your code here and remove the pass statement below
- pass
- def abdlmax_value(game_state, alpha, beta, depth):
- """
- Calculate the utility for a non-terminal state under Max's control
- using depth limited alpha beta pruning and the evaluation
- function game_state.eval()
- :param game_state: non-terminal GameState object
- :return: (integer) utility (evaluation function) of that state
- """
- # Enter your code here and remove the pass statement below
- pass
- def abdlmin_value(game_state, alpha, beta, depth):
- """
- Calculate the utility for a non-terminal state under Min's control
- using depth limited alpha beta pruning and the evaluation
- function game_state.eval()
- :param game_state: non-terminal GameState object
- :return: (integer) utility (evaluation function) of that state
- """
- # Enter your code here and remove the pass statement below
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement