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

sum doesn't work as expected on variables #1215

Closed
non-Jedi opened this issue Apr 5, 2018 · 4 comments
Closed

sum doesn't work as expected on variables #1215

non-Jedi opened this issue Apr 5, 2018 · 4 comments

Comments

@non-Jedi
Copy link

non-Jedi commented Apr 5, 2018

Illustrated with an example:

julia> using JuMP, GLPKMathProgInterface;

julia> solver = GLPKSolverLP();

julia> m = Model(solver=solver);

julia> @variable(m, x[1:2,1:2])
2×2 Array{JuMP.Variable,2}:
 x[1,1]  x[1,2]
 x[2,1]  x[2,2]

julia> x[1,1] + x[1,2]
x[1,1] + x[1,2]

julia> sum(x[1,:])
x[1,1] + x[1,2]

julia> sum(x)
x[1,1] + x[2,1] + x[1,2] + x[2,2]

julia> sum(x,2)
ERROR: MethodError: Cannot `convert` an object of type JuMP.GenericAffExpr{Float64,JuMP.Variable} to an object of type JuMP.Variable
This may have arisen from a call to the constructor JuMP.Variable(...),
since type constructors fall back to convert methods.
Stacktrace:
 [1] fill!(::Array{JuMP.Variable,2}, ::JuMP.GenericAffExpr{Float64,JuMP.Variable}) at ./multidimensional.jl:841
 [2] reducedim_initarray(::Array{JuMP.Variable,2}, ::Int64, ::JuMP.GenericAffExpr{Float64,JuMP.Variable}, ::Type{JuMP.Variable}) at ./reducedim.jl:73
 [3] _reducedim_init(::Base.#identity, ::Base.#+, ::Base.#zero, ::Base.#sum, ::Array{JuMP.Variable,2}, ::Int64) at ./reducedim.jl:102
 [4] reducedim_init(::Function, ::Base.#+, ::Array{JuMP.Variable,2}, ::Int64) at ./reducedim.jl:87
 [5] mapreducedim(::Function, ::Function, ::Array{JuMP.Variable,2}, ::Int64) at ./reducedim.jl:242
 [6] sum(::Array{JuMP.Variable,2}, ::Int64) at ./reducedim.jl:585
 [7] macro expansion at ./REPL.jl:97 [inlined]
 [8] (::Base.REPL.##1#2{Base.REPL.REPLBackend})() at ./event.jl:73

The last line should give me the same thing as e.g. [x[1,1] + x[1,2]; x[2,1] + x[2,2]] but throws an error instead. Let me know if there's another issue I missed about this or if this is somehow by design. Thanks!

@mlubin
Copy link
Member

mlubin commented Apr 5, 2018

This is more of a feature request than a bug report, but yes, it would be nice if this worked.

@blegat
Copy link
Member

blegat commented Apr 5, 2018

The issue is due to this line:
https://github.com/JuliaLang/julia/blob/39d8aef7c6288d3fef481d56d49e3b054d3512e8/base/reducedim.jl#L98
In our case, z and x have type JuMP.GenericAffExpr{Float64,JuMP.Variable} and T is JuMP.Variable which is not isbits so Tr is T and not typeof(z).

@blegat
Copy link
Member

blegat commented Apr 5, 2018

I have opened an issue on Julia Base JuliaLang/julia#26709

@non-Jedi
Copy link
Author

Fixed by JuliaLang/julia#28089 once JuMP works on 1.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants