def getAction(self, gameState):
        """
    Calls chooseAction on a grid position, but continues on half positions.
    
    This method also cedes some processing time to the distance calculator, 
    which computes the shortest path distance between all pairs of points.
    
    If you subclass CaptureAgent, you shouldn't need to override this method.  It
    takes care of appending the current gameState on to your observation history
    (so you have a record of the game states of the game) and will call your
    choose action method if you're in a state (rather than halfway through your last
    move - this occurs because Pacman agents move half as quickly as ghost agents).
    
    If you aren't going to be using the distance calculator we provide, you can comment
    out the line beginning "distanceCalculator" so as not to lose computing time to the
    calculating distances you're not using.
    """
        # Give some time to the distance calculator thread
        distanceCalculator.waitOnDistanceCalculator(self.timeForComputing)

        self.observationHistory.append(gameState)

        myState = gameState.getAgentState(self.index)
        myPos = myState.getPosition()
        if myPos != nearestPoint(myPos):
            # We're halfway from one position to the next
            return gameState.getLegalActions(self.index)[0]
        else:
            return self.chooseAction(gameState)
 def getAction(self, gameState):
   """
   Calls chooseAction on a grid position, but continues on half positions.
   
   This method also cedes some processing time to the distance calculator, 
   which computes the shortest path distance between all pairs of points.
   
   If you subclass CaptureAgent, you shouldn't need to override this method.  It
   takes care of appending the current gameState on to your observation history
   (so you have a record of the game states of the game) and will call your
   choose action method if you're in a state (rather than halfway through your last
   move - this occurs because Pacman agents move half as quickly as ghost agents).
   
   If you aren't going to be using the distance calculator we provide, you can comment
   out the line beginning "distanceCalculator" so as not to lose computing time to the
   calculating distances you're not using.
   """
   # Give some time to the distance calculator thread
   distanceCalculator.waitOnDistanceCalculator(self.timeForComputing)
   
   self.observationHistory.append(gameState)
   
   myState = gameState.getAgentState(self.index)
   myPos = myState.getPosition()
   if myPos != nearestPoint(myPos): 
     # We're halfway from one position to the next
     return gameState.getLegalActions(self.index)[0]
   else:
     return self.chooseAction(gameState)
    def getFeatures(self, gameState):
        features = util.Counter()

        myState = gameState.getAgentState(self.index)
        myPos = myState.getPosition()

        # Computes whether we're on defense (1) or offense (0)
        features['onDefense'] = 1
        if myState.isPacman: features['onDefense'] = 0

        # Computes distance to invaders we can see
        enemies = [
            gameState.getAgentState(i) for i in self.getOpponents(gameState)
        ]
        invaders = [
            a for a in enemies if a.isPacman and a.getPosition() != None
        ]
        features['numInvaders'] = len(invaders)
        if len(invaders) > 0:
            dists = [
                self.getMazeDistance(myPos, a.getPosition()) for a in invaders
            ]
            features['invaderDistance'] = min(dists)

        #compute using particle filtering
        distanceCalculator.waitOnDistanceCalculator(0.5)
        myState = gameState.getAgentState(self.index)
        myPos = myState.getPosition()
        legal = gameState.getLegalActions(self.index)
        distributions = self.particlefilter.getEnemyPositionDistributions(
            gameState, myPos)
        modes = []
        for dist in distributions:
            if len(dist.keys()) > 0:
                modes.append(dist.argMax())
        if len(modes) != 0:
            options = []
            for l in legal:
                succ = gameState.generateSuccessor(self.index, l)
                closestMode = min([
                    self.distancer.getDistance(myPos, mode) for mode in modes
                ])
                options.append((closestMode, l))
            features['particleFilter'] = min(options)[0]

        # Computes distance to last food eaten
        if 'lastFoodEaten' not in dir(self): self.lastFoodEaten = []
        eatenFood = self.getEatenFoodSinceLastObservation()
        if len(eatenFood) > 0: self.lastFoodEaten = eatenFood
        if len(self.lastFoodEaten) > 0:
            foodDist = [
                self.getMazeDistance(myPos, food)
                for food in self.lastFoodEaten
            ]
            features['foodEatenDistance'] = min(foodDist)

        features['score'] = self.getScore(gameState)

        return features
  def getFeatures(self, gameState):
    features = util.Counter()

    myState = gameState.getAgentState(self.index)
    myPos = myState.getPosition()

    # Computes whether we're on defense (1) or offense (0)
    features['onDefense'] = 1
    if myState.isPacman: features['onDefense'] = 0
    
    # Computes distance to invaders we can see
    enemies = [gameState.getAgentState(i) for i in self.getOpponents(gameState)]
    invaders = [a for a in enemies if a.isPacman and a.getPosition() != None]
    features['numInvaders'] = len(invaders)
    if len(invaders) > 0:
      dists = [self.getMazeDistance(myPos, a.getPosition()) for a in invaders]
      features['invaderDistance'] = min(dists)
      
    #compute using particle filtering
    distanceCalculator.waitOnDistanceCalculator(0.5)
    myState = gameState.getAgentState(self.index)
    myPos = myState.getPosition()
    legal = gameState.getLegalActions(self.index)
    distributions = self.particlefilter.getEnemyPositionDistributions(gameState,myPos)
    modes = []
    for dist in distributions:
      if len(dist.keys()) > 0:
        modes.append(dist.argMax())
    if len(modes) != 0:
        options = []
        for l in legal:
            succ = gameState.generateSuccessor(self.index,l)
            closestMode = min([self.distancer.getDistance(myPos, mode) for mode in modes]) 
            options.append((closestMode, l))
        features['particleFilter'] = min(options)[0]
      
    # Computes distance to last food eaten
    if 'lastFoodEaten' not in dir(self): self.lastFoodEaten = []
    eatenFood = self.getEatenFoodSinceLastObservation()
    if len(eatenFood) > 0: self.lastFoodEaten = eatenFood
    if len(self.lastFoodEaten) > 0:
      foodDist = [self.getMazeDistance(myPos, food) for food in self.lastFoodEaten]
      features['foodEatenDistance'] = min(foodDist)
      
    features['score'] = self.getScore(gameState)  
      
    return features
예제 #5
0
 def getAction(self, state):
   distanceCalculator.waitOnDistanceCalculator(0.5)
   legal = state.getLegalActions(self.index)
   distributions = self.getGhostPositionDistributions(state)
   modes = []
   for dist in distributions:
     if len(dist.keys()) > 0:
       modes.append(dist.argMax())
   if len(modes) == 0: return random.choice(legal)
   
   options = []
   for l in legal:
     succ = state.generatePacmanSuccessor(l)
     pos = succ.getPacmanPosition()
     closestMode = min([self.distancer.getDistance(pos, mode) for mode in modes]) 
     options.append((closestMode, l))
   return min(options)[1]
 def chooseAction(self, gameState):
   distanceCalculator.waitOnDistanceCalculator(0.5)
   myState = gameState.getAgentState(self.index)
   myPos = myState.getPosition()
   legal = gameState.getLegalActions(self.index)
   distributions = self.particlefilter.getEnemyPositionDistributions(gameState,myPos)
   modes = []
   for dist in distributions:
     if len(dist.keys()) > 0:
       modes.append(dist.argMax())
   if len(modes) == 0: return random.choice(legal)
   
   options = []
   for l in legal:
     succ = gameState.generateSuccessor(self.index,l)
     closestMode = min([self.distancer.getDistance(myPos, mode) for mode in modes]) 
     options.append((closestMode, l))
   return min(options)[1]
 def getAction(self, gameState):
   """
   Calls chooseAction on a grid position, but continues on half positions.
   
   This method also cedes some processing time to the distance calculator, 
   which computes the shortest path distance between all pairs of points.
   """
   # Give some time to the distance calculator thread
   distanceCalculator.waitOnDistanceCalculator(self.timeForComputing)
   
   self.observationHistory.append(gameState)
   
   myState = gameState.getAgentState(self.index)
   myPos = myState.getPosition()
   if myPos != nearestPoint(myPos): 
     # We're halfway from one position to the next
     return gameState.getLegalActions(self.index)[0]
   else:
     return self.chooseAction(gameState)
    def getAction(self, gameState):
        """
    Calls chooseAction on a grid position, but continues on half positions.
    
    This method also cedes some processing time to the distance calculator, 
    which computes the shortest path distance between all pairs of points.
    """
        # Give some time to the distance calculator thread
        distanceCalculator.waitOnDistanceCalculator(self.timeForComputing)

        self.observationHistory.append(gameState)

        myState = gameState.getAgentState(self.index)
        myPos = myState.getPosition()
        if myPos != nearestPoint(myPos):
            # We're halfway from one position to the next
            return gameState.getLegalActions(self.index)[0]
        else:
            return self.chooseAction(gameState)
    def chooseAction(self, gameState):
        distanceCalculator.waitOnDistanceCalculator(0.5)
        myState = gameState.getAgentState(self.index)
        myPos = myState.getPosition()
        legal = gameState.getLegalActions(self.index)
        distributions = self.particlefilter.getEnemyPositionDistributions(
            gameState, myPos)
        modes = []
        for dist in distributions:
            if len(dist.keys()) > 0:
                modes.append(dist.argMax())
        if len(modes) == 0: return random.choice(legal)

        options = []
        for l in legal:
            succ = gameState.generateSuccessor(self.index, l)
            closestMode = min(
                [self.distancer.getDistance(myPos, mode) for mode in modes])
            options.append((closestMode, l))
        return min(options)[1]