Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
| Download
Project: test
Views: 91872# -*- coding: utf-8 -*-1"""2Created on Fri May 2 11:48:55 201434@author: rlabbe5"""6from __future__ import division7from __future__ import print_function89import copy10import numpy as np11import numpy.random as random12import matplotlib.pyplot as plt1314''' should this be a class? seems like both sense and update are very15problem specific16'''171819def bar_plot(pos, ylim=(0,1), title=None):20plt.cla()21ax = plt.gca()22x = np.arange(len(pos))23ax.bar(x, pos, color='#30a2da')24if ylim:25plt.ylim(ylim)26plt.xticks(x+0.4, x)27if title is not None:28plt.title(title)293031class DiscreteBayes1D(object):3233def __init__(self, world_map, belief=None):34self.world_map = copy.deepcopy(world_map)35self.N = len(world_map)3637if belief is None:38# create belief, make all equally likely39self.belief = np.empty(self.N)40self.belief.fill (1./self.N)4142else:43self.belief = copy.deepcopy(belief)4445# This will be used to temporarily store calculations of the new46# belief. 'k' just means 'this iteration'.47self.belief_k = np.empty(self.N)4849assert self.belief.shape == self.world_map.shape505152def _normalize (self):53s = sum(self.belief)54self.belief = self.belief/s5556def sense(self, Z, pHit, pMiss):57for i in range (self.N):58hit = (self.world_map[i] ==Z)59self.belief_k[i] = self.belief[i] * (pHit*hit + pMiss*(1-hit))6061# copy results to the belief vector using swap-copy idiom62self.belief, self.belief_k = self.belief_k, self.belief63self._normalize()6465def update(self, U, kernel):66N = self.N67kN = len(kernel)68width = int((kN - 1) / 2)6970self.belief_k.fill(0)7172for i in range(N):73for k in range (kN):74index = (i + (width-k)-U) % N75#print(i,k,index)76self.belief_k[i] += self.belief[index] * kernel[k]7778# copy results to the belief vector using swap-copy idiom79self.belief, self.belief_k = self.belief_k, self.belief8081def add_noise (Z, count):82n= len(Z)83for i in range(count):84j = random.randint(0,n)85Z[j] = random.randint(0,2)868788def animate_three_doors (loops=5):89world = np.array([1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0])90#world = np.array([1,1,1,1,1])91#world = np.array([1,0,1,0,1,0])929394f = DiscreteBayes1D(world)9596measurements = np.tile(world,loops)97add_noise(measurements, 4)9899for m in measurements:100f.sense (m, .8, .2)101f.update(1, (.05, .9, .05))102103bar_plot(f.belief)104plt.pause(0.01)105106107def _test_filter():108def is_near_equal(a,b):109try:110assert sum(abs(a-b)) < 0.001111except:112print(a, b)113assert False114115def test_update_1():116f = DiscreteBayes1D(np.array([0,0,1,0,0]), np.array([0,0,.8,0,0]))117f.update (1, [1])118is_near_equal (f.belief, np.array([0,0,0,.8,0]))119120f.update (1, [1])121is_near_equal (f.belief, np.array([0,0,0,0,.8]))122123f.update (1, [1])124is_near_equal (f.belief, np.array([.8,0,0,0,0]))125126f.update (-1, [1])127is_near_equal (f.belief, np.array([0,0,0,0,.8]))128129f.update (2, [1])130is_near_equal (f.belief, np.array([0,.8,0,0,0]))131132f.update (5, [1])133is_near_equal (f.belief, np.array([0,.8,0,0,0]))134135136def test_undershoot():137f = DiscreteBayes1D(np.array([0,0,1,0,0]), np.array([0,0,.8,0,0]))138f.update (2, [.2, .8,0.])139is_near_equal (f.belief, np.array([0,0,0,.16,.64]))140141def test_overshoot():142f = DiscreteBayes1D(np.array([0,0,1,0,0]), np.array([0,0,.8,0,0]))143f.update (2, [0, .8, .2])144is_near_equal (f.belief, np.array([.16,0,0,0,.64]))145146147test_update_1()148test_undershoot()149150if __name__ == "__main__":151152_test_filter()153154155156animate_three_doors(loops=1)157158159160161162163