Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 42 additions & 12 deletions src/JuMP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,63 @@ 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
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)
Expand All @@ -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

Expand Down Expand Up @@ -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}
Expand All @@ -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}
Expand Down Expand Up @@ -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}
Expand Down
4 changes: 2 additions & 2 deletions src/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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{
Expand Down
103 changes: 103 additions & 0 deletions test/JuMP_wrapper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading