Skip to content

Commit

Permalink
Prune code
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Sep 24, 2023
1 parent 9e686a1 commit 0754294
Show file tree
Hide file tree
Showing 8 changed files with 4 additions and 128 deletions.
1 change: 0 additions & 1 deletion src/QUBODrivers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export MOI, Sample, SampleSet, Spin, ↓, ↑
include("interface/sampler.jl")
include("interface/attributes.jl")

include("library/sampler/attributes.jl")
include("library/sampler/wrappers/moi.jl")
include("library/sampler/wrappers/qubotools.jl")

Expand Down
2 changes: 0 additions & 2 deletions src/interface/sampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ function sample end

function sample(::S) where {S<:AbstractSampler}
error("`QUBODrivers.sample` is not implemented for '$S'")

return nothing
end

@doc raw"""
Expand Down
2 changes: 0 additions & 2 deletions src/library/drivers/IdentitySampler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ function sample_state(sampler::Optimizer{T}, n::Integer) where {T}
v = QUBOTools.variable(sampler, i)

error("Warm-start value for '$v' is missing")

return nothing
end
end

Expand Down
8 changes: 0 additions & 8 deletions src/library/sampler/attributes.jl

This file was deleted.

69 changes: 4 additions & 65 deletions src/library/sampler/wrappers/moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,16 @@ MOI.supports_constraint(
) = false

# ~ They are also binary
MOI.supports_constraint(
::AbstractSampler,
::Type{VI},
::Type{MOI.ZeroOne},
) = true
MOI.supports_constraint(::AbstractSampler, ::Type{VI}, ::Type{MOI.ZeroOne}) = true

MOI.supports_constraint(
::AbstractSampler,
::Type{VI},
::Type{Spin},
) = true
MOI.supports_constraint(::AbstractSampler, ::Type{VI}, ::Type{Spin}) = true

# ~ Objective Function Support
MOI.supports(
::AbstractSampler,
::MOI.ObjectiveFunction{<:Any}
) = false
MOI.supports(::AbstractSampler, ::MOI.ObjectiveFunction{<:Any}) = false

MOI.supports(
::AbstractSampler{T},
::MOI.ObjectiveFunction{<:Union{SQF{T}, SAF{T}, VI}}
::MOI.ObjectiveFunction{<:Union{SQF{T},SAF{T},VI}},
) where {T} = true

# By default, all samplers are their own raw solvers.
Expand Down Expand Up @@ -144,12 +133,8 @@ function MOI.get(sampler::AbstractSampler{T}, vp::MOI.VariablePrimal, vi::VI) wh

if isempty(ω)
error("Invalid result index '$i'; There are no solutions")

return nothing
elseif !(1 <= i <= m)
error("Invalid result index '$i'; There are $(m) solutions")

return nothing
end

j = QUBOTools.index(sampler, vi)
Expand All @@ -161,49 +146,3 @@ end
function MOI.get(sampler::AbstractSampler, ::MOI.NumberOfVariables)
return QUBOTools.dimension(sampler)
end

# ~*~ File IO: Base API ~*~ #
# function Base.write(
# filename::AbstractString,
# sampler::AbstractSampler,
# fmt::QUBOTools.AbstractFormat = QUBOTools.infer_format(filename),
# )
# return QUBOTools.write_model(filename, sampler, fmt)
# end

# function Base.read!(
# filename::AbstractString,
# sampler::AbstractSampler,
# fmt::QUBOTools.AbstractFormat = QUBOTools.infer_format(filename),
# )
# sampler.source = QUBOTools.read_model(filename, fmt)
# sampler.target = QUBOTools.format(sampler, sampler.source)

# return sampler
# end

function warm_start(sampler::AbstractSampler, i::Integer)
v = QUBOTools.variable_inv(sampler, i)
x = MOI.get(sampler, MOI.VariablePrimalStart(), v)

if isnothing(x)
return nothing
else
return QUBOTools.cast(
source_domain(sampler) => target_domain(sampler),
round(Int, x),
)
end
end

function warm_start(sampler::AbstractSampler{T}) where {T}
n = MOI.get(sampler, MOI.NumberOfVariables())
s = sizehint!(Dict{Int,Int}(), n)

for i = 1:n
x = warm_start(sampler, i)
isnothing(x) || (s[i] = x)
end

return s
end
2 changes: 0 additions & 2 deletions src/library/setup/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@ end

function setup_error(msg::AbstractString)
throw(DriverSetupError(msg))

return nothing
end
40 changes: 0 additions & 40 deletions src/library/setup/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ end

function __setup_parse(args...)
setup_error("Macro takes 1 or 2 arguments, not '$(length(args))'")

return nothing
end

function __setup_parse(expr)
Expand All @@ -17,16 +15,12 @@ function __setup_parse(expr)
setup_error(
"Single argument must be either an identifier or a `begin ... end` block",
)

return nothing
end
end

function __setup_parse(id, block)
if !(id isa Symbol) || !Base.isidentifier(id)
setup_error("First argument must be a valid identifier")

return nothing
end

if !(block isa Expr && block.head === :block)
Expand Down Expand Up @@ -55,32 +49,24 @@ function __setup_parse_block(block; id = :Optimizer)
if key === :name
if !isnothing(name)
setup_error("Duplicate entries for 'name'")

return nothing
end

name = value
elseif key === :version
if !isnothing(version)
setup_error("Duplicate entries for 'version'")

return nothing
end

version = value
elseif key === :attributes
if !isnothing(attributes)
setup_error("Duplicate entries for 'attributes' block")

return nothing
end

if !(value isa Expr && value.head === :block)
setup_error(
"Sampler attributes must be placed inside a `begin ... end` block",
)

return nothing
end

attributes = _AttrSpec[]
Expand All @@ -96,25 +82,17 @@ function __setup_parse_block(block; id = :Optimizer)
setup_error(
"Sampler configuration keys must be either 'name', 'version' or 'attributes', not '$key'",
)

return nothing
end
else
setup_error("Sampler configuration keys must be a valid identifiers")

return nothing
end
else
setup_error("Sampler configuration must be provided by `key = value` pairs")

return nothing
end
end

if isnothing(name)
setup_error("'name' entry is missing")

return nothing
end

if isnothing(version)
Expand Down Expand Up @@ -149,16 +127,12 @@ function __setup_parse_attr(stmt)
if attr isa Symbol # ~ MOI attribute only
if !(Base.isidentifier(attr))
setup_error("Attribute identifier '$attr' is not valid")

return nothing
end

opt_attr = attr
elseif attr isa String # ~ Raw attribute only
if isempty(attr)
setup_error("Raw attribute key can't be an empty string")

return nothing
end

raw_attr = attr
Expand All @@ -168,8 +142,6 @@ function __setup_parse_attr(stmt)
if attr isa Symbol
if !(Base.isidentifier(attr))
setup_error("Attribute identifier '$attr' is not a valid one")

return nothing
end

opt_attr = attr
Expand All @@ -181,37 +153,25 @@ function __setup_parse_attr(stmt)
if opt_attr isa Symbol && raw_attr isa String
if !(Base.isidentifier(opt_attr))
setup_error("Attribute identifier '$opt_attr' is not a valid one")

return nothing
end
else
setup_error("Invalid attribute identifier '$name($raw)'")

return nothing
end
else
setup_error("Invalid attribute identifier '$attr'")

return nothing
end
elseif attr isa Expr && (attr.head === :ref || attr.head === :call)
opt_attr, raw_attr = attr.args

if opt_attr isa Symbol && raw_attr isa String
if !(Base.isidentifier(opt_attr))
setup_error("Attribute identifier '$opt_attr' is not a valid one")

return nothing
end
else
setup_error("Invalid attribute identifier '$name[$raw_attr]'")

return nothing
end
else
setup_error("Invalid attribute signature '$attr'")

return nothing
end

if !isnothing(raw_attr)
Expand Down
8 changes: 0 additions & 8 deletions src/library/setup/quote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,27 +236,19 @@ function __setup_quote_moi_attrs(spec::_SamplerSpec)
function MOI.set(sampler::$(Optimizer){T}, ::MOI.VariablePrimalStart, vi::VI, value) where {T}
if !(isnothing(value) || value isa Real)
error("Value for 'MOI.VariablePrimalStart' must be an integer, or 'nothing'")

return nothing
end

if !isnothing(value)
if !(value isa Real && isinteger(value))
error("Value for 'MOI.VariablePrimalStart' must be an integer, or 'nothing'")

return nothing
end

X = QUBOTools.domain(sampler)

if X === QUBOTools.BoolDomain && !(value == zero(value) || value == one(value))
error("Integer value for 'MOI.VariablePrimalStart' must be either '0' or '1'")

return nothing
elseif X === QUBOTools.SpinDomain && !(value == -one(value) || value == one(value))
error("Integer value for 'MOI.VariablePrimalStart' must be either '-1' or '1'")

return nothing
end
end

Expand Down

0 comments on commit 0754294

Please sign in to comment.