Module pacai.agents.capture.capture
Classes
class CaptureAgent (index, timeForComputing=0.1, **kwargs)
-
A base class for capture agents. This class has some helper methods that students may find useful.
The recommended way of setting up a capture agent is just to extend this class and implement
CaptureAgent.chooseAction()
.Expand source code
class CaptureAgent(BaseAgent): """ A base class for capture agents. This class has some helper methods that students may find useful. The recommended way of setting up a capture agent is just to extend this class and implement `CaptureAgent.chooseAction`. """ def __init__(self, index, timeForComputing = 0.1, **kwargs): super().__init__(index, **kwargs) # Whether or not you're on the red team self.red = None # Agent objects controlling you and your teammates self.agentsOnTeam = None # Maze distance calculator self.distancer = None # A history of observations self.observationHistory = [] # Time to spend each turn on computing maze distances self.timeForComputing = timeForComputing def registerInitialState(self, gameState): """ This method handles the initial setup of the agent and populates useful fields, such as the team the agent is on and the `pacai.core.distanceCalculator.Distancer`. """ self.red = gameState.isOnRedTeam(self.index) self.distancer = distanceCalculator.Distancer(gameState.getInitialLayout()) self.distancer.getMazeDistances() def final(self, gameState): self.observationHistory = [] def registerTeam(self, agentsOnTeam): """ Fills the self.agentsOnTeam field with a list of the indices of the agents on your team. """ self.agentsOnTeam = agentsOnTeam def getAction(self, gameState): """ Calls `CaptureAgent.chooseAction` on a grid position, but continues on partial positions. If you subclass `CaptureAgent`, you shouldn't need to override this method. It takes care of appending the current state on to your observation history (so you have a record of the game states of the game) and will call your `CaptureAgent.chooseAction` method if you're in a proper state. """ self.observationHistory.append(gameState) myState = gameState.getAgentState(self.index) myPos = myState.getPosition() if (myPos != util.nearestPoint(myPos)): # We're halfway from one position to the next. return gameState.getLegalActions(self.index)[0] else: return self.chooseAction(gameState) @abc.abstractmethod def chooseAction(self, gameState): """ Override this method to make a good agent. It should return a legal action within the time limit (otherwise a random legal action will be chosen for you). """ pass def getFood(self, gameState): """ Returns the food you're meant to eat. This is in the form of a `pacai.core.grid.Grid` where `m[x][y] = True` if there is food you can eat (based on your team) in that square. """ if (self.red): return gameState.getBlueFood() else: return gameState.getRedFood() def getFoodYouAreDefending(self, gameState): """ Returns the food you're meant to protect (i.e., that your opponent is supposed to eat). This is in the form of a `pacai.core.grid.Grid` where `m[x][y] = True` if there is food at (x, y) that your opponent can eat. """ if (self.red): return gameState.getRedFood() else: return gameState.getBlueFood() def getCapsules(self, gameState): if (self.red): return gameState.getBlueCapsules() else: return gameState.getRedCapsules() def getCapsulesYouAreDefending(self, gameState): if (self.red): return gameState.getRedCapsules() else: return gameState.getBlueCapsules() def getOpponents(self, gameState): """ Returns agent indices of your opponents. This is the list of the numbers of the agents (e.g., red might be 1, 3, 5) """ if self.red: return gameState.getBlueTeamIndices() else: return gameState.getRedTeamIndices() def getTeam(self, gameState): """ 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) """ if (self.red): return gameState.getRedTeamIndices() else: return gameState.getBlueTeamIndices() def getScore(self, gameState): """ 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 number is negative if you're losing. """ if (self.red): return gameState.getScore() else: return gameState.getScore() * -1 def getMazeDistance(self, pos1, pos2): """ Returns the distance between two points using the builtin distancer. """ return self.distancer.getDistance(pos1, pos2) def getPreviousObservation(self): """ Returns the `pacai.core.gamestate.AbstractGameState` object corresponding to the last state this agent saw. That is the observed state of the game last time this agent moved, this may not include all of your opponent's agent locations exactly. """ if (len(self.observationHistory) <= 1): return None return self.observationHistory[-2] def getCurrentObservation(self): """ Returns the GameState object corresponding this agent's current observation (the observed state of the game - this may not include all of your opponent's agent locations exactly). Returns the `pacai.core.gamestate.AbstractGameState` object corresponding to this agent's current observation. That is the observed state of the game last time this agent moved, this may not include all of your opponent's agent locations exactly. """ if (len(self.observationHistory) == 0): return None return self.observationHistory[-1]
Ancestors
- BaseAgent
- abc.ABC
Subclasses
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 chooseAction(self, gameState)
-
Override this method to make a good agent. It should return a legal action within the time limit (otherwise a random legal action will be chosen for you).
def final(self, gameState)
-
Inherited from:
BaseAgent
.final
Inform the agent about the result of a game.
def getAction(self, gameState)
-
Calls
CaptureAgent.chooseAction()
on a grid position, but continues on partial positions. If you subclassCaptureAgent
, you shouldn't need to override this method. It takes care of appending the current state on to your observation history (so you have a record of the game states of the game) and will call yourCaptureAgent.chooseAction()
method if you're in a proper state. def getCapsules(self, gameState)
def getCapsulesYouAreDefending(self, gameState)
def getCurrentObservation(self)
-
Returns the GameState object corresponding this agent's current observation (the observed state of the game - this may not include all of your opponent's agent locations exactly).
Returns the
AbstractGameState
object corresponding to this agent's current observation. That is the observed state of the game last time this agent moved, this may not include all of your opponent's agent locations exactly. def getFood(self, gameState)
-
Returns the food you're meant to eat. This is in the form of a
Grid
wherem[x][y] = True
if there is food you can eat (based on your team) in that square. def getFoodYouAreDefending(self, gameState)
-
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
wherem[x][y] = True
if there is food at (x, y) that your opponent can eat. def getMazeDistance(self, pos1, pos2)
-
Returns the distance between two points using the builtin distancer.
def getOpponents(self, gameState)
-
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)
-
Returns the
AbstractGameState
object corresponding to the last state this agent saw. That is the observed state of the game last time this agent moved, this may not include all of your opponent's agent locations exactly. def getScore(self, gameState)
-
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 number is negative if you're losing.
def getTeam(self, gameState)
-
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 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, gameState)
-
This method handles the initial setup of the agent and populates useful fields, such as the team the agent is on and the
Distancer
. def registerTeam(self, agentsOnTeam)
-
Fills the self.agentsOnTeam field with a list of the indices of the agents on your team.