I am trying to calculate the weights of certain particles in a particle filter and then normalize those weights accordingly. My code:
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
However, I am getting the error: /Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py:30: RuntimeWarning: overflow encountered in true_divide weights = weights / n /Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py:30: RuntimeWarning: invalid value encountered in true_divide weights = weights / n
As said in comments, I added parenthesis to your code but there might be another thing. I feel that you are trying to multiply weights with the likelihood and then normalize the result. To do so you should cut the loop in 2:
I'll write it like this:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.