@@ -5,7 +5,7 @@ Parse the problem instance found in `model_filepath` defined in the UAI model
55format.
66
77The UAI file formats are defined in:
8- https://personal.utdallas.edu/~vibhav.gogate/uai16-evaluation/uaiformat.html
8+ https://uaicompetition.github.io/uci-2022/file-formats/
99"""
1010function read_model_file (model_filepath; factor_eltype = Float64)
1111 # Read the uai file into an array of lines
@@ -69,7 +69,7 @@ Return the observed variables and values in `evidence_filepath`. If the passed
6969file path is an empty string, return empty vectors.
7070
7171The UAI file formats are defined in:
72- https://personal.utdallas.edu/~vibhav.gogate/uai16-evaluation/uaiformat.html
72+ https://uaicompetition.github.io/uci-2022/file-formats/
7373"""
7474function read_evidence_file (evidence_filepath:: AbstractString )
7575
@@ -93,35 +93,67 @@ function read_evidence_file(evidence_filepath::AbstractString)
9393 return obsvars, obsvals
9494end
9595
96+ """
97+ $(TYPEDSIGNATURES)
98+
99+ Return the query variables in `query_filepath`. If the passed file path is an
100+ empty string, return an empty vector.
101+
102+ The UAI file formats are defined in:
103+ https://uaicompetition.github.io/uci-2022/file-formats/
104+ """
105+ function read_query_file (query_filepath:: AbstractString )
106+ isempty (query_filepath) && return Int64[]
107+
108+ # Read the first line of the uai query file
109+ line = open (query_filepath) do file
110+ readlines (file)
111+ end |> first
112+
113+ # Separate the number of query vars and their indices
114+ nqueryvars, queryvars_zero_based = split (line) |> x -> parse .(Int, x) |> x -> (x[1 ], x[2 : end ])
115+
116+ # Convert to 1-based indexing
117+ queryvars = queryvars_zero_based .+ 1
118+
119+ @assert nqueryvars == length (queryvars)
120+
121+ return queryvars
122+ end
123+
96124function read_solution_file (solution_filepath:: AbstractString ; factor_eltype = Float64)
125+
97126 result = Vector{factor_eltype}[]
98127 extension = splitext (solution_filepath)[2 ]
128+
129+ # Read the solution file into an array of lines
130+ rawlines = open (solution_filepath) do file
131+ readlines (file)
132+ end
133+
99134 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)
135+ result = parse_mar_solution_file (rawlines; factor_eltype)
136+ elseif extension == " .MAP" || extension == " .MMAP"
137+ # Return all elements except the first in the last line as a vector of integers
138+ result = last (rawlines) |> split |> x -> x[2 : end ] |> x -> parse .(Int, x)
139+ elseif extension == " .PR"
140+ # Parse the number in the last line as a floating point
141+ result = last (rawlines) |> x -> parse (Float64, x)
106142 end
143+
107144 return result
108145end
109146
110147"""
111148$(TYPEDSIGNATURES)
112149
113- Return the marginals of all variables. The order of the variables is the same
114- as in the model
150+ Parse the solution marginals of all variables from the UAI MAR solution file.
151+ The order of the variables is the same as in the model definition.
115152
116153The UAI file formats are defined in:
117- https://personal.utdallas.edu/~vibhav.gogate/uai16-evaluation/uaiformat.html
154+ https://uaicompetition.github.io/uci-2022/file-formats/
118155"""
119- function read_mar_solution_file (solution_filepath:: AbstractString ; factor_eltype = Float64)
120-
121- # Read the uai mar file into an array of lines
122- rawlines = open (solution_filepath) do file
123- readlines (file)
124- end
156+ function parse_mar_solution_file (rawlines:: Vector{String} ; factor_eltype = Float64)
125157
126158 parsed_margs = split (rawlines[2 ]) |> x -> x[2 : end ] |> x -> parse .(factor_eltype, x)
127159
@@ -192,13 +224,15 @@ Read a UAI problem instance from a file.
192224function read_instance (
193225 model_filepath:: AbstractString ;
194226 evidence_filepath:: AbstractString = " " ,
227+ query_filepath:: AbstractString = " " ,
195228 solution_filepath:: AbstractString = " " ,
196229 eltype = Float64
197230):: UAIInstance
198231 nvars, cards, ncliques, factors = read_model_file (model_filepath; factor_eltype = eltype)
199232 obsvars, obsvals = read_evidence_file (evidence_filepath)
233+ queryvars = read_query_file (query_filepath)
200234 reference_solution = isempty (solution_filepath) ? Vector{eltype}[] : read_solution_file (solution_filepath)
201- return UAIInstance (nvars, ncliques, cards, factors, obsvars, obsvals, reference_solution)
235+ return UAIInstance (nvars, ncliques, cards, factors, obsvars, obsvals, queryvars, reference_solution)
202236end
203237
204238function read_instance_from_string (uai:: AbstractString ; eltype = Float64):: UAIInstance
0 commit comments