master
huangjielun 3 years ago
parent 0f23be4a5b
commit 19886fb86e

@ -146,7 +146,30 @@ class MinimaxAgent(MultiAgentSearchAgent):
""" """
Your minimax agent (question 2) 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): def getAction(self, gameState):
""" """
Returns the minimax action from the current gameState using self.depth Returns the minimax action from the current gameState using self.depth
@ -163,8 +186,54 @@ class MinimaxAgent(MultiAgentSearchAgent):
gameState.getNumAgents(): gameState.getNumAgents():
Returns the total number of agents in the game Returns the total number of agents in the game
gameState.isWin():
Returns whether or not the game state is a winning state
gameState.isLose():
Returns whether or not the game state is a losing state
""" """
"*** YOUR CODE HERE ***" "*** YOUR CODE HERE ***"
'''
GhostIndex = [i for i in range(1, gameState.getNumAgents())]
def gameOver(state, d):
return state.isWin() or state.isLose() or d == self.depth
def min_value(state, d, ghost): # minimizer
if gameOver(state, d):
return self.evaluationFunction(state)
Beta = 10000000000000000
for action in state.getLegalActions(ghost):
if ghost == GhostIndex[-1]:
Beta = min(Beta, max_value(state.generateSuccessor(ghost, action), d + 1))
else:
Beta = min(Beta, min_value(state.generateSuccessor(ghost, action), d, ghost + 1))
return Beta
def max_value(state, d): # maximizer
if gameOver(state, d):
return self.evaluationFunction(state)
Alpha = -10000000000000000
for action in state.getLegalActions(0):
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() util.raiseNotDefined()
class AlphaBetaAgent(MultiAgentSearchAgent): class AlphaBetaAgent(MultiAgentSearchAgent):

Loading…
Cancel
Save