-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmultiAgents.py
More file actions
executable file
·375 lines (323 loc) · 17.4 KB
/
multiAgents.py
File metadata and controls
executable file
·375 lines (323 loc) · 17.4 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
# multiAgents.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).
from util import manhattanDistance
from game import Directions
import random, util
from game import Agent
class ReflexAgent(Agent):
"""
A reflex agent chooses an action at each choice point by examining
its alternatives via a state evaluation function.
The code below is provided as a guide. You are welcome to change
it in any way you see fit, so long as you don't touch our method
headers.
"""
def getAction(self, gameState):
"""
You do not need to change this method, but you're welcome to.
getAction chooses among the best options according to the evaluation function.
Just like in the previous project, getAction takes a GameState and returns
some Directions.X for some X in the set {NORTH, SOUTH, WEST, EAST, STOP}
"""
# Collect legal moves and successor states
legalMoves = gameState.getLegalActions()
# Choose one of the best actions
scores = [self.evaluationFunction(gameState, action) for action in legalMoves]
bestScore = max(scores)
bestIndices = [index for index in range(len(scores)) if scores[index] == bestScore]
chosenIndex = random.choice(bestIndices) # Pick randomly among the best
"Add more of your code here if you want to"
return legalMoves[chosenIndex]
def evaluationFunction(self, currentGameState, action):
"""
Design a better evaluation function here.
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
The code below extracts some useful information from the state, like the
remaining food (newFood) and Pacman position after moving (newPos).
newScaredTimes holds the number of moves that each ghost will remain
scared because of Pacman having eaten a power pellet.
Print out these variables to see what you're getting, then combine them
to create a masterful evaluation function.
"""
# Useful information you can extract from a GameState (pacman.py)
successorGameState = currentGameState.generatePacmanSuccessor(action)
newPos = successorGameState.getPacmanPosition()
newFood = successorGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
"*** YOUR CODE HERE ***"
# 通过鬼怪和当前pacman的位置计算危险值
# 将所有鬼怪离当前的位置全部计算出来
Ghosts = [manhattanDistance(ghost.configuration.pos, newPos) for ghost in newGhostStates]
# 求最近的鬼怪的曼哈顿距离,其他的鬼怪可以不计
nearestGhost = min(Ghosts)
# 以为吃到豆豆可以+10,设置为-20可以抵消吃掉豆豆的得分和下方的豆豆启发值
# 为什么小于2,因为大于等于2的鬼怪,不要考虑呀,否则,豆豆就老是躲着鬼怪
dangousScore = -1000 if nearestGhost<2 else 0
# 计算最近的豆豆,对自己的影响
if len(newFood.asList())>0:
Foods = [manhattanDistance(food, newPos) for food in newFood.asList()]
# 求最近的豆豆的距离
nearestFood = min(Foods)
# 豆豆离我越近,其启发值就必须越大!吃一个豆豆+10,但是还要消耗1点精力移动,所以吃掉隔壁的豆豆的9分
foodHeuristic = 9/nearestFood
else:
foodHeuristic = 0
# 最后把下一个状态的得分也计入评价值结果
return successorGameState.getScore() + dangousScore + foodHeuristic
def scoreEvaluationFunction(currentGameState):
"""
This default evaluation function just returns the score of the state.
The score is the same one displayed in the Pacman GUI.
This evaluation function is meant for use with adversarial search agents
(not reflex agents).
"""
return currentGameState.getScore()
class MultiAgentSearchAgent(Agent):
"""
This class provides some common elements to all of your
multi-agent searchers. Any methods defined here will be available
to the MinimaxPacmanAgent, AlphaBetaPacmanAgent & ExpectimaxPacmanAgent.
You *do not* need to make any changes here, but you can if you want to
add functionality to all your adversarial search agents. Please do not
remove anything, however.
Note: this is an abstract class: one that should not be instantiated. It's
only partially specified, and designed to be extended. Agent (game.py)
is another abstract class.
"""
def __init__(self, evalFn = 'scoreEvaluationFunction', depth = '2'):
self.index = 0 # Pacman is always agent index 0
self.evaluationFunction = util.lookup(evalFn, globals())
self.depth = int(depth)
class MinimaxAgent(MultiAgentSearchAgent):
"""
Your minimax agent (question 2)
"""
def getAction(self, gameState):
"""
Returns the minimax action from the current gameState using self.depth
and self.evaluationFunction.
Here are some method calls that might be useful when implementing minimax.
gameState.getLegalActions(agentIndex):
Returns a list of legal actions for an agent
agentIndex=0 means Pacman, ghosts are >= 1
gameState.generateSuccessor(agentIndex, action):
Returns the successor game state after an agent takes an action
gameState.getNumAgents():
Returns the total number of agents in the game
gameState.isWin():
Returns whether or not the game state is a winning state
gameState.isLose():
Returns whether or not the game state is a losing state
"""
"*** YOUR CODE HERE ***"
# 一开始肯定是先从吃豆人的行动开始遍历,这样的返回结果表示吃豆人的行动方案
maxVal = -float('inf')
bestAction = None
for action in gameState.getLegalActions(0):
# 求出所有可行的action中,哪一个是最优的
# 参数中的0表示搜索深度从0开始,1表示下一个agent的Index是1,即第一个鬼怪
value = self.getMin(gameState.generateSuccessor(0, action),0,1)
# 通过比较最优值,将对应的action记录下来
if value>maxVal:
maxVal = value
bestAction = action
# 最后,只需将最优行动返回给吃豆人即可
return bestAction
# getMax主要是计算吃豆人选择最佳的动作
def getMax(self,gameState,depth=0,agentIndex=0):
# 如果达到搜索深度,则将当前状态的评价值返回
if depth == self.depth:
return self.evaluationFunction(gameState)
# 如果接下来没有可行的行动,也要终止迭代
if len(gameState.getLegalActions(agentIndex)) == 0:
return self.evaluationFunction(gameState)
# 获得吃豆人的所有可行操作,并进行遍历
maxVal = -float('inf')
for action in gameState.getLegalActions(agentIndex):
# 参数中最后的“1”,表示接下来的动作是计算鬼怪的行动影响
value = self.getMin(gameState.generateSuccessor(agentIndex, action),depth,agentIndex+1)
if value>maxVal:
maxVal = value
return maxVal
# getMin主要是计算鬼怪选择造成最坏影响的动作
def getMin(self,gameState,depth=0,agentIndex=1):
# 如果达到搜索深度,则将当前状态的评价值返回
if depth == self.depth:
return self.evaluationFunction(gameState)
# 如果接下来没有可行的行动,也要终止迭代
if len(gameState.getLegalActions(agentIndex)) == 0:
return self.evaluationFunction(gameState)
# 获得当前鬼怪的所有可行操作,并进行遍历
minVal = float('inf')
for action in gameState.getLegalActions(agentIndex):
# 如果你是最后一个鬼怪的agent,那么接下来就要去计算吃豆人的行动,否则就去计算下一个鬼怪的行动
if agentIndex == gameState.getNumAgents()-1:
# 参数中最后的“0”,表示接下来的动作是计算吃豆人的行动影响
value = self.getMax(gameState.generateSuccessor(agentIndex, action),depth+1,0)
else:
# 参数中最后的agentIndex(大于1),表示接下来的动作是计算鬼怪的行动影响
value = self.getMin(gameState.generateSuccessor(agentIndex, action),depth,agentIndex+1)
if value<minVal:
minVal = value
return minVal
class AlphaBetaAgent(MultiAgentSearchAgent):
"""
Your minimax agent with alpha-beta pruning (question 3)
"""
def getAction(self, gameState):
"""
Returns the minimax action using self.depth and self.evaluationFunction
"""
"*** YOUR CODE HERE ***"
# 一开始肯定是先从吃豆人的行动开始,所以直接调用getMax函数
maxVal, bestAction = self.getMax(gameState)
return bestAction
# 上述语句的另外一种写法
return self.getMax(gameState)[1]
# getMax主要是计算吃豆人选择最佳的动作
def getMax(self,gameState,depth=0,agentIndex=0,alpha=-float('inf'),beta=float('inf')):
# 如果达到搜索深度,则将当前状态的评价值返回
if depth == self.depth:
return self.evaluationFunction(gameState),None
# 如果接下来没有可行的行动,也要终止迭代
if len(gameState.getLegalActions(agentIndex)) == 0:
return self.evaluationFunction(gameState),None
# 获得吃豆人的所有可行操作,并进行遍历
maxVal = -float('inf')
bestAction = None
for action in gameState.getLegalActions(agentIndex):
# 参数中最后的“1”,表示接下来的动作是计算鬼怪的行动影响
value = self.getMin(gameState.generateSuccessor(agentIndex, action),depth,agentIndex+1,alpha,beta)[0]
if value>maxVal:
maxVal = value
bestAction = action
# 如果v>beta,
if value>beta:
return value,action
alpha = value if value>alpha else alpha
return maxVal,bestAction
# getMin主要是计算鬼怪选择造成最坏影响的动作
def getMin(self,gameState,depth=0,agentIndex=1,alpha=-float('inf'),beta=float('inf')):
# 如果达到搜索深度,则将当前状态的评价值返回
if depth == self.depth:
return self.evaluationFunction(gameState),None
# 如果接下来没有可行的行动,也要终止迭代
if len(gameState.getLegalActions(agentIndex)) == 0:
return self.evaluationFunction(gameState),None
# 获得当前鬼怪的所有可行操作,并进行遍历
minVal = float('inf')
bestAction = None
for action in gameState.getLegalActions(agentIndex):
# 如果你是最后一个鬼怪的agent,那么接下来就要去计算吃豆人的行动,否则就去计算下一个鬼怪的行动
if agentIndex == gameState.getNumAgents()-1:
# 参数中最后的“0”,表示接下来的动作是计算吃豆人的行动影响
value = self.getMax(gameState.generateSuccessor(agentIndex, action),depth+1,0,alpha,beta)[0]
else:
# 参数中最后的agentIndex(大于1),表示接下来的动作是计算鬼怪的行动影响
value = self.getMin(gameState.generateSuccessor(agentIndex, action),depth,agentIndex+1,alpha,beta)[0]
if value<minVal:
minVal = value
bestAction = action
if value<alpha:
return value,action
beta = value if value<beta else beta # 这个条件选择语句和C语言中"exp1?exp2:exp3"一样
return minVal,bestAction
class ExpectimaxAgent(MultiAgentSearchAgent):
"""
Your expectimax agent (question 4)
"""
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.
"""
"*** YOUR CODE HERE ***"
return self.getMax(gameState)[1]
# 与Minimax算法一样,getMax主要是计算吃豆人的最佳行动
def getMax(self,gameState,depth=0,agentIndex=0):
# 如果达到搜索深度,则将当前状态的评价值返回
if depth == self.depth:
return self.evaluationFunction(gameState),None
# 如果接下来吃豆人已经没有可行的行动,也要终止迭代
if len(gameState.getLegalActions(agentIndex)) == 0:
return self.evaluationFunction(gameState),None
# 获得吃豆人的所有可行操作,并进行遍历
maxVal = -float('inf')
bestAction = None
for action in gameState.getLegalActions(agentIndex):
# 参数中最后的“1”,表示接下来的动作是计算鬼怪的行动影响
value = self.getExpect(gameState.generateSuccessor(agentIndex, action),depth,agentIndex+1)
if value>maxVal:
maxVal = value
bestAction = action
return maxVal,bestAction
# getExpect主要是计算鬼怪选择造成影响的状态的效用值,即各种可能的状态的效用值平均
def getExpect(self,gameState,depth,agentIndex=1):
# 如果达到搜索深度,则将当前状态的评价值返回
if depth == self.depth:
return self.evaluationFunction(gameState)
# 如果接下来没有可行的行动,也要终止迭代
if len(gameState.getLegalActions(agentIndex)) == 0:
return self.evaluationFunction(gameState)
# 获得当前鬼怪的所有可行操作,并进行遍历,求ExpectValue
totalUtil = 0
for action in gameState.getLegalActions(agentIndex):
# 如果当前是最后一个鬼怪的agent,那么下一次轮到吃豆人
if agentIndex == gameState.getNumAgents()-1:
# 参数中最后的“0”,表示接下来的动作是计算吃豆人的行动影响
value = self.getMax(gameState.generateSuccessor(agentIndex, action),depth+1,0)[0]
# 因为当前的步骤依然是鬼怪的行动,所以即便下一步是吃豆人的行动,本次计算中依然要求ExpectValue
totalUtil += value
else:
# 参数中最后的agentIndex(大于1),表示接下来的动作是计算鬼怪的行动影响
value = self.getExpect(gameState.generateSuccessor(agentIndex, action),depth,agentIndex+1)
totalUtil += value
# 将totalUtil除以所有可行的动作数,求得平均值,并返回
return totalUtil/len(gameState.getLegalActions(agentIndex))
def betterEvaluationFunction(currentGameState):
"""
Your extreme ghost-hunting, pellet-nabbing, food-gobbling, unstoppable
evaluation function (question 5).
DESCRIPTION: <write something here so we know what you did>
"""
"*** YOUR CODE HERE ***"
# 获得计算需要的初始信息,包括吃豆人位置、食物、鬼怪以及鬼怪为惊吓状态的剩余时间
pacmanPos = currentGameState.getPacmanPosition()
foods = currentGameState.getFood().asList()
ghostStates = currentGameState.getGhostStates()
scaredTime = [ghost.scaredTimer for ghost in ghostStates]
# 先计算最近的食物对吃豆人的影响
if len(foods)>0:
Foods = [manhattanDistance(food, pacmanPos) for food in foods]
# 求最近的豆豆的距离
nearestFood = min(Foods)
# 豆豆离我越近,其启发值就必须越大!吃一个豆豆+10,但是还要消耗1点精力移动,所以吃掉隔壁的豆豆得9分
foodHeuristic = 0
else:
foodHeuristic = 0
# 通过鬼怪和当前pacman的位置计算危险值
if len(ghostStates)>0:
# 将所有鬼怪离当前的位置全部计算出来
Ghosts = [manhattanDistance(ghost.configuration.pos, pacmanPos) for ghost in ghostStates]
# 求最近的鬼怪的曼哈顿距离,其他的鬼怪可以不计
nearestGhost = min(Ghosts)
dangousScore = -1000 if nearestGhost<2 else 0
# 尽量让鬼怪保持惊吓状态,因为这种状态下的鬼怪可以被吃豆人吃掉
totalScaredTimes = sum(scaredTime)
# 最后把下一个状态的得分也计入评价值结果
return currentGameState.getScore() + foodHeuristic + dangousScore + totalScaredTimes
# Abbreviation
better = betterEvaluationFunction