simple reflex agent working

This commit is contained in:
Arjun Patel
2019-02-21 19:23:57 -08:00
parent 5157e6043d
commit fb964d7478
+38 -10
View File
@@ -4,7 +4,7 @@
# educational purposes provided that (1) you do not distribute or publish # educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear # solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu. # attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
# #
# Attribution Information: The Pacman AI projects were developed at UC Berkeley. # Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero # The core projects and autograders were primarily created by John DeNero
# ([email protected]) and Dan Klein ([email protected]). # ([email protected]) and Dan Klein ([email protected]).
@@ -14,10 +14,12 @@
from util import manhattanDistance from util import manhattanDistance
from game import Directions from game import Directions
import random, util import random
import util
from game import Agent from game import Agent
class ReflexAgent(Agent): class ReflexAgent(Agent):
""" """
A reflex agent chooses an action at each choice point by examining A reflex agent chooses an action at each choice point by examining
@@ -28,7 +30,6 @@ class ReflexAgent(Agent):
headers. headers.
""" """
def getAction(self, gameState): def getAction(self, gameState):
""" """
You do not need to change this method, but you're welcome to. You do not need to change this method, but you're welcome to.
@@ -42,10 +43,13 @@ class ReflexAgent(Agent):
legalMoves = gameState.getLegalActions() legalMoves = gameState.getLegalActions()
# Choose one of the best actions # 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) bestScore = max(scores)
bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore] bestIndices = [index for index in range(
chosenIndex = random.choice(bestIndices) # Pick randomly among the best 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" "Add more of your code here if you want to"
@@ -71,14 +75,32 @@ class ReflexAgent(Agent):
newPos = successorGameState.getPacmanPosition() newPos = successorGameState.getPacmanPosition()
newFood = successorGameState.getFood() newFood = successorGameState.getFood()
newGhostStates = successorGameState.getGhostStates() newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates] newScaredTimes = [
ghostState.scaredTimer for ghostState in newGhostStates]
print(newPos) print(newPos)
print(newFood) print(newFood)
print(newGhostStates) print(newGhostStates)
print(newScaredTimes) 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 ***" "*** YOUR CODE HERE ***"
return successorGameState.getScore() return score
def scoreEvaluationFunction(currentGameState): def scoreEvaluationFunction(currentGameState):
""" """
@@ -90,6 +112,7 @@ def scoreEvaluationFunction(currentGameState):
""" """
return currentGameState.getScore() return currentGameState.getScore()
class MultiAgentSearchAgent(Agent): class MultiAgentSearchAgent(Agent):
""" """
This class provides some common elements to all of your This class provides some common elements to all of your
@@ -105,11 +128,12 @@ class MultiAgentSearchAgent(Agent):
is another abstract class. is another abstract class.
""" """
def __init__(self, evalFn = 'scoreEvaluationFunction', depth = '2'): def __init__(self, evalFn='scoreEvaluationFunction', depth='2'):
self.index = 0 # Pacman is always agent index 0 self.index = 0 # Pacman is always agent index 0
self.evaluationFunction = util.lookup(evalFn, globals()) self.evaluationFunction = util.lookup(evalFn, globals())
self.depth = int(depth) self.depth = int(depth)
class MinimaxAgent(MultiAgentSearchAgent): class MinimaxAgent(MultiAgentSearchAgent):
""" """
Your minimax agent (question 2) Your minimax agent (question 2)
@@ -141,6 +165,7 @@ class MinimaxAgent(MultiAgentSearchAgent):
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
util.raiseNotDefined() util.raiseNotDefined()
class AlphaBetaAgent(MultiAgentSearchAgent): class AlphaBetaAgent(MultiAgentSearchAgent):
""" """
Your minimax agent with alpha-beta pruning (question 3) Your minimax agent with alpha-beta pruning (question 3)
@@ -153,6 +178,7 @@ class AlphaBetaAgent(MultiAgentSearchAgent):
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
util.raiseNotDefined() util.raiseNotDefined()
class ExpectimaxAgent(MultiAgentSearchAgent): class ExpectimaxAgent(MultiAgentSearchAgent):
""" """
Your expectimax agent (question 4) Your expectimax agent (question 4)
@@ -168,6 +194,7 @@ class ExpectimaxAgent(MultiAgentSearchAgent):
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
util.raiseNotDefined() util.raiseNotDefined()
def betterEvaluationFunction(currentGameState): def betterEvaluationFunction(currentGameState):
""" """
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
@@ -178,5 +205,6 @@ def betterEvaluationFunction(currentGameState):
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
util.raiseNotDefined() util.raiseNotDefined()
# Abbreviation # Abbreviation
better = betterEvaluationFunction better = betterEvaluationFunction