-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmultiAgents.py
221 lines (188 loc) · 9.36 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
# 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
# ([email protected]) and Dan Klein ([email protected]).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel ([email protected]).
from game import Directions
import random, util
import numpy as np
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.
"""
def getAction(self, gameState):
"""
getAction chooses among the best options according to the evaluation function.
"""
# Collect legal moves and successor states
legalMoves = gameState.getLegalActions()
# Choose one of the best actions
scores = [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]
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.
"""
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):
def minimax(self, agent, depth, gameState):
if gameState.isLose() or gameState.isWin() or depth == self.depth:
return self.evaluationFunction(gameState)
if agent == 0: # maximize for pacman
return max(self.minimax(1, depth, gameState.generateSuccessor(agent, action)) for action in
getLegalActionsNoStop(0, gameState))
else: # minimize for ghosts
nextAgent = agent + 1 # get the next agent
if gameState.getNumAgents() == nextAgent:
nextAgent = 0
if nextAgent == 0: # increase depth every time all agents have moved
depth += 1
return min(self.minimax(nextAgent, depth, gameState.generateSuccessor(agent, action)) for action in
getLegalActionsNoStop(agent, gameState))
def getAction(self, gameState):
"""
Returns the minimax action from the current gameState using self.depth
and self.evaluationFunction.
"""
possibleActions = getLegalActionsNoStop(0, gameState)
action_scores = [self.minimax(0, 0, gameState.generateSuccessor(0, action)) for action
in possibleActions]
max_action = max(action_scores)
max_indices = [index for index in range(len(action_scores)) if action_scores[index] == max_action]
chosenIndex = random.choice(max_indices)
return possibleActions[chosenIndex]
class AlphaBetaAgent(MultiAgentSearchAgent):
def alphabeta(self, agent, depth, gameState, alpha, beta):
if gameState.isLose() or gameState.isWin() or depth == self.depth:
return self.evaluationFunction(gameState)
if agent == 0: # maximize for pacman
value = -999999
for action in getLegalActionsNoStop(agent, gameState):
value = max(value, self.alphabeta(1, depth, gameState.generateSuccessor(agent, action), alpha, beta))
alpha = max(alpha, value)
if beta <= alpha: # alpha-beta pruning
break
return value
else: # minimize for ghosts
nextAgent = agent + 1 # get the next agent
if gameState.getNumAgents() == nextAgent:
nextAgent = 0
if nextAgent == 0: # increase depth every time all agents have moved
depth += 1
for action in getLegalActionsNoStop(agent, gameState):
value = 999999
value = min(value, self.alphabeta(nextAgent, depth, gameState.generateSuccessor(agent, action), alpha, beta))
beta = min(beta, value)
if beta <= alpha: # alpha-beta pruning
break
return value
def getAction(self, gameState):
"""
Returns the minimax action from the current gameState using self.depth
and self.evaluationFunction using alpha-beta pruning.
"""
possibleActions = getLegalActionsNoStop(0, gameState)
alpha = -999999
beta = 999999
action_scores = [self.alphabeta(0, 0, gameState.generateSuccessor(0, action), alpha, beta) for action
in possibleActions]
max_action = max(action_scores)
max_indices = [index for index in range(len(action_scores)) if action_scores[index] == max_action]
chosenIndex = random.choice(max_indices)
return possibleActions[chosenIndex]
class ExpectimaxAgent(MultiAgentSearchAgent):
def expectimax(self, agent, depth, gameState):
if gameState.isLose() or gameState.isWin() or depth == self.depth:
return self.evaluationFunction(gameState)
if agent == 0: # maximize for pacman
return max(self.expectimax(1, depth, gameState.generateSuccessor(agent, action)) for action in
getLegalActionsNoStop(0, gameState))
else: # minimize for ghosts
nextAgent = agent + 1 # get the next agent
if gameState.getNumAgents() == nextAgent:
nextAgent = 0
if nextAgent == 0: # increase depth every time all agents have moved
depth += 1
return sum(self.expectimax(nextAgent, depth, gameState.generateSuccessor(agent, action)) for action in
getLegalActionsNoStop(agent, gameState)) / float(len(getLegalActionsNoStop(agent, gameState)))
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.
"""
possibleActions = getLegalActionsNoStop(0, gameState)
action_scores = [self.expectimax(0, 0, gameState.generateSuccessor(0, action)) for action
in possibleActions]
max_action = max(action_scores)
max_indices = [index for index in range(len(action_scores)) if action_scores[index] == max_action]
chosenIndex = random.choice(max_indices)
return possibleActions[chosenIndex]
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()
def evaluationFunction(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 getLegalActionsNoStop(index, gameState):
possibleActions = gameState.getLegalActions(index)
if Directions.STOP in possibleActions:
possibleActions.remove(Directions.STOP)
return possibleActions