-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyTeam.py
More file actions
executable file
·825 lines (699 loc) · 32.2 KB
/
myTeam.py
File metadata and controls
executable file
·825 lines (699 loc) · 32.2 KB
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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
# myTeam.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
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).
# 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 ast import Raise
from typing import List, Tuple
from numpy import true_divide
from captureAgents import CaptureAgent
import distanceCalculator
import random, time, util, sys, os
from capture import GameState, noisyDistance
from game import Directions, Actions, AgentState, Agent
from util import nearestPoint
import sys, os
# the folder of current file.
BASE_FOLDER = os.path.dirname(os.path.abspath(__file__))
from lib_piglet.utils.pddl_solver import pddl_solver
from lib_piglet.domains.pddl import pddl_state
from lib_piglet.utils.pddl_parser import Action
CLOSE_DISTANCE = 4
MEDIUM_DISTANCE = 15
LONG_DISTANCE = 25
#################
# Team creation #
#################
def createTeam(firstIndex, secondIndex, isRed, first="MixedAgent", second="MixedAgent"):
"""
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 MixedAgent(CaptureAgent):
"""
This is an agent that use pddl to guide the high level actions of Pacman
"""
# Default weights for q learning, if no QLWeights.txt find, we use the following weights.
# You should add your weights for new low level planner here as well.
# weights are defined as class attribute here, so taht agents share same weights.
QLWeights = {
"offensiveWeights": {
"closest-food": -1,
"bias": 1,
"#-of-ghosts-1-step-away": -100,
"successorScore": 100,
"chance-return-food": 10,
},
"defensiveWeights": {
"numInvaders": -1000,
"onDefense": 100,
"teamDistance": 2,
"invaderDistance": -10,
"stop": -100,
"reverse": -2,
},
"escapeWeights": {
"onDefense": 1000,
"enemyDistance": 30,
"stop": -100,
"distanceToHome": -20,
},
}
QLWeightsFile = BASE_FOLDER + "/QLWeightsMyTeam.txt"
# Also can use class variable to exchange information between agents.
CURRENT_ACTION = {}
def registerInitialState(self, gameState: GameState):
self.pddl_solver = pddl_solver(BASE_FOLDER + "/myTeam.pddl")
self.highLevelPlan: List[Tuple[Action, pddl_state]] = (
None # Plan is a list Action and pddl_state
)
self.currentNegativeGoalStates = []
self.currentPositiveGoalStates = []
self.currentActionIndex = (
0 # index of action in self.highLevelPlan should be execute next
)
self.startPosition = gameState.getAgentPosition(
self.index
) # the start location of the agent
CaptureAgent.registerInitialState(self, gameState)
self.lowLevelPlan: List[Tuple[str, Tuple]] = []
self.lowLevelActionIndex = 0
# REMEMBER TRUN TRAINNING TO FALSE when submit to contest server.
self.trainning = False # trainning mode to true will keep update weights and generate random movements by prob.
self.epsilon = 0.1 # default exploration prob, change to take a random step
self.alpha = 0.02 # default learning rate
self.discountRate = (
0.9 # default discount rate on successor state q value when update
)
# Use a dictionary to save information about current agent.
MixedAgent.CURRENT_ACTION[self.index] = {}
"""
Open weights file if it exists, otherwise start with empty weights.
NEEDS TO BE CHANGED BEFORE SUBMISSION
"""
if os.path.exists(MixedAgent.QLWeightsFile):
with open(MixedAgent.QLWeightsFile, "r") as file:
MixedAgent.QLWeights = eval(file.read())
def final(self, gameState: GameState):
"""
This function write weights into files after the game is over.
You may want to comment (disallow) this function when submit to contest server.
"""
if self.trainning:
file = open(MixedAgent.QLWeightsFile, "w")
file.write(str(MixedAgent.QLWeights))
file.close()
def chooseAction(self, gameState: GameState):
"""
This is the action entry point for the agent.
In the game, this function is called when its current agent's turn to move.
We first pick a high-level action.
Then generate low-level action ("North", "South", "East", "West", "Stop") to achieve the high-level action.
"""
# -------------High Level Plan Section-------------------
# Get high level action from a pddl plan.
# Collect objects and init states from gameState
objects, initState = self.get_pddl_state(gameState)
positiveGoal, negtiveGoal = self.getGoals(objects, initState)
# Check if we can stick to current plan
if not self.stateSatisfyCurrentPlan(initState, positiveGoal, negtiveGoal):
# Cannot stick to current plan, prepare goals and replan
self.highLevelPlan: List[Tuple[Action, pddl_state]] = self.getHighLevelPlan(
objects, initState, positiveGoal, negtiveGoal
) # Plan is a list Action and pddl_state
self.currentActionIndex = 0
self.lowLevelPlan = [] # reset low level plan
self.currentNegativeGoalStates = negtiveGoal
self.currentPositiveGoalStates = positiveGoal
if len(self.highLevelPlan) == 0:
raise Exception(
"Solver retuned empty plan, you need to think how you handle this situation or how you modify your model "
)
# Get next action from the plan
highLevelAction = self.highLevelPlan[self.currentActionIndex][0].name
MixedAgent.CURRENT_ACTION[self.index] = highLevelAction
# -------------Low Level Plan Section-------------------
# Get the low level plan using Q learning, and return a low level action at last.
# A low level action is defined in Directions, whihc include {"North", "South", "East", "West", "Stop"}
if not self.posSatisfyLowLevelPlan(gameState):
self.lowLevelPlan = self.getLowLevelPlanQL(
gameState, highLevelAction
) # Generate low level plan with q learning
# you can replace the getLowLevelPlanQL with getLowLevelPlanHS and implement heuristic search planner
self.lowLevelActionIndex = 0
lowLevelAction = self.lowLevelPlan[self.lowLevelActionIndex][0]
self.lowLevelActionIndex += 1
return lowLevelAction
# ------------------------------- PDDL and High-Level Action Functions -------------------------------
def getHighLevelPlan(
self, objects, initState, positiveGoal, negtiveGoal
) -> List[Tuple[Action, pddl_state]]:
"""
This function prepare the pddl problem, solve it and return pddl plan
"""
# Prepare pddl problem
self.pddl_solver.parser_.reset_problem()
self.pddl_solver.parser_.set_objects(objects)
self.pddl_solver.parser_.set_state(initState)
self.pddl_solver.parser_.set_negative_goals(negtiveGoal)
self.pddl_solver.parser_.set_positive_goals(positiveGoal)
# Solve the problem and return the plan
return self.pddl_solver.solve()
def get_pddl_state(self, gameState: GameState) -> Tuple[List[Tuple], List[Tuple]]:
"""
This function collects pddl :objects and :init states from simulator gameState.
"""
# Collect objects and states from the gameState
states = []
objects = []
# Collect available foods on the map
foodLeft = self.getFood(gameState).asList()
if len(foodLeft) > 0:
states.append(("food_available",))
myPos = gameState.getAgentPosition(self.index)
myObj = "a{}".format(self.index)
cloestFoodDist = self.closestFood(
myPos, self.getFood(gameState), gameState.getWalls()
)
if cloestFoodDist != None and cloestFoodDist <= CLOSE_DISTANCE:
states.append(("near_food", myObj))
# Collect capsule states
capsules = self.getCapsules(gameState)
if len(capsules) > 0:
states.append(("capsule_available",))
for cap in capsules:
if self.getMazeDistance(cap, myPos) <= CLOSE_DISTANCE:
states.append(("near_capsule", myObj))
break
# Collect winning states
currentScore = gameState.data.score
if gameState.isOnRedTeam(self.index):
if currentScore > 0:
states.append(("winning",))
if currentScore > 3:
states.append(("winning_gt3",))
if currentScore > 5:
states.append(("winning_gt5",))
if currentScore > 10:
states.append(("winning_gt10",))
if currentScore > 20:
states.append(("winning_gt20",))
else:
if currentScore < 0:
states.append(("winning",))
if currentScore < -3:
states.append(("winning_gt3",))
if currentScore < -5:
states.append(("winning_gt5",))
if currentScore < -10:
states.append(("winning_gt10",))
if currentScore < -20:
states.append(("winning_gt20",))
# Collect team agents states
agents: List[Tuple[int, AgentState]] = [
(i, gameState.getAgentState(i)) for i in self.getTeam(gameState)
]
for agent_index, agent_state in agents:
agent_object = "a{}".format(agent_index)
agent_type = "current_agent" if agent_index == self.index else "ally"
objects += [(agent_object, agent_type)]
if (
agent_index != self.index
and self.getMazeDistance(
gameState.getAgentPosition(self.index),
gameState.getAgentPosition(agent_index),
)
<= CLOSE_DISTANCE
):
states.append(("near_ally",))
if agent_state.scaredTimer > 0:
states.append(("is_scared", agent_object))
if agent_state.numCarrying > 0:
states.append(("food_in_backpack", agent_object))
if agent_state.numCarrying >= 20:
states.append(("20_food_in_backpack", agent_object))
if agent_state.numCarrying >= 10:
states.append(("10_food_in_backpack", agent_object))
if agent_state.numCarrying >= 5:
states.append(("5_food_in_backpack", agent_object))
if agent_state.numCarrying >= 3:
states.append(("3_food_in_backpack", agent_object))
if agent_state.isPacman:
states.append(("is_pacman", agent_object))
# Collect enemy agents states
enemies: List[Tuple[int, AgentState]] = [
(i, gameState.getAgentState(i)) for i in self.getOpponents(gameState)
]
noisyDistance = gameState.getAgentDistances()
typeIndex = 1
for enemy_index, enemy_state in enemies:
enemy_position = enemy_state.getPosition()
enemy_object = "e{}".format(enemy_index)
objects += [(enemy_object, "enemy{}".format(typeIndex))]
if enemy_state.scaredTimer > 0:
states.append(("is_scared", enemy_object))
if enemy_position != None:
for agent_index, agent_state in agents:
if (
self.getMazeDistance(agent_state.getPosition(), enemy_position)
<= CLOSE_DISTANCE
):
states.append(
("enemy_around", enemy_object, "a{}".format(agent_index))
)
else:
if noisyDistance[enemy_index] >= LONG_DISTANCE:
states.append(
("enemy_long_distance", enemy_object, "a{}".format(self.index))
)
elif noisyDistance[enemy_index] >= MEDIUM_DISTANCE:
states.append(
(
"enemy_medium_distance",
enemy_object,
"a{}".format(self.index),
)
)
else:
states.append(
("enemy_short_distance", enemy_object, "a{}".format(self.index))
)
if enemy_state.isPacman:
states.append(("is_pacman", enemy_object))
typeIndex += 1
return objects, states
def stateSatisfyCurrentPlan(
self, init_state: List[Tuple], positiveGoal, negtiveGoal
):
if self.highLevelPlan is None or len(self.highLevelPlan) == 0:
# No plan, need a new plan
self.currentNegativeGoalStates = negtiveGoal
self.currentPositiveGoalStates = positiveGoal
return False
if (
positiveGoal != self.currentPositiveGoalStates
or negtiveGoal != self.currentNegativeGoalStates
):
return False
if self.pddl_solver.matchEffect(
init_state, self.highLevelPlan[self.currentActionIndex][0]
):
# The current state match the effect of current action, current action action done, move to next action
if self.currentActionIndex < len(
self.highLevelPlan
) - 1 and self.pddl_solver.satisfyPrecondition(
init_state, self.highLevelPlan[self.currentActionIndex + 1][0]
):
# Current action finished and next action is applicable
self.currentActionIndex += 1
self.lowLevelPlan = [] # reset low level plan
return True
else:
# Current action finished, next action is not applicable or finish last action in the plan
return False
if self.pddl_solver.satisfyPrecondition(
init_state, self.highLevelPlan[self.currentActionIndex][0]
):
# Current action precondition satisfied, continue executing current action of the plan
return True
# Current action precondition not satisfied anymore, need new plan
return False
def getGoals(self, objects: List[Tuple], initState: List[Tuple]):
# Check a list of goal functions from high priority to low priority if the goal is applicable
# Return the pddl goal states for selected goal function
if ("winning_gt10",) in initState:
return self.goalDefWinning(objects, initState)
else:
return self.goalScoring(objects, initState)
def goalScoring(self, objects: List[Tuple], initState: List[Tuple]):
# If we are not winning more than 5 points,
# we invate enemy land and eat foods, and bring then back.
positiveGoal = []
negtiveGoal = [("food_available",)] # no food avaliable means eat all the food
for obj in objects:
agent_obj = obj[0]
agent_type = obj[1]
if agent_type == "enemy1" or agent_type == "enemy2":
negtiveGoal += [
("is_pacman", agent_obj)
] # no enemy should standing on our land.
return positiveGoal, negtiveGoal
def goalDefWinning(self, objects: List[Tuple], initState: List[Tuple]):
# If winning greater than 5 points,
# this example want defend foods only, and let agents patrol on our ground.
# The "win_the_game" pddl state is only reachable by the "patrol" action in pddl,
# using it as goal, pddl will generate plan eliminate invading enemy and patrol on our ground.
positiveGoal = [("defend_foods",)]
negtiveGoal = []
return positiveGoal, negtiveGoal
# ------------------------------- Heuristic search low level plan Functions -------------------------------
def getLowLevelPlanHS(
self, gameState: GameState, highLevelAction: str
) -> List[Tuple[str, Tuple]]:
# This is a function for plan low level actions using heuristic search.
# You need to implement this function if you want to solve low level actions using heuristic search.
# Here, we list some function you might need, read the GameState and CaptureAgent code for more useful functions.
# These functions also useful for collecting features for Q learnning low levels.
map = (
gameState.getWalls()
) # a 2d array matrix of obstacles, map[x][y] = true means a obstacle(wall) on x,y, map[x][y] = false indicate a free location
foods = self.getFood(
gameState
) # a 2d array matrix of food, foods[x][y] = true if there's a food.
capsules = self.getCapsules(gameState) # a list of capsules
foodNeedDefend = self.getFoodYouAreDefending(
gameState
) # return food will be eatan by enemy (food next to enemy)
capsuleNeedDefend = self.getCapsulesYouAreDefending(
gameState
) # return capsule will be eatan by enemy (capsule next to enemy)
Raise(NotImplementedError("Heuristic Search low level "))
return (
[]
) # You should return a list of tuple of move action and target location (exclude current location).
def posSatisfyLowLevelPlan(self, gameState: GameState):
if (
self.lowLevelPlan == None
or len(self.lowLevelPlan) == 0
or self.lowLevelActionIndex >= len(self.lowLevelPlan)
):
return False
myPos = gameState.getAgentPosition(self.index)
nextPos = Actions.getSuccessor(
myPos, self.lowLevelPlan[self.lowLevelActionIndex][0]
)
if nextPos != self.lowLevelPlan[self.lowLevelActionIndex][1]:
return False
return True
# ------------------------------- Q-learning low level plan Functions -------------------------------
"""
Iterate through all q-values that we get from all
possible actions, and return the action associated
with the highest q-value.
"""
def getLowLevelPlanQL(
self, gameState: GameState, highLevelAction: str
) -> List[Tuple[str, Tuple]]:
values = []
legalActions = gameState.getLegalActions(self.index)
rewardFunction = None
featureFunction = None
weights = None
learningRate = 0
##########
# The following classification of high level actions is only a example.
# You should think and use your own way to design low level planner.
##########
if highLevelAction == "attack":
# The q learning process for offensive actions are complete,
# you can improve getOffensiveFeatures to collect more useful feature to pass more information to Q learning model
# you can improve the getOffensiveReward function to give reward for new features and improve the trainning process .
rewardFunction = self.getOffensiveReward
featureFunction = self.getOffensiveFeatures
weights = self.getOffensiveWeights()
learningRate = self.alpha
elif highLevelAction == "go_home":
# The q learning process for escape actions are NOT complete,
# Introduce more features and complete the q learning process
rewardFunction = self.getEscapeReward
featureFunction = self.getEscapeFeatures
weights = self.getEscapeWeights()
learningRate = 0 # learning rate set to 0 as reward function not implemented for this action, do not do q update,
else:
# The q learning process for defensive actions are NOT complete,
# Introduce more features and complete the q learning process
rewardFunction = self.getDefensiveReward
featureFunction = self.getDefensiveFeatures
weights = self.getDefensiveWeights()
learningRate = 0 # learning rate set to 0 as reward function not implemented for this action, do not do q update
if len(legalActions) != 0:
prob = util.flipCoin(self.epsilon) # get change of perform random movement
if prob and self.trainning:
action = random.choice(legalActions)
else:
for action in legalActions:
if self.trainning:
self.updateWeights(
gameState,
action,
rewardFunction,
featureFunction,
weights,
learningRate,
)
values.append(
(
self.getQValue(featureFunction(gameState, action), weights),
action,
)
)
action = max(values)[1]
myPos = gameState.getAgentPosition(self.index)
nextPos = Actions.getSuccessor(myPos, action)
return [(action, nextPos)]
"""
Iterate through all features (closest food, bias, ghost dist),
multiply each of the features' value to the feature's weight,
and return the sum of all these values to get the q-value.
"""
def getQValue(self, features, weights):
return features * weights
"""
Iterate through all features and for each feature, update
its weight values using the following formula:
w(i) = w(i) + alpha((reward + discount*value(nextState)) - Q(s,a)) * f(i)(s,a)
"""
def updateWeights(
self, gameState, action, rewardFunction, featureFunction, weights, learningRate
):
features = featureFunction(gameState, action)
nextState = self.getSuccessor(gameState, action)
reward = rewardFunction(gameState, nextState)
for feature in features:
correction = (
reward
+ self.discountRate * self.getValue(nextState, featureFunction, weights)
) - self.getQValue(features, weights)
weights[feature] = (
weights[feature] + learningRate * correction * features[feature]
)
"""
Iterate through all q-values that we get from all
possible actions, and return the highest q-value
"""
def getValue(self, nextState: GameState, featureFunction, weights):
qVals = []
legalActions = nextState.getLegalActions(self.index)
if len(legalActions) == 0:
return 0.0
else:
for action in legalActions:
features = featureFunction(nextState, action)
qVals.append(self.getQValue(features, weights))
return max(qVals)
def getOffensiveReward(self, gameState: GameState, nextState: GameState):
# Calculate the reward.
currentAgentState: AgentState = gameState.getAgentState(self.index)
nextAgentState: AgentState = nextState.getAgentState(self.index)
ghosts = self.getGhostLocs(gameState)
ghost_1_step = sum(
nextAgentState.getPosition()
in Actions.getLegalNeighbors(g, gameState.getWalls())
for g in ghosts
)
base_reward = -50 + nextAgentState.numReturned + nextAgentState.numCarrying
new_food_returned = nextAgentState.numReturned - currentAgentState.numReturned
score = self.getScore(nextState)
if ghost_1_step > 0:
base_reward -= 5
if score < 0:
base_reward += score
if new_food_returned > 0:
# return home with food get reward score
base_reward += new_food_returned * 10
return base_reward
def getDefensiveReward(self, gameState, nextState):
return 0
def getEscapeReward(self, gameState, nextState):
return 0
# ------------------------------- Feature Related Action Functions -------------------------------
def getOffensiveFeatures(self, gameState: GameState, action):
food = self.getFood(gameState)
currAgentState = gameState.getAgentState(self.index)
walls = gameState.getWalls()
ghosts = self.getGhostLocs(gameState)
# Initialize features
features = util.Counter()
nextState = self.getSuccessor(gameState, action)
# Successor Score
features["successorScore"] = (
self.getScore(nextState) / (walls.width + walls.height) * 10
)
# Bias
features["bias"] = 1.0
# Get the location of pacman after he takes the action
next_x, next_y = nextState.getAgentPosition(self.index)
# 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
)
dist_home = (
self.getMazeDistance(
(next_x, next_y), gameState.getInitialAgentPosition(self.index)
)
+ 1
)
features["chance-return-food"] = (currAgentState.numCarrying) * (
1 - dist_home / (walls.width + walls.height)
) # The closer to home, the larger food carried, more chance return food
# Closest food
dist = self.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"] = dist / (walls.width + walls.height)
else:
features["closest-food"] = 0
return features
def getOffensiveWeights(self):
return MixedAgent.QLWeights["offensiveWeights"]
def getEscapeFeatures(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)]
enemiesAround = [
a for a in enemies if not a.isPacman and a.getPosition() != None
]
if len(enemiesAround) > 0:
dists = [
self.getMazeDistance(myPos, a.getPosition()) for a in enemiesAround
]
features["enemyDistance"] = min(dists)
if action == Directions.STOP:
features["stop"] = 1
features["distanceToHome"] = self.getMazeDistance(myPos, self.startPosition)
return features
def getEscapeWeights(self):
return MixedAgent.QLWeights["escapeWeights"]
def getDefensiveFeatures(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
team = [successor.getAgentState(i) for i in self.getTeam(successor)]
team_dist = self.getMazeDistance(team[0].getPosition(), team[1].getPosition())
features["teamDistance"] = team_dist
# 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 getDefensiveWeights(self):
return MixedAgent.QLWeights["defensiveWeights"]
def closestFood(self, pos, food, walls):
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 stateClosestFood(self, gameState: GameState):
pos = gameState.getAgentPosition(self.index)
food = self.getFood(gameState)
walls = gameState.getWalls()
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 getSuccessor(self, gameState: 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 getGhostLocs(self, gameState: GameState):
ghosts = []
opAgents = CaptureAgent.getOpponents(self, gameState)
# Get ghost locations and states if observable
if opAgents:
for opponent in opAgents:
opPos = gameState.getAgentPosition(opponent)
opIsPacman = gameState.getAgentState(opponent).isPacman
if opPos and not opIsPacman:
ghosts.append(opPos)
return ghosts