def __init__(self, numRows, numCols): self.belief = util.Belief(numRows, numCols) # Load the transition probabilities and store them in an integer-valued defaultdict. # Use self.transProbDict[oldTile][newTile] to get the probability of transitioning from oldTile to newTile. self.transProb = util.loadTransProb() self.transProbDict = dict() for (oldTile, newTile) in self.transProb: if not oldTile in self.transProbDict: self.transProbDict[oldTile] = collections.defaultdict(int) self.transProbDict[oldTile][newTile] = self.transProb[(oldTile, newTile)] # Initialize the particles randomly. self.particles = collections.defaultdict(int) potentialParticles = list(self.transProbDict.keys()) for i in range(self.NUM_PARTICLES): particleIndex = int(random.random() * len(potentialParticles)) self.particles[potentialParticles[particleIndex]] += 1 self.updateBelief()
def __init__(self, numRows, numCols): self.belief = util.Belief(numRows, numCols) # Load the transition probabilities and store them in a dict of Counters # self.transProbDict[oldTile][newTile] = probability of transitioning from oldTile to newTile self.transProb = util.loadTransProb() self.transProbDict = dict() for (oldTile, newTile) in self.transProb: if not oldTile in self.transProbDict: self.transProbDict[oldTile] = collections.Counter() self.transProbDict[oldTile][newTile] = \ self.transProb[(oldTile, newTile)] # Initialize the particles randomly self.particles = collections.Counter() potentialParticles = self.transProbDict.keys() for i in range(self.NUM_PARTICLES): particleIndex = int(random.random() * len(potentialParticles)) self.particles[potentialParticles[particleIndex]] += 1 self.updateBelief()
def elapseTime(self): if self.skipElapse: return ### ONLY FOR THE GRADER TO USE IN Problem 2 # BEGIN_YOUR_CODE (our solution is 7 lines of code, but don't worry if you deviate from this) # Create the set of beliefs for the new time step new_belief = util.Belief(self.belief.getNumRows(), self.belief.getNumCols(), value = 0.0) for key in self.transProb.keys(): # Unpack old and new coordinates of tiles old_x, old_y = key[0] new_x, new_y = key[1] # Unpack transition probability transitionProb = self.transProb[key] # Update probability # Get the original from the current belief oldProb = self.belief.getProb(old_x, old_y) # Update with the current transition probability newProb = oldProb*transitionProb # Set the new probability in the new set of beliefs # (we use addProb to sum as per the elimination formula) new_belief.addProb(new_x, new_y, newProb) self.belief = new_belief # Re-normalize after updating self.belief.normalize() # In-place modification, return NoneType return None
def __init__(self, numRows, numCols): self.belief = util.Belief(numRows, numCols) # Load the transition probabilities and store them in an integer-valued defaultdict. # Use self.transProbDict[oldTile][newTile] to get the probability of transitioning from oldTile to newTile. self.transProb = util.loadTransProb() self.transProbDict = dict() for (oldTile, newTile) in self.transProb: # 遍历转移概率的keys if not oldTile in self.transProbDict: # 同样,默认是指 键key 的集合. self.transProbDict[oldTile] = collections.defaultdict(int) self.transProbDict[oldTile][newTile] = self.transProb[(oldTile, newTile)] # 在构造 transProbDict - 它是一个字典的字典.(字典的嵌套) # Initialize the particles randomly. self.particles = collections.defaultdict( int) # 索引是一个tile的位置(row,col), 对应的值是该位置上的粒子个数. potentialParticles = list( self.transProbDict.keys()) # 这时候存的是各个可能存在粒子的位置. for i in range(self.NUM_PARTICLES): particleIndex = int(random.random() * len(potentialParticles)) self.particles[potentialParticles[particleIndex]] += 1 self.updateBelief()
def observe(self, agentX, agentY, observedDist): ''' your code here''' savedic = {} for key in self.randomPart: value = self.randomPart[key] mean = math.sqrt((abs(agentX - util.colToX(key[1])))**2 + (abs(agentY - util.rowToY(key[0])))**2) pdf = util.pdf(mean, Const.SONAR_STD, observedDist) newprob = value * pdf # prob in that tile and that tile's prob savedic[key] = newprob # pdf -> fill into savedic count = 0 for key in self.realtemp: count = count + 1 self.realtemp[key] = self.realtemp[key] # random self.randomPart = {} # initializing for i in range(self.NUM_PARTICLES): randIndex = util.weightedRandomChoice(savedic) if not randIndex in self.randomPart: self.randomPart[randIndex] = 1 else: self.randomPart[randIndex] += 1 count = count + 1 NumRow = self.belief.getNumRows() NumCol = self.belief.getNumCols() new = util.Belief(NumRow, NumCol, 0) for key in self.randomPart: new.setProb(key[0], key[1], self.randomPart[key]) new.normalize() self.belief = new
def __init__(self, numRows, numCols): self.skipElapse = False ### ONLY USED BY GRADER.PY in case problem 3 has not been completed # util.Belief is a class (constructor) that represents the belief for a single # inference state of a single car (see util.py). self.belief = util.Belief(numRows, numCols) self.transProb = util.loadTransProb()
def updateBelief(self): newBelief = util.Belief(self.belief.getNumRows(), self.belief.getNumCols(), 0) for tile in self.particles: newBelief.setProb(tile[0], tile[1], self.particles[tile]) newBelief.normalize() self.belief = newBelief
def __init__(self, numRows, numCols): self.skipElapse = False ### ONLY USED BY GRADER.PY in case problem 2 has not been completed self.belief = util.Belief(numRows, numCols) self.transProb = util.loadTransProb()
def __init__(self, numRows, numCols): self.belief = util.Belief(numRows, numCols) ''' initialize any variables you will need later ''' self.transprob = util.loadTransProb()
def __init__(self, numRows, numCols): self.belief = util.Belief(numRows, numCols)
def __init__(self, numRows, numCols): self.belief = util.Belief(numRows, numCols) ''' initialize any variables you will need later '''