Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 13 additions & 2 deletions src/Utilities/penalty_relaxation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@
PenaltyRelaxation(
penalties = Dict{MOI.ConstraintIndex,Float64}();
default::Union{Nothing,T} = 1.0,
no_warning_skip_constraint::Bool = false,
)
A problem modifier that, when passed to [`MOI.modify`](@ref), destructively
Expand Down Expand Up @@ -187,6 +188,10 @@
To modify variable bounds, rewrite them as linear constraints.
If a constraint type can not be modified, a warning is logged and the
constraint is skipped. This can be disabled by setting
`no_warning_skip_constraint = true`.
## Example
```jldoctest
Expand Down Expand Up @@ -242,12 +247,14 @@
mutable struct PenaltyRelaxation{T}
default::Union{Nothing,T}
penalties::Dict{MOI.ConstraintIndex,T}
no_warning_skip_constraint::Bool

function PenaltyRelaxation(
p::Dict{MOI.ConstraintIndex,T};
default::Union{Nothing,T} = one(T),
no_warning_skip_constraint::Bool = false,
) where {T}
return new{T}(default, p)
return new{T}(default, p, no_warning_skip_constraint)

Check warning on line 257 in src/Utilities/penalty_relaxation.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/penalty_relaxation.jl#L257

Added line #L257 was not covered by tests
end
end

Expand Down Expand Up @@ -286,7 +293,11 @@
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.no_warning_skip_constraint
@warn(

Check warning on line 297 in src/Utilities/penalty_relaxation.jl

View check run for this annotation

Codecov / codecov/patch

src/Utilities/penalty_relaxation.jl#L296-L297

Added lines #L296 - L297 were not covered by tests
"Skipping PenaltyRelaxation for ConstraintIndex{$F,$S}"
)
end
return
end
rethrow(err)
Expand Down
31 changes: 31 additions & 0 deletions test/Utilities/penalty_relaxation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,37 @@ function test_relax_bounds()
return
end

function test_relax_no_warn()
src_str = """
variables: x, y
minobjective: x + y
x >= 0.0
y <= 0.0
x in ZeroOne()
y in Integer()
"""
relaxed_str = """
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, src_str)
@test_logs(
MOI.modify(
model,
MOI.Utilities.PenaltyRelaxation(no_warning_skip_constraint = true),
),
)
dest = MOI.Utilities.Model{Float64}()
MOI.Utilities.loadfromstring!(dest, relaxed_str)
MOI.Bridges._test_structural_identical(model, dest)
return
end

function test_relax_affine_lessthan()
_test_roundtrip(
"""
Expand Down
Loading