-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubmission.py
397 lines (336 loc) · 14.9 KB
/
submission.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
from util import manhattanDistance
from game import Directions
import random, util
from typing import Any, DefaultDict, List, Set, Tuple
from game import Agent
from pacman import GameState
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 __init__(self):
self.lastPositions = []
self.dc = None
def getAction(self, gameState: GameState):
"""
getAction chooses among the best options according to the evaluation function.
getAction takes a GameState and returns some Directions.X for some X in the set {North, South, West, East}
------------------------------------------------------------------------------
Description of GameState and helper functions:
A GameState specifies the full game state, including the food, capsules,
agent configurations and score changes. In this function, the |gameState| argument
is an object of GameState class. Following are a few of the helper methods that you
can use to query a GameState object to gather information about the present state
of Pac-Man, the ghosts and the maze.
gameState.getLegalActions(agentIndex):
Returns the legal actions for the agent specified. Returns Pac-Man's legal moves by default.
gameState.generateSuccessor(agentIndex, action):
Returns the successor state after the specified agent takes the action.
Pac-Man is always agent 0.
gameState.getPacmanState():
Returns an AgentState object for pacman (in game.py)
state.configuration.pos gives the current position
state.direction gives the travel vector
gameState.getGhostStates():
Returns list of AgentState objects for the ghosts
gameState.getNumAgents():
Returns the total number of agents in the game
gameState.getScore():
Returns the score corresponding to the current state of the game
The GameState class is defined in pacman.py and you might want to look into that for
other helper methods, though you don't need to.
"""
# 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
return legalMoves[chosenIndex]
def evaluationFunction(self, currentGameState: GameState, action: str) -> float:
"""
The evaluation function takes in the current GameState (defined in pacman.py)
and a proposed action and returns a rough estimate of the resulting successor
GameState's value.
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.
"""
# 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]
return successorGameState.getScore()
def scoreEvaluationFunction(currentGameState: GameState):
"""
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)
######################################################################################
# Problem 1b: implementing minimax
class MinimaxAgent(MultiAgentSearchAgent):
"""
Your minimax agent (problem 1)
"""
def getAction(self, gameState: GameState) -> str:
"""
Returns the minimax action from the current gameState using self.depth
and self.evaluationFunction. Terminal states can be found by one of the following:
pacman won, pacman lost or there are no legal moves.
Don't forget to limit the search depth using self.depth. Also, avoid modifying
self.depth directly (e.g., when implementing depth-limited search) since it
is a member variable that should stay fixed throughout runtime.
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.getScore():
Returns the score corresponding to the current state of the game
gameState.isWin():
Returns True if it's a winning state
gameState.isLose():
Returns True if it's a losing state
self.depth:
The depth to which search should continue
"""
# BEGIN_YOUR_CODE (our solution is 20 lines of code, but don't worry if you deviate from this)
def maxValue(gameState,agentIndex,currentDepth):
v=(float("-inf"),Directions.STOP)
nextAgent=(agentIndex + 1)
for action in gameState.getLegalActions(agentIndex):
nextGameState=gameState.generateSuccessor(agentIndex,action)
newVal=minimaxValue(nextGameState,nextAgent,currentDepth)
if newVal>v[0]:
v=(newVal,action)
return v
def minValue(gameState,agentIndex,currentDepth):
v=(float("inf"), Directions.STOP)
nextAgent=(agentIndex+1)
if nextAgent==gameState.getNumAgents():
nextAgent=0
currentDepth-=1
for action in gameState.getLegalActions(agentIndex):
nextGameState=gameState.generateSuccessor(agentIndex,action)
newVal=minimaxValue(nextGameState,nextAgent,currentDepth)
if newVal<v[0]:
v=(newVal,action)
return v
def minimaxValue(gameState,agentIndex,currentDepth):
if gameState.isLose() or gameState.isWin():
return gameState.getScore()
if currentDepth<=0:
return self.evaluationFunction(gameState)
if agentIndex==0:
return maxValue(gameState,agentIndex,currentDepth)[0]
else:
return minValue(gameState,agentIndex,currentDepth)[0]
return maxValue(gameState,0,self.depth)[1]
#raise Exception("Not implemented yet")
# END_YOUR_CODE
######################################################################################
# Problem 2a: implementing alpha-beta
class AlphaBetaAgent(MultiAgentSearchAgent):
"""
Your minimax agent with alpha-beta pruning (problem 2)
You may reference the pseudocode for Alpha-Beta pruning here:
en.wikipedia.org/wiki/Alpha%E2%80%93beta_pruning#Pseudocode
"""
def getAction(self, gameState: GameState) -> str:
"""
Returns the minimax action using self.depth and self.evaluationFunction
"""
# BEGIN_YOUR_CODE (our solution is 36 lines of code, but don't worry if you deviate from this)
def maxValue(gameState,agentIndex,currentDepth,alpha,beta):
v=(float("-inf"),Directions.STOP)
nextAgent=(agentIndex+1)
for action in gameState.getLegalActions(agentIndex):
nextGameState=gameState.generateSuccessor(agentIndex,action)
newVal=minimaxValue(nextGameState,nextAgent,currentDepth,alpha,beta)
if newVal>v[0]:
v=(newVal,action)
alpha=max(alpha,v[0])
if v[0]>beta:
return v
return v
def minValue(gameState,agentIndex,currentDepth,alpha,beta):
v=(float("inf"),Directions.STOP)
nextAgent=(agentIndex+1)
if nextAgent==gameState.getNumAgents():
nextAgent=0
currentDepth-=1
for action in gameState.getLegalActions(agentIndex):
nextGameState=gameState.generateSuccessor(agentIndex,action)
newVal=minimaxValue(nextGameState,nextAgent,currentDepth,alpha,beta)
if newVal<v[0]:
v=(newVal,action)
beta=min(beta,v[0])
if v[0]<alpha:
return v
return v
def minimaxValue(gameState,agentIndex,currentDepth,alpha,beta):
if gameState.isLose() or gameState.isWin():
return gameState.getScore()
if currentDepth<=0:
return self.evaluationFunction(gameState)
if agentIndex==0:
return maxValue(gameState, agentIndex, currentDepth,alpha,beta)[0]
else:
return minValue(gameState,agentIndex,currentDepth,alpha,beta)[0]
return maxValue(gameState,0,self.depth,float("-inf"),float("inf"))[1]
#raise Exception("Not implemented yet")
# END_YOUR_CODE
######################################################################################
# Problem 3b: implementing expectimax
class ExpectimaxAgent(MultiAgentSearchAgent):
"""
Your expectimax agent (problem 3)
"""
def getAction(self, gameState: GameState) -> str:
"""
Returns the expectimax action using self.depth and self.evaluationFunction
All ghosts should be modeled as choosing uniformly at random from their
legal moves.
"""
# BEGIN_YOUR_CODE (our solution is 20 lines of code, but don't worry if you deviate from this)
def maxValue(gameState,agentIndex,currentDepth):
v=(float("-inf"),Directions.STOP)
nextAgent=(agentIndex+1)
for action in gameState.getLegalActions(agentIndex):
nextGameState=gameState.generateSuccessor(agentIndex,action)
newVal=minimaxValue(nextGameState,nextAgent,currentDepth)
if newVal>v[0]:
v=(newVal,action)
return v
def expectimaxValue(gameState,agentIndex,currentDepth):
v=[0.0]
nextAgent=(agentIndex+1)
if nextAgent==gameState.getNumAgents():
nextAgent=0
currentDepth-=1
for action in gameState.getLegalActions(agentIndex):
nextGameState=gameState.generateSuccessor(agentIndex,action)
newVal=minimaxValue(nextGameState,nextAgent,currentDepth)
v.append(newVal)
v.pop(0)
return sum(v)/len(v)
def minimaxValue(gameState,agentIndex,currentDepth):
if gameState.isLose() or gameState.isWin():
return gameState.getScore()
if currentDepth<=0:
return self.evaluationFunction(gameState)
if agentIndex==0:
return maxValue(gameState,agentIndex,currentDepth)[0]
else:
return expectimaxValue(gameState,agentIndex,currentDepth)
return maxValue(gameState,0,self.depth)[1]
#raise Exception("Not implemented yet")
# END_YOUR_CODE
######################################################################################
# Problem 4a (extra credit): creating a better evaluation function
def betterEvaluationFunction(currentGameState: GameState) -> float:
"""
Your extreme, unstoppable evaluation function (problem 4). Note that you can't fix a seed in this function.
"""
# BEGIN_YOUR_CODE (our solution is 13 lines of code, but don't worry if you deviate from this)
currentPacmanPosition = currentGameState.getPacmanPosition()
food = currentGameState.getFood()
foodAsList = food.asList()
ghostStates = currentGameState.getGhostStates()
huntingGhosts = []
scaredGhosts = []
scaredTimes=[]
for ghost in ghostStates:
if ghost.scaredTimer:
scaredTimes.append(ghost.scaredTimer)
scaredGhosts.append(ghost)
else:
huntingGhosts.append(ghost)
capsules=currentGameState.getCapsules()
remainingFood=len(foodAsList)
remainingCapsules=len(capsules)
currentScore = currentGameState.getScore()
distToClosestFood = float("inf")
invDistanceToClosestFood=0
for item in foodAsList:
dist = util.manhattanDistance(currentPacmanPosition, item)
if dist < distToClosestFood:
distToClosestFood = dist
if distToClosestFood>0:
invDistanceToClosestFood=1/distToClosestFood
if len(foodAsList)<3:
invDistanceToClosestFood=100000
if len(foodAsList)==1:
invDistToClosestFood=500000
distToClosestCapsules = float("inf")
invDistToClosestCapsule=0
if remainingCapsules == 0:
distToClosestCapsules = 0
for item in capsules:
dist = util.manhattanDistance(currentPacmanPosition, item)
if dist < distToClosestCapsules:
distToClosestCapsules = dist
if distToClosestCapsules>0:
invDistToClosestCapsule=1/distToClosestCapsules
distToHuntingGhost=float("inf")
for ghost in huntingGhosts:
dist = util.manhattanDistance(currentPacmanPosition, ghost.getPosition())
if dist < distToHuntingGhost:
distToHuntingGhost = dist
if len(scaredGhosts) == 0:
distToScaredGhost = 0
scaredTime=0
else:
distToScaredGhost = float("inf")
for ghost in scaredGhosts:
dist = util.manhattanDistance(currentPacmanPosition, ghost.getPosition())
if dist < distToScaredGhost:
distToScaredGhost = dist
scaredTime=scaredTimes[0]
invDistToHuntingGhost = 0
if distToHuntingGhost > 0:
invDistToHuntingGhost = 1.0 / distToHuntingGhost
invDistToScaredGhost = 0
if distToScaredGhost > 0:
invDistToScaredGhost = 1.0 / distToScaredGhost
score=currentGameState.getScore()\
-2*invDistToHuntingGhost\
+15*scaredTime*invDistToScaredGhost\
-2*remainingFood\
-3*invDistToClosestCapsule\
-1*distToClosestFood
return score
#raise Exception("Not implemented yet")
# END_YOUR_CODE
# Abbreviation
better = betterEvaluationFunction