Skip to content

Commit

Permalink
Merge pull request #12 from psrenergy/pr/objective-sense
Browse files Browse the repository at this point in the history
Allow setting MOI.ObjectiveSense
  • Loading branch information
pedroripper authored Jul 2, 2024
2 parents 973a41c + 8667de5 commit f8f29b6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
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"

0 comments on commit f8f29b6

Please sign in to comment.