From b71e8ee444f1c4b70bcc7f1dadaed7a82b6f0524 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Fri, 14 Feb 2025 21:31:17 +0100 Subject: [PATCH 1/7] VAF improved constructor --- src/functions.jl | 13 +++++++++++++ test/functions.jl | 8 ++++++++ 2 files changed, 21 insertions(+) diff --git a/src/functions.jl b/src/functions.jl index b6193e0192..e764126158 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -578,6 +578,19 @@ function VectorAffineFunction{T}(f::VectorOfVariables) where {T} return VectorAffineFunction(terms, constants) end + +function VectorAffineFunction(elements::Vector{ScalarAffineFunction{T}}) where {T} + terms = Vector{VectorAffineTerm{T}}() + constants = Vector{T}() + for (idx, saf) in enumerate(elements) + push!(constants, saf.constant) + for term in saf.terms + push!(terms, VectorAffineTerm(idx, term)) + end + end + return VectorAffineFunction{T}(terms, constants) +end + """ VectorQuadraticTerm{T}( output_index::Int64, diff --git a/test/functions.jl b/test/functions.jl index d6143d161b..f8abf996ea 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -129,6 +129,14 @@ function test_functions_convert_ScalarQuadraticFunction() ) end +function test_vectoraffinefunction_creation() + x = MOI.VariableIndex(1) + f = MOI.VectorAffineFunction([1.0 * x, 2.0 * x]) + @test f.constants == [0.0, 0.0] + @test f.terms[1].scalar_term.coefficient == 1.0 + @test f.terms[2].scalar_term.coefficient == 2.0 +end + function test_isapprox_VectorOfVariables() x = MOI.VariableIndex(1) y = MOI.VariableIndex(2) From 2bb14ee15f72ce1cf4c8c5e62199084429e444d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Fri, 14 Feb 2025 21:31:35 +0100 Subject: [PATCH 2/7] formatter --- src/functions.jl | 5 +++-- test/functions.jl | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/functions.jl b/src/functions.jl index e764126158..0dbfc33edd 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -578,8 +578,9 @@ function VectorAffineFunction{T}(f::VectorOfVariables) where {T} return VectorAffineFunction(terms, constants) end - -function VectorAffineFunction(elements::Vector{ScalarAffineFunction{T}}) where {T} +function VectorAffineFunction( + elements::Vector{ScalarAffineFunction{T}}, +) where {T} terms = Vector{VectorAffineTerm{T}}() constants = Vector{T}() for (idx, saf) in enumerate(elements) diff --git a/test/functions.jl b/test/functions.jl index f8abf996ea..73bb3eb2ad 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -130,7 +130,7 @@ function test_functions_convert_ScalarQuadraticFunction() end function test_vectoraffinefunction_creation() - x = MOI.VariableIndex(1) + x = MOI.VariableIndex(1) f = MOI.VectorAffineFunction([1.0 * x, 2.0 * x]) @test f.constants == [0.0, 0.0] @test f.terms[1].scalar_term.coefficient == 1.0 From a72a9c6a5f0ebaa027ec79400f99b4c0e8913219 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Sat, 15 Feb 2025 14:14:22 +1300 Subject: [PATCH 3/7] Update functions.jl --- test/functions.jl | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/test/functions.jl b/test/functions.jl index 73bb3eb2ad..e4b14b42e8 100644 --- a/test/functions.jl +++ b/test/functions.jl @@ -131,10 +131,16 @@ end function test_vectoraffinefunction_creation() x = MOI.VariableIndex(1) - f = MOI.VectorAffineFunction([1.0 * x, 2.0 * x]) - @test f.constants == [0.0, 0.0] - @test f.terms[1].scalar_term.coefficient == 1.0 - @test f.terms[2].scalar_term.coefficient == 2.0 + f = MOI.VectorAffineFunction([1.0 * x + 2.0, 3.0 * x + 4.0]) + g = MOI.VectorAffineFunction( + MOI.VectorAffineTerm{Float64}[ + MOI.VectorAffineTerm(1, MOI.ScalarAffineTerm(1.0, x)), + MOI.VectorAffineTerm(2, MOI.ScalarAffineTerm(3.0, x)), + ], + [2.0, 4.0], + ) + @test f ≈ g + return end function test_isapprox_VectorOfVariables() From 19e4a9e79836d5ccf81628e04b56b104ec2849ee Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Sat, 15 Feb 2025 14:16:52 +1300 Subject: [PATCH 4/7] Update functions.jl --- src/functions.jl | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/functions.jl b/src/functions.jl index 0dbfc33edd..e43e4ced18 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -578,18 +578,15 @@ function VectorAffineFunction{T}(f::VectorOfVariables) where {T} return VectorAffineFunction(terms, constants) end -function VectorAffineFunction( - elements::Vector{ScalarAffineFunction{T}}, -) where {T} - terms = Vector{VectorAffineTerm{T}}() - constants = Vector{T}() - for (idx, saf) in enumerate(elements) - push!(constants, saf.constant) - for term in saf.terms - push!(terms, VectorAffineTerm(idx, term)) +function VectorAffineFunction(rows::Vector{ScalarAffineFunction{T}}) where {T} + ret = VectorAffineFunction{T}(VectorAffineTerm{T}[], T[]) + for (row, f) in enumerate(rows) + push!(ret.constants, f.constant) + for term in f.terms + push!(ret.terms, VectorAffineTerm(row, term)) end end - return VectorAffineFunction{T}(terms, constants) + return ret end """ From 232c5a661e333304a24fa0bc16dfa50d310d2636 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Sat, 15 Feb 2025 09:42:49 +0100 Subject: [PATCH 5/7] make the vector abstract --- src/functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions.jl b/src/functions.jl index 0dbfc33edd..f5e56242d0 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -579,7 +579,7 @@ function VectorAffineFunction{T}(f::VectorOfVariables) where {T} end function VectorAffineFunction( - elements::Vector{ScalarAffineFunction{T}}, + elements::AbstractVector{ScalarAffineFunction{T}}, ) where {T} terms = Vector{VectorAffineTerm{T}}() constants = Vector{T}() From 8d88d3597bce05215499a659c3f9ffaf7cb5a9f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathieu=20Besan=C3=A7on?= Date: Sat, 15 Feb 2025 10:03:01 +0100 Subject: [PATCH 6/7] format --- src/functions.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/functions.jl b/src/functions.jl index 863b962dbd..f088cc3cee 100644 --- a/src/functions.jl +++ b/src/functions.jl @@ -579,7 +579,7 @@ function VectorAffineFunction{T}(f::VectorOfVariables) where {T} end function VectorAffineFunction( - rows::AbstractVector{ScalarAffineFunction{T}} + rows::AbstractVector{ScalarAffineFunction{T}}, ) where {T} ret = VectorAffineFunction{T}(VectorAffineTerm{T}[], T[]) for (row, f) in enumerate(rows) From 1214d91c533955889b282ff21003c249bd9a6e0a Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Mon, 17 Feb 2025 12:03:37 +1300 Subject: [PATCH 7/7] Update functions.jl --- src/Utilities/functions.jl | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/Utilities/functions.jl b/src/Utilities/functions.jl index 2d558f92e4..2675e8512c 100644 --- a/src/Utilities/functions.jl +++ b/src/Utilities/functions.jl @@ -2207,14 +2207,7 @@ Returns the vector of scalar affine functions in the form of a `MOI.VectorAffineFunction{T}`. """ function vectorize(funcs::AbstractVector{MOI.ScalarAffineFunction{T}}) where {T} - nterms = - mapreduce(func -> number_of_affine_terms(T, func), +, funcs, init = 0) - out_dim = mapreduce(func -> output_dim(T, func), +, funcs, init = 0) - terms = Vector{MOI.VectorAffineTerm{T}}(undef, nterms) - constant = zeros(T, out_dim) - fill_vector(terms, T, fill_terms, number_of_affine_terms, funcs) - fill_vector(constant, T, fill_constant, output_dim, funcs) - return MOI.VectorAffineFunction(terms, constant) + return MOI.VectorAffineFunction(funcs) end """