Module pacai.agents.greedy

Expand source code
import random

from pacai.agents.base import BaseAgent
from pacai.core.directions import Directions
from pacai.util import reflection

class GreedyAgent(BaseAgent):
    """
    An agent that greedily takes the available move with the best score at the time.
    """

    def __init__(self, index, evalFn = "pacai.core.eval.score", **kwargs):
        super().__init__(index, **kwargs)

        self.evaluationFunction = reflection.qualifiedImport(evalFn)
        assert(self.evaluationFunction is not None)

    def getAction(self, state):
        # Generate candidate actions
        legal = state.getLegalPacmanActions()
        if (Directions.STOP in legal):
            legal.remove(Directions.STOP)

        successors = [(state.generateSuccessor(0, action), action) for action in legal]
        scored = [(self.evaluationFunction(state), action) for state, action in successors]
        bestScore = max(scored)[0]
        bestActions = [pair[1] for pair in scored if pair[0] == bestScore]

        return random.choice(bestActions)

Classes

class GreedyAgent (index, evalFn='pacai.core.eval.score', **kwargs)

An agent that greedily takes the available move with the best score at the time.

Expand source code
class GreedyAgent(BaseAgent):
    """
    An agent that greedily takes the available move with the best score at the time.
    """

    def __init__(self, index, evalFn = "pacai.core.eval.score", **kwargs):
        super().__init__(index, **kwargs)

        self.evaluationFunction = reflection.qualifiedImport(evalFn)
        assert(self.evaluationFunction is not None)

    def getAction(self, state):
        # Generate candidate actions
        legal = state.getLegalPacmanActions()
        if (Directions.STOP in legal):
            legal.remove(Directions.STOP)

        successors = [(state.generateSuccessor(0, action), action) for action in legal]
        scored = [(self.evaluationFunction(state), action) for state, action in successors]
        bestScore = max(scored)[0]
        bestActions = [pair[1] for pair in scored if pair[0] == bestScore]

        return random.choice(bestActions)

Ancestors

Static methods

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

Inherited from: BaseAgent.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 final(self, state)

Inherited from: BaseAgent.final

Inform the agent about the result of a game.

def getAction(self, state)

Inherited from: BaseAgent.getAction

The BaseAgent will receive an AbstractGameState, and must return an action from Directions.

Expand source code
def getAction(self, state):
    # Generate candidate actions
    legal = state.getLegalPacmanActions()
    if (Directions.STOP in legal):
        legal.remove(Directions.STOP)

    successors = [(state.generateSuccessor(0, action), action) for action in legal]
    scored = [(self.evaluationFunction(state), action) for state, action in successors]
    bestScore = max(scored)[0]
    bestActions = [pair[1] for pair in scored if pair[0] == bestScore]

    return random.choice(bestActions)
def observationFunction(self, state)

Inherited from: BaseAgent.observationFunction

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

def registerInitialState(self, state)

Inherited from: BaseAgent.registerInitialState

Inspect the starting state.