diff --git a/src/functions.jl b/src/functions.jl index a74121a2ab..f5d14d99e8 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -301,9 +301,15 @@ function Base.isapprox( return f == g end -# For affine and quadratic functions, terms are compressed in a dictionary using `_dicts` and then the dictionaries are compared with `dict_compare` +# For affine and quadratic functions, terms are compressed in a dictionary using +# `_dicts` and then the dictionaries are compared with `dict_compare` function dict_compare(d1::Dict, d2::Dict{<:Any,T}, compare::Function) where {T} - return all(kv -> compare(kv.second, Base.get(d2, kv.first, zero(T))), d1) + for key in union(keys(d1), keys(d2)) + if !compare(Base.get(d1, key, zero(T)), Base.get(d2, key, zero(T))) + return false + end + end + return true end # Build a dictionary where the duplicate keys are summed diff --git a/test/Bridges/Variable/zeros.jl b/test/Bridges/Variable/zeros.jl index aea370b8ae..fddc12b412 100644 --- a/test/Bridges/Variable/zeros.jl +++ b/test/Bridges/Variable/zeros.jl @@ -159,7 +159,7 @@ function test_zeros() s = """ variables: x x >= 0.0 - con1: x + 0.0 == 0.0 + con1: 0.0 == 0.0 con2: x + 0.0 >= 1.0 minobjective: x """ diff --git a/test/FileFormats/SDPA/SDPA.jl b/test/FileFormats/SDPA/SDPA.jl index 2d64615903..cc556acf9b 100644 --- a/test/FileFormats/SDPA/SDPA.jl +++ b/test/FileFormats/SDPA/SDPA.jl @@ -261,7 +261,7 @@ const _EXAMPLE_MODELS = [ """ variables: x, y minobjective: 10x + 20y - c1: [x + -1, 0, x + -2] in PositiveSemidefiniteConeTriangle(2) + c1: [x + -1, 0, x + y + -2] in PositiveSemidefiniteConeTriangle(2) c2: [5y + -3, 2y, 6y + -4] in PositiveSemidefiniteConeTriangle(2) """, ), @@ -298,7 +298,7 @@ end function runtests() for name in names(@__MODULE__, all = true) if startswith("$(name)", "test_") - @testset "name" begin + @testset "$name" begin getfield(@__MODULE__, name)() end end diff --git a/test/functions.jl b/test/functions.jl index fbe5c46ccd..f5552e1f11 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -110,6 +110,7 @@ function test_isapprox_SingleVariable() y = MOI.VariableIndex(2) @test MOI.SingleVariable(x) == MOI.SingleVariable(x) @test MOI.SingleVariable(x) != MOI.SingleVariable(y) + return end function test_isapprox_VectorOfVariables() @@ -119,6 +120,7 @@ function test_isapprox_VectorOfVariables() @test MOI.VectorOfVariables([y, x]) != MOI.VectorOfVariables([x, y]) @test MOI.VectorOfVariables([x, x]) != MOI.VectorOfVariables([x]) @test MOI.VectorOfVariables([x]) != MOI.VectorOfVariables([y]) + return end function test_isapprox_ScalarAffineFunction() @@ -137,8 +139,11 @@ function test_isapprox_ScalarAffineFunction() f = MOI.ScalarAffineFunction(MOI.ScalarAffineTerm.([2, 4], [x, y]), 6) g = deepcopy(f) @test g ≈ f + @test f ≈ g f.terms[2] = MOI.ScalarAffineTerm(3, y) @test !(g ≈ f) + @test !(f ≈ g) + return end function test_isapprox_VectorAffineFunction() @@ -155,8 +160,11 @@ function test_isapprox_VectorAffineFunction() @test f ≈ g f.terms[3] = MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(9, y)) @test !(f ≈ g) + @test !(g ≈ f) push!(f.terms, MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(-6, y))) @test f ≈ g + @test g ≈ f + return end function test_isapprox_ScalarQuadraticFunction() @@ -175,8 +183,11 @@ function test_isapprox_ScalarQuadraticFunction() g = deepcopy(f) push!(f.quadratic_terms, MOI.ScalarQuadraticTerm(2, y, x)) @test !(f ≈ g) + @test !(g ≈ f) push!(f.quadratic_terms, MOI.ScalarQuadraticTerm(-2, y, x)) @test f ≈ g + @test g ≈ f + return end function test_isapprox_VectorQuadraticFunction() @@ -196,14 +207,47 @@ function test_isapprox_VectorQuadraticFunction() ) g = deepcopy(f) @test f ≈ g + @test g ≈ f f.affine_terms[1] = MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(4, x)) @test !(f ≈ g) + @test !(g ≈ f) push!(g.affine_terms, MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(-3, x))) push!(g.affine_terms, MOI.VectorAffineTerm(3, MOI.ScalarAffineTerm(4, x))) @test f ≈ g + @test g ≈ f f.quadratic_terms[1] = MOI.VectorQuadraticTerm(3, MOI.ScalarQuadraticTerm(1, x, x)) @test !(f ≈ g) + @test !(g ≈ f) + return +end + +function test_isapprox_issue_1483() + x = MOI.ScalarQuadraticFunction( + MOI.ScalarAffineTerm{Float16}[], + MOI.ScalarQuadraticTerm{Float16}[], + Float16(0.0), + ) + y = MOI.ScalarQuadraticFunction( + MOI.ScalarAffineTerm{Float16}[MOI.ScalarAffineTerm( + Float16(1.0), + MOI.VariableIndex(1234), + )], + MOI.ScalarQuadraticTerm{Float16}[], + Float16(0.0), + ) + z = MOI.ScalarQuadraticFunction( + MOI.ScalarAffineTerm{Float16}[], + MOI.ScalarQuadraticTerm{Float16}[], + Float16(0.0), + ) + @test !(x ≈ y) + @test !(y ≈ x) + @test x ≈ z + @test z ≈ x + @test !(y ≈ z) + @test !(z ≈ y) + return end function runtests()