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

Stop struct_learn when there are no more candidates for transformation #68

Merged
merged 1 commit into from
Mar 1, 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
23 changes: 20 additions & 3 deletions src/transformations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ Randomly picking egde and variable from candidates
"""
function random_split(circuit::Node)
candidates, variable_scope = split_candidates(circuit)
if isempty(candidates) return nothing end
or, and = rand(candidates)
vars = Var.(collect(variable_scope[and]))
var = rand(vars)
Expand All @@ -344,6 +345,8 @@ end
Split step
"""
function split_step(circuit::Node; loss=random_split, depth=0, sanity_check=true)
res = loss(circuit)
if isnothing(res) return nothing end
edge, var = loss(circuit)
split(circuit, edge, var; depth=depth, sanity_check=sanity_check)
end
Expand All @@ -354,12 +357,26 @@ Structure learning manager
function struct_learn(circuit::Node;
primitives=[split_step],
kwargs=Dict(split_step=>(loss=random_split, depth=0)),
maxiter=typemax(Int), stop::Function=x->false)
maxiter=typemax(Int), stop::Function=x->false, verbose = true)

valid_prims = copy(primitives)
m = length(valid_prims)

for iter in 1 : maxiter
primitive_step = rand(primitives)
which_primitive = rand(1:m)
primitive_step = valid_prims[which_primitive]
kwarg = kwargs[primitive_step]
c2, _ = primitive_step(circuit; kwarg...)
r = primitive_step(circuit; kwarg...)
if isnothing(r)
verbose && println("No more candidates to ", primitive_step, ". Skipping...")
deleteat!(valid_prims, which_primitive)
m -= 1
if m == 0
verbose && println("No more transformations available.")
return circuit
end
end
c2, _ = r
if stop(c2)
return c2
end
Expand Down
5 changes: 5 additions & 0 deletions test/transformations_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@ end
# TODO
# @test isstructdecomposable(c1)
# @test isdeterministic(c1)

# Test when there are no candidates to transform.
c2 = (compile(PlainLogicCircuit, -Lit(2)) & compile(PlainLogicCircuit, Lit(1))) |
(compile(PlainLogicCircuit, -Lit(1)) & compile(PlainLogicCircuit, Lit(2)))
@test_nowarn struct_learn(c2; maxiter = 10, verbose = false)
end

@testset "Clone Candidates" begin
Expand Down