Compare commits

..

No commits in common. "3fcd55923bd5daa7756eb69ce8abde5771b7f028" and "359e3094ca325a1f8db63d4c00ca69cc7e3c97cd" have entirely different histories.

View File

@ -72,12 +72,12 @@ 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]
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
ans = successorGameState.getScore() ans = successorGameState.getScore()
AVOID_GHOST_SCORE = 25 AVOID_GHOST_SCORE = 10
EAT_FOOD_SCORE = 20 EAT_FOOD_SCORE = 20
EAT_CAPSULE_SCORE = 70
nearest_ghost_distance = 1e9 nearest_ghost_distance = 1e9
for ghostState in newGhostStates: for ghostState in newGhostStates:
@ -88,17 +88,13 @@ class ReflexAgent(Agent):
for foodPos in newFood.asList(): for foodPos in newFood.asList():
nearest_food_distance = min(nearest_food_distance, util.manhattanDistance(foodPos, newPos)+1) nearest_food_distance = min(nearest_food_distance, util.manhattanDistance(foodPos, newPos)+1)
nearest_capsule_distance = 1e9
for capsulePos in successorGameState.getCapsules():
nearest_capsule_distance = min(nearest_capsule_distance, util.manhattanDistance(capsulePos, newPos)+1)
ans -= AVOID_GHOST_SCORE * (1/nearest_ghost_distance) ans -= AVOID_GHOST_SCORE * (1/nearest_ghost_distance)
ans += EAT_FOOD_SCORE * (1/nearest_food_distance) ans += EAT_FOOD_SCORE * (1/nearest_food_distance)
if nearest_capsule_distance != 1e9:
ans += EAT_CAPSULE_SCORE * (1/nearest_capsule_distance)
else:
ans += 500
return ans return ans
return successorGameState.getScore() return successorGameState.getScore()
@ -162,29 +158,7 @@ class MinimaxAgent(MultiAgentSearchAgent):
Returns whether or not the game state is a losing state Returns whether or not the game state is a losing state
""" """
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
return self.result(gameState, 0, self.depth)[1] util.raiseNotDefined()
def result(self, state, agentIndex, depth):
if depth == 0 or state.isLose() or state.isWin():
return self.evaluationFunction(state), None
elif agentIndex == 0: # maximize
(nextAgentIndex, nextDepth) = (0, depth-1) if agentIndex == (state.getNumAgents()-1) else (agentIndex+1, depth)
max_reward, max_action = -1e9, None
for action in state.getLegalActions(agentIndex):
reward = self.result(state.generateSuccessor(agentIndex, action), nextAgentIndex, nextDepth)[0]
if reward > max_reward:
max_reward, max_action = reward, action
return max_reward, max_action
else: # minimize
(nextAgentIndex, nextDepth) = (0, depth-1) if agentIndex == (state.getNumAgents()-1) else (agentIndex+1, depth)
min_reward, min_action = 1e9, None
for action in state.getLegalActions(agentIndex):
reward = self.result(state.generateSuccessor(agentIndex, action), nextAgentIndex, nextDepth)[0]
if reward < min_reward:
min_reward, min_action = reward, action
return min_reward, min_action
class AlphaBetaAgent(MultiAgentSearchAgent): class AlphaBetaAgent(MultiAgentSearchAgent):
""" """
@ -196,35 +170,7 @@ class AlphaBetaAgent(MultiAgentSearchAgent):
Returns the minimax action using self.depth and self.evaluationFunction Returns the minimax action using self.depth and self.evaluationFunction
""" """
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
return self.result(gameState, 0, self.depth, -1e9, 1e9)[1] util.raiseNotDefined()
def result(self, state, agentIndex, depth, alpha, beta):
if depth == 0 or state.isLose() or state.isWin():
return self.evaluationFunction(state), None
elif agentIndex == 0: # maximize
(nextAgentIndex, nextDepth) = (0, depth-1) if agentIndex == (state.getNumAgents()-1) else (agentIndex+1, depth)
max_reward, max_action = -1e9, None
for action in state.getLegalActions(agentIndex):
reward = self.result(state.generateSuccessor(agentIndex, action), nextAgentIndex, nextDepth, alpha, beta)[0]
if reward > max_reward:
max_reward, max_action = reward, action
if reward > beta:
return reward, action
alpha = max(alpha, max_reward)
return max_reward, max_action
else: # minimize
(nextAgentIndex, nextDepth) = (0, depth-1) if agentIndex == (state.getNumAgents()-1) else (agentIndex+1, depth)
min_reward, min_action = 1e9, None
for action in state.getLegalActions(agentIndex):
reward = self.result(state.generateSuccessor(agentIndex, action), nextAgentIndex, nextDepth, alpha, beta)[0]
if reward < min_reward:
min_reward, min_action = reward, action
if reward < alpha:
return reward, action
beta = min(beta, min_reward)
return min_reward, min_action
class ExpectimaxAgent(MultiAgentSearchAgent): class ExpectimaxAgent(MultiAgentSearchAgent):
""" """