Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting MOI.ObjectiveSense #12

Merged
merged 1 commit into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/library/sampler/wrappers/moi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ MOI.supports_constraint(::AbstractSampler, ::Type{VI}, ::Type{Spin}) = true
# ~ Objective Function Support
MOI.supports(::AbstractSampler, ::MOI.ObjectiveFunction{<:Any}) = false

MOI.supports(::AbstractSampler, ::MOI.ObjectiveSense) = true

MOI.supports(
::AbstractSampler{T},
::MOI.ObjectiveFunction{<:Union{SQF{T},SAF{T},VI}},
Expand Down
87 changes: 87 additions & 0 deletions src/library/test/examples/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,92 @@ function _test_basic_spin_max(
return nothing
end


function _test_basic_spin_min_and_max(
config!::Function,
sampler::Type{S},
n::Integer,
h::Vector{T},
J::Matrix{T},
) where {T,S<:AbstractSampler{T}}
Test.@testset "▷ Spin ⋄ Min and Max" begin
# Build Model
model = MOI.instantiate(sampler; with_bridge_type = T)

s, _ = MOI.add_constrained_variables(model, fill(Spin(), n))

MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
MOI.set(
model,
MOI.ObjectiveFunction{SQF{T}}(),
SQF{T}(
SQT{T}[SQT{T}(J[i, j], s[i], s[j]) for i = 1:n for j = 1:n],
SAT{T}[SAT{T}(h[i], s[i]) for i = 1:n],
zero(T),
),
)

config!(model)

MOI.optimize!(model)

Test.@test MOI.get(model, MOI.ResultCount()) > 0

for i = 1:MOI.get(model, MOI.ResultCount())
si = MOI.get.(model, MOI.VariablePrimal(i), s)
Hi = MOI.get(model, MOI.ObjectiveValue(i))

if si ≈ [↑, ↑, ↓] || si ≈ [↑, ↓, ↑] || si ≈ [↓, ↑, ↑]
Test.@test Hi ≈ -5.0
elseif si ≈ [↓, ↓, ↑] || si ≈ [↓, ↑, ↓] || si ≈ [↑, ↓, ↓]
Test.@test Hi ≈ -3.0
elseif si ≈ [↑, ↑, ↑]
Test.@test Hi ≈ 9.0
elseif si ≈ [↓, ↓, ↓]
Test.@test Hi ≈ 15.0
else
Test.@test false
end
end

# Change ObjectiveSense to Max
MOI.set(model, MOI.ObjectiveSense(), MOI.MAX_SENSE)
MOI.set(
model,
MOI.ObjectiveFunction{SQF{T}}(),
SQF{T}(
SQT{T}[SQT{T}(J[i, j], s[i], s[j]) for i = 1:n for j = 1:n],
SAT{T}[SAT{T}(h[i], s[i]) for i = 1:n],
zero(T),
),
)

config!(model)

MOI.optimize!(model)

Test.@test MOI.get(model, MOI.ResultCount()) > 0

for i = 1:MOI.get(model, MOI.ResultCount())
si = MOI.get.(model, MOI.VariablePrimal(i), s)
Hi = MOI.get(model, MOI.ObjectiveValue(i))

if si ≈ [↓, ↓, ↓]
Test.@test Hi ≈ 15.0
elseif si ≈ [↑, ↑, ↑]
Test.@test Hi ≈ 9.0
elseif si ≈ [↓, ↓, ↑] || si ≈ [↓, ↑, ↓] || si ≈ [↑, ↓, ↓]
Test.@test Hi ≈ -3.0
elseif si ≈ [↑, ↑, ↓] || si ≈ [↑, ↓, ↑] || si ≈ [↓, ↑, ↑]
Test.@test Hi ≈ -5.0
else
Test.@test false
end
end
end
return nothing
end

function _test_basic_examples(
config!::Function,
sampler::Type{S},
Expand All @@ -217,6 +303,7 @@ function _test_basic_examples(
_test_basic_bool_max(config!, sampler, n, Q)
_test_basic_spin_min(config!, sampler, n, h, J)
_test_basic_spin_max(config!, sampler, n, h, J)
_test_basic_spin_min_and_max(config!, sampler, n, h, J)
end

return nothing
Expand Down
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ PythonCall = "6099a3de-0909-46bc-b1f4-468b9a2dfc0d"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
PythonCall = "0.9.14"
PythonCall = "0.9.20"
Loading