Skip to content

should become an atomic int when we care about multithreading #229

Description

@github-actions

The struct def for constructing a new Boolean variable f with the given probability

so you can assign f = Flip(0.6, "x") --> f is a new probabilistic Boolean variable

p = f.prob

p = if p isa AbstractFloat round(p, digits=2) else p end

if isnothing(f.name)

print(io, "$(typeof(f))($(f.global_id),$(p))")

else

print(io, "$(typeof(f))($(f.global_id),$(p),$(f.name))")

end

end

(is returned from this function?)

https://github.com/Tractables/Dice.jl/blob/9cb0217e2c195f8be123847bcc44e5bb27a450fb/src/dist/bool.jl#L10

const AnyBool = Union{Dist{Bool}, Bool}

# TODO should become an atomic int when we care about multithreading
global_flip_id::Int64 = one(Int64)
    # number field on 'sampling branch'

# Compilation into wbf
#   The struct def for constructing a new Boolean variable f with the given probability
#       so you can assign f = Flip(0.6, "x") --> f is a new probabilistic Boolean variable 
mutable struct Flip <: Dist{Bool}
    const global_id::Int
    prob
    const name
    ## NEW ## - note that bit_index 0 = MSB
    bit_index::Union{Nothing, Int}
    ordering::Int
    ## End NEW ##
    
    Flip(prob, name, bit_index) = begin
        if prob isa Real
            @assert !isone(prob) "Use `true` for deterministic flips"
            @assert !iszero(prob) "Use `false` for deterministic flips"
            @assert isnan(prob) || 0 < prob < 1 "Probabilities are between 0 and 1 (or undefined as NaN)"
        end
        global global_flip_id
        id = global_flip_id+=1
        new(id, prob, name, bit_index, id)
    end
end

# Modified SHOW to include orderings
function Base.show(io::IO, f::Flip)
    p = f.prob
    p = if p isa AbstractFloat round(p, digits=2) else p end
    if isnothing(f.name)
        print(io, "$(typeof(f))($(f.global_id),$(p), ordering=$(f.ordering)), bit_index=$(f.bit_index))")
    else
        print(io, "$(typeof(f))($(f.global_id),$(p),$(f.name), ordering=$(f.ordering)), bit_index=$(f.bit_index))")
    end
end

# function Base.show(io::IO, f::Flip)
#     p = f.prob
#     p = if p isa AbstractFloat round(p, digits=2) else p end
#     if isnothing(f.name)
#         print(io, "$(typeof(f))($(f.global_id),$(p))")
#     else
#         print(io, "$(typeof(f))($(f.global_id),$(p),$(f.name))")
#     end
# end

"Create a Bernoulli random variable with the given probability (a coin flip)"
#   C-FLIP (coin flip compilation)
#   (is returned from this function?)
function flip(prob = NaN16; name = nothing, bit_index = nothing)
    if prob isa Real
        iszero(prob) && return false
        isone(prob) && return true
    end
    Flip(prob, name, bit_index)
end

"Set the probability of a flip that has not been assigned a probability yet"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions