Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 12 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,
warn::Bool = true,
)

A problem modifier that, when passed to [`MOI.modify`](@ref), destructively
Expand Down Expand Up @@ -187,6 +188,9 @@

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
Expand Down Expand Up @@ -242,12 +246,14 @@
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)

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

View check run for this annotation

Codecov / codecov/patch

src/Utilities/penalty_relaxation.jl#L256

Added line #L256 was not covered by tests
end
end

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

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

View check run for this annotation

Codecov / codecov/patch

src/Utilities/penalty_relaxation.jl#L295-L296

Added lines #L295 - L296 were not covered by tests
"Skipping PenaltyRelaxation for ConstraintIndex{$F,$S}"
)
end
return
end
rethrow(err)
Expand Down
19 changes: 19 additions & 0 deletions test/Utilities/penalty_relaxation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
"""
Expand Down
Loading