Skip to content

Commit

Permalink
fix small bugs, add docs and notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
stecrotti committed Dec 22, 2024
1 parent 415e4ce commit d2eacac
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
28 changes: 14 additions & 14 deletions examples/ksat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 95,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -29,7 +29,7 @@
},
{
"cell_type": "code",
"execution_count": 101,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -39,7 +39,7 @@
"k = 3\n",
"g = rand_regular_factor_graph(rng, n, m, k)\n",
"ψ = [KSATClause(bitrand(rng, length(neighbors(g, factor(a))))) for a in factors(g)]\n",
"bp = BP(g, ψ, fill(2, nvariables(g)));"
"bp = fast_ksat_bp(g, ψ);"
]
},
{
Expand All @@ -51,21 +51,21 @@
},
{
"cell_type": "code",
"execution_count": 103,
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"40"
"43"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"iters = iterate!(bp; maxiter=1000, tol=1e-12, rein=5e-2)"
"iters = iterate!(bp; maxiter=1000, tol=1e-14, rein=5e-2)"
]
},
{
Expand All @@ -78,14 +78,14 @@
},
{
"cell_type": "code",
"execution_count": 108,
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"100 adjoint(::Vector{Int64}) with eltype Int64:\n",
" 0 0 0 1 0 0 1 0 1 0 0 0 1 … 0 0 1 0 0 0 0 1 1 0 0 1"
"1000 adjoint(::Vector{Int64}) with eltype Int64:\n",
" 0 0 0 1 0 0 1 0 1 1 0 0 1 … 0 0 0 0 0 0 0 1 0 0 0 1"
]
},
"metadata": {},
Expand All @@ -106,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 112,
"execution_count": 21,
"metadata": {},
"outputs": [
{
Expand All @@ -118,23 +118,23 @@
}
],
"source": [
"nunsat = sum(1 - Int(bp.ψ[a](xstar[i] for i in neighbors(bp.g, factor(a)))) \n",
"nunsat = sum(!(Bool(bp.ψ[a](xstar[i]+1 for i in neighbors(bp.g, factor(a))))) \n",
" for a in factors(bp.g))\n",
"println(\"Number of unsatisfied clauses: $nunsat\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.10.5",
"display_name": "Julia 1.11.2",
"language": "julia",
"name": "julia-1.10"
"name": "julia-1.11"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.10.5"
"version": "1.11.2"
}
},
"nbformat": 4,
Expand Down
9 changes: 5 additions & 4 deletions src/Models/ksat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const BPKSAT = BP{<:KSATClause, <:BPFactor, <:NTuple{2,<:Real}, <:NTuple{2,<:Rea
@doc raw"""
fast_ksat_bp(g::AbstractFactorGraph, ψ::Vector{<:KSATClause}, [ϕ])
Return a specialized BP instance with `KSATClause` and messages encoded as tuples of two reals instead of vectors.
```
"""
function fast_ksat_bp(g::AbstractFactorGraph, ψ::Vector{<:KSATClause},
Expand All @@ -55,9 +56,9 @@ Base.eltype(bp::BPKSAT) = eltype(eltype(eltype(bp.b)))
function BeliefPropagation.reset!(bp::BPKSAT)
(; u, h, b) = bp
T = eltype(bp)
u .= (T(0.5), T(0.5))
h .= (T(0.5), T(0.5))
b .= (T(0.5), T(0.5))
u .= ((T(0.5), T(0.5)),)
h .= ((T(0.5), T(0.5)),)
b .= ((T(0.5), T(0.5)),)
return nothing
end
function BeliefPropagation.randomize!(rng::AbstractRNG, bp::BPKSAT)
Expand All @@ -68,7 +69,7 @@ function BeliefPropagation.randomize!(rng::AbstractRNG, bp::BPKSAT)
u[ia] = (ru, 1-ru)
h[ia] = (rh, 1-rh)
end
b .= (T(0.5), T(0.5))
b .= ((T(0.5), T(0.5)),)
return nothing
end

Expand Down
2 changes: 2 additions & 0 deletions test/Models/ksat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ end
g = rand_regular_factor_graph(rng, n, m, k)
ψ = [KSATClause(bitrand(rng, degree(g, factor(a)))) for a in factors(g)]
bp = fast_ksat_bp(g, ψ)
reset!(bp)
randomize!(bp)
iterate!(bp; maxiter=50, tol=1e-10)
b = beliefs(bp)
fb = factor_beliefs(bp)
Expand Down

4 comments on commit d2eacac

@stecrotti
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/121852

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" d2eacac9454d81bc17ed2c2953bced57c807ec30
git push origin v0.2.0

@stecrotti
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register

Release notes:

Breaking changes

  • The signature of compute_za, compute_zi and compute_zai changed from compute_za(ψₐ, msg_in) to compute_za(bp, a, msg=...) in order to improve the mechanism of dispatching on factor type and message types in optimized versions

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request updated: JuliaRegistries/General/121852

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" d2eacac9454d81bc17ed2c2953bced57c807ec30
git push origin v0.2.0

Please sign in to comment.