-
-
Notifications
You must be signed in to change notification settings - Fork 401
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
Splatting does not work inside [] for variable declarations #1964
Comments
Seems like this issue was not addressed. Instead, an error message was added saying "do not use splatting". Some explanation would be helpful for the motivation behind closing this issue. Otherwise, just silently closing issues like this really demotivates from filing them in the future. |
Hi @AzamatB, See the discussion in the pull request #1977. There are some fundamental challenges that mean making splatting work here is very hard. In particular, splatting needs to happen at run-time, whereas we rewrite the contents inside the macros at compile time. Essentially @variable(model, x[A, B])
# becomes
x = ...
for a in A
for b in B
x[a, b] = add_variable(model)
end
end with the splatting, we don't know how many elements there are, so we can't preallocate I appreciate you opening the issue. Please feel free to open more in future. It helps to improve the quality of JuMP :) |
@odow @mlubin I appreciate the nice warning, but what actually is the way to get around splatting? |
Splatting is restricted only in the macros; it's a parsing issue. If you want more flexibility, you should choose a container type (e.g., Array, DenseAxisArray, SparseAxisArray, [insert your favorite data structure]) and construct it with function calls that allow you to splat. See https://jump.dev/JuMP.jl/v0.21.5/containers/. |
This is still not clear to me. I don't understand what you mean by "construct it with function calls that allow you to splat". using JuMP
model = JuMP.Model()
dims = [Base.OneTo(5), Base.OneTo(3)] # let's pretend we don't know the dimensions
v = @variable(model, [dims...]) # doesn't work, as discussed above
v = @variable(model, dims) # obviously doesn't work, since it believes dims is a variable name
idxs = collect(Base.product([collect(s) for s in sizes]...))
v = @variable(model, [idxs]) # doesn't have desired behavior, since v has dimension 1.
array = JuMP.Containers.DenseAxisArray(idxs, sizes...)
v = @variable(model, [array]) # doesn't work, since indexing is by v[Tuple], not v[Tuple...] I am stumped. What is the right approach here? |
For example: using JuMP
model = JuMP.Model()
dims = [Base.OneTo(5), Base.OneTo(3)]
array = JuMP.Containers.DenseAxisArray{JuMP.VariableRef}(undef, dims...)
for idx in eachindex(array)
array[idx] = @variable(model)
end |
Or if you want a plain Array: sizes = [5, 3]
v = Array{JuMP.VariableRef}(undef, sizes...)
for i in eachindex(v)
v[i] = @variable(model)
end |
Ok, maybe I am being a pain in the neck, but this is precisely what I would prefer not to do. Because let's say I decided to name the variables: dims = [Base.OneTo(5), Base.OneTo(3)]
array = JuMP.Containers.DenseAxisArray{JuMP.VariableRef}(undef, dims...)
for idx in eachindex(array)
array[idx] = @variable(model, base_name = "c")
end Now the issue is when I call |
Is for idx in eachindex(array)
array[idx] = @variable(model)
set_name(array[idx], "c[$(join(Tuple(idx),","))]")
end |
|
Write
now the following
does not work, while
does.
The text was updated successfully, but these errors were encountered: