-
-
Notifications
You must be signed in to change notification settings - Fork 399
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
Extend setvalue to JuMPArrays #540
Conversation
3c0b286
to
4a863b1
Compare
@@ -346,6 +346,10 @@ function setValue(v::Variable, val::Number) | |||
end | |||
end | |||
|
|||
setValue(set::Array{Variable}, val::Array) = map(setValue, set, val) | |||
|
|||
setValue(set::JuMPArray{Variable}, val::Array) = setValue(set.innerArray, val) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method only really makes sense for OneIndexedArray
. Otherwise you could do:
@defVar(m, x[[:a,:b,:c], 1:2])
setValue(x, [1,2,3,4,5,6])
which doesn't make too much sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your example gives me the error
ERROR: `setValue` has no method matching setValue(::JuMPDict{Variable,2}, ::Array{Int64,1})
in v0.3.12. Maybe there have been some changes in v0.4?
It seems like my restriction to JuMPArrays means this does not work with variables that use symbols in indices.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I see the method was for JuMPArray
, try this:
@defVar(m, x[5:7, 1:2])
setValue(x, [1,2,3,4,5,6])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems fine?
julia> @defVar(m, x[5:7, 1:2])
x[i,j] free for all i in {5,6,7}, j in {1,2}
julia> setValue(x, [1,2,3,4,5,6])
ERROR: dimensions must match
in promote_shape at operators.jl:191
in map at abstractarray.jl:1352
in setValue at /home/asbjorn/.julia/v0.3/JuMP/src/JuMP.jl:267
I should test before posting code. This code works without error:
I would argue that it's misleading to accept this as input if the variable Also returning an array of |
x0 = [1:3] | ||
@defVar(m, x[1:3]) | ||
setValue(x,x0) | ||
@fact getValue(x).innerArray --> x0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see from the build logs that getValue(x) returns an Array for that version of Julia+JuMP.
It runs fine on my setup, but maybe it's too outdated?
Julia 0.3.12-pre+3, JuMP 0.9.3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, on JuMP master getValue
returns a julia Array
when possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would also be good to add a test for the error case with mismatching dimensions.
accec91
to
8296505
Compare
Ah, I understand what you mean by restricting it to OneIndexedArray now ...
Added dimension mismatch test as well. |
|
Yes, a loop like for I in eachindex(x)
setValue(set[I], val[I])
end
nothing should do the trick. |
8296505
to
49a7990
Compare
|
I'd rather remove this entirely.
Note travis is failing on
May need to special-case that test for 0.3 and 0.4. |
49a7990
to
c72f31c
Compare
I removed the JuMPArray call and added a 0.4 case. |
m = Model() | ||
@defVar(m, x[1:3]) | ||
x0 = [1:3] | ||
setValue(x.innerArray, x0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be setValue(x, x0)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed setValue(::JuMPArray, ...) as you wanted, so it is no longer possible to run setValue(x, x0).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I see from the test logs that your Julia/JuMP version is not happy with calling .innerArray. Whilst my setup is not happy with setValue(x, x0) ...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, this behavior changed in #548 about 16 hours ago :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're running with JuMP master, setValue(x, x0)
will work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah, ok - I should learn to rebase on upstream master more often.
Looks good to me! Thanks. We can merge after CI passes. |
c72f31c
to
6e05fd9
Compare
Thanks! |
This commit enables us to run
for an array of numbers, x0.
Hope I put the code in the right place?