-
Notifications
You must be signed in to change notification settings - Fork 0
/
myTeam_caesar.py
538 lines (464 loc) · 22.2 KB
/
myTeam_caesar.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
# -*- coding: utf-8 -*-
# __author__ = 'siyuan'
# myTeam_caesar.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, sys
from game import Directions, Actions
from util import nearestPoint
from decimal import Decimal
#################
# Team creation #
#################
def createTeam(firstIndex, secondIndex, isRed,
first='OffensiveReflexAgent', second='DefensiveReflexAgent'):
"""
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.
"""
if firstIndex < 2:
first = 'Caesar'
elif firstIndex < 4:
first = 'Caesar1'
if secondIndex < 2:
second = 'Caesar'
else:
second = 'Caesar1'
print first
print second
return [eval(first)(firstIndex), eval(second)(secondIndex)]
##########
# Agents #
##########
class ReflexCaptureAgent(CaptureAgent):
"""
A base class for reflex agents that chooses score-maximizing actions
"""
def registerInitialState(self, gameState):
self.start = gameState.getAgentPosition(self.index)
#print "self.index", self.index
CaptureAgent.registerInitialState(self, gameState)
#print self.index, self.getWeights( gameState, "Stop")
def chooseAction(self, gameState):
"""
Picks among the actions with the highest Q(s,a).
"""
actions = gameState.getLegalActions(self.index)
print "agent0 eat:", gameState.getAgentState(0).numCarrying, gameState.getAgentState(0).numReturned
print "agent1 eat:", gameState.getAgentState(1).numCarrying, gameState.getAgentState(1).numReturned
print "agent2 eat:", gameState.getAgentState(2).numCarrying, gameState.getAgentState(2).numReturned
print "agent3 eat:", gameState.getAgentState(3).numCarrying, gameState.getAgentState(3).numReturned
# 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]
foodLeft = len(self.getFood(gameState).asList())
if foodLeft <= 2:
bestDist = 9999
for action in actions:
successor = self.getSuccessor(gameState, action)
pos2 = successor.getAgentPosition(self.index)
dist = self.getMazeDistance(self.start, pos2)
if dist < bestDist:
bestAction = action
bestDist = dist
return bestAction
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}
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 = util.Counter()
successor = self.getSuccessor(gameState, action)
foodList = self.getFood(successor).asList()
features['successorScore'] = -len(foodList) # self.getScore(successor)
# Compute distance to the nearest food
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):
return {'successorScore': 100, 'distanceToFood': -1}
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 = util.Counter()
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
return features
def getWeights(self, gameState, action):
return {'numInvaders': -1000, 'onDefense': 100, 'invaderDistance': -10, 'stop': -100, 'reverse': -2}
class Caesar(ReflexCaptureAgent):
def getFeatures(self, state, action):
food = self.getFood(state)
foodList = food.asList()
walls = state.getWalls()
isPacman = self.getSuccessor(state, action).getAgentState(self.index).isPacman
# Zone of the board agent is primarily responsible for
zone = (self.index - self.index % 2) / 2
teammates = [state.getAgentState(i).getPosition() for i in self.getTeam(state)]
opponents = [state.getAgentState(i) for i in self.getOpponents(state)]
chasers = [a for a in opponents if not (a.isPacman) and a.getPosition() != None]
prey = [a for a in opponents if a.isPacman and a.getPosition() != None]
features = util.Counter()
if action == Directions.STOP:
features["stopped"] = 1.0
# compute the location of pacman after he takes the action
x, y = state.getAgentState(self.index).getPosition()
dx, dy = Actions.directionToVector(action)
next_x, next_y = int(x + dx), int(y + dy)
# count the number of ghosts 1-step away
for g in chasers:
if (next_x, next_y) == g.getPosition():
if g.scaredTimer > 0:
features["eats-ghost"] += 1
features["eats-food"] += 2
else:
features["#-of-dangerous-ghosts-1-step-away"] = 1
features["#-of-harmless-ghosts-1-step-away"] = 0
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
if g.scaredTimer > 0:
features["#-of-harmless-ghosts-1-step-away"] += 1
elif isPacman:
features["#-of-dangerous-ghosts-1-step-away"] += 1
features["#-of-harmless-ghosts-1-step-away"] = 0
if state.getAgentState(self.index).scaredTimer == 0:
for g in prey:
if (next_x, next_y) == g.getPosition:
features["eats-invader"] = 1
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
features["invaders-1-step-away"] += 1
else:
for g in opponents:
if g.getPosition() != None:
if (next_x, next_y) == g.getPosition:
features["eats-invader"] = -10
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
features["invaders-1-step-away"] += -10
for capsule_x, capsule_y in state.getCapsules():
if next_x == capsule_x and next_y == capsule_y and isPacman:
features["eats-capsules"] = 1.0
if not features["#-of-dangerous-ghosts-1-step-away"]:
if food[next_x][next_y]:
features["eats-food"] = 1.0
if len(foodList) > 0: # This should always be True, but better safe than sorry
myFood = []
for food in foodList:
food_x, food_y = food
if (food_y > zone * walls.height / 3 and food_y < (zone + 1) * walls.height / 3):
myFood.append(food)
if len(myFood) == 0:
myFood = foodList
myMinDist = min([self.getMazeDistance((next_x, next_y), food) for food in myFood])
if myMinDist is not None:
features["closest-food"] = float(myMinDist) / (walls.width * walls.height)
features.divideAll(10.0)
return features
def getWeights(self, gameState, action):
return {'eats-invader': 5, 'invaders-1-step-away': 0, 'teammateDist': 1.5, 'closest-food': -1,
'eats-capsules': 10.0, '#-of-dangerous-ghosts-1-step-away': -20, 'eats-ghost': 1.0,
'#-of-harmless-ghosts-1-step-away': 0.1, 'stopped': -5, 'eats-food': 1}
class Caesar1(ReflexCaptureAgent):
def getFeatures(self, state, action):
food = self.getFood(state)
foodList = food.asList()
walls = state.getWalls()
isPacman = self.getSuccessor(state, action).getAgentState(self.index).isPacman
# Zone of the board agent is primarily responsible for
zone = (self.index - self.index % 2) / 2
teammates = [state.getAgentState(i).getPosition() for i in self.getTeam(state)]
opponents = [state.getAgentState(i) for i in self.getOpponents(state)]
chasers = [a for a in opponents if not (a.isPacman) and a.getPosition() != None]
prey = [a for a in opponents if a.isPacman and a.getPosition() != None]
features = util.Counter()
if action == Directions.STOP:
features["stopped"] = 1.0
# compute the location of pacman after he takes the action
x, y = state.getAgentState(self.index).getPosition()
dx, dy = Actions.directionToVector(action)
next_x, next_y = int(x + dx), int(y + dy)
# count the number of ghosts 1-step away
for g in chasers:
if (next_x, next_y) == g.getPosition():
if g.scaredTimer > 0:
features["eats-ghost"] += 1
features["eats-food"] += 2
else:
features["#-of-dangerous-ghosts-1-step-away"] = 1
features["#-of-harmless-ghosts-1-step-away"] = 0
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
if g.scaredTimer > 0:
features["#-of-harmless-ghosts-1-step-away"] += 1
elif isPacman:
features["#-of-dangerous-ghosts-1-step-away"] += 1
features["#-of-harmless-ghosts-1-step-away"] = 0
if state.getAgentState(self.index).scaredTimer == 0:
for g in prey:
if (next_x, next_y) == g.getPosition:
features["eats-invader"] = 1
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
features["invaders-1-step-away"] += 1
else:
for g in opponents:
if g.getPosition() != None:
if (next_x, next_y) == g.getPosition:
features["eats-invader"] = -10
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
features["invaders-1-step-away"] += -10
for capsule_x, capsule_y in state.getCapsules():
if next_x == capsule_x and next_y == capsule_y and isPacman:
features["eats-capsules"] = 1.0
if not features["#-of-dangerous-ghosts-1-step-away"]:
if food[next_x][next_y]:
features["eats-food"] = 1.0
if len(foodList) > 0: # This should always be True, but better safe than sorry
myFood = []
for food in foodList:
food_x, food_y = food
if (food_y > zone * walls.height / 3 and food_y < (zone + 1) * walls.height / 3):
myFood.append(food)
if len(myFood) == 0:
myFood = foodList
myMinDist = min([self.getMazeDistance((next_x, next_y), food) for food in myFood])
if myMinDist is not None:
features["closest-food"] = float(myMinDist) / (walls.width * walls.height)
features.divideAll(10.0)
return features
def getWeights(self, gameState, action):
return {'eats-invader': 5, 'invaders-1-step-away': 1, 'teammateDist': 1.5, 'closest-food': -1,
'eats-capsules': 10.0, '#-of-dangerous-ghosts-1-step-away': -20, 'eats-ghost': 1.0,
'#-of-harmless-ghosts-1-step-away': 0.1, 'stopped': -5, 'eats-food': 1}
class Caesar2(ReflexCaptureAgent):
def registerInitialState(self, origin, givenWeights):
if origin is True:
self.setWeights = {}
self.randomWeights()
else:
self.setWeights = givenWeights
def randomWeights(self):
self.setWeights['eats-invader'] = round(random.random()*10,2)
self.setWeights['invaders-1-step-away'] = round(random.random()*5,2)
self.setWeights['teammateDist'] = round(random.random()*5,2)
self.setWeights['closest-food'] = round(-random.random()*5,2)
self.setWeights['eats-capsules'] = round(random.random()*20,2)
self.setWeights['#-of-dangerous-ghosts-1-step-away'] = round(-random.random()*40,2)
self.setWeights['eats-ghost'] = round(random.random()*3,2)
self.setWeights['#-of-harmless-ghosts-1-step-away'] = round(random.random(),2)
self.setWeights['stopped'] = round(-random.random()*10,2)
self.setWeights['eats-food'] = round(random.random()*3,2)
def mybin(self, float, bit=10):
"""
这个程序用来将十进制数转化为二进制数,也可以转化浮点数
用法: mybin(float, bit=10)
float是指要转化的十进制数,可以为浮点数
bit是用来指定小数点后面的位数,默认为10位。
函数返回字符串
"""
negative = 0
if float < 0:
negative = 1
float = -1 * float
integer = int(float) # 整数部分
decimal = Decimal(str(float)) - integer # 小数部分
integer_convert = "" # 转化后的整数部分
decimal_convert = "" # 转化后的小数部分
binary = "" # 最后的二进制数
if integer == 0:
binary = "0"
else:
while integer != 0:
result = int(integer % 2)
integer = integer / 2
integer_convert = str(result) + integer_convert
if decimal == 0:
binary = integer_convert
else:
i = 0
while decimal != 0 and i < bit:
result = int(decimal * 2)
decimal = decimal * 2 - result
decimal_convert = decimal_convert + str(result)
i = i + 1
binary = integer_convert + '.' + decimal_convert
if negative == 1:
binary = '-' + binary
return binary
def mydec(self,binary):
"""
将二进制数变成十进制数,包括浮点数
用法:mydec(binary)
binary是一个字符型二进制数,可以是浮点数
返回一个浮点数或者整数
"""
negative = 0
if binary[0] == '-':
negative = 1
binary = binary[1:]
integer = ""
decimal = ""
dot = binary.find('.')
if dot == -1:
integer = binary
else:
integer = binary[:dot]
decimal = binary[dot + 1:]
cnt1 = integer.__len__()
cnt2 = decimal.__len__()
if cnt1 != 0:
temp = 0
index_reverse = range(0, cnt1)
for i in index_reverse:
temp = temp + int(integer[i]) * (2 ** (cnt1 - i - 1))
integer = temp
else:
integer = 0
if cnt2 != 0:
temp = 0
index = range(1, cnt1 + 1)
for i in index:
temp = temp + int(decimal[i - 1]) * (2 ** (-1 * i))
decimal = temp
else:
decimal = 0
result = integer + decimal
if negative == 1:
result = -1 * result
return result
def getFeatures(self, state, action):
food = self.getFood(state)
foodList = food.asList()
walls = state.getWalls()
isPacman = self.getSuccessor(state, action).getAgentState(self.index).isPacman
# Zone of the board agent is primarily responsible for
zone = (self.index - self.index % 2) / 2
teammates = [state.getAgentState(i).getPosition() for i in self.getTeam(state)]
opponents = [state.getAgentState(i) for i in self.getOpponents(state)]
chasers = [a for a in opponents if not (a.isPacman) and a.getPosition() != None]
prey = [a for a in opponents if a.isPacman and a.getPosition() != None]
features = util.Counter()
if action == Directions.STOP:
features["stopped"] = 1.0
# compute the location of pacman after he takes the action
x, y = state.getAgentState(self.index).getPosition()
dx, dy = Actions.directionToVector(action)
next_x, next_y = int(x + dx), int(y + dy)
# count the number of ghosts 1-step away
for g in chasers:
if (next_x, next_y) == g.getPosition():
if g.scaredTimer > 0:
features["eats-ghost"] += 1
features["eats-food"] += 2
else:
features["#-of-dangerous-ghosts-1-step-away"] = 1
features["#-of-harmless-ghosts-1-step-away"] = 0
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
if g.scaredTimer > 0:
features["#-of-harmless-ghosts-1-step-away"] += 1
elif isPacman:
features["#-of-dangerous-ghosts-1-step-away"] += 1
features["#-of-harmless-ghosts-1-step-away"] = 0
if state.getAgentState(self.index).scaredTimer == 0:
for g in prey:
if (next_x, next_y) == g.getPosition:
features["eats-invader"] = 1
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
features["invaders-1-step-away"] += 1
else:
for g in opponents:
if g.getPosition() != None:
if (next_x, next_y) == g.getPosition:
features["eats-invader"] = -10
elif (next_x, next_y) in Actions.getLegalNeighbors(g.getPosition(), walls):
features["invaders-1-step-away"] += -10
for capsule_x, capsule_y in state.getCapsules():
if next_x == capsule_x and next_y == capsule_y and isPacman:
features["eats-capsules"] = 1.0
if not features["#-of-dangerous-ghosts-1-step-away"]:
if food[next_x][next_y]:
features["eats-food"] = 1.0
if len(foodList) > 0: # This should always be True, but better safe than sorry
myFood = []
for food in foodList:
food_x, food_y = food
if (food_y > zone * walls.height / 3 and food_y < (zone + 1) * walls.height / 3):
myFood.append(food)
if len(myFood) == 0:
myFood = foodList
myMinDist = min([self.getMazeDistance((next_x, next_y), food) for food in myFood])
if myMinDist is not None:
features["closest-food"] = float(myMinDist) / (walls.width * walls.height)
features.divideAll(10.0)
return features
def getWeights(self, gameState, action):
return self.setWeights