2D世界中的本地化

时间:2016-10-17 12:01:11

标签: python grid cells

我正在努力使用以下代码来在红色和绿色网格单元的2D世界中定位机器人。我基本上得到的错误表明列表索引超出了范围。

enter image description here

p=[.2,.2,.2,.2,.2]# Initial cell probability
w=[['R','G','G','R','R'],
   ['R','R','G','R','R'],
   ['R','R','G','G','R'],
   ['R','R','R','R','R']]# World
meas = ['G','G','G','G','G']   # measurements
mov  = [[0,0],[0,1],[1,0],[1,0],[0,1]]               # motion
phit = .6                      # Probability to measure: R->0.6
pmiss = .2                     # Probability to measure: R->0.2
pExact = .8                    # Prob. exact motion
pOver = .1                     # Prob. overshoot
pUnder = .1                    # Prob. undershoot

def entropy (p):
    s = [p[i]*log(p[i]) for i in range(len(p))]
    return round(-sum(s), 2)

def sense(p, z):
    q = []
    for i in range(len(p)):
        hit = w[i]==z
        q.append( p[i]*(phit*hit + pmiss*(1-hit)) )
    s = sum(q)
    q = [i/s for i in q]
    return q

#Moving u cells
def move(p, u):
    q = []
    for i in range(len(p)):
        motion = pExact * p[(i-u)%len(p)]
        motion += pOver * p[(i-u-1)%len(p)]
        motion += pUnder * p[(i-u+1)%len(p)]
        q.append(motion)
    return q

for i in range(len(meas)):
    p = sense(p, meas[i])
    r = [format(j,'.3f') for j in p]
    print "Sense %i:"%(i),
    print r, entropy(p)
    p = move(p, mov[i])
    r = [format(j,'.3f') for j in p]
    print "Move  %i:"%(i),
    print r, entropy(p)
    print

1 个答案:

答案 0 :(得分:0)

在常规意义上()你的程序试图访问列表 w 的第5个元素(列表中的索引4),不幸的是 w 只有4个元素(只有索引) 0,1,2,3有效)

范围(len(p))返回[0,1,2,3,4]

同样 w [i] == z 永远不会成真:确实 w [i] 是一个字符列表, z 是单个字符

sense()可能没有按照你原来的意图行事。