diff --git a/Project.toml b/Project.toml index f8e72a49..e1e36de7 100644 --- a/Project.toml +++ b/Project.toml @@ -7,7 +7,7 @@ authors = [ "joaquimg ", "bernalde " ] -version = "0.5.2" +version = "0.5.3" [deps] InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240" diff --git a/src/analysis/plots.jl b/src/analysis/plots.jl index f22cb35c..bcb09cc0 100644 --- a/src/analysis/plots.jl +++ b/src/analysis/plots.jl @@ -2,32 +2,25 @@ title --> "Solution Summary" xlabel --> "Energy" ylabel --> "Frequency" - fill --> 0 legend --> nothing - seriestype := :bar - - # n = length(ω) - # a = ω[1].value - # b = ω[n].value - # m = 1000 - # σ = 0.08 * (b - a) - # t = range(a, b, m) - # z = zeros(T, m) - - # for i = 1:n - # μ = ω[i].value - # y = ω[i].reads + + x = value.(ω) + y = reads.(ω) + n = length(y) + z = zeros(Int, n) + λ = nothing - # for j = 1:m - # x = t[j] - # z[j] += y * exp(-((x - μ) / σ)^2) - # end - # end + for i = 1:n + if !isnothing(λ) && λ ≈ x[i] + z[i] = y[i-1] + y[i] = y[i] + z[i] + end - # return (t, z) + λ = x[i] + end - x = value.(ω) - y = reads.(ω) + seriestype := :bar + fillrange := z return (x, y) end \ No newline at end of file diff --git a/src/formats/bqpjson/printer.jl b/src/formats/bqpjson/printer.jl index 92c159c9..4b4f1def 100644 --- a/src/formats/bqpjson/printer.jl +++ b/src/formats/bqpjson/printer.jl @@ -71,8 +71,8 @@ function write_model(io::IO, model::AbstractModel{D}, ::BQPJSON{D}) where {D<:Va assignment = Dict{String,Any}[ Dict{String,Any}( "id" => i, - "value" => state(s, j) - ) for (i, j) in variable_map(model) + "value" => state(s, i), + ) for i in values(variable_map(model)) ] for _ = 1:reads(s) diff --git a/src/formats/qubist/printer.jl b/src/formats/qubist/printer.jl index 755798e3..24786cda 100644 --- a/src/formats/qubist/printer.jl +++ b/src/formats/qubist/printer.jl @@ -18,11 +18,11 @@ function write_model(io::IO, model::AbstractModel{D}, fmt::Qubist{D}) where {D} _print_header(io, data, fmt) for (i, h) in linear_terms(model) - println(io, "$(variable_inv(model, i)) $(variable_inv(model, i)) $(h)") + println(io, "$(i) $(i) $(h)") end for ((i, j), J) in quadratic_terms(model) - println(io, "$(variable_inv(model, i)) $(variable_inv(model, j)) $(J)") + println(io, "$(i) $(j) $(J)") end return nothing diff --git a/src/formats/qubo/printer.jl b/src/formats/qubo/printer.jl index 2708847a..db1b6ca8 100644 --- a/src/formats/qubo/printer.jl +++ b/src/formats/qubo/printer.jl @@ -70,14 +70,14 @@ function write_model(io::IO, model::AbstractModel{D}, fmt::QUBO{D}) where {D} !isnothing(fmt.comment) && println(io, "$(fmt.comment) linear terms") - for (i, l) in explicit_linear_terms(model) - println(io, "$(variable_inv(model, i)) $(variable_inv(model, i)) $(l)") + for (i, l) in linear_terms(model) + println(io, "$(i) $(i) $(l)") end !isnothing(fmt.comment) && println(io, "$(fmt.comment) quadratic terms") for ((i, j), q) in quadratic_terms(model) - println(io, "$(variable_inv(model, i)) $(variable_inv(model, j)) $(q)") + println(io, "$(i) $(j) $(q)") end return nothing diff --git a/src/interface/fallback.jl b/src/interface/fallback.jl index 769fc09f..4250c3d6 100644 --- a/src/interface/fallback.jl +++ b/src/interface/fallback.jl @@ -5,6 +5,8 @@ This file contains fallback implementations by calling the model's backend. This allows for external models to define a QUBOTools-based backend and profit from these queries. """ +frontend(model) = backend(model) + # ~*~ Data access ~*~ # model_name(model) = model_name(backend(model)) domain(model) = domain(backend(model)) @@ -42,4 +44,8 @@ quadratic_size(model) = quadratic_size(backend(model)) density(model) = density(backend(model)) linear_density(model) = linear_density(backend(model)) quadratic_density(model) = quadratic_density(backend(model)) -adjacency(model, args...) = adjacency(backend(model), args...) \ No newline at end of file +adjacency(model, args...) = adjacency(backend(model), args...) + +# ~*~ File I/O ~*~ # +write_model(io, model, args...) = write_model(io, backend(model), args...) +read_model!(io, model, args...) = read_model!(io, backend(model), args...) \ No newline at end of file diff --git a/src/interface/generic.jl b/src/interface/generic.jl index b4ff228f..c7fd78b2 100644 --- a/src/interface/generic.jl +++ b/src/interface/generic.jl @@ -334,7 +334,7 @@ function ising(Q::SparseMatrixCSC{T}, α::T = one(T), β::T = zero(T)) where {T} end swap_sense(::Nothing) = nothing -swap_sense(s::Sense) = s === Min ? Max : Min +swap_sense(s::Sense) = s === Min ? Max : Min swap_sense(target::Symbol, x::Any) = swap_sense(Sense(target), x) swap_sense(source::Symbol, target::Symbol, x::Any) = swap_sense(Sense(source), Sense(target), x) @@ -356,4 +356,18 @@ end function swap_sense(Q::Dict{Tuple{Int,Int},T}) where {T} return Dict{Tuple{Int,Int},T}(ij => -c for (ij, c) in Q) +end + +function format( + source_sense::Sense, + source_domain::VariableDomain, + target_sense::Sense, + target_domain::VariableDomain, + x::Any, +) + return swap_sense( + source_sense, + target_sense, + swap_domain(source_domain, target_domain, x), + ) end \ No newline at end of file diff --git a/src/interface/interface.jl b/src/interface/interface.jl index 476c2492..385caa93 100644 --- a/src/interface/interface.jl +++ b/src/interface/interface.jl @@ -83,6 +83,14 @@ Returns a list containing all available QUBO file formats. Given a file path, tries to infer the type associated to a QUBO model format. """ function infer_format end +@doc raw""" + frontend(model)::AbstractModel + frontend(model::AbstractModel)::AbstractModel + +Retrieves the model's backend. +Implementing this function allows one to profit from fallback implementations of the other methods. +""" function frontend end + @doc raw""" backend(model)::AbstractModel backend(model::AbstractModel)::AbstractModel @@ -427,5 +435,11 @@ If a second parameter, an integer, is present, then the set of neighbors of that @doc raw""" format(data::Vector{Sample{T,U}}) where {T,U} - + format( + source_sense::Sense, + source_domain::VariableDomain, + target_sense::Sense, + target_domain::VariableDomain, + x::Any + ) """ function format end \ No newline at end of file diff --git a/src/model/model.jl b/src/model/model.jl index 72dbe3cf..9302603e 100644 --- a/src/model/model.jl +++ b/src/model/model.jl @@ -144,14 +144,14 @@ function Base.copy(model::Model{D,V,T,U}) where {D,V,T,U} copy(model.quadratic_terms), copy(model.variable_map), copy(model.variable_inv); - scale = model.scale, - offset = model.offset, - sense = model.sense, - id = model.id, - version = model.version, - description = model.description, - metadata = deepcopy(model.metadata), - sampleset = model.sampleset, + scale = scale(model), + offset = offset(model), + sense = sense(model), + id = id(model), + version = version(model), + description = description(model), + metadata = deepcopy(metadata(model)), + sampleset = copy(sampleset(model)), ) end @@ -191,6 +191,7 @@ function swap_domain(::X, ::Y, model::Model{X,V,T,U}) where {X,Y,V,T,U} copy(variable_inv(model)); scale = α, offset = β, + sense = sense(model), id = id(model), version = version(model), description = description(model),