Skip to content

Commit a29fe7c

Browse files
committed
Optionally disable warn on PenaltyRelaxation
1 parent fa61f9c commit a29fe7c

File tree

2 files changed

+44
-2
lines changed

2 files changed

+44
-2
lines changed

src/Utilities/penalty_relaxation.jl

Lines changed: 13 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+
no_warning_skip_constraint::Bool = false,
145146
)
146147
147148
A problem modifier that, when passed to [`MOI.modify`](@ref), destructively
@@ -187,6 +188,10 @@ cannot be modified in-place.
187188
188189
To modify variable bounds, rewrite them as linear constraints.
189190
191+
If a constraint type can not be modified, a warning is logged and the
192+
constraint is skipped. This can be disabled by setting
193+
`no_warning_skip_constraint = true`.
194+
190195
## Example
191196
192197
```jldoctest
@@ -242,12 +247,14 @@ true
242247
mutable struct PenaltyRelaxation{T}
243248
default::Union{Nothing,T}
244249
penalties::Dict{MOI.ConstraintIndex,T}
250+
no_warning_skip_constraint::Bool
245251

246252
function PenaltyRelaxation(
247253
p::Dict{MOI.ConstraintIndex,T};
248254
default::Union{Nothing,T} = one(T),
255+
no_warning_skip_constraint::Bool = false,
249256
) where {T}
250-
return new{T}(default, p)
257+
return new{T}(default, p, no_warning_skip_constraint)
251258
end
252259
end
253260

@@ -286,7 +293,11 @@ function _modify_penalty_relaxation(
286293
map[ci] = MOI.modify(model, ci, ScalarPenaltyRelaxation(penalty))
287294
catch err
288295
if err isa MethodError && err.f == MOI.modify
289-
@warn("Skipping PenaltyRelaxation for ConstraintIndex{$F,$S}")
296+
if !relax.no_warning_skip_constraint
297+
@warn(
298+
"Skipping PenaltyRelaxation for ConstraintIndex{$F,$S}"
299+
)
300+
end
290301
return
291302
end
292303
rethrow(err)

test/Utilities/penalty_relaxation.jl

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

68+
function test_relax_no_warn()
69+
src_str = """
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+
relaxed_str = """
78+
variables: x, y
79+
minobjective: x + y
80+
x >= 0.0
81+
y <= 0.0
82+
x in ZeroOne()
83+
y in Integer()
84+
"""
85+
model = MOI.Utilities.Model{Float64}()
86+
MOI.Utilities.loadfromstring!(model, src_str)
87+
@test_logs(
88+
MOI.modify(
89+
model,
90+
MOI.Utilities.PenaltyRelaxation(no_warning_skip_constraint = true),
91+
),
92+
)
93+
dest = MOI.Utilities.Model{Float64}()
94+
MOI.Utilities.loadfromstring!(dest, relaxed_str)
95+
MOI.Bridges._test_structural_identical(model, dest)
96+
return
97+
end
98+
6899
function test_relax_affine_lessthan()
69100
_test_roundtrip(
70101
"""

0 commit comments

Comments
 (0)