Skip to content

Commit d6653ba

Browse files
committed
add comment and descriptive var
1 parent 80f4d2c commit d6653ba

File tree

1 file changed

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

1 file changed

+13
-9
lines changed

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ class MinimaxAgent(MultiAgentSearchAgent):
112112
"""
113113
Your minimax agent (question 2)
114114
"""
115+
pacmanAgent = 0
116+
ghostAgent = 1 # index of first ghost agent
115117

116118
def getAction(self, gameState: GameState):
117119
"""
@@ -138,15 +140,15 @@ def getAction(self, gameState: GameState):
138140
"""
139141
"*** YOUR CODE HERE ***"
140142

141-
actions = gameState.getLegalActions(0)
142-
successor_states = [gameState.generateSuccessor(0, act) for act in actions]
143-
# Map successor_state to corresponding actino
143+
actions = gameState.getLegalActions(self.pacmanAgent)
144+
successor_states = [gameState.generateSuccessor(self.pacmanAgent, act) for act in actions]
145+
# Map successor_state to corresponding action
144146
result_action = dict(zip(successor_states, actions))
145147
maxVal = -float('inf')
146148
maxAction = actions[0]
147149

148150
for state, act in result_action.items():
149-
currVal = self.value(state, self.depth, 1)
151+
currVal = self.value(state, self.depth, self.ghostAgent)
150152
if maxVal != max(currVal, maxVal):
151153
maxVal = max(currVal, maxVal)
152154
maxAction = act
@@ -166,7 +168,7 @@ def maxValue(self, gameState, currDepth, agent):
166168
maxVal = -float("inf")
167169
for act in actions:
168170
successor_state = gameState.generateSuccessor(agent, act) # get child
169-
val = self.value(successor_state, currDepth, 1)
171+
val = self.value(successor_state, currDepth, self.ghostAgent)
170172
maxVal = max(maxVal, val)
171173
return maxVal
172174

@@ -188,13 +190,15 @@ class AlphaBetaAgent(MultiAgentSearchAgent):
188190
"""
189191
Your minimax agent with alpha-beta pruning (question 3)
190192
"""
193+
pacmanAgent = 0
194+
ghostAgent = 1 # index of first ghost agent
191195

192196
def getAction(self, gameState: GameState):
193197
"""
194198
Returns the minimax action using self.depth and self.evaluationFunction
195199
"""
196-
actions = gameState.getLegalActions(0)
197-
successor_states = [gameState.generateSuccessor(0, act) for act in actions]
200+
actions = gameState.getLegalActions(self.pacmanAgent)
201+
successor_states = [gameState.generateSuccessor(self.pacmanAgent, act) for act in actions]
198202
result_action = dict(zip(successor_states, actions))
199203
maxVal = -float('inf')
200204
maxAction = actions[0]
@@ -203,7 +207,7 @@ def getAction(self, gameState: GameState):
203207
beta = float('inf')
204208

205209
for state, act in result_action.items():
206-
currVal = self.value(state, self.depth, 1, alpha, beta)
210+
currVal = self.value(state, self.depth, self.ghostAgent, alpha, beta)
207211
if maxVal != max(currVal, maxVal):
208212
maxVal = max(currVal, maxVal)
209213
maxAction = act
@@ -225,7 +229,7 @@ def maxValue(self, gameState, currDepth, agent, alpha, beta):
225229
maxVal = -float("inf")
226230
for act in actions:
227231
successor_state = gameState.generateSuccessor(agent, act)
228-
val = self.value(successor_state, currDepth, 1, alpha, beta)
232+
val = self.value(successor_state, currDepth, self.ghostAgent, alpha, beta)
229233
maxVal = max(maxVal, val)
230234
if maxVal > beta: # Check for pruning
231235
return maxVal

0 commit comments

Comments
 (0)