Skip to content

Commit 80f4d2c

Browse files
committed
minimax & alphabeta modification
1 parent 389501b commit 80f4d2c

File tree

1 file changed

+22
-20
lines changed
  • 2022/FA22/intro-ai-series/workshop-2-multi-agent-algorithms/src

1 file changed

+22
-20
lines changed

2022/FA22/intro-ai-series/workshop-2-multi-agent-algorithms/src/multiAgents.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ def getAction(self, gameState: GameState):
140140

141141
actions = gameState.getLegalActions(0)
142142
successor_states = [gameState.generateSuccessor(0, act) for act in actions]
143+
# Map successor_state to corresponding actino
143144
result_action = dict(zip(successor_states, actions))
144145
maxVal = -float('inf')
145146
maxAction = actions[0]
@@ -152,33 +153,34 @@ def getAction(self, gameState: GameState):
152153
return maxAction
153154

154155
def value(self, gameState, currDepth, agent):
155-
# Base case (Terminate state)
156+
# Base case (Terminal state)
156157
if currDepth == 0 or gameState.isLose() or gameState.isWin():
157-
return self.evaluationFunction(gameState) #static evalutation
158+
return self.evaluationFunction(gameState) # static evalutation
158159
if agent == 0:
159160
return self.maxValue(gameState, currDepth, agent)
160161
else:
161162
return self.minValue(gameState, currDepth, agent)
162163

163164
def maxValue(self, gameState, currDepth, agent):
164165
actions = gameState.getLegalActions(agent) # all possible actions
165-
successor_states = [gameState.generateSuccessor(agent, act) for act in actions]
166166
maxVal = -float("inf")
167-
for act in successor_states:
168-
val = self.value(act, currDepth, 1)
167+
for act in actions:
168+
successor_state = gameState.generateSuccessor(agent, act) # get child
169+
val = self.value(successor_state, currDepth, 1)
169170
maxVal = max(maxVal, val)
170171
return maxVal
171172

172173
def minValue(self, gameState, currDepth, agent):
173174
actions = gameState.getLegalActions(agent) # all possible actions
174-
successor_states = [gameState.generateSuccessor(agent, act) for act in actions]
175-
176175
minVal = float("inf")
177-
for act in successor_states:
178-
if agent + 1 == gameState.getNumAgents():
179-
val = self.value(act, currDepth - 1, 0)
180-
else:
181-
val = self.value(act, currDepth, agent + 1)
176+
numAgents = gameState.getNumAgents()
177+
for act in actions:
178+
successor_state = gameState.generateSuccessor(agent, act)
179+
depth = currDepth
180+
# Evaluate for all ghosts. If reach the last ghost, update depth
181+
if agent + 1 == numAgents:
182+
depth -= 1
183+
val = self.value(successor_state, depth, (agent + 1) % numAgents)
182184
minVal = min(minVal, val)
183185
return minVal
184186

@@ -225,24 +227,24 @@ def maxValue(self, gameState, currDepth, agent, alpha, beta):
225227
successor_state = gameState.generateSuccessor(agent, act)
226228
val = self.value(successor_state, currDepth, 1, alpha, beta)
227229
maxVal = max(maxVal, val)
228-
# Check for pruning
229-
if maxVal > beta:
230+
if maxVal > beta: # Check for pruning
230231
return maxVal
231232
alpha = max(alpha, maxVal)
232233
return maxVal
233234

234235
def minValue(self, gameState, currDepth, agent, alpha, beta):
235236
actions = gameState.getLegalActions(agent) # all possible actions
236237
minVal = float("inf")
238+
numAgents = gameState.getNumAgents()
237239
for act in actions:
238240
successor_state = gameState.generateSuccessor(agent, act)
239-
if agent + 1 == gameState.getNumAgents():
240-
val = self.value(successor_state, currDepth - 1, 0, alpha, beta)
241-
else:
242-
val = self.value(successor_state, currDepth, agent + 1, alpha, beta)
241+
depth = currDepth
242+
# Evaluate for all ghosts. If reach the last ghost, update depth
243+
if agent + 1 == numAgents:
244+
depth -= 1
245+
val = self.value(successor_state, depth, (agent + 1) % numAgents, alpha, beta)
243246
minVal = min(minVal, val)
244-
# Check for pruning
245-
if minVal < alpha:
247+
if minVal < alpha: # Check for pruning
246248
return minVal
247249
beta = min(beta, minVal)
248250
return minVal

0 commit comments

Comments
 (0)