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

Broadcasting over multiple DenseAxisArrays #2267

Closed
davide-f opened this issue Jul 8, 2020 · 3 comments · Fixed by #2533
Closed

Broadcasting over multiple DenseAxisArrays #2267

davide-f opened this issue Jul 8, 2020 · 3 comments · Fixed by #2533
Labels
Category: Containers Related to the Containers submodule

Comments

@davide-f
Copy link

davide-f commented Jul 8, 2020

While trying to set up the initial point for a problem, the following problem occur.
no method matching set_start_value(::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{String,1}},Tuple{Dict{String,Int64}}}, ::JuMP.Containers.DenseAxisArray{Float64,1,Tuple{Array{String,1}},Tuple{Dict{String,Int64}}})

In attach a sample of the code
report_error.txt

@odow
Copy link
Member

odow commented Jul 8, 2020

See the Note in the start values section of the docs: https://jump.dev/JuMP.jl/v0.21.1/variables/#Start-values-1. One option is (note the . after set_start_value):

set_start_value.(all_variables(model), value.(all_variables(model)))

Unfortunately, broadcasting over multiple DenseAxisArrays isn't supported yet, so you can't go
set_start_value.(x, value.(x)).

using JuMP
S = [:a, :b]
model = Model()
@variable(model, x[S], start = 0.0)
set_start_value.(x, start_value.(x))
ERROR: Broadcast operations with multiple DenseAxisArrays are not yet supported.
Stacktrace:
 [1] error(::String) at ./error.jl:33
 [2] broadcasted(::JuMP.Containers.DenseAxisArrayBroadcastStyle, ::Function, ::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{Symbol,1}},Tuple{Dict{Symbol,Int64}}}, ::JuMP.Containers.DenseAxisArray{Float64,1,Tuple{Array{Symbol,1}},Tuple{Dict{Symbol,Int64}}}) at /Users/oscar/.julia/packages/JuMP/YXK4e/src/Containers/DenseAxisArray.jl:249
 [3] broadcasted(::Function, ::JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{Symbol,1}},Tuple{Dict{Symbol,Int64}}}, ::JuMP.Containers.DenseAxisArray{Float64,1,Tuple{Array{Symbol,1}},Tuple{Dict{Symbol,Int64}}}) at ./broadcast.jl:1237
 [4] top-level scope at REPL[21]:1

As a work-around, you can go

using JuMP
S = [:a, :b]
model = Model()
@variable(model, x[S], start = 0.0)
for i in eachindex(x)
    set_start_value(x[i], start_value(x[i]))
end

@odow odow changed the title Problem in setting set_start_value Broadcasting over multiple DenseAxisArrays Jul 8, 2020
@odow odow added the Category: Containers Related to the Containers submodule label Jul 8, 2020
@chkwon
Copy link
Contributor

chkwon commented Mar 11, 2021

Can the error message improved in this case?

using JuMP
S = [:a, :b]
model = Model()
@variable(model, x[S], start = 0.0)
set_start_value.(x, [0.0 1.0])

If the error message for this case could mention broadcasting, it would be helpful.

ERROR: MethodError: no method matching UnitRange{Int64}(::Array{Symbol,1})
Closest candidates are:
  UnitRange{Int64}(::Any, ::Static.StaticInt) where T<:Real at /Users/chkwon/.julia/packages/Static/XMB22/src/static_implementation.jl:133
  UnitRange{Int64}(::Any, ::Any) where T<:Real at range.jl:280
  UnitRange{Int64}(::UnitRange{T}) where T<:Real at range.jl:918
  ...
Stacktrace:
 [1] axistype(::Array{Symbol,1}, ::Base.OneTo{Int64}) at ./broadcast.jl:511
 [2] _bcs1(::Array{Symbol,1}, ::Base.OneTo{Int64}) at ./broadcast.jl:501
 [3] _bcs at ./broadcast.jl:495 [inlined]
 [4] broadcast_shape at ./broadcast.jl:489 [inlined]
 [5] combine_axes at ./broadcast.jl:484 [inlined]
 [6] instantiate at ./broadcast.jl:266 [inlined]
 [7] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.Unknown,Nothing,typeof(set_start_value),Tuple{JuMP.Containers.DenseAxisArray{VariableRef,1,Tuple{Array{Symbol,1}},Tuple{Dict{Symbol,Int64}}},Array{Float64,2}}}) at ./broadcast.jl:837
 [8] top-level scope at REPL[70]:1

@odow
Copy link
Member

odow commented Mar 12, 2021

This is a tricky one that we haven't really addressed: people want a DenseAxisArray to broadcast like an Array of the same dimension.

At the very least, it deserves a better error message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Containers Related to the Containers submodule
Development

Successfully merging a pull request may close this issue.

3 participants