Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pedromxavier committed Nov 20, 2023
1 parent 503d48d commit 4c6b531
Showing 1 changed file with 81 additions and 31 deletions.
112 changes: 81 additions & 31 deletions test/integration/examples/continuous/continuous_2.jl
Original file line number Diff line number Diff line change
@@ -1,50 +1,100 @@
"""
(x11 + x12 + x21 + x22 + x31 + x32 - 4 - s1 - s2)^2
x11*x11 + x11*x12 + x11*x21 + x11*x22 + x11*x31 + x11*x32 - 4*x11 - s1*x11 - s2*x11 +
x12*x11 + x12*x12 + x12*x21 + x12*x22 + x12*x31 + x12*x32 - 4*x12 - s1*x12 - s2*x12 +
x21*x11 + x21*x12 + x21*x21 + x21*x22 + x21*x31 + x21*x32 - 4*x21 - s1*x21 - s2*x21 +
x22*x11 + x22*x12 + x22*x21 + x22*x22 + x22*x31 + x22*x32 - 4*x22 - s1*x22 - s2*x22 +
x31*x11 + x31*x12 + x31*x21 + x31*x22 + x31*x31 + x31*x32 - 4*x31 - s1*x31 - s2*x31 +
x32*x11 + x32*x12 + x32*x21 + x32*x22 + x32*x31 + x32*x32 - 4*x32 - s1*x32 - s2*x32 +
-4*x11 - 4*x12 - 4*x21 - 4*x22 - 4*x31 - 4*x32 + 16 + 4*s1 + 4*s2 +
-s1*x11 - s1*x12 - s1*x21 - s1*x22 - s1*x31 - s1*x32 + 4*s1 + s1*s1 + s1*s2 +
-s2*x11 - s2*x12 - s2*x21 - s2*x22 - s2*x31 - s2*x32 + 4*s2 + s1*s2 + s2*s2
min f(x) = x₁ + 2x₂ + 3x₃
s.t. x₁ + x₂ + x₃ ≥ 4
x₁, x₂, x₃ ∈ [0, 2] ⊂ ℤ
QUBO formulation:
x₁ ↤ x₁,₁ + x₁,₂
x₂ ↤ x₂,₁ + x₂,₂
x₃ ↤ x₃,₁ + x₃,₂
min f(x) = x₁,₁ + x₁,₂ + 2 (x₂,₁ + x₂,₂) + 3 (x₃,₁ + x₃,₂)
s.t. x₁,₁ + x₁,₂ + x₂,₁ + x₂,₂ + x₃,₁ + x₃,₂ ≥ 4
x₁,₁, x₁,₂, x₂,₁, x₂,₂, x₃,₁, x₃,₂ ∈ 𝔹
Adding a slack variable s ∈ [0, 2]:
min f(x) = x₁,₁ + x₁,₂ + 2 (x₂,₁ + x₂,₂) + 3 (x₃,₁ + x₃,₂)
s.t. x₁,₁ + x₁,₂ + x₂,₁ + x₂,₂ + x₃,₁ + x₃,₂ - s - 4 = 0
x₁,₁, x₁,₂, x₂,₁, x₂,₂, x₃,₁, x₃,₂ ∈ 𝔹
s ∈ [0, 2] ⊂ ℤ
Encoding s using binary variables:
min f(x) = x₁,₁ + x₁,₂ + 2 (x₂,₁ + x₂,₂) + 3 (x₃,₁ + x₃,₂)
s.t. x₁,₁ + x₁,₂ + x₂,₁ + x₂,₂ + x₃,₁ + x₃,₂ - 4 - s₁ - 2 s₂ = 0
x₁,₁, x₁,₂, x₂,₁, x₂,₂, x₃,₁, x₃,₂, s₁, s₂ ∈ 𝔹
Moving the constraint to the objective as a penalty:
min f(x) = x₁,₁ + x₁,₂ + 2 (x₂,₁ + x₂,₂) + 3 (x₃,₁ + x₃,₂) + ρ (x₁,₁ + x₁,₂ + x₂,₁ + x₂,₂ + x₃,₁ + x₃,₂ - 4 - s₁ - 2 s₂)²
s.t. x₁,₁, x₁,₂, x₂,₁, x₂,₂, x₃,₁, x₃,₂, s₁, s₂ ∈ 𝔹
(x₁,₁ + x₁,₂ + x₂,₁ + x₂,₂ + x₃,₁ + x₃,₂ - 4 - s₁ - s₂)^2 =
- 7 x₁,₁ - 7 x₁,₂ - 7 x₂,₁ - 7 x₂,₂ - 7 x₃,₁ - 7 x₃,₂ + 9 s₁ + 9 s₂
x₁,₁ x₁,₂ + x₁,₁ x₂,₁ + x₁,₁ x₂,₂ + x₁,₁ x₃,₁ + x₁,₁ x₃,₂ - 2 s₁ x₁,₁ - 2 s₂ x₁,₁ +
x₁,₂ x₁,₁ + x₁,₂ x₂,₁ + x₁,₂ x₂,₂ + x₁,₂ x₃,₁ + x₁,₂ x₃,₂ - 2 s₁ x₁,₂ - 2 s₂ x₁,₂ +
x₂,₁ x₁,₁ + x₂,₁ x₁,₂ + x₂,₁ x₂,₂ + x₂,₁ x₃,₁ + x₂,₁ x₃,₂ - 2 s₁ x₂,₁ - 2 s₂ x₂,₁ +
x₂,₂ x₁,₁ + x₂,₂ x₁,₂ + x₂,₂ x₂,₁ + x₂,₂ x₃,₁ + x₂,₂ x₃,₂ - 2 s₁ x₂,₂ - 2 s₂ x₂,₂ +
x₃,₁ x₁,₁ + x₃,₁ x₁,₂ + x₃,₁ x₂,₁ + x₃,₁ x₂,₂ + x₃,₁ x₃,₂ - 2 s₁ x₃,₁ - 2 s₂ x₃,₁ +
x₃,₂ x₁,₁ + x₃,₂ x₁,₂ + x₃,₂ x₂,₁ + x₃,₂ x₂,₂ + x₃,₂ x₃,₁ - 2 s₁ x₃,₂ - 2 s₂ x₃,₂ +
+ 2 s₁ s₂ + 16
"""
function test_continuous_2()
@testset "Greater than constraint penalty hint" begin

