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 Sat Apr 4 19:02:39 201534@author: Roger5"""678import numpy as np9import numpy.random as random10import matplotlib.pyplot as plt111213def bar_plot(pos, ylim=(0,1), title=None):14plt.cla()15ax = plt.gca()16x = np.arange(len(pos))17ax.bar(x, pos, color='#30a2da')18if ylim:19plt.ylim(ylim)20plt.xticks(x+0.4, x)21if title is not None:22plt.title(title)23242526def normalize(belief):27s = sum(belief)28belief /= s2930def update(map_, belief, z, p_hit, p_miss):31for i, val in enumerate(map_):32if val == z:33belief[i] *= p_hit34else:35belief[i] *= p_miss3637belief = normalize(belief)383940def predict(prob_dist, offset, kernel):41N = len(prob_dist)42kN = len(kernel)43width = int((kN - 1) / 2)4445result = np.zeros(N)46for i in range(N):47for k in range (kN):48index = (i + (width-k) - offset) % N49result[i] += prob_dist[index] * kernel[k]50prob_dist[:] = result[:] # update belief515253def add_noise (Z, count):54n= len(Z)55for i in range(count):56j = random.randint(0,n)57Z[j] = random.randint(0,2)585960def animate_three_doors (loops=5):61world = np.array([1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0])62#world = np.array([1,1,1,1,1])63#world = np.array([1,0,1,0,1,0])646566f = DiscreteBayes1D(world)6768measurements = np.tile(world, loops)69add_noise(measurements, 50)7071for m in measurements:72f.sense (m, .8, .2)73f.update(1, (.05, .9, .05))7475bar_plot(f.belief)76plt.pause(0.01)777879def animate_book(loops=5):80world = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])81#world = np.array([1,1,1,1,1])82#world = np.array([1,0,1,0,1,0])8384N = len(world)85belief = np.array([1./N]*N)8687measurements = np.tile(world, loops)88add_noise(measurements, 5)8990for m in measurements:91update(world, belief, m, .8, .2)92predict(belief, 1, (.05, .9, .05))9394bar_plot(belief)95plt.pause(0.01)96print(f.belief)979899import random100101class Train(object):102103def __init__(self, track, kernel=[1.], sense_error=.1, no_sense_error=.05):104self.track = track105self.pos = 0106self.kernel = kernel107self.sense_error = sense_error108self.no_sense_error = no_sense_error109110111def move(self, distance=1):112""" move in the specified direction with some small chance of error"""113114self.pos += distance115116# insert random movement error according to kernel117r = random.random()118s = 0119offset = -(len(self.kernel) - 1) / 2120for k in self.kernel:121s += k122if r <= s:123break124offset += 1125126self.pos = (self.pos + offset) % len(self.track)127return self.pos128129def sense(self):130pos = self.pos131132# insert random sensor error133r = random.random()134if r < self.sense_error:135if random.random() > 0.5:136pos += 1137else:138pos -= 1139print('sense error')140return pos141142143def animate_train(loops=5):144world = np.array([1,2,3,4,5,6,7,8,9,10])145146N = len(world)147belief = np.zeros(N)148belief[0] = 1.0149150robot = Train(world, [.1, .8, .1], .1, .1)151152for i in range(N*loops):153robot.move(1)154m = robot.sense()155update(world, belief, m, .9, .1)156predict(belief, 1, (.05, .9, .05))157158bar_plot(belief)159plt.pause(0.1)160print(belief)161162animate_train(3)163164world = np.array([1,2,3,4,5,6,7,8,9,10])165#world = np.array([1,1,1,1,1])166#world = np.array([1,0,1,0,1,0])167168169170171