Skip to content

Commit d98fe73

Browse files
authored
Merge pull request #36 from TensorBFS/solution-files
Support reading solution files for the MAP, MMAP and PR tasks
2 parents 550867a + 288727b commit d98fe73

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed

src/Core.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ $(TYPEDEF)
2424
2525
* `obsvars` is a vector of observed variables,
2626
* `obsvals` is a vector of observed values,
27-
* `reference_marginals` is a vector of marginal probabilities.
27+
* `reference_solution` is a vector with the reference solution.
2828
"""
2929
struct UAIInstance{ET, FT <: Factor{ET}}
3030
nvars::Int
@@ -34,7 +34,7 @@ struct UAIInstance{ET, FT <: Factor{ET}}
3434

3535
obsvars::Vector{Int}
3636
obsvals::Vector{Int}
37-
reference_marginals::Vector{Vector{ET}}
37+
reference_solution::Union{Vector{Vector{ET}}, Vector{Int}}
3838
end
3939

4040
"""

src/utils.jl

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,41 @@ The UAI file formats are defined in:
7272
https://personal.utdallas.edu/~vibhav.gogate/uai16-evaluation/uaiformat.html
7373
"""
7474
function read_evidence_file(evidence_filepath::AbstractString)
75-
if isempty(evidence_filepath)
76-
# No evidence
77-
return Int64[], Int64[]
78-
else
79-
# Read the last line of the uai evid file
80-
line = open(evidence_filepath) do file
81-
readlines(file)
82-
end |> last
83-
84-
# Extract number of observed vars, and their id together with their corresponding value
85-
nobsvars, rest = split(line) |> x -> parse.(Int, x) |> x -> (x[1], x[2:end])
86-
observations = reshape(rest, 2, :)
87-
88-
# Convert to 1-based indexing
89-
obsvars = observations[1, :] .+ 1
90-
obsvals = observations[2, :]
91-
92-
@assert nobsvars == length(obsvars)
93-
end
75+
76+
isempty(evidence_filepath) && return Int64[], Int64[] # no evidence
77+
78+
# Read the last line of the uai evid file
79+
line = open(evidence_filepath) do file
80+
readlines(file)
81+
end |> last
82+
83+
# Extract number of observed vars, and their id together with their corresponding value
84+
nobsvars, rest = split(line) |> x -> parse.(Int, x) |> x -> (x[1], x[2:end])
85+
observations = reshape(rest, 2, :)
86+
87+
# Convert to 1-based indexing
88+
obsvars = observations[1, :] .+ 1
89+
obsvals = observations[2, :]
90+
91+
@assert nobsvars == length(obsvars)
9492

9593
return obsvars, obsvals
9694
end
9795

96+
function read_solution_file(solution_filepath::AbstractString; factor_eltype = Float64)
97+
result = Vector{factor_eltype}[]
98+
extension = splitext(solution_filepath)[2]
99+
if extension == ".MAR"
100+
return read_mar_solution_file(solution_filepath; factor_eltype)
101+
elseif extension == ".MAP" || extension == ".MMAP" || extension == ".PR"
102+
# Return the last line of the file as a vector of integers
103+
result = open(solution_filepath) do file
104+
readlines(file)
105+
end |> last |> split |> x -> parse.(Int, x)
106+
end
107+
return result
108+
end
109+
98110
"""
99111
$(TYPEDSIGNATURES)
100112
@@ -104,7 +116,7 @@ as in the model
104116
The UAI file formats are defined in:
105117
https://personal.utdallas.edu/~vibhav.gogate/uai16-evaluation/uaiformat.html
106118
"""
107-
function read_solution_file(solution_filepath::AbstractString; factor_eltype = Float64)
119+
function read_mar_solution_file(solution_filepath::AbstractString; factor_eltype = Float64)
108120

109121
# Read the uai mar file into an array of lines
110122
rawlines = open(solution_filepath) do file
@@ -185,8 +197,8 @@ function read_instance(
185197
)::UAIInstance
186198
nvars, cards, ncliques, factors = read_model_file(model_filepath; factor_eltype = eltype)
187199
obsvars, obsvals = read_evidence_file(evidence_filepath)
188-
reference_marginals = isempty(solution_filepath) ? Vector{eltype}[] : read_solution_file(solution_filepath)
189-
return UAIInstance(nvars, ncliques, cards, factors, obsvars, obsvals, reference_marginals)
200+
reference_solution = isempty(solution_filepath) ? Vector{eltype}[] : read_solution_file(solution_filepath)
201+
return UAIInstance(nvars, ncliques, cards, factors, obsvars, obsvals, reference_solution)
190202
end
191203

192204
function read_instance_from_string(uai::AbstractString; eltype = Float64)::UAIInstance

test/cuda.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ CUDA.allowscalar(false)
1515
# for dangling vertices, the output size is 1.
1616
npass = 0
1717
for i in 1:(instance.nvars)
18-
npass += (length(marginals2[i]) == 1 && instance.reference_marginals[i] == [0.0, 1]) || isapprox(Array(marginals2[i]), instance.reference_marginals[i]; atol = 1e-6)
18+
npass += (length(marginals2[i]) == 1 && instance.reference_solution[i] == [0.0, 1]) || isapprox(Array(marginals2[i]), instance.reference_solution[i]; atol = 1e-6)
1919
end
2020
@test npass == instance.nvars
2121
end

test/mar.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ end
1414
@testset "cached, rescaled contract" begin
1515
model_filepath, evidence_filepath, solution_filepath = get_instance_filepaths("Promedus_14", "MAR")
1616
instance = read_instance(model_filepath; evidence_filepath, solution_filepath)
17-
ref_sol = instance.reference_marginals
17+
ref_sol = instance.reference_solution
1818
optimizer = TreeSA(ntrials = 1, niters = 5, βs = 0.1:0.1:100)
1919
tn = TensorNetworkModel(instance; optimizer)
2020
p1 = probability(tn; usecuda = false, rescale = false)
@@ -68,7 +68,7 @@ end
6868
@testset "$(problem_name)" begin
6969
model_filepath, evidence_filepath, solution_filepath = get_instance_filepaths(problem_name, "MAR")
7070
instance = read_instance(model_filepath; evidence_filepath, solution_filepath)
71-
ref_sol = instance.reference_marginals
71+
ref_sol = instance.reference_solution
7272
obsvars = instance.obsvars
7373

7474
# does not optimize over open vertices

0 commit comments

Comments
 (0)