feat: alpha-beta prunning

This commit is contained in:
snsd0805 2024-04-09 18:46:56 +08:00
parent e7464ceeb4
commit 3fcd55923b
Signed by: snsd0805
GPG Key ID: 569349933C77A854

View File

@ -196,7 +196,35 @@ class AlphaBetaAgent(MultiAgentSearchAgent):
Returns the minimax action using self.depth and self.evaluationFunction
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()
return self.result(gameState, 0, self.depth, -1e9, 1e9)[1]
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):
"""