1
+ < html >
2
+ < head >
3
+ < title > ghostAgents.py</ title >
4
+ </ head >
5
+ < body >
6
+ < h3 > ghostAgents.py (< a href ="../ghostAgents.py "> original</ a > )</ h3 >
7
+ < hr >
8
+ < pre >
9
+ < span style ="color: green; font-style: italic "> # ghostAgents.py
10
+ # --------------
11
+ # Licensing Information: Please do not distribute or publish solutions to this
12
+ # project. You are free to use and extend these projects for educational
13
+ # purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
14
+ # John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
15
+ # For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
16
+
17
+ </ span > < span style ="color: blue; font-weight: bold "> from </ span > game < span style ="color: blue; font-weight: bold "> import </ span > Agent
18
+ < span style ="color: blue; font-weight: bold "> from </ span > game < span style ="color: blue; font-weight: bold "> import </ span > Actions
19
+ < span style ="color: blue; font-weight: bold "> from </ span > game < span style ="color: blue; font-weight: bold "> import </ span > Directions
20
+ < span style ="color: blue; font-weight: bold "> import </ span > random
21
+ < span style ="color: blue; font-weight: bold "> from </ span > util < span style ="color: blue; font-weight: bold "> import </ span > manhattanDistance
22
+ < span style ="color: blue; font-weight: bold "> import </ span > util
23
+
24
+ < span style ="color: blue; font-weight: bold "> class </ span > GhostAgent< span style ="font-weight: bold "> ( </ span > Agent < span style ="font-weight: bold "> ):
25
+ </ span > < span style ="color: blue; font-weight: bold "> def </ span > __init__< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> , </ span > index < span style ="font-weight: bold "> ):
26
+ </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > index < span style ="font-weight: bold "> = </ span > index
27
+
28
+ < span style ="color: blue; font-weight: bold "> def </ span > getAction< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> , </ span > state < span style ="font-weight: bold "> ):
29
+ </ span > dist < span style ="font-weight: bold "> = </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > getDistribution< span style ="font-weight: bold "> (</ span > state< span style ="font-weight: bold "> )
30
+ </ span > < span style ="color: blue; font-weight: bold "> if </ span > len< span style ="font-weight: bold "> (</ span > dist< span style ="font-weight: bold "> ) == </ span > < span style ="color: red "> 0</ span > < span style ="font-weight: bold "> :
31
+ </ span > < span style ="color: blue; font-weight: bold "> return </ span > Directions< span style ="font-weight: bold "> .</ span > STOP
32
+ < span style ="color: blue; font-weight: bold "> else</ span > < span style ="font-weight: bold "> :
33
+ </ span > < span style ="color: blue; font-weight: bold "> return </ span > util< span style ="font-weight: bold "> .</ span > chooseFromDistribution< span style ="font-weight: bold "> ( </ span > dist < span style ="font-weight: bold "> )
34
+
35
+ </ span > < span style ="color: blue; font-weight: bold "> def </ span > getDistribution< span style ="font-weight: bold "> (</ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> , </ span > state< span style ="font-weight: bold "> ):
36
+ </ span > < span style ="color: red "> "Returns a Counter encoding a distribution over actions from the provided state."
37
+ </ span > util< span style ="font-weight: bold "> .</ span > raiseNotDefined< span style ="font-weight: bold "> ()
38
+
39
+ </ span > < span style ="color: blue; font-weight: bold "> class </ span > RandomGhost< span style ="font-weight: bold "> ( </ span > GhostAgent < span style ="font-weight: bold "> ):
40
+ </ span > < span style ="color: red "> "A ghost that chooses a legal action uniformly at random."
41
+ </ span > < span style ="color: blue; font-weight: bold "> def </ span > getDistribution< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> , </ span > state < span style ="font-weight: bold "> ):
42
+ </ span > dist < span style ="font-weight: bold "> = </ span > util< span style ="font-weight: bold "> .</ span > Counter< span style ="font-weight: bold "> ()
43
+ </ span > < span style ="color: blue; font-weight: bold "> for </ span > a < span style ="color: blue; font-weight: bold "> in </ span > state< span style ="font-weight: bold "> .</ span > getLegalActions< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > index < span style ="font-weight: bold "> ): </ span > dist< span style ="font-weight: bold "> [</ span > a< span style ="font-weight: bold "> ] = </ span > < span style ="color: red "> 1.0
44
+ </ span > dist< span style ="font-weight: bold "> .</ span > normalize< span style ="font-weight: bold "> ()
45
+ </ span > < span style ="color: blue; font-weight: bold "> return </ span > dist
46
+
47
+ < span style ="color: blue; font-weight: bold "> class </ span > DirectionalGhost< span style ="font-weight: bold "> ( </ span > GhostAgent < span style ="font-weight: bold "> ):
48
+ </ span > < span style ="color: red "> "A ghost that prefers to rush Pacman, or flee when scared."
49
+ </ span > < span style ="color: blue; font-weight: bold "> def </ span > __init__< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> , </ span > index< span style ="font-weight: bold "> , </ span > prob_attack< span style ="font-weight: bold "> =</ span > < span style ="color: red "> 0.8</ span > < span style ="font-weight: bold "> , </ span > prob_scaredFlee< span style ="font-weight: bold "> =</ span > < span style ="color: red "> 0.8 </ span > < span style ="font-weight: bold "> ):
50
+ </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > index < span style ="font-weight: bold "> = </ span > index
51
+ < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > prob_attack < span style ="font-weight: bold "> = </ span > prob_attack
52
+ < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > prob_scaredFlee < span style ="font-weight: bold "> = </ span > prob_scaredFlee
53
+
54
+ < span style ="color: blue; font-weight: bold "> def </ span > getDistribution< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> , </ span > state < span style ="font-weight: bold "> ):
55
+ </ span > < span style ="color: green; font-style: italic "> # Read variables from state
56
+ </ span > ghostState < span style ="font-weight: bold "> = </ span > state< span style ="font-weight: bold "> .</ span > getGhostState< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > index < span style ="font-weight: bold "> )
57
+ </ span > legalActions < span style ="font-weight: bold "> = </ span > state< span style ="font-weight: bold "> .</ span > getLegalActions< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > index < span style ="font-weight: bold "> )
58
+ </ span > pos < span style ="font-weight: bold "> = </ span > state< span style ="font-weight: bold "> .</ span > getGhostPosition< span style ="font-weight: bold "> ( </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > index < span style ="font-weight: bold "> )
59
+ </ span > isScared < span style ="font-weight: bold "> = </ span > ghostState< span style ="font-weight: bold "> .</ span > scaredTimer < span style ="font-weight: bold "> > </ span > < span style ="color: red "> 0
60
+
61
+ </ span > speed < span style ="font-weight: bold "> = </ span > < span style ="color: red "> 1
62
+ </ span > < span style ="color: blue; font-weight: bold "> if </ span > isScared< span style ="font-weight: bold "> : </ span > speed < span style ="font-weight: bold "> = </ span > < span style ="color: red "> 0.5
63
+
64
+ </ span > actionVectors < span style ="font-weight: bold "> = [</ span > Actions< span style ="font-weight: bold "> .</ span > directionToVector< span style ="font-weight: bold "> ( </ span > a< span style ="font-weight: bold "> , </ span > speed < span style ="font-weight: bold "> ) </ span > < span style ="color: blue; font-weight: bold "> for </ span > a < span style ="color: blue; font-weight: bold "> in </ span > legalActions< span style ="font-weight: bold "> ]
65
+ </ span > newPositions < span style ="font-weight: bold "> = [( </ span > pos< span style ="font-weight: bold "> [</ span > < span style ="color: red "> 0</ span > < span style ="font-weight: bold "> ]+</ span > a< span style ="font-weight: bold "> [</ span > < span style ="color: red "> 0</ span > < span style ="font-weight: bold "> ], </ span > pos< span style ="font-weight: bold "> [</ span > < span style ="color: red "> 1</ span > < span style ="font-weight: bold "> ]+</ span > a< span style ="font-weight: bold "> [</ span > < span style ="color: red "> 1</ span > < span style ="font-weight: bold "> ] ) </ span > < span style ="color: blue; font-weight: bold "> for </ span > a < span style ="color: blue; font-weight: bold "> in </ span > actionVectors< span style ="font-weight: bold "> ]
66
+ </ span > pacmanPosition < span style ="font-weight: bold "> = </ span > state< span style ="font-weight: bold "> .</ span > getPacmanPosition< span style ="font-weight: bold "> ()
67
+
68
+ </ span > < span style ="color: green; font-style: italic "> # Select best actions given the state
69
+ </ span > distancesToPacman < span style ="font-weight: bold "> = [</ span > manhattanDistance< span style ="font-weight: bold "> ( </ span > pos< span style ="font-weight: bold "> , </ span > pacmanPosition < span style ="font-weight: bold "> ) </ span > < span style ="color: blue; font-weight: bold "> for </ span > pos < span style ="color: blue; font-weight: bold "> in </ span > newPositions< span style ="font-weight: bold "> ]
70
+ </ span > < span style ="color: blue; font-weight: bold "> if </ span > isScared< span style ="font-weight: bold "> :
71
+ </ span > bestScore < span style ="font-weight: bold "> = </ span > max< span style ="font-weight: bold "> ( </ span > distancesToPacman < span style ="font-weight: bold "> )
72
+ </ span > bestProb < span style ="font-weight: bold "> = </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > prob_scaredFlee
73
+ < span style ="color: blue; font-weight: bold "> else</ span > < span style ="font-weight: bold "> :
74
+ </ span > bestScore < span style ="font-weight: bold "> = </ span > min< span style ="font-weight: bold "> ( </ span > distancesToPacman < span style ="font-weight: bold "> )
75
+ </ span > bestProb < span style ="font-weight: bold "> = </ span > < span style ="color: blue "> self</ span > < span style ="font-weight: bold "> .</ span > prob_attack
76
+ bestActions < span style ="font-weight: bold "> = [</ span > action < span style ="color: blue; font-weight: bold "> for </ span > action< span style ="font-weight: bold "> , </ span > distance < span style ="color: blue; font-weight: bold "> in </ span > zip< span style ="font-weight: bold "> ( </ span > legalActions< span style ="font-weight: bold "> , </ span > distancesToPacman < span style ="font-weight: bold "> ) </ span > < span style ="color: blue; font-weight: bold "> if </ span > distance < span style ="font-weight: bold "> == </ span > bestScore< span style ="font-weight: bold "> ]
77
+
78
+ </ span > < span style ="color: green; font-style: italic "> # Construct distribution
79
+ </ span > dist < span style ="font-weight: bold "> = </ span > util< span style ="font-weight: bold "> .</ span > Counter< span style ="font-weight: bold "> ()
80
+ </ span > < span style ="color: blue; font-weight: bold "> for </ span > a < span style ="color: blue; font-weight: bold "> in </ span > bestActions< span style ="font-weight: bold "> : </ span > dist< span style ="font-weight: bold "> [</ span > a< span style ="font-weight: bold "> ] = </ span > bestProb < span style ="font-weight: bold "> / </ span > len< span style ="font-weight: bold "> (</ span > bestActions< span style ="font-weight: bold "> )
81
+ </ span > < span style ="color: blue; font-weight: bold "> for </ span > a < span style ="color: blue; font-weight: bold "> in </ span > legalActions< span style ="font-weight: bold "> : </ span > dist< span style ="font-weight: bold "> [</ span > a< span style ="font-weight: bold "> ] += ( </ span > < span style ="color: red "> 1</ span > < span style ="font-weight: bold "> -</ span > bestProb < span style ="font-weight: bold "> ) / </ span > len< span style ="font-weight: bold "> (</ span > legalActions< span style ="font-weight: bold "> )
82
+ </ span > dist< span style ="font-weight: bold "> .</ span > normalize< span style ="font-weight: bold "> ()
83
+ </ span > < span style ="color: blue; font-weight: bold "> return </ span > dist
84
+
85
+ </ pre >
86
+ </ body >
87
+ </ html >
88
+
0 commit comments