forked from tjsavage-test-organization/cs221_final_project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
baselineAgents.py
executable file
·355 lines (286 loc) · 12.3 KB
/
baselineAgents.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
# baselineAgents.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 ([email protected]) and Dan Klein ([email protected]).
# For more info, see http://inst.eecs.berkeley.edu/~cs188/sp09/pacman.html
from captureAgents import CaptureAgent
from captureAgents import AgentFactory
import distanceCalculator
import random, time, util
from game import Directions
import keyboardAgents
import game
from util import nearestPoint
import regularMutation
import math
#############
# FACTORIES #
#############
NUM_KEYBOARD_AGENTS = 0
class BaselineAgents(AgentFactory):
"Returns one keyboard agent and offensive reflex agents"
def __init__(self, isRed, first='offense', second='defense', third='offense', rest='offense', **args):
AgentFactory.__init__(self, isRed)
self.agents = [first, second, third]
self.rest = rest
def getAgent(self, index):
if len(self.agents) > 0:
return self.choose(self.agents.pop(0), index)
else:
return self.choose(self.rest, index)
def choose(self, agentStr, index):
if agentStr == 'keys':
global NUM_KEYBOARD_AGENTS
NUM_KEYBOARD_AGENTS += 1
if NUM_KEYBOARD_AGENTS == 1:
return keyboardAgents.KeyboardAgent(index)
elif NUM_KEYBOARD_AGENTS == 2:
return keyboardAgents.KeyboardAgent2(index)
else:
raise Exception('Max of two keyboard agents supported')
elif agentStr == 'offense':
return OffensiveReflexAgent(index)
elif agentStr == 'defense':
return DefensiveReflexAgent(index)
else:
raise Exception("No staff agent identified by " + agentStr)
class AllOffenseAgents(AgentFactory):
"Returns one keyboard agent and offensive reflex agents"
def __init__(self, **args):
AgentFactory.__init__(self, **args)
def getAgent(self, index):
if index % 2 == 0:
return OffensiveReflexAgent(index)
return AIAgent(index)
"return OffensiveReflexAgent(index)"
class OffenseDefenseAgents(AgentFactory):
"Returns one keyboard agent and offensive reflex agents"
def __init__(self, **args):
AgentFactory.__init__(self, **args)
self.offense = False
def getAgent(self, index):
self.offense = not self.offense
if self.offense:
return OffensiveReflexAgent(index)
else:
return DefensiveReflexAgent(index)
##########
# Agents #
##########
class AIAgent(CaptureAgent):
def __init__(self, index):
CaptureAgent.__init__(self, index)
self.enemyPos = list()
self.firstTurnComplete = False
self.legalPositions = list()
def chooseAction(self, gameState):
if not self.firstTurnComplete:
self.firstTurnComplete = True
self.startingFood = len(self.getFoodYouAreDefending(gameState).asList())
self.theirStartingFood = len(self.getFood(gameState).asList())
agentDistances = gameState.getAgentDistances()
walls = gameState.getWalls()
wallList = walls.asList(False)
self.legalPositions = wallList
numAgents = gameState.getNumAgents()
enemies = self.getOpponents(gameState)
for enemy in enemies:
posCounter = util.Counter()
for p in self.legalPositions: posCounter[p] = 1.0
posCounter.normalize()
self.enemyPos.append((enemy,posCounter))
"""
Picks among the actions with the highest Q(s,a).
"""
newEnemyPositions = list()
for enemy in self.enemyPos:
newPositions = util.Counter()
for p in self.legalPositions:
trueDistance = util.manhattanDistance(p, self.getPosition(gameState))
prior = enemy[1][p]
#print prior
prob = math.log1p(gameState.getDistanceProb(trueDistance, gameState.getAgentDistances()[enemy[0]])) + math.log1p(prior)
newPositions[p] = prob
newPositions.normalize()
newEnemyPositions.append((enemy[0], newPositions))
self.enemyPos = newEnemyPositions
print self.enemyPos
counters = list()
for item in self.enemyPos:
counters.append(item[1])
if self.index == 1 or self.index == 2: self.displayDistributionsOverPositions(counters)
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]
# print 'eval time for agent %d: %.4f' % (self.index, time.time() - start)
#maxValue = max(values)
#bestActions = [a for a, v in zip(actions, values) if v == maxValue]
bestActions = self.getLegalActions(gameState)
return random.choice(bestActions)
class ReflexCaptureAgent(CaptureAgent):
def __init__(self, index):
CaptureAgent.__init__(self, index)
self.firstTurnComplete = False
self.startingFood = 0
self.theirStartingFood = 0
"""
A base class for reflex agents that chooses score-maximizing actions
"""
def chooseAction(self, gameState):
if not self.firstTurnComplete:
self.firstTurnComplete = True
self.startingFood = len(self.getFoodYouAreDefending(gameState).asList())
self.theirStartingFood = len(self.getFood(gameState).asList())
"""
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]
# print 'eval time for agent %d: %.4f' % (self.index, time.time() - start)
maxValue = max(values)
bestActions = [a for a, v in zip(actions, values) if v == 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}
"""
Features (not the best features) which have learned weight values stored.
"""
def getMutationFeatures(self, gameState, action):
features = util.Counter()
successor = self.getSuccessor(gameState, action)
position = self.getPosition(gameState)
distances = 0.0
for tpos in self.getTeamPositions(successor):
distances = distances + abs(tpos[0] - position[0])
features['xRelativeToFriends'] = distances
enemyX = 0.0
for epos in self.getOpponentPositions(successor):
if epos is not None:
enemyX = enemyX + epos[0]
features['avgEnemyX'] = distances
foodLeft = len(self.getFoodYouAreDefending(successor).asList())
features['percentOurFoodLeft'] = foodLeft / self.startingFood
foodLeft = len(self.getFood(successor).asList())
features['percentTheirFoodLeft'] = foodLeft / self.theirStartingFood
features['IAmAScaredGhost'] = 1.0 if self.isPacman(successor) and self.getScaredTimer(successor) > 0 else 0.0
features['enemyPacmanNearMe'] = 0.0
minOppDist = 10000
minOppPos = (0, 0)
for ep in self.getOpponentPositions(successor):
# For a feature later on
if ep is not None and self.getMazeDistance(ep, position) < minOppDist:
minOppDist = self.getMazeDistance(ep, position)
minOppPos = ep
if ep is not None and self.getMazeDistance(ep, position) <= 1 and self.isPositionInTeamTerritory(successor, ep):
features['enemyPacmanNearMe'] = 1.0
features['numSameFriends'] = 0
for friend in self.getTeam(successor):
if successor.getAgentState(self.index).isPacman is self.isPacman(successor):
features['numSameFriends'] = features['numSameFriends'] + 1
# 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
minDiffDistance = min([1000] + [self.getMazeDistance(position, food) - self.getMazeDistance(minOppPos, food) for food in foodList if minOppDist < 1000])
features['blockableFood'] = 1.0 if minDiffDistance < 1.0 else 0.0
return features
class OffensiveReflexAgent(ReflexCaptureAgent):
"""
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 getFeatures(self, gameState, action):
features = self.getMutationFeatures(gameState, action)
successor = self.getSuccessor(gameState, action)
features['successorScore'] = self.getScore(successor)
# Compute distance to the nearest food
foodList = self.getFood(successor).asList()
features['numFood'] = len(foodList)
if len(foodList) > 0: # This should always be True, but better safe than sorry
myPos = successor.getAgentState(self.index).getPosition()
minDistance = min([self.getMazeDistance(myPos, food) for food in foodList])
features['distanceToFood'] = minDistance
return features
def getWeights(self, gameState, action):
weights = regularMutation.aggressiveDWeightsDict
weights['successorScore'] = 1.5
# Always eat nearby food
weights['numFood'] = -1000
# Favor reaching new food the most
weights['distanceToFood'] = -5
return weights
class DefensiveReflexAgent(ReflexCaptureAgent):
"""
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 = self.getMutationFeatures(gameState, action)
successor = self.getSuccessor(gameState, action)
myState = successor.getAgentState(self.index)
myPos = myState.getPosition()
# Computes whether we're on defense (1) or offense (0)
features['onDefense'] = 1
if myState.isPacman: features['onDefense'] = 0
# Computes distance to invaders we can see
enemies = [successor.getAgentState(i) for i in self.getOpponents(successor)]
invaders = [a for a in enemies if a.isPacman and a.getPosition() != None]
features['numInvaders'] = len(invaders)
if len(invaders) > 0:
dists = [self.getMazeDistance(myPos, a.getPosition()) for a in invaders]
features['invaderDistance'] = min(dists)
if action == Directions.STOP: features['stop'] = 1
rev = Directions.REVERSE[gameState.getAgentState(self.index).configuration.direction]
if action == rev: features['reverse'] = 1
foodList = self.getFoodYouAreDefending(successor).asList()
distance = 0
for food in foodList:
distance = distance + self.getMazeDistance(myPos, food)
features['totalDistancesToFood'] = distance
return features
def getWeights(self, gameState, action):
weights = regularMutation.goalieDWeightsDict
weights['numInvaders'] = -100
weights['onDefense'] = 100
weights['invaderDistance'] = -1.5
weights['totalDistancesToFood'] = -0.1
weights['stop'] = -1
weights['reverse'] = -1
return weights