ρ = 3.0

Q = [
1-7ρ 2ρ 2ρ 2ρ 2ρ 2ρ -2ρ -2ρ
0 1-7ρ 2ρ 2ρ 2ρ 2ρ -2ρ -2ρ
0 0 1-7ρ 2ρ 2ρ 2ρ -2ρ -2ρ
0 0 0 1-7ρ 2ρ 2ρ -2ρ -2ρ
0 0 0 0 1-7ρ 2ρ -2ρ -2ρ
0 0 0 0 0 1-7ρ -2ρ -2ρ
0 0 0 0 0 0 9ρ 0
0 0 0 0 0 0 0 9ρ
@testset "Greater than constraint penalty hint" begin
ρ̄ = 3.0
ᾱ = 1.0
β̄ = 16ρ̄

= [
1 0 0 0 0 0 0 0
0 1 0 0 0 0 0 0
0 0 2 0 0 0 0 0
0 0 0 2 0 0 0 0
0 0 0 0 3 0 0 0
0 0 0 0 0 3 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
]

= [
-7 2 2 2 2 2 -2 -2
0 -7 2 2 2 2 -2 -2
0 0 -7 2 2 2 -2 -2
0 0 0 -7 2 2 -2 -2
0 0 0 0 -7 2 -2 -2
0 0 0 0 0 -7 -2 -2
0 0 0 0 0 0 9 2
0 0 0 0 0 0 0 9
]

=+ ρ̄ *

= [2.0, 2.0, 0.0]
= 6

model = Model(() -> ToQUBO.Optimizer(ExactSampler.Optimizer))

@variable(model, 0 <= x[1:3] <= 2, Int)
@constraint(model, c, sum(x) >= 4)
@objective(model, Min, sum(x))
@objective(model, Min, sum(i * x[i] for i = 1:3))

set_attribute(c, ToQUBO.Attributes.ConstraintEncodingPenaltyHint(), ρ̄)

JuMP.set_attribute(c, ToQUBO.Attributes.ConstraintEncodingPenaltyHint(), 3.0)


optimize!(model)


n, L, Q, α, β = QUBOTools.qubo(model, :dense)

ρ = get_attribute(c, ToQUBO.Attributes.ConstraintEncodingPenalty())

= Q + diagm(L)

@test n == 8
@test ρ ρ̄
@test α ᾱ
@test β β̄
@test
Expand All @@ -55,7 +105,7 @@ function test_continuous_2()

@test
@test

end
return
end

return nothing
end

0 comments on commit 4c6b531

Please sign in to comment.