-
Notifications
You must be signed in to change notification settings - Fork 4
/
myTeam.py
371 lines (304 loc) · 14 KB
/
myTeam.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# myTeam.py
# ---------
# Licensing Information: Please do not distribute or publish solutions to this
# project. You are free to use and extend these projects for educational
# purposes. The Pacman AI projects were developed at UC Berkeley, primarily by
# John DeNero (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
from captureAgents import CaptureAgent
import random, time, util
from util import nearestPoint
from game import Directions
import game
#################
# Team creation #
#################
def createTeam(firstIndex, secondIndex, isRed,
first = 'OffensiveAgent', second = 'DefensiveAgent'):
"""
This function should return a list of two agents that will form the
team, initialized using firstIndex and secondIndex as their agent
index numbers. isRed is True if the red team is being created, and
will be False if the blue team is being created.
As a potentially helpful development aid, this function can take
additional string-valued keyword arguments ("first" and "second" are
such arguments in the case of this function), which will come from
the --redOpts and --blueOpts command-line arguments to capture.py.
For the nightly contest, however, your team will be created without
any extra arguments, so you should make sure that the default
behavior is what you want for the nightly contest.
"""
# The following line is an example only; feel free to change it.
return [eval(first)(firstIndex), eval(second)(secondIndex)]
##########
# Agents #
##########
class SmartAgent(CaptureAgent):
"""
A base class for search agents that chooses score-maximizing actions.
"""
def registerInitialState(self, gameState):
CaptureAgent.registerInitialState(self, gameState)
self.boundary_top = True
if gameState.getAgentState(self.index).getPosition()[0] == 1:
self.isRed = True
else:
self.isRed = False
self.boundaries = self.boundaryTravel(gameState)
self.treeDepth = 3
def chooseAction(self, gameState):
"""
Picks among the actions with the highest Q(s,a).
"""
actions = gameState.getLegalActions(self.index)
# You can profile your evaluation time by uncommenting these lines
start = time.time()
values = [self.evaluate(gameState, a) for a in actions]
if time.time() - start > 0.9: print 'eval time for agent %d: %.4f' % (self.index, time.time() - start)
maxValue = max(values)
bestActions = [action for action, value in zip(actions, values) if value == maxValue]
return random.choice(bestActions)
def getSuccessor(self, gameState, action):
"""
Finds the next successor which is a grid position (location tuple).
"""
successor = gameState.generateSuccessor(self.index, action)
pos = successor.getAgentState(self.index).getPosition()
if pos != nearestPoint(pos):
# Only half a grid position was covered
return successor.generateSuccessor(self.index, action)
else:
return successor
def evaluate(self, gameState, action):
"""
Computes a linear combination of features and feature weights
"""
features = self.getFeatures(gameState, action)
weights = self.getWeights(gameState, action)
return features * weights
def getFeatures(self, gameState, action):
"""
Returns a counter of features for the state
"""
features = util.Counter()
successor = self.getSuccessor(gameState, action)
features['successorScore'] = self.getScore(successor)
return features
def getWeights(self, gameState, action):
"""
Normally, weights do not depend on the gamestate. They can be either
a counter or a dictionary.
"""
return {'successorScore': 1.0}
def boundaryTravel(self, gameState):
return (0, 0), (0, 0)
class OffensiveAgent(SmartAgent):
"""
A reflex agent that seeks food. This is an agent
we give you to get an idea of what an offensive agent might look like,
but it is by no means the best or only way to build an offensive agent.
"""
def getAction(self, gameState):
"""
Returns the expectimax action using self.depth and self.evaluationFunction
All ghosts should be modeled as choosing uniformly at random from their
legal moves.
"""
opponents = {}
for enemy in self.getOpponents(gameState):
opponents[enemy] = gameState.getAgentState(enemy).getPosition()
directions = {'north': (0, 1), 'south': (0, -1), 'east': (1, 0), 'west': (-1, 0)}
ghost_weights = {'distance': 5, 'scared': 5}
def get_ghost_actions(current_pos):
walls = gameState.getWalls().asList()
max_x = max([wall[0] for wall in walls])
max_y = max([wall[1] for wall in walls])
actions = []
for direction in directions:
action = directions[direction]
new_pos = (int(current_pos[0] + action[0]), int(current_pos[1] + action[1]))
if new_pos not in walls:
if (1 <= new_pos[0] < max_x) and (1 <= new_pos[1] < max_y):
actions.append(direction.title())
return actions
def get_new_position(current_pos, action):
act = directions[[direction for direction in directions if str(action).lower() == direction][0]]
return (current_pos[0] + act[0], current_pos[1] + act[1])
def expectation(gamestate, position, legalActions):
ghost_dict = {}
for action in legalActions:
newPos = get_new_position(position, action)
ghost_dict[action] = self.getMazeDistance(position, newPos)*ghost_weights['distance']
min_action = min(ghost_dict)
for action in ghost_dict:
if ghost_dict[action] == min_action:
ghost_dict[action] = .8
else:
ghost_dict[action] = .2/len(legalActions)
return ghost_dict
def ghost_eval(gamestate, opponents, opponent):
newPos = opponents[opponent]
enemy = gamestate.getAgentState(opponent)
myPos = gamestate.getAgentState(self.index).getPosition()
if enemy.scaredTimer != 0:
distance = -self.getMazeDistance(myPos, newPos)*ghost_weights['distance']
else:
distance = self.getMazeDistance(myPos, newPos)*ghost_weights['distance']
return distance
def minimax(gamestate, depth, agent, opponents, alpha=-float('inf'), beta=float('inf')):
"""
"""
# Get legal moves per agent
legalActions = [action for action in gamestate.getLegalActions(self.index) if action != Directions.STOP]
# Generate optimal action recursively
actions = {}
if agent == self.index:
maxVal = -float('inf')
for action in legalActions:
eval = self.evaluate(gamestate, action)
if depth == self.treeDepth:
value = eval
else:
value = eval+minimax(self.getSuccessor(gamestate, action), depth, agent+1, opponents, alpha, beta)
maxVal = max(maxVal, value)
if beta < maxVal:
return maxVal
else:
alpha = max(alpha, maxVal)
if depth == 1:
actions[value] = action
if depth == 1: # If you're up to the first depth, return a legal action
return actions[maxVal]
return maxVal
else:
minVal = float('inf')
for opponent in opponents:
if gamestate.getAgentState(opponent).getPosition() is not None:
legalActions = get_ghost_actions(opponents[opponent])
expectations = expectation(gamestate, opponents[opponent], legalActions)
for action in legalActions:
new_opponents = opponents.copy()
new_opponents[opponent] = get_new_position(opponents[opponent], action)
ghost_val = ghost_eval(gamestate, new_opponents, opponent)*expectations[action]
value = ghost_val + minimax(gamestate, depth+1, self.index, new_opponents, alpha, beta)
minVal = min(minVal, value)
if minVal < alpha:
return minVal
else:
beta = min(beta, minVal)
if minVal == float('inf'):
return 0
return minVal
return minimax(gameState, 1, self.index, opponents)
def getFeatures(self, gameState, action):
features = util.Counter()
successor = self.getSuccessor(gameState, action)
features['successorScore'] = self.getScore(successor)
myState = successor.getAgentState(self.index)
myPos = myState.getPosition()
# Computes distance to enemy ghosts
enemies = [successor.getAgentState(i) for i in self.getOpponents(successor)]
ghosts = [a for a in enemies if not a.isPacman and a.getPosition() != None]
invaders = [a for a in enemies if a.isPacman and a.getPosition() != None]
features['invaderDistance'] = 0.0
if len(invaders) > 0:
features['invaderDistance'] = min([self.getMazeDistance(myPos, invader.getPosition()) for invader in invaders]) + 1
if len(ghosts) > 0:
ghostEval = 0.0
scaredDistance = 0.0
regGhosts = [ghost for ghost in ghosts if ghost.scaredTimer == 0]
scaredGhosts = [ghost for ghost in ghosts if ghost.scaredTimer > 0]
if len(regGhosts) > 0:
ghostEval = min([self.getMazeDistance(myPos, ghost.getPosition()) for ghost in regGhosts])
if ghostEval <= 1: ghostEval = -float('inf')
if len(scaredGhosts) > 0:
scaredDistance = min([self.getMazeDistance(myPos, ghost.getPosition()) for ghost in scaredGhosts])
if scaredDistance < ghostEval or ghostEval == 0:
if scaredDistance == 0: features['ghostScared'] = -10
features['distanceToGhost'] = ghostEval
# Compute distance to the nearest food
foodList = self.getFood(successor).asList()
if len(foodList) > 0: # This should always be True, but better safe than sorry
minDistance = min([self.getMazeDistance(myPos, food) for food in foodList])
features['distanceToFood'] = minDistance
features['foodRemaining'] = len(foodList)
# Compute distance to capsules
capsules = self.getCapsules(gameState)
if len(capsules) > 0:
minDistance = min([ self.getMazeDistance(myPos, capsule) for capsule in capsules ])
if minDistance == 0: minDistance = -100
features['distanceToCapsules'] = minDistance
if action == Directions.STOP: features['stop'] = 1
rev = Directions.REVERSE[gameState.getAgentState(self.index).configuration.direction]
if action == rev: features['reverse'] = 1
return features
def getWeights(self, gameState, action):
return {'successorScore': 100, 'invaderDistance': -50, 'distanceToFood': -1, 'foodRemaining': -1, 'distanceToGhost': 2, 'ghostScared': -1, 'distanceToCapsules': -1, 'stop': -100, 'reverse': -20}
class DefensiveAgent(SmartAgent):
"""
A reflex agent that keeps its side Pacman-free. Again,
this is to give you an idea of what a defensive agent
could be like. It is not the best or only way to make
such an agent.
"""
def getFeatures(self, gameState, action):
features = util.Counter()
successor = self.getSuccessor(gameState, action)
features['successorScore'] = self.getScore(successor)
myState = successor.getAgentState(self.index)
myPos = myState.getPosition()
enemies = [successor.getAgentState(i) for i in self.getOpponents(successor)]
invaders = [a for a in enemies if a.isPacman and a.getPosition() != None]
numInvaders = len([invader for invader in enemies if invader.isPacman])
features['numInvaders'] = numInvaders
defenseFood = self.getFoodYouAreDefending(successor).asList()
numFood = len([food for food in defenseFood])
boundaries = self.boundaries
# Computes whether we're on defense (1) or offense (0)
defense = 10
if myState.isPacman: features['onDefense'] = 0
else: features['onDefense'] = 1
if numInvaders == 0:
# If the agent needs to go to the upper bound, the bound is set to the upper bound. Otherwise it's the lower bound
if self.boundary_top is True: bound = boundaries[0]
else: bound = boundaries[1]
# If the agent has reached the upper bound, set the top boundary to false and vice versa
if myPos == bound: self.boundary_top = not(self.boundary_top)
features['bound'] = self.getMazeDistance(myPos, bound)
# # Computes distance to invaders we can see and their distance to the food we are defending
# if not myState.isPacman and myState.scaredTimer > 0:
# # Compute distance to the nearest food
# foodList = self.getFood(successor).asList()
# if len(foodList) > 0: # This should always be True, but better safe than sorry
# minDistance = min([self.getMazeDistance(myPos, food) for food in foodList])
# features['distanceToFood'] = minDistance + 1
# features['defenseFoodDistance'] = 0.
if len(invaders) > 0:
dist = min([self.getMazeDistance(myPos, invader.getPosition()) for invader in invaders])
if dist == 0: dist = -100
features['invaderDistance'] = min([self.getMazeDistance(myPos, invader.getPosition()) for invader in invaders]) + 1
features['distanceToFood'] = 0.0
if action == Directions.STOP: features['stop'] = 1
rev = Directions.REVERSE[gameState.getAgentState(self.index).configuration.direction]
if action == rev: features['reverse'] = 1
return features
def getWeights(self, gameState, action):
return {'numInvaders': -1000, 'onDefense': 50, 'invaderDistance': -100, 'distanceToFood': -1, 'defenseFoodDistance': -8, 'bound': -10, 'stop': -100, 'reverse': -15}
def boundaryTravel(self, gameState):
"""
Returns two points that act as a boundary line along which the agent travels
"""
walls = gameState.getWalls().asList()
max_y = max([wall[1] for wall in walls])
if not self.isRed:
mid_x = (max([wall[0] for wall in walls])/2) + 2
else:
mid_x = max([wall[0] for wall in walls])/2
walls = gameState.getWalls().asList()
# lower bound is 1/3 of grid. Upper bound is 2/3 of grid
lower = max_y/3
upper = (max_y*2)/3
# If the positions are illegal states, add 1 to get a legal state
while (mid_x, lower) in walls: lower += 1
while (mid_x, upper) in walls: upper -= 1
return (mid_x, lower), (mid_x, upper)