Skip to content

Commit

Permalink
Fix MQLib support
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Dec 17, 2022
1 parent 732aa40 commit 220e77c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 14 deletions.
34 changes: 32 additions & 2 deletions src/formats/qubo/parser.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
function _parse_line!(fmt::QUBO, data::Dict{Symbol,Any}, line::AbstractString)
_parse_entry!(fmt, data, line) && return nothing
_parse_entry!(fmt, data, line, fmt.style) && return nothing
_parse_comment!(fmt, data, line, fmt.comment) && return nothing
_parse_header!(fmt, data, line, fmt.style) && return nothing

syntax_error("$line")
end

function _parse_entry!(::QUBO, data::Dict{Symbol,Any}, line::AbstractString)
function _parse_entry!(fmt::QUBO, data::Dict{Symbol,Any}, line::AbstractString, style::Symbol)
return _parse_entry!(fmt, data, line, Val(style))
end

function _parse_entry!(::QUBO, data::Dict{Symbol,Any}, line::AbstractString, ::Any)
m = match(r"^([0-9]+) ([0-9]+) ([+-]?([0-9]*[.])?[0-9]+)$", line)

if isnothing(m)
Expand All @@ -32,6 +36,32 @@ function _parse_entry!(::QUBO, data::Dict{Symbol,Any}, line::AbstractString)
return true
end

function _parse_entry!(::QUBO, data::Dict{Symbol,Any}, line::AbstractString, ::Val{:mqlib})
m = match(r"^([0-9]+) ([0-9]+) ([+-]?([0-9]*[.])?[0-9]+)$", line)

if isnothing(m)
return false
end

i = tryparse(Int, m[1])
j = tryparse(Int, m[2])
c = tryparse(Float64, m[3])

if isnothing(i) || isnothing(j) || isnothing(c)
syntax_error("")
end

if i == j
L = data[:linear_terms]
L[i] = get(L, i, 0.0) + c
else
Q = data[:quadratic_terms]
Q[(i, j)] = get(Q, (i, j), 0.0) + 2c
end

return true
end

function _parse_header!(fmt::QUBO, data::Dict{Symbol,Any}, line::AbstractString, style::Symbol)
return _parse_header!(fmt, data, line, Val(style))
end
Expand Down
26 changes: 14 additions & 12 deletions src/formats/qubo/printer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ end
function _print_entries(io::IO, fmt::QUBO, data::Dict{Symbol,Any}, ::Any)
!isnothing(fmt.comment) && println(io, "$(fmt.comment) linear terms")

for (i, l) in linear_terms(model)
for (i, l) in data[:linear_terms]
println(io, "$(i) $(i) $(l)")
end

!isnothing(fmt.comment) && println(io, "$(fmt.comment) quadratic terms")

for ((i, j), q) in quadratic_terms(model)
for ((i, j), q) in data[:quadratic_terms]
println(io, "$(i) $(j) $(q)")
end

Expand All @@ -76,13 +76,13 @@ end
function _print_entries(io::IO, fmt::QUBO, data::Dict{Symbol,Any}, ::Val{:mqlib})
!isnothing(fmt.comment) && println(io, "$(fmt.comment) linear terms")

for (i, l) in linear_terms(model)
for (i, l) in data[:linear_terms]
println(io, "$(i) $(i) $(l)")
end

!isnothing(fmt.comment) && println(io, "$(fmt.comment) quadratic terms")

for ((i, j), q) in quadratic_terms(model)
for ((i, j), q) in data[:quadratic_terms]
println(io, "$(i) $(j) $(q/2)")
end

Expand All @@ -91,14 +91,16 @@ end

function write_model(io::IO, model::AbstractModel{D}, fmt::QUBO{D}) where {D}
data = Dict{Symbol,Any}(
:scale => scale(model),
:offset => offset(model),
:id => id(model),
:description => description(model),
:metadata => metadata(model),
:domain_size => domain_size(model),
:linear_size => linear_size(model),
:quadratic_size => quadratic_size(model),
:linear_terms => linear_terms(model),
:quadratic_terms => quadratic_terms(model),
:linear_size => linear_size(model),
:quadratic_size => quadratic_size(model),
:scale => scale(model),
:offset => offset(model),
:id => id(model),
:description => description(model),
:metadata => metadata(model),
:domain_size => domain_size(model),
)

_print_metadata(io, fmt, data, fmt.comment)
Expand Down

0 comments on commit 220e77c

Please sign in to comment.