-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbustersGhostAgents.py
60 lines (51 loc) · 2.65 KB
/
bustersGhostAgents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# bustersGhostAgents.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
# ([email protected]) and Dan Klein ([email protected]).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel ([email protected]).
import ghostAgents
from game import Directions
from game import Actions
from util import manhattanDistance
import util
class StationaryGhost( ghostAgents.GhostAgent ):
def getDistribution( self, state ):
dist = util.Counter()
dist[Directions.STOP] = 1.0
return dist
class DispersingGhost( ghostAgents.GhostAgent ):
"Chooses an action that distances the ghost from the other ghosts with probability spreadProb."
def __init__( self, index, spreadProb=0.5):
self.index = index
self.spreadProb = spreadProb
def getDistribution( self, state ):
ghostState = state.getGhostState( self.index )
legalActions = state.getLegalActions( self.index )
pos = state.getGhostPosition( self.index )
isScared = ghostState.scaredTimer > 0
speed = 1
if isScared: speed = 0.5
actionVectors = [Actions.directionToVector( a, speed ) for a in legalActions]
newPositions = [( pos[0]+a[0], pos[1]+a[1] ) for a in actionVectors]
# get other ghost positions
others = [i for i in range(1,state.getNumAgents()) if i != self.index]
for a in others: assert state.getGhostState(a) != None, "Ghost position unspecified in state!"
otherGhostPositions = [state.getGhostPosition(a) for a in others if state.getGhostPosition(a)[1] > 1]
# for each action, get the sum of inverse squared distances to the other ghosts
sumOfDistances = []
for pos in newPositions:
sumOfDistances.append( sum([(1+manhattanDistance(pos, g))**(-2) for g in otherGhostPositions]) )
bestDistance = min(sumOfDistances)
numBest = [bestDistance == dist for dist in sumOfDistances].count(True)
distribution = util.Counter()
for action, distance in zip(legalActions, sumOfDistances):
if distance == bestDistance: distribution[action] += self.spreadProb / numBest
distribution[action] += (1 - self.spreadProb) / len(legalActions)
return distribution