Skip to content

Commit

Permalink
Merge pull request #30 from psrenergy/px/docs
Browse files Browse the repository at this point in the history
Update Docs
  • Loading branch information
pedromxavier authored Dec 15, 2022
2 parents d012252 + 3e54037 commit 00d9b00
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ authors = [
"joaquimg <[email protected]>",
"bernalde <[email protected]>"
]
version = "0.5.1"
version = "0.5.2"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
10 changes: 3 additions & 7 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,11 @@ where ``J \in \mathbb{R}^{n \times n}`` is upper triangular and ``\mathbf{h} \in
QUBOTools.jl is avaible through Julia's General Registry:

```julia-repl
julia> ]add QUBOTools
julia> import Pkg
julia> using QUBOTools
```

You might also be interested in the latest development version:
julia> Pkg.add("QUBOTools")
```julia-repl
julia> ]add QUBOTools#main
julia> using QUBOTools
```

## Objectives
Expand Down
33 changes: 29 additions & 4 deletions src/interface/interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,29 @@ const 𝔹 = BoolDomain

@doc raw"""
AbstractModel{D<:VariableDomain}
Represents an abstract QUBO Model and should support most of the queries made available
by `QUBOTools`.
## Example
A common use case is to build wrappers around the [`Model`](@ref) concrete type:
```julia
struct ModelWrapper{D} <: AbstractModel{D}
model::Model{D,Int,Float64,Int}
attrs::Dict{String,Any}
end
QUBOTools.backend(mw::ModelWrapper) = mw.model
```
As shown in the example above, implementing a method for the [`backend`](@ref) function
gives access to most fallback implementations.
""" abstract type AbstractModel{D<:VariableDomain} end

@doc raw"""
AbstractFormat{D<:VariableDomain}
""" abstract type AbstractFormat{D<:VariableDomain} end

@doc raw"""
Expand Down Expand Up @@ -168,7 +187,9 @@ Reveses the sign of the objective value.
""" function metadata end

@doc raw"""
sampleset(model)
sampleset(model)::SampleSet
Returns the [`SampleSet`](@ref) stored in a model.
""" function sampleset end

@doc raw"""
Expand Down Expand Up @@ -294,12 +315,16 @@ Returns Ising Model form from QUBO Model (Bool).

# ~*~ Data queries ~*~ #
@doc raw"""
state(model, index::Integer)
state(model, i::Integer)
Returns the state vector corresponding to the ``i``-th solution on the model's sampleset.
""" function state end

@doc raw"""
reads(model)
reads(model, index::Integer)
reads(model, i::Integer)
Returns the read frequency of the ``i``-th solution on the model's sampleset.
""" function reads end

@doc raw"""
Expand Down
9 changes: 6 additions & 3 deletions src/library/sampleset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ It was inspired by [1], with a few tweaks.
It is optimized to support queries over the solution set.
## References
[1] https://docs.ocean.dwavesys.com/en/stable/docs_dimod/reference/S.html#dimod.SampleSet
[1] [ocean docs](https://docs.ocean.dwavesys.com/en/stable/docs_dimod/reference/S.html#dimod.SampleSet)
""" struct SampleSet{T,U} <: AbstractSampleSet{T,U}
data::Vector{Sample{T,U}}
metadata::Dict{String,Any}
Expand Down Expand Up @@ -254,12 +254,15 @@ Base.isempty(ω::SampleSet) = isempty(ω.data)
Base.collect::SampleSet) = collect.data)
Base.getindex::SampleSet, i::Integer) = ω.data[i]

Base.iterate::SampleSet) = iterate.data)
Base.iterate::SampleSet, i::Integer) = iterate.data, i)

metadata::SampleSet) = ω.metadata

function swap_domain(::A, ::B, ω::SampleSet{T,U}) where {A<:𝔻,B<:𝔻,T,U}
return SampleSet{T,U}(swap_domain.(A(), B(), ω), deepcopy(metadata(ω)))
return SampleSet{T,U}(Vector{Sample{T,U}}(swap_domain.(A(), B(), ω)), deepcopy(metadata(ω)))
end

function swap_sense::SampleSet{T,U}) where {T,U}
return SampleSet{T,U}(swap_sense.(ω), deepcopy(metadata(ω)))
return SampleSet{T,U}(Vector{Sample{T,U}}(swap_sense.(ω)), deepcopy(metadata(ω)))
end
48 changes: 39 additions & 9 deletions src/model/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -362,30 +362,60 @@ function Base.copy!(
end

function Base.show(io::IO, model::AbstractModel)
print(
s = sense(model) === Min ? "Min" : "Max"

println(
io,
"""
$(model_name(model)):
$(domain_size(model)) variables [$(domain_name(model))]
$(model_name(model)) [$(s), $(domain_name(model))]
▷ Variables ……… $(domain_size(model))
"""
)

if !isempty(model)
print(
if isempty(model)
println(
io,
"""
The model is empty.
"""
)

return nothing
else
println(
io,
"""
Density:
linear ~ $(@sprintf("%0.2f", 100.0 * linear_density(model)))%
quadratic ~ $(@sprintf("%0.2f", 100.0 * quadratic_density(model)))%
total ~ $(@sprintf("%0.2f", 100.0 * density(model)))%
▷ Linear ……………… $(@sprintf("%0.2f", 100.0 * linear_density(model)))%
▷ Quadratic ……… $(@sprintf("%0.2f", 100.0 * quadratic_density(model)))%
▷ Total ………………… $(@sprintf("%0.2f", 100.0 * density(model)))%
"""
)
end

if isempty(sampleset(model))
print(
io,
"""
There are no solutions available.
"""
)

return nothing
else
ω = sampleset(model)
n = length(ω)
z = sense(model) === Min ? value(ω[begin]) : value(ω[end])

print(
io,
"""
The model is empty
Solutions:
▷ Samples …………… $(n)
▷ Best value …… $(z)
"""
)
end

return nothing
end
36 changes: 15 additions & 21 deletions src/model/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,18 @@ function swap_domain(::X, ::Y, model::Model{X,V,T,U}) where {X,Y,V,T,U}
offset(model),
)

ω = sampleset(model)

if isnothing(ω)
η = nothing
else
η = swap_domain(X(), Y(), sampleset(model))
end

return Model{Y,V,T,U}(
L,
Q;
Q,
copy(variable_map(model)),
copy(variable_inv(model));
scale = α,
offset = β,
id = id(model),
version = version(model),
description = description(model),
metadata = metadata(model),
sampleset = η,
sampleset = swap_domain(X(), Y(), sampleset(model)),
)
end

Expand All @@ -223,17 +217,17 @@ function swap_sense(model::Model{D,V,T,U}) where {D,V,T,U}
end

function Base.copy!(target::Model{D,V,T,U}, source::Model{D,V,T,U}) where {D,V,T,U}
target.linear_terms = copy(source.linear_terms)
target.quadratic_terms = copy(source.quadratic_terms)
target.variable_map = copy(source.variable_map)
target.variable_inv = copy(source.variable_inv)
target.scale = source.scale
target.offset = source.offset
target.id = source.id
target.version = source.version
target.description = source.description
target.metadata = deepcopy(source.metadata)
target.sampleset = source.sampleset
target.linear_terms = copy(linear_terms(source))
target.quadratic_terms = copy(quadratic_terms(source))
target.variable_map = copy(variable_map(source))
target.variable_inv = copy(variable_inv(source))
target.scale = scale(source)
target.offset = offset(source)
target.id = id(source)
target.version = version(source)
target.description = description(source)
target.metadata = deepcopy(metadata(source))
target.sampleset = copy(sampleset(source))

return target
end
Expand Down

2 comments on commit 00d9b00

@pedromxavier
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/74216

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.5.2 -m "<description of version>" 00d9b00d712495b622de15f71e7090edfc85e591
git push origin v0.5.2

Please sign in to comment.