diff --git a/multiAgents.py b/multiAgents.py index 94c2997..9e2c709 100644 --- a/multiAgents.py +++ b/multiAgents.py @@ -4,7 +4,7 @@ # educational purposes provided that (1) you do not distribute or publish # solutions, (2) you retain this notice, and (3) you provide clear # attribution to UC Berkeley, including a link to http://ai.berkeley.edu. -# +# # Attribution Information: The Pacman AI projects were developed at UC Berkeley. # The core projects and autograders were primarily created by John DeNero # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu). @@ -14,10 +14,12 @@ from util import manhattanDistance from game import Directions -import random, util +import random +import util from game import Agent + class ReflexAgent(Agent): """ A reflex agent chooses an action at each choice point by examining @@ -28,7 +30,6 @@ class ReflexAgent(Agent): headers. """ - def getAction(self, gameState): """ You do not need to change this method, but you're welcome to. @@ -42,10 +43,13 @@ class ReflexAgent(Agent): legalMoves = gameState.getLegalActions() # Choose one of the best actions - scores = [self.evaluationFunction(gameState, action) for action in legalMoves] + scores = [self.evaluationFunction( + gameState, action) for action in legalMoves] bestScore = max(scores) - bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore] - chosenIndex = random.choice(bestIndices) # Pick randomly among the best + bestIndices = [index for index in range( + len(scores)) if scores[index] == bestScore] + # Pick randomly among the best + chosenIndex = random.choice(bestIndices) "Add more of your code here if you want to" @@ -71,14 +75,32 @@ class ReflexAgent(Agent): newPos = successorGameState.getPacmanPosition() newFood = successorGameState.getFood() newGhostStates = successorGameState.getGhostStates() - newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates] + newScaredTimes = [ + ghostState.scaredTimer for ghostState in newGhostStates] print(newPos) print(newFood) print(newGhostStates) print(newScaredTimes) + # make is so that if it is closer to a ghost then reduce the score + # increase if closer to food + + # only one ghost in classic map + ghost_pos = newGhostStates[0].getPosition() + nearest_ghost_dist = manhattanDistance(ghost_pos, newPos) + + # nearest food + all_food_pos = newFood.asList() + score = successorGameState.getScore() + + if len(all_food_pos) and nearest_ghost_dist: + nearest_food_dist = min([manhattanDistance( + foodPos, newPos) for foodPos in all_food_pos]) + score = score - (5 / nearest_ghost_dist) + (10 / nearest_food_dist) + "*** YOUR CODE HERE ***" - return successorGameState.getScore() + return score + def scoreEvaluationFunction(currentGameState): """ @@ -90,6 +112,7 @@ def scoreEvaluationFunction(currentGameState): """ return currentGameState.getScore() + class MultiAgentSearchAgent(Agent): """ This class provides some common elements to all of your @@ -105,11 +128,12 @@ class MultiAgentSearchAgent(Agent): is another abstract class. """ - def __init__(self, evalFn = 'scoreEvaluationFunction', depth = '2'): - self.index = 0 # Pacman is always agent index 0 + def __init__(self, evalFn='scoreEvaluationFunction', depth='2'): + self.index = 0 # Pacman is always agent index 0 self.evaluationFunction = util.lookup(evalFn, globals()) self.depth = int(depth) + class MinimaxAgent(MultiAgentSearchAgent): """ Your minimax agent (question 2) @@ -141,6 +165,7 @@ class MinimaxAgent(MultiAgentSearchAgent): "*** YOUR CODE HERE ***" util.raiseNotDefined() + class AlphaBetaAgent(MultiAgentSearchAgent): """ Your minimax agent with alpha-beta pruning (question 3) @@ -153,6 +178,7 @@ class AlphaBetaAgent(MultiAgentSearchAgent): "*** YOUR CODE HERE ***" util.raiseNotDefined() + class ExpectimaxAgent(MultiAgentSearchAgent): """ Your expectimax agent (question 4) @@ -168,6 +194,7 @@ class ExpectimaxAgent(MultiAgentSearchAgent): "*** YOUR CODE HERE ***" util.raiseNotDefined() + def betterEvaluationFunction(currentGameState): """ Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable @@ -178,5 +205,6 @@ def betterEvaluationFunction(currentGameState): "*** YOUR CODE HERE ***" util.raiseNotDefined() + # Abbreviation better = betterEvaluationFunction