完成剪枝

master
huangjielun 2 years ago
parent 018c7104fb
commit 9418256a0b

@ -74,6 +74,7 @@ class ReflexAgent(Agent):
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
#many ghosts
"*** YOUR CODE HERE ***"
GhostPos = successorGameState.getGhostPositions()
x_pacman,y_pacman = newPos
failedDist = min([(abs(each[0]- x_pacman) + abs(each[1]-y_pacman)) for each in GhostPos])
@ -112,6 +113,7 @@ class ReflexAgent(Agent):
foodScore = 0
return successorGameState.getScore() + foodScore + ghostScore
def scoreEvaluationFunction(currentGameState):
"""
This default evaluation function just returns the score of the state.
@ -146,30 +148,6 @@ class MinimaxAgent(MultiAgentSearchAgent):
"""
Your minimax agent (question 2)
"""
def minimax(self,agentIndex,gameState,Depth):
if Depth >= self.depth * 2 or gameState.isWin() or gameState.isLose():
return self.evaluationFunction(gameState)
if agentIndex == 0:
MAX = float("-inf")
pacmanActions = gameState.getLegalActions(agentIndex)
for each_action in pacmanActions:
successor_state = gameState.generateSuccessor(agentIndex, each_action)
value = self.minimax(1, successor_state, Depth + 1)
MAX = max(MAX, value)
return MAX
else:
values = []
MIN = float("inf")
ghostActions = gameState.getLegalActions(agentIndex)
for each_action in ghostActions:
successor_state = gameState.generateSuccessor(agentIndex, each_action)
if agentIndex >= gameState.getNumAgents() - 1:
values.append(self.minimax(0, successor_state, Depth + 1))
else:
values.append(self.minimax(agentIndex + 1, successor_state, Depth))
value = min(values)
MIN = min(MIN, value)
return MIN
def getAction(self, gameState):
"""
Returns the minimax action from the current gameState using self.depth
@ -194,7 +172,6 @@ class MinimaxAgent(MultiAgentSearchAgent):
Returns whether or not the game state is a losing state
"""
"*** YOUR CODE HERE ***"
'''
GhostIndex = [i for i in range(1, gameState.getNumAgents())]
def gameOver(state, d):
@ -217,23 +194,14 @@ class MinimaxAgent(MultiAgentSearchAgent):
return self.evaluationFunction(state)
Alpha = -10000000000000000
for action in state.getLegalActions(0):
if action == 'Stop':
continue
Alpha = max(Alpha, min_value(state.generateSuccessor(0, action), d, 1))
return Alpha
res = [(action, min_value(gameState.generateSuccessor(0, action), 0, 1)) for action in
gameState.getLegalActions(0)]
res.sort(key=lambda k: k[1])
return res[-1][0]
'''
bestaction = "stop"
maxvalue = float('-inf')
for each_action in gameState.getLegalActions(0):
successor_state = gameState.generateSuccessor(0, each_action)
value = self.minimax(agentIndex=0, gameState=successor_state, Depth=0)
if value is not None and maxvalue < value:
maxvalue = value
bestaction = each_action
return bestaction
util.raiseNotDefined()
class AlphaBetaAgent(MultiAgentSearchAgent):
@ -246,6 +214,51 @@ class AlphaBetaAgent(MultiAgentSearchAgent):
Returns the minimax action using self.depth and self.evaluationFunction
"""
"*** YOUR CODE HERE ***"
ghostIndex = [i for i in range(1,gameState.getNumAgents())]
def gameOver(state,depth):
return state.isWin() or state.isLose() or depth == self.depth
def min_value(state,depth,ghost,alpha,beta):
if gameOver(state,depth):
return self.evaluationFunction(state)
value = float('inf')
for action in state.getLegalActions(ghost):
if ghost == ghostIndex[-1]:
value = min(value,max_value(state.generateSuccessor(ghost,action),depth+1,alpha,beta))
else:
value = min(value,min_value(state.generateSuccessor(ghost,action),depth,ghost+1,alpha,beta))
if value < alpha:
return value
beta = min(beta,value)
return value
def max_value(state,depth,alpha,beta):
if gameOver(state,depth):
return self.evaluationFunction(state)
value = float('-inf')
for action in state.getLegalActions(0):
if action == 'Stop':
continue
value = max(value,min_value(state.generateSuccessor(0,action),depth,1,alpha,beta))
if value > beta:
return value
alpha = max(value,alpha)
return value
def function(state):
value = float('-inf')
actions = None
alpha = float('-inf')
beta = float('inf')
for action in state.getLegalActions(0):
if action == 'Stop':
continue
tmpValue = min_value(state.generateSuccessor(0,action),0,1,alpha,beta)
if value < tmpValue:
value = tmpValue
actions = action
if value > beta:
return value
alpha = max(value,alpha)
return actions
return function(gameState)
util.raiseNotDefined()
class ExpectimaxAgent(MultiAgentSearchAgent):

Loading…
Cancel
Save