Skip to content

Commit f68a7a0

Browse files
joaquimgodowblegat
authored
[Utilities] optionally disable warning in PenaltyRelaxation (#2774)
Co-authored-by: Oscar Dowson <odow@users.noreply.github.com> Co-authored-by: Benoît Legat <benoit.legat@gmail.com>
1 parent fa61f9c commit f68a7a0

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/Utilities/penalty_relaxation.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ end
142142
PenaltyRelaxation(
143143
penalties = Dict{MOI.ConstraintIndex,Float64}();
144144
default::Union{Nothing,T} = 1.0,
145+
warn::Bool = true,
145146
)
146147
147148
A problem modifier that, when passed to [`MOI.modify`](@ref), destructively
@@ -187,6 +188,9 @@ cannot be modified in-place.
187188
188189
To modify variable bounds, rewrite them as linear constraints.
189190
191+
If a constraint cannot be modified, a warning is logged and the
192+
constraint is skipped. The warning can be disabled by setting `warn = false`.
193+
190194
## Example
191195
192196
```jldoctest
@@ -242,12 +246,14 @@ true
242246
mutable struct PenaltyRelaxation{T}
243247
default::Union{Nothing,T}
244248
penalties::Dict{MOI.ConstraintIndex,T}
249+
warn::Bool
245250

246251
function PenaltyRelaxation(
247252
p::Dict{MOI.ConstraintIndex,T};
248253
default::Union{Nothing,T} = one(T),
254+
warn::Bool = true,
249255
) where {T}
250-
return new{T}(default, p)
256+
return new{T}(default, p, warn)
251257
end
252258
end
253259

@@ -286,7 +292,11 @@ function _modify_penalty_relaxation(
286292
map[ci] = MOI.modify(model, ci, ScalarPenaltyRelaxation(penalty))
287293
catch err
288294
if err isa MethodError && err.f == MOI.modify
289-
@warn("Skipping PenaltyRelaxation for ConstraintIndex{$F,$S}")
295+
if relax.warn
296+
@warn(
297+
"Skipping PenaltyRelaxation for ConstraintIndex{$F,$S}"
298+
)
299+
end
290300
return
291301
end
292302
rethrow(err)

test/Utilities/penalty_relaxation.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,25 @@ function test_relax_bounds()
6565
return
6666
end
6767

68+
function test_relax_no_warn()
69+
input = """
70+
variables: x, y
71+
minobjective: x + y
72+
x >= 0.0
73+
y <= 0.0
74+
x in ZeroOne()
75+
y in Integer()
76+
"""
77+
model = MOI.Utilities.Model{Float64}()
78+
MOI.Utilities.loadfromstring!(model, input)
79+
relaxation = MOI.Utilities.PenaltyRelaxation(; warn = false)
80+
@test_logs MOI.modify(model, relaxation)
81+
dest = MOI.Utilities.Model{Float64}()
82+
MOI.Utilities.loadfromstring!(dest, input)
83+
MOI.Bridges._test_structural_identical(model, dest)
84+
return
85+
end
86+
6887
function test_relax_affine_lessthan()
6988
_test_roundtrip(
7089
"""

0 commit comments

Comments
 (0)