Skip to content

Commit

Permalink
added initialstate_distribution for SimpleGridWorld
Browse files Browse the repository at this point in the history
  • Loading branch information
zsunberg committed Sep 19, 2019
1 parent 78087d5 commit e6fb186
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/gridworld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,20 @@ function POMDPs.stateindex(mdp::SimpleGridWorld, s::AbstractVector{Int})
end
end

POMDPs.initialstate(mdp::SimpleGridWorld, rng::AbstractRNG) = GWPos(rand(rng, 1:mdp.size[1]), rand(rng, 1:mdp.size[2]))
struct GWUniform
size::Tuple{Int, Int}
end
Base.rand(rng::AbstractRNG, d::GWUniform) = GWPos(rand(rng, 1:d.size[1]), rand(rng, 1:d.size[2]))
function POMDPs.pdf(d::GWUniform, s::GWPos)
if all(1 .<= s[1] .<= d.size)
return 1/prod(d.size)
else
return 0.0
end
end
POMDPs.support(d::GWUniform) = (GWPos(x, y) for x in 1:d.size[1], y in 1:d.size[2])

POMDPs.initialstate_distribution(mdp::SimpleGridWorld) = GWUniform(mdp.size)

# Actions

Expand All @@ -55,7 +67,6 @@ const aind = Dict(:up=>1, :down=>2, :left=>3, :right=>4)
POMDPs.actionindex(mdp::SimpleGridWorld, a::Symbol) = aind[a]



# Transitions

POMDPs.isterminal(m::SimpleGridWorld, s::AbstractVector{Int}) = any(s.<0)
Expand Down
10 changes: 9 additions & 1 deletion test/simple_gridworld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,17 @@ let
POMDPModelTools.render(problem, stp, color=s->reward(problem,s))
POMDPModelTools.render(problem, stp, color=s->rand())
POMDPModelTools.render(problem, stp, color=s->"yellow")

ss = collect(states(problem))
isd = initialstate_distribution(problem)
for s in ss
if !isterminal(problem, s)
@test s in support(isd)
@test pdf(isd, s) > 0.0
end
end
end

# disabled until POMDPSimulators v0.1.2 is tagged
let
@nbinclude(joinpath(dirname(@__FILE__), "..", "notebooks", "GridWorld Visualization.ipynb"))
end

0 comments on commit e6fb186

Please sign in to comment.