From 8667de5d6704fbe2365197b5cf50d651bf7c86c7 Mon Sep 17 00:00:00 2001 From: pedroripper Date: Mon, 24 Jun 2024 16:58:06 -0300 Subject: [PATCH] Allow setting MOI.ObjectiveSense --- src/library/sampler/wrappers/moi.jl | 2 + src/library/test/examples/basic.jl | 87 +++++++++++++++++++++++++++++ test/Project.toml | 2 +- 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/library/sampler/wrappers/moi.jl b/src/library/sampler/wrappers/moi.jl index 67cbd59..233c8db 100644 --- a/src/library/sampler/wrappers/moi.jl +++ b/src/library/sampler/wrappers/moi.jl @@ -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}}, diff --git a/src/library/test/examples/basic.jl b/src/library/test/examples/basic.jl index 708a24e..cd36007 100644 --- a/src/library/test/examples/basic.jl +++ b/src/library/test/examples/basic.jl @@ -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}, @@ -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 diff --git a/test/Project.toml b/test/Project.toml index bacfa8d..4865e68 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -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"