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

Inconsistency between keys and the operator [] of JuMP.JuMPArray #819

Closed
IssamT opened this issue Aug 3, 2016 · 2 comments
Closed

Inconsistency between keys and the operator [] of JuMP.JuMPArray #819

IssamT opened this issue Aug 3, 2016 · 2 comments

Comments

@IssamT
Copy link
Contributor

IssamT commented Aug 3, 2016

For a binary variable that was created like this:

I = 1:10
@variable(m, x[v in I], Bin)

...
solve(m)
b = getvalue(x)

This function, I wrote, crashes when I provide b as an argument

function bin_decisions(b::JuMP.JuMPArray)
    ret = []
    for v in keys(b)
        b[v] == 1.0 && push!(ret,v)
    end
    ret
end

If I understood well the JuMPArray in this case is indexed with a tuple that contains one element, which I'm fine with, but the issue is that to access an element in b you can t use the 1-element tuple as index => inconsistency between keys and the operator [].

Basically the function that works is this one.

function bin_decisions(b::JuMP.JuMPArray)
    ret = []
    for v in keys(b)
        b[v[1]] == 1.0 && push!(ret,v)
    end
    ret
end

I am quite new to julia and in particual to JuMP. So, I would like just a confirmation that this is a bug and I will try to find the error and suggest a fix.

@joehuchette
Copy link
Contributor

joehuchette commented Aug 3, 2016

This is closely related to #646. This is a thorny issue and we're not entirely happy with the current implementation, but we also don't have a better solution that avoids the ambiguity here (suggestions welcome). Regardless, this code should work as well:

function bin_decisions(b::JuMP.JuMPArray)
    ret = []
    for v in keys(b)
        b[v...] == 1.0 && push!(ret,v)
    end
    ret
end

(though I haven't tested).

@IssamT
Copy link
Contributor Author

IssamT commented Aug 3, 2016

Thanks for the fast reply. I confirm that your code works as well and is more generic than my solution.
The fact that my solution was not generic is the thing that annoyed me the most since I am not a language purist.

However I will think about a way to fix this inconsistency between keys and the operator[] and if I have a suggestion I will provide it in the thread #646 because it is indeed the same issue. So feel free to close this one.

@mlubin mlubin closed this as completed Aug 3, 2016
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