CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

| Download
Project: test
Views: 91872
1
# -*- coding: utf-8 -*-
2
"""
3
Created on Fri May 2 11:48:55 2014
4
5
@author: rlabbe
6
"""
7
from __future__ import division
8
from __future__ import print_function
9
10
import copy
11
import numpy as np
12
import numpy.random as random
13
import matplotlib.pyplot as plt
14
15
''' should this be a class? seems like both sense and update are very
16
problem specific
17
'''
18
19
20
def bar_plot(pos, ylim=(0,1), title=None):
21
plt.cla()
22
ax = plt.gca()
23
x = np.arange(len(pos))
24
ax.bar(x, pos, color='#30a2da')
25
if ylim:
26
plt.ylim(ylim)
27
plt.xticks(x+0.4, x)
28
if title is not None:
29
plt.title(title)
30
31
32
class DiscreteBayes1D(object):
33
34
def __init__(self, world_map, belief=None):
35
self.world_map = copy.deepcopy(world_map)
36
self.N = len(world_map)
37
38
if belief is None:
39
# create belief, make all equally likely
40
self.belief = np.empty(self.N)
41
self.belief.fill (1./self.N)
42
43
else:
44
self.belief = copy.deepcopy(belief)
45
46
# This will be used to temporarily store calculations of the new
47
# belief. 'k' just means 'this iteration'.
48
self.belief_k = np.empty(self.N)
49
50
assert self.belief.shape == self.world_map.shape
51
52
53
def _normalize (self):
54
s = sum(self.belief)
55
self.belief = self.belief/s
56
57
def sense(self, Z, pHit, pMiss):
58
for i in range (self.N):
59
hit = (self.world_map[i] ==Z)
60
self.belief_k[i] = self.belief[i] * (pHit*hit + pMiss*(1-hit))
61
62
# copy results to the belief vector using swap-copy idiom
63
self.belief, self.belief_k = self.belief_k, self.belief
64
self._normalize()
65
66
def update(self, U, kernel):
67
N = self.N
68
kN = len(kernel)
69
width = int((kN - 1) / 2)
70
71
self.belief_k.fill(0)
72
73
for i in range(N):
74
for k in range (kN):
75
index = (i + (width-k)-U) % N
76
#print(i,k,index)
77
self.belief_k[i] += self.belief[index] * kernel[k]
78
79
# copy results to the belief vector using swap-copy idiom
80
self.belief, self.belief_k = self.belief_k, self.belief
81
82
def add_noise (Z, count):
83
n= len(Z)
84
for i in range(count):
85
j = random.randint(0,n)
86
Z[j] = random.randint(0,2)
87
88
89
def animate_three_doors (loops=5):
90
world = np.array([1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0])
91
#world = np.array([1,1,1,1,1])
92
#world = np.array([1,0,1,0,1,0])
93
94
95
f = DiscreteBayes1D(world)
96
97
measurements = np.tile(world,loops)
98
add_noise(measurements, 4)
99
100
for m in measurements:
101
f.sense (m, .8, .2)
102
f.update(1, (.05, .9, .05))
103
104
bar_plot(f.belief)
105
plt.pause(0.01)
106
107
108
def _test_filter():
109
def is_near_equal(a,b):
110
try:
111
assert sum(abs(a-b)) < 0.001
112
except:
113
print(a, b)
114
assert False
115
116
def test_update_1():
117
f = DiscreteBayes1D(np.array([0,0,1,0,0]), np.array([0,0,.8,0,0]))
118
f.update (1, [1])
119
is_near_equal (f.belief, np.array([0,0,0,.8,0]))
120
121
f.update (1, [1])
122
is_near_equal (f.belief, np.array([0,0,0,0,.8]))
123
124
f.update (1, [1])
125
is_near_equal (f.belief, np.array([.8,0,0,0,0]))
126
127
f.update (-1, [1])
128
is_near_equal (f.belief, np.array([0,0,0,0,.8]))
129
130
f.update (2, [1])
131
is_near_equal (f.belief, np.array([0,.8,0,0,0]))
132
133
f.update (5, [1])
134
is_near_equal (f.belief, np.array([0,.8,0,0,0]))
135
136
137
def test_undershoot():
138
f = DiscreteBayes1D(np.array([0,0,1,0,0]), np.array([0,0,.8,0,0]))
139
f.update (2, [.2, .8,0.])
140
is_near_equal (f.belief, np.array([0,0,0,.16,.64]))
141
142
def test_overshoot():
143
f = DiscreteBayes1D(np.array([0,0,1,0,0]), np.array([0,0,.8,0,0]))
144
f.update (2, [0, .8, .2])
145
is_near_equal (f.belief, np.array([.16,0,0,0,.64]))
146
147
148
test_update_1()
149
test_undershoot()
150
151
if __name__ == "__main__":
152
153
_test_filter()
154
155
156
157
animate_three_doors(loops=1)
158
159
160
161
162
163