-
Notifications
You must be signed in to change notification settings - Fork 9
/
change_weight.py
509 lines (418 loc) · 21.4 KB
/
change_weight.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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# baselineTeam.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]).
# baselineTeam.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
import distanceCalculator
import random, time, util, sys
from game import Directions
import game
from util import nearestPoint
#################
# Team creation #
#################
import sys
sys.path.append("teams/<COMPAI>/")
def createTeam(firstIndex, secondIndex, isRed,
first='Attacker', second='Defender'):
"""
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.
"""
return [eval(first)(firstIndex), eval(second)(secondIndex)]
##########
# Agents #
##########
class Actions():
"""
A base class for all the actions that can be used by both attacker and defender.
"""
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.agent.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}
class getOffensiveActions(Actions):
def __init__(self, agent, index, gameState):
self.agent = agent
self.index = index
self.agent.distancer.getMazeDistances()
self.retreat = False
self.numEnemyFood = "+inf"
self.counter = 0;
if self.agent.red:
boundary = (gameState.data.layout.width - 2) / 2
else:
boundary = ((gameState.data.layout.width - 2) / 2) + 1
self.boundary = []
for i in range(1, gameState.data.layout.height - 1):
if not gameState.hasWall(boundary, i):
self.boundary.append((boundary, i))
self.patrolSpot = []
while len(self.patrolSpot) > (gameState.data.layout.height - 2) / 2:
self.patrolSpot.pop(0)
self.patrolSpot.pop(len(self.patrolSpot) - 1)
# Update probabilities to each patrol point.
def getFeatures(self, gameState, action):
"""
Get features used for state evaluation.
"""
features = util.Counter()
successor = self.getSuccessor(gameState, action)
# Compute score from successor state
features['successorScore'] = self.agent.getScore(successor)
# get current position of the agent
CurrentPosition = successor.getAgentState(self.index).getPosition()
# Compute the distance to the nearest boundary
boundaryMin = 1000000
for i in range(len(self.boundary)):
disBoundary = self.agent.getMazeDistance(CurrentPosition, self.boundary[i])
if (disBoundary < boundaryMin):
boundaryMin = disBoundary
features['returned'] = boundaryMin
features['carrying'] = successor.getAgentState(self.index).numCarrying
# Compute distance to the nearest food
foodList = self.agent.getFood(successor).asList()
if len(foodList) > 0 :
minFoodDistance = 99999
for food in foodList:
distance = self.agent.getMazeDistance(CurrentPosition, food)
if (distance < minFoodDistance):
minFoodDistance = distance
features['distanceToFood'] = minFoodDistance
# Compute distance to the nearest capsule
capsuleList = self.agent.getCapsules(successor)
if len(capsuleList) > 0:
minCapsuleDistance = 99999
for c in capsuleList:
distance = self.agent.getMazeDistance(CurrentPosition, c)
if distance < minCapsuleDistance:
minCapsuleDistance = distance
features['distanceToCapsule'] = minCapsuleDistance
else:
features['distanceToCapsule'] = 0
# Compute distance to closest ghost
opponentsState = []
for i in self.agent.getOpponents(successor):
opponentsState.append(successor.getAgentState(i))
visible = filter(lambda x: not x.isPacman and x.getPosition() != None, opponentsState)
if len(visible) > 0:
positions = [agent.getPosition() for agent in visible]
closest = min(positions, key=lambda x: self.agent.getMazeDistance(CurrentPosition, x))
closestDist = self.agent.getMazeDistance(CurrentPosition, closest)
if closestDist <= 5:
# print(CurrentPosition,closest,closestDist)
features['GhostDistance'] = closestDist
else:
probDist = []
for i in self.agent.getOpponents(successor):
probDist.append(successor.getAgentDistances()[i])
features['GhostDistance'] = min(probDist)
# Attacker only try to kill the enemy if : itself is ghost form and the distance between him and the ghost is less than 4
enemiesPacMan = [successor.getAgentState(i) for i in self.agent.getOpponents(successor)]
Range = filter(lambda x: x.isPacman and x.getPosition() != None, enemiesPacMan)
if len(Range) > 0:
positions = [agent.getPosition() for agent in Range]
closest = min(positions, key=lambda x: self.agent.getMazeDistance(CurrentPosition, x))
closestDist = self.agent.getMazeDistance(CurrentPosition, closest)
if closestDist < 4:
# print(CurrentPosition,closest,closestDist)
features['distanceToEnemiesPacMan'] = closestDist
else:
features['distanceToEnemiesPacMan'] = 0
return features
def getWeights(self, gameState, action):
"""
Get weights for the features used in the evaluation.
"""
# If opponent is scared, the agent should not care about GhostDistance
successor = self.getSuccessor(gameState, action)
numOfCarrying = successor.getAgentState(self.index).numCarrying
curPosition=successor.getAgentState(self.index).getPosition()
opponents = [successor.getAgentState(i) for i in self.agent.getOpponents(successor)]
visible = filter(lambda x: not x.isPacman and x.getPosition() != None, opponents)
if len(visible) > 0:
for agent in visible:
if agent.scaredTimer > 0:
if agent.scaredTimer > 12:
print("More than 12")
return {'successorScore': 110, 'distanceToFood': -15, 'distanceToEnemiesPacMan': 0,
'GhostDistance': 0, 'distanceToCapsule': 0, 'returned': 10-3*numOfCarrying,'carrying': 450 }
elif 6 < agent.scaredTimer < 12 :
print(" 6 Less than 12")
return {'successorScore': 110+5*numOfCarrying, 'distanceToFood': -5, 'distanceToEnemiesPacMan': 0,
'GhostDistance': -1, 'distanceToCapsule': -10, 'returned': -5-4*numOfCarrying,'carrying': 200,
}
# Less than 6 steps
elif agent.scaredTimer < 6:
print("Less than 6")
return {'successorScore': 110+10*numOfCarrying, 'distanceToFood': -5, 'distanceToEnemiesPacMan': 0,
'GhostDistance': 15, 'distanceToCapsule': -10,'returned': -1-3*numOfCarrying,'carrying': 2,
}
# Visible and not scared
else:
if self.agent.getMazeDistance(curPosition,agent.getPosition())<=5:
print("visible no scare")
return {'successorScore': 110, 'distanceToFood': -5, 'distanceToEnemiesPacMan': 0,
'GhostDistance': 35, 'distanceToCapsule': -10,'returned': -1-4*numOfCarrying, 'carrying': 0,
}
# If I am not PacMan the enemy is a pacMan, I can try to eliminate him
# Attacker only try to defence if it is close to it (less than 4 steps)
# enemiesPacMan = [successor.getAgentState(i) for i in self.agent.getOpponents(successor)]
# Range = filter(lambda x: x.isPacman and x.getPosition() != None, enemiesPacMan)
# if len(Range) > 0 and not gameState.getAgentState(self.index).isPacman:
# return {'successorScore': 0, 'distanceToFood': -1, 'distanceToEnemiesPacMan': -8,
# 'distanceToCapsule': 0, 'GhostDistance': 0,
# 'returned': 0, 'carrying': 10}
# Did not see anything
else:
self.counter += 1
print("No visible !!!!!!!!!!",self.counter)
return {'successorScore': 1000+numOfCarrying*3.5, 'distanceToFood': -7, 'GhostDistance': 0, 'distanceToEnemiesPacMan': 0,
'distanceToCapsule': -5,'returned': 5-numOfCarrying*3, 'carrying': 350,}
def allSimulation(self, depth, gameState, decay):
new_state = gameState.deepCopy()
if depth == 0:
result_list = []
actions = new_state.getLegalActions(self.index)
actions.remove(Directions.STOP)
reversed_direction = Directions.REVERSE[new_state.getAgentState(self.index).configuration.direction]
if reversed_direction in actions and len(actions) > 1:
actions.remove(reversed_direction)
a = random.choice(actions)
next_state = new_state.generateSuccessor(self.index, a)
result_list.append(self.evaluate(next_state, Directions.STOP))
return max(result_list)
# Get valid actions
result_list = []
actions = new_state.getLegalActions(self.index)
current_direction = new_state.getAgentState(self.index).configuration.direction
# The agent should not use the reverse direction during simulation
reversed_direction = Directions.REVERSE[current_direction]
if reversed_direction in actions and len(actions) > 1:
actions.remove(reversed_direction)
# Randomly chooses a valid action
for a in actions:
# Compute new state and update depth
next_state = new_state.generateSuccessor(self.index, a)
result_list.append(
self.evaluate(next_state, Directions.STOP) + decay * self.allSimulation(depth - 1, next_state, decay))
return max(result_list)
def MTCS(self, depth, gameState, decay):
new_state = gameState.deepCopy()
value = self.evaluate(new_state, Directions.STOP)
decay_index = 1
while depth > 0:
# Get valid actions
actions = new_state.getLegalActions(self.index)
# The agent should not stay put in the simulation
# actions.remove(Directions.STOP)
current_direction = new_state.getAgentState(self.agent.index).configuration.direction
# The agent should not use the reverse direction during simulation
reversed_direction = Directions.REVERSE[new_state.getAgentState(self.agent.index).configuration.direction]
if reversed_direction in actions and len(actions) > 1:
actions.remove(reversed_direction)
# Randomly chooses a valid action
a = random.choice(actions)
# Compute new state and update depth
new_state = new_state.generateSuccessor(self.agent.index, a)
value = value + decay ** decay_index * self.evaluate(new_state, Directions.STOP)
depth -= 1
decay_index += 1
# Evaluate the final simulation state
return value
def chooseAction(self, gameState):
start = time.time()
# Get valid actions. Randomly choose a valid one out of the best (if best is more than one)
actions = gameState.getLegalActions(self.agent.index)
actions.remove(Directions.STOP)
possibleValue = []
for a in actions:
NextState = gameState.generateSuccessor(self.agent.index, a)
value = self.allSimulation(2, NextState, 0.7)
possibleValue .append(value)
# Choose randomly from possible (very unlikely) best
bestAction = max(possibleValue)
possibleChoice = filter(lambda x: x[0] == bestAction, zip(possibleValue, actions))
#print 'eval time for offensive agent %d: %.4f' % (self.agent.index, time.time() - start)
return random.choice(possibleChoice)[1]
class getDefensiveActions(Actions):
# Load the denfensive information
def __init__(self, agent, index, gameState):
self.index = index
self.agent = agent
self.defenderList = {}
if self.agent.red:
middle = (gameState.data.layout.width - 2) / 2
else:
middle = ((gameState.data.layout.width - 2) / 2) + 1
self.noWall = []
for i in range(1, gameState.data.layout.height - 1):
if not gameState.hasWall(middle, i):
self.noWall.append((middle, i))
self.target = None
self.lastObservedFood = None
# Remove some positions. The agent do not need to patrol
# all positions in the central area.
while len(self.noWall) > (gameState.data.layout.height - 2) / 2:
self.noWall.pop(0)
self.noWall.pop(len(self.noWall) - 1)
# Update probabilities to each patrol point.
self.DefendingProbability(gameState)
def DefendingProbability(self, gameState):
total = 0
# Get the minimum distance from the food to our
# patrol points.
for position in self.noWall:
closestFoodDistance = 99999
foodList = self.agent.getFoodYouAreDefending(gameState).asList()
for food in foodList:
dist = self.agent.getMazeDistance(position, food)
if dist < closestFoodDistance:
closestFoodDistance = dist
if closestFoodDistance == 0:
closestFoodDistance = 1
self.defenderList[position] = 1.0 / float(closestFoodDistance)
total += self.defenderList[position]
# Normalize the value used as probability.
if total == 0:
total = 1
for x in self.defenderList.keys():
self.defenderList[x] = float(self.defenderList[x]) / float(total)
def selectPatrolTarget(self):
rand = random.random()
sum = 0.0
for x in self.defenderList.keys():
sum += self.defenderList[x]
if rand < sum:
return x
def chooseAction(self, gameState):
# start = time.time()
# Patrol probabilities.
DefendingList = self.agent.getFoodYouAreDefending(gameState).asList()
if self.lastObservedFood and len(self.lastObservedFood) != len(DefendingList):
self.DefendingProbability(gameState)
CurrentPosition = gameState.getAgentPosition(self.index)
if CurrentPosition == self.target:
self.target = None
# Visible enemy , keep chasing.
opponentsState = []
for i in self.agent.getOpponents(gameState):
opponentsState.append(gameState.getAgentState(i))
visible = filter(lambda x:x.isPacman and x.getPosition() != None, opponentsState)
if len(visible)>0:
positions = []
for invader in visible:
positions.append(invader.getPosition())
self.target = min(positions, key=lambda x: self.agent.getMazeDistance(CurrentPosition, x))
# If we can't see an invader, but our pacdots were eaten,
# we will check the position where the pacdot disappeared.
elif self.lastObservedFood != None:
eaten = set(self.lastObservedFood) - set(self.agent.getFoodYouAreDefending(gameState).asList())
if len(eaten)>0:
self.target = eaten.pop()
# Update the distribution.
self.lastObservedFood = self.agent.getFoodYouAreDefending(gameState).asList()
# If we have only a few pacdots, let's walk among them.
if self.target == None and len(self.agent.getFoodYouAreDefending(gameState).asList()) <= 4:
food = self.agent.getFoodYouAreDefending(gameState).asList() \
+ self.agent.getCapsulesYouAreDefending(gameState)
self.target = random.choice(food)
# Random patrolling
elif self.target == None:
self.target = self.selectPatrolTarget()
# Choose action. We will take the action that brings us
# closer to the target. However, we will never stay put
# and we will never invade the enemy side.
actions = gameState.getLegalActions(self.index)
feasible = []
fvalues = []
for a in actions:
new_state = gameState.generateSuccessor(self.index, a)
if not a == Directions.STOP and not new_state.getAgentState(self.index).isPacman:
newPosition = new_state.getAgentPosition(self.index)
feasible.append(a)
fvalues.append(self.agent.getMazeDistance(newPosition, self.target))
# Randomly chooses between ties.
best = min(fvalues)
ties = filter(lambda x: x[0] == best, zip(fvalues, feasible))
# print 'eval time for defender agent %d: %.4f' % (self.index, time.time() - start)
return random.choice(ties)[1]
class Attacker(CaptureAgent):
def __init__(self, index):
CaptureAgent.__init__(self, index)
def registerInitialState(self, gameState):
CaptureAgent.registerInitialState(self, gameState)
self.DefenceStatus = getDefensiveActions(self, self.index, gameState)
self.OffenceStatus = getOffensiveActions(self, self.index, gameState)
def chooseAction(self, gameState):
self.enemies = self.getOpponents(gameState)
if self.getScore(gameState) > 15:
return self.DefenceStatus.chooseAction(gameState)
else:
return self.OffenceStatus.chooseAction(gameState)
class Defender(CaptureAgent):
def __init__(self, index):
CaptureAgent.__init__(self, index)
def registerInitialState(self, gameState):
CaptureAgent.registerInitialState(self, gameState)
self.DefenceStatus = getDefensiveActions(self, self.index, gameState)
self.OffenceStatus = getOffensiveActions(self, self.index, gameState)
def chooseAction(self, gameState):
self.enemies = self.getOpponents(gameState)
# if numInvaders == 0 and self.getScore(gameState) < 10:
# return self.OffenceStatus.chooseAction(gameState)
# else:
# print(self.DefenceStatus.target, "Target is ..........")
return self.DefenceStatus.chooseAction(gameState)