Skip to content
This repository has been archived by the owner on Apr 26, 2023. It is now read-only.

Commit

Permalink
Merge pull request #69 from JuliaPOMDP/alphavec_utilities
Browse files Browse the repository at this point in the history
Some helpful functions to work with alpha vector policies
  • Loading branch information
MaximeBouton authored May 18, 2018
2 parents 88bce80 + 53143cc commit 38dbb76
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/POMDPToolbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ using ProgressMeter
using StatsBase
using DataFrames


# export commons
export
update,
Expand Down Expand Up @@ -70,7 +69,9 @@ include("convenience/implementations.jl")

# policies
export
AlphaVectorPolicy
AlphaVectorPolicy,
belief_vector,
unnormalized_util
include("policies/alpha_vector.jl")

export
Expand Down
18 changes: 16 additions & 2 deletions src/beliefs/discrete.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct DiscreteBelief{P<:POMDP, S}
b::Vector{Float64}
end

function DiscreteBelief(pomdp, b::Vector{Float64}; check::Bool=true)
function DiscreteBelief(pomdp::POMDP, state_list::AbstractVector, b::AbstractVector{Float64}, check::Bool=true)
if check
if !isapprox(sum(b), 1.0, atol=0.001)
warn("""
Expand All @@ -33,7 +33,21 @@ function DiscreteBelief(pomdp, b::Vector{Float64}; check::Bool=true)
""")
end
end
return DiscreteBelief(pomdp, ordered_states(pomdp), b)
return DiscreteBelief(pomdp, state_list, b)
end

function DiscreteBelief(pomdp::POMDP, b::Vector{Float64}; check::Bool=true)
return DiscreteBelief(pomdp, ordered_states(pomdp), b, check)
end

function DiscreteBelief(pomdp::POMDP, b; check::Bool=true)
# convert b to a vector representation
state_list = ordered_states(pomdp)
bv = Vector{Float64}(n_states(pomdp))
for (i, s) in enumerate(state_list)
bv[i] = pdf(b, s)
end
return DiscreteBelief(pomdp, state_list, bv, check)
end


Expand Down
8 changes: 8 additions & 0 deletions src/policies/alpha_vector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,11 @@ function Base.push!(p::AlphaVectorPolicy, alpha::Vector{Float64}, a)
push!(p.alphas, alpha)
push!(p.action_map, a)
end

function action(p::AlphaVectorPolicy, b)
return action(p, DiscreteBelief(p.pomdp, b))
end

function value(p::AlphaVectorPolicy, b)
return value(p, DiscreteBelief(p.pomdp, b))
end
1 change: 1 addition & 0 deletions test/test_alpha_policy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ push!(policy, [0.0,0.0], true)

@test value(policy, b0) == 0.0
@test action(policy, b0) == true

0 comments on commit 38dbb76

Please sign in to comment.