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

added bool distribution #47

Merged
merged 2 commits into from
Jan 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Provides a sparse categorical distribution `SparseCat`. This distribution simply
#### [`weighted_iteration.jl`](src/distributions/weighted_iteration.jl)
Function for iterating through pairs of values and their probabilities in a distribution.

#### [`bool.jl`](src/distributions/bool.jl)
Distribution over an outcome being true. Create with `BoolDistribution(p_true)`. Obviously, the probability of false is simply `1 - p_true`.

### Model
#### [`info.jl`](src/model/info.jl)
Contains a small interface for outputting extra information (usually a `Dict` or `nothing`) from simulations.
Expand Down
5 changes: 5 additions & 0 deletions src/POMDPToolbox.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export
product
include("beliefs/discrete.jl")


export
PreviousObservationUpdater,
FastPreviousObservationUpdater,
Expand Down Expand Up @@ -169,6 +170,10 @@ export
SparseCat
include("distributions/sparse_cat.jl")

export
BoolDistribution
include("distributions/bool.jl")

# testing
export test_solver
include("testing/solver.jl")
Expand Down
21 changes: 21 additions & 0 deletions src/distributions/bool.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
BoolDistribution

A distribution that provides the probabilities of true or false.
Can construct with `BoolDistribution(p_true)`.
"""
struct BoolDistribution
p::Float64 # probability of true
end

pdf(d::BoolDistribution, s::Bool) = s ? d.p : 1.0-d.p

rand(rng::AbstractRNG, d::BoolDistribution) = rand(rng) <= d.p

iterator(d::BoolDistribution) = [true, false]

==(d1::BoolDistribution, d2::BoolDistribution) = d1.p == d2.p

Base.hash(d::BoolDistribution) = hash(d.p)

Base.length(d::BoolDistribution) = 2
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ include("test_implementations.jl")
include("test_distributions_jl.jl")
include("test_weighted_iteration.jl")
include("test_sparse_cat.jl")
include("test_bool.jl")
include("test_info.jl")
21 changes: 21 additions & 0 deletions test/test_bool.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using POMDPToolbox
using Base.Test

# Hack to get this to build with POMDPModels
# this can be removed once BoolDistribution is removed from POMDPModels
BoolDistribution = POMDPToolbox.BoolDistribution

# testing constructor and pdf
d = BoolDistribution(0.3)
@test pdf(d, true) == 0.3
@test pdf(d, false) == 0.7

# testing iterator
@test iterator(d) == [true, false]

# testing ==
d2 = BoolDistribution(0.3)
@test d == d2

# testing hash
@test hash(d) == hash(d.p)