From 509c2a510f68cc0025e3fc97ca372d42f46f99d9 Mon Sep 17 00:00:00 2001 From: Pedro Maciel Xavier Date: Tue, 12 Dec 2023 19:59:34 -0500 Subject: [PATCH] Fix problem + Bump Version --- Project.toml | 23 +++++++++--------- src/MQLib.jl | 61 ++++++++++++++++++++++++++++++------------------ test/runtests.jl | 4 ++-- 3 files changed, 51 insertions(+), 37 deletions(-) diff --git a/Project.toml b/Project.toml index 0db18c4..a3dfc78 100644 --- a/Project.toml +++ b/Project.toml @@ -1,17 +1,16 @@ -name = "MQLib" -uuid = "16f11440-1623-44c9-850c-358a6c72f3c9" -version = "0.2.0" -authors = [ - "pedromxavier ", - "pedroripper ", -] +name = "MQLib" +uuid = "16f11440-1623-44c9-850c-358a6c72f3c9" +authors = ["pedromxavier ", "pedroripper "] +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" diff --git a/src/MQLib.jl b/src/MQLib.jl index 47157ec..eca84f8 100644 --- a/src/MQLib.jl +++ b/src/MQLib.jl @@ -1,5 +1,7 @@ module MQLib +using Printf + import MQLib_jll import QUBODrivers: MOI, @@ -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 @@ -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()) @@ -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 @@ -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` @@ -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}) @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index 6d8716e..73b8725 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 \ No newline at end of file + MOI.set(model, MQLib.Heuristic(), first(MQLib.heuristics())) +end