|
| 1 | +# Taken out of GenOpt, we can add ArrayDiff as dependency to GenOpt and remove it in GenOpt |
| 2 | + |
| 3 | +struct ArrayOfVariables{T,N} <: AbstractJuMPArray{JuMP.GenericVariableRef{T},N} |
| 4 | + model::JuMP.GenericModel{T} |
| 5 | + offset::Int64 |
| 6 | + size::NTuple{N,Int64} |
| 7 | +end |
| 8 | + |
| 9 | +const MatrixOfVariables{T} = ArrayOfVariables{T,2} |
| 10 | + |
| 11 | +Base.size(array::ArrayOfVariables) = array.size |
| 12 | +function Base.getindex(A::ArrayOfVariables{T}, I...) where {T} |
| 13 | + index = |
| 14 | + A.offset + Base._to_linear_index(Base.CartesianIndices(A.size), I...) |
| 15 | + return JuMP.GenericVariableRef{T}(A.model, MOI.VariableIndex(index)) |
| 16 | +end |
| 17 | + |
| 18 | +function JuMP.Containers.container( |
| 19 | + f::Function, |
| 20 | + indices::JuMP.Containers.VectorizedProductIterator{ |
| 21 | + NTuple{N,Base.OneTo{Int}}, |
| 22 | + }, |
| 23 | + ::Type{ArrayOfVariables}, |
| 24 | +) where {N} |
| 25 | + return to_generator(JuMP.Containers.container(f, indices, Array)) |
| 26 | +end |
| 27 | + |
| 28 | +JuMP._is_real(::ArrayOfVariables) = true |
| 29 | + |
| 30 | +function Base.convert( |
| 31 | + ::Type{ArrayOfVariables{T,N}}, |
| 32 | + array::Array{JuMP.GenericVariableRef{T},N}, |
| 33 | +) where {T,N} |
| 34 | + model = JuMP.owner_model(array[1]) |
| 35 | + offset = JuMP.index(array[1]).value - 1 |
| 36 | + for i in eachindex(array) |
| 37 | + @assert JuMP.owner_model(array[i]) === model |
| 38 | + @assert JuMP.index(array[i]).value == offset + i |
| 39 | + end |
| 40 | + return ArrayOfVariables{T,N}(model, offset, size(array)) |
| 41 | +end |
| 42 | + |
| 43 | +function to_generator(array::Array{JuMP.GenericVariableRef{T},N}) where {T,N} |
| 44 | + return convert(ArrayOfVariables{T,N}, array) |
| 45 | +end |
0 commit comments