Skip to content

Commit

Permalink
Add tests for fallback methods
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Sep 23, 2023
1 parent d8ddc99 commit 8694253
Show file tree
Hide file tree
Showing 9 changed files with 309 additions and 44 deletions.
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ QUBOTools.State
QUBOTools.AbstractSample
QUBOTools.Sample
QUBOTools.sample
QUBOTools.hassample
```

```@docs
Expand Down
3 changes: 3 additions & 0 deletions src/interface/fallback.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ frame(src) = frame(backend(src))
id(src) = id(backend(src))
description(src) = description(backend(src))
metadata(src) = metadata(backend(src))

# Variables
index(src, v) = index(backend(src), v)
hasindex(src, i) = hasindex(backend(src), i)
indices(src) = indices(backend(src))
Expand All @@ -41,6 +43,7 @@ value(src, i) = value(backend(src), i)
reads(src) = reads(backend(src))
reads(src, i) = reads(backend(src), i)
sample(src, i) = sample(backend(src), i)
hassample(src, i) = hassample(backend(src), i)
solution(src) = solution(backend(src))
start(src, args...; kws...) = start(backend(src), args...; kws...)
attach!(src, x) = attach!(backend(src), x)
Expand Down
9 changes: 8 additions & 1 deletion src/interface/solution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,19 @@ Returns the model's current solution.
function solution end

@doc raw"""
sample(model, i::Integer) where {T,U<:Integer}
sample(model, i::Integer)
Returns the ``i``-th sample on the model's current solution, if available.
"""
function sample end

@doc raw"""
hassample(solution::AbstractSolution, i::Integer)
Tells if the ``i``-th sample is available on the solution.
"""
function hassample end

@doc raw"""
state(sample::AbstractSample{T,U}) where {T,U<:Integer}
Expand Down
52 changes: 34 additions & 18 deletions src/library/model/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ function Base.isempty(model::AbstractModel)
return iszero(dimension(model))

Check warning on line 2 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L1-L2

Added lines #L1 - L2 were not covered by tests
end

function Base.broadcastable(model::AbstractModel)
return Ref(model) # Broadcast over model as scalar

Check warning on line 6 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L5-L6

Added lines #L5 - L6 were not covered by tests
end

function indices(model::AbstractModel)
return collect(1:dimension(model))
end
Expand Down Expand Up @@ -42,20 +46,28 @@ function form(
end

# ~*~ Data queries ~*~ #
function state(model::AbstractModel, index::Integer)
return state(solution(model), index)
function hassample(model::AbstractModel, i::Integer)
return hassample(solution(model), i)
end

function sample(model::AbstractModel, i::Integer)
return sample(solution(model), i)
end

function state(model::AbstractModel, i::Integer)
return state(solution(model), i)
end

function reads(model::AbstractModel)
return reads(solution(model))
end

function reads(model::AbstractModel, index::Integer)
return reads(solution(model), index)
function reads(model::AbstractModel, i::Integer)
return reads(solution(model), i)
end

function value(model::AbstractModel, index::Integer)
return value(solution(model), index)
function value(model::AbstractModel, i::Integer)
return value(solution(model), i)
end

function value(model::AbstractModel, ψ::State{U}) where {U}
Expand All @@ -67,9 +79,20 @@ dimension(model::AbstractModel) = dimension(form(model))
linear_size(model::AbstractModel) = linear_size(form(model))
quadratic_size(model::AbstractModel) = quadratic_size(form(model))

# Queries: Topology
topology(model::AbstractModel) = topology(form(model))
topology(model::AbstractModel, k::Integer) = adjacency(form(model), k)
# Queries: Layout, Topology & Geometry
topology(model::AbstractModel) = topology(form(model))
geometry(model::AbstractModel) = geometry(topology(model))

Check warning on line 84 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L84

Added line #L84 was not covered by tests

function geometry(g::AbstractGraph)
return NetworkLayout.layout(NetworkLayout.Shell(; Ptype = Float64), g)

Check warning on line 87 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L86-L87

Added lines #L86 - L87 were not covered by tests
end

function layout(model::AbstractModel)
g = QUBOTools.topology(model)
P = QUBOTools.geometry(g)

Check warning on line 92 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L90-L92

Added lines #L90 - L92 were not covered by tests

return Layout(g, P)

Check warning on line 94 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L94

Added line #L94 was not covered by tests
end

# Queries: Metadata
function id(model::AbstractModel)
Expand Down Expand Up @@ -140,8 +163,8 @@ function Base.show(io::IO, model::AbstractModel)
)
else
sol = solution(model)
n = length(sol)
z = sense(model) === Min ? value(sol[begin]) : value(sol[end])
n = length(sol)
z = value(sol, 1)

Check warning on line 167 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L165-L167

Added lines #L165 - L167 were not covered by tests

print(

Check warning on line 169 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L169

Added line #L169 was not covered by tests
io,
Expand All @@ -155,10 +178,3 @@ function Base.show(io::IO, model::AbstractModel)

return nothing

Check warning on line 179 in src/library/model/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/model/abstract.jl#L179

Added line #L179 was not covered by tests
end

function layout(model::AbstractModel)
g = QUBOTools.topology(model)
P = NetworkLayout.layout(NetworkLayout.Shell(; Ptype = Float64), g)

return Layout(g, P)
end
24 changes: 13 additions & 11 deletions src/library/model/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mutable struct Model{V,T,U,F<:AbstractForm{T}} <: AbstractModel{V,T,U}
form::F;
metadata::Union{Dict{String,Any},Nothing} = nothing,
solution::Union{SampleSet{T,U},Nothing} = nothing,
start::Union{Dict{Int,U},Nothing} = nothing,
start::Union{Dict{V,U},Nothing} = nothing,
# Extra Metadata
id::Union{Integer,Nothing} = nothing,
description::Union{String,Nothing} = nothing,
Expand All @@ -36,14 +36,6 @@ mutable struct Model{V,T,U,F<:AbstractForm{T}} <: AbstractModel{V,T,U}
metadata = Dict{String,Any}()
end

if isnothing(solution)
solution = SampleSet{T,U}()
end

if isnothing(start)
start = Dict{Int,U}()
end

if !isnothing(id)
metadata["id"] = id
end
Expand All @@ -52,7 +44,17 @@ mutable struct Model{V,T,U,F<:AbstractForm{T}} <: AbstractModel{V,T,U}
metadata["description"] = description
end

return new{V,T,U,F}(variable_map, form, metadata, solution, start)
model = new{V,T,U,F}(variable_map, form, metadata, SampleSet{T,U}(), Dict{Int,U}())

if !isnothing(solution)
attach!(model, solution)
end

if !isnothing(start)
attach!(model, start)
end

return model
end
end

Expand All @@ -64,7 +66,7 @@ function Model{V,T,U}(;
domain::Union{Domain,Symbol} = :bool,
metadata::Union{Dict{String,Any},Nothing} = nothing,
solution::Union{SampleSet{T,U},Nothing} = nothing,
start::Union{Dict{Int,U},Nothing} = nothing,
start::Union{Dict{V,U},Nothing} = nothing,
# Extra Metadata
id::Union{Integer,Nothing} = nothing,
description::Union{String,Nothing} = nothing,
Expand Down
38 changes: 26 additions & 12 deletions src/library/solution/abstract.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ state(s::AbstractSample, i::Integer) = getindex(state(s), i)
# Solutions
Base.size(sol::AbstractSolution) = (size(sol, 1),)

Check warning on line 7 in src/library/solution/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/solution/abstract.jl#L7

Added line #L7 was not covered by tests

function Base.size(sol::AbstractSolution, axis::Integer)
if axis == 1
return length(sol)

Check warning on line 11 in src/library/solution/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/solution/abstract.jl#L9-L11

Added lines #L9 - L11 were not covered by tests
else
return 1

Check warning on line 13 in src/library/solution/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/solution/abstract.jl#L13

Added line #L13 was not covered by tests
end
end

# Comparison
function Base.:(==)(x::S, y::S) where {T,U,S<:AbstractSample{T,U}}
return value(x) == value(y) && reads(x) == reads(y) && state(x) == state(y)
Expand All @@ -17,18 +25,24 @@ function Base.isapprox(x::S, y::S; kws...) where {T,U,S<:AbstractSample{T,U}}
state(x) == state(y)
end

function Base.size(sol::AbstractSolution, axis::Integer)
if axis == 1
return length(sol)
else
return 1
end
end

Base.firstindex(::AbstractSolution) = 1
Base.firstindex(::AbstractSolution, ::Integer) = 1

Check warning on line 29 in src/library/solution/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/solution/abstract.jl#L29

Added line #L29 was not covered by tests
Base.lastindex(sol::AbstractSolution) = length(sol)

function hassample(sol::AbstractSolution, i::Integer)
return 1 <= i <= length(sol)
end

function sample(sol::AbstractSolution, i::Integer)
if hassample(sol, i)
return getindex(sol, i)
else
error("Sample with index '$i' does not belong to the solution")

return nothing

Check warning on line 42 in src/library/solution/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/solution/abstract.jl#L42

Added line #L42 was not covered by tests
end
end

function Base.lastindex(sol::AbstractSolution, axis::Integer)
if axis == 1
return length(sol)
Expand Down Expand Up @@ -69,8 +83,8 @@ function Base.show(io::IO, sol::S) where {S<:AbstractSolution}
return nothing

Check warning on line 83 in src/library/solution/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/solution/abstract.jl#L83

Added line #L83 was not covered by tests
end

state(sol::AbstractSolution, i::Integer) = state(getindex(sol, i))
state(sol::AbstractSolution, i::Integer, j::Integer) = state(getindex(sol, i), j)
value(sol::AbstractSolution, i::Integer) = value(getindex(sol, i))
reads(sol::AbstractSolution, i::Integer) = reads(getindex(sol, i))
state(sol::AbstractSolution, i::Integer) = state(sample(sol, i))
state(sol::AbstractSolution, i::Integer, j::Integer) = state(sample(sol, i), j)

Check warning on line 87 in src/library/solution/abstract.jl

View check run for this annotation

Codecov / codecov/patch

src/library/solution/abstract.jl#L87

Added line #L87 was not covered by tests
value(sol::AbstractSolution, i::Integer) = value(sample(sol, i))
reads(sol::AbstractSolution, i::Integer) = reads(sample(sol, i))
reads(sol::AbstractSolution) = sum(reads.(sol))
1 change: 1 addition & 0 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[deps]
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ using Printf
using SparseArrays
using RecipesBase
using QUBOTools
using Graphs
using MathOptInterface

# using QUBODrivers
Expand Down
Loading

0 comments on commit 8694253

Please sign in to comment.