Skip to content

Commit

Permalink
Add support for constant LHS in complements constraint (#3452)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Aug 16, 2023
1 parent 038be2e commit 8a856bb
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/complement.jl
Expand Up @@ -11,7 +11,7 @@

function _build_complements_constraint(
errorf::Function,
F::AbstractArray{<:AbstractJuMPScalar},
F::AbstractArray{<:Union{Real,AbstractJuMPScalar}},
x::AbstractArray{<:AbstractVariableRef},
)
if size(F) != size(x)
Expand All @@ -25,7 +25,7 @@ end

function _build_complements_constraint(
errorf::Function,
F::Containers.SparseAxisArray{<:AbstractJuMPScalar},
F::Containers.SparseAxisArray{<:Union{Real,AbstractJuMPScalar}},
x::Containers.SparseAxisArray{<:AbstractVariableRef},
)
elements = [F[i] for i in eachindex(F)]
Expand All @@ -41,23 +41,23 @@ end

function _build_complements_constraint(
errorf::Function,
::AbstractArray{<:AbstractJuMPScalar},
::AbstractArray{<:Union{Real,AbstractJuMPScalar}},
::AbstractArray{<:AbstractJuMPScalar},
)
return errorf("second term must be an array of variables.")
end

function _build_complements_constraint(
::Function,
F::AbstractJuMPScalar,
F::Union{Real,AbstractJuMPScalar},
x::AbstractVariableRef,
)
return VectorConstraint([F, x], MOI.Complements(2))
end

function _build_complements_constraint(
errorf::Function,
::AbstractJuMPScalar,
::Union{Real,AbstractJuMPScalar},
::AbstractJuMPScalar,
)
return errorf("second term must be a variable.")
Expand Down
48 changes: 48 additions & 0 deletions test/test_complement.jl
Expand Up @@ -57,6 +57,19 @@ function test_scalar_error_F_F()
return
end

function test_scalar_error_0_F()
model = Model()
@variable(model, x >= 0)
@test_throws_strip(
ErrorException(
"In `@constraint(model, 0 ⟂ 2x - 1)`: second term must be a " *
"variable.",
),
@constraint(model, 0 2x - 1)
)
return
end

function test_vector_complements()
model = Model()
@variable(model, x[1:2] >= 0)
Expand Down Expand Up @@ -116,6 +129,20 @@ function test_vector_error_F_F()
return
end

function test_vector_error_0_F()
model = Model()
@variable(model, x[1:2] >= 0)
y = [1.2, -1.3]
@test_throws_strip(
ErrorException(
"In `@constraint(model, y ⟂ 2x .- 1)`: second term must " *
"be an array of variables.",
),
@constraint(model, y 2x .- 1)
)
return
end

function test_sparse_complements()
model = Model()
@variable(model, x[i = 1:3; isodd(i)] >= 0)
Expand Down Expand Up @@ -146,4 +173,25 @@ function test_sparse_key_mismatch()
return
end

function test_F_constant_scalar()
model = Model()
@variable(model, 0 <= x <= 1)
@constraint(model, c, 0 x)
obj = constraint_object(c)
@test obj.func == AffExpr[0, x]
@test obj.set == MOI.Complements(2)
return
end

function test_F_constant_vector()
model = Model()
@variable(model, 0 <= x[1:2] <= 1)
F = [1.2, -1.3]
@constraint(model, c, F x)
obj = constraint_object(c)
@test obj.func == [F; x]
@test obj.set == MOI.Complements(4)
return
end

end # module

0 comments on commit 8a856bb

Please sign in to comment.