From a6a64c693610668811ef5b8025c98206786532c3 Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 15 Feb 2021 10:50:42 +1300 Subject: [PATCH 1/3] [Utilities] add a default for shift_constant --- src/Utilities/sets.jl | 21 +++++++++++++++++++-- test/Utilities/sets.jl | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Utilities/sets.jl b/src/Utilities/sets.jl index 23418d201e..887b4a7070 100644 --- a/src/Utilities/sets.jl +++ b/src/Utilities/sets.jl @@ -1,15 +1,32 @@ """ - shift_constant(set::MOI.AbstractScalarSet, - offset) + shift_constant(set::MOI.AbstractScalarSet, offset) Returns a new scalar set `new_set` such that `func`-in-`set` is equivalent to `func + offset`-in-`new_set`. +Only define this function if it makes sense to! + +By default, this function returns `nothing`, which can be used as a flag to +check if the set supports shifting: +```Julia +new_set = shift_constant(old_set, offset) +if new_set === nothing + add_constraint(model, f, old_set) +else + f.constant = 0 + add_constraint(model, f, new_set) +end +``` + ## Examples The call `shift_constant(MOI.Interval(-2, 3), 1)` is equal to `MOI.Interval(-1, 4)`. """ +function shift_constant(::MOI.AbstractScalarSet, ::Any) + return nothing +end + function shift_constant( set::Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}}, offset::T, diff --git a/test/Utilities/sets.jl b/test/Utilities/sets.jl index b6d633f72e..b21dfdd988 100644 --- a/test/Utilities/sets.jl +++ b/test/Utilities/sets.jl @@ -37,6 +37,7 @@ end @test MOIU.shift_constant(MOI.GreaterThan(6), -1) == MOI.GreaterThan(5) @test MOIU.shift_constant(MOI.LessThan(2), 2) == MOI.LessThan(4) @test MOIU.shift_constant(MOI.Interval(-2, 3), 1) == MOI.Interval(-1, 4) + @test MOIU.shift_constant(MOI.ZeroOne(), 0.0) === nothing end @testset "Dimension" begin From 2582d5e0cb6fbbb0cfbd232fdc8ae6685a8471c8 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 2 Mar 2021 17:23:59 +1300 Subject: [PATCH 2/3] Add supports_shift_constant --- src/Utilities/sets.jl | 31 ++++++++++++++++++++++--------- test/Utilities/sets.jl | 7 ++++++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/src/Utilities/sets.jl b/src/Utilities/sets.jl index 887b4a7070..167b980922 100644 --- a/src/Utilities/sets.jl +++ b/src/Utilities/sets.jl @@ -6,26 +6,34 @@ Returns a new scalar set `new_set` such that `func`-in-`set` is equivalent to Only define this function if it makes sense to! -By default, this function returns `nothing`, which can be used as a flag to -check if the set supports shifting: +Use [`supports_shift_constant`](@ref) to check if the set supports shifting: ```Julia -new_set = shift_constant(old_set, offset) -if new_set === nothing - add_constraint(model, f, old_set) -else +if supports_shift_constant(typeof(old_set)) + new_set = shift_constant(old_set, offset) f.constant = 0 add_constraint(model, f, new_set) +else + add_constraint(model, f, old_set) end ``` +See also [`supports_shift_constant`](@ref). + ## Examples The call `shift_constant(MOI.Interval(-2, 3), 1)` is equal to `MOI.Interval(-1, 4)`. """ -function shift_constant(::MOI.AbstractScalarSet, ::Any) - return nothing -end +function shift_constant end + +""" + supports_shift_constant(::Type{S}) where {S<:MOI.AbstractSet} + +Return `true` if [`shift_constant`](@ref) is defined for set `S`. + +See also [`shift_constant`](@ref). +""" +supports_shift_constant(::Type{S}) where {S<:MOI.AbstractSet} = false function shift_constant( set::Union{MOI.LessThan{T},MOI.GreaterThan{T},MOI.EqualTo{T}}, @@ -33,9 +41,14 @@ function shift_constant( ) where {T} return typeof(set)(MOI.constant(set) + offset) end +supports_shift_constant(::Type{<:MOI.LessThan}) = true +supports_shift_constant(::Type{<:MOI.GreaterThan}) = true +supports_shift_constant(::Type{<:MOI.EqualTo}) = true + function shift_constant(set::MOI.Interval, offset) return MOI.Interval(set.lower + offset, set.upper + offset) end +supports_shift_constant(::Type{<:MOI.Interval}) = true const ScalarLinearSet{T} = Union{MOI.EqualTo{T},MOI.LessThan{T},MOI.GreaterThan{T}} diff --git a/test/Utilities/sets.jl b/test/Utilities/sets.jl index b21dfdd988..3df517ce8a 100644 --- a/test/Utilities/sets.jl +++ b/test/Utilities/sets.jl @@ -33,11 +33,16 @@ end end @testset "Shifts" begin + @test MOIU.supports_shift_constant(MOI.EqualTo{Int}) @test MOIU.shift_constant(MOI.EqualTo(3), 1) == MOI.EqualTo(4) + @test MOIU.supports_shift_constant(MOI.GreaterThan{Int}) @test MOIU.shift_constant(MOI.GreaterThan(6), -1) == MOI.GreaterThan(5) + @test MOIU.supports_shift_constant(MOI.LessThan{Int}) @test MOIU.shift_constant(MOI.LessThan(2), 2) == MOI.LessThan(4) + @test MOIU.supports_shift_constant(MOI.Interval{Int}) @test MOIU.shift_constant(MOI.Interval(-2, 3), 1) == MOI.Interval(-1, 4) - @test MOIU.shift_constant(MOI.ZeroOne(), 0.0) === nothing + @test MOIU.supports_shift_constant(MOI.ZeroOne) == false + @test_throws MethodError MOIU.shift_constant(MOI.ZeroOne(), 1.0) end @testset "Dimension" begin From 21a0bd53f0450ab05cb28fe32ca98080dbcec021 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 2 Mar 2021 19:52:34 +1300 Subject: [PATCH 3/3] Add doc --- docs/src/apireference.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/src/apireference.md b/docs/src/apireference.md index b56cf5c5b0..97b47e9ef5 100644 --- a/docs/src/apireference.md +++ b/docs/src/apireference.md @@ -1073,6 +1073,7 @@ The following utilities are available for moving the function constant to the set for scalar constraints: ```@docs Utilities.shift_constant +Utilities.supports_shift_constant Utilities.normalize_and_add_constraint Utilities.normalize_constant ```