Skip to content

Commit def5945

Browse files
committed
Warn on unsupported types
1 parent 3fb87d8 commit def5945

File tree

2 files changed

+38
-23
lines changed

2 files changed

+38
-23
lines changed

src/Utilities/penalty_relaxation.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,7 @@ function PenaltyRelaxation(
115115
return PenaltyRelaxation(convert(Dict{MOI.ConstraintIndex,T}, d); kwargs...)
116116
end
117117

118-
function MOI.modify(
119-
model::MOI.ModelLike,
120-
relax::PenaltyRelaxation{T},
121-
) where {T}
118+
function MOI.modify(model::MOI.ModelLike, relax::PenaltyRelaxation{T}) where {T}
122119
sense = MOI.get(model, MOI.ObjectiveSense())
123120
if sense == MOI.FEASIBILITY_SENSE
124121
MOI.set(model, MOI.ObjectiveSense(), MOI.MIN_SENSE)
@@ -147,10 +144,18 @@ end
147144

148145
function MOI.modify(
149146
::MOI.ModelLike,
150-
::MOI.ConstraintIndex,
147+
ci::MOI.ConstraintIndex,
151148
::PenaltyRelaxation,
152149
)
153-
return # Silently skip modifying other constraint types.
150+
# We use this fallback to avoid ambiguity errors that would occur if we
151+
# added {F,S} directly to the argument.
152+
_eltype(::MOI.ConstraintIndex{F,S}) where {F,S} = F, S
153+
F, S = _eltype(ci)
154+
@warn(
155+
"Skipping PenaltyRelaxation of constraints of type $F-in-$S",
156+
maxlog = 1,
157+
)
158+
return
154159
end
155160

156161
function MOI.modify(

test/Utilities/penalty_relaxation.jl

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,34 @@ function _test_roundtrip(src_str, relaxed_str)
3333
end
3434

3535
function test_relax_bounds()
36-
_test_roundtrip(
37-
"""
38-
variables: x, y
39-
minobjective: x + y
40-
x >= 0.0
41-
y <= 0.0
42-
x in ZeroOne()
43-
y in Integer()
44-
""",
45-
"""
46-
variables: x, y
47-
minobjective: x + y
48-
x >= 0.0
49-
y <= 0.0
50-
x in ZeroOne()
51-
y in Integer()
52-
""",
36+
src_str = """
37+
variables: x, y
38+
minobjective: x + y
39+
x >= 0.0
40+
y <= 0.0
41+
x in ZeroOne()
42+
y in Integer()
43+
"""
44+
relaxed_str = """
45+
variables: x, y
46+
minobjective: x + y
47+
x >= 0.0
48+
y <= 0.0
49+
x in ZeroOne()
50+
y in Integer()
51+
"""
52+
model = MOI.Utilities.Model{Float64}()
53+
MOI.Utilities.loadfromstring!(model, src_str)
54+
@test_logs(
55+
(:warn,),
56+
(:warn,),
57+
(:warn,),
58+
(:warn,),
59+
MOI.modify(model, MOI.Utilities.PenaltyRelaxation()),
5360
)
61+
dest = MOI.Utilities.Model{Float64}()
62+
MOI.Utilities.loadfromstring!(dest, relaxed_str)
63+
MOI.Bridges._test_structural_identical(model, dest)
5464
return
5565
end
5666

0 commit comments

Comments
 (0)