Finish problem 2.
This commit is contained in:
@@ -223,8 +223,27 @@ class ExpectimaxAgent(MultiAgentSearchAgent):
|
||||
All ghosts should be modeled as choosing uniformly at random from their
|
||||
legal moves.
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
util.raiseNotDefined()
|
||||
numAgents = gameState.getNumAgents()
|
||||
totalDepth = self.depth * numAgents
|
||||
|
||||
def value(depth, state):
|
||||
agentIndex = depth % numAgents
|
||||
actions = state.getLegalActions(agentIndex)
|
||||
if not actions or depth == totalDepth:
|
||||
return (self.evaluationFunction(state), "terminal")
|
||||
successorStates = [state.generateSuccessor(agentIndex, action) for action in actions]
|
||||
successorValueActionPairs = [(value(depth + 1, state)[0], action)
|
||||
for action, state in zip(actions, successorStates)]
|
||||
# Pacman (agentIndex=0) maximizes, ghosts minimize.
|
||||
if agentIndex == 0:
|
||||
return max(successorValueActionPairs)
|
||||
else:
|
||||
values = [va[0] for va in successorValueActionPairs]
|
||||
average = sum(values) / float(len(values))
|
||||
return (average, "expected")
|
||||
|
||||
# [0] is the best value, [1] is the best action
|
||||
return value(0, gameState)[1]
|
||||
|
||||
|
||||
def betterEvaluationFunction(currentGameState):
|
||||
@@ -234,9 +253,51 @@ def betterEvaluationFunction(currentGameState):
|
||||
|
||||
DESCRIPTION: <write something here so we know what you did>
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
util.raiseNotDefined()
|
||||
|
||||
from searchAgents import mazeDistance
|
||||
|
||||
state = currentGameState
|
||||
pos = state.getPacmanPosition()
|
||||
|
||||
# foodDists = [mazeDistance(pos, foodPos, state)
|
||||
# for foodPos in state.getFood().asList()]
|
||||
scaredTimeScore = 0
|
||||
scaredTimes = [ghostSt.scaredTimer for ghostSt in state.getGhostStates()]
|
||||
if scaredTimes:
|
||||
scaredTimeScore = min(scaredTimes)
|
||||
|
||||
ghostDists = []
|
||||
for ghostState in state.getGhostStates():
|
||||
x, y = ghostState.getPosition()
|
||||
ghostPos = (int(x), int(y))
|
||||
distance = mazeDistance(pos, ghostPos, state)
|
||||
ghostDists.append(distance)
|
||||
if ghostDists:
|
||||
try:
|
||||
ghostScore = 1. / min(ghostDists)
|
||||
except ZeroDivisionError:
|
||||
ghostScore = 100
|
||||
|
||||
foodDists = [manhattanDistance(pos, foodPos)
|
||||
for foodPos in state.getFood().asList()]
|
||||
foodScore = 0
|
||||
if foodDists:
|
||||
foodScore = 1. / min(foodDists)
|
||||
|
||||
gameScore = state.getScore()
|
||||
|
||||
weightGhost = -0.01
|
||||
weightFood = 0.5
|
||||
weightScore = 0.2
|
||||
weightScaredTime = 0.01
|
||||
|
||||
score = ghostScore * weightGhost + \
|
||||
foodScore * weightFood + \
|
||||
gameScore * weightScore + \
|
||||
scaredTimeScore * weightScaredTime
|
||||
# print(state)
|
||||
# print(score, ghostScore, foodScore, gameScore, scaredTimeScore)
|
||||
return score
|
||||
|
||||
# Abbreviation
|
||||
better = betterEvaluationFunction
|
||||
|
||||
Reference in New Issue
Block a user