Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix constructing an array of Parameter variables #3524

Merged
merged 2 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/sets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 19 additions & 0 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading