-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgents.py
323 lines (264 loc) · 12 KB
/
Agents.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
from os import DirEntry
from telnetlib import GA
from util import manhattanDistance
from game import Directions
import random
import util
from typing import Any, DefaultDict, List, Set, Tuple
import numpy as np
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.
"""
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}
"""
# 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]
# Pick randomly among the best
chosenIndex = random.choice(bestIndices)
return legalMoves[chosenIndex]
def evaluationFunction(self,currentGameState, action):
"""
The evaluation function takes in the current and proposed successor
GameStates (pacman.py) and returns a number, where higher numbers are better.
"""
# 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]
#Calculate distance to the nearest food
newFoodList = np.array(newFood.asList())
distanceToFood = [util.manhattanDistance(newPos, food) for food in newFoodList]
min_food_distance = 0
if len(newFoodList) > 0:
min_food_distance = distanceToFood[np.argmin(distanceToFood)]
#Calculate the distance to nearest ghost
ghostPositions = np.array(successorGameState.getGhostPositions())
distanceToGhost = [util.manhattanDistance(newPos, ghost) for ghost in ghostPositions]
min_ghost_distance = 0
nearestGhostScaredTime = 0
if len(ghostPositions) > 0:
min_ghost_distance = distanceToGhost[np.argmin(distanceToGhost)]
nearestGhostScaredTime = newScaredTimes[np.argmin(distanceToGhost)]
# avoid certain death
if min_ghost_distance <= 1 and nearestGhostScaredTime == 0:
return -999999
# eat a scared ghost
if min_ghost_distance <= 1 and nearestGhostScaredTime > 0:
return 999999
value = successorGameState.getScore() - min_food_distance
if nearestGhostScaredTime > 0:
# follow ghosts if scared
value -= min_ghost_distance
else:
value += min_ghost_distance
return value
def scoreEvaluationFunction(successorGameState: 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()
newPos = successorGameState.getPacmanPosition()
newFood = successorGameState.getFood()
newGhostStates = successorGameState.getGhostStates()
newScaredTimes = [ghostState.scaredTimer for ghostState in newGhostStates]
#Calculate distance to the nearest food
newFoodList = np.array(newFood.asList())
distanceToFood = [util.manhattanDistance(newPos, food) for food in newFoodList]
min_food_distance = 0
if len(newFoodList) > 0:
min_food_distance = distanceToFood[np.argmin(distanceToFood)]
#Calculate the distance to nearest ghost
ghostPositions = np.array(successorGameState.getGhostPositions())
distanceToGhost = [util.manhattanDistance(newPos, ghost) for ghost in ghostPositions]
min_ghost_distance = 0
nearestGhostScaredTime = 0
if len(ghostPositions) > 0:
min_ghost_distance = distanceToGhost[np.argmin(distanceToGhost)]
nearestGhostScaredTime = newScaredTimes[np.argmin(distanceToGhost)]
# avoid certain death
if min_ghost_distance <= 1 and nearestGhostScaredTime == 0:
return -999999
# eat a scared ghost
if min_ghost_distance <= 1 and nearestGhostScaredTime > 0:
return 999999
value = successorGameState.getScore() - min_food_distance
if nearestGhostScaredTime > 0:
# follow ghosts if scared
value -= min_ghost_distance
else:
value += min_ghost_distance
return value
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='4'):
self.index = 0 # Pacman is always agent index 0
self.evaluationFunction = util.lookup(evalFn, globals())
self.depth = int(depth)
class MinimaxAgent(MultiAgentSearchAgent):
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.
"""
def V_minimax(state:GameState,agent_index,current_depth)-> tuple:
#-> (score,action)
# the case that game is end
if state.isLose() or state.isWin():
return(state.getScore(),Directions.STOP)
else:
if current_depth == 0:
return(scoreEvaluationFunction(state),Directions.STOP)
#current_depth >0
else:
next_index = agent_index + 1
next_depth = current_depth
if next_index == state.getNumAgents() :
next_index = 0
next_depth -= 1
assert agent_index < state.getNumAgents()
if agent_index == 0: #max layer
bestVal = -float('inf')
bestMove = None
for action in state.getLegalActions(agent_index):
value = V_minimax(state.generateSuccessor(agent_index,action),next_index,\
next_depth)[0]
if value > bestVal:
bestVal = value
bestMove = action
return bestVal,bestMove
else: #min layer
bestVal = float('inf')
bestMove = None
for action in state.getLegalActions(agent_index):
value = V_minimax(state.generateSuccessor(agent_index,action),next_index,\
next_depth)[0]
if value < bestVal:
bestVal = value
bestMove = action
return bestVal,bestMove
score,action = V_minimax(gameState,self.index,self.depth)
return action
class AlphaBetaAgent(MultiAgentSearchAgent):
def getAction(self, gameState: GameState) -> str:
"""
Returns the minimax action using self.depth and self.evaluationFunction
"""
def alpha_beta(state:GameState,depth:int,alpha:float,beta:float,agent_index:int)->tuple:
#alpha_beta ->(score,action)
if state.isLose() or state.isWin(): return state.getScore(),Directions.STOP
#######
elif depth == 0:return scoreEvaluationFunction(state),Directions.STOP
#######
else:
next_index = agent_index + 1
next_depth = depth
if next_index == state.getNumAgents():
next_index = 0
next_depth -= 1
if agent_index == 0: #max level
best_val = -float('inf')
bestMove = None
for action in state.getLegalActions():
value = alpha_beta(state.generateSuccessor(agent_index,action)\
,next_depth,alpha,beta,next_index)[0]
if value > best_val:
best_val = value
bestMove = action
alpha = max(alpha,value)
if value >= beta: break
return best_val,bestMove
else: #min level
assert agent_index != 0 #checked
assert agent_index < state.getNumAgents()
best_val = float('inf')
bestMove = None
for action in state.getLegalActions(agent_index):
value = alpha_beta(state.generateSuccessor(agent_index,action)\
,next_depth,alpha,beta,next_index)[0]
if value < best_val:
best_val = value
bestMove = action
beta = min(beta,value)
if alpha >= value: break
return best_val,bestMove
alpha = -float('inf')
beta = float('inf')
score,action = alpha_beta(gameState,self.depth,alpha,beta,self.index)
return action
class ExpectimaxAgent(MultiAgentSearchAgent):
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.
"""
def Vem(gameState, depth, index):
legalMoves = gameState.getLegalActions(index)
if gameState.isWin() or gameState.isLose() or len(legalMoves) == 0:
return gameState.getScore()
elif depth == 0:
return self.evaluationFunction(gameState)
if index == 0: # Pacman's turn
score_list = []
for action in legalMoves:
score_list.append(
Vem(gameState.generateSuccessor(index, action), depth, index + 1))
return max(score_list)
elif index == gameState.getNumAgents() - 1: # Last ghost turn
score_list = []
for action in legalMoves:
score_list.append(
Vem(gameState.generateSuccessor(index, action), depth-1, 0))
return sum(score_list) / len(score_list)
else: # Non-last ghost turn
score_list = []
for action in legalMoves:
score_list.append(
Vem(gameState.generateSuccessor(index, action), depth, index+1))
return sum(score_list) / len(score_list)
legalMoves = gameState.getLegalActions()
# Choose one of the best actions
scores = []
for action in legalMoves:
scores.append(Vem(gameState.generateSuccessor(
self.index, action), self.depth, self.index + 1))
bestScore = max(scores)
bestIndices = [index for index in range(
len(scores)) if scores[index] == bestScore]
chosenIndex = random.choice(bestIndices)
return legalMoves[chosenIndex]