Discrete Bayesian filtering python programming code Practice

Examples of Background

Suppose you have a robot trapped in a pipe at its inside, and can only go forward or backward. The tube length is 20cm. After the robot receive a "forward" command has three possible "there is a 25% probability is not implemented, there is a 50% probability forward 1cm, there is a 25% probability forward 2cm". Of course there are special circumstances when the robot is only 1cm from the end when it "does not execute command 25% probability 75% probability of only 1cm away." When the robot at the ends of it stuck. Now 11cm robot in that place, I sent 9 "forward" command to control it. After asking the robot executes this command in the probability of each nine is the number of positions (ie, determination of probability distribution located in various locations of the robot).

analysis

It is 11cm initial position in that place, which means the robot located at 19 other places probability is 0, and the probability that place is 11cm 1. Note: This robot is located in 20 places and the probabilities add up to equal 1 (because they are inside a probability distribution).
Represents the probability that the array Jiangzi following:
[0 0,000,000,001,000,000,000]

Next, we analyze it received under the first forward command how to operate

In front of the background information from the robot can be seen now in 11th place so there are three cases (only near the end it may be less than three kinds)

25% probability is not Run
the 50% probability forward a
25% probability proceeds 2
so probability array is updated to
[00000000001 0.25 1 0.5 1 * 0.25 0000000] = [0000000000 0.25 0.5 0.25 0,000,000]

Next look forward command it received a second how changes in
this particular case it is still not close to the endpoint. But a little complicated. Because there are three positions (11, 12, 13) need to be considered.
First considered only when the robot is in position 11 of the case (substitutes 0 value on 12-bit and 13-bit probability)
[0 0 0 0 0 0 0 0 0 0 0.25 0.25 0.25 0.5 0.25 0.25 0 0 0 0 0 0 0]
now considered only when the robot is at the first 12 bits (a 0 instead of the value of impact 11 and 13 probability)
[0 0 0 0 0 0 0 0 0 0 0 0.5
0.25 0.5 0.5 0.5 0.25 0 0 0 0 0 0]
now considered only when the robot is at the 13 cases (substitutes 0 value on 11 and 12 probability)
[0 0 0 0 0 0 0 0 0 0 0 0 0.25 0.25 0.25 0.5 0.25 0.25 0 0 0 0 0]
Therefore the final array changes the probability of receiving the second command proceeds probability front of the array of three cases and:
[0000000000 0.25
0.25 0.25 0.5 0.25 0.25 0,000,000 ] + [00000000000 0.5 0.25 0.5 0.5 0.50.25 0 0 0 0 0 0]+[0 0 0 0 0 0 0 0 0 0 0 0 0.250.25 0.250.5 0.250.25 0 0 0 0 0]

Behind the change is more complex we achieve it with some Python code (check code wrong standard did not add up to the number 20 is equal to 1).

#coding=utf-8

import numpy as np
# hstack	水平堆叠序列中的数组(列方向)
#一共20个数据,在第11位的地方写1
prob = np.hstack((np.zeros(10),1,np.zeros(9)))
prob_update = np.zeros(20)#待更新的数列
# _ 是一个临时变量,前进9次,当前位置是11,距离终点20还有9
for _ in range(9):
    for i in range(prob.shape[0]):# shape输出维度
        if i == (prob.shape[0]-1):#如果就是在端点那就会100%不执行任何操作
            prob_update[i] += prob[i]
            pass
        elif i == (prob.shape[0]-2):
            #如果是离端点只有一位,那25%不执行任何操作,75%跳到下一位
            prob_update[i] += prob[i]*0.25
            prob_update[i+1] += prob[i]*0.75
            pass
        else:
            #只要i不是靠近端点,就有三种情况
            prob_update[i] += prob[i]*0.25
            prob_update[i+1] += prob[i]*0.5
            prob_update[i+2] += prob[i]*0.25
            pass
        pass
    prob = prob_update
    print(np.sum(prob))
    prob_update = np.zeros(20)
    
import matplotlib.pyplot as plt
plt.xticks(np.arange(0, 20, 1))
plt.imshow(np.expand_dims(prob,axis=0),cmap=plt.get_cmap('Greys'))
plt.show()

9 can be seen through the execution of the command robot is a great probability at the 20 (subscript below this figure is zero).
Here Insert Picture Description
Original link: https: //blog.csdn.net/varyshare/article/details/100098979

Published 34 original articles · won praise 2 · Views 2303

Guess you like

Origin blog.csdn.net/weixin_44088559/article/details/105390067
Recommended