2019-05-02-Sense函数

python

Posted by berlinfog on May 2, 2019

Sense函数

在这个笔记本中,让我们回顾一下机器人采取的步骤,即帮助将自己从最初的均匀分布本地化到感知和更新该分布。在这个notebook中,我们要回顾一下机器人使自己从最初的均匀分布本地化到感知与更新该分布。

通过之前的学习,你对这些步骤应该非常熟悉了,而这一次,你的任务是编写一个涵盖这种行为的sense函数。

  1. 机器人刚开始对周围一无所知,它可能会出现在任何地方,因此 p是均匀分布。
  2. 然后,机器人感知到一个网格颜色:红色或绿色,并根据pHit和pMiss的值更新此分布p
  • 机器人对颜色感知正确的概率是pHit = 0.6
  • 机器人对颜色感知错误的概率是‘pMiss = 0.2。

<img src=’images/robot_sensing.png’ width=50% height=50% />

# importing resources
import matplotlib.pyplot as plt
import numpy as np

还有一个是用于可视化概率分布的辅助函数。

def display_map(grid, bar_width=1):
    if(len(grid) > 0):
        x_labels = range(len(grid))
        plt.bar(x_labels, height=grid, width=bar_width, color='b')
        plt.xlabel('Grid Cell')
        plt.ylabel('Probability')
        plt.ylim(0, 1) # range of 0-1 for probability values 
        plt.title('Probability of the robot being at each cell in the grid')
        plt.xticks(np.arange(min(x_labels), max(x_labels)+1, 1))
        plt.show()
    else:
        print('Grid is empty')

练习:完成sense函数,使其在感应后输出非均匀分布p

我们从上一个练习开始。 q = [0.04, 0.12, 0.12, 0.04, 0.04]应该是传感器测量Z= 'red'时的分布。

Z= 'green'时,这个完整的函数也应该会输出正确的q

请注意,pHit是指机器人正确感知其所在方块的颜色的概率,因此,如果机器人感知到红色确实正处于红色方块上,我们会将当前位置概率(0.2)乘以pHit。如果机器人感觉到绿色确实正处于绿色方块上,情况也是如此。

# given initial variables
p=[0.2, 0.2, 0.2, 0.2, 0.2]
# the color of each grid cell in the 1D world
world=['green', 'red', 'red', 'green', 'green']
# Z, the sensor reading ('red' or 'green')
Z = 'red'
pHit = 0.6
pMiss = 0.2

## Complete this function
def sense(p, Z):
    ''' Takes in a current probability distribution, p, and a sensor reading, Z.
        Returns an unnormalized distribution after the sensor measurement has been made, q.
        This should be accurate whether Z is 'red' or 'green'. '''
    
    q=[]
    
    return q

q = sense(p,Z)
print(q)
display_map(q)