diff --git a/src/JuMP_wrapper.jl b/src/JuMP_wrapper.jl index 5bb6130..e9f0fdf 100644 --- a/src/JuMP_wrapper.jl +++ b/src/JuMP_wrapper.jl @@ -27,17 +27,45 @@ JuMP._is_real(::Union{IteratorInExpr,IteratorIndex}) = true JuMP.moi_function(i::Union{IteratorInExpr,IteratorIndex}) = i JuMP.jump_function(_, i::Union{IteratorInExpr,IteratorIndex}) = i -struct ArrayOfVariables{T,N} <: AbstractArray{JuMP.GenericVariableRef{T},N} - model::JuMP.GenericModel{T} +struct ArrayOfVariables{ + T, + N, + V<:JuMP.AbstractVariableRef, + M<:JuMP.AbstractModel, +} <: AbstractArray{V,N} + model::M offset::Int64 size::NTuple{N,Int64} end +function ArrayOfVariables{T,N}( + model::M, + offset::Int64, + size::NTuple{N,Int64}, +) where {T,N,M<:JuMP.AbstractModel} + T == JuMP.value_type(M) || + throw(ArgumentError("Array coefficient type must match its model")) + V = JuMP.variable_ref_type(M) + return ArrayOfVariables{T,N,V,M}(model, offset, size) +end + +function ArrayOfVariables( + model::JuMP.AbstractModel, + offset::Int64, + size::NTuple{N,Int64}, +) where {N} + return ArrayOfVariables{JuMP.value_type(typeof(model)),N}( + model, + offset, + size, + ) +end + Base.size(array::ArrayOfVariables) = array.size -function Base.getindex(A::ArrayOfVariables{T}, I...) where {T} +function Base.getindex(A::ArrayOfVariables{T,N,V}, I...) where {T,N,V} index = A.offset + Base._to_linear_index(Base.CartesianIndices(A.size), I...) - return JuMP.GenericVariableRef{T}(A.model, MOI.VariableIndex(index)) + return V(A.model, MOI.VariableIndex(index)) end JuMP._is_real(::ArrayOfVariables) = true @@ -45,16 +73,17 @@ function JuMP.moi_function(array::ArrayOfVariables) return ContiguousArrayOfVariables(array.offset, array.size) end function JuMP.jump_function( - model::JuMP.GenericModel{T}, + model::JuMP.AbstractModel, array::ContiguousArrayOfVariables{N}, -) where {T,N} +) where {N} + T = JuMP.value_type(typeof(model)) return ArrayOfVariables{T,N}(model, array.offset, array.size) end function Base.convert( ::Type{ArrayOfVariables{T,N}}, - array::Array{JuMP.GenericVariableRef{T},N}, -) where {T,N} + array::Array{V,N}, +) where {T,N,V<:JuMP.AbstractVariableRef} model = JuMP.owner_model(array[1]) offset = JuMP.index(array[1]).value - 1 for i in eachindex(array) @@ -64,7 +93,8 @@ function Base.convert( return ArrayOfVariables{T,N}(model, offset, size(array)) end -function to_generator(array::Array{JuMP.GenericVariableRef{T},N}) where {T,N} +function to_generator(array::Array{V,N}) where {V<:JuMP.AbstractVariableRef,N} + T = JuMP.value_type(V) return convert(ArrayOfVariables{T,N}, array) end @@ -96,7 +126,7 @@ end # Vector{<:AbstractJuMPScalar}}}, ::Type{<:MOI.AbstractSet})`, which `show(model)` calls for # every type of `list_of_constraint_types`. function JuMP.num_constraints( - model::JuMP.GenericModel, + model::JuMP.AbstractModel, ::Type{F}, ::Type{S}, ) where {F<:ExprGenerator,S<:MOI.AbstractSet} @@ -105,7 +135,7 @@ function JuMP.num_constraints( end function JuMP.all_constraints( - model::JuMP.GenericModel, + model::JuMP.AbstractModel, ::Type{F}, ::Type{S}, ) where {F<:ExprGenerator,S<:MOI.AbstractVectorSet} @@ -242,7 +272,7 @@ _bound_values(x::Real, n) = fill(float(x), n) struct IteratedConstraint{ E, - V<:JuMP.GenericVariableRef, + V<:JuMP.AbstractVariableRef, S<:MOI.AbstractVectorSet, } <: JuMP.AbstractConstraint func::ExprGenerator{E,V} diff --git a/src/operators.jl b/src/operators.jl index d0f95c3..97cdf13 100644 --- a/src/operators.jl +++ b/src/operators.jl @@ -296,7 +296,7 @@ function JuMP.moi_function(s::LazySum{E}) where {E} end function JuMP.jump_function_type( - model::JuMP.GenericModel, + model::JuMP.AbstractModel, ::Type{SumGenerator{F}}, ) where {F} return LazySum{ @@ -314,7 +314,7 @@ function JuMP.moi_function(s::FilteredLazySum{E}) where {E} end function JuMP.jump_function_type( - model::JuMP.GenericModel, + model::JuMP.AbstractModel, ::Type{FilteredSumGenerator{F}}, ) where {F} return FilteredLazySum{ diff --git a/test/JuMP_wrapper.jl b/test/JuMP_wrapper.jl index ff759e4..2c21b87 100644 --- a/test/JuMP_wrapper.jl +++ b/test/JuMP_wrapper.jl @@ -24,6 +24,109 @@ function runtests() return end +# A minimal JuMP extension keeps the model/backend and variable-reference types +# concrete without requiring a development version of JuMP. +struct ExtensionModel{T,B<:MOI.ModelLike} <: JuMP.AbstractModel + backend::B +end + +struct ExtensionVariable{M<:ExtensionModel} <: JuMP.AbstractVariableRef + model::M + index::MOI.VariableIndex +end + +JuMP.value_type(::Type{<:ExtensionModel{T}}) where {T} = T +JuMP.value_type(::Type{ExtensionVariable{M}}) where {M} = JuMP.value_type(M) +function JuMP.variable_ref_type(::Type{M}) where {M<:ExtensionModel} + return ExtensionVariable{M} +end +JuMP.owner_model(variable::ExtensionVariable) = variable.model +JuMP.index(variable::ExtensionVariable) = variable.index +JuMP.backend(model::ExtensionModel) = model.backend +function JuMP.jump_function_type( + model::ExtensionModel, + ::Type{MOI.VariableIndex}, +) + return JuMP.variable_ref_type(model) +end +function MOI.get(model::ExtensionModel, attr::MOI.AbstractModelAttribute) + return MOI.get(JuMP.backend(model), attr) +end +function JuMP.constraint_ref_with_index( + model::ExtensionModel, + index::MOI.ConstraintIndex{ + <:MOI.AbstractVectorFunction, + <:MOI.AbstractVectorSet, + }, +) + return JuMP.ConstraintRef(model, index, JuMP.VectorShape()) +end + +function test_extension_variable_arrays() + for T in (Float32, Float64) + backend = MOI.Utilities.UniversalFallback(MOI.Utilities.Model{T}()) + model = ExtensionModel{T,typeof(backend)}(backend) + V = JuMP.variable_ref_type(model) + variables = [V(model, index) for index in MOI.add_variables(backend, 4)] + array = reshape(variables, 2, 2) + generated = GenOpt.to_generator(array) + @test generated isa GenOpt.ArrayOfVariables{T,2,V,typeof(model)} + @test typeof(GenOpt.ArrayOfVariables(model, 0, size(array))) === + typeof(generated) + other_type = T === Float32 ? Float64 : Float32 + @test_throws ArgumentError GenOpt.ArrayOfVariables{other_type,2}( + model, + 0, + size(array), + ) + @test eltype(generated) === V + @test generated.model === model + @test size(generated) == (2, 2) + @test JuMP.index(generated[2, 1]) == JuMP.index(array[2, 1]) + @test JuMP.owner_model(generated[1, 2]) === model + + moi_array = JuMP.moi_function(generated) + @test moi_array isa GenOpt.ContiguousArrayOfVariables{2} + restored = JuMP.jump_function(model, moi_array) + @test typeof(restored) === typeof(generated) + @test restored.model === model + @test [JuMP.index(restored[i, j]) for i in 1:2, j in 1:2] == JuMP.index.(array) + + i = GenOpt.iterator(1:2) + template = array[i, 1] + @test template isa GenOpt.ExprTemplate{V,V} + generator = GenOpt.ExprGenerator(template) + constraint = + JuMP.build_constraint(error, generator, MOI.Nonnegatives(2)) + @test constraint isa GenOpt.IteratedConstraint{V,V,MOI.Nonnegatives} + F = GenOpt.FunctionGenerator{MOI.VariableIndex} + @test JuMP.moi_function_type(typeof(generator)) === F + @test JuMP.jump_function_type(model, F) === typeof(generator) + @test JuMP.jump_function_type( + model, + GenOpt.SumGenerator{MOI.VariableIndex}, + ) === GenOpt.LazySum{V,V} + @test JuMP.jump_function_type( + model, + GenOpt.FilteredSumGenerator{MOI.VariableIndex}, + ) === GenOpt.FilteredLazySum{V,V} + + function_ = JuMP.moi_function(generator) + index = MOI.add_constraint(backend, function_, constraint.set) + @test JuMP.num_constraints( + model, + typeof(generator), + MOI.Nonnegatives, + ) == 1 + references = + JuMP.all_constraints(model, typeof(generator), MOI.Nonnegatives) + @test length(references) == 1 + @test JuMP.index(only(references)) == index + @test JuMP.owner_model(only(references)) === model + end + return +end + function test_container() model = Model() @variable(model, x)