Module pacai.agents.capture.offense

Expand source code
from pacai.agents.capture.reflex import ReflexCaptureAgent

class OffensiveReflexAgent(ReflexCaptureAgent):
    """
    A reflex agent that seeks food.
    This agent will give you an idea of what an offensive agent might look like,
    but it is by no means the best or only way to build an offensive agent.
    """

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

    def getFeatures(self, gameState, action):
        features = {}
        successor = self.getSuccessor(gameState, action)
        features['successorScore'] = self.getScore(successor)

        # Compute distance to the nearest food.
        foodList = self.getFood(successor).asList()

        # This should always be True, but better safe than sorry.
        if (len(foodList) > 0):
            myPos = successor.getAgentState(self.index).getPosition()
            minDistance = min([self.getMazeDistance(myPos, food) for food in foodList])
            features['distanceToFood'] = minDistance

        return features

    def getWeights(self, gameState, action):
        return {
            'successorScore': 100,
            'distanceToFood': -1
        }

Classes

class OffensiveReflexAgent (index, **kwargs)

A reflex agent that seeks food. This agent will give you an idea of what an offensive agent might look like, but it is by no means the best or only way to build an offensive agent.

Expand source code
class OffensiveReflexAgent(ReflexCaptureAgent):
    """
    A reflex agent that seeks food.
    This agent will give you an idea of what an offensive agent might look like,
    but it is by no means the best or only way to build an offensive agent.
    """

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

    def getFeatures(self, gameState, action):
        features = {}
        successor = self.getSuccessor(gameState, action)
        features['successorScore'] = self.getScore(successor)

        # Compute distance to the nearest food.
        foodList = self.getFood(successor).asList()

        # This should always be True, but better safe than sorry.
        if (len(foodList) > 0):
            myPos = successor.getAgentState(self.index).getPosition()
            minDistance = min([self.getMazeDistance(myPos, food) for food in foodList])
            features['distanceToFood'] = minDistance

        return features

    def getWeights(self, gameState, action):
        return {
            'successorScore': 100,
            'distanceToFood': -1
        }

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)
    features['successorScore'] = self.getScore(successor)

    # Compute distance to the nearest food.
    foodList = self.getFood(successor).asList()

    # This should always be True, but better safe than sorry.
    if (len(foodList) > 0):
        myPos = successor.getAgentState(self.index).getPosition()
        minDistance = min([self.getMazeDistance(myPos, food) for food in foodList])
        features['distanceToFood'] = minDistance

    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 {
        'successorScore': 100,
        'distanceToFood': -1
    }
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.