Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 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. "*** YOUR CODE HERE ***"
  24.  
  25. agentCount = gameState.getNumAgents()
  26. foodCount = len(gameState.getFood().asList())
  27. score = []
  28. MAX_ROUND = self.depth * agentCount
  29.  
  30. def isGhost(index):
  31. return index != 0
  32.  
  33. def isOver(state):
  34. return state.isWin() or state.isLose()
  35.  
  36. def terminalTest(state, round):
  37. return isOver(state) or round >= MAX_ROUND
  38.  
  39. def minValue(state, round, agentIndex):
  40. result = foodCount
  41. for action in state.getLegalActions(agentIndex):
  42. result = min(minMax(state.generateSuccessor(agentIndex,action), round + 1),result)
  43. return result
  44.  
  45. def maxValue(state, round, agentIndex):
  46. result = -foodCount
  47. for action in state.getLegalActions(agentIndex):
  48. result = max(minMax(state.generateSuccessor(agentIndex,action), round + 1),result)
  49. if round==0:
  50. score.append(result)
  51. return result
  52.  
  53. def minMax(state, round):
  54. agentIndex = round%agentCount
  55. if terminalTest(state, round):
  56. return self.evaluationFunction(state)
  57. if isGhost(agentIndex):
  58. return minValue(state, round, agentIndex)
  59. else:
  60. return maxValue(state, round, agentIndex)
  61.  
  62. minMax(gameState, 0)
  63. maxValue = max(score)
  64. return gameState.getLegalActions(0)[score.index(maxValue)]
  65.  
  66. #util.raiseNotDefined()
  67.  
  68. class AlphaBetaAgent(MultiAgentSearchAgent):
  69. """
  70. Your minimax agent with alpha-beta pruning (question 3)
  71. """
  72.  
  73. def getAction(self, gameState):
  74. """
  75. Returns the minimax action using self.depth and self.evaluationFunction
  76. """
  77. "*** YOUR CODE HERE ***"
  78.  
  79. agentCount = gameState.getNumAgents()
  80. foodCount = len(gameState.getFood().asList())
  81. score = []
  82. MAX_ROUND = self.depth * agentCount
  83.  
  84. def isGhost(index):
  85. return index != 0
  86.  
  87. def isOver(state):
  88. return state.isWin() or state.isLose()
  89.  
  90. def terminalTest(state, round):
  91. return isOver(state) or round >= MAX_ROUND
  92.  
  93. def minValue(state, round, agentIndex, alpha, beta):
  94. result = foodCount
  95. for action in state.getLegalActions(agentIndex):
  96. result = min(alphaBeta(state.generateSuccessor(agentIndex,action), round + 1, alpha, beta),result)
  97. beta = min(beta, result)
  98. if beta < alpha:
  99. break
  100. return result
  101.  
  102. def maxValue(state, round, agentIndex, alpha, beta):
  103. result = -foodCount
  104. for action in state.getLegalActions(agentIndex):
  105. result = max(alphaBeta(state.generateSuccessor(agentIndex,action), round + 1, alpha, beta),result)
  106. alpha = max(alpha, result)
  107. if round == 0:
  108. score.append(result)
  109. if beta < alpha:
  110. break
  111. return result
  112.  
  113. def alphaBeta(state, round, alpha, beta):
  114. agentIndex = round%agentCount
  115. if terminalTest(state, round):
  116. return self.evaluationFunction(state)
  117. if isGhost(agentIndex):
  118. minValue(state, round, agentIndex, alpha, beta)
  119. else:
  120. maxValue(state, round, agentIndex, alpha, beta)
  121.  
  122. alphaBeta(gameState, 0, -foodCount, foodCount)
  123. maxValue = max(score)
  124. return gameState.getLegalActions(0)[score.index(maxValue)]
  125.  
  126. #util.raiseNotDefined()
  127.  
  128. class ExpectimaxAgent(MultiAgentSearchAgent):
  129. """
  130. Your expectimax agent (question 4)
  131. """
  132.  
  133. def getAction(self, gameState):
  134. """
  135. Returns the expectimax action using self.depth and self.evaluationFunction
  136.  
  137. All ghosts should be modeled as choosing uniformly at random from their
  138. legal moves.
  139. """
  140. agentCount = gameState.getNumAgents()
  141. foodCount = len(gameState.getFood().asList())
  142. score = []
  143. MAX_ROUND = self.depth * agentCount
  144.  
  145. def isGhost(index):
  146. return index != 0
  147.  
  148. def isOver(state):
  149. return state.isWin() or state.isLose()
  150.  
  151. def terminalTest(state, round):
  152. return isOver(state) or round >= MAX_ROUND
  153.  
  154. def minValue(state, round, agentIndex):
  155. succ = []
  156. scoreSum = 0
  157. result = foodCount
  158. for action in state.getLegalActions(agentIndex):
  159. result = expectiMax(state.generateSuccessor(agentIndex,action), round + 1)
  160. succ.append(result)
  161. for s in succ:
  162. scoreSum += s
  163. average = scoreSum/len(succ)
  164. return average
  165.  
  166. def maxValue(state, round, agentIndex):
  167. result = -foodCount
  168. for action in state.getLegalActions(agentIndex):
  169. result = max(expectiMax(state.generateSuccessor(agentIndex,action), round + 1),result)
  170. if round==0:
  171. score.append(result)
  172. return result
  173.  
  174. def expectiMax(state, round):
  175. agentIndex = round%agentCount
  176. if terminalTest(state, round):
  177. return self.evaluationFunction(state)
  178. if isGhost(agentIndex):
  179. return minValue(state, round, agentIndex)
  180. else:
  181. return maxValue(state, round, agentIndex)
  182.  
  183. expectiMax(gameState, 0);
  184. maxValue = max(score)
  185. return gameState.getLegalActions(0)[score.index(maxValue)]
  186.  
  187. #util.raiseNotDefined()
  188.  
  189.  
  190.  
  191. # def foodDistanceHeuristic():
  192. # distanceToFood = []
  193. # for food in gameState.getFood().asList():
  194. # distanceToFood.append(manhattanDistance(gameState.getPacmanPosition(), food))
  195. # if len(distanceToFood) > 0:
  196. # return 100.0/(1.0 + min(distanceToFood))
  197. # else:
  198. # return 0.0
  199.  
  200. # def manhattanHeuristic(position, problem, info={}):
  201. # "The Manhattan distance heuristic for a PositionSearchProblem"
  202. # xy1 = position
  203. # xy2 = problem.goal
  204. # return abs(xy1[0] - xy2[0]) + abs(xy1[1] - xy2[1])
  205.  
  206. # def dynamicHeuristic(state):
  207.  
  208. # def computeLambda():
  209. # foods_left = len(state.getFood().asList())
  210. # foods_total = len(problem.getStartState().getFood().asList())
  211.  
  212. # return (foods_total - foods_left) / foods_total
  213.  
  214. # heuristic_early_game = foodDistanceHeuristicd
  215. # heuristic_late_game = manhattanHeuristic(state, problem, 0)
  216.  
  217. # # Scale heuristics to same scale
  218. # pass
  219.  
  220. # weight = computeLambda()
  221.  
  222. # # Convex combination
  223. # return ((1 - weight) * heuristic_early_game) + (weight * heuristic_late_game)
  224.  
  225. def betterEvaluationFunction(currentGameState):
  226. """
  227. Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
  228. evaluation function (question 5).
  229.  
  230. DESCRIPTION: <write something here so we know what you did>
  231. """
  232. "*** YOUR CODE HERE ***"
  233. def ghostScore(gameState):
  234. score = 0
  235. for ghost in gameState.getGhostStates():
  236. distance = manhattanDistance(gameState.getPacmanPosition(), ghost.getPosition())
  237. if ghost.scaredTimer > distance:
  238. score += 1/(1 + sqrt(distance))
  239. else:
  240. score -= 1/(1 + sqrt(distance))
  241. return score
  242.  
  243. def foodScore(gameState):
  244. distance = []
  245. for food in gameState.getFood().asList():
  246. distance.append(manhattanDistance(gameState.getPacmanPosition(), food))
  247. if len(distance) > 0:
  248. return 100.0/(1.0 + min(distance))
  249. else:
  250. return 0.0
  251.  
  252. def capsuleScore(gameState):
  253. distance = []
  254. for capsule in gameState.getCapsules():
  255. distance.append(manhattanDistance(gameState.getPacmanPosition(), capsule))
  256. if len(distance) > 0:
  257. return 1000.0/(1.0 + min(distance))
  258. else:
  259. return 0
  260.  
  261. #def _suicide(gameState):
  262. # score = 0
  263. # disGhost = 1e6
  264. # for ghost in gameState.getGhostStates():
  265. # disGhost = min(manhattanDistance(gameState.getPacmanPosition(), ghost.getPosition()), disGhost)
  266. # score -= pow(disGhost, 2)
  267. # if gameState.isLose():
  268. # score = 1e6
  269. # return score
  270.  
  271. score = currentGameState.getScore()
  272. scoreGhosts = ghostScore(currentGameState)
  273. scoreFood = foodScore(currentGameState)
  274. scoreCapsules = capsuleScore(currentGameState)
  275. #foodHeuristic = dynamicHeuristic(currentGameState, problem)
  276. #if score < 800 and currentGameState.getNumFood() <= 1 and len(currentGameState.getCapsules()) == 0:
  277. # return _suicide(currentGameState)
  278. #else:
  279. return score + scoreGhosts + scoreFood + scoreCapsules
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement