@@ -72,29 +72,41 @@ The UAI file formats are defined in:
7272https://personal.utdallas.edu/~vibhav.gogate/uai16-evaluation/uaiformat.html
7373"""
7474function 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
9694end
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
104116The UAI file formats are defined in:
105117https://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 )
190202end
191203
192204function read_instance_from_string (uai:: AbstractString ; eltype = Float64):: UAIInstance
0 commit comments