-
Notifications
You must be signed in to change notification settings - Fork 3
/
featureExtractors.py
290 lines (246 loc) · 9.54 KB
/
featureExtractors.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# featureExtractors.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]).
"Feature extractors for Pacman game states"
from game import Directions, Actions
import util
class FeatureExtractor:
def getFeatures(self, state, action):
"""
Returns a dict from features to counts
Usually, the count will just be 1.0 for
indicator functions.
"""
util.raiseNotDefined()
class IdentityExtractor(FeatureExtractor):
def getFeatures(self, state, action):
feats = util.Counter()
feats[(state, action)] = 1.0
return feats
class CoordinateExtractor(FeatureExtractor):
def getFeatures(self, state, action):
feats = util.Counter()
feats[state] = 1.0
feats['x=%d' % state[0]] = 1.0
feats['y=%d' % state[0]] = 1.0
feats['action=%s' % action] = 1.0
return feats
def toInt(pos):
x, y = pos
x, y = int(x), int(y)
return x, y
def closestFood(pos, food, walls):
"""
closestFood -- this is similar to the function that we have
worked on in the search project; here its all in one place
"""
fringe = [(pos[0], pos[1], 0)]
expanded = set()
while fringe:
pos_x, pos_y, dist = fringe.pop(0)
if (pos_x, pos_y) in expanded:
continue
expanded.add((pos_x, pos_y))
# if we find a food at this location then exit
if food[pos_x][pos_y]:
return dist
# otherwise spread out from the location to its neighbours
nbrs = Actions.getLegalNeighbors((pos_x, pos_y), walls)
for nbr_x, nbr_y in nbrs:
fringe.append((nbr_x, nbr_y, dist+1))
# no food found
return None
def closestGhost(pos, ghosts, walls, ghostStates):
"""
return distance of closest scared ghost if it exists
"""
fringe = [(pos[0], pos[1], 0)]
expanded = set()
while fringe:
pos_x, pos_y, dist = fringe.pop(0)
if dist > 2:
return None
if (pos_x, pos_y) in expanded:
continue
expanded.add((pos_x, pos_y))
# if we find a food at this location then exit
for g, s in zip(ghosts, ghostStates):
ghostPos = toInt(g)
if ghostPos == (pos_x, pos_y) and s.scaredTimer == 0:
return dist
# otherwise spread out from the location to its neighbours
nbrs = Actions.getLegalNeighbors((pos_x, pos_y), walls)
for nbr_x, nbr_y in nbrs:
fringe.append((nbr_x, nbr_y, dist+1))
return None
def closestScaredGhost(pos, ghostStates, walls):
"""
return distance of closest scared ghost if it exists
"""
fringe = [(pos[0], pos[1], 0)]
expanded = set()
while fringe:
pos_x, pos_y, dist = fringe.pop(0)
if (pos_x, pos_y) in expanded:
continue
expanded.add((pos_x, pos_y))
for s in ghostStates:
ghostPos = toInt(s.getPosition())
if s.scaredTimer and ghostPos == (pos_x, pos_y):
return dist
nbrs = Actions.getLegalNeighbors((pos_x, pos_y), walls)
for nbr_x, nbr_y in nbrs:
fringe.append((nbr_x, nbr_y, dist+1))
return None
def closestManhattanScaredGhost(pos, ghostStates):
"""
return manhattan distance of closest scared ghost
"""
dist = 1e9
for s in ghostStates:
if s.scaredTimer > 0:
dist = min(dist, util.manhattanDistance(s.getPosition(), pos))
if dist < 1e9:
return dist
return None
def closestCapsule(pos, capsules, walls):
"""
return distance to closest capsule if it exists
"""
fringe = [(pos[0], pos[1], 0)]
expanded = set()
while fringe:
pos_x, pos_y, dist = fringe.pop(0)
if (pos_x, pos_y) in expanded:
continue
expanded.add((pos_x, pos_y))
# if we find a food at this location then exit
for c in capsules:
if c == (pos_x, pos_y):
return dist
# otherwise spread out from the location to its neighbours
nbrs = Actions.getLegalNeighbors((pos_x, pos_y), walls)
for nbr_x, nbr_y in nbrs:
fringe.append((nbr_x, nbr_y, dist+1))
return None
def findMaxTimeScared(ghostStates):
maxTime = 0
for s in ghostStates:
if s.scaredTimer > maxTime:
maxTime = s.scaredTimer
return maxTime
class SimpleExtractor(FeatureExtractor):
"""
Returns simple features for a basic reflex Pacman:
- whether food will be eaten
- how far away the next food is
- whether a ghost collision is imminent
- whether a ghost is one step away
"""
def getFeatures(self, state, action):
# extract the grid of food and wall locations and get the ghost locations
food = state.getFood()
walls = state.getWalls()
ghosts = state.getGhostPositions()
features = util.Counter()
features["bias"] = 1.0
# compute the location of pacman after he takes the action
x, y = state.getPacmanPosition()
dx, dy = Actions.directionToVector(action)
next_x, next_y = int(x + dx), int(y + dy)
# count the number of ghosts 1-step away
features["#-of-ghosts-1-step-away"] = sum((next_x, next_y) in Actions.getLegalNeighbors(g, walls) for g in ghosts)
# if there is no danger of ghosts then add the food feature
if not features["#-of-ghosts-1-step-away"] and food[next_x][next_y]:
features["eats-food"] = 1.0
dist = closestFood((next_x, next_y), food, walls)
if dist is not None:
# make the distance a number less than one otherwise the update
# will diverge wildly
features["closest-food"] = float(dist) / (walls.width * walls.height)
features.divideAll(10.0)
return features
class NewExtractor(FeatureExtractor):
"""
Design you own feature extractor here. You may define other helper functions you find necessary.
"""
def getFeatures(self, state, action):
"""
- whether food will be eaten
- how far away the next food is
- whether a ghost collision is imminent
- whether a ghost is one step away
Things to implement additionally:
- Pacman goes towards Capsule when Ghost is 5 grids away
--> Capsule must be near for this condition to take place
- Pacman to go after ghosts when Ghosts are scared
"""
"""
- PowerUp Mode
--> Distance from Ghost (go towards ghost if they are close)
--> Power Up distance (Should be lowered so that it can be used next time)
-->
- Non-PowerUp Mode
--> Distance from Ghost (get away from ghost)
--> Power Up distance (if near should be higher)
-->
- BOTH
--> Closest Food
--> Eat Food
"""
"*** YOUR CODE HERE ***"
food = state.getFood()
walls = state.getWalls()
ghosts = state.getGhostPositions()
ghostStates = state.getGhostStates()
capsules = state.getCapsules()
# Actions vectors
# compute the location of pacman after he takes the action
x, y = state.getPacmanPosition()
dx, dy = Actions.directionToVector(action)
next_x, next_y = int(x + dx), int(y + dy)
nextPos = (next_x, next_y)
features = util.Counter()
features["bias"] = 1.0
hasScared = False
for s in ghostStates:
if s.scaredTimer > 0:
hasScared = True
if capsules:
distCapsule = closestCapsule(nextPos, capsules, walls)
if distCapsule is not None:
features["closest-capsule"] = float(distCapsule) / (walls.width * walls.height)
else:
dist = closestFood((next_x, next_y), food, walls)
if dist is not None:
# make the distance a number less than one otherwise the update
# will diverge wildly
features["closest-food"] = float(dist) / (walls.width * walls.height)
if food[next_x][next_y]:
features["eats-food"] = 1.0
if hasScared:
distScaredGhost = closestScaredGhost(nextPos, ghostStates, walls)
if distScaredGhost is not None:
features["closest-scared-ghost"] = float(distScaredGhost) / (walls.width * walls.height)
for g, s in zip(ghosts, ghostStates):
if nextPos == g and s.scaredTimer > 0:
features["eat-scared-ghost"] = 1.0
features["closest-capsule"] = 0
features["#-of-ghosts-1-step-away"] = 0
for g, s in zip(ghosts, ghostStates):
if s.scaredTimer == 0:
features["#-of-ghosts-1-step-away"] += nextPos in Actions.getLegalNeighbors(g, walls)
for g, s in zip(ghosts, ghostStates):
if nextPos == g and s.scaredTimer == 0:
features["die-to-ghost"] = 1.0
features.divideAll(10.0)
return features