From 3de2f7a615562645b2fb6e71798c1dee65323031 Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Wed, 3 Jul 2024 16:08:32 +0000 Subject: [PATCH] tracking q5 --- tracking/inference.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/tracking/inference.py b/tracking/inference.py index e1463f1..2ec727d 100644 --- a/tracking/inference.py +++ b/tracking/inference.py @@ -354,7 +354,11 @@ class DiscreteDistribution(dict): {} """ "*** YOUR CODE HERE ***" - raiseNotDefined() + total_prob = self.total() + if total_prob == 0: + return + for key in self.keys(): + self[key] /= total_prob "*** END YOUR CODE HERE ***" def sample(self): @@ -379,7 +383,15 @@ class DiscreteDistribution(dict): 0.0 """ "*** YOUR CODE HERE ***" - raiseNotDefined() + total_prob = self.total() + if total_prob == 0: + return None + rand_num = random.random() * total_prob + for key in self.keys(): + rand_num -= self[key] + if rand_num <= 0: + return key + return key "*** END YOUR CODE HERE ***" @@ -454,7 +466,19 @@ class InferenceModule: Return the probability P(noisyDistance | pacmanPosition, ghostPosition). """ "*** YOUR CODE HERE ***" - raiseNotDefined() + if(jailPosition==ghostPosition): + if noisyDistance==None: + return 1 + else: + return 0 + if noisyDistance==None: + # the ghost is in the jail + if ghostPosition==jailPosition: + return 1 + else: + return 0 + true_distance=manhattanDistance(pacmanPosition,ghostPosition) + return busters.getObservationProbability(noisyDistance,true_distance) "*** END YOUR CODE HERE ***" def setGhostPosition(self, gameState, ghostPosition, index):