Skip to content

Commit f6df82d

Browse files
Fix resampling step
1 parent 0ecdcce commit f6df82d

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/container.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,14 @@ end
198198
199199
Create new unique keys for the particles in the ParticleContainer
200200
"""
201-
function update_keys!(pc::ParticleContainer)
201+
function update_keys!(pc::ParticleContainer, ref::Union{Particle,Nothing}=nothing)
202202
# Update keys to new particle ids
203-
for i in 1:length(pc)
203+
nparticles = length(pc)
204+
n = ref === nothing ? nparticles : nparticles - 1
205+
for i in 1:n
204206
pi = pc.vals[i]
205207
k = split(pi.rng, 1)
206-
seed!(pi.rng, k[1])
207-
set_counter!(pi.rng.rng, pi.rng.count)
208+
update_rng!(pi.rng, k[1])
208209
end
209210
end
210211

@@ -253,13 +254,13 @@ function resample_propagate!(
253254
p = isref ? fork(pi, isref) : pi
254255

255256
seeds = split(p.rng, ni)
256-
seed!(p.rng, seeds[1])
257+
!isref && update_rng!(p.rng, seeds[1])
257258

258259
children[j += 1] = p
259260
# fork additional children
260261
for k in 2:ni
261262
part = fork(p, isref)
262-
seed!(part.rng, seeds[k])
263+
update_rng!(part.rng, seeds[k])
263264
children[j += 1] = part
264265
end
265266
end
@@ -288,10 +289,11 @@ function resample_propagate!(
288289
# Compute the effective sample size ``1 / ∑ wᵢ²`` with normalized weights ``wᵢ``
289290
ess = inv(sum(abs2, weights))
290291

291-
if ess resampler.threshold * length(pc)
292+
#if ess ≤ resampler.threshold * length(pc)
293+
if true
292294
resample_propagate!(rng, pc, resampler.resampler, ref; weights=weights)
293295
else
294-
update_keys!(pc)
296+
update_keys!(pc, ref)
295297
end
296298

297299
return pc

src/rng.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ function load_state(rng::TracedRNG{T}) where {T}
7676
return set_counter!(rng.rng, rng.count)
7777
end
7878

79+
"""
80+
update_rng!(rng::TracedRNG)
81+
82+
Set key and counter of inner RNG to key and the running model step
83+
"""
84+
function update_rng!(rng::TracedRNG{T}, key) where {T}
85+
seed!(rng, key)
86+
return set_counter!(rng.rng, rng.count)
87+
end
88+
7989
"""
8090
save_state!(r::TracedRNG)
8191

0 commit comments

Comments
 (0)