JuliaPOMDP / RockSample.jl

Implement the rock sample problem using POMDPs.jl
Other
5 stars 5 forks source link

no method matching iterate(::ParticleCollection{RSState{3}}) #22

Closed LorenzoBonanni closed 1 year ago

LorenzoBonanni commented 1 year ago

Hi, I'm facing a problem when trying to generate a Gif of the Simulation using BasicPomcp and RockSample. The error is the following:

ERROR: LoadError: MethodError: no method matching iterate(::ParticleCollection{RSState{3}})
Closest candidates are:
  iterate(::Union{LinRange, StepRangeLen}) at range.jl:664
  iterate(::Union{LinRange, StepRangeLen}, ::Int64) at range.jl:664
  iterate(::T) where T<:Union{Base.KeySet{var"#s77", var"#s76"} where {var"#s77", var"#s76"<:Dict}, Base.ValueIterator{var"#s75"} where var"#s75"<:Dict} at dict.jl:693
  ...
Stacktrace:
 [1] iterate
   @ ./iterators.jl:159 [inlined]
 [2] iterate
   @ ./iterators.jl:158 [inlined]
 [3] get_rock_beliefs(pomdp::RockSamplePOMDP{3}, b::ParticleCollection{RSState{3}})
   @ RockSample ~/Desktop/BORSA/despot_test/venv/.julia/packages/RockSample/BhVFX/src/visualization.jl:78
 [4] render_belief(pomdp::RockSamplePOMDP{3}, step::NamedTuple{(:s, :a, :sp, :o, :r, :info, :t, :action_info, :b, :bp, :update_info), Tuple{RSState{3}, Int64, RSState{3}, Int64, Float64, Nothing, Int64, Dict{Symbol, Any}, ParticleCollection{RSState{3}}, ParticleCollection{RSState{3}}, Nothing}})
   @ RockSample ~/Desktop/BORSA/despot_test/venv/.julia/packages/RockSample/BhVFX/src/visualization.jl:56
 [5] render(pomdp::RockSamplePOMDP{3}, step::NamedTuple{(:s, :a, :sp, :o, :r, :info, :t, :action_info, :b, :bp, :update_info), Tuple{RSState{3}, Int64, RSState{3}, Int64, Float64, Nothing, Int64, Dict{Symbol, Any}, ParticleCollection{RSState{3}}, ParticleCollection{RSState{3}}, Nothing}}; viz_rock_state::Bool, viz_belief::Bool, pre_act_text::String)
   @ RockSample ~/Desktop/BORSA/despot_test/venv/.julia/packages/RockSample/BhVFX/src/visualization.jl:42
 [6] render(pomdp::RockSamplePOMDP{3}, step::NamedTuple{(:s, :a, :sp, :o, :r, :info, :t, :action_info, :b, :bp, :update_info), Tuple{RSState{3}, Int64, RSState{3}, Int64, Float64, Nothing, Int64, Dict{Symbol, Any}, ParticleCollection{RSState{3}}, ParticleCollection{RSState{3}}, Nothing}})
   @ RockSample ~/Desktop/BORSA/despot_test/venv/.julia/packages/RockSample/BhVFX/src/visualization.jl:6
 [7] makegif(m::RockSamplePOMDP{3}, hist::POMDPTools.Simulators.SimHistory{NamedTuple{(:s, :a, :sp, :o, :r, :info, :t, :action_info, :b, :bp, :update_info), Tuple{RSState{3}, Int64, RSState{3}, Int64, Float64, Nothing, Int64, Dict{Symbol, Any}, ParticleCollection{RSState{3}}, ParticleCollection{RSState{3}}, Nothing}}, Float64}; filename::String, spec::Nothing, show_progress::Bool, extra_initial::Bool, extra_final::Bool, render_kwargs::NamedTuple{(), Tuple{}}, fps::Int64)
   @ POMDPGifs ~/Desktop/BORSA/despot_test/venv/.julia/packages/POMDPGifs/AIFGJ/src/POMDPGifs.jl:145
 [8] simulate(::GifSimulator, ::RockSamplePOMDP{3}, ::POMCPPlanner{RockSamplePOMDP{3}, BasicPOMCP.SolvedPORollout{RandomPolicy{Random._GLOBAL_RNG, RockSamplePOMDP{3}, POMDPTools.BeliefUpdaters.NothingUpdater}, POMDPTools.BeliefUpdaters.NothingUpdater, Random._GLOBAL_RNG}, Random._GLOBAL_RNG})
   @ POMDPGifs ~/Desktop/BORSA/despot_test/venv/.julia/packages/POMDPGifs/AIFGJ/src/POMDPGifs.jl:61
 [9] top-level scope
   @ ~/Desktop/BORSA/despot_test/src/a.jl:22
in expression starting at /home/lorenzobonanni/Desktop/BORSA/despot_test/src/a.jl:22

Here a minimal example to reproduce the Issue:

using POMDPs
using RockSample 
using POMDPGifs # to make gifs
using Cairo # for making/saving the gif
using BasicPOMCP

pomdp = RockSamplePOMDP(rocks_positions=[(2,3), (4,4), (4,2)], 
                        sensor_efficiency=20.0,
                        discount_factor=0.95, 
                        good_rock_reward = 20.0)

solver = POMCPSolver(
        max_depth=100,
        c=10.0
    )

policy = solve(solver, pomdp)

sim = GifSimulator(filename="test.gif", max_steps=30)
simulate(sim, pomdp, policy)

OS: Pop Os 22.04 Julia Version: 1.6 BasicPOMCP: v0.3.8 RockSample: v0.1.5 Cairo: v1.0.5 POMDPGifs: v0.1.1 POMDPs: v0.9.5

zsunberg commented 1 year ago

Looks like support needs to be added in visualization.jl line 78, like this:

for (i, bᵢ) ∈ enumerate(support(b′))

Currently there is no test that visualizes a belief, so we should definitely add that.

@LorenzoBonanni do you have the expertise to submit a PR to fix it? If not, I'll try to get to it later this week.

zsunberg commented 1 year ago

I tried fixing, and a visualization test passes for the initial belief, but I think that is mostly just a fluke - I don't think it will pass for a generic belief like a particle collection.

LorenzoBonanni commented 1 year ago

Yeah, I've tried using the test you made on a ParticleCollection and it gives the above error because it can't enumerate a Particle Collection. Here is the code to try:

  b0 = ParticleFilters.ParticleCollection{RSState{3}}(
      RSState{3}[
          RSState{3}([1, 1], Bool[0, 1, 0]), 
          RSState{3}([1, 1], Bool[1, 1, 1])
      ], nothing)
  s0 = rand(b0)
  c = render(pomdp, (s=s0, a=6, b=b0))
  c |> SVG("rocksample.svg") 

I've fixed this error by converting b′ into weighted_particles by adding the following code in visualization.jl line 75

elseif b isa ParticleCollection
    b′ = weighted_particles(b)

This fixes the iterate error. But when iterating over the Belief if the rocks field of the state contains ones it gives another error:

ERROR: LoadError: MethodError: no method matching +(::Float64, ::Pair{RSState{3}, Float64})
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
  +(::Union{Float16, Float32, Float64}, ::BigFloat) at mpfr.jl:399
  +(::AbstractFloat, ::Bool) at bool.jl:102
  ...
Stacktrace:
  [1] _broadcast_getindex_evalf
    @ ./broadcast.jl:648 [inlined]
  [2] _broadcast_getindex
    @ ./broadcast.jl:621 [inlined]
  [3] getindex
    @ ./broadcast.jl:575 [inlined]
  [4] macro expansion
    @ ./broadcast.jl:984 [inlined]
  [5] macro expansion
    @ ./simdloop.jl:77 [inlined]
  [6] copyto!
    @ ./broadcast.jl:983 [inlined]
  [7] copyto!
    @ ./broadcast.jl:936 [inlined]
  [8] materialize!
    @ ./broadcast.jl:894 [inlined]
  [9] materialize!
    @ ./broadcast.jl:891 [inlined]
 [10] errore()
    @ Main ~/Desktop/BORSA/despot_test/src/a.jl:67
 [11] top-level scope
    @ ~/Desktop/BORSA/despot_test/src/a.jl:82
in expression starting at /home/lorenzobonanni/Desktop/BORSA/despot_test/src/a.jl:82

This is the code to reproduce the error: it is essentialy the inner part of the for loop at line 80 of visualization.jl

rock_beliefs = zeros(Float64, 3)
s_i = RSState{3}([1, 1], Bool[1, 0, 0])
b_i = RSState{3}([1, 1], Bool[1, 0, 0]) => 0.001
a = rock_beliefs[s_i.rocks.==1]
rock_beliefs[s_i.rocks.==1] .+= b_i

If we change the code a bit it gives no more error:

rock_beliefs = zeros(Float64, 3)
s_i = RSState{3}([1, 1], Bool[0, 0, 0])
b_i = RSState{3}([1, 1], Bool[1, 0, 0]) => 0.001
a = rock_beliefs[s_i.rocks.==1]
rock_beliefs[s_i.rocks.==1] .+= b_i
LorenzoBonanni commented 1 year ago

@zsunberg with the latest commit I've Fixed the errror

zsunberg commented 1 year ago

Thanks @LorenzoBonanni !! I will take a look later today.

LorenzoBonanni commented 1 year ago

@zsunberg Bump Up Version