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..8b97242f115 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