help converting JuMPArray to regular array

#1

I’m trying to inspect the dual values of one of my constraints, but I cannot figure out how to convert the returned variable to a regular Array (so that I can ultimately save it to a CSV). I get a 3-dimensional JuMPArray when I call getdual for this constraint (but not for other constraints which have the same dimensions - a question for another time would be why this is happening?).

After solving, this is what I’ve tried -

> xx = getdual(ramplim)
> typeof(xx)
JuMP.JuMPArray{Float64,3,Tuple{UnitRange{Int64},Array{Int64,1},UnitRange{Int64}}}
> JuMP.size(xx)
(324, 9, 25)

I can extract a 1D array but not a 3D array. (I could work with 1D if I understood how it was flattened out, and reconstruct the 3D array… but I’d only do this as a last resort since I’d probably mess it up.)

> xxarray = xx[:]
72900-element Array{Float64,1}:
 0.0
 0.0
...
> xxarray = xx[:,:,:]
ERROR: Failed attempt to index JuMPArray along dimension 2: Colon() ∉ [2, 3, 4, 5, 6, 7, 8, 9, 10]
Stacktrace:
 [1] macro expansion at /home/users/pjlevi/.julia/v0.6/JuMP/src/JuMPArray.jl:100 [inlined]
 [2] getindex(::JuMP.JuMPArray{Float64,3,Tuple{UnitRange{Int64},Array{Int64,1},UnitRange{Int64}}}, ::Colon, ::Colon, ::Colon) at /home/users/pjlevi/.julia/v0.6/JuMP/src/JuMPArray.jl:40

I can’t just convert it either:

xx = convert(Array,rampl_shadow)
ERROR: MethodError: Cannot `convert` an object of type JuMP.JuMPArray{Float64,3,Tuple{UnitRange{Int64},Array{Int64,1},UnitRange{Int6
4}}} to an object of type Array
This may have arisen from a call to the constructor Array(...),
since type constructors fall back to convert methods.

In case it’s helpful: this is how the constraint in question is created

    @constraint(m,ramplim[g=GENERATORS,t=t_notfirst, o = SCENARIOS],
        p[g,t,o] - p[g,t-1,o] <= (rampmax[g] * pmax[g]))

Any guidance would be very appreciated! The ultimate goal here is to save the output as a CSV, but it would be nice to have it as an Array first so that in the future I can manipulate it first.

0 Likes

#2

Hi @levip,

Welcome to JuMP!

This isn’t really supported, since if you convert it you will lose the indices. As a work around, you can either access the underlying data (xx.data Note: this is an internal feature, so it may break at any point!).

A better approach is to just use the JuMPArray as much as possible. If you want to write it to CSV, you can always build an array manually.

I, J, K = axes(xx)
[xx[i,j,k] for i in I, j in J, k in K]

Good luck using JuMP!

p.s. in the future, it’s easier to help if you follow

0 Likes

#3

Try
xx = getdual(ramplim)[:,:,:]

0 Likes