From 49a77b4713624a65074e77fa083812f9d6096239 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 23 Feb 2022 15:11:54 -0500 Subject: [PATCH 1/3] Canonicalize function in add_constraint for MatrixOfConstraints --- src/Utilities/matrix_of_constraints.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Utilities/matrix_of_constraints.jl b/src/Utilities/matrix_of_constraints.jl index 85ff23e81a..e28788cc92 100644 --- a/src/Utilities/matrix_of_constraints.jl +++ b/src/Utilities/matrix_of_constraints.jl @@ -352,6 +352,7 @@ function _add_constraint( func::F, set::S, ) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet} + func = MOI.Utilities.canonical(func) allocate_terms(model.coefficients, index_map, func) # Without this type annotation, the compiler is unable to know the type # of `caches[i]` so this is slower and produce an allocation. @@ -379,6 +380,9 @@ function MOI.add_constraint( ), ) end + if !Utilities.is_canonical(func) + func = Utilities.canonical(func) + end return _add_constraint(model, i, IdentityMap(), func, set) end From 6813640a943ff72961bc5e4de3c70ad8bed34c13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 23 Feb 2022 15:36:33 -0500 Subject: [PATCH 2/3] Remove second canonical --- src/Utilities/matrix_of_constraints.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Utilities/matrix_of_constraints.jl b/src/Utilities/matrix_of_constraints.jl index e28788cc92..0c042f9461 100644 --- a/src/Utilities/matrix_of_constraints.jl +++ b/src/Utilities/matrix_of_constraints.jl @@ -352,7 +352,6 @@ function _add_constraint( func::F, set::S, ) where {F<:MOI.AbstractFunction,S<:MOI.AbstractSet} - func = MOI.Utilities.canonical(func) allocate_terms(model.coefficients, index_map, func) # Without this type annotation, the compiler is unable to know the type # of `caches[i]` so this is slower and produce an allocation. From fbebf8fcc9966428dabdafe210e64e5ccd9ec203 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Thu, 24 Feb 2022 10:14:20 -0500 Subject: [PATCH 3/3] Add test --- test/Utilities/matrix_of_constraints.jl | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/Utilities/matrix_of_constraints.jl b/test/Utilities/matrix_of_constraints.jl index cef44753c0..9b9dc1c936 100644 --- a/test/Utilities/matrix_of_constraints.jl +++ b/test/Utilities/matrix_of_constraints.jl @@ -587,6 +587,16 @@ function test_dual_power_cone() return end +function test_duplicate() + model = _new_ScalarSets() + x = MOI.add_variable(model) + c = MOI.add_constraint(model, 1.0x + 2.0x, MOI.EqualTo(-1.0)) + MOI.Utilities.final_touch(model, nothing) + @test MOI.get(model, MOI.ConstraintFunction(), c) ≈ 3.0x + @test MOI.get(model, MOI.ConstraintSet(), c) == MOI.EqualTo(-1.0) + return +end + end TestMatrixOfConstraints.runtests()