-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiAgents.py
381 lines (301 loc) · 13.9 KB
/
multiAgents.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
# multiAgents.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 util import manhattanDistance
from game import Directions
import random, util
from game import Agent
import math
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 (oldFood) 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()
oldFood = currentGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
ghost_pos = [ghostState.getPosition() for ghostState in newGhostStates]
food_dist = []
for i in oldFood.asList():
food_dist.append(manhattanDistance(newPos, i))
min_food = min(food_dist)
if oldFood[newPos[0]][newPos[1]] == True: x = 20
else: x= 0
min_ghost_dist = min([manhattanDistance(ghost_pos[x], newPos) for x in xrange(len(ghost_pos))])
return x - math.exp(-(min_ghost_dist-5)) - 1.5*min_food
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
Directions.STOP:
The stop direction, which is always legal
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
"""
actions_list = []
for a in gameState.getLegalActions(0):
action_val, ply = self.min_value(gameState.generateSuccessor(0, a), 0, 1)
actions_list.append((action_val, a))
take_action = max(actions_list)
#print(gameState.getNumAgents())
#print(take_action[0])
return take_action[1]
def min_value(self, gameState, ply, agent):
if gameState.isWin() or gameState.isLose() or ply == self.depth*gameState.getNumAgents()-1:
return (self.evaluationFunction(gameState), ply)
v = 1000000000000000000000000
for action in gameState.getLegalActions(agent):
if agent < gameState.getNumAgents()-1:
test_action, new_ply = self.min_value(gameState.generateSuccessor(agent,action), ply +1, agent+1)
v = min(v, test_action)
else:
test_action, new_ply = self.max_value(gameState.generateSuccessor(agent,action), ply +1, 0)
v = min(v, test_action)
return (v, new_ply)
def max_value(self, gameState, ply, agent):
if gameState.isWin() or gameState.isLose() or ply == self.depth*gameState.getNumAgents()-1: return self.evaluationFunction(gameState), ply
util_val = -1000000000000000000000
for a in gameState.getLegalActions(agent):
test_action, new_ply = self.min_value(gameState.generateSuccessor(agent, a), ply +1, agent +1 )
util_val = max(util_val, test_action)
return (util_val, new_ply)
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
"""
actions_list = []
#print gameState.getLegalActions(0)\
best_action = Directions.STOP
for a in gameState.getLegalActions(0):
action_val = self.min_value_ab(gameState.generateSuccessor(0, a), 0, 1, -10000000,10000000)
actions_list.append((action_val, a))
take_action = max(actions_list)
best_action = take_action[1]
#print(gameState.getNumAgents())
print(take_action[0])
return best_action
#return self.max_value_ab(gameState.generateSuccessor(0, a), 0, 1, -10000000,10000000)
def min_value_ab(self, gameState, ply, agent, alpha, beta):
# print alpha
# print beta
if gameState.isWin() or gameState.isLose() or ply == (self.depth*gameState.getNumAgents()): return (self.evaluationFunction(gameState))
v = 10000000
for a in gameState.getLegalActions(agent):
if agent < gameState.getNumAgents()-1:
test_action = self.min_value_ab(gameState.generateSuccessor(agent,a), ply +1, agent+1, alpha, beta)
v = min(v, test_action)
else:
test_action = self.max_value_ab(gameState.generateSuccessor(agent,a), ply +1, 0, alpha, beta)
#print test_action
v = min(v, test_action)
#print v
if v <= alpha: return v
beta = min(beta, v)
return v
def max_value_ab(self, gameState, ply, agent, alpha, beta):
if gameState.isWin() or gameState.isLose() or (ply == self.depth*gameState.getNumAgents()-1): return (self.evaluationFunction(gameState))
v = -10000000
for a in gameState.getLegalActions(agent):
test_action = self.min_value_ab(gameState.generateSuccessor(agent, a), ply +1, agent +1 , alpha, beta)
v = max(v, test_action)
if v >= beta: return v
alpha = max(alpha, v)
return v
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.
"""
actions_list = []
for a in gameState.getLegalActions(0):
if a == Directions.STOP: continue
action_val, ply = self.chance_val(gameState.generateSuccessor(0, a), 0, 1)
actions_list.append((action_val, a))
take_action = max(actions_list)
return take_action[1]
def max_value(self, gameState, ply, agent):
if gameState.isWin() or gameState.isLose() or ply == self.depth*gameState.getNumAgents()-1: return self.evaluationFunction(gameState), ply
util_val = -1000000000000000000000
for a in gameState.getLegalActions(agent):
test_action, new_ply = self.chance_val(gameState.generateSuccessor(agent, a), ply +1, agent +1 )
util_val = max(util_val, test_action)
return (util_val, new_ply)
def chance_val(self, gameState, ply, agent):
if gameState.isWin() or gameState.isLose() or ply == self.depth*gameState.getNumAgents()-1: return self.evaluationFunction(gameState), ply
p_move = 1/len(gameState.getLegalActions(agent))
v = 0
for a in gameState.getLegalActions(agent):
if agent < gameState.getNumAgents()-1:
test_action, new_ply = self.chance_val(gameState.generateSuccessor(agent,a), ply +1, agent+1)
v += p_move * test_action
else:
test_action, new_ply = self.max_value(gameState.generateSuccessor(agent,a), ply +1, 0)
v += p_move * test_action
return (v, new_ply)
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>
"""
pos = currentGameState.getPacmanPosition()
food = currentGameState.getFood()
ghostStates = currentGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in ghostStates]
num_ghosts = currentGameState.getNumAgents()
capsule_pos = currentGameState.getCapsules()
walls = currentGameState.getWalls()
ghost_pos = [ghostState.getPosition() for ghostState in ghostStates]
food_list = food.asList()
food_dist = []
food_proximity = 0
capsule_proximity = 0
for i in food_list:
food_dist.append(manhattanDistance(pos, i))
min_food = min(food_dist)
# for every food adjacent to pacman, increment food_proximity by 10
actions = currentGameState.getLegalActions(0)
if Directions.NORTH in actions:
if food[pos[0]][pos[1]+ 1]: food_proximity += 10
if (pos[0], pos[1]+ 1) in capsule_pos: capsule_proximity += 10
if Directions.SOUTH in actions:
if food[pos[0]][pos[1]- 1]: food_proximity += 10
if (pos[0], pos[1]- 1) in capsule_pos: capsule_proximity += 10
if Directions.EAST in actions:
if food[pos[0]+ 1][pos[1]]: food_proximity += 10
if (pos[0]+ 1, pos[1]) in capsule_pos: capsule_proximity += 10
if Directions.WEST in actions:
if food[pos[0]- 1][pos[1]]: food_proximity += 10
if (pos[0]- 1, pos[1]) in capsule_pos: capsule_proximity += 10
min_ghost_dist = min([manhattanDistance(ghost_pos[x], pos) for x in xrange(len(ghost_pos))])
min_scare_time = min(newScaredTimes)
#print food_proximity
# print "Min scare time " + str(min_scare_time)
# print "min_ghost_dist exp " + str(-math.exp(-(min_ghost_dist-5)))
return_val = 0
c_dist = []
for c_pos in capsule_pos:
c_dist.append(manhattanDistance(pos, c_pos))
if c_dist: return_val += 10*1./ min(c_dist)
if c_dist: return_val += 1./len(c_dist)
if min_ghost_dist <= 1:
#return_val -= 20
print "on"
if newScaredTimes[0]>0:
if min_ghost_dist != 0 and min_ghost_dist <2:
return_val -= 10* 1./min_ghost_dist
else:
return_val -= 0
return_val += (1./len(food_list))
#return_val += 1./min_food
return_val += 10.* 1./min_food
return_val += food_proximity
#return_val += currentGameState.getScore()
#better state has higher eval function
#print food_proximity + capsule_proximity - math.exp(-(min_ghost_dist-3)) - min_food
#print return_val
return return_val
"""
things that are important:
-want less food on the board ----->1/len food increases as num food decreases
-want to be closer to food -------> 1/min_food increases as min_food gets smaller
-want to be closer to capsules ------> 1/c_dist inc as ghost gets closer to capsule
-want to be far from ghosts -----> 1/ghost dist inc as ghosts get close
1/
"""
# Abbreviation
better = betterEvaluationFunction
class ContestAgent(MultiAgentSearchAgent):
"""
Your agent for the mini-contest
"""
def getAction(self, gameState):
"""
Returns an action. You can use any method you want and search to any depth you want.
Just remember that the mini-contest is timed, so you have to trade off speed and computation.
Ghosts don't behave randomly anymore, but they aren't perfect either -- they'll usually
just make a beeline straight towards Pacman (or away from him if they're scared!)
"""
"*** YOUR CODE HERE ***"
util.raiseNotDefined()