Skip to content
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
14 changes: 13 additions & 1 deletion src/Bridges/Constraint/bridges/SemiToBinaryBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ function bridge_constraint(
f::MOI.VariableIndex,
s::S,
) where {T<:Real,S<:Union{MOI.Semicontinuous{T},MOI.Semiinteger{T}}}
if !isfinite(s.lower) || !isfinite(s.upper)
# We could be cleverer than this, but this is a simple check that should
# be sufficient for most cases. We can revisit if anyone complains.
msg = "Cannot add constraint $f-in-$s because the set has an open interval. To fix, make sure the lower and upper bounds are both finite."
throw(MOI.AddConstraintNotAllowed{MOI.VariableIndex,S}(msg))
end
binary, binary_con = MOI.add_constrained_variable(model, MOI.ZeroOne())
# var - LB * bin >= 0
lb = MOI.Utilities.operate(*, T, -s.lower, binary)
Expand Down Expand Up @@ -147,10 +153,16 @@ end

function MOI.set(
model::MOI.ModelLike,
::MOI.ConstraintSet,
attr::MOI.ConstraintSet,
bridge::SemiToBinaryBridge{T,S},
set::S,
) where {T,S}
if !isfinite(set.lower) || !isfinite(set.upper)
# We could be cleverer than this, but this is a simple check that should
# be sufficient for most cases. We can revisit if anyone complains.
msg = "Cannot modify the set $set because the set has an open interval. To fix, make sure the lower and upper bounds are both finite."
throw(MOI.SetAttributeNotAllowed(attr, msg))
end
bridge.semi_set = set
MOI.modify(
model,
Expand Down
30 changes: 30 additions & 0 deletions test/Bridges/Constraint/test_SemiToBinaryBridge.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,36 @@ function test_runtests()
return
end

function test_open_interval()
for set in Any[
MOI.Semicontinuous(1.0, Inf),
MOI.Semicontinuous(-1.0, Inf),
MOI.Semicontinuous(-Inf, 1.0),
MOI.Semicontinuous(-Inf, -1.0),
MOI.Semiinteger(1.0, Inf),
MOI.Semiinteger(-1.0, Inf),
MOI.Semiinteger(-Inf, 1.0),
MOI.Semiinteger(-Inf, -1.0),
]
model = MOI.Utilities.Model{Float64}()
bridged = MOI.Bridges.Constraint.SemiToBinary{Float64}(model)
x = MOI.add_variable(bridged)
@test_throws(
MOI.AddConstraintNotAllowed{MOI.VariableIndex,typeof(set)},
MOI.add_constraint(bridged, x, set)
)
model = MOI.Utilities.Model{Float64}()
bridged = MOI.Bridges.Constraint.SemiToBinary{Float64}(model)
x = MOI.add_variable(bridged)
c = MOI.add_constraint(bridged, x, typeof(set)(1.0, 2.0))
@test_throws(
MOI.SetAttributeNotAllowed,
MOI.set(bridged, MOI.ConstraintSet(), c, set),
)
end
return
end

end # module

TestConstraintSemiToBinary.runtests()
Loading