From 51d77dfc6abc398409af86d8486858d41815bbcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Sun, 17 May 2026 21:17:22 +0200 Subject: [PATCH 1/2] Add tests for briding cost attributes --- src/Test/test_attribute.jl | 53 +++++++++++++++++++++++++++++++ src/Test/test_basic_constraint.jl | 10 ++++++ 2 files changed, 63 insertions(+) diff --git a/src/Test/test_attribute.jl b/src/Test/test_attribute.jl index a8fa9faaac..bf452cb4ae 100644 --- a/src/Test/test_attribute.jl +++ b/src/Test/test_attribute.jl @@ -392,4 +392,57 @@ function test_attribute_unsupported_constraint(model::MOI.ModelLike, ::Config) return end +""" + test_attribute_VariableBridgingCost(model::MOI.ModelLike, config::Config) + +Test that, for every set `S` that the model claims to support via +`supports_add_constrained_variable(s)`, the corresponding +[`MOI.VariableBridgingCost`](@ref) attribute returns a finite value. + +This is the variable-side analogue of the +`ConstraintBridgingCost` check in `_basic_constraint_test_helper`. +The fallback works for most model but it may need +custom method for some MOI layer (see +https://github.com/jump-dev/MathOptInterface.jl/pull/3001#issuecomment-4468198935) +This test is here to catch that. +""" +function test_attribute_VariableBridgingCost( + model::MOI.ModelLike, + ::Config{T}, +) where {T} + scalar_sets = Type{<:MOI.AbstractScalarSet}[ + MOI.GreaterThan{T}, + MOI.LessThan{T}, + MOI.EqualTo{T}, + MOI.Interval{T}, + MOI.Integer, + MOI.ZeroOne, + MOI.Semicontinuous{T}, + MOI.Semiinteger{T}, + ] + vector_sets = Type{<:MOI.AbstractVectorSet}[ + MOI.Reals, + MOI.Zeros, + MOI.Nonnegatives, + MOI.Nonpositives, + MOI.SecondOrderCone, + MOI.RotatedSecondOrderCone, + MOI.ExponentialCone, + MOI.PositiveSemidefiniteConeTriangle, + ] + for S in scalar_sets + if MOI.supports_add_constrained_variable(model, S) + @test MOI.get(model, MOI.VariableBridgingCost{S}()) < Inf + end + end + for S in vector_sets + if MOI.supports_add_constrained_variables(model, S) + @test MOI.get(model, MOI.VariableBridgingCost{S}()) < Inf + end + end + return +end + +version_added(::typeof(test_attribute_VariableBridgingCost)) = v"1.52.0" + version_added(::typeof(test_attribute_unsupported_constraint)) = v"1.9.0" diff --git a/src/Test/test_basic_constraint.jl b/src/Test/test_basic_constraint.jl index 0d3788d009..bacc84d8e6 100644 --- a/src/Test/test_basic_constraint.jl +++ b/src/Test/test_basic_constraint.jl @@ -253,6 +253,16 @@ function _basic_constraint_test_helper( ### @requires MOI.supports_constraint(model, F, S) ### + ### Test MOI.ConstraintBridgingCost + ### + # If `supports_constraint(F, S)` returns `true`, then the model must be + # able to handle that pair (possibly via bridging), so the bridging cost + # must be finite. The fallback works for most model but it may need + # custom method for some MOI layer (see + # https://github.com/jump-dev/MathOptInterface.jl/pull/3001#issuecomment-4468198935) + # This test is here to catch that. + @test MOI.get(model, MOI.ConstraintBridgingCost{F,S}()) < Inf + ### ### Test MOI.NumberOfConstraints ### @test MOI.get(model, MOI.NumberOfConstraints{F,S}()) == 0 From 8c9083ece58e1f00d2a4bd6b697d50e41f7711f4 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Mon, 18 May 2026 10:20:59 +1200 Subject: [PATCH 2/2] Refactor test_attribute_VariableBridgingCost function Refactor test for VariableBridgingCost to improve clarity and maintainability. --- src/Test/test_attribute.jl | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Test/test_attribute.jl b/src/Test/test_attribute.jl index bf452cb4ae..a5bb796f27 100644 --- a/src/Test/test_attribute.jl +++ b/src/Test/test_attribute.jl @@ -392,6 +392,8 @@ function test_attribute_unsupported_constraint(model::MOI.ModelLike, ::Config) return end +version_added(::typeof(test_attribute_unsupported_constraint)) = v"1.9.0" + """ test_attribute_VariableBridgingCost(model::MOI.ModelLike, config::Config) @@ -399,18 +401,19 @@ Test that, for every set `S` that the model claims to support via `supports_add_constrained_variable(s)`, the corresponding [`MOI.VariableBridgingCost`](@ref) attribute returns a finite value. -This is the variable-side analogue of the -`ConstraintBridgingCost` check in `_basic_constraint_test_helper`. -The fallback works for most model but it may need -custom method for some MOI layer (see -https://github.com/jump-dev/MathOptInterface.jl/pull/3001#issuecomment-4468198935) +This is the variable-side analog of the `ConstraintBridgingCost` check in +`_basic_constraint_test_helper`. + +The fallback works for most model but it may need custom method for some MOI +layers (see https://github.com/jump-dev/MathOptInterface.jl/pull/3001#issuecomment-4468198935). + This test is here to catch that. """ function test_attribute_VariableBridgingCost( model::MOI.ModelLike, ::Config{T}, ) where {T} - scalar_sets = Type{<:MOI.AbstractScalarSet}[ + for S in Any[ MOI.GreaterThan{T}, MOI.LessThan{T}, MOI.EqualTo{T}, @@ -420,7 +423,11 @@ function test_attribute_VariableBridgingCost( MOI.Semicontinuous{T}, MOI.Semiinteger{T}, ] - vector_sets = Type{<:MOI.AbstractVectorSet}[ + if MOI.supports_add_constrained_variable(model, S) + @test MOI.get(model, MOI.VariableBridgingCost{S}()) < Inf + end + end + for S in Any[ MOI.Reals, MOI.Zeros, MOI.Nonnegatives, @@ -430,12 +437,6 @@ function test_attribute_VariableBridgingCost( MOI.ExponentialCone, MOI.PositiveSemidefiniteConeTriangle, ] - for S in scalar_sets - if MOI.supports_add_constrained_variable(model, S) - @test MOI.get(model, MOI.VariableBridgingCost{S}()) < Inf - end - end - for S in vector_sets if MOI.supports_add_constrained_variables(model, S) @test MOI.get(model, MOI.VariableBridgingCost{S}()) < Inf end @@ -444,5 +445,3 @@ function test_attribute_VariableBridgingCost( end version_added(::typeof(test_attribute_VariableBridgingCost)) = v"1.52.0" - -version_added(::typeof(test_attribute_unsupported_constraint)) = v"1.9.0"