def elapseTime(self): transProb = util.loadTransProb() #transProb = util.loadTransProb() belief = util.Belief(self.belief.getNumRows(), self.belief.getNumCols(), 0.0) #belief = self.getBelief() for row in xrange(self.belief.getNumRows()): for col in xrange(self.belief.getNumCols()): if row in transProb and col in transProb[row]: for newRow in transProb[row][col]: for newCol, prob in transProb[row][col][newRow].iteritems(): belief.addProb(newRow, newCol, prob*self.belief.getProb(row, col)) else: belief.setProb(row, col, self.getBelief().getProb(row, col)) #print belief.getSum() #belief.normalize() self.belief = belief
def elapseTime(self): ''' your code here''' numRows = self.belief.getNumRows() numCols = self.belief.getNumCols() transProb = util.loadTransProb() tempProb = [] for row in range(0, numRows): temp = [] for col in range(0, numCols): temp.append(self.belief.getProb(row, col)) self.belief.setProb(row, col, 0) tempProb.append(temp) for row in range(0, numRows): for col in range(0, numCols): # p: sum of transitionProbability * p(xt|evidence1:t) if (row, col) in transProb: for newPos, transitionP in transProb[(row, col)].iteritems(): self.belief.addProb(newPos[0], newPos[1], transitionP * tempProb[row][col])
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 = 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 __init__(self, numRows, numCols): ''' initialize any variables you will need later ''' self.belief = util.Belief(numRows, numCols) self.transprob = util.loadTransProb() self.probdic = {} self.realtemp = {} for key in self.transprob: if not key[0] in self.probdic: self.probdic[key[0]] = {} self.realtemp[key[0]] = key[1] self.probdic[key[0]][key[1]] = self.transprob[(key[0], key[1])] else: self.probdic[key[0]][key[1]] = self.transprob[(key[0], key[1])] # move form transprob -> prodic # make a random particle keys = self.probdic.keys() self.randomPart = {} # key = tile = (row, col) for i in range(self.NUM_PARTICLES): randomIndex = random.choice(keys) if not randomIndex in self.randomPart: self.randomPart[randomIndex] = 1 else: self.randomPart[randomIndex] += 1 # update belief NumRow = self.belief.getNumRows() NumCol = self.belief.getNumCols() new = util.Belief(NumRow, NumCol, 0) # its 0 make linear times O(numtile) 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.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 __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 __init__(self, numRows, numCols): self.skipElapse = False ### ONLY USED BY GRADER.PY in case problem 3 has not been completed self.belief = util.Belief(numRows, numCols) self.transProb = util.loadTransProb()
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()