-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
413 lines (356 loc) · 15 KB
/
inference.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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# inference.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]).
import util
from collections import Counter
import itertools
import random
import busters
import game
from util import manhattanDistance, raiseNotDefined
class DiscreteDistribution(dict):
"""
A DiscreteDistribution models belief distributions and weight distributions
over a finite set of discrete keys.
"""
def __getitem__(self, key):
self.setdefault(key, 0)
return dict.__getitem__(self, key)
def copy(self):
"""
Return a copy of the distribution.
"""
return DiscreteDistribution(dict.copy(self))
def argMax(self):
"""
Return the key with the highest value.
"""
if len(self.keys()) == 0:
return None
all = list(self.items())
values = [x[1] for x in all]
maxIndex = values.index(max(values))
return all[maxIndex][0]
def total(self):
"""
Return the sum of values for all keys.
"""
return float(sum(self.values()))
def normalize(self):
"""
Normalize the distribution such that the total value of all keys sums
to 1. The ratio of values for all keys will remain the same. In the case
where the total value of the distribution is 0, do nothing.
"""
"*** CS5368 Fall 2023 YOUR CODE HERE ***"
total = self.total()
if total != 0:
for key in self.keys():
self[key] /= total
" you should normalize variable self"
#raiseNotDefined()
def sample(self):
"""
Draw a random sample from the distribution and return the key, weighted
by the values associated with each key.
>>> dist = DiscreteDistribution()
>>> dist['a'] = 1
>>> dist['b'] = 2
>>> dist['c'] = 2
>>> dist['d'] = 0
>>> N = 100000.0
>>> samples = [dist.sample() for _ in range(int(N))]
>>> round(samples.count('a') * 1.0/N, 1) # proportion of 'a'
0.2
>>> round(samples.count('b') * 1.0/N, 1)
0.4
>>> round(samples.count('c') * 1.0/N, 1)
0.4
>>> round(samples.count('d') * 1.0/N, 1)
0.0
"""
population = list(self.keys())
weights = list(self.values())
return random.choices(population, weights=weights)[0]
class InferenceModule:
"""
An inference module tracks a belief distribution over a ghost's location.
"""
############################################
# Useful methods for all inference modules #
############################################
def __init__(self, ghostAgent):
"""
Set the ghost agent for later access.
"""
self.ghostAgent = ghostAgent
self.index = ghostAgent.index
self.obs = [] # most recent observation position
def getJailPosition(self):
return (2 * self.ghostAgent.index - 1, 1)
def getPositionDistributionHelper(self, gameState, pos, index, agent):
try:
jail = self.getJailPosition()
gameState = self.setGhostPosition(gameState, pos, index + 1)
except TypeError:
jail = self.getJailPosition(index)
gameState = self.setGhostPositions(gameState, pos)
pacmanPosition = gameState.getPacmanPosition()
ghostPosition = gameState.getGhostPosition(index + 1) # The position you set
dist = DiscreteDistribution()
if pacmanPosition == ghostPosition: # The ghost has been caught!
dist[jail] = 1.0
return dist
pacmanSuccessorStates = game.Actions.getLegalNeighbors(pacmanPosition, \
gameState.getWalls()) # Positions Pacman can move to
if ghostPosition in pacmanSuccessorStates: # Ghost could get caught
mult = 1.0 / float(len(pacmanSuccessorStates))
dist[jail] = mult
else:
mult = 0.0
actionDist = agent.getDistribution(gameState)
for action, prob in actionDist.items():
successorPosition = game.Actions.getSuccessor(ghostPosition, action)
if successorPosition in pacmanSuccessorStates: # Ghost could get caught
denom = float(len(actionDist))
dist[jail] += prob * (1.0 / denom) * (1.0 - mult)
dist[successorPosition] = prob * ((denom - 1.0) / denom) * (1.0 - mult)
else:
dist[successorPosition] = prob * (1.0 - mult)
return dist
def getPositionDistribution(self, gameState, pos, index=None, agent=None):
"""
Return a distribution over successor positions of the ghost from the
given gameState. You must first place the ghost in the gameState, using
setGhostPosition below.
"""
if index == None:
index = self.index - 1
if agent == None:
agent = self.ghostAgent
return self.getPositionDistributionHelper(gameState, pos, index, agent)
def getObservationProb(self, noisyDistance, pacmanPosition, ghostPosition, jailPosition):
"""
Return the probability P(noisyDistance | pacmanPosition, ghostPosition).
"""
"*** CS5368 Fall 2023 YOUR CODE HERE ***"
if ghostPosition == jailPosition:
if noisyDistance == None:
return 1
else:
return 0
if noisyDistance == None:
return 0
trueDistance = manhattanDistance(pacmanPosition, ghostPosition)
return float(busters.getObservationProbability(noisyDistance, trueDistance))
"""
here is a procesure for you
if noisyDistance is none, special case check the project discription (should return something)
if ghost is in jail position, special case check the project discription (should return something)
use manhatten distance to estimate the distance (actual)
use busters.getObservationProbability to estimate the noisy distance
return that noisy distance
"""
raiseNotDefined()
def setGhostPosition(self, gameState, ghostPosition, index):
"""
Set the position of the ghost for this inference module to the specified
position in the supplied gameState.
Note that calling setGhostPosition does not change the position of the
ghost in the GameState object used for tracking the true progression of
the game. The code in inference.py only ever receives a deep copy of
the GameState object which is responsible for maintaining game state,
not a reference to the original object. Note also that the ghost
distance observations are stored at the time the GameState object is
created, so changing the position of the ghost will not affect the
functioning of observe.
"""
conf = game.Configuration(ghostPosition, game.Directions.STOP)
gameState.data.agentStates[index] = game.AgentState(conf, False)
return gameState
def setGhostPositions(self, gameState, ghostPositions):
"""
Sets the position of all ghosts to the values in ghostPositions.
"""
for index, pos in enumerate(ghostPositions):
conf = game.Configuration(pos, game.Directions.STOP)
gameState.data.agentStates[index + 1] = game.AgentState(conf, False)
return gameState
def observe(self, gameState):
"""
Collect the relevant noisy distance observation and pass it along.
"""
distances = gameState.getNoisyGhostDistances()
if len(distances) >= self.index: # Check for missing observations
obs = distances[self.index - 1]
self.obs = obs
self.observeUpdate(obs, gameState)
def initialize(self, gameState):
"""
Initialize beliefs to a uniform distribution over all legal positions.
"""
self.legalPositions = [p for p in gameState.getWalls().asList(False) if p[1] > 1]
self.allPositions = self.legalPositions + [self.getJailPosition()]
self.initializeUniformly(gameState)
######################################
# Methods that need to be overridden #
######################################
def initializeUniformly(self, gameState):
"""
Set the belief state to a uniform prior belief over all positions.
"""
raise NotImplementedError
def observeUpdate(self, observation, gameState):
"""
Update beliefs based on the given distance observation and gameState.
"""
raise NotImplementedError
def elapseTime(self, gameState):
"""
Predict beliefs for the next time step from a gameState.
"""
raise NotImplementedError
def getBeliefDistribution(self):
"""
Return the agent's current belief state, a distribution over ghost
locations conditioned on all evidence so far.
"""
raise NotImplementedError
class ExactInference(InferenceModule):
"""
The exact dynamic inference module should use forward algorithm updates to
compute the exact belief function at each time step.
"""
def initializeUniformly(self, gameState):
"""
Begin with a uniform distribution over legal ghost positions (i.e., not
including the jail position).
"""
self.beliefs = DiscreteDistribution()
for p in self.legalPositions:
self.beliefs[p] = 1.0
self.beliefs.normalize()
def observeUpdate(self, observation, gameState):
"""
Update beliefs based on the distance observation and Pacman's position.
The observation is the noisy Manhattan distance to the ghost you are
tracking.
self.allPositions is a list of the possible ghost positions, including
the jail position. You should only consider positions that are in
self.allPositions.
The update model is not entirely stationary: it may depend on Pacman's
current position. However, this is not a problem, as Pacman's current
position is known.
"""
"*** CS5368 Fall 2023 YOUR CODE HERE ***"
'''
procedure:
1- get pacman position
2- get jail posisition
3- get the current belief
4- loop over all positions to
4.1. get the probability of noisy position given the actual (q1 implimentation is needed here)
4.2. update the belief of that position
5- normalize
'''
jailPosition = self.getJailPosition()
pacmanPosition = gameState.getPacmanPosition()
ghostBelief = self.beliefs
for position in self.allPositions:
probability = self.getObservationProb(observation, pacmanPosition , position , jailPosition )
previos_pos = ghostBelief[position ]
ghostBelief[position ] = probability * previos_pos
ghostBelief.normalize()
#raiseNotDefined()
def elapseTime(self, gameState):
"""
Predict beliefs in response to a time step passing from the current
state.
The transition model is not entirely stationary: it may depend on
Pacman's current position. However, this is not a problem, as Pacman's
current position is known.
"""
"*** CS5368 Fall 2023 YOUR CODE HERE ***"
'''
procedure:
1- get the beliefs and new beliefs (from DiscreteDistribution())
2- for all avialable positions
update the pprobability of trasitioning to new state (use getPositionDistribution)
3- then calculate the new beliefs
4- set self.beliefs to the newest ones
'''
# Get Pacman's position
pacman_position = gameState.getPacmanPosition()
# all possible ghost positions
all_ghost_positions = self.allPositions
# new distribution to store updated beliefs
new_beliefs = DiscreteDistribution()
# Iterate over all ghost positions
for ghost_position in all_ghost_positions:
# distribution of possible next positions for the ghost
action_distribution = self.getPositionDistribution(gameState, ghost_position)
# Iterate over possible next positions and update beliefs
for new_position, probability in action_distribution.items():
# If new position is not in distribution, initialize it
if new_position not in new_beliefs:
new_beliefs[new_position] = 0
# Update the belief for the new position based on the ghost's current position
new_beliefs[new_position] += self.beliefs[ghost_position] * probability
# Update the beliefs with the new distribution
self.beliefs = new_beliefs
# raiseNotDefined()
def getBeliefDistribution(self):
return self.beliefs
def evenlyDistributedParticles(numParticles, legalPositions):
"""§
Generates a sequence of particles evenly distributed in the grid
in a semi-deterministic way.
"""
numCopies = numParticles // len(legalPositions)
numRemaining = numParticles % len(legalPositions)
particles = legalPositions * numCopies
particles += random.sample(legalPositions, k=numRemaining)
return particles
class MarginalInference(InferenceModule):
"""
A wrapper around the JointInference module that returns marginal beliefs
about ghosts.
"""
def initializeUniformly(self, gameState):
"""
Set the belief state to an initial, prior value.
"""
if self.index == 1:
jointInference.initialize(gameState, self.legalPositions)
jointInference.addGhostAgent(self.ghostAgent)
def observe(self, gameState):
"""
Update beliefs based on the given distance observation and gameState.
"""
if self.index == 1:
jointInference.observe(gameState)
def elapseTime(self, gameState):
"""
Predict beliefs for a time step elapsing from a gameState.
"""
if self.index == 1:
jointInference.elapseTime(gameState)
def getBeliefDistribution(self):
"""
Return the marginal belief over a particular ghost by summing out the
others.
"""
jointDistribution = jointInference.getBeliefDistribution()
dist = DiscreteDistribution()
for t, prob in jointDistribution.items():
dist[t[self.index - 1]] += prob
return dist