amloessb

betterEvaluationFunction

Jan 24th, 2012
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.92 KB | None | 0 0
  1. def betterEvaluationFunction(currentGameState):
  2.     """
  3.    Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
  4.    evaluation function (question 5).
  5.  
  6.    DESCRIPTION:
  7.        This is essentially our reflex agent, but pertaining the current state only.
  8.        1.  Start with score 0
  9.        2.  Add 200 * (4 - # of capsules left)
  10.            This incites pacman to eat capsules (if there are <4; qty can be varied arbitrarily)
  11.        3.  Subtract twice the distance to the nearest capsule (if pacman is w/in 5 of it)
  12.            This makes pacman beeline for a capsule once he gets close
  13.        4.  Subtract the distance to the nearest piece of food (if NOT w/in 5 of a capsule)
  14.            This makes pacman continuously move toward food
  15.        5.  Subtract the distance to the nearest ghost if it is "afraid"
  16.            This makes pacman try to eat vulnerable ghosts
  17.        6.  Add the distance to the nearest "non-afraid" ghost (if pacman is w/in 3 of it)
  18.            This makes pacman keep away from ghosts, but also not give undue thought to distant ghosts
  19.        7.  Add 4 (if NOT w/in 3 of any ghost)
  20.            This keeps pacman from trying to be too friendly with the ghosts :)
  21.        8.  Subtract 500 if pacman occupies the same space as a ghost
  22.            Prevents pacman from suiciding
  23.        9.  Subtract 250 if pacman occupies a space adjacent to a ghost
  24.            Keeps pacman from getting into the ghosts' attack range
  25.        10. Subtract the amount of food left on the board
  26.            Gives pacman an incentive to eat food he is adjacent to
  27.        11. Add 4 times the current score
  28.            Clobbers any corner cases where pacman gets stuck next to a piece of food
  29.    """
  30.     newPos = currentGameState.getPacmanPosition()
  31.     newFood = currentGameState.getFood()
  32.     newGhostStates = currentGameState.getGhostStates()
  33.     newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
  34.     newGhostPositions = [ghostState.getPosition() for ghostState in newGhostStates]
  35.     newFoodCount = newFood.count()
  36.     newFoodPositions = newFood.asList()
  37.     newCapsulePositions = currentGameState.getCapsules()
  38.    
  39.     scaredGhosts = []
  40.     numGhosts = len(newGhostStates)
  41.     for i in xrange(0, numGhosts):
  42.         if not newScaredTimes[i] == 0:
  43.             scaredGhosts.append(newGhostPositions[i])
  44.    
  45.     score = 0
  46.    
  47.     targetCapsule = None
  48.     capsuleDist = 999999
  49.     for capsulePos in newCapsulePositions:
  50.         tempDist = manhattanDistance(newPos, capsulePos)
  51.         if tempDist < 5:
  52.             targetCapsule = capsulePos
  53.             capsuleDist = tempDist
  54.     if not targetCapsule == None:
  55.         score -= capsuleDist * 2
  56.     score += 200 * (4 - len(newCapsulePositions))
  57.    
  58.     minDist = 999999
  59.     bestFood = None
  60.     if capsuleDist >= 5:
  61.         for foodPos in newFoodPositions:
  62.             currDist = manhattanDistance(newPos, foodPos)
  63.             if currDist < minDist:
  64.                 minDist = currDist
  65.                 bestFood = foodPos
  66.         if not newFoodCount == 0:
  67.             score -= minDist
  68.    
  69.     minDist = 999999
  70.     isOnGhost = False
  71.     isNextToGhost = False
  72.     closestGhost = None
  73.     for ghostPos in newGhostPositions:
  74.         currDist = manhattanDistance(newPos, ghostPos)
  75.         if currDist == 0 and ghostPos not in scaredGhosts:
  76.             isOnGhost = True
  77.         if currDist == 1 and ghostPos not in scaredGhosts:
  78.             isNextToGhost = True
  79.         if currDist < minDist:
  80.             minDist = currDist
  81.             closestGhost = ghostPos
  82.     if closestGhost in scaredGhosts:
  83.         score -= minDist
  84.     else:
  85.         if minDist < 3:
  86.             score += minDist
  87.         else:
  88.             score += 4
  89.    
  90.     score -= newFoodCount
  91.    
  92.     if isOnGhost:
  93.         score -= 500
  94.     if isNextToGhost:
  95.         score -= 250
  96.    
  97.     score += currentGameState.getScore() * 4
  98.    
  99.     return score
Advertisement
Add Comment
Please, Sign In to add comment