Skip to content

Commit

Permalink
save BP results to file
Browse files Browse the repository at this point in the history
  • Loading branch information
annamariadziubyna committed Jan 19, 2024
1 parent 077dc0b commit e3d7148
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version = "1.0"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
HDF5 = "f67ccb44-e63f-5c2f-98bd-6dc0ccc4ba2f"
Expand Down
1 change: 1 addition & 0 deletions src/SpinGlassNetworks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module SpinGlassNetworks
using LightGraphs
using MetaGraphs # TODO: to be replaced by MetaGraphsNext
using CSV
using JLD2
using DocStringExtensions
using LinearAlgebra, MKL
using Base.Cartesian
Expand Down
60 changes: 50 additions & 10 deletions src/truncate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,62 @@ to keep. It computes the beliefs for all 2-site combinations and selects the sta
# Returns:
- `LabelledGraph{S, T}`: A truncated clustered Hamiltonian.
"""
# function truncate_clustered_hamiltonian_2site_BP(
# cl_h::LabelledGraph{S, T},
# beliefs::Dict,
# num_states::Int;
# beta=1.0
# ) where {S, T}
# # TODO: name to be clean to make it consistent with square2 and squarestar2
# states = Dict()
# for node in vertices(cl_h)
# if node in keys(states) continue end
# i, j, _ = node
# sx = has_vertex(cl_h, (i, j, 1)) ? length(get_prop(cl_h, (i, j, 1), :spectrum).energies) : 1
# E = beliefs[(i, j)]
# ind1, ind2 = select_numstate_best(E, sx, num_states)
# push!(states, (i, j, 1) => ind1)
# push!(states, (i, j, 2) => ind2)
# end
# truncate_clustered_hamiltonian(cl_h, states)
# end

function load_file(filename)
if isfile(filename)
try
load_object(string(filename))
catch e
return nothing
end
else
return nothing
end
end

function truncate_clustered_hamiltonian_2site_BP(
cl_h::LabelledGraph{S, T},
beliefs::Dict,
num_states::Int;
num_states::Int,
result_folder::String = "results_folder",
inst::String = "inst";
beta=1.0
) where {S, T}
# TODO: name to be clean to make it consistent with square2 and squarestar2
states = Dict()
for node in vertices(cl_h)
if node in keys(states) continue end
i, j, _ = node
sx = has_vertex(cl_h, (i, j, 1)) ? length(get_prop(cl_h, (i, j, 1), :spectrum).energies) : 1
E = beliefs[(i, j)]
ind1, ind2 = select_numstate_best(E, sx, num_states)
push!(states, (i, j, 1) => ind1)
push!(states, (i, j, 2) => ind2)

saved_states = load_file(joinpath(result_folder, "$(inst).jld2"))
if saved_states == nothing
for node in vertices(cl_h)
i, j, _ = node
sx = has_vertex(cl_h, (i, j, 1)) ? length(get_prop(cl_h, (i, j, 1), :spectrum).energies) : 1
E = beliefs[(i, j)]
ind1, ind2 = select_numstate_best(E, sx, num_states)
push!(states, (i, j, 1) => ind1)
push!(states, (i, j, 2) => ind2)
end
path = joinpath(result_folder, "$(inst).jld2")
save_object(string(path), states)
else
states = saved_states
end
truncate_clustered_hamiltonian(cl_h, states)
end
Expand Down

0 comments on commit e3d7148

Please sign in to comment.