Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. class MinimaxAgent(MultiAgentSearchAgent):
  2. """
  3. Your minimax agent (question 2)
  4. """
  5.  
  6. def getAction(self, gameState):
  7. """
  8. Returns the minimax action from the current gameState using self.depth
  9. and self.evaluationFunction.
  10.  
  11. Here are some method calls that might be useful when implementing minimax.
  12.  
  13. gameState.getLegalActions(agentIndex):
  14. Returns a list of legal actions for an agent
  15. agentIndex=0 means Pacman, ghosts are >= 1
  16.  
  17. gameState.generateSuccessor(agentIndex, action):
  18. Returns the successor game state after an agent takes an action
  19.  
  20. gameState.getNumAgents():
  21. Returns the total number of agents in the game
  22.  
  23. gameState.isWin():
  24. Returns whether or not the game state is a winning state
  25.  
  26. gameState.isLose():
  27. Returns whether or not the game state is a losing state
  28. """
  29. def dispatcher(depth, index, gameState):
  30. if (index == gameState.getNumAgents() - 1 and depth == self.depth) or gameState.isWin() or gameState.isLose():
  31. return self.evaluationFunction(gameState)
  32. if index:
  33. return min_func(depth, index, gameState)
  34. else:
  35. if depth:
  36. return max_func(depth, index, gameState)
  37. else:
  38. return max_func_start(depth, index,gameState)
  39. def min_func(depth, index, gameState):
  40. v = float("inf")
  41. index = index + 1
  42. index %= gameState.getNumAgents()
  43. if not index:
  44. depth += 1
  45. for action in gameState.getLegalActions():
  46. successor = gameState.generateSuccessor(index,action)
  47. v = min(v, dispatcher(depth, index, successor))
  48. return v
  49. def max_func(depth, index, gameState):
  50. v = float("-inf")
  51. index = index + 1
  52. index %= gameState.getNumAgents()
  53. if not index:
  54. depth += 1
  55. for action in gameState.getLegalActions():
  56. successor = gameState.generateSuccessor(index,action)
  57. v = max(v, dispatcher(depth, index, successor))
  58. return v
  59. def max_func_start(depth, index, gameState):
  60. v = float("-inf")
  61. index = index + 1
  62. index %= gameState.getNumAgents()
  63. if not index:
  64. depth += 1
  65. bestAction = None
  66. for action in gameState.getLegalActions():
  67. oldv = v
  68. successor = gameState.generateSuccessor(index,action)
  69. v = max(v, dispatcher(depth, index, successor))
  70. if v != oldv:
  71. bestAction = action
  72. return bestAction
  73. return dispatcher(0, 0, gameState)
  74. "*** YOUR CODE HERE ***"
  75. util.raiseNotDefined()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement