Module pacai.agents.capture.defense

Expand source code
from pacai.agents.capture.reflex import ReflexCaptureAgent
from pacai.core.directions import Directions

class DefensiveReflexAgent(ReflexCaptureAgent):
    """
    A reflex agent that tries to keep its side Pacman-free.
    This is to give you an idea of what a defensive agent could be like.
    It is not the best or only way to make such an agent.
    """

    def __init__(self, index, **kwargs):
        super().__init__(index)

    def getFeatures(self, gameState, action):
        features = {}

        successor = self.getSuccessor(gameState, action)
        myState = successor.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 = [successor.getAgentState(i) for i in self.getOpponents(successor)]
        invaders = [a for a in enemies if a.isPacman() and a.getPosition() is not None]
        features['numInvaders'] = len(invaders)

        if (len(invaders) > 0):
            dists = [self.getMazeDistance(myPos, a.getPosition()) for a in invaders]
            features['invaderDistance'] = min(dists)

        if (action == Directions.STOP):
            features['stop'] = 1

        rev = Directions.REVERSE[gameState.getAgentState(self.index).getDirection()]
        if (action == rev):
            features['reverse'] = 1

        return features

    def getWeights(self, gameState, action):
        return {
            'numInvaders': -1000,
            'onDefense': 100,
            'invaderDistance': -10,
            'stop': -100,
            'reverse': -2
        }

Classes

class DefensiveReflexAgent (index, **kwargs)

A reflex agent that tries to keep its side Pacman-free. This is to give you an idea of what a defensive agent could be like. It is not the best or only way to make such an agent.

Expand source code
class DefensiveReflexAgent(ReflexCaptureAgent):
    """
    A reflex agent that tries to keep its side Pacman-free.
    This is to give you an idea of what a defensive agent could be like.
    It is not the best or only way to make such an agent.
    """

    def __init__(self, index, **kwargs):
        super().__init__(index)

    def getFeatures(self, gameState, action):
        features = {}

        successor = self.getSuccessor(gameState, action)
        myState = successor.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 = [successor.getAgentState(i) for i in self.getOpponents(successor)]
        invaders = [a for a in enemies if a.isPacman() and a.getPosition() is not None]
        features['numInvaders'] = len(invaders)

        if (len(invaders) > 0):
            dists = [self.getMazeDistance(myPos, a.getPosition()) for a in invaders]
            features['invaderDistance'] = min(dists)

        if (action == Directions.STOP):
            features['stop'] = 1

        rev = Directions.REVERSE[gameState.getAgentState(self.index).getDirection()]
        if (action == rev):
            features['reverse'] = 1

        return features

    def getWeights(self, gameState, action):
        return {
            'numInvaders': -1000,
            'onDefense': 100,
            'invaderDistance': -10,
            'stop': -100,
            'reverse': -2
        }

Ancestors

Static methods

def loadAgent(name, index, args={})

Inherited from: ReflexCaptureAgent.loadAgent

Load an agent with the given class name. The name can be fully qualified or just the bare class name. If the bare name is given, the class should …

Methods

def chooseAction(self, gameState)

Inherited from: ReflexCaptureAgent.chooseAction

Picks among the actions with the highest return from ReflexCaptureAgent.evaluate.

def evaluate(self, gameState, action)

Inherited from: ReflexCaptureAgent.evaluate

Computes a linear combination of features and feature weights.

def final(self, gameState)

Inherited from: ReflexCaptureAgent.final

Inform the agent about the result of a game.

def getAction(self, gameState)

Inherited from: ReflexCaptureAgent.getAction

Calls CaptureAgent.chooseAction on a grid position, but continues on partial positions. If you subclass CaptureAgent, you shouldn't need to …

def getCurrentObservation(self)

Inherited from: ReflexCaptureAgent.getCurrentObservation

Returns the GameState object corresponding this agent's current observation (the observed state of the game - this may not include all of your …

def getFeatures(self, gameState, action)

Inherited from: ReflexCaptureAgent.getFeatures

Returns a dict of features for the state. The keys match up with the return from ReflexCaptureAgent.getWeights.

Expand source code
def getFeatures(self, gameState, action):
    features = {}

    successor = self.getSuccessor(gameState, action)
    myState = successor.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 = [successor.getAgentState(i) for i in self.getOpponents(successor)]
    invaders = [a for a in enemies if a.isPacman() and a.getPosition() is not None]
    features['numInvaders'] = len(invaders)

    if (len(invaders) > 0):
        dists = [self.getMazeDistance(myPos, a.getPosition()) for a in invaders]
        features['invaderDistance'] = min(dists)

    if (action == Directions.STOP):
        features['stop'] = 1

    rev = Directions.REVERSE[gameState.getAgentState(self.index).getDirection()]
    if (action == rev):
        features['reverse'] = 1

    return features
def getFood(self, gameState)

Inherited from: ReflexCaptureAgent.getFood

Returns the food you're meant to eat. This is in the form of a Grid where m[x][y] = True if there is food you can eat (based on …

def getFoodYouAreDefending(self, gameState)

Inherited from: ReflexCaptureAgent.getFoodYouAreDefending

Returns the food you're meant to protect (i.e., that your opponent is supposed to eat). This is in the form of a Grid where `m[x][y] …

def getMazeDistance(self, pos1, pos2)

Inherited from: ReflexCaptureAgent.getMazeDistance

Returns the distance between two points using the builtin distancer.

def getOpponents(self, gameState)

Inherited from: ReflexCaptureAgent.getOpponents

Returns agent indices of your opponents. This is the list of the numbers of the agents (e.g., red might be 1, 3, 5)

def getPreviousObservation(self)

Inherited from: ReflexCaptureAgent.getPreviousObservation

Returns the AbstractGameState object corresponding to the last state this agent saw. That is the observed state of the game …

def getScore(self, gameState)

Inherited from: ReflexCaptureAgent.getScore

Returns how much you are beating the other team by in the form of a number that is the difference between your score and the opponents score. This …

def getSuccessor(self, gameState, action)

Inherited from: ReflexCaptureAgent.getSuccessor

Finds the next successor which is a grid position (location tuple).

def getTeam(self, gameState)

Inherited from: ReflexCaptureAgent.getTeam

Returns agent indices of your team. This is the list of the numbers of the agents (e.g., red might be the list of 1,3,5)

def getWeights(self, gameState, action)

Inherited from: ReflexCaptureAgent.getWeights

Returns a dict of weights for the state. The keys match up with the return from ReflexCaptureAgent.getFeatures.

Expand source code
def getWeights(self, gameState, action):
    return {
        'numInvaders': -1000,
        'onDefense': 100,
        'invaderDistance': -10,
        'stop': -100,
        'reverse': -2
    }
def observationFunction(self, state)

Inherited from: ReflexCaptureAgent.observationFunction

Make an observation on the state of the game. Called once for each round of the game.

def registerInitialState(self, gameState)

Inherited from: ReflexCaptureAgent.registerInitialState

This method handles the initial setup of the agent and populates useful fields, such as the team the agent is on and the …

def registerTeam(self, agentsOnTeam)

Inherited from: ReflexCaptureAgent.registerTeam

Fills the self.agentsOnTeam field with a list of the indices of the agents on your team.