-1

我正在尝试计算粒子过滤器中某些粒子的权重,然后相应地对这些权重进行归一化。我的代码:

def update(particles, weights, landmark, sigma):
    n = 0.0
    for i in range(len(weights)):
        distance = np.power((particles[i][0] - landmark[0]) ** 2 + (particles[i][1] - 
        landmark[1])**2, 0.5)
        likelihood = exp(-(np.power(distance, 2))/2 * sigma ** 2)
        weights[i] = weights[i] * likelihood
        n += weights[i]
        weights += 1.e-30
        if n != 0:
            weights = weights / n

但是,我收到错误消息:/Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py:30: Ru​​ntimeWarning: 在 true_divide weights = weights = weights/n /Users/scottdayton/Uncertainty Research/particle.py:30 中遇到溢出: RuntimeWarning: true_divide weights = weights / n中遇到的无效值

4

1 回答 1

0

正如评论中所说,我在您的代码中添加了括号,但可能还有另一件事。我觉得您正在尝试将权重与可能性相乘,然后对结果进行归一化。为此,您应该在 2 中切断循环:

  • 校正权重和总和。
  • 归一化总和为一。

我会这样写:

def update(particles, weights, landmark, sigma):
    n = 0.0
    # Correction of weights and computation of the sum
    for i in range(len(weights)):
        distance = np.power((particles[i][0] - landmark[0]) ** 2 + (particles[i][1] - 
        landmark[1])**2, 0.5)
        likelihood = np.exp(-(np.power(distance, 2))/(2 * sigma ** 2))
        weights[i] = weights[i] * likelihood + 1.e-30
        n += weights[i]
    # Normalization to sum up to one
    for i in range(len(weights)):
        weights[i] = weights[i] / n
于 2020-06-30T22:39:03.647 回答