bustersAgents

.py

School

University of California, Berkeley *

*We aren’t endorsed by this school

Course

188

Subject

Computer Science

Date

Apr 26, 2024

Type

py

Pages

3

Uploaded by ElderMongoose4017 on coursehero.com

# bustersAgents.py # ---------------- # Licensing Information: You are free to use or extend these projects for # educational purposes provided that (1) you do not distribute or publish # solutions, (2) you retain this notice, and (3) you provide clear # attribution to UC Berkeley, including a link to http://ai.berkeley.edu. # # Attribution Information: The Pacman AI projects were developed at UC Berkeley. # The core projects and autograders were primarily created by John DeNero # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu). # Student side autograding was added by Brad Miller, Nick Hay, and # Pieter Abbeel (pabbeel@cs.berkeley.edu). import util from util import raiseNotDefined from game import Agent from game import Directions from keyboardAgents import KeyboardAgent import inference import busters class NullGraphics: "Placeholder for graphics" def initialize(self, state, isBlue = False): pass def update(self, state): pass def pause(self): pass def draw(self, state): pass def updateDistributions(self, dist): pass def finish(self): pass class KeyboardInference(inference.InferenceModule): """ Basic inference module for use with the keyboard. """ def initializeUniformly(self, gameState): "Begin with a uniform distribution over ghost positions." self.beliefs = util.Counter() for p in self.legalPositions: self.beliefs[p] = 1.0 self.beliefs.normalize() def observeUpdate(self, observation, gameState): noisyDistance = observation pacmanPosition = gameState.getPacmanPosition() allPossible = util.Counter() for p in self.legalPositions: trueDistance = util.manhattanDistance(p, pacmanPosition) if noisyDistance != None and \ busters.getObservationProbability(noisyDistance, trueDistance) > 0: allPossible[p] = 1.0 allPossible.normalize() self.beliefs = allPossible
def elapseTime(self, gameState): pass def getBeliefDistribution(self): return self.beliefs class BustersAgent: "An agent that tracks and displays its beliefs about ghost positions." def __init__( self, index = 0, inference = "ExactInference", ghostAgents = None, observeEnable = True, elapseTimeEnable = True): try: inferenceType = util.lookup(inference, globals()) except Exception: inferenceType = util.lookup('inference.' + inference, globals()) self.inferenceModules = [inferenceType(a) for a in ghostAgents] self.observeEnable = observeEnable self.elapseTimeEnable = elapseTimeEnable def registerInitialState(self, gameState): "Initializes beliefs and inference modules" import __main__ self.display = __main__._display for inference in self.inferenceModules: inference.initialize(gameState) self.ghostBeliefs = [inf.getBeliefDistribution() for inf in self.inferenceModules] self.firstMove = True def observationFunction(self, gameState): "Removes the ghost states from the gameState" agents = gameState.data.agentStates gameState.data.agentStates = [agents[0]] + [None for i in range(1, len(agents))] return gameState def getAction(self, gameState): "Updates beliefs, then chooses an action based on updated beliefs." for index, inf in enumerate(self.inferenceModules): if not self.firstMove and self.elapseTimeEnable: inf.elapseTime(gameState) self.firstMove = False if self.observeEnable: inf.observe(gameState) self.ghostBeliefs[index] = inf.getBeliefDistribution() self.display.updateDistributions(self.ghostBeliefs) return self.chooseAction(gameState) def chooseAction(self, gameState): "By default, a BustersAgent just stops. This should be overridden." return Directions.STOP class BustersKeyboardAgent(BustersAgent, KeyboardAgent): "An agent controlled by the keyboard that displays beliefs about ghost positions." def __init__(self, index = 0, inference = "KeyboardInference", ghostAgents =
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help