From f286a212023c7c164baf9e183c66d8c5d2b18ecb Mon Sep 17 00:00:00 2001 From: Pedro Maciel Xavier Date: Sun, 9 Apr 2023 13:09:50 -0300 Subject: [PATCH] Use `QUBODrivers` --- Project.toml | 14 ++++++------- src/MQLib.jl | 53 +++++++++++++++++++++++++++--------------------- test/runtests.jl | 5 +++-- 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/Project.toml b/Project.toml index 55db4a8..0db18c4 100644 --- a/Project.toml +++ b/Project.toml @@ -1,17 +1,17 @@ name = "MQLib" uuid = "16f11440-1623-44c9-850c-358a6c72f3c9" +version = "0.2.0" authors = [ "pedromxavier ", "pedroripper ", ] -version = "0.1.2" [deps] -Anneal = "e4d9eb7f-b088-426e-aeb5-1c0dae3d8abb" -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" [compat] -Anneal = "0.6" -MQLib_jll = "0.1" -julia = "1.6" +MQLib_jll = "0.1" +QUBODrivers = "0.1" +julia = "1.6" diff --git a/src/MQLib.jl b/src/MQLib.jl index 936483b..47157ec 100644 --- a/src/MQLib.jl +++ b/src/MQLib.jl @@ -1,27 +1,34 @@ module MQLib import MQLib_jll -using Anneal -using Printf +import QUBODrivers: + MOI, + QUBODrivers, + QUBOTools, + Sample, + SampleSet, + @setup, + sample -export get_heuristic, set_heuristic, unset_heuristic, list_heuristics +using Printf -const _MQLIB_HEURISTICS = Dict{String,String}() +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)) for m in ms - push!(_MQLIB_HEURISTICS, m[1] => m[2]) + push!(HEURISTICS, m[1] => m[2]) end end end -Anneal.@anew Optimizer begin - name = "MQLib" - sense = :max - domain = :bool +@setup Optimizer begin + name = "MQLib" + sense = :max + domain = :bool + version = v"0.1.0" attributes = begin RandomSeed["seed"]::Union{Integer,Nothing} = nothing NumberOfReads["num_reads"]::Integer = 1 @@ -29,7 +36,7 @@ Anneal.@anew Optimizer begin end end -function Anneal.sample(sampler::Optimizer{T}) where {T} +function sample(sampler::Optimizer{T}) where {T} α = QUBOTools.scale(sampler) β = QUBOTools.offset(sampler) @@ -43,7 +50,7 @@ function Anneal.sample(sampler::Optimizer{T}) where {T} error("Number of reads must be a positive integer") end - if !isnothing(heuristic) && !haskey(_MQLIB_HEURISTICS, heuristic) + if !isnothing(heuristic) && !haskey(HEURISTICS, heuristic) error("Invalid QUBO Heuristic code '$heuristic'") end @@ -57,28 +64,28 @@ function Anneal.sample(sampler::Optimizer{T}) where {T} time_limit_sec / num_reads end - samples = Anneal.Sample{T,Int}[] + samples = Sample{T,Int}[] metadata = Dict{String,Any}("time" => Dict{String,Any}()) - mktempdir() do path - qubo_file = joinpath(path, "file.qubo") + mktempdir() do temp_path + file_path = joinpath(temp_path, "file.qubo") fmt = QUBOTools.QUBO(QUBOTools.BoolDomain(), QUBOTools.MQLibStyle()) args = _mqlib_args(; - qubo_file = qubo_file, + file_path = file_path, heuristic = heuristic, random_seed = random_seed, run_time_limit = run_time_limit, ) - QUBOTools.write_model(qubo_file, sampler, fmt) + QUBOTools.write_model(file_path, sampler, fmt) MQLib_jll.MQLib() do exe cmd = `$exe $args` - + _print_header(silent, heuristic) - + t = 0.0 for i = 1:num_reads @@ -105,7 +112,7 @@ function Anneal.sample(sampler::Optimizer{T}) where {T} end end - return Anneal.SampleSet{T}(samples, metadata) + return SampleSet{T}(samples, metadata) end function _print_header(silent::Bool, heuristic::Union{String,Nothing}) @@ -155,12 +162,12 @@ function _print_iter(silent::Bool, i::Integer, λ::Vector{Float64}, t::Vector{Fl end function _mqlib_args(; - qubo_file::String, + file_path::String, heuristic::Union{String,Nothing}, random_seed::Union{Integer,Nothing}, run_time_limit::Float64, ) - args = `-fQ $qubo_file -r $run_time_limit -nv -ps` + args = `-fQ $file_path -r $run_time_limit -nv -ps` if !isnothing(random_seed) args = `$args -s $random_seed` @@ -192,12 +199,12 @@ function get_heuristic(model) end function heuristics() - return sort!(collect(keys(_MQLIB_HEURISTICS))) + return sort!(collect(keys(HEURISTICS))) end function show_heuristics() for heuristic in heuristics() - println("$(heuristic): \n $(_MQLIB_HEURISTICS[heuristic])") + println("$(heuristic): \n $(HEURISTICS[heuristic])") end return nothing diff --git a/test/runtests.jl b/test/runtests.jl index 99d6853..6d8716e 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,5 +1,6 @@ -import MQLib +import MQLib: MOI, MQLib, QUBODrivers -MQLib.test(; examples=true) do model +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