From 4893d57d90733fe54e744897ab2766d842b1f511 Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 29 May 2023 16:41:39 +1200 Subject: [PATCH 1/9] Fix promote_operation for ScalarNonlinearFunction --- src/Bridges/Constraint/bridges/vectorize.jl | 11 +++++++++-- src/Utilities/functions.jl | 8 ++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Bridges/Constraint/bridges/vectorize.jl b/src/Bridges/Constraint/bridges/vectorize.jl index cbf572759a..e103cfa0f8 100644 --- a/src/Bridges/Constraint/bridges/vectorize.jl +++ b/src/Bridges/Constraint/bridges/vectorize.jl @@ -58,9 +58,16 @@ end function MOI.supports_constraint( ::Type{VectorizeBridge{T}}, - ::Type{<:MOI.AbstractScalarFunction}, + ::Type{F}, ::Type{<:MOI.Utilities.ScalarLinearSet{T}}, -) where {T} +) where { + T, + F<:Union{ + MOI.VariableIndex, + MOI.ScalarAffineFunction{T}, + MOI.ScalarQuadraticFunction{T}, + }, +} return true end diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 4946acdf67..35ceb2069f 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1816,6 +1816,14 @@ end ### ScalarNonlinearFunction +function promote_operation( + ::typeof(-), + ::Type{T}, + ::Type{MOI.ScalarNonlinearFunction}, +) where {T} + return MOI.ScalarNonlinearFunction +end + function promote_operation( ::typeof(-), ::Type{T}, From 35f537e0e078ef31e643acb9468920f061930c6e Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 30 May 2023 08:08:54 +1200 Subject: [PATCH 2/9] Skip vectorize test for SNF --- src/Bridges/Constraint/bridges/vectorize.jl | 19 ++++++++++--------- test/Bridges/Constraint/vectorize.jl | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Bridges/Constraint/bridges/vectorize.jl b/src/Bridges/Constraint/bridges/vectorize.jl index e103cfa0f8..9f60285e28 100644 --- a/src/Bridges/Constraint/bridges/vectorize.jl +++ b/src/Bridges/Constraint/bridges/vectorize.jl @@ -58,19 +58,20 @@ end function MOI.supports_constraint( ::Type{VectorizeBridge{T}}, - ::Type{F}, + ::Type{<:MOI.AbstractScalarFunction}, ::Type{<:MOI.Utilities.ScalarLinearSet{T}}, -) where { - T, - F<:Union{ - MOI.VariableIndex, - MOI.ScalarAffineFunction{T}, - MOI.ScalarQuadraticFunction{T}, - }, -} +) where {T} return true end +function MOI.supports_constraint( + ::Type{VectorizeBridge{T}}, + ::Type{MOI.ScalarNonlinearFunction}, + ::Type{<:MOI.Utilities.ScalarLinearSet{T}}, +) where {T} + return false +end + function MOI.Bridges.added_constrained_variable_types(::Type{<:VectorizeBridge}) return Tuple{Type}[] end diff --git a/test/Bridges/Constraint/vectorize.jl b/test/Bridges/Constraint/vectorize.jl index 79c17546d9..f1ec9975be 100644 --- a/test/Bridges/Constraint/vectorize.jl +++ b/test/Bridges/Constraint/vectorize.jl @@ -238,6 +238,27 @@ function test_runtests() return end +MOI.Utilities.@model( + Model2179, + (), + (MOI.GreaterThan, MOI.LessThan), + (), + (), + (), + (MOI.ScalarAffineFunction,), + (), + () +) + +function test_unsupported_ScalarNonnlinearFunction() + model = MOI.instantiate(Model2179{Float64}; with_bridge_type = Float64) + MOI.supports_constraint( + model, + MOI.ScalarNonlinearFunction, + MOI.GreaterThan{Float64}, + ) +end + end # module TestConstraintVectorize.runtests() From 89a69a817bee1e12e4afda3db14c66d8e93bcb97 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 30 May 2023 09:18:52 +1200 Subject: [PATCH 3/9] More fixes --- src/Utilities/functions.jl | 18 +++++++++++++++--- test/Bridges/Constraint/flip_sign.jl | 22 ++++++++++++++++++++++ test/Bridges/Constraint/vectorize.jl | 3 ++- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 35ceb2069f..650359d724 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1816,11 +1816,12 @@ end ### ScalarNonlinearFunction +# Needed for LessToGreaterBridge function promote_operation( - ::typeof(-), - ::Type{T}, + ::Union{typeof(+),typeof(-)}, + ::Type{<:Real}, ::Type{MOI.ScalarNonlinearFunction}, -) where {T} +) return MOI.ScalarNonlinearFunction end @@ -1851,6 +1852,17 @@ function operate( return MOI.ScalarNonlinearFunction(Symbol(op), Any[f, g]) end +function operate( + ::typeof(-), + ::Type{T}, + f::MOI.ScalarNonlinearFunction, +) where {T} + if f.head == :- && length(f.args) == 1 && f.args[1] isa MOI.ScalarNonlinearFunction + return f.args[1] + end + return MOI.ScalarNonlinearFunction(:-, Any[f]) +end + ### Base methods _eltype(args::Tuple) = _eltype(first(args), Base.tail(args)) diff --git a/test/Bridges/Constraint/flip_sign.jl b/test/Bridges/Constraint/flip_sign.jl index 167c594f1f..9aaaa18cf2 100644 --- a/test/Bridges/Constraint/flip_sign.jl +++ b/test/Bridges/Constraint/flip_sign.jl @@ -337,6 +337,17 @@ function test_runtests() -1.5 * x <= -1.0 """, ) + MOI.Bridges.runtests( + MOI.Bridges.Constraint.GreaterToLessBridge, + """ + variables: x + ScalarNonlinearFunction(1.5 * x) >= 1.0 + """, + """ + variables: x + ScalarNonlinearFunction(-(1.5 * x)) <= -1.0 + """, + ) MOI.Bridges.runtests( MOI.Bridges.Constraint.LessToGreaterBridge, """ @@ -359,6 +370,17 @@ function test_runtests() -1.5 * x >= -1.0 """, ) + MOI.Bridges.runtests( + MOI.Bridges.Constraint.LessToGreaterBridge, + """ + variables: x + ScalarNonlinearFunction(1.5 * x) <= 1.0 + """, + """ + variables: x + ScalarNonlinearFunction(-(1.5 * x)) >= -1.0 + """, + ) MOI.Bridges.runtests( MOI.Bridges.Constraint.NonnegToNonposBridge, """ diff --git a/test/Bridges/Constraint/vectorize.jl b/test/Bridges/Constraint/vectorize.jl index f1ec9975be..e51e6cb398 100644 --- a/test/Bridges/Constraint/vectorize.jl +++ b/test/Bridges/Constraint/vectorize.jl @@ -250,13 +250,14 @@ MOI.Utilities.@model( () ) -function test_unsupported_ScalarNonnlinearFunction() +function test_unsupported_ScalarNonlinearFunction() model = MOI.instantiate(Model2179{Float64}; with_bridge_type = Float64) MOI.supports_constraint( model, MOI.ScalarNonlinearFunction, MOI.GreaterThan{Float64}, ) + return end end # module From 2e5e8798030cdec715317e5b4a25564a7bd58e05 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 30 May 2023 09:42:08 +1200 Subject: [PATCH 4/9] Fix formattinng --- src/Utilities/functions.jl | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 650359d724..e43c0f4c63 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1857,8 +1857,11 @@ function operate( ::Type{T}, f::MOI.ScalarNonlinearFunction, ) where {T} - if f.head == :- && length(f.args) == 1 && f.args[1] isa MOI.ScalarNonlinearFunction - return f.args[1] + if f.head == :- && length(f.args) == 1 + # A simplification for -(-(f)) into f, but only if f is an SNF. + if f.args[1] isa MOI.ScalarNonlinearFunction + return f.args[1] + end end return MOI.ScalarNonlinearFunction(:-, Any[f]) end From f66e799122275d7750237b225528dd190b00aac1 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 30 May 2023 14:29:58 +1200 Subject: [PATCH 5/9] Update ci.yml --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4daf7128c9..c70adae519 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: - version: '1.6' os: ubuntu-latest arch: x64 - - version: '1.6' + - version: '1' os: ubuntu-latest arch: x86 steps: From 44f019e6ec28500a05ef394c5a0db0b5ed08d1d1 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Tue, 30 May 2023 21:10:23 +1200 Subject: [PATCH 6/9] Update functions.jl --- src/Utilities/functions.jl | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index e43c0f4c63..d878671e87 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1816,13 +1816,14 @@ end ### ScalarNonlinearFunction -# Needed for LessToGreaterBridge -function promote_operation( - ::Union{typeof(+),typeof(-)}, - ::Type{<:Real}, - ::Type{MOI.ScalarNonlinearFunction}, -) - return MOI.ScalarNonlinearFunction +@static if Sys.WORD_SIZE == 64 + function promote_operation( + ::Union{typeof(+),typeof(-)}, + ::Type{<:Real}, + ::Type{MOI.ScalarNonlinearFunction}, + ) + return MOI.ScalarNonlinearFunction + end end function promote_operation( From b2633a03774f2c798228056928f027562839f875 Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 31 May 2023 10:18:20 +1200 Subject: [PATCH 7/9] Update --- .github/workflows/ci.yml | 2 +- src/Utilities/functions.jl | 20 +++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c70adae519..4daf7128c9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,7 +28,7 @@ jobs: - version: '1.6' os: ubuntu-latest arch: x64 - - version: '1' + - version: '1.6' os: ubuntu-latest arch: x86 steps: diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index d878671e87..d9b1093b04 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1816,14 +1816,12 @@ end ### ScalarNonlinearFunction -@static if Sys.WORD_SIZE == 64 - function promote_operation( - ::Union{typeof(+),typeof(-)}, - ::Type{<:Real}, - ::Type{MOI.ScalarNonlinearFunction}, - ) - return MOI.ScalarNonlinearFunction - end +function promote_operation( + ::Union{typeof(+),typeof(-)}, + ::Type{<:Number}, + ::Type{MOI.ScalarNonlinearFunction}, +) + return MOI.ScalarNonlinearFunction end function promote_operation( @@ -1831,16 +1829,16 @@ function promote_operation( ::Type{T}, ::Type{MOI.ScalarNonlinearFunction}, ::Type{T}, -) where {T} +) where {T<:Number} return MOI.ScalarNonlinearFunction end function promote_operation( ::typeof(-), - ::Type{T}, + ::Type{<:Number}, ::Type{MOI.ScalarNonlinearFunction}, ::Type{MOI.VariableIndex}, -) where {T} +) return MOI.ScalarNonlinearFunction end From b706216d4de378dbc15ff2dbc390779dbadf1845 Mon Sep 17 00:00:00 2001 From: odow Date: Wed, 31 May 2023 10:31:04 +1200 Subject: [PATCH 8/9] Update --- src/Utilities/functions.jl | 56 +++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 25 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index d9b1093b04..da2f6f1eb6 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1824,47 +1824,53 @@ function promote_operation( return MOI.ScalarNonlinearFunction end -function promote_operation( +function operate( ::typeof(-), ::Type{T}, - ::Type{MOI.ScalarNonlinearFunction}, - ::Type{T}, -) where {T<:Number} - return MOI.ScalarNonlinearFunction + f::MOI.ScalarNonlinearFunction, +) where {T} + if f.head == :- && length(f.args) == 1 + # A simplification for -(-(f)) into f, but only if f is an SNF. + if f.args[1] isa MOI.ScalarNonlinearFunction + return f.args[1] + end + end + return MOI.ScalarNonlinearFunction(:-, Any[f]) end function promote_operation( - ::typeof(-), - ::Type{<:Number}, + ::Union{typeof(+),typeof(-),typeof(*),typeof(/)}, + ::Type{T}, ::Type{MOI.ScalarNonlinearFunction}, - ::Type{MOI.VariableIndex}, -) + ::Type{S}, +) where { + T<:Number, + S<:Union{ + T, + MOI.VariableIndex, + MOI.ScalarAffineFunction{T}, + MOI.ScalarQuadraticFunction{T}, + MOI.ScalarNonlinearFunction, + } +} return MOI.ScalarNonlinearFunction end function operate( - op::Union{typeof(+),typeof(-)}, + op::Union{typeof(+),typeof(-),typeof(*),typeof(/)}, ::Type{T}, f::MOI.ScalarNonlinearFunction, - g::ScalarQuadraticLike{T}, + g::Union{ + T, + MOI.VariableIndex, + MOI.ScalarAffineFunction{T}, + MOI.ScalarQuadraticFunction{T}, + MOI.ScalarNonlinearFunction, + } ) where {T} return MOI.ScalarNonlinearFunction(Symbol(op), Any[f, g]) end -function operate( - ::typeof(-), - ::Type{T}, - f::MOI.ScalarNonlinearFunction, -) where {T} - if f.head == :- && length(f.args) == 1 - # A simplification for -(-(f)) into f, but only if f is an SNF. - if f.args[1] isa MOI.ScalarNonlinearFunction - return f.args[1] - end - end - return MOI.ScalarNonlinearFunction(:-, Any[f]) -end - ### Base methods _eltype(args::Tuple) = _eltype(first(args), Base.tail(args)) From 62c8261c7d5b903bcbaa3f4b04af88528bfe6ea7 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Wed, 31 May 2023 11:06:24 +1200 Subject: [PATCH 9/9] Apply suggestions from code review --- src/Utilities/functions.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index da2f6f1eb6..6c2b92d4d5 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -1851,7 +1851,7 @@ function promote_operation( MOI.ScalarAffineFunction{T}, MOI.ScalarQuadraticFunction{T}, MOI.ScalarNonlinearFunction, - } + }, } return MOI.ScalarNonlinearFunction end @@ -1866,7 +1866,7 @@ function operate( MOI.ScalarAffineFunction{T}, MOI.ScalarQuadraticFunction{T}, MOI.ScalarNonlinearFunction, - } + }, ) where {T} return MOI.ScalarNonlinearFunction(Symbol(op), Any[f, g]) end