Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated with low-variance resampling #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Localization/Particle filter/particle_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,34 @@ def resample_particles(self, weights):

self.particles = resampled_particles

def low_variance_resample(self, weights):
"""Low variance resampling method."""
weights = np.array(weights)
weights_sum = np.sum(weights)

if weights_sum == 0:
print("All weights are zero! Check measurement model.")
weights = np.ones(self.num_particles) / self.num_particles
else:
weights /= weights_sum

num_particles = self.num_particles
resampled_particles = []

r = np.random.uniform(0, 1/num_particles)
cumulative_sum = 0.0
index = 0
c = weights[0]

for i in range(num_particles):
U = r + i / num_particles
while U > c:
index += 1
c += weights[index]
resampled_particles.append(self.particles[index])

self.particles = resampled_particles


def update(self, measurement):
"""
Expand Down Expand Up @@ -232,4 +260,4 @@ def main():
pf.e_plot_particles()

if __name__=="__main__":
main()
main()