diff --git a/.travis.yml b/.travis.yml index 89a9c52..3e7c379 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,8 @@ os: - linux # - osx julia: - - 0.7 - 1.0 + - 1 notifications: email: false after_success: diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..9f3d9d0 --- /dev/null +++ b/Project.toml @@ -0,0 +1,31 @@ +name = "ParticleFilters" +uuid = "c8b314e2-9260-5cf8-ae76-3be7461ca6d0" +repo = "https://github.com/JuliaPOMDP/ParticleFilters.jl" +version = "0.4.0" + +[deps] +POMDPModelTools = "08074719-1b2a-587c-a292-00f91cc44415" +POMDPPolicies = "182e52fb-cfd0-5e46-8c26-fd0667c990f4" +POMDPs = "a93abf59-7444-517b-a68a-c42f96afdd7d" +Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" +Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2" +StatsBase = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91" + +[compat] +POMDPs = "0.7.3, 0.8" +julia = "1" + +[extras] +DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab" +Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f" +LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +NBInclude = "0db19996-df87-5ea3-a455-e3a50d440464" +POMDPModels = "355abbd5-f08e-5560-ac9e-8b5f2592a0ca" +POMDPSimulators = "e0d0a172-29c6-5d4e-96d0-f262df5d01fd" +Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" +Reel = "71555da5-176e-5e73-a222-aebc6c6e4f2f" +StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" +Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" + +[targets] +test = ["DelimitedFiles", "Distributions", "LinearAlgebra", "NBInclude", "Plots", "POMDPModels", "POMDPSimulators", "Reel", "StaticArrays", "Test"] diff --git a/REQUIRE b/REQUIRE deleted file mode 100644 index f28b28e..0000000 --- a/REQUIRE +++ /dev/null @@ -1,5 +0,0 @@ -julia 0.7 -StatsBase -POMDPs 0.6 -POMDPModelTools -POMDPPolicies diff --git a/docs/src/beliefs.md b/docs/src/beliefs.md index f2dbbff..60b6941 100644 --- a/docs/src/beliefs.md +++ b/docs/src/beliefs.md @@ -20,7 +20,6 @@ The following functions from the [POMDPs.jl distributions interface](http://juli - [`rand`](@ref) - [`pdf`](@ref) - [`support`](@ref) -- [`sampletype`](@ref) (will be replaced with `Random.gentype`) - [`mode`](@ref) - [`mean`](@ref) @@ -43,7 +42,6 @@ These functions provide access to the particles and weights in the beliefs (clic POMDPs.rand POMDPs.pdf POMDPs.support -POMDPs.sampletype POMDPs.mode POMDPs.mean n_particles diff --git a/src/ParticleFilters.jl b/src/ParticleFilters.jl index 7b13b6d..e44c3fd 100644 --- a/src/ParticleFilters.jl +++ b/src/ParticleFilters.jl @@ -3,10 +3,8 @@ module ParticleFilters using POMDPs import POMDPs: pdf, mode, update, initialize_belief, support import POMDPs: statetype, isterminal, observation -import POMDPs: generate_s import POMDPs: action, value import POMDPs: implemented -import POMDPs: sampletype import POMDPModelTools: obs_weight using StatsBase @@ -15,7 +13,7 @@ using Statistics using POMDPPolicies using POMDPModelTools # for weighted_iterator -import Random: rand +import Random: rand, gentype import Statistics: mean export diff --git a/src/beliefs.jl b/src/beliefs.jl index acfcd37..6584aa8 100644 --- a/src/beliefs.jl +++ b/src/beliefs.jl @@ -1,6 +1,5 @@ abstract type AbstractParticleBelief{T} end -sampletype(::Type{B}) where B<:AbstractParticleBelief{T} where T = T Random.gentype(::Type{B}) where B<:AbstractParticleBelief{T} where T = T ######################## diff --git a/src/policies.jl b/src/policies.jl index b1c9581..c77d703 100644 --- a/src/policies.jl +++ b/src/policies.jl @@ -1,26 +1,26 @@ -#= -Implementation specific to using ParticleBeliefs with AlphaVectorPolicy from POMDPToolbox -these are more efficient than converting the ParticleBelief to a DiscreteBelief -=# - -""" -Given a particle belief, return the unnormalized utility function that weights each state value by the weight of -the corresponding particle - unnormalized_util(p::AlphaVectorPolicy, b::AbstractParticleBelief) -""" -function unnormalized_util(p::AlphaVectorPolicy, b::AbstractParticleBelief) - util = zeros(n_actions(p.pomdp)) - for (i, s) in enumerate(particles(b)) - j = stateindex(p.pomdp, s) - util += weight(b, i)*getindex.(p.alphas, (j,)) - end - return util -end - -function action(p::AlphaVectorPolicy, b::AbstractParticleBelief) - util = unnormalized_util(p, b) - ihi = argmax(util) - return p.action_map[ihi] -end - -value(p::AlphaVectorPolicy, b::AbstractParticleBelief) = maximum(unnormalized_util(p, b))/weight_sum(b) +#= +Implementation specific to using ParticleBeliefs with AlphaVectorPolicy from POMDPToolbox +these are more efficient than converting the ParticleBelief to a DiscreteBelief +=# + +""" +Given a particle belief, return the unnormalized utility function that weights each state value by the weight of +the corresponding particle + unnormalized_util(p::AlphaVectorPolicy, b::AbstractParticleBelief) +""" +function unnormalized_util(p::AlphaVectorPolicy, b::AbstractParticleBelief) + util = p.pomdp |> actions |> length |> zeros + for (i, s) in enumerate(particles(b)) + j = stateindex(p.pomdp, s) + util += weight(b, i)*getindex.(p.alphas, (j,)) + end + return util +end + +function action(p::AlphaVectorPolicy, b::AbstractParticleBelief) + util = unnormalized_util(p, b) + ihi = argmax(util) + return p.action_map[ihi] +end + +value(p::AlphaVectorPolicy, b::AbstractParticleBelief) = maximum(unnormalized_util(p, b))/weight_sum(b) diff --git a/src/pomdps.jl b/src/pomdps.jl index ac0197d..603dd16 100644 --- a/src/pomdps.jl +++ b/src/pomdps.jl @@ -18,7 +18,7 @@ function predict!(pm, m::POMDP, b, a, rng) s = particle(b, i) if !isterminal(m, s) all_terminal = false - sp = generate_s(m, s, a, rng) + sp = gen(DDNOut(:sp), m, s, a, rng) pm[i] = sp end end @@ -68,7 +68,7 @@ function initialize_belief(up::BasicParticleFilter, d::D) where D if ex isa MethodError @warn(""" Suppressing MethodError in initialize_belief in ParticleFilters.jl. Please file an issue here: - + https://github.com/JuliaPOMDP/ParticleFilters.jl/issues/new The error was diff --git a/src/unweighted.jl b/src/unweighted.jl index cfeb4c0..3b43e62 100644 --- a/src/unweighted.jl +++ b/src/unweighted.jl @@ -14,11 +14,11 @@ function UnweightedParticleFilter(model, n::Integer; rng=Base.GLOBAL_RNG) end function update(up::UnweightedParticleFilter, b::ParticleCollection, a, o) - new = sampletype(b)[] + new = Random.gentype(b)[] i = 1 while i <= up.n s = particle(b, mod1(i, n_particles(b))) - sp, o_gen = generate_so(up.model, s, a, up.rng) + sp, o_gen = gen(DDNOut(:sp, :o), up.model, s, a, up.rng) if o_gen == o push!(new, sp) end diff --git a/test/REQUIRE b/test/REQUIRE deleted file mode 100644 index 4e559b8..0000000 --- a/test/REQUIRE +++ /dev/null @@ -1,12 +0,0 @@ -Distributions -StaticArrays -POMDPModels -POMDPSimulators -POMDPModelTools -NBInclude -Plots -Random -DelimitedFiles -LinearAlgebra -POMDPPolicies -Reel diff --git a/test/domain_specific_resampler.jl b/test/domain_specific_resampler.jl index 0114ee5..bc80233 100644 --- a/test/domain_specific_resampler.jl +++ b/test/domain_specific_resampler.jl @@ -36,5 +36,5 @@ for bp in stepthrough(m, p2, up, "bp", max_steps=3) @test bp isa ParticleCollection{LightDark1DState} @test n_particles(bp) == n end - + end diff --git a/test/runtests.jl b/test/runtests.jl index 259ec5d..8424563 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -58,13 +58,13 @@ struct ContinuousPOMDP <: POMDP{Float64, Float64, Float64} end rb2 = @inferred resample(rs, WeightedParticleBelief(particles(b), ones(n_particles(b))), MersenneTwister(3)) @test all(particles(rb1).==particles(rb2)) end - + @testset "unweighted" begin rng = MersenneTwister(47) uf = UnweightedParticleFilter(p, 1000, rng) ps = @inferred initialize_belief(uf, initialstate_distribution(p)) a = @inferred rand(rng, actions(p)) - sp, o = @inferred generate_so(p, rand(rng, ps), a, rng) + sp, o = @inferred gen(DDNOut(:sp, :o), p, rand(rng, ps), a, rng) bp = @inferred update(uf, ps, a, o) wp1 = @inferred collect(weighted_particles(ParticleCollection([1,2])))