Module pacai.student.multiagents
Expand source code
import random
from pacai.agents.base import BaseAgent
from pacai.agents.search.multiagent import MultiAgentSearchAgent
class ReflexAgent(BaseAgent):
"""
A reflex agent chooses an action at each choice point by examining
its alternatives via a state evaluation function.
The code below is provided as a guide.
You are welcome to change it in any way you see fit,
so long as you don't touch the method headers.
"""
def __init__(self, index, **kwargs):
super().__init__(index, **kwargs)
def getAction(self, gameState):
"""
You do not need to change this method, but you're welcome to.
`ReflexAgent.getAction` chooses among the best options according to the evaluation function.
Just like in the previous project, this method takes a
`pacai.core.gamestate.AbstractGameState` and returns some value from
`pacai.core.directions.Directions`.
"""
# Collect legal moves.
legalMoves = gameState.getLegalActions()
# Choose one of the best actions.
scores = [self.evaluationFunction(gameState, action) for action in legalMoves]
bestScore = max(scores)
bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore]
chosenIndex = random.choice(bestIndices) # Pick randomly among the best.
return legalMoves[chosenIndex]
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current `pacai.bin.pacman.PacmanGameState`
and an action, and returns a number, where higher numbers are better.
Make sure to understand the range of different values before you combine them
in your evaluation function.
"""
successorGameState = currentGameState.generatePacmanSuccessor(action)
# Useful information you can extract.
# newPosition = successorGameState.getPacmanPosition()
# oldFood = currentGameState.getFood()
# newGhostStates = successorGameState.getGhostStates()
# newScaredTimes = [ghostState.getScaredTimer() for ghostState in newGhostStates]
# *** Your Code Here ***
return successorGameState.getScore()
class MinimaxAgent(MultiAgentSearchAgent):
"""
A minimax agent.
Here are some method calls that might be useful when implementing minimax.
`pacai.core.gamestate.AbstractGameState.getNumAgents()`:
Get the total number of agents in the game
`pacai.core.gamestate.AbstractGameState.getLegalActions`:
Returns a list of legal actions for an agent.
Pacman is always at index 0, and ghosts are >= 1.
`pacai.core.gamestate.AbstractGameState.generateSuccessor`:
Get the successor game state after an agent takes an action.
`pacai.core.directions.Directions.STOP`:
The stop direction, which is always legal, but you may not want to include in your search.
Method to Implement:
`pacai.agents.base.BaseAgent.getAction`:
Returns the minimax action from the current gameState using
`pacai.agents.search.multiagent.MultiAgentSearchAgent.getTreeDepth`
and `pacai.agents.search.multiagent.MultiAgentSearchAgent.getEvaluationFunction`.
"""
def __init__(self, index, **kwargs):
super().__init__(index, **kwargs)
class AlphaBetaAgent(MultiAgentSearchAgent):
"""
A minimax agent with alpha-beta pruning.
Method to Implement:
`pacai.agents.base.BaseAgent.getAction`:
Returns the minimax action from the current gameState using
`pacai.agents.search.multiagent.MultiAgentSearchAgent.getTreeDepth`
and `pacai.agents.search.multiagent.MultiAgentSearchAgent.getEvaluationFunction`.
"""
def __init__(self, index, **kwargs):
super().__init__(index, **kwargs)
class ExpectimaxAgent(MultiAgentSearchAgent):
"""
An expectimax agent.
All ghosts should be modeled as choosing uniformly at random from their legal moves.
Method to Implement:
`pacai.agents.base.BaseAgent.getAction`:
Returns the expectimax action from the current gameState using
`pacai.agents.search.multiagent.MultiAgentSearchAgent.getTreeDepth`
and `pacai.agents.search.multiagent.MultiAgentSearchAgent.getEvaluationFunction`.
"""
def __init__(self, index, **kwargs):
super().__init__(index, **kwargs)
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable evaluation function.
DESCRIPTION: <write something here so we know what you did>
"""
return currentGameState.getScore()
class ContestAgent(MultiAgentSearchAgent):
"""
Your agent for the mini-contest.
You can use any method you want and search to any depth you want.
Just remember that the mini-contest is timed, so you have to trade off speed and computation.
Ghosts don't behave randomly anymore, but they aren't perfect either -- they'll usually
just make a beeline straight towards Pacman (or away if they're scared!)
Method to Implement:
`pacai.agents.base.BaseAgent.getAction`
"""
def __init__(self, index, **kwargs):
super().__init__(index, **kwargs)
Functions
def betterEvaluationFunction(currentGameState)
-
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable evaluation function.
DESCRIPTION:
Expand source code
def betterEvaluationFunction(currentGameState): """ Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable evaluation function. DESCRIPTION: <write something here so we know what you did> """ return currentGameState.getScore()
Classes
class AlphaBetaAgent (index, **kwargs)
-
A minimax agent with alpha-beta pruning.
Method to Implement:
BaseAgent.getAction()
: Returns the minimax action from the current gameState usingMultiAgentSearchAgent.getTreeDepth()
andMultiAgentSearchAgent.getEvaluationFunction()
.Expand source code
class AlphaBetaAgent(MultiAgentSearchAgent): """ A minimax agent with alpha-beta pruning. Method to Implement: `pacai.agents.base.BaseAgent.getAction`: Returns the minimax action from the current gameState using `pacai.agents.search.multiagent.MultiAgentSearchAgent.getTreeDepth` and `pacai.agents.search.multiagent.MultiAgentSearchAgent.getEvaluationFunction`. """ def __init__(self, index, **kwargs): super().__init__(index, **kwargs)
Ancestors
- MultiAgentSearchAgent
- BaseAgent
- abc.ABC
Static methods
def loadAgent(name, index, args={})
-
Inherited from:
MultiAgentSearchAgent
.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:
MultiAgentSearchAgent
.final
Inform the agent about the result of a game.
def getAction(self, state)
-
Inherited from:
MultiAgentSearchAgent
.getAction
The BaseAgent will receive an
AbstractGameState
, and must return an action fromDirections
. def observationFunction(self, state)
-
Inherited from:
MultiAgentSearchAgent
.observationFunction
Make an observation on the state of the game. Called once for each round of the game.
def registerInitialState(self, state)
-
Inherited from:
MultiAgentSearchAgent
.registerInitialState
Inspect the starting state.
class ContestAgent (index, **kwargs)
-
Your agent for the mini-contest.
You can use any method you want and search to any depth you want. Just remember that the mini-contest is timed, so you have to trade off speed and computation.
Ghosts don't behave randomly anymore, but they aren't perfect either – they'll usually just make a beeline straight towards Pacman (or away if they're scared!)
Method to Implement:
Expand source code
class ContestAgent(MultiAgentSearchAgent): """ Your agent for the mini-contest. You can use any method you want and search to any depth you want. Just remember that the mini-contest is timed, so you have to trade off speed and computation. Ghosts don't behave randomly anymore, but they aren't perfect either -- they'll usually just make a beeline straight towards Pacman (or away if they're scared!) Method to Implement: `pacai.agents.base.BaseAgent.getAction` """ def __init__(self, index, **kwargs): super().__init__(index, **kwargs)
Ancestors
- MultiAgentSearchAgent
- BaseAgent
- abc.ABC
Static methods
def loadAgent(name, index, args={})
-
Inherited from:
MultiAgentSearchAgent
.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:
MultiAgentSearchAgent
.final
Inform the agent about the result of a game.
def getAction(self, state)
-
Inherited from:
MultiAgentSearchAgent
.getAction
The BaseAgent will receive an
AbstractGameState
, and must return an action fromDirections
. def observationFunction(self, state)
-
Inherited from:
MultiAgentSearchAgent
.observationFunction
Make an observation on the state of the game. Called once for each round of the game.
def registerInitialState(self, state)
-
Inherited from:
MultiAgentSearchAgent
.registerInitialState
Inspect the starting state.
class ExpectimaxAgent (index, **kwargs)
-
An expectimax agent.
All ghosts should be modeled as choosing uniformly at random from their legal moves.
Method to Implement:
BaseAgent.getAction()
: Returns the expectimax action from the current gameState usingMultiAgentSearchAgent.getTreeDepth()
andMultiAgentSearchAgent.getEvaluationFunction()
.Expand source code
class ExpectimaxAgent(MultiAgentSearchAgent): """ An expectimax agent. All ghosts should be modeled as choosing uniformly at random from their legal moves. Method to Implement: `pacai.agents.base.BaseAgent.getAction`: Returns the expectimax action from the current gameState using `pacai.agents.search.multiagent.MultiAgentSearchAgent.getTreeDepth` and `pacai.agents.search.multiagent.MultiAgentSearchAgent.getEvaluationFunction`. """ def __init__(self, index, **kwargs): super().__init__(index, **kwargs)
Ancestors
- MultiAgentSearchAgent
- BaseAgent
- abc.ABC
Static methods
def loadAgent(name, index, args={})
-
Inherited from:
MultiAgentSearchAgent
.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:
MultiAgentSearchAgent
.final
Inform the agent about the result of a game.
def getAction(self, state)
-
Inherited from:
MultiAgentSearchAgent
.getAction
The BaseAgent will receive an
AbstractGameState
, and must return an action fromDirections
. def observationFunction(self, state)
-
Inherited from:
MultiAgentSearchAgent
.observationFunction
Make an observation on the state of the game. Called once for each round of the game.
def registerInitialState(self, state)
-
Inherited from:
MultiAgentSearchAgent
.registerInitialState
Inspect the starting state.
class MinimaxAgent (index, **kwargs)
-
A minimax agent.
Here are some method calls that might be useful when implementing minimax.
AbstractGameState.getNumAgents()
: Get the total number of agents in the gameAbstractGameState.getLegalActions()
: Returns a list of legal actions for an agent. Pacman is always at index 0, and ghosts are >= 1.AbstractGameState.generateSuccessor()
: Get the successor game state after an agent takes an action.Directions.STOP
: The stop direction, which is always legal, but you may not want to include in your search.Method to Implement:
BaseAgent.getAction()
: Returns the minimax action from the current gameState usingMultiAgentSearchAgent.getTreeDepth()
andMultiAgentSearchAgent.getEvaluationFunction()
.Expand source code
class MinimaxAgent(MultiAgentSearchAgent): """ A minimax agent. Here are some method calls that might be useful when implementing minimax. `pacai.core.gamestate.AbstractGameState.getNumAgents()`: Get the total number of agents in the game `pacai.core.gamestate.AbstractGameState.getLegalActions`: Returns a list of legal actions for an agent. Pacman is always at index 0, and ghosts are >= 1. `pacai.core.gamestate.AbstractGameState.generateSuccessor`: Get the successor game state after an agent takes an action. `pacai.core.directions.Directions.STOP`: The stop direction, which is always legal, but you may not want to include in your search. Method to Implement: `pacai.agents.base.BaseAgent.getAction`: Returns the minimax action from the current gameState using `pacai.agents.search.multiagent.MultiAgentSearchAgent.getTreeDepth` and `pacai.agents.search.multiagent.MultiAgentSearchAgent.getEvaluationFunction`. """ def __init__(self, index, **kwargs): super().__init__(index, **kwargs)
Ancestors
- MultiAgentSearchAgent
- BaseAgent
- abc.ABC
Static methods
def loadAgent(name, index, args={})
-
Inherited from:
MultiAgentSearchAgent
.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:
MultiAgentSearchAgent
.final
Inform the agent about the result of a game.
def getAction(self, state)
-
Inherited from:
MultiAgentSearchAgent
.getAction
The BaseAgent will receive an
AbstractGameState
, and must return an action fromDirections
. def observationFunction(self, state)
-
Inherited from:
MultiAgentSearchAgent
.observationFunction
Make an observation on the state of the game. Called once for each round of the game.
def registerInitialState(self, state)
-
Inherited from:
MultiAgentSearchAgent
.registerInitialState
Inspect the starting state.
class ReflexAgent (index, **kwargs)
-
A reflex agent chooses an action at each choice point by examining its alternatives via a state evaluation function.
The code below is provided as a guide. You are welcome to change it in any way you see fit, so long as you don't touch the method headers.
Expand source code
class ReflexAgent(BaseAgent): """ A reflex agent chooses an action at each choice point by examining its alternatives via a state evaluation function. The code below is provided as a guide. You are welcome to change it in any way you see fit, so long as you don't touch the method headers. """ def __init__(self, index, **kwargs): super().__init__(index, **kwargs) def getAction(self, gameState): """ You do not need to change this method, but you're welcome to. `ReflexAgent.getAction` chooses among the best options according to the evaluation function. Just like in the previous project, this method takes a `pacai.core.gamestate.AbstractGameState` and returns some value from `pacai.core.directions.Directions`. """ # Collect legal moves. legalMoves = gameState.getLegalActions() # Choose one of the best actions. scores = [self.evaluationFunction(gameState, action) for action in legalMoves] bestScore = max(scores) bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore] chosenIndex = random.choice(bestIndices) # Pick randomly among the best. return legalMoves[chosenIndex] def evaluationFunction(self, currentGameState, action): """ Design a better evaluation function here. The evaluation function takes in the current `pacai.bin.pacman.PacmanGameState` and an action, and returns a number, where higher numbers are better. Make sure to understand the range of different values before you combine them in your evaluation function. """ successorGameState = currentGameState.generatePacmanSuccessor(action) # Useful information you can extract. # newPosition = successorGameState.getPacmanPosition() # oldFood = currentGameState.getFood() # newGhostStates = successorGameState.getGhostStates() # newScaredTimes = [ghostState.getScaredTimer() for ghostState in newGhostStates] # *** Your Code Here *** return successorGameState.getScore()
Ancestors
- BaseAgent
- abc.ABC
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 evaluationFunction(self, currentGameState, action)
-
Design a better evaluation function here.
The evaluation function takes in the current
PacmanGameState
and an action, and returns a number, where higher numbers are better. Make sure to understand the range of different values before you combine them in your evaluation function.Expand source code
def evaluationFunction(self, currentGameState, action): """ Design a better evaluation function here. The evaluation function takes in the current `pacai.bin.pacman.PacmanGameState` and an action, and returns a number, where higher numbers are better. Make sure to understand the range of different values before you combine them in your evaluation function. """ successorGameState = currentGameState.generatePacmanSuccessor(action) # Useful information you can extract. # newPosition = successorGameState.getPacmanPosition() # oldFood = currentGameState.getFood() # newGhostStates = successorGameState.getGhostStates() # newScaredTimes = [ghostState.getScaredTimer() for ghostState in newGhostStates] # *** Your Code Here *** return successorGameState.getScore()
def final(self, state)
-
Inherited from:
BaseAgent
.final
Inform the agent about the result of a game.
def getAction(self, gameState)
-
You do not need to change this method, but you're welcome to.
ReflexAgent.getAction()
chooses among the best options according to the evaluation function.Just like in the previous project, this method takes a
AbstractGameState
and returns some value fromDirections
.Expand source code
def getAction(self, gameState): """ You do not need to change this method, but you're welcome to. `ReflexAgent.getAction` chooses among the best options according to the evaluation function. Just like in the previous project, this method takes a `pacai.core.gamestate.AbstractGameState` and returns some value from `pacai.core.directions.Directions`. """ # Collect legal moves. legalMoves = gameState.getLegalActions() # Choose one of the best actions. scores = [self.evaluationFunction(gameState, action) for action in legalMoves] bestScore = max(scores) bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore] chosenIndex = random.choice(bestIndices) # Pick randomly among the best. return legalMoves[chosenIndex]
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.