diff --git a/src/Utilities/penalty_relaxation.jl b/src/Utilities/penalty_relaxation.jl index 3521cc3cce..ed414225fb 100644 --- a/src/Utilities/penalty_relaxation.jl +++ b/src/Utilities/penalty_relaxation.jl @@ -142,6 +142,7 @@ end PenaltyRelaxation( penalties = Dict{MOI.ConstraintIndex,Float64}(); default::Union{Nothing,T} = 1.0, + warn::Bool = true, ) A problem modifier that, when passed to [`MOI.modify`](@ref), destructively @@ -187,6 +188,9 @@ cannot be modified in-place. To modify variable bounds, rewrite them as linear constraints. +If a constraint cannot be modified, a warning is logged and the +constraint is skipped. The warning can be disabled by setting `warn = false`. + ## Example ```jldoctest @@ -242,12 +246,14 @@ true mutable struct PenaltyRelaxation{T} default::Union{Nothing,T} penalties::Dict{MOI.ConstraintIndex,T} + warn::Bool function PenaltyRelaxation( p::Dict{MOI.ConstraintIndex,T}; default::Union{Nothing,T} = one(T), + warn::Bool = true, ) where {T} - return new{T}(default, p) + return new{T}(default, p, warn) end end @@ -286,7 +292,11 @@ function _modify_penalty_relaxation( map[ci] = MOI.modify(model, ci, ScalarPenaltyRelaxation(penalty)) catch err if err isa MethodError && err.f == MOI.modify - @warn("Skipping PenaltyRelaxation for ConstraintIndex{$F,$S}") + if relax.warn + @warn( + "Skipping PenaltyRelaxation for ConstraintIndex{$F,$S}" + ) + end return end rethrow(err) diff --git a/test/Utilities/penalty_relaxation.jl b/test/Utilities/penalty_relaxation.jl index 226bf8a623..ae6c839f4b 100644 --- a/test/Utilities/penalty_relaxation.jl +++ b/test/Utilities/penalty_relaxation.jl @@ -65,6 +65,25 @@ function test_relax_bounds() return end +function test_relax_no_warn() + input = """ + variables: x, y + minobjective: x + y + x >= 0.0 + y <= 0.0 + x in ZeroOne() + y in Integer() + """ + model = MOI.Utilities.Model{Float64}() + MOI.Utilities.loadfromstring!(model, input) + relaxation = MOI.Utilities.PenaltyRelaxation(; warn = false) + @test_logs MOI.modify(model, relaxation) + dest = MOI.Utilities.Model{Float64}() + MOI.Utilities.loadfromstring!(dest, input) + MOI.Bridges._test_structural_identical(model, dest) + return +end + function test_relax_affine_lessthan() _test_roundtrip( """