Skip to content

Commit 9cd01bd

Browse files
committed
2 parents a868492 + 12854c7 commit 9cd01bd

File tree

1 file changed

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

1 file changed

+56
-2
lines changed

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

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,62 @@ def getAction(self, gameState: GameState):
203203
All ghosts should be modeled as choosing uniformly at random from their
204204
legal moves.
205205
"""
206-
"*** YOUR CODE HERE ***"
207-
util.raiseNotDefined()
206+
numGhosts = gameState.getNumAgents()
207+
agentIndex=0
208+
maxVal = -99999999
209+
bestAction = 0
210+
depth = 1
211+
212+
213+
successor={}
214+
for i in range((numGhosts-1)):
215+
successor[i]=i+1
216+
successor[(numGhosts-1)]=0
217+
nextAgent = successor[agentIndex]
218+
219+
for action in gameState.getLegalActions(agentIndex):
220+
nextState = gameState.generateSuccessor(agentIndex,action)
221+
actionVal = self.expectAgentNode(nextAgent,depth,nextState,successor)
222+
if actionVal>maxVal:
223+
maxVal = actionVal
224+
bestAction = action
225+
226+
return bestAction
227+
228+
def expectAgentNode(self,agentIndex,depth,gameState,successor):
229+
import numpy as np
230+
expectVal = []
231+
nextAgent = successor[agentIndex]
232+
233+
if gameState.getLegalActions(agentIndex)==[]:
234+
return self.evaluationFunction(gameState)
235+
236+
if nextAgent==0:
237+
for action in gameState.getLegalActions(agentIndex):
238+
nextState = gameState.generateSuccessor(agentIndex,action)
239+
expectVal.append(self.maxAgentNode(nextAgent, depth, nextState,successor))
240+
return np.mean(expectVal)
241+
else:
242+
for action in gameState.getLegalActions(agentIndex):
243+
nextState = gameState.generateSuccessor(agentIndex,action)
244+
expectVal.append(self.expectAgentNode(nextAgent, depth, nextState,successor))
245+
return np.mean(expectVal)
246+
247+
248+
249+
def maxAgentNode(self,agentIndex,depth,gameState,successor):
250+
depth += 1
251+
maxVal=-99999999
252+
nextAgent = successor[agentIndex]
253+
254+
if depth==(self.depth+1) or gameState.getLegalActions(agentIndex)==[]:
255+
return self.evaluationFunction(gameState)
256+
257+
258+
for action in gameState.getLegalActions(agentIndex):
259+
nextState = gameState.generateSuccessor(agentIndex, action)
260+
maxVal = max(maxVal, self.expectAgentNode(nextAgent,depth,nextState,successor))
261+
return maxVal
208262

209263
def betterEvaluationFunction(currentGameState: GameState):
210264
"""

0 commit comments

Comments
 (0)