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

2D JuMPArray (symbols-integers): indexing fails with column #851

Closed
dourouc05 opened this issue Sep 14, 2016 · 7 comments
Closed

2D JuMPArray (symbols-integers): indexing fails with column #851

dourouc05 opened this issue Sep 14, 2016 · 7 comments

Comments

@dourouc05
Copy link
Contributor

I am defining a series of variables with one dimension being symbols and the other integers; then I am trying to work with ranges of variables, but this yields strange errors.

using JuMP
m = Model()
@variable(m, w[1:2], Bin)
@variable(m, x[[:1,:2]], Bin)
@variable(m, y[[:1, :2], [1, 2]], Bin)
@variable(m, z[[1, 2], [:1, :2]], Bin)

When there is only one dimension, indexed with symbols, columns work as expected:

julia> x[:]
2-element Array{JuMP.Variable,1}:
 x[1]
 x[2]

However, when selecting a subset of variables, this no more works (unlike with basic integers for indexing):

julia> w[:]
2-element Array{JuMP.Variable,1}:
 v[1]
 v[2]

julia> x[[:1, :2]]
ERROR: Failed attempt to index JuMPArray along dimension 1: [1,2] ∉ [1,2]
 in error at error.jl:21

Things become worse with more than one dimension, as nothing works as expected when slicing on the symbol or the integer variables (only selecting one symbol value works):

julia> y[:1,1]
y[1,1]

julia> y[:,1]
ERROR: Failed attempt to index JuMPArray along dimension 1: Colon() ∉ [1,2]
 in error at error.jl:21

julia> y[:1,:]
ERROR: Failed attempt to index JuMPArray along dimension 2: Colon() ∉ [1,2]
 in error at error.jl:21

julia> y[:,:]
ERROR: Failed attempt to index JuMPArray along dimension 1: Colon() ∉ [1,2]
 in error at error.jl:21

(The same tests fail on z when switching symbols and integers for indexing.)

To solve this issue, I am currently forced to use the sum{} construction, which is much less nice just to sum over a dimension (like sum(y[:1, :]).

@constraint(m, sum{z[h, g], g in gs} == 1)
@constraint(m, sum(z[h, :]) == 1)

Would this issue be similar to #833 or #730?

@mlubin
Copy link
Member

mlubin commented Sep 14, 2016

If you index with anything except a numeric range then you cannot slice or use any array-like properties.

@dourouc05
Copy link
Contributor Author

This does not make sense to me… Why then allow using anything else than integers for indexing, if it's just going to bite you the second after?

@mlubin
Copy link
Member

mlubin commented Sep 14, 2016

What kind of object would you expect y[:,1] to produce? Julia doesn't allow slicing over dictionaries:

julia> d = Dict(:a => 1, :b => 2);

julia> sum(d[:])
ERROR: KeyError: Colon() not found
 in getindex at dict.jl:738

@dourouc05
Copy link
Contributor Author

Why couldn't y[:,1] be of the same type as x? That would be the most intuitive solution to me. Exactly like for other cases that are not handled by basic Julia arrays:

julia> @variable(m, u[1:4, 0:4], Bin)
u[i,j] in {0,1} for all i in {1,2,3,4}, j in {0,1,..,3,4}

julia> typeof(u)
JuMP.JuMPArray{JuMP.Variable,2,Tuple{UnitRange{Int64},UnitRange{Int64}}}

julia> typeof(u[1, :])
Array{JuMP.Variable,2}

@mlubin
Copy link
Member

mlubin commented Sep 14, 2016

We've been heading in this direction, see #782. PRs accepted.

@mlubin
Copy link
Member

mlubin commented Sep 14, 2016

I believe this is a dup of #643

@dourouc05
Copy link
Contributor Author

OK, thanks! So I'm closing this, and having a look at these issues (and maybe send a PR, at least for a better error message).

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

2 participants