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

getValue of an array of Variables #118

Closed
vgupta1 opened this issue Mar 4, 2014 · 17 comments
Closed

getValue of an array of Variables #118

vgupta1 opened this issue Mar 4, 2014 · 17 comments

Comments

@vgupta1
Copy link

vgupta1 commented Mar 4, 2014

This code seems to choke with error:
ERROR: no method getValue(Array{Variable,1},)

A small example is:

m = Model()
@defVar(m, x[1:5, 1:5] >= 0)
for i = 1:5
    @addConstraint(m, sum{x[i, j], j=1:5} == 1)
    @addConstraint(m, sum{x[j, i], j=1:5} == 1)
end

costs = rand(Normal(10, 2), 5, 5)
@setObjective(m, Min, sum{costs[i, j] * x[i, j], i=1:5, j=1:5})

solve(m)

#This chokes
getValue(x[1:5, 1])
joehuchette added a commit that referenced this issue Mar 4, 2014
@IainNZ IainNZ reopened this Mar 4, 2014
@IainNZ
Copy link
Collaborator

IainNZ commented Mar 4, 2014

That probably isn't a great general solution, e.g. consider x[1:5,2:3]

@joehuchette
Copy link
Contributor

Why? If you pass in an array of Variables, it makes sense to get an array of same size back

julia> getValue(x[1:5,2:3])
5x2 Array{Float64,2}:
 0.0  1.0
 0.0  0.0
 0.0  0.0
 1.0  0.0
 0.0  0.0

@IainNZ
Copy link
Collaborator

IainNZ commented Mar 4, 2014

what if you mix ranges slicing with arbitrary sets?

@IainNZ
Copy link
Collaborator

IainNZ commented Mar 4, 2014

I just feel like getting a JuMPDict back is probably the right thing to do

@IainNZ
Copy link
Collaborator

IainNZ commented Mar 4, 2014

or even better, throw errors, and slice the result of getValue(x)

@joehuchette
Copy link
Contributor

I see what you're getting at, but it seems silly to construct a JuMPDict when the argument is an array. What about

julia> @defVar(m,x)

julia> @defVar(m,y)

julia> solve(m)
:Optimal

julia> getValue([x,y])
2-element Array{Float64,1}:
 0.0
 0.0

@joehuchette
Copy link
Contributor

I could get behind throwing an error, but I don't find anything particularly egregious about this behavior

@IainNZ
Copy link
Collaborator

IainNZ commented Mar 4, 2014

How about

@defVar(m, x[-10:10])
getValue(x)[-10]   # works fine, getValue(x::JuMPDicT) returns JuMPDict
getValue(x[-10])   # works fine, getValue(x::Variable) returns Float64
getValue(x[-3:+3])   # dies, but with your patch returns a vector of floats
getValue(x[-3:+3])[-3]   # dies... surprising? I'm not sure

@joehuchette
Copy link
Contributor

Since you can't do x[-3:+3][-3] to begin with, I don't find this particularly surprising. Would it be more coherent for slices of a JuMPDict to return a JuMPDict? Then both x[-3:+3][-3] and getValue(x[-3:+3])[-3] would work.

@IainNZ
Copy link
Collaborator

IainNZ commented Mar 4, 2014

Thats what I think, or alternatively x[-3:+3] is an error - no ambiguity possible.

@joehuchette
Copy link
Contributor

Would there be any obvious performance reasons not to return a JuMPDict?

@vgupta1
Copy link
Author

vgupta1 commented Mar 4, 2014

Iain, when you wrote:
Thats what I think, or alternatively x[-3:+3] is an error - no ambiguity possible.

Are you suggesting that getValue(x[-10:10]) would work, but no other slicing would be possible? I find that sort of weird and sad...

@IainNZ
Copy link
Collaborator

IainNZ commented Mar 5, 2014

No, I'd mean that no slicing would work on JuMPDicts

so

getValue(x[single,single,single])  # works
getValue(x)[whatever slice you want]  # works
getValue(x[slice])  # throws error - and can tell you to do getValue(x)[slice]

@joehuchette
Copy link
Contributor

I'm still not entirely sure about this use case, though. If you're slicing into an array you have no expectation that the original indexing is preserved:

julia> A = rand(3,3)
julia> A[2:3,2:3][3,3] # no dice

Why should you have that expectation for JuMPDicts? It seems that if you need your crazy indexing somewhere down the pipeline, you should just work on the original JuMPDict.

@IainNZ
Copy link
Collaborator

IainNZ commented Mar 6, 2014

You know, more I think about this, more around in circles I go
I'd expect (or hope)

m=Model()
@defVar(m, x[1:3,1:10])
a = rand(3, 10)
b = rand(3)
for i = 1:3
  @addConstraint( dot(a, x[i,:]) <= b[i] )
end

to work. It doesn't right now, because of how x[i,:] returns an array of variables. So I see two paths forward:

  • Slicing JuMPDicts gives JuMPDicts (harder, maybe not so efficient)
  • We define convenience functions that work on JuMPDicts to also work on Array (easy)

@mlubin
Copy link
Member

mlubin commented Mar 6, 2014

We can and should define dot on arrays of variables. On slicing JuMPDicts, I'm convinced by @joehuchette's point that slicing a plain array doesn't preserve the indexing, so there's no expectation that it should be the same for JuMPDicts. At the same time, slicing a dictionary doesn't make sense, so it should be an error to slice over any index set that isn't ordered.

@joehuchette
Copy link
Contributor

I agree about dot working on arrays; that's arguably even more natural than on JuMPDicts, since a is an array itself. I would argue that this should even be the "base" method, and that dot(a,b::JuMPDict) just calls dot(a,b::Array) if certain conditions are met (no unordered sets, like @mlubin mentioned).

@IainNZ IainNZ mentioned this issue Mar 10, 2014
@IainNZ IainNZ closed this as completed in 249a449 Mar 10, 2014
mlubin added a commit that referenced this issue Mar 10, 2014
blegat pushed a commit that referenced this issue Oct 16, 2018
Fix handling of MIQP problem type. Fixes #115
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

4 participants