Skip to content

Commit

Permalink
Update QUBOTools, Anneal -> 0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Jan 6, 2023
1 parent cbe5bf4 commit 4df8299
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
align_assignment = true
align_pair_arrow = true
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ authors = [
"pedromxavier <[email protected]>",
"pedroripper <[email protected]>",
]
version = "0.1.1"
version = "0.1.2"

[deps]
Anneal = "e4d9eb7f-b088-426e-aeb5-1c0dae3d8abb"
MQLib_jll = "4dedf8fe-8d9a-5fb8-8563-19379e8d5c54"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[compat]
Anneal = "0.5.7"
Anneal = "0.6"
MQLib_jll = "0.1"
julia = "1.6"
81 changes: 43 additions & 38 deletions src/MQLib.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ const _MQLIB_HEURISTICS = Dict{String,String}()

function __init__()
MQLib_jll.MQLib() do exe
for m in 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 ([^\r\n]+)\r?\n?", read(`$exe -l`, String))

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

Anneal.@anew Optimizer begin
name = "MQLib"
sense = :max
name = "MQLib"
sense = :max
domain = :bool
attributes = begin
RandomSeed["seed"]::Union{Integer,Nothing} = nothing
NumberOfReads["num_reads"]::Integer = 1
RandomSeed["seed"]::Union{Integer,Nothing} = nothing
NumberOfReads["num_reads"]::Integer = 1
Heuristic["heuristic"]::Union{String,Nothing} = nothing
end
end
Expand All @@ -39,8 +41,8 @@ function Anneal.sample(sampler::Optimizer{T}) where {T}

if num_reads <= 0
error("Number of reads must be a positive integer")
end
end

if !isnothing(heuristic) && !haskey(_MQLIB_HEURISTICS, heuristic)
error("Invalid QUBO Heuristic code '$heuristic'")
end
Expand All @@ -56,31 +58,28 @@ function Anneal.sample(sampler::Optimizer{T}) where {T}
end

samples = Anneal.Sample{T,Int}[]
metadata = Dict{String,Any}(
"time" => Dict{String,Any}()
)
metadata = Dict{String,Any}("time" => Dict{String,Any}())

mktempdir() do path
qubo_file = joinpath(path, "file.qubo")

QUBOTools.write_model(
qubo_file,
sampler,
QUBOTools.QUBO(; style = :mqlib)
)

fmt = QUBOTools.QUBO(QUBOTools.BoolDomain(), QUBOTools.MQLibStyle())

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


QUBOTools.write_model(qubo_file, sampler, fmt)

MQLib_jll.MQLib() do exe
cmd = `$exe $args`
t = 0.0


_print_header(silent, heuristic)

t = 0.0

for i = 1:num_reads
lines = readlines(cmd)
Expand All @@ -95,10 +94,9 @@ function Anneal.sample(sampler::Optimizer{T}) where {T}
m = collect(eachmatch(r"(([0-9]+):([0-9]+))+", info[6]))
λ̄ = parse.(Float64, getindex.(m, 2))
= parse.(Float64, getindex.(m, 3))
t += parse(Float64, info[5])

_print_iter(silent, i, λ̄, t .+ t̄)

t += parse(Float64, info[5])
end

_print_footer(silent)
Expand All @@ -112,18 +110,15 @@ end

function _print_header(silent::Bool, heuristic::Union{String,Nothing})
if !silent
if isnothing(heuristic)
heuristic = "Hyper-Heuristic"
end
heuristic = something(heuristic, "Hyper-Heuristic")

print(
"""
* MQLib
* Heuristic: $(heuristic)
*------*-------------*--------*
| iter | value | time |
*------*-------------*--------*
▷ MQLib
▷ Heuristic: $(heuristic)
┌────────┬─────────────┬──────────┐
│ iter │ value │ time │
├────────┼─────────────┼──────────┤
"""
)
end
Expand All @@ -133,18 +128,24 @@ end

function _print_footer(silent::Bool)
if !silent
println("*------*-------------*--------*")
println(
"""
└────────┴─────────────┴──────────┘
"""
)
end

return nothing
end

function _print_iter(silent::Bool, i::Integer, λ::Vector{Float64}, t::Vector{Float64})
if !silent
for (λ̄, t̄) in zip(λ, t)
if isnothing(i)
@printf("| | %11.2f | %6.2f |\n", λ̄, t̄)
@printf(" %11.3f │ %8.2f \n", λ̄, t̄)
else
@printf("| %4d | %11.2f | %6.2f |\n", i, λ̄, t̄)
@printf("│ %6d │ %11.3f │ %8.2f \n", i, λ̄, t̄)

i = nothing
end
end
Expand Down Expand Up @@ -190,10 +191,14 @@ function get_heuristic(model)
return MOI.get(model, MQLib.Heuristic())
end

function list_heuristics()
for (heuristic, description) in sort(collect(_MQLIB_HEURISTICS))
println("$(heuristic): \n $(description)")
end
function heuristics()
return sort!(collect(keys(_MQLIB_HEURISTICS)))
end

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

return nothing
end
Expand Down
4 changes: 3 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import MQLib

MQLib.test(; examples=true)
MQLib.test(; examples=true) do model
MQLib.set_heuristic(model, first(MQLib.heuristics()))
end

0 comments on commit 4df8299

Please sign in to comment.