Skip to content

Commit

Permalink
Fix problem + Bump Version
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Dec 13, 2023
1 parent 70de172 commit 509c2a5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
23 changes: 11 additions & 12 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
name = "MQLib"
uuid = "16f11440-1623-44c9-850c-358a6c72f3c9"
version = "0.2.0"
authors = [
"pedromxavier <[email protected]>",
"pedroripper <[email protected]>",
]
name = "MQLib"
uuid = "16f11440-1623-44c9-850c-358a6c72f3c9"
authors = ["pedromxavier <[email protected]>", "pedroripper <[email protected]>"]
version = "0.3.0"

[deps]
MQLib_jll = "4dedf8fe-8d9a-5fb8-8563-19379e8d5c54"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
MQLib_jll = "4dedf8fe-8d9a-5fb8-8563-19379e8d5c54"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
QUBODrivers = "a3f166f7-2cd3-47b6-9e1e-6fbfe0449eb0"
QUBOTools = "60eb5b62-0a39-4ddc-84c5-97d2adff9319"

[compat]
MQLib_jll = "0.1"
QUBODrivers = "0.1"
julia = "1.6"
MQLib_jll = "0.1"
Printf = "1"
QUBODrivers = "0.3"
julia = "1.9"
61 changes: 38 additions & 23 deletions src/MQLib.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module MQLib

using Printf

import MQLib_jll
import QUBODrivers:
MOI,
Expand All @@ -10,25 +12,24 @@ import QUBODrivers:
@setup,
sample

using Printf

const HEURISTICS = Dict{String,String}()
const __VERSION__ = v"0.1.0"
const _HEURISTICS = Dict{String,String}()

function __init__()
MQLib_jll.MQLib() do exe
ms = eachmatch(r"([a-zA-Z0-9]+)\r?\n ([^\r\n]+)\r?\n?", read(`$exe -l`, String))
ms = eachmatch(r"([a-zA-Z0-9]+)\r?\n\s+([^\r\n]+)\r?\n?", read(`$exe -l`, String))

for m in ms
push!(HEURISTICS, m[1] => m[2])
push!(_HEURISTICS, m[1] => m[2])
end
end

return nothing
end

@setup Optimizer begin
name = "MQLib"
sense = :max
domain = :bool
version = v"0.1.0"
version = __VERSION__
attributes = begin
RandomSeed["seed"]::Union{Integer,Nothing} = nothing
NumberOfReads["num_reads"]::Integer = 1
Expand All @@ -37,8 +38,17 @@ end
end

function sample(sampler::Optimizer{T}) where {T}
α = QUBOTools.scale(sampler)
β = QUBOTools.offset(sampler)
n, L, Q, α, β = QUBOTools.qubo(sampler, :dict; sense = :max, domain = :bool)

V = Set{Int}(1:n)

model = QUBOTools.Model{Int,T,Int}(
V, L, Q;
scale = α,
offset = β,
sense = :max,
domain = :bool,
)

num_reads = MOI.get(sampler, MQLib.NumberOfReads())
silent = MOI.get(sampler, MOI.Silent())
Expand All @@ -50,7 +60,7 @@ function sample(sampler::Optimizer{T}) where {T}
error("Number of reads must be a positive integer")
end

if !isnothing(heuristic) && !haskey(HEURISTICS, heuristic)
if !isnothing(heuristic) && !haskey(_HEURISTICS, heuristic)
error("Invalid QUBO Heuristic code '$heuristic'")
end

Expand All @@ -65,21 +75,26 @@ function sample(sampler::Optimizer{T}) where {T}
end

samples = Sample{T,Int}[]
metadata = Dict{String,Any}("time" => Dict{String,Any}())
metadata = Dict{String,Any}(
"time" => Dict{String,Any}(),
"origin" => Dict{String,Any}(
"name" => "MQLib",
"version" => __VERSION__,
"heuristic" => heuristic,
),
)

mktempdir() do temp_path
file_path = joinpath(temp_path, "file.qubo")

fmt = QUBOTools.QUBO(QUBOTools.BoolDomain(), QUBOTools.MQLibStyle())
file_path = joinpath(temp_path, "model.qubo")

args = _mqlib_args(;
file_path = file_path,
heuristic = heuristic,
random_seed = random_seed,
run_time_limit = run_time_limit,
file_path,
heuristic,
random_seed,
run_time_limit,
)

QUBOTools.write_model(file_path, sampler, fmt)
QUBOTools.write_model(file_path, model, QUBOTools.QUBO(:mqlib))

MQLib_jll.MQLib() do exe
cmd = `$exe $args`
Expand Down Expand Up @@ -112,7 +127,7 @@ function sample(sampler::Optimizer{T}) where {T}
end
end

return SampleSet{T}(samples, metadata)
return SampleSet{T}(samples, metadata; sense = :max, domain = :bool)
end

function _print_header(silent::Bool, heuristic::Union{String,Nothing})
Expand Down Expand Up @@ -199,12 +214,12 @@ function get_heuristic(model)
end

function heuristics()
return sort!(collect(keys(HEURISTICS)))
return sort!(collect(keys(_HEURISTICS)))
end

function show_heuristics()
for heuristic in heuristics()
println("$(heuristic): \n $(HEURISTICS[heuristic])")
println("$(heuristic):\n $(_HEURISTICS[heuristic])")
end

return nothing
Expand Down
4 changes: 2 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import MQLib: MOI, MQLib, QUBODrivers

QUBODrivers.test(MQLib.Optimizer) do model
MOI.set(model, MOI.Silent(), true)
MQLib.set_heuristic(model, first(MQLib.heuristics()))
end
MOI.set(model, MQLib.Heuristic(), first(MQLib.heuristics()))
end

0 comments on commit 509c2a5

Please sign in to comment.