all changes for all parts
This commit is contained in:
@@ -144,3 +144,24 @@ class GreedyBustersAgent(BustersAgent):
|
||||
[beliefs for i, beliefs in enumerate(self.ghostBeliefs)
|
||||
if livingGhosts[i+1]]
|
||||
"*** YOUR CODE HERE ***"
|
||||
|
||||
max_level = []
|
||||
|
||||
for belief in livingGhostPositionDistributions:
|
||||
max_level.append(belief.argMax())
|
||||
|
||||
goalProbability, goalCoordinate = 0, None
|
||||
for index, coordinate in enumerate(max_level):
|
||||
# checking goal
|
||||
if livingGhostPositionDistributions[index][coordinate] >= goalProbability:
|
||||
goalCoordinate = coordinate
|
||||
goalProbability = livingGhostPositionDistributions[index][coordinate]
|
||||
|
||||
prs = []
|
||||
|
||||
# checking all acitons
|
||||
for action in legal:
|
||||
nextLocation = Actions.getSuccessor(pacmanPosition, action)
|
||||
prs.append((self.distancer.getDistance(nextLocation, goalCoordinate), action))
|
||||
|
||||
return min(prs)[1]
|
||||
|
||||
+172
-20
@@ -25,6 +25,7 @@ class DiscreteDistribution(dict):
|
||||
A DiscreteDistribution models belief distributions and weight distributions
|
||||
over a finite set of discrete keys.
|
||||
"""
|
||||
|
||||
def __getitem__(self, key):
|
||||
self.setdefault(key, 0)
|
||||
return dict.__getitem__(self, key)
|
||||
@@ -75,7 +76,14 @@ class DiscreteDistribution(dict):
|
||||
{}
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
total_calc = float(self.total())
|
||||
|
||||
# need to stop and return func since landed on ghost
|
||||
if total_calc == 0:
|
||||
return
|
||||
|
||||
for key in self.keys():
|
||||
self[key] = self[key] / total_calc
|
||||
|
||||
def sample(self):
|
||||
"""
|
||||
@@ -99,7 +107,23 @@ class DiscreteDistribution(dict):
|
||||
0.0
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
# already normalized?
|
||||
if self.total() != 1:
|
||||
self.normalize()
|
||||
|
||||
items_sorted = sorted(self.items())
|
||||
|
||||
dist = [i[1] for i in items_sorted]
|
||||
values = [i[0] for i in items_sorted]
|
||||
|
||||
# incorporating the random as in spec
|
||||
choice = random.random()
|
||||
|
||||
total, i = dist[0], 0
|
||||
while choice > total:
|
||||
i += 1
|
||||
total += dist[i]
|
||||
return values[i]
|
||||
|
||||
|
||||
class InferenceModule:
|
||||
@@ -129,12 +153,13 @@ class InferenceModule:
|
||||
jail = self.getJailPosition(index)
|
||||
gameState = self.setGhostPositions(gameState, pos)
|
||||
pacmanPosition = gameState.getPacmanPosition()
|
||||
ghostPosition = gameState.getGhostPosition(index + 1) # The position you set
|
||||
ghostPosition = gameState.getGhostPosition(
|
||||
index + 1) # The position you set
|
||||
dist = DiscreteDistribution()
|
||||
if pacmanPosition == ghostPosition: # The ghost has been caught!
|
||||
dist[jail] = 1.0
|
||||
return dist
|
||||
pacmanSuccessorStates = game.Actions.getLegalNeighbors(pacmanPosition, \
|
||||
pacmanSuccessorStates = game.Actions.getLegalNeighbors(pacmanPosition,
|
||||
gameState.getWalls()) # Positions Pacman can move to
|
||||
if ghostPosition in pacmanSuccessorStates: # Ghost could get caught
|
||||
mult = 1.0 / float(len(pacmanSuccessorStates))
|
||||
@@ -143,11 +168,13 @@ class InferenceModule:
|
||||
mult = 0.0
|
||||
actionDist = agent.getDistribution(gameState)
|
||||
for action, prob in actionDist.items():
|
||||
successorPosition = game.Actions.getSuccessor(ghostPosition, action)
|
||||
successorPosition = game.Actions.getSuccessor(
|
||||
ghostPosition, action)
|
||||
if successorPosition in pacmanSuccessorStates: # Ghost could get caught
|
||||
denom = float(len(actionDist))
|
||||
dist[jail] += prob * (1.0 / denom) * (1.0 - mult)
|
||||
dist[successorPosition] = prob * ((denom - 1.0) / denom) * (1.0 - mult)
|
||||
dist[successorPosition] = prob * \
|
||||
((denom - 1.0) / denom) * (1.0 - mult)
|
||||
else:
|
||||
dist[successorPosition] = prob * (1.0 - mult)
|
||||
return dist
|
||||
@@ -169,7 +196,20 @@ class InferenceModule:
|
||||
Return the probability P(noisyDistance | pacmanPosition, ghostPosition).
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
# check if ghost in jail currently
|
||||
if jailPosition == ghostPosition:
|
||||
# remember need floats now
|
||||
if noisyDistance == None:
|
||||
return 1.0
|
||||
else:
|
||||
return 0.0
|
||||
|
||||
if noisyDistance == None:
|
||||
return 0.0
|
||||
|
||||
actual = manhattanDistance(pacmanPosition, ghostPosition)
|
||||
|
||||
return busters.getObservationProbability(noisyDistance, actual)
|
||||
|
||||
def setGhostPosition(self, gameState, ghostPosition, index):
|
||||
"""
|
||||
@@ -195,7 +235,8 @@ class InferenceModule:
|
||||
"""
|
||||
for index, pos in enumerate(ghostPositions):
|
||||
conf = game.Configuration(pos, game.Directions.STOP)
|
||||
gameState.data.agentStates[index + 1] = game.AgentState(conf, False)
|
||||
gameState.data.agentStates[index +
|
||||
1] = game.AgentState(conf, False)
|
||||
return gameState
|
||||
|
||||
def observe(self, gameState):
|
||||
@@ -212,7 +253,8 @@ class InferenceModule:
|
||||
"""
|
||||
Initialize beliefs to a uniform distribution over all legal positions.
|
||||
"""
|
||||
self.legalPositions = [p for p in gameState.getWalls().asList(False) if p[1] > 1]
|
||||
self.legalPositions = [
|
||||
p for p in gameState.getWalls().asList(False) if p[1] > 1]
|
||||
self.allPositions = self.legalPositions + [self.getJailPosition()]
|
||||
self.initializeUniformly(gameState)
|
||||
|
||||
@@ -251,6 +293,7 @@ class ExactInference(InferenceModule):
|
||||
The exact dynamic inference module should use forward algorithm updates to
|
||||
compute the exact belief function at each time step.
|
||||
"""
|
||||
|
||||
def initializeUniformly(self, gameState):
|
||||
"""
|
||||
Begin with a uniform distribution over legal ghost positions (i.e., not
|
||||
@@ -277,9 +320,20 @@ class ExactInference(InferenceModule):
|
||||
position is known.
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
distribution = DiscreteDistribution()
|
||||
# from input vars
|
||||
pacmanPosition = gameState.getPacmanPosition()
|
||||
|
||||
self.beliefs.normalize()
|
||||
jailPosition = self.getJailPosition()
|
||||
|
||||
for pos in self.allPositions:
|
||||
prob = self.getObservationProb(
|
||||
observation, pacmanPosition, pos, jailPosition)
|
||||
distribution[pos] = prob * self.beliefs[pos]
|
||||
|
||||
distribution.normalize()
|
||||
|
||||
self.beliefs = distribution
|
||||
|
||||
def elapseTime(self, gameState):
|
||||
"""
|
||||
@@ -291,7 +345,20 @@ class ExactInference(InferenceModule):
|
||||
current position is known.
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
import util
|
||||
|
||||
distribution = DiscreteDistribution()
|
||||
|
||||
# iterate all over pos
|
||||
for old_pos in self.allPositions:
|
||||
new_pos = self.getPositionDistribution(gameState, old_pos)
|
||||
# update keys
|
||||
old_prob = self.beliefs[old_pos]
|
||||
|
||||
for newPos in new_pos.keys():
|
||||
distribution[newPos] += old_prob * new_pos[newPos]
|
||||
|
||||
self.beliefs = distribution
|
||||
|
||||
def getBeliefDistribution(self):
|
||||
return self.beliefs
|
||||
@@ -301,6 +368,7 @@ class ParticleFilter(InferenceModule):
|
||||
"""
|
||||
A particle filter for approximately tracking a single ghost.
|
||||
"""
|
||||
|
||||
def __init__(self, ghostAgent, numParticles=300):
|
||||
InferenceModule.__init__(self, ghostAgent)
|
||||
self.setNumParticles(numParticles)
|
||||
@@ -318,7 +386,14 @@ class ParticleFilter(InferenceModule):
|
||||
"""
|
||||
self.particles = []
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
count = 0
|
||||
self.particles = []
|
||||
# going through all particles
|
||||
while count < self.numParticles:
|
||||
for position in self.legalPositions:
|
||||
if count < self.numParticles:
|
||||
self.particles.append(position)
|
||||
count += 1
|
||||
|
||||
def observeUpdate(self, observation, gameState):
|
||||
"""
|
||||
@@ -333,7 +408,25 @@ class ParticleFilter(InferenceModule):
|
||||
the DiscreteDistribution may be useful.
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
# get from given input
|
||||
pacmanPosition, jailPosition = gameState.getPacmanPosition(), self.getJailPosition()
|
||||
distribution = DiscreteDistribution()
|
||||
|
||||
for par in self.particles:
|
||||
prob = self.getObservationProb(
|
||||
observation, pacmanPosition, par, jailPosition)
|
||||
distribution[par] += prob
|
||||
|
||||
# check if not vialbe
|
||||
if distribution.total() == 0:
|
||||
self.initializeUniformly(gameState)
|
||||
else:
|
||||
distribution.normalize()
|
||||
self.beliefs = distribution
|
||||
|
||||
for x in range(self.numParticles):
|
||||
new_sample = distribution.sample()
|
||||
self.particles[x] = new_sample
|
||||
|
||||
def elapseTime(self, gameState):
|
||||
"""
|
||||
@@ -341,7 +434,20 @@ class ParticleFilter(InferenceModule):
|
||||
gameState.
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
import util
|
||||
|
||||
cache = {}
|
||||
# maybe use counter?
|
||||
for i in range(self.numParticles):
|
||||
particle = self.particles[i]
|
||||
|
||||
# check cases in spec
|
||||
if particle in cache:
|
||||
self.particles[i] = cache[particle].sample()
|
||||
else:
|
||||
dist = self.getPositionDistribution(gameState, particle)
|
||||
cache[particle] = dist
|
||||
self.particles[i] = dist.sample()
|
||||
|
||||
def getBeliefDistribution(self):
|
||||
"""
|
||||
@@ -352,7 +458,14 @@ class ParticleFilter(InferenceModule):
|
||||
This function should return a normalized distribution.
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
import util
|
||||
distribution = util.Counter()
|
||||
|
||||
for element in self.particles:
|
||||
distribution[element] += 1
|
||||
|
||||
distribution.normalize()
|
||||
return distribution
|
||||
|
||||
|
||||
class JointParticleFilter(ParticleFilter):
|
||||
@@ -360,6 +473,7 @@ class JointParticleFilter(ParticleFilter):
|
||||
JointParticleFilter tracks a joint distribution over tuples of all ghost
|
||||
positions.
|
||||
"""
|
||||
|
||||
def __init__(self, numParticles=600):
|
||||
self.setNumParticles(numParticles)
|
||||
|
||||
@@ -380,7 +494,16 @@ class JointParticleFilter(ParticleFilter):
|
||||
"""
|
||||
self.particles = []
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
perm = list(itertools.product(self.legalPositions, repeat = self.numGhosts))
|
||||
|
||||
random.shuffle(perm)
|
||||
size, n = len(perm), self.numParticles
|
||||
|
||||
while size < n:
|
||||
n -= size
|
||||
self.particles += perm
|
||||
|
||||
self.particles = self.particles + perm[:n]
|
||||
|
||||
def addGhostAgent(self, agent):
|
||||
"""
|
||||
@@ -413,20 +536,48 @@ class JointParticleFilter(ParticleFilter):
|
||||
the DiscreteDistribution may be useful.
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
pacmanPosition, distribution = gameState.getPacmanPosition(), DiscreteDistribution()
|
||||
|
||||
for pros in self.particles:
|
||||
curr_p = 1
|
||||
for i in range(self.numGhosts):
|
||||
noisy_dist = observation[i]
|
||||
curr_p *= self.getObservationProb(noisy_dist, pacmanPosition, pros[i], self.getJailPosition(i))
|
||||
distribution[pros] += curr_p
|
||||
|
||||
self.beliefs = distribution
|
||||
# check for total/norm
|
||||
if self.beliefs.total() == 0:
|
||||
self.initializeUniformly(gameState)
|
||||
else:
|
||||
self.beliefs.normalize()
|
||||
# ok through all particles and assign
|
||||
for x in range(self.numParticles):
|
||||
newPos = self.beliefs.sample()
|
||||
self.particles[x] = newPos
|
||||
|
||||
def elapseTime(self, gameState):
|
||||
"""
|
||||
Sample each particle's next state based on its current state and the
|
||||
gameState.
|
||||
"""
|
||||
newParticles = []
|
||||
newParticles, cache = [], {}
|
||||
for oldParticle in self.particles:
|
||||
newParticle = list(oldParticle) # A list of ghost positions
|
||||
|
||||
# now loop through and update each entry in newParticle...
|
||||
"*** YOUR CODE HERE ***"
|
||||
raiseNotDefined()
|
||||
prevPos = list(oldParticle)
|
||||
# through all ghosts
|
||||
for i in range(self.numGhosts):
|
||||
# check if seen before
|
||||
if (oldParticle, i) in cache:
|
||||
newParticle[i] = cache[(oldParticle, i)].sample()
|
||||
else:
|
||||
newPosDist = self.getPositionDistribution(gameState, prevPos, i, self.ghostAgents[i])
|
||||
cache[(oldParticle, i)] = newPosDist
|
||||
# assign new ones to each particle
|
||||
newParticle[i] = newPosDist.sample()
|
||||
|
||||
"""*** END YOUR CODE HERE ***"""
|
||||
newParticles.append(tuple(newParticle))
|
||||
@@ -442,6 +593,7 @@ class MarginalInference(InferenceModule):
|
||||
A wrapper around the JointInference module that returns marginal beliefs
|
||||
about ghosts.
|
||||
"""
|
||||
|
||||
def initializeUniformly(self, gameState):
|
||||
"""
|
||||
Set the belief state to an initial, prior value.
|
||||
|
||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user