From 074e65b47c34474ad4050ba6a12decc15a88e69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 17 Jun 2021 23:49:26 -0400 Subject: [PATCH 1/3] Fix isapprox when the first function is missing terms --- src/functions.jl | 3 ++- test/functions.jl | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/functions.jl b/src/functions.jl index a74121a2ab..061c261e82 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -303,7 +303,8 @@ end # 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) + return all(kv -> compare(kv.second, Base.get(d2, kv.first, zero(T))), d1) && + all(kv -> compare(kv.second, Base.get(d1, kv.first, zero(T))), d2) end # Build a dictionary where the duplicate keys are summed diff --git a/test/functions.jl b/test/functions.jl index fbe5c46ccd..2bf2d73af5 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -139,6 +139,9 @@ function test_isapprox_ScalarAffineFunction() @test g ≈ f f.terms[2] = MOI.ScalarAffineTerm(3, y) @test !(g ≈ f) + pop!(f.terms) + @test !(g ≈ f) + @test !(f ≈ g) end function test_isapprox_VectorAffineFunction() @@ -157,6 +160,9 @@ function test_isapprox_VectorAffineFunction() @test !(f ≈ g) push!(f.terms, MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(-6, y))) @test f ≈ g + pop!(g.terms) + @test !(g ≈ f) + @test !(f ≈ g) end function test_isapprox_ScalarQuadraticFunction() @@ -177,6 +183,9 @@ function test_isapprox_ScalarQuadraticFunction() @test !(f ≈ g) push!(f.quadratic_terms, MOI.ScalarQuadraticTerm(-2, y, x)) @test f ≈ g + pop!(g.quadratic_terms) + @test !(g ≈ f) + @test !(f ≈ g) end function test_isapprox_VectorQuadraticFunction() @@ -204,6 +213,11 @@ function test_isapprox_VectorQuadraticFunction() f.quadratic_terms[1] = MOI.VectorQuadraticTerm(3, MOI.ScalarQuadraticTerm(1, x, x)) @test !(f ≈ g) + f.quadratic_terms[1] = + MOI.VectorQuadraticTerm(1, MOI.ScalarQuadraticTerm(1, x, x)) + pop!(f.quadratic_terms) + @test !(g ≈ f) + @test !(f ≈ g) end function runtests() From fc52c3cb2c93b0d9faad59ff91ec673442c07aca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Fri, 18 Jun 2021 09:17:30 -0400 Subject: [PATCH 2/3] Fix test --- test/Bridges/Variable/zeros.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Bridges/Variable/zeros.jl b/test/Bridges/Variable/zeros.jl index 66f1ae26f5..1d111cbba5 100644 --- a/test/Bridges/Variable/zeros.jl +++ b/test/Bridges/Variable/zeros.jl @@ -149,7 +149,7 @@ end 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 """ From a2e85bb6947d5353aeb51416ef87392a4077b4c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Fri, 18 Jun 2021 09:18:26 -0400 Subject: [PATCH 3/3] Fix formatting --- src/functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions.jl b/src/functions.jl index 061c261e82..383a8d195e 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -304,7 +304,7 @@ end # 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) && - all(kv -> compare(kv.second, Base.get(d1, kv.first, zero(T))), d2) + all(kv -> compare(kv.second, Base.get(d1, kv.first, zero(T))), d2) end # Build a dictionary where the duplicate keys are summed