Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add implied literals #63

Merged
merged 5 commits into from
Jan 22, 2021
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
6 changes: 3 additions & 3 deletions Artifacts.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ lazy = true
sha256 = "5d66a95037e44010502ad28ec8d6dc26f46947dc88e962633b7de54e614b1a47"

[circuit_model_zoo]
git-tree-sha1 = "49e3a134c68acbcd87075dee811aff997c29cad2"
git-tree-sha1 = "af581f4a7ca5e29685f8d67843e0120302ead3ab"
lazy = true

[[circuit_model_zoo.download]]
url = "https://github.com/UCLA-StarAI/Circuit-Model-Zoo/archive/v0.1.3.tar.gz"
sha256 = "537d4d414b8955e8f9b64e4e3aaf5382889a6901c5e24aa9bc8bc2fbea51dc4f"
url = "https://github.com/UCLA-StarAI/Circuit-Model-Zoo/archive/v0.1.4.tar.gz"
sha256 = "ec6ea6649364ec0e1305fea3afc3a2dcb3dd40d4d54626e85499a47a07a436b4"

2 changes: 1 addition & 1 deletion src/LoadSave/circuit_loaders.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export zoo_cnf,
# loaders from model zoo
#####################

const zoo_version = "/Circuit-Model-Zoo-0.1.3"
const zoo_version = "/Circuit-Model-Zoo-0.1.4"

zoo_cnf(name) =
load_cnf(zoo_cnf_file(name))
Expand Down
43 changes: 43 additions & 0 deletions src/queries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export variables_by_node,
isstruct_decomposable,
isdeterministic,
iscanonical,
implied_literals,
sat_prob,
model_count,
prob_equiv_signature,
Expand Down Expand Up @@ -223,6 +224,48 @@ function isdeterministic(root::LogicCircuit)::Bool
result
end


Copy link
Collaborator

Choose a reason for hiding this comment

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

I guess you could also add a doc string to describe input/outputs and what implied_literals means a bit.

"""
implied_literals(root::LogicCircuit)::Dict{LogicCircuit, Union{BitSet, Nothing}}

Compute at each node literals that are implied by the formula.
nothing at a node means all literals are implied (i.e. the node's formula is false)

This algorithm is sound but not complete - all literals returned are correct, but some true implied literals may be missing.
"""
implied_literals(root::LogicCircuit)::Dict{LogicCircuit, Union{BitSet, Nothing}} =
implied_literals_rec(root, Dict{LogicCircuit, Union{BitSet, Nothing}}())


function implied_literals_rec(root::LogicCircuit, lcache::Dict{LogicCircuit, Union{BitSet, Nothing}})
if isconstantgate(root)
if constant(root)
# True implies no literals
lcache[root] = BitSet()
else
# False implies any literal, so "full" bitset represented by Nothing
lcache[root] = nothing
end
elseif isliteralgate(root)
lcache[root] = BitSet([literal(root)])
elseif isinnergate(root)
for c in root.children
implied_literals_rec(c, lcache)
end
if is⋀gate(root)
# If there's a false in here then this is false too
if any(x -> lcache[x] === nothing, root.children)
lcache[root] = nothing
else
lcache[root] = mapreduce(c -> lcache[c], union, root.children)
end
else
# Just filter out any falses, they don't do anything here
lcache[root] = mapreduce(c -> lcache[c], intersect, filter(x -> lcache[x] !== nothing, root.children))
end
end
lcache
end
#####################
# structural properties
#####################
Expand Down
28 changes: 27 additions & 1 deletion test/queries_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using LogicCircuits

include("helper/plain_logic_circuits.jl")


@testset "Queries test" begin

r1 = fully_factorized_circuit(PlainLogicCircuit,10)
Expand Down Expand Up @@ -117,4 +118,29 @@ end
@test respects_vtree(random, random_vtree)
# @test respects_vtree(random, random_inferred) # "issues/#47"

end
end


@testset "Implied Literals Test" begin

# Load and compile as a bdd
c17 = zoo_cnf("easy/C17_mince.cnf")
vtr = PlainVtree(num_variables(c17), :rightlinear)
mgr = SddMgr(vtr)
circuit = compile(mgr, c17)
plc = PlainLogicCircuit(circuit)

il = implied_literals(plc)

for ornode in or_nodes(plc)
@test num_children(ornode) == 2
# If there's a nothing just continue, it'll always work
if il[ornode.children[1]] === nothing || il[ornode.children[2]] === nothing
continue
end
implied1 = il[ornode.children[1]]
implied2 = il[ornode.children[2]]
neg_implied2 = BitSet(map(x -> -x, collect(implied2)))
@test !isempty(intersect(implied1, neg_implied2))
end
end