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

save metadata of zdd #45

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions ZDD/jl_zdd/weighted.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ using DataStructures
using LightGraphs
using Statistics
import Base: Dict
using JSON

include("weighted_node.jl")
include("node_auxillaries.jl")
Expand Down
3 changes: 2 additions & 1 deletion ZDD/jl_zdd/weightless.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using DataStructures
using LightGraphs
import Base: Dict
using JSON

include("weightless_node.jl")
include("node_auxillaries.jl")
Expand Down Expand Up @@ -85,7 +86,7 @@ end
function connect_components!(n::Node, Cᵤ::UInt8, Cᵥ::UInt8)
"""
If the two components are different, remove the smaller component from n.comp,
and update n.comp_assign.
and update n.comp_assign.
"""
assignment = max(Cᵤ, Cᵥ)
to_change = min(Cᵤ, Cᵥ)
Expand Down
30 changes: 26 additions & 4 deletions ZDD/jl_zdd/zdd.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ function construct_zdd(g::SimpleGraph,
g_edges::Array{NodeEdge,1};
weights::Vector{Int64}=Vector{Int64}([1 for i in 1:nv(g)]),
viz::Bool=false,
save_fp::String="zdd_tree.txt")::ZDD
tree_save_fp::String="zdd_tree.txt",
meta_save_fp::String="zdd_metadata.json")::ZDD
# delete file if it already exists
if isfile(save_fp)
rm(save_fp)
if isfile(tree_save_fp)
rm(tree_save_fp)
end
if isfile(meta_save_fp)
rm(meta_save_fp)
end

weights = Vector{UInt32}([convert(UInt32,i) for i in weights])
Expand All @@ -72,6 +76,7 @@ function construct_zdd(g::SimpleGraph,
reusable_set = Set{ForbiddenPair}([])
recycler = Stack{Node}()
lower_vs = Vector{UInt8}([])
layer_sizes = Vector{Int}([])

for i = 1:ne(g)
for n in N[i]
Expand Down Expand Up @@ -104,15 +109,32 @@ function construct_zdd(g::SimpleGraph,
add_zdd_edge!(zdd, n, n′, n_idx, x)
end
end
push!(layer_sizes, length(N[i]))
zdd.deleted_nodes += length(N[i])
save_tree_so_far!(zdd, save_fp, length(N[i]))
save_tree_so_far!(zdd, tree_save_fp, length(N[i]))
erase_upper_levels!(zdd, N[i+1], zero_terminal, one_terminal, length(N[i])) # release memory
N[i] = Set{Node}([]) # release memory
# println(i, ": ", Base.summarysize(zdd))
end
save_metadata(meta_save_fp, g_edges, layer_sizes)
return zdd
end

function save_metadata(save_fp::String, g_edges::Vector{NodeEdge}, layer_sizes::Vector{Int})
output_file = open(save_fp, "w")
# dictionary to write
dict = Dict("edges" => g_edges,
"layer_sizes" => layer_sizes)

# pass data as a json string (how it shall be displayed in a file)
stringdata = JSON.json(dict)

# write the file with the stringdata variable information
open(save_fp, "w") do f
write(f, stringdata)
end
end

function save_tree_so_far!(zdd::ZDD, save_fp::String, layer_size::Int)
output_file = open(save_fp, "a")
for zdd_node in zdd.graph[3:layer_size+2]
Expand Down