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 Sat Apr 4 19:02:39 2015
4
5
@author: Roger
6
"""
7
8
9
import numpy as np
10
import numpy.random as random
11
import matplotlib.pyplot as plt
12
13
14
def bar_plot(pos, ylim=(0,1), title=None):
15
plt.cla()
16
ax = plt.gca()
17
x = np.arange(len(pos))
18
ax.bar(x, pos, color='#30a2da')
19
if ylim:
20
plt.ylim(ylim)
21
plt.xticks(x+0.4, x)
22
if title is not None:
23
plt.title(title)
24
25
26
27
def normalize(belief):
28
s = sum(belief)
29
belief /= s
30
31
def update(map_, belief, z, p_hit, p_miss):
32
for i, val in enumerate(map_):
33
if val == z:
34
belief[i] *= p_hit
35
else:
36
belief[i] *= p_miss
37
38
belief = normalize(belief)
39
40
41
def predict(prob_dist, offset, kernel):
42
N = len(prob_dist)
43
kN = len(kernel)
44
width = int((kN - 1) / 2)
45
46
result = np.zeros(N)
47
for i in range(N):
48
for k in range (kN):
49
index = (i + (width-k) - offset) % N
50
result[i] += prob_dist[index] * kernel[k]
51
prob_dist[:] = result[:] # update belief
52
53
54
def add_noise (Z, count):
55
n= len(Z)
56
for i in range(count):
57
j = random.randint(0,n)
58
Z[j] = random.randint(0,2)
59
60
61
def animate_three_doors (loops=5):
62
world = np.array([1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,0,0])
63
#world = np.array([1,1,1,1,1])
64
#world = np.array([1,0,1,0,1,0])
65
66
67
f = DiscreteBayes1D(world)
68
69
measurements = np.tile(world, loops)
70
add_noise(measurements, 50)
71
72
for m in measurements:
73
f.sense (m, .8, .2)
74
f.update(1, (.05, .9, .05))
75
76
bar_plot(f.belief)
77
plt.pause(0.01)
78
79
80
def animate_book(loops=5):
81
world = np.array([1, 1, 0, 0, 0, 0, 0, 0, 1, 0])
82
#world = np.array([1,1,1,1,1])
83
#world = np.array([1,0,1,0,1,0])
84
85
N = len(world)
86
belief = np.array([1./N]*N)
87
88
measurements = np.tile(world, loops)
89
add_noise(measurements, 5)
90
91
for m in measurements:
92
update(world, belief, m, .8, .2)
93
predict(belief, 1, (.05, .9, .05))
94
95
bar_plot(belief)
96
plt.pause(0.01)
97
print(f.belief)
98
99
100
import random
101
102
class Train(object):
103
104
def __init__(self, track, kernel=[1.], sense_error=.1, no_sense_error=.05):
105
self.track = track
106
self.pos = 0
107
self.kernel = kernel
108
self.sense_error = sense_error
109
self.no_sense_error = no_sense_error
110
111
112
def move(self, distance=1):
113
""" move in the specified direction with some small chance of error"""
114
115
self.pos += distance
116
117
# insert random movement error according to kernel
118
r = random.random()
119
s = 0
120
offset = -(len(self.kernel) - 1) / 2
121
for k in self.kernel:
122
s += k
123
if r <= s:
124
break
125
offset += 1
126
127
self.pos = (self.pos + offset) % len(self.track)
128
return self.pos
129
130
def sense(self):
131
pos = self.pos
132
133
# insert random sensor error
134
r = random.random()
135
if r < self.sense_error:
136
if random.random() > 0.5:
137
pos += 1
138
else:
139
pos -= 1
140
print('sense error')
141
return pos
142
143
144
def animate_train(loops=5):
145
world = np.array([1,2,3,4,5,6,7,8,9,10])
146
147
N = len(world)
148
belief = np.zeros(N)
149
belief[0] = 1.0
150
151
robot = Train(world, [.1, .8, .1], .1, .1)
152
153
for i in range(N*loops):
154
robot.move(1)
155
m = robot.sense()
156
update(world, belief, m, .9, .1)
157
predict(belief, 1, (.05, .9, .05))
158
159
bar_plot(belief)
160
plt.pause(0.1)
161
print(belief)
162
163
animate_train(3)
164
165
world = np.array([1,2,3,4,5,6,7,8,9,10])
166
#world = np.array([1,1,1,1,1])
167
#world = np.array([1,0,1,0,1,0])
168
169
170
171