From b5d81b06e111f7267014448c8e6088860b7bf523 Mon Sep 17 00:00:00 2001 From: odow Date: Sat, 23 Sep 2023 12:14:10 +1200 Subject: [PATCH 1/2] Fix constructing an array of Parameter variables --- src/macros.jl | 9 ++------- src/sets.jl | 8 ++++++++ test/test_variable.jl | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/macros.jl b/src/macros.jl index 8195ddb18cc..9cb281d0315 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -1199,14 +1199,9 @@ end function model_convert( model::AbstractModel, - x::AbstractVector{<:VariableConstrainedOnCreation}, + x::AbstractArray{<:VariableConstrainedOnCreation}, ) - return map(x) do xi - return VariableConstrainedOnCreation( - xi.scalar_variable, - model_convert(model, xi.set), - ) - end + return model_convert.(model, x) end # TODO: update 3-argument @constraint macro to pass through names like @variable diff --git a/src/sets.jl b/src/sets.jl index 7c017b5653f..25e2b564b98 100644 --- a/src/sets.jl +++ b/src/sets.jl @@ -273,6 +273,14 @@ function build_variable( return build_variable.(_error, variables, sets) end +function build_variable( + _error::Function, + variables::AbstractArray{<:AbstractVariable}, + set::AbstractScalarSet, +) + return build_variable.(_error, variables, Ref(set)) +end + function build_constraint( _error::Function, func::AbstractJuMPScalar, diff --git a/test/test_variable.jl b/test/test_variable.jl index 87f376502d3..261aed2dace 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -1536,4 +1536,23 @@ function test_missing_variable_constraint_errors() return end +function test_parameter_arrays() + model = Model(); + @variable(model, x1[1:2, 1:2] in Parameter(0.0)) + @test all(is_parameter.(x1)) + @test parameter_value.(x1) == [0.0 0.0; 0.0 0.0] + @variable(model, x2[i = 1:2, j = 1:2] in Parameter(i + j)) + @test all(is_parameter.(x2)) + @test parameter_value.(x2) == [2.0 3.0; 3.0 4.0] + @variable(model, x3[i = 1:2, j = [:A, :B]] in Parameter(i)) + @test all(is_parameter.(x3)) + @test parameter_value.(x3) == + Containers.@container([i = 1:2, j = [:A, :B]], 1.0 * i) + @variable(model, x4[i = 1:2, j = i:2] in Parameter(i + j)) + @test all(is_parameter.(x4)) + @test parameter_value.(x4) == + Containers.@container([i = 1:2, j = i:2], 1.0 * i + j) + return +end + end # module TestVariable From f5118588c330098f4037ba0377a07f577bdfc83b Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Sat, 23 Sep 2023 12:25:24 +1200 Subject: [PATCH 2/2] Update test/test_variable.jl --- test/test_variable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_variable.jl b/test/test_variable.jl index 261aed2dace..8b97242f115 100644 --- a/test/test_variable.jl +++ b/test/test_variable.jl @@ -1537,7 +1537,7 @@ function test_missing_variable_constraint_errors() end function test_parameter_arrays() - model = Model(); + model = Model() @variable(model, x1[1:2, 1:2] in Parameter(0.0)) @test all(is_parameter.(x1)) @test parameter_value.(x1) == [0.0 0.0; 0.0 0.0